diff --git a/widgets/chatwidget.cpp b/widgets/chatwidget.cpp index 133c9ec04..681d9894b 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..cf1ddb8f3 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("columns", pChats); + } + tabs.push_back(std::make_pair("", pTab)); } diff --git a/widgets/notebookpage.cpp b/widgets/notebookpage.cpp index d2e1582b7..591ecdbe1 100644 --- a/widgets/notebookpage.cpp +++ b/widgets/notebookpage.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -124,7 +125,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)); } @@ -241,12 +243,106 @@ NotebookPage::paintEvent(QPaintEvent *) } } -void -NotebookPage::load(const boost::property_tree::ptree &v) +static std::pair +getWidgetPositionInLayout(QLayout *layout, const ChatWidget *chatWidget) { - const std::string &tabName = v.get("name", "UNNAMED"); + for (int i = 0; i < layout->count(); ++i) { + printf("xD\n"); + } - qDebug() << "tab name :" << tabName.c_str(); + 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 { + 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; } } // namespace widgets diff --git a/widgets/notebookpage.h b/widgets/notebookpage.h index 926635233..50c04cc9e 100644 --- a/widgets/notebookpage.h +++ b/widgets/notebookpage.h @@ -72,8 +72,11 @@ protected: private: void setPreviewRect(QPoint mousePos); + std::pair getChatPosition(const ChatWidget *chatWidget); + public: - void load(const boost::property_tree::ptree &v); + void load(const boost::property_tree::ptree &tree); + boost::property_tree::ptree save(); }; } // namespace widgets 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;