From c0b4035e6a1511358b08d38ebed386db5d2c9516 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Jan 2017 11:38:00 +0100 Subject: [PATCH 1/3] we now load and save chats as well --- widgets/chatwidget.cpp | 22 ++++++++++++++++++++++ widgets/chatwidget.h | 10 ++++++++-- widgets/notebook.cpp | 7 +++++++ widgets/notebookpage.cpp | 29 +++++++++++++++++++++++++---- widgets/notebookpage.h | 3 ++- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/widgets/chatwidget.cpp b/widgets/chatwidget.cpp index 184271374..e2d941bcb 100644 --- a/widgets/chatwidget.cpp +++ b/widgets/chatwidget.cpp @@ -83,5 +83,27 @@ ChatWidget::paintEvent(QPaintEvent *) painter.fillRect(this->rect(), ColorScheme::getInstance().ChatBackground); } + +void +ChatWidget::load(const boost::property_tree::ptree &tree) +{ + // Load tab text + try { + this->setChannelName( + QString::fromStdString(tree.get("channelName"))); + } catch (boost::property_tree::ptree_error) { + } } + +boost::property_tree::ptree +ChatWidget::save() +{ + boost::property_tree::ptree tree; + + tree.put("channelName", this->getChannelName().toStdString()); + + return tree; } + +} // namespace widgets +} // namespace chatterino diff --git a/widgets/chatwidget.h b/widgets/chatwidget.h index cff83f8c5..5b1782d3c 100644 --- a/widgets/chatwidget.h +++ b/widgets/chatwidget.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace chatterino { namespace widgets { @@ -56,8 +57,13 @@ private: ChatWidgetHeader header; ChatWidgetView view; ChatWidgetInput input; + +public: + void load(const boost::property_tree::ptree &tree); + boost::property_tree::ptree save(); }; -} -} + +} // namespace widgets +} // namespace chatterino #endif // CHATWIDGET_H diff --git a/widgets/notebook.cpp b/widgets/notebook.cpp index 10fe76843..c850cfad5 100644 --- a/widgets/notebook.cpp +++ b/widgets/notebook.cpp @@ -226,6 +226,13 @@ Notebook::save(boost::property_tree::ptree &tree) // 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(); + + boost::property_tree::ptree pChats = page->save(); + + if (pChats.size() > 0) { + pTab.add_child("chats", pChats); + } + tabs.push_back(std::make_pair("", pTab)); } diff --git a/widgets/notebookpage.cpp b/widgets/notebookpage.cpp index d2e1582b7..015ee4be4 100644 --- a/widgets/notebookpage.cpp +++ b/widgets/notebookpage.cpp @@ -124,7 +124,8 @@ void NotebookPage::mouseReleaseEvent(QMouseEvent *event) { if (this->hbox.count() == 0 && event->button() == Qt::LeftButton) { - addToLayout(new ChatWidget(), std::pair(-1, -1)); + // "Add Chat" was clicked + this->addToLayout(new ChatWidget(), std::pair(-1, -1)); setCursor(QCursor(Qt::ArrowCursor)); } @@ -242,11 +243,31 @@ NotebookPage::paintEvent(QPaintEvent *) } void -NotebookPage::load(const boost::property_tree::ptree &v) +NotebookPage::load(const boost::property_tree::ptree &tree) { - const std::string &tabName = v.get("name", "UNNAMED"); + try { + BOOST_FOREACH (const boost::property_tree::ptree::value_type &v, + tree.get_child("chats.")) { + auto widget = new ChatWidget(); + widget->load(v.second); + this->addToLayout(widget, std::pair(-1, -1)); + } + } catch (boost::property_tree::ptree_error &) { + // can't read tabs + } +} - qDebug() << "tab name :" << tabName.c_str(); +boost::property_tree::ptree +NotebookPage::save() +{ + boost::property_tree::ptree tree; + + for (const auto &chat : this->chatWidgets) { + boost::property_tree::ptree child = chat->save(); + tree.push_back(std::make_pair("", child)); + } + + return tree; } } // namespace widgets diff --git a/widgets/notebookpage.h b/widgets/notebookpage.h index 926635233..c7e7f35f5 100644 --- a/widgets/notebookpage.h +++ b/widgets/notebookpage.h @@ -73,7 +73,8 @@ private: void setPreviewRect(QPoint mousePos); public: - void load(const boost::property_tree::ptree &v); + void load(const boost::property_tree::ptree &tree); + boost::property_tree::ptree save(); }; } // namespace widgets From 199e87896b85d214f978488d2f919ba669bf431e Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Jan 2017 11:39:16 +0100 Subject: [PATCH 2/3] rename "text" to "title" in NotebookTab --- widgets/notebooktab.cpp | 14 +++++++------- widgets/notebooktab.h | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/widgets/notebooktab.cpp b/widgets/notebooktab.cpp index b4a568755..07b204306 100644 --- a/widgets/notebooktab.cpp +++ b/widgets/notebooktab.cpp @@ -14,7 +14,7 @@ NotebookTab::NotebookTab(Notebook *notebook) , posAnimated(false) , posAnimationDesired() , notebook(notebook) - , text("") + , title("") , selected(false) , mouseOver(false) , mouseDown(false) @@ -52,9 +52,9 @@ void NotebookTab::calcSize() { if (Settings::getInstance().hideTabX.get()) { - this->resize(this->fontMetrics().width(this->text) + 8, 24); + this->resize(this->fontMetrics().width(this->title) + 8, 24); } else { - this->resize(this->fontMetrics().width(this->text) + 8 + 24, 24); + this->resize(this->fontMetrics().width(this->title) + 8 + 24, 24); } } @@ -115,7 +115,7 @@ NotebookTab::paintEvent(QPaintEvent *) width() - (Settings::getInstance().hideTabX.get() ? 0 : 16), height()); - painter.drawText(rect, this->text, QTextOption(Qt::AlignCenter)); + painter.drawText(rect, this->title, QTextOption(Qt::AlignCenter)); if (!Settings::getInstance().hideTabX.get() && (this->mouseOver || this->selected)) { @@ -207,9 +207,9 @@ NotebookTab::mouseMoveEvent(QMouseEvent *event) void NotebookTab::load(const boost::property_tree::ptree &tree) { - // Load tab text + // Load tab title try { - this->setText(QString::fromStdString(tree.get("text"))); + this->setTitle(QString::fromStdString(tree.get("title"))); } catch (boost::property_tree::ptree_error) { } } @@ -219,7 +219,7 @@ NotebookTab::save() { boost::property_tree::ptree tree; - tree.put("text", this->getText().toStdString()); + tree.put("title", this->getTitle().toStdString()); return tree; } diff --git a/widgets/notebooktab.h b/widgets/notebooktab.h index 51a8caf35..4a4dc6e59 100644 --- a/widgets/notebooktab.h +++ b/widgets/notebooktab.h @@ -30,15 +30,15 @@ public: NotebookPage *page; const QString & - getText() const + getTitle() const { - return this->text; + return this->title; } void - setText(const QString &text) + setTitle(const QString &title) { - this->text = text; + this->title = title; } bool @@ -95,7 +95,7 @@ private: Notebook *notebook; - QString text; + QString title; bool selected; bool mouseOver; From 3266f8ad4ff300763dcdbe0f56d6c7d71870a3a0 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Jan 2017 12:26:22 +0100 Subject: [PATCH 3/3] split layout is now saved too --- widgets/notebook.cpp | 2 +- widgets/notebookpage.cpp | 85 +++++++++++++++++++++++++++++++++++++--- widgets/notebookpage.h | 2 + 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/widgets/notebook.cpp b/widgets/notebook.cpp index c850cfad5..cf1ddb8f3 100644 --- a/widgets/notebook.cpp +++ b/widgets/notebook.cpp @@ -230,7 +230,7 @@ Notebook::save(boost::property_tree::ptree &tree) boost::property_tree::ptree pChats = page->save(); if (pChats.size() > 0) { - pTab.add_child("chats", pChats); + pTab.add_child("columns", pChats); } tabs.push_back(std::make_pair("", pTab)); diff --git a/widgets/notebookpage.cpp b/widgets/notebookpage.cpp index 015ee4be4..591ecdbe1 100644 --- a/widgets/notebookpage.cpp +++ b/widgets/notebookpage.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -242,30 +243,104 @@ NotebookPage::paintEvent(QPaintEvent *) } } +static std::pair +getWidgetPositionInLayout(QLayout *layout, const ChatWidget *chatWidget) +{ + for (int i = 0; i < layout->count(); ++i) { + printf("xD\n"); + } + + return std::make_pair(-1, -1); +} + +std::pair +NotebookPage::getChatPosition(const ChatWidget *chatWidget) +{ + auto layout = this->hbox.layout(); + + if (layout == nullptr) { + return std::make_pair(-1, -1); + } + + return getWidgetPositionInLayout(layout, chatWidget); +} + void NotebookPage::load(const boost::property_tree::ptree &tree) { try { - BOOST_FOREACH (const boost::property_tree::ptree::value_type &v, - tree.get_child("chats.")) { - auto widget = new ChatWidget(); - widget->load(v.second); - this->addToLayout(widget, std::pair(-1, -1)); + int column = 0; + for (const auto &v : tree.get_child("columns.")) { + int row = 0; + for (const auto &innerV : v.second.get_child("")) { + auto widget = new ChatWidget(); + widget->load(innerV.second); + this->addToLayout(widget, std::pair(column, row)); + ++row; + } + ++column; } } catch (boost::property_tree::ptree_error &) { // can't read tabs } } +static void +saveFromLayout(QLayout *layout, boost::property_tree::ptree &tree) +{ + for (int i = 0; i < layout->count(); ++i) { + auto item = layout->itemAt(i); + + auto innerLayout = item->layout(); + if (innerLayout != nullptr) { + boost::property_tree::ptree innerLayoutTree; + + saveFromLayout(innerLayout, innerLayoutTree); + + if (innerLayoutTree.size() > 0) { + tree.push_back(std::make_pair("", innerLayoutTree)); + } + + continue; + } + + auto widget = item->widget(); + + if (widget == nullptr) { + // This layoutitem does not manage a widget for some reason + continue; + } + + ChatWidget *chatWidget = qobject_cast(widget); + + if (chatWidget != nullptr) { + boost::property_tree::ptree chat = chatWidget->save(); + + tree.push_back(std::make_pair("", chat)); + continue; + } + } +} + boost::property_tree::ptree NotebookPage::save() { boost::property_tree::ptree tree; + auto layout = this->hbox.layout(); + + saveFromLayout(layout, tree); + + /* for (const auto &chat : this->chatWidgets) { boost::property_tree::ptree child = chat->save(); + + // Set child position + child.put("position", "5,3"); + tree.push_back(std::make_pair("", child)); } + */ return tree; } diff --git a/widgets/notebookpage.h b/widgets/notebookpage.h index c7e7f35f5..50c04cc9e 100644 --- a/widgets/notebookpage.h +++ b/widgets/notebookpage.h @@ -72,6 +72,8 @@ protected: private: void setPreviewRect(QPoint mousePos); + std::pair getChatPosition(const ChatWidget *chatWidget); + public: void load(const boost::property_tree::ptree &tree); boost::property_tree::ptree save();