mirror-chatterino2/widgets/notebook.cpp

265 lines
5.5 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/notebook.h"
2017-01-01 02:30:42 +01:00
#include "colorscheme.h"
2017-01-18 21:30:23 +01:00
#include "widgets/notebookbutton.h"
#include "widgets/notebookpage.h"
#include "widgets/notebooktab.h"
#include "widgets/settingsdialog.h"
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>
#include <QStandardPaths>
2017-01-18 04:52:47 +01:00
#include <QWidget>
#include <boost/foreach.hpp>
2017-01-18 04:52:47 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2016-12-29 17:31:07 +01:00
Notebook::Notebook(QWidget *parent)
2017-01-11 18:52:09 +01:00
: QWidget(parent)
2017-01-18 04:33:30 +01:00
, addButton(this)
, settingsButton(this)
, userButton(this)
2017-01-22 12:46:35 +01:00
, selectedPage(nullptr)
2016-12-29 17:31:07 +01:00
{
2017-01-18 04:33:30 +01:00
connect(&this->settingsButton, SIGNAL(clicked()), this,
2017-01-11 18:52:09 +01:00
SLOT(settingsButtonClicked()));
connect(&this->userButton, SIGNAL(clicked()), this,
SLOT(usersButtonClicked()));
connect(&this->addButton, SIGNAL(clicked()), this,
SLOT(addPageButtonClicked()));
2017-01-01 18:43:52 +01:00
2017-01-18 04:33:30 +01:00
this->settingsButton.resize(24, 24);
this->settingsButton.icon = NotebookButton::IconSettings;
2017-01-18 04:33:30 +01:00
this->userButton.resize(24, 24);
this->userButton.move(24, 0);
this->userButton.icon = NotebookButton::IconUser;
2017-01-01 18:43:52 +01:00
this->addButton.resize(24, 24);
2017-02-02 02:46:33 +01:00
Settings::getInstance().hidePreferencesButton.valueChanged.connect(
[this](const bool &) { this->performLayout(); });
Settings::getInstance().hideUserButton.valueChanged.connect(
[this](const bool &) { this->performLayout(); });
2017-01-01 18:43:52 +01:00
}
2017-01-11 18:52:09 +01:00
NotebookPage *
Notebook::addPage(bool select)
2016-12-29 17:31:07 +01:00
{
2016-12-30 12:20:26 +01:00
auto tab = new NotebookTab(this);
2017-01-01 02:30:42 +01:00
auto page = new NotebookPage(this, tab);
2016-12-30 12:20:26 +01:00
2017-01-22 12:46:35 +01:00
tab->show();
if (select || this->pages.count() == 0) {
this->select(page);
2016-12-30 18:00:25 +01:00
}
2016-12-29 18:45:08 +01:00
2017-01-18 04:33:30 +01:00
this->pages.append(page);
2016-12-29 18:45:08 +01:00
2017-01-22 12:46:35 +01:00
this->performLayout();
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
void
Notebook::removePage(NotebookPage *page)
{
2017-01-22 12:46:35 +01:00
int index = this->pages.indexOf(page);
if (pages.size() == 1) {
2017-01-22 12:46:35 +01:00
this->select(NULL);
} else if (index == pages.count() - 1) {
2017-01-22 12:46:35 +01:00
this->select(pages[index - 1]);
} else {
2017-01-22 12:46:35 +01:00
this->select(pages[index + 1]);
}
delete page->tab;
delete page;
2017-01-22 12:46:35 +01:00
this->pages.removeOne(page);
if (this->pages.size() == 0) {
addPage();
}
performLayout();
}
2017-01-11 18:52:09 +01:00
void
Notebook::select(NotebookPage *page)
2016-12-30 18:00:25 +01:00
{
2017-01-18 04:33:30 +01:00
if (page == this->selectedPage)
2017-01-11 18:52:09 +01:00
return;
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);
2016-12-30 18:00:25 +01:00
page->tab->setSelected(true);
2017-01-26 05:26:21 +01:00
page->tab->raise();
2016-12-30 18:00:25 +01:00
}
2017-01-18 04:33:30 +01:00
if (this->selectedPage != nullptr) {
this->selectedPage->setHidden(true);
this->selectedPage->tab->setSelected(false);
2017-01-01 02:30:42 +01:00
}
2017-01-18 04:33:30 +01:00
this->selectedPage = page;
2016-12-30 18:00:25 +01:00
performLayout();
}
2017-01-22 12:46:35 +01:00
NotebookPage *
Notebook::tabAt(QPoint point, int &index)
{
int i = 0;
for (auto *page : pages) {
2017-01-26 05:26:21 +01:00
if (page->tab->getDesiredRect().contains(point)) {
2017-01-22 12:46:35 +01:00
index = i;
return page;
}
i++;
}
index = -1;
return nullptr;
}
void
Notebook::rearrangePage(NotebookPage *page, int index)
{
pages.move(pages.indexOf(page), index);
performLayout();
}
2017-01-11 18:52:09 +01:00
void
2017-01-26 05:26:21 +01:00
Notebook::performLayout(bool animated)
2016-12-30 12:20:26 +01:00
{
2017-02-02 02:46:33 +01:00
int x = 0, y = 0;
if (Settings::getInstance().hidePreferencesButton.get()) {
settingsButton.hide();
} else {
settingsButton.show();
x += 24;
}
if (Settings::getInstance().hideUserButton.get()) {
userButton.hide();
} else {
userButton.show();
x += 24;
}
2016-12-30 12:20:26 +01:00
int tabHeight = 16;
2016-12-30 18:00:25 +01:00
bool first = true;
2016-12-30 12:20:26 +01:00
2017-01-18 04:33:30 +01:00
for (auto &i : this->pages) {
2016-12-30 12:20:26 +01:00
tabHeight = i->tab->height();
2017-01-11 18:52:09 +01:00
if (!first &&
2017-01-18 04:33:30 +01:00
(i == this->pages.last() ? tabHeight : 0) + x + i->tab->width() >
2017-01-11 18:52:09 +01:00
width()) {
y += i->tab->height();
2017-01-26 05:26:21 +01:00
i->tab->moveAnimated(QPoint(0, y), animated);
2016-12-30 12:20:26 +01:00
x = i->tab->width();
2017-01-11 18:52:09 +01:00
} else {
2017-01-26 05:26:21 +01:00
i->tab->moveAnimated(QPoint(x, y), animated);
2016-12-30 12:20:26 +01:00
x += i->tab->width();
}
2016-12-30 18:00:25 +01:00
first = false;
2016-12-30 12:20:26 +01:00
}
2017-01-18 04:33:30 +01:00
this->addButton.move(x, y);
2016-12-30 18:00:25 +01:00
2017-01-18 04:33:30 +01:00
if (this->selectedPage != nullptr) {
this->selectedPage->move(0, y + tabHeight);
this->selectedPage->resize(width(), height() - y - tabHeight);
2016-12-30 18:00:25 +01:00
}
2016-12-30 12:20:26 +01:00
}
2017-01-11 18:52:09 +01:00
void
Notebook::resizeEvent(QResizeEvent *)
2016-12-30 12:20:26 +01:00
{
2017-01-26 05:26:21 +01:00
performLayout(false);
2016-12-30 12:20:26 +01:00
}
void
Notebook::settingsButtonClicked()
{
SettingsDialog *a = new SettingsDialog();
a->show();
}
void
Notebook::usersButtonClicked()
{
}
void
Notebook::addPageButtonClicked()
{
addPage(true);
}
void
Notebook::load(const boost::property_tree::ptree &tree)
{
// Read a list of tabs
try {
BOOST_FOREACH (const boost::property_tree::ptree::value_type &v,
tree.get_child("tabs.")) {
bool select = v.second.get<bool>("selected", false);
auto page = this->addPage(select);
auto tab = page->tab;
tab->load(v.second);
page->load(v.second);
}
} catch (boost::property_tree::ptree_error &) {
// can't read tabs
}
if (this->pages.size() == 0) {
// No pages saved, show default stuff
this->loadDefaults();
}
}
void
Notebook::save(boost::property_tree::ptree &tree)
{
boost::property_tree::ptree tabs;
// Iterate through all tabs and add them to our tabs property thing
for (const auto &page : this->pages) {
boost::property_tree::ptree pTab = page->tab->save();
2017-01-29 11:38:00 +01:00
boost::property_tree::ptree pChats = page->save();
if (pChats.size() > 0) {
2017-01-29 12:26:22 +01:00
pTab.add_child("columns", pChats);
2017-01-29 11:38:00 +01:00
}
tabs.push_back(std::make_pair("", pTab));
}
tree.add_child("tabs", tabs);
2017-01-18 21:30:23 +01:00
}
void
Notebook::loadDefaults()
{
this->addPage();
2017-01-18 21:30:23 +01:00
}
} // namespace widgets
} // namespace chatterino