2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/notebook.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"
|
2017-12-19 02:16:01 +01:00
|
|
|
#include "widgets/accountswitchpopupwidget.hpp"
|
2017-11-12 17:21:50 +01:00
|
|
|
#include "widgets/helper/notebookbutton.hpp"
|
|
|
|
#include "widgets/helper/notebooktab.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
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
Notebook::Notebook(Window *parent, bool _showButtons, const std::string &settingPrefix)
|
2017-06-26 16:41:20 +02:00
|
|
|
: BaseWidget(parent)
|
2018-01-15 01:35:35 +01:00
|
|
|
, parentWindow(parent)
|
2017-12-22 14:44:31 +01:00
|
|
|
, settingRoot(fS("{}/notebook", settingPrefix))
|
2017-06-26 16:41:20 +02:00
|
|
|
, addButton(this)
|
|
|
|
, settingsButton(this)
|
|
|
|
, userButton(this)
|
2017-11-12 17:21:50 +01:00
|
|
|
, showButtons(_showButtons)
|
2017-12-28 18:15:27 +01:00
|
|
|
, tabs(fS("{}/tabs", this->settingRoot))
|
2018-01-10 01:59:55 +01:00
|
|
|
, closeConfirmDialog(this)
|
2016-12-29 17:31:07 +01:00
|
|
|
{
|
2017-08-13 15:33:18 +02:00
|
|
|
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()));
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->settingsButton.icon = NotebookButton::IconSettings;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->userButton.move(24, 0);
|
|
|
|
this->userButton.icon = NotebookButton::IconUser;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &settingsManager = singletons::SettingManager::getInstance();
|
2017-12-17 03:37:46 +01:00
|
|
|
|
|
|
|
settingsManager.hidePreferencesButton.connectSimple([this](auto) { this->performLayout(); });
|
|
|
|
settingsManager.hideUserButton.connectSimple([this](auto) { this->performLayout(); });
|
2017-12-22 14:44:31 +01:00
|
|
|
|
2017-12-28 18:15:27 +01:00
|
|
|
this->loadTabs();
|
2018-01-10 01:59:55 +01:00
|
|
|
|
2018-01-16 18:55:30 +01:00
|
|
|
closeConfirmDialog.setText("Are you sure you want to close this tab?");
|
2018-01-10 01:59:55 +01:00
|
|
|
closeConfirmDialog.setIcon(QMessageBox::Icon::Question);
|
2018-01-16 18:55:30 +01:00
|
|
|
closeConfirmDialog.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
|
|
|
closeConfirmDialog.setDefaultButton(QMessageBox::Yes);
|
2017-01-01 18:43:52 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
SplitContainer *Notebook::addNewPage()
|
2016-12-29 17:31:07 +01:00
|
|
|
{
|
2017-12-22 14:44:31 +01:00
|
|
|
return this->addPage(CreateUUID().toStdString(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitContainer *Notebook::addPage(const std::string &uuid, bool select)
|
|
|
|
{
|
2017-12-28 18:15:27 +01:00
|
|
|
auto tab = new NotebookTab(this, uuid);
|
2017-12-31 00:50:07 +01:00
|
|
|
auto page = new SplitContainer(this, tab, uuid);
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-01-22 12:46:35 +01:00
|
|
|
tab->show();
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
if (select || this->pages.count() == 0) {
|
2017-01-22 05:58:23 +01:00
|
|
|
this->select(page);
|
2016-12-30 18:00:25 +01:00
|
|
|
}
|
2016-12-29 18:45:08 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->pages.append(page);
|
2016-12-29 18:45:08 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->performLayout();
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2016-12-29 18:45:08 +01:00
|
|
|
return page;
|
2016-12-29 17:31:07 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
void Notebook::removePage(SplitContainer *page)
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2018-01-16 18:55:30 +01:00
|
|
|
if (page->splitCount() > 0 && closeConfirmDialog.exec() != QMessageBox::Yes) {
|
2018-01-10 01:59:55 +01:00
|
|
|
return;
|
2018-01-16 18:55:30 +01:00
|
|
|
}
|
2018-01-10 01:59:55 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
int index = this->pages.indexOf(page);
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
if (this->pages.size() == 1) {
|
2017-06-11 09:37:30 +02:00
|
|
|
select(nullptr);
|
2017-08-13 15:33:18 +02:00
|
|
|
} else if (index == this->pages.count() - 1) {
|
|
|
|
select(this->pages[index - 1]);
|
2017-01-22 05:58:23 +01:00
|
|
|
} else {
|
2017-08-13 15:33:18 +02:00
|
|
|
select(this->pages[index + 1]);
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-17 19:15:03 +02:00
|
|
|
page->getTab()->deleteLater();
|
|
|
|
page->deleteLater();
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->pages.removeOne(page);
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
if (this->pages.size() == 0) {
|
2017-12-22 14:44:31 +01:00
|
|
|
this->addNewPage();
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->performLayout();
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 20:58:56 +01:00
|
|
|
void Notebook::removeCurrentPage()
|
|
|
|
{
|
|
|
|
if (this->selectedPage == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->removePage(this->selectedPage);
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
void Notebook::select(SplitContainer *page)
|
2016-12-30 18:00:25 +01:00
|
|
|
{
|
2017-08-13 15:33:18 +02:00
|
|
|
if (page == this->selectedPage) {
|
2017-01-11 18:52:09 +01:00
|
|
|
return;
|
2017-08-13 15:33:18 +02:00
|
|
|
}
|
2016-12-30 18:00:25 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (page != nullptr) {
|
2017-01-01 02:30:42 +01:00
|
|
|
page->setHidden(false);
|
2017-04-12 17:46:44 +02:00
|
|
|
page->getTab()->setSelected(true);
|
|
|
|
page->getTab()->raise();
|
2016-12-30 18:00:25 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
if (this->selectedPage != nullptr) {
|
|
|
|
this->selectedPage->setHidden(true);
|
|
|
|
this->selectedPage->getTab()->setSelected(false);
|
2018-01-23 23:28:06 +01:00
|
|
|
for (auto split : this->selectedPage->getSplits()) {
|
|
|
|
split->updateLastReadMessage();
|
|
|
|
}
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->selectedPage = page;
|
2016-12-30 18:00:25 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->performLayout();
|
2016-12-30 18:00:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
void Notebook::selectIndex(int index)
|
2018-01-06 20:27:26 +01:00
|
|
|
{
|
2018-01-11 20:16:25 +01:00
|
|
|
if (index < 0 || index >= this->pages.size()) {
|
2018-01-06 20:27:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->select(this->pages.at(index));
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:25:06 +01:00
|
|
|
int Notebook::tabCount()
|
|
|
|
{
|
|
|
|
return this->pages.size();
|
|
|
|
}
|
|
|
|
|
2018-01-22 21:31:45 +01:00
|
|
|
SplitContainer *Notebook::tabAt(QPoint point, int &index, int maxWidth)
|
2017-01-22 12:46:35 +01:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
for (auto *page : this->pages) {
|
2018-01-22 21:31:45 +01:00
|
|
|
QRect rect = page->getTab()->getDesiredRect();
|
|
|
|
rect.setWidth(std::min(maxWidth, rect.width()));
|
|
|
|
|
|
|
|
if (rect.contains(point)) {
|
2017-01-22 12:46:35 +01:00
|
|
|
index = i;
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = -1;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
void Notebook::rearrangePage(SplitContainer *page, int index)
|
2017-01-22 12:46:35 +01:00
|
|
|
{
|
2017-08-13 15:33:18 +02:00
|
|
|
this->pages.move(this->pages.indexOf(page), index);
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->performLayout();
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 15:24:41 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Notebook::performLayout(bool animated)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2018-01-05 23:55:41 +01:00
|
|
|
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
|
|
|
|
|
2017-02-02 02:46:33 +01:00
|
|
|
int x = 0, y = 0;
|
2017-09-22 00:50:43 +02:00
|
|
|
float scale = this->getDpiMultiplier();
|
2018-01-15 01:35:35 +01:00
|
|
|
bool customFrame = this->parentWindow->hasCustomWindowFrame();
|
2017-02-02 02:46:33 +01:00
|
|
|
|
2018-01-15 01:35:35 +01:00
|
|
|
if (!this->showButtons || settings.hidePreferencesButton || customFrame) {
|
2017-08-13 15:33:18 +02:00
|
|
|
this->settingsButton.hide();
|
2017-02-02 02:46:33 +01:00
|
|
|
} else {
|
2017-08-13 15:33:18 +02:00
|
|
|
this->settingsButton.show();
|
2017-09-22 00:50:43 +02:00
|
|
|
x += settingsButton.width();
|
2017-02-02 02:46:33 +01:00
|
|
|
}
|
2018-01-15 01:35:35 +01:00
|
|
|
if (!this->showButtons || settings.hideUserButton || customFrame) {
|
2017-08-13 15:33:18 +02:00
|
|
|
this->userButton.hide();
|
2017-02-02 02:46:33 +01:00
|
|
|
} else {
|
2017-08-13 15:33:18 +02:00
|
|
|
this->userButton.move(x, 0);
|
|
|
|
this->userButton.show();
|
2017-09-22 00:50:43 +02:00
|
|
|
x += userButton.width();
|
2017-02-02 02:46:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 01:35:35 +01:00
|
|
|
if (customFrame || !this->showButtons ||
|
|
|
|
(settings.hideUserButton && settings.hidePreferencesButton)) {
|
2018-01-05 23:55:41 +01:00
|
|
|
x += (int)(scale * 2);
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:50:43 +02:00
|
|
|
int tabHeight = static_cast<int>(24 * scale);
|
2016-12-30 18:00:25 +01:00
|
|
|
bool first = true;
|
2016-12-30 12:20:26 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
for (auto &i : this->pages) {
|
|
|
|
if (!first &&
|
|
|
|
(i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
y += i->getTab()->height();
|
|
|
|
i->getTab()->moveAnimated(QPoint(0, y), animated);
|
|
|
|
x = i->getTab()->width();
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2017-04-12 17:46:44 +02:00
|
|
|
i->getTab()->moveAnimated(QPoint(x, y), animated);
|
|
|
|
x += i->getTab()->width();
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:00:25 +01:00
|
|
|
first = false;
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->addButton.move(x, y);
|
2016-12-30 18:00:25 +01:00
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
if (this->selectedPage != nullptr) {
|
|
|
|
this->selectedPage->move(0, y + tabHeight);
|
|
|
|
this->selectedPage->resize(width(), height() - y - tabHeight);
|
2018-01-22 21:31:45 +01:00
|
|
|
this->selectedPage->raise();
|
2016-12-30 18:00:25 +01:00
|
|
|
}
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Notebook::resizeEvent(QResizeEvent *)
|
2016-12-30 12:20:26 +01:00
|
|
|
{
|
2017-09-22 00:50:43 +02:00
|
|
|
float scale = this->getDpiMultiplier();
|
|
|
|
|
|
|
|
this->settingsButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
|
|
|
|
this->userButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
|
|
|
|
this->addButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
|
|
|
|
|
|
|
|
for (auto &i : this->pages) {
|
|
|
|
i->getTab()->calcSize();
|
|
|
|
}
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
this->performLayout(false);
|
2016-12-30 12:20:26 +01:00
|
|
|
}
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Notebook::settingsButtonClicked()
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2017-09-16 00:05:06 +02:00
|
|
|
QTimer::singleShot(80, [this] { SettingsDialog::showDialog(); });
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Notebook::usersButtonClicked()
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
static QWidget *lastFocusedWidget = nullptr;
|
|
|
|
static AccountSwitchPopupWidget *w = new AccountSwitchPopupWidget(this);
|
|
|
|
|
|
|
|
if (w->hasFocus()) {
|
|
|
|
w->hide();
|
|
|
|
if (lastFocusedWidget) {
|
|
|
|
lastFocusedWidget->setFocus();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastFocusedWidget = this->focusWidget();
|
|
|
|
|
|
|
|
w->refresh();
|
|
|
|
|
|
|
|
QPoint buttonPos = this->userButton.rect().bottomRight();
|
|
|
|
w->move(buttonPos.x(), buttonPos.y());
|
|
|
|
|
|
|
|
w->show();
|
|
|
|
w->setFocus();
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void Notebook::addPageButtonClicked()
|
2017-01-22 05:58:23 +01:00
|
|
|
{
|
2017-12-22 14:44:31 +01:00
|
|
|
QTimer::singleShot(80, [this] { this->addNewPage(); });
|
2017-01-22 05:58:23 +01:00
|
|
|
}
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-12-28 18:15:27 +01:00
|
|
|
void Notebook::loadTabs()
|
2017-01-28 22:35:23 +01:00
|
|
|
{
|
2017-12-28 18:15:27 +01:00
|
|
|
const std::vector<std::string> tabArray = this->tabs.getValue();
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-12-28 18:15:27 +01:00
|
|
|
if (tabArray.size() == 0) {
|
|
|
|
this->addNewPage();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-22 14:44:31 +01:00
|
|
|
|
2017-12-28 18:15:27 +01:00
|
|
|
for (const std::string &tabUUID : tabArray) {
|
|
|
|
this->addPage(tabUUID);
|
2017-01-28 22:35:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:44:31 +01:00
|
|
|
void Notebook::save()
|
2017-01-28 22:35:23 +01:00
|
|
|
{
|
2017-12-28 18:15:27 +01:00
|
|
|
std::vector<std::string> tabArray;
|
|
|
|
|
2017-08-13 15:33:18 +02:00
|
|
|
for (const auto &page : this->pages) {
|
2017-12-28 18:15:27 +01:00
|
|
|
tabArray.push_back(page->getUUID());
|
2017-12-22 14:44:31 +01:00
|
|
|
page->save();
|
2017-01-28 22:35:23 +01:00
|
|
|
}
|
2017-12-28 18:15:27 +01:00
|
|
|
|
|
|
|
this->tabs = tabArray;
|
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
|