This commit is contained in:
fourtf
2018-05-23 04:22:17 +02:00
parent bf560d37bd
commit dafbda6a4a
22 changed files with 404 additions and 1103 deletions
+32 -308
View File
@@ -89,6 +89,8 @@ void Notebook2::removePage(QWidget *page)
break;
}
}
this->performLayout();
}
void Notebook2::removeCurrentPage()
@@ -179,6 +181,11 @@ int Notebook2::getPageCount() const
return this->items.count();
}
QWidget *Notebook2::getPageAt(int index) const
{
return this->items[index].page;
}
int Notebook2::getSelectedIndex() const
{
return this->indexOf(this->selectedPage);
@@ -242,11 +249,11 @@ void Notebook2::setShowAddButton(bool value)
void Notebook2::scaleChangedEvent(float scale)
{
// float h = 24 * this->getScale();
float h = NOTEBOOK_TAB_HEIGHT * this->getScale();
// this->settingsButton.setFixedSize(h, h);
// this->userButton.setFixedSize(h, h);
// this->addButton.setFixedSize(h, h);
this->addButton.setFixedSize(h, h);
for (auto &i : this->items) {
i.tab->updateSize();
@@ -290,10 +297,12 @@ void Notebook2::performLayout(bool animated)
// x += (int)(scale * 2);
// }
int tabHeight = static_cast<int>(24 * scale);
int tabHeight = static_cast<int>(NOTEBOOK_TAB_HEIGHT * scale);
bool first = true;
for (auto i = this->items.begin(); i != this->items.end(); i++) {
// int yOffset = i->tab->isSelected() ? 0 : 1;
if (!first &&
(i == this->items.end() && this->showAddButton ? tabHeight : 0) + x + i->tab->width() >
width()) //
@@ -321,7 +330,7 @@ void Notebook2::performLayout(bool animated)
this->update();
}
y += (int)(1 * scale);
y += (int)(3 * scale);
for (auto &i : this->items) {
i.tab->raise();
@@ -343,10 +352,15 @@ void Notebook2::paintEvent(QPaintEvent *event)
BaseWidget::paintEvent(event);
QPainter painter(this);
painter.fillRect(0, this->lineY, this->width(), (int)(1 * this->getScale()),
painter.fillRect(0, this->lineY, this->width(), (int)(3 * this->getScale()),
this->themeManager->tabs.bottomLine);
}
NotebookButton *Notebook2::getAddButton()
{
return &this->addButton;
}
NotebookTab2 *Notebook2::getTabFromPage(QWidget *page)
{
for (auto &it : this->items) {
@@ -358,318 +372,28 @@ NotebookTab2 *Notebook2::getTabFromPage(QWidget *page)
return nullptr;
}
// Notebook2::OLD NOTEBOOK
Notebook::Notebook(Window *parent, bool _showButtons)
: BaseWidget(parent)
, parentWindow(parent)
, addButton(this)
, settingsButton(this)
, userButton(this)
, showButtons(_showButtons)
, closeConfirmDialog(this)
SplitNotebook::SplitNotebook(QWidget *parent)
: Notebook2(parent)
{
auto app = getApp();
this->connect(&this->settingsButton, SIGNAL(clicked()), this, SLOT(settingsButtonClicked()));
this->connect(&this->userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked()));
this->connect(&this->addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked()));
this->settingsButton.icon = NotebookButton::IconSettings;
this->userButton.move(24, 0);
this->userButton.icon = NotebookButton::IconUser;
app->settings->hidePreferencesButton.connectSimple([this](auto) { this->performLayout(); });
app->settings->hideUserButton.connectSimple([this](auto) { this->performLayout(); });
closeConfirmDialog.setText("Are you sure you want to close this tab?");
closeConfirmDialog.setIcon(QMessageBox::Icon::Question);
closeConfirmDialog.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
closeConfirmDialog.setDefaultButton(QMessageBox::Yes);
this->scaleChangedEvent(this->getScale());
// Window-wide hotkeys
// CTRL+T: Create new split in selected notebook page
// CreateWindowShortcut(this, "CTRL+T", [this]() {
// if (this->selectedPage == nullptr) {
// return;
// }
// this->selectedPage->addChat(true);
// });
this->connect(this->getAddButton(), &NotebookButton::clicked,
[this]() { QTimer::singleShot(80, this, [this] { this->addPage(true); }); });
}
SplitContainer *Notebook::addNewPage(bool select)
SplitContainer *SplitNotebook::addPage(bool select)
{
auto tab = new NotebookTab(this);
auto page = new SplitContainer(this, tab);
SplitContainer *container = new SplitContainer(this);
auto *tab = Notebook2::addPage(container, QString(), select);
container->setTab(tab);
tab->setParent(this);
tab->show();
if (select || this->pages.count() == 0) {
this->select(page);
}
this->pages.append(page);
this->performLayout();
return page;
return container;
}
void Notebook::removePage(SplitContainer *page)
SplitContainer *SplitNotebook::getOrAddSelectedPage()
{
if (page->getSplitCount() > 0 && closeConfirmDialog.exec() != QMessageBox::Yes) {
return;
}
auto *selectedPage = this->getSelectedPage();
int index = this->pages.indexOf(page);
if (this->pages.size() == 1) {
select(nullptr);
} else if (index == this->pages.count() - 1) {
select(this->pages[index - 1]);
} else {
select(this->pages[index + 1]);
}
page->getTab()->deleteLater();
page->deleteLater();
this->pages.removeOne(page);
if (this->pages.empty()) {
this->addNewPage();
}
this->performLayout();
}
void Notebook::removeCurrentPage()
{
if (this->selectedPage == nullptr) {
return;
}
this->removePage(this->selectedPage);
}
SplitContainer *Notebook::getOrAddSelectedPage()
{
if (selectedPage == nullptr) {
this->addNewPage(true);
}
return selectedPage;
}
SplitContainer *Notebook::getSelectedPage()
{
return selectedPage;
}
void Notebook::select(SplitContainer *page)
{
if (page == this->selectedPage) {
return;
}
if (page != nullptr) {
page->setHidden(false);
page->getTab()->setSelected(true);
page->getTab()->raise();
}
if (this->selectedPage != nullptr) {
this->selectedPage->setHidden(true);
this->selectedPage->getTab()->setSelected(false);
for (Split *split : this->selectedPage->getSplits()) {
split->updateLastReadMessage();
}
}
this->selectedPage = page;
this->performLayout();
}
void Notebook::selectIndex(int index)
{
if (index < 0 || index >= this->pages.size()) {
return;
}
this->select(this->pages.at(index));
}
int Notebook::tabCount()
{
return this->pages.size();
}
SplitContainer *Notebook::tabAt(QPoint point, int &index, int maxWidth)
{
int i = 0;
for (auto *page : this->pages) {
QRect rect = page->getTab()->getDesiredRect();
rect.setHeight((int)(this->getScale() * 24));
rect.setWidth(std::min(maxWidth, rect.width()));
if (rect.contains(point)) {
index = i;
return page;
}
i++;
}
index = -1;
return nullptr;
}
SplitContainer *Notebook::tabAt(int index)
{
return this->pages[index];
}
void Notebook::rearrangePage(SplitContainer *page, int index)
{
this->pages.move(this->pages.indexOf(page), index);
this->performLayout();
}
void Notebook::nextTab()
{
if (this->pages.size() <= 1) {
return;
}
int index = (this->pages.indexOf(this->selectedPage) + 1) % this->pages.size();
this->select(this->pages[index]);
}
void Notebook::previousTab()
{
if (this->pages.size() <= 1) {
return;
}
int index = (this->pages.indexOf(this->selectedPage) - 1);
if (index < 0) {
index += this->pages.size();
}
this->select(this->pages[index]);
}
void Notebook::performLayout(bool animated)
{
auto app = getApp();
int x = 0, y = 0;
float scale = this->getScale();
bool customFrame = this->parentWindow->hasCustomWindowFrame();
if (!this->showButtons || app->settings->hidePreferencesButton || customFrame) {
this->settingsButton.hide();
} else {
this->settingsButton.show();
x += settingsButton.width();
}
if (!this->showButtons || app->settings->hideUserButton || customFrame) {
this->userButton.hide();
} else {
this->userButton.move(x, 0);
this->userButton.show();
x += userButton.width();
}
if (customFrame || !this->showButtons ||
(app->settings->hideUserButton && app->settings->hidePreferencesButton)) {
x += (int)(scale * 2);
}
int tabHeight = static_cast<int>(24 * scale);
bool first = true;
for (auto &i : this->pages) {
if (!first &&
(i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) {
y += i->getTab()->height();
// y += 20;
i->getTab()->moveAnimated(QPoint(0, y), animated);
x = i->getTab()->width();
} else {
i->getTab()->moveAnimated(QPoint(x, y), animated);
x += i->getTab()->width();
}
// x -= (int)(8 * scale);
x += 1;
first = false;
}
// x += (int)(8 * scale);
// x += 1;
this->addButton.move(x, y);
y -= 1;
for (auto &i : this->pages) {
i->getTab()->raise();
}
this->addButton.raise();
if (this->selectedPage != nullptr) {
this->selectedPage->move(0, y + tabHeight);
this->selectedPage->resize(width(), height() - y - tabHeight);
this->selectedPage->raise();
}
}
void Notebook::resizeEvent(QResizeEvent *)
{
this->performLayout(false);
}
void Notebook::scaleChangedEvent(float)
{
float h = 24 * this->getScale();
this->settingsButton.setFixedSize(h, h);
this->userButton.setFixedSize(h, h);
this->addButton.setFixedSize(h, h);
for (auto &i : this->pages) {
i->getTab()->updateSize();
}
}
void Notebook::settingsButtonClicked()
{
auto app = getApp();
app->windows->showSettingsDialog();
}
void Notebook::usersButtonClicked()
{
auto app = getApp();
app->windows->showAccountSelectPopup(this->mapToGlobal(this->userButton.rect().bottomRight()));
}
void Notebook::addPageButtonClicked()
{
QTimer::singleShot(80, [this] { this->addNewPage(true); });
return selectedPage != nullptr ? (SplitContainer *)selectedPage : this->addPage();
}
} // namespace widgets