mirror-chatterino2/widgets/notebook.cpp

247 lines
5.4 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-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2016-12-29 17:31:07 +01:00
Notebook::Notebook(QWidget *parent)
2017-01-11 18:52:09 +01:00
: QWidget(parent)
2017-04-12 17:46:44 +02:00
, _addButton(this)
, _settingsButton(this)
, _userButton(this)
, _selectedPage(nullptr)
2016-12-29 17:31:07 +01:00
{
2017-04-12 17:46:44 +02:00
connect(&_settingsButton, SIGNAL(clicked()), this, SLOT(settingsButtonClicked()));
connect(&_userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked()));
connect(&_addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked()));
_settingsButton.resize(24, 24);
_settingsButton.icon = NotebookButton::IconSettings;
_userButton.resize(24, 24);
_userButton.move(24, 0);
_userButton.icon = NotebookButton::IconUser;
_addButton.resize(24, 24);
SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect(
[this](const bool &) { performLayout(); });
SettingsManager::getInstance().hideUserButton.valueChanged.connect(
[this](const bool &) { performLayout(); });
2017-01-01 18:43:52 +01:00
}
2017-04-12 17:46:44 +02: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();
2017-04-12 17:46:44 +02:00
if (select || _pages.count() == 0) {
this->select(page);
2016-12-30 18:00:25 +01:00
}
2016-12-29 18:45:08 +01:00
2017-04-12 17:46:44 +02:00
_pages.append(page);
2016-12-29 18:45:08 +01:00
2017-04-12 17:46:44 +02:00
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
2017-04-12 17:46:44 +02:00
void Notebook::removePage(NotebookPage *page)
{
2017-04-12 17:46:44 +02:00
int index = _pages.indexOf(page);
2017-04-12 17:46:44 +02:00
if (_pages.size() == 1) {
select(NULL);
} else if (index == _pages.count() - 1) {
select(_pages[index - 1]);
} else {
2017-04-12 17:46:44 +02:00
select(_pages[index + 1]);
}
2017-04-12 17:46:44 +02:00
delete page->getTab();
delete page;
2017-04-12 17:46:44 +02:00
_pages.removeOne(page);
2017-01-22 12:46:35 +01:00
2017-04-12 17:46:44 +02:00
if (_pages.size() == 0) {
2017-01-22 12:46:35 +01:00
addPage();
}
performLayout();
}
2017-04-12 17:46:44 +02:00
void Notebook::select(NotebookPage *page)
2016-12-30 18:00:25 +01:00
{
2017-04-12 17:46:44 +02:00
if (page == _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);
2017-04-12 17:46:44 +02:00
page->getTab()->setSelected(true);
page->getTab()->raise();
2016-12-30 18:00:25 +01:00
}
2017-04-12 17:46:44 +02:00
if (_selectedPage != nullptr) {
_selectedPage->setHidden(true);
_selectedPage->getTab()->setSelected(false);
2017-01-01 02:30:42 +01:00
}
2017-04-12 17:46:44 +02:00
_selectedPage = page;
2016-12-30 18:00:25 +01:00
performLayout();
}
2017-04-12 17:46:44 +02:00
NotebookPage *Notebook::tabAt(QPoint point, int &index)
2017-01-22 12:46:35 +01:00
{
int i = 0;
2017-04-12 17:46:44 +02:00
for (auto *page : _pages) {
if (page->getTab()->getDesiredRect().contains(point)) {
2017-01-22 12:46:35 +01:00
index = i;
return page;
}
i++;
}
index = -1;
return nullptr;
}
2017-04-12 17:46:44 +02:00
void Notebook::rearrangePage(NotebookPage *page, int index)
2017-01-22 12:46:35 +01:00
{
2017-04-12 17:46:44 +02:00
_pages.move(_pages.indexOf(page), index);
2017-01-22 12:46:35 +01:00
performLayout();
}
2017-04-12 17:46:44 +02:00
void 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;
2017-04-12 17:46:44 +02:00
if (SettingsManager::getInstance().hidePreferencesButton.get()) {
_settingsButton.hide();
2017-02-02 02:46:33 +01:00
} else {
2017-04-12 17:46:44 +02:00
_settingsButton.show();
2017-02-02 02:46:33 +01:00
x += 24;
}
2017-04-12 17:46:44 +02:00
if (SettingsManager::getInstance().hideUserButton.get()) {
_userButton.hide();
2017-02-02 02:46:33 +01:00
} else {
2017-04-12 17:46:44 +02:00
_userButton.move(x, 0);
_userButton.show();
2017-02-02 02:46:33 +01:00
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-04-12 17:46:44 +02:00
for (auto &i : _pages) {
tabHeight = i->getTab()->height();
2016-12-30 12:20:26 +01:00
2017-04-12 17:46:44 +02:00
if (!first && (i == _pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) {
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-04-12 17:46:44 +02:00
_addButton.move(x, y);
2016-12-30 18:00:25 +01:00
2017-04-12 17:46:44 +02:00
if (_selectedPage != nullptr) {
_selectedPage->move(0, y + tabHeight);
_selectedPage->resize(width(), height() - y - tabHeight);
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-01-26 05:26:21 +01:00
performLayout(false);
2016-12-30 12:20:26 +01:00
}
2017-04-12 17:46:44 +02:00
void Notebook::settingsButtonClicked()
{
SettingsDialog *a = new SettingsDialog();
a->show();
}
2017-04-12 17:46:44 +02:00
void Notebook::usersButtonClicked()
{
}
2017-04-12 17:46:44 +02:00
void Notebook::addPageButtonClicked()
{
addPage(true);
}
2017-04-12 17:46:44 +02:00
void Notebook::load(const boost::property_tree::ptree &tree)
{
// Read a list of tabs
try {
2017-04-12 17:46:44 +02:00
BOOST_FOREACH (const boost::property_tree::ptree::value_type &v, tree.get_child("tabs.")) {
bool select = v.second.get<bool>("selected", false);
2017-04-12 17:46:44 +02:00
auto page = addPage(select);
auto tab = page->getTab();
tab->load(v.second);
page->load(v.second);
}
} catch (boost::property_tree::ptree_error &) {
// can't read tabs
}
2017-04-12 17:46:44 +02:00
if (_pages.size() == 0) {
// No pages saved, show default stuff
2017-04-12 17:46:44 +02:00
loadDefaults();
}
}
2017-04-12 17:46:44 +02:00
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
2017-04-12 17:46:44 +02:00
for (const auto &page : _pages) {
boost::property_tree::ptree pTab = page->getTab()->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
}
2017-04-12 17:46:44 +02:00
void Notebook::loadDefaults()
{
2017-04-12 17:46:44 +02:00
addPage();
2017-01-18 21:30:23 +01:00
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino