2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/notebook.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2017-12-19 02:16:01 +01:00
|
|
|
#include "debug/log.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/thememanager.hpp"
|
2018-01-24 15:08:22 +01:00
|
|
|
#include "singletons/windowmanager.hpp"
|
2017-11-12 17:21:50 +01:00
|
|
|
#include "widgets/helper/notebookbutton.hpp"
|
|
|
|
#include "widgets/helper/notebooktab.hpp"
|
2018-04-07 12:27:08 +02:00
|
|
|
#include "widgets/helper/shortcut.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/settingsdialog.hpp"
|
2017-11-12 17:21:50 +01:00
|
|
|
#include "widgets/splitcontainer.hpp"
|
|
|
|
#include "widgets/window.hpp"
|
2016-12-29 17:31:07 +01:00
|
|
|
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QFile>
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QFormLayout>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QList>
|
2017-05-29 21:26:55 +02:00
|
|
|
#include <QShortcut>
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <QStandardPaths>
|
2017-12-22 14:44:31 +01:00
|
|
|
#include <QUuid>
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QWidget>
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2017-01-18 04:52:47 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
Notebook2::Notebook2(QWidget *parent)
|
2018-04-27 22:11:19 +02:00
|
|
|
: BaseWidget(parent)
|
2018-04-18 09:12:29 +02:00
|
|
|
, addButton(this)
|
|
|
|
{
|
|
|
|
this->addButton.setHidden(true);
|
|
|
|
|
|
|
|
auto *shortcut_next = new QShortcut(QKeySequence("Ctrl+Tab"), this);
|
|
|
|
QObject::connect(shortcut_next, &QShortcut::activated, [this] { this->selectNextTab(); });
|
|
|
|
|
|
|
|
auto *shortcut_prev = new QShortcut(QKeySequence("Ctrl+Shift+Tab"), this);
|
|
|
|
QObject::connect(shortcut_prev, &QShortcut::activated, [this] { this->selectPreviousTab(); });
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:51:53 +02:00
|
|
|
NotebookTab2 *Notebook2::addPage(QWidget *page, QString title, bool select)
|
2018-04-18 09:12:29 +02:00
|
|
|
{
|
|
|
|
auto *tab = new NotebookTab2(this);
|
|
|
|
tab->page = page;
|
|
|
|
|
2018-04-18 17:51:53 +02:00
|
|
|
if (!title.isEmpty()) {
|
|
|
|
tab->setTitle(title);
|
|
|
|
tab->useDefaultTitle = false;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
Item item;
|
|
|
|
item.page = page;
|
|
|
|
item.tab = tab;
|
|
|
|
|
|
|
|
this->items.append(item);
|
|
|
|
|
|
|
|
page->hide();
|
|
|
|
page->setParent(this);
|
|
|
|
|
|
|
|
if (select || this->items.count() == 1) {
|
|
|
|
this->select(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->performLayout();
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::removePage(QWidget *page)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < this->items.count(); i++) {
|
|
|
|
if (this->items[i].page == page) {
|
|
|
|
if (this->items.count() == 1) {
|
|
|
|
this->select(nullptr);
|
|
|
|
} else if (i == this->items.count() - 1) {
|
|
|
|
this->select(this->items[i - 1].page);
|
|
|
|
} else {
|
|
|
|
this->select(this->items[i + 1].page);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->items[i].page->deleteLater();
|
|
|
|
this->items[i].tab->deleteLater();
|
|
|
|
|
|
|
|
// if (this->items.empty()) {
|
|
|
|
// this->addNewPage();
|
|
|
|
// }
|
|
|
|
|
|
|
|
this->items.removeAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 04:22:17 +02:00
|
|
|
|
|
|
|
this->performLayout();
|
2018-04-18 09:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::removeCurrentPage()
|
|
|
|
{
|
|
|
|
if (this->selectedPage != nullptr) {
|
|
|
|
this->removePage(this->selectedPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Notebook2::indexOf(QWidget *page) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < this->items.count(); i++) {
|
|
|
|
if (this->items[i].page == page) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::select(QWidget *page)
|
|
|
|
{
|
|
|
|
if (page == this->selectedPage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page != nullptr) {
|
|
|
|
page->setHidden(false);
|
|
|
|
|
|
|
|
NotebookTab2 *tab = this->getTabFromPage(page);
|
|
|
|
tab->setSelected(true);
|
|
|
|
tab->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->selectedPage != nullptr) {
|
|
|
|
this->selectedPage->setHidden(true);
|
|
|
|
|
|
|
|
NotebookTab2 *tab = this->getTabFromPage(selectedPage);
|
|
|
|
tab->setSelected(false);
|
|
|
|
|
|
|
|
// for (auto split : this->selectedPage->getSplits()) {
|
|
|
|
// split->updateLastReadMessage();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
this->selectedPage = page;
|
|
|
|
|
|
|
|
this->performLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::selectIndex(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || this->items.count() <= index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->select(this->items[index].page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::selectNextTab()
|
|
|
|
{
|
|
|
|
if (this->items.size() <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = (this->indexOf(this->selectedPage) + 1) % this->items.count();
|
|
|
|
|
|
|
|
this->select(this->items[index].page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::selectPreviousTab()
|
|
|
|
{
|
|
|
|
if (this->items.size() <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = this->indexOf(this->selectedPage) - 1;
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
index += this->items.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->select(this->items[index].page);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Notebook2::getPageCount() const
|
|
|
|
{
|
|
|
|
return this->items.count();
|
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
QWidget *Notebook2::getPageAt(int index) const
|
|
|
|
{
|
|
|
|
return this->items[index].page;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
int Notebook2::getSelectedIndex() const
|
|
|
|
{
|
|
|
|
return this->indexOf(this->selectedPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *Notebook2::getSelectedPage() const
|
|
|
|
{
|
|
|
|
return this->selectedPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *Notebook2::tabAt(QPoint point, int &index, int maxWidth)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (auto &item : this->items) {
|
|
|
|
QRect rect = item.tab->getDesiredRect();
|
|
|
|
rect.setHeight((int)(this->getScale() * 24));
|
|
|
|
|
|
|
|
rect.setWidth(std::min(maxWidth, rect.width()));
|
|
|
|
|
|
|
|
if (rect.contains(point)) {
|
|
|
|
index = i;
|
|
|
|
return item.page;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = -1;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::rearrangePage(QWidget *page, int index)
|
|
|
|
{
|
|
|
|
this->items.move(this->indexOf(page), index);
|
|
|
|
|
|
|
|
this->performLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Notebook2::getAllowUserTabManagement() const
|
|
|
|
{
|
|
|
|
return this->allowUserTabManagement;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::setAllowUserTabManagement(bool value)
|
|
|
|
{
|
|
|
|
this->allowUserTabManagement = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Notebook2::getShowAddButton() const
|
|
|
|
{
|
|
|
|
return this->showAddButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::setShowAddButton(bool value)
|
|
|
|
{
|
|
|
|
this->showAddButton = value;
|
|
|
|
|
|
|
|
this->addButton.setHidden(!value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::scaleChangedEvent(float scale)
|
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
float h = NOTEBOOK_TAB_HEIGHT * this->getScale();
|
2018-04-18 09:12:29 +02:00
|
|
|
|
|
|
|
// this->settingsButton.setFixedSize(h, h);
|
|
|
|
// this->userButton.setFixedSize(h, h);
|
2018-05-23 04:22:17 +02:00
|
|
|
this->addButton.setFixedSize(h, h);
|
2018-04-18 09:12:29 +02:00
|
|
|
|
|
|
|
for (auto &i : this->items) {
|
|
|
|
i.tab->updateSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
|
|
|
this->performLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::performLayout(bool animated)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-04-18 09:12:29 +02:00
|
|
|
|
|
|
|
int xStart = (int)(2 * this->getScale());
|
|
|
|
|
|
|
|
int x = xStart, y = 0;
|
|
|
|
float scale = this->getScale();
|
|
|
|
|
|
|
|
// bool customFrame = this->parentWindow->hasCustomWindowFrame();
|
|
|
|
|
|
|
|
// bool customFrame = false;
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
// if (!this->showButtons || app->settings->hidePreferencesButton || customFrame) {
|
2018-04-18 09:12:29 +02:00
|
|
|
// this->settingsButton.hide();
|
|
|
|
// } else {
|
|
|
|
// this->settingsButton.show();
|
|
|
|
// x += settingsButton.width();
|
|
|
|
// }
|
2018-04-27 22:11:19 +02:00
|
|
|
// if (!this->showButtons || app->settings->hideUserButton || customFrame) {
|
2018-04-18 09:12:29 +02:00
|
|
|
// this->userButton.hide();
|
|
|
|
// } else {
|
|
|
|
// this->userButton.move(x, 0);
|
|
|
|
// this->userButton.show();
|
|
|
|
// x += userButton.width();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (customFrame || !this->showButtons ||
|
2018-04-27 22:11:19 +02:00
|
|
|
// (app->settings->hideUserButton && app->settings->hidePreferencesButton)) {
|
2018-04-18 09:12:29 +02:00
|
|
|
// x += (int)(scale * 2);
|
|
|
|
// }
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
int tabHeight = static_cast<int>(NOTEBOOK_TAB_HEIGHT * scale);
|
2018-04-18 09:12:29 +02:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for (auto i = this->items.begin(); i != this->items.end(); i++) {
|
2018-05-23 04:22:17 +02:00
|
|
|
// int yOffset = i->tab->isSelected() ? 0 : 1;
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
if (!first &&
|
|
|
|
(i == this->items.end() && this->showAddButton ? tabHeight : 0) + x + i->tab->width() >
|
|
|
|
width()) //
|
|
|
|
{
|
|
|
|
y += i->tab->height();
|
|
|
|
// y += 20;
|
|
|
|
i->tab->moveAnimated(QPoint(xStart, y), animated);
|
|
|
|
x = i->tab->width() + xStart;
|
|
|
|
} else {
|
|
|
|
i->tab->moveAnimated(QPoint(x, y), animated);
|
|
|
|
x += i->tab->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
x += 1;
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->showAddButton) {
|
|
|
|
this->addButton.move(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->lineY != y + tabHeight) {
|
|
|
|
this->lineY = y + tabHeight;
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
y += (int)(3 * scale);
|
2018-04-18 09:12:29 +02:00
|
|
|
|
|
|
|
for (auto &i : this->items) {
|
|
|
|
i.tab->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->showAddButton) {
|
|
|
|
this->addButton.raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->selectedPage != nullptr) {
|
|
|
|
this->selectedPage->move(0, y + tabHeight);
|
|
|
|
this->selectedPage->resize(width(), height() - y - tabHeight);
|
|
|
|
this->selectedPage->raise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notebook2::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
BaseWidget::paintEvent(event);
|
|
|
|
|
|
|
|
QPainter painter(this);
|
2018-05-23 04:22:17 +02:00
|
|
|
painter.fillRect(0, this->lineY, this->width(), (int)(3 * this->getScale()),
|
2018-04-27 22:11:19 +02:00
|
|
|
this->themeManager->tabs.bottomLine);
|
2018-04-18 09:12:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
NotebookButton *Notebook2::getAddButton()
|
|
|
|
{
|
|
|
|
return &this->addButton;
|
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
NotebookTab2 *Notebook2::getTabFromPage(QWidget *page)
|
|
|
|
{
|
|
|
|
for (auto &it : this->items) {
|
|
|
|
if (it.page == page) {
|
|
|
|
return it.tab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
SplitNotebook::SplitNotebook(QWidget *parent)
|
|
|
|
: Notebook2(parent)
|
2016-12-29 17:31:07 +01:00
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
this->connect(this->getAddButton(), &NotebookButton::clicked,
|
|
|
|
[this]() { QTimer::singleShot(80, this, [this] { this->addPage(true); }); });
|
2017-01-01 18:43:52 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
SplitContainer *SplitNotebook::addPage(bool select)
|
2017-12-22 14:44:31 +01:00
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
SplitContainer *container = new SplitContainer(this);
|
|
|
|
auto *tab = Notebook2::addPage(container, QString(), select);
|
|
|
|
container->setTab(tab);
|
|
|
|
tab->setParent(this);
|
2017-01-22 12:46:35 +01:00
|
|
|
tab->show();
|
2018-05-23 04:22:17 +02:00
|
|
|
return container;
|
2016-12-29 17:31:07 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
SplitContainer *SplitNotebook::getOrAddSelectedPage()
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
auto *selectedPage = this->getSelectedPage();
|
2018-01-22 21:31:45 +01:00
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
return selectedPage != nullptr ? (SplitContainer *)selectedPage : this->addPage();
|
2017-01-18 21:30:23 +01:00
|
|
|
}
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|