mirror-chatterino2/src/widgets/Notebook.cpp

471 lines
11 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "widgets/Notebook.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "debug/Log.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include "util/InitUpdateButton.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/Window.hpp"
#include "widgets/dialogs/SettingsDialog.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/helper/NotebookButton.hpp"
#include "widgets/helper/NotebookTab.hpp"
#include "widgets/helper/Shortcut.hpp"
#include "widgets/splits/SplitContainer.hpp"
2016-12-29 17:31:07 +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>
#include <QStandardPaths>
#include <QUuid>
2017-01-18 04:52:47 +01:00
#include <QWidget>
#include <boost/foreach.hpp>
2017-01-18 04:52:47 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
2018-05-23 11:59:37 +02:00
Notebook::Notebook(QWidget *parent)
: 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-05-23 11:59:37 +02:00
NotebookTab *Notebook::addPage(QWidget *page, QString title, bool select)
2018-04-18 09:12:29 +02:00
{
2018-05-23 11:59:37 +02:00
auto *tab = new NotebookTab(this);
2018-04-18 09:12:29 +02:00
tab->page = page;
2018-06-01 16:01:49 +02:00
tab->setCustomTitle(title);
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();
2018-06-04 12:54:09 +02:00
tab->show();
2018-04-18 09:12:29 +02:00
return tab;
}
2018-05-23 11:59:37 +02:00
void Notebook::removePage(QWidget *page)
2018-04-18 09:12:29 +02:00
{
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(true);
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
void Notebook::removeCurrentPage()
2018-04-18 09:12:29 +02:00
{
if (this->selectedPage != nullptr) {
this->removePage(this->selectedPage);
}
}
2018-05-23 11:59:37 +02:00
int Notebook::indexOf(QWidget *page) const
2018-04-18 09:12:29 +02:00
{
for (int i = 0; i < this->items.count(); i++) {
if (this->items[i].page == page) {
return i;
}
}
return -1;
}
2018-05-23 11:59:37 +02:00
void Notebook::select(QWidget *page)
2018-04-18 09:12:29 +02:00
{
if (page == this->selectedPage) {
return;
}
if (page != nullptr) {
page->setHidden(false);
2018-05-31 16:02:20 +02:00
assert(this->containsPage(page));
Item &item = this->findItem(page);
item.tab->setSelected(true);
item.tab->raise();
if (item.selectedWidget == nullptr) {
item.page->setFocus();
} else {
if (containsChild(page, item.selectedWidget)) {
qDebug() << item.selectedWidget;
item.selectedWidget->setFocus(Qt::MouseFocusReason);
} else {
qDebug() << "Notebook: selected child of page doesn't exist anymore";
}
}
2018-04-18 09:12:29 +02:00
}
if (this->selectedPage != nullptr) {
this->selectedPage->setHidden(true);
2018-05-31 16:02:20 +02:00
Item &item = this->findItem(selectedPage);
item.tab->setSelected(false);
2018-04-18 09:12:29 +02:00
// for (auto split : this->selectedPage->getSplits()) {
// split->updateLastReadMessage();
// }
2018-05-31 16:02:20 +02:00
item.selectedWidget = this->selectedPage->focusWidget();
2018-04-18 09:12:29 +02:00
}
this->selectedPage = page;
this->performLayout();
}
2018-05-31 16:02:20 +02:00
bool Notebook::containsPage(QWidget *page)
{
return std::any_of(this->items.begin(), this->items.end(),
[page](const auto &item) { return item.page == page; });
}
Notebook::Item &Notebook::findItem(QWidget *page)
{
auto it = std::find_if(this->items.begin(), this->items.end(),
[page](const auto &item) { return page == item.page; });
assert(it != this->items.end());
return *it;
}
bool Notebook::containsChild(const QObject *obj, const QObject *child)
{
return std::any_of(obj->children().begin(), obj->children().end(), [child](const QObject *o) {
if (o == child) {
return true;
}
return containsChild(o, child);
});
}
2018-05-23 11:59:37 +02:00
void Notebook::selectIndex(int index)
2018-04-18 09:12:29 +02:00
{
if (index < 0 || this->items.count() <= index) {
return;
}
this->select(this->items[index].page);
}
2018-05-23 11:59:37 +02:00
void Notebook::selectNextTab()
2018-04-18 09:12:29 +02:00
{
if (this->items.size() <= 1) {
return;
}
int index = (this->indexOf(this->selectedPage) + 1) % this->items.count();
this->select(this->items[index].page);
}
2018-05-23 11:59:37 +02:00
void Notebook::selectPreviousTab()
2018-04-18 09:12:29 +02:00
{
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);
}
2018-05-23 11:59:37 +02:00
int Notebook::getPageCount() const
2018-04-18 09:12:29 +02:00
{
return this->items.count();
}
2018-05-23 11:59:37 +02:00
QWidget *Notebook::getPageAt(int index) const
2018-05-23 04:22:17 +02:00
{
return this->items[index].page;
}
2018-05-23 11:59:37 +02:00
int Notebook::getSelectedIndex() const
2018-04-18 09:12:29 +02:00
{
return this->indexOf(this->selectedPage);
}
2018-05-23 11:59:37 +02:00
QWidget *Notebook::getSelectedPage() const
2018-04-18 09:12:29 +02:00
{
return this->selectedPage;
}
2018-05-23 11:59:37 +02:00
QWidget *Notebook::tabAt(QPoint point, int &index, int maxWidth)
2018-04-18 09:12:29 +02:00
{
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;
}
2018-05-23 11:59:37 +02:00
void Notebook::rearrangePage(QWidget *page, int index)
2018-04-18 09:12:29 +02:00
{
this->items.move(this->indexOf(page), index);
this->performLayout(true);
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
bool Notebook::getAllowUserTabManagement() const
2018-04-18 09:12:29 +02:00
{
return this->allowUserTabManagement;
}
2018-05-23 11:59:37 +02:00
void Notebook::setAllowUserTabManagement(bool value)
2018-04-18 09:12:29 +02:00
{
this->allowUserTabManagement = value;
}
2018-05-23 11:59:37 +02:00
bool Notebook::getShowAddButton() const
2018-04-18 09:12:29 +02:00
{
return this->showAddButton;
}
2018-05-23 11:59:37 +02:00
void Notebook::setShowAddButton(bool value)
2018-04-18 09:12:29 +02:00
{
this->showAddButton = value;
this->addButton.setHidden(!value);
}
2018-05-23 11:59:37 +02:00
void Notebook::scaleChangedEvent(float scale)
2018-04-18 09:12:29 +02:00
{
2018-05-23 04:22:17 +02:00
float h = NOTEBOOK_TAB_HEIGHT * this->getScale();
2018-04-18 09:12:29 +02:00
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();
}
}
2018-05-23 11:59:37 +02:00
void Notebook::resizeEvent(QResizeEvent *)
2018-04-18 09:12:29 +02:00
{
this->performLayout();
}
2018-05-23 11:59:37 +02:00
void Notebook::performLayout(bool animated)
2018-04-18 09:12:29 +02:00
{
int xStart = int(2 * this->getScale());
2018-04-18 09:12:29 +02:00
int x = xStart, y = 0;
float scale = this->getScale();
int h = int(NOTEBOOK_TAB_HEIGHT * this->getScale());
for (auto *btn : this->customButtons) {
if (!btn->isVisible()) {
continue;
}
btn->setFixedSize(h, h);
btn->move(x, 0);
x += h;
}
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++) {
bool wrap =
!first && (((i + 1 == this->items.end() && this->showAddButton) ? tabHeight : 0) + x +
i->tab->width()) > width();
if (wrap) {
2018-04-18 09:12:29 +02:00
y += i->tab->height();
i->tab->moveAnimated(QPoint(xStart, y), animated);
x = i->tab->width() + xStart;
} else {
i->tab->moveAnimated(QPoint(x, y), animated);
x += i->tab->width();
}
2018-06-11 21:57:17 +02:00
x += int(scale * 1);
2018-04-18 09:12:29 +02:00
first = false;
}
if (this->showAddButton) {
this->addButton.move(x, y);
}
if (this->lineY != y + tabHeight) {
this->lineY = y + tabHeight;
this->update();
}
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();
}
}
2018-05-23 11:59:37 +02:00
void Notebook::paintEvent(QPaintEvent *event)
2018-04-18 09:12:29 +02:00
{
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()),
this->themeManager->tabs.bottomLine);
2018-04-18 09:12:29 +02:00
}
2018-05-23 11:59:37 +02:00
NotebookButton *Notebook::getAddButton()
2018-05-23 04:22:17 +02:00
{
return &this->addButton;
}
NotebookButton *Notebook::addCustomButton()
{
NotebookButton *btn = new NotebookButton(this);
this->customButtons.push_back(btn);
this->performLayout();
return btn;
}
2018-05-23 11:59:37 +02:00
NotebookTab *Notebook::getTabFromPage(QWidget *page)
2018-04-18 09:12:29 +02:00
{
for (auto &it : this->items) {
if (it.page == page) {
return it.tab;
}
}
return nullptr;
}
SplitNotebook::SplitNotebook(Window *parent)
2018-05-23 11:59:37 +02:00
: Notebook(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); }); });
bool customFrame = parent->hasCustomWindowFrame();
if (!customFrame) {
this->addCustomButtons();
}
2017-01-01 18:43:52 +01:00
}
void SplitNotebook::addCustomButtons()
{
// settings
auto settingsBtn = this->addCustomButton();
settingsBtn->setVisible(!getApp()->settings->hidePreferencesButton.getValue());
getApp()->settings->hidePreferencesButton.connect(
[settingsBtn](bool hide, auto) { settingsBtn->setVisible(!hide); },
this->uniqueConnections);
settingsBtn->setIcon(NotebookButton::Settings);
QObject::connect(settingsBtn, &NotebookButton::clicked,
[] { getApp()->windows->showSettingsDialog(); });
// account
auto userBtn = this->addCustomButton();
userBtn->setVisible(!getApp()->settings->hideUserButton.getValue());
getApp()->settings->hideUserButton.connect(
[userBtn](bool hide, auto) { userBtn->setVisible(!hide); }, this->uniqueConnections);
userBtn->setIcon(NotebookButton::User);
QObject::connect(userBtn, &NotebookButton::clicked, [this, userBtn] {
getApp()->windows->showAccountSelectPopup(this->mapToGlobal(userBtn->rect().bottomRight()));
});
// updates
auto updateBtn = this->addCustomButton();
initUpdateButton(*updateBtn, this->updateDialogHandle_, this->signalHolder_);
}
2018-05-23 04:22:17 +02:00
SplitContainer *SplitNotebook::addPage(bool select)
{
2018-05-23 04:22:17 +02:00
SplitContainer *container = new SplitContainer(this);
2018-05-23 11:59:37 +02:00
auto *tab = Notebook::addPage(container, QString(), select);
2018-05-23 04:22:17 +02:00
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()
{
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-04-14 17:52:22 +02:00
} // namespace chatterino