Clean up Notebook

This commit is contained in:
Rasmus Karlsson 2017-08-13 15:33:18 +02:00
parent d9f87c0824
commit 773208ac6b
2 changed files with 49 additions and 48 deletions

View file

@ -26,25 +26,24 @@ Notebook::Notebook(ChannelManager &_channelManager, MainWindow *parent)
, addButton(this) , addButton(this)
, settingsButton(this) , settingsButton(this)
, userButton(this) , userButton(this)
, selectedPage(nullptr)
{ {
connect(&settingsButton, SIGNAL(clicked()), this, SLOT(settingsButtonClicked())); this->connect(&this->settingsButton, SIGNAL(clicked()), this, SLOT(settingsButtonClicked()));
connect(&userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked())); this->connect(&this->userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked()));
connect(&addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked())); this->connect(&this->addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked()));
settingsButton.resize(24, 24); this->settingsButton.resize(24, 24);
settingsButton.icon = NotebookButton::IconSettings; this->settingsButton.icon = NotebookButton::IconSettings;
userButton.resize(24, 24); this->userButton.resize(24, 24);
userButton.move(24, 0); this->userButton.move(24, 0);
userButton.icon = NotebookButton::IconUser; this->userButton.icon = NotebookButton::IconUser;
addButton.resize(24, 24); this->addButton.resize(24, 24);
SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect( SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect(
[this](const bool &) { performLayout(); }); [this](const bool &) { this->performLayout(); });
SettingsManager::getInstance().hideUserButton.valueChanged.connect( SettingsManager::getInstance().hideUserButton.valueChanged.connect(
[this](const bool &) { performLayout(); }); [this](const bool &) { this->performLayout(); });
} }
NotebookPage *Notebook::addPage(bool select) NotebookPage *Notebook::addPage(bool select)
@ -54,45 +53,46 @@ NotebookPage *Notebook::addPage(bool select)
tab->show(); tab->show();
if (select || pages.count() == 0) { if (select || this->pages.count() == 0) {
this->select(page); this->select(page);
} }
pages.append(page); this->pages.append(page);
performLayout(); this->performLayout();
return page; return page;
} }
void Notebook::removePage(NotebookPage *page) void Notebook::removePage(NotebookPage *page)
{ {
int index = pages.indexOf(page); int index = this->pages.indexOf(page);
if (pages.size() == 1) { if (this->pages.size() == 1) {
select(nullptr); select(nullptr);
} else if (index == pages.count() - 1) { } else if (index == this->pages.count() - 1) {
select(pages[index - 1]); select(this->pages[index - 1]);
} else { } else {
select(pages[index + 1]); select(this->pages[index + 1]);
} }
delete page->getTab(); delete page->getTab();
delete page; delete page;
pages.removeOne(page); this->pages.removeOne(page);
if (pages.size() == 0) { if (this->pages.size() == 0) {
addPage(); addPage();
} }
performLayout(); this->performLayout();
} }
void Notebook::select(NotebookPage *page) void Notebook::select(NotebookPage *page)
{ {
if (page == selectedPage) if (page == this->selectedPage) {
return; return;
}
if (page != nullptr) { if (page != nullptr) {
page->setHidden(false); page->setHidden(false);
@ -100,21 +100,21 @@ void Notebook::select(NotebookPage *page)
page->getTab()->raise(); page->getTab()->raise();
} }
if (selectedPage != nullptr) { if (this->selectedPage != nullptr) {
selectedPage->setHidden(true); this->selectedPage->setHidden(true);
selectedPage->getTab()->setSelected(false); this->selectedPage->getTab()->setSelected(false);
} }
selectedPage = page; this->selectedPage = page;
performLayout(); this->performLayout();
} }
NotebookPage *Notebook::tabAt(QPoint point, int &index) NotebookPage *Notebook::tabAt(QPoint point, int &index)
{ {
int i = 0; int i = 0;
for (auto *page : pages) { for (auto *page : this->pages) {
if (page->getTab()->getDesiredRect().contains(point)) { if (page->getTab()->getDesiredRect().contains(point)) {
index = i; index = i;
return page; return page;
@ -129,9 +129,9 @@ NotebookPage *Notebook::tabAt(QPoint point, int &index)
void Notebook::rearrangePage(NotebookPage *page, int index) void Notebook::rearrangePage(NotebookPage *page, int index)
{ {
pages.move(pages.indexOf(page), index); this->pages.move(this->pages.indexOf(page), index);
performLayout(); this->performLayout();
} }
void Notebook::nextTab() void Notebook::nextTab()
@ -165,26 +165,27 @@ void Notebook::performLayout(bool animated)
int x = 0, y = 0; int x = 0, y = 0;
if (SettingsManager::getInstance().hidePreferencesButton.get()) { if (SettingsManager::getInstance().hidePreferencesButton.get()) {
settingsButton.hide(); this->settingsButton.hide();
} else { } else {
settingsButton.show(); this->settingsButton.show();
x += 24; x += 24;
} }
if (SettingsManager::getInstance().hideUserButton.get()) { if (SettingsManager::getInstance().hideUserButton.get()) {
userButton.hide(); this->userButton.hide();
} else { } else {
userButton.move(x, 0); this->userButton.move(x, 0);
userButton.show(); this->userButton.show();
x += 24; x += 24;
} }
int tabHeight = 16; int tabHeight = 16;
bool first = true; bool first = true;
for (auto &i : pages) { for (auto &i : this->pages) {
tabHeight = i->getTab()->height(); tabHeight = i->getTab()->height();
if (!first && (i == pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) { if (!first &&
(i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) {
y += i->getTab()->height(); y += i->getTab()->height();
i->getTab()->moveAnimated(QPoint(0, y), animated); i->getTab()->moveAnimated(QPoint(0, y), animated);
x = i->getTab()->width(); x = i->getTab()->width();
@ -196,17 +197,17 @@ void Notebook::performLayout(bool animated)
first = false; first = false;
} }
addButton.move(x, y); this->addButton.move(x, y);
if (selectedPage != nullptr) { if (this->selectedPage != nullptr) {
selectedPage->move(0, y + tabHeight); this->selectedPage->move(0, y + tabHeight);
selectedPage->resize(width(), height() - y - tabHeight); this->selectedPage->resize(width(), height() - y - tabHeight);
} }
} }
void Notebook::resizeEvent(QResizeEvent *) void Notebook::resizeEvent(QResizeEvent *)
{ {
performLayout(false); this->performLayout(false);
} }
void Notebook::settingsButtonClicked() void Notebook::settingsButtonClicked()
@ -239,7 +240,7 @@ void Notebook::load(const boost::property_tree::ptree &tree)
// can't read tabs // can't read tabs
} }
if (pages.size() == 0) { if (this->pages.size() == 0) {
// No pages saved, show default stuff // No pages saved, show default stuff
loadDefaults(); loadDefaults();
} }
@ -250,7 +251,7 @@ void Notebook::save(boost::property_tree::ptree &tree)
boost::property_tree::ptree tabs; boost::property_tree::ptree tabs;
// Iterate through all tabs and add them to our tabs property thing // Iterate through all tabs and add them to our tabs property thing
for (const auto &page : pages) { for (const auto &page : this->pages) {
boost::property_tree::ptree pTab = page->getTab()->save(); boost::property_tree::ptree pTab = page->getTab()->save();
boost::property_tree::ptree pChats = page->save(); boost::property_tree::ptree pChats = page->save();

View file

@ -32,7 +32,7 @@ public:
void removePage(NotebookPage *page); void removePage(NotebookPage *page);
void select(NotebookPage *page); void select(NotebookPage *page);
NotebookPage *getSelectedPage() NotebookPage *getSelectedPage() const
{ {
return selectedPage; return selectedPage;
} }
@ -66,7 +66,7 @@ private:
NotebookButton settingsButton; NotebookButton settingsButton;
NotebookButton userButton; NotebookButton userButton;
NotebookPage *selectedPage; NotebookPage *selectedPage = nullptr;
public: public:
void load(const boost::property_tree::ptree &tree); void load(const boost::property_tree::ptree &tree);