we now load and save chats as well

This commit is contained in:
Rasmus Karlsson 2017-01-29 11:38:00 +01:00
parent 1c076b803e
commit c0b4035e6a
5 changed files with 64 additions and 7 deletions

View file

@ -83,5 +83,27 @@ ChatWidget::paintEvent(QPaintEvent *)
painter.fillRect(this->rect(), ColorScheme::getInstance().ChatBackground); 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<std::string>("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

View file

@ -10,6 +10,7 @@
#include <QFont> #include <QFont>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QWidget> #include <QWidget>
#include <boost/property_tree/ptree.hpp>
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -56,8 +57,13 @@ private:
ChatWidgetHeader header; ChatWidgetHeader header;
ChatWidgetView view; ChatWidgetView view;
ChatWidgetInput input; ChatWidgetInput input;
public:
void load(const boost::property_tree::ptree &tree);
boost::property_tree::ptree save();
}; };
}
} } // namespace widgets
} // namespace chatterino
#endif // CHATWIDGET_H #endif // CHATWIDGET_H

View file

@ -226,6 +226,13 @@ Notebook::save(boost::property_tree::ptree &tree)
// 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 : this->pages) { for (const auto &page : this->pages) {
boost::property_tree::ptree pTab = page->tab->save(); 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)); tabs.push_back(std::make_pair("", pTab));
} }

View file

@ -124,7 +124,8 @@ void
NotebookPage::mouseReleaseEvent(QMouseEvent *event) NotebookPage::mouseReleaseEvent(QMouseEvent *event)
{ {
if (this->hbox.count() == 0 && event->button() == Qt::LeftButton) { if (this->hbox.count() == 0 && event->button() == Qt::LeftButton) {
addToLayout(new ChatWidget(), std::pair<int, int>(-1, -1)); // "Add Chat" was clicked
this->addToLayout(new ChatWidget(), std::pair<int, int>(-1, -1));
setCursor(QCursor(Qt::ArrowCursor)); setCursor(QCursor(Qt::ArrowCursor));
} }
@ -242,11 +243,31 @@ NotebookPage::paintEvent(QPaintEvent *)
} }
void void
NotebookPage::load(const boost::property_tree::ptree &v) NotebookPage::load(const boost::property_tree::ptree &tree)
{ {
const std::string &tabName = v.get<std::string>("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<int, int>(-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 } // namespace widgets

View file

@ -73,7 +73,8 @@ private:
void setPreviewRect(QPoint mousePos); void setPreviewRect(QPoint mousePos);
public: public:
void load(const boost::property_tree::ptree &v); void load(const boost::property_tree::ptree &tree);
boost::property_tree::ptree save();
}; };
} // namespace widgets } // namespace widgets