split layout is now saved too

This commit is contained in:
Rasmus Karlsson 2017-01-29 12:26:22 +01:00
parent 199e87896b
commit 3266f8ad4f
3 changed files with 83 additions and 6 deletions

View file

@ -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));

View file

@ -6,6 +6,7 @@
#include <QDebug>
#include <QHBoxLayout>
#include <QMimeData>
#include <QObject>
#include <QPainter>
#include <QVBoxLayout>
#include <QWidget>
@ -242,30 +243,104 @@ NotebookPage::paintEvent(QPaintEvent *)
}
}
static std::pair<int, int>
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<int, int>
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.")) {
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(v.second);
this->addToLayout(widget, std::pair<int, int>(-1, -1));
widget->load(innerV.second);
this->addToLayout(widget, std::pair<int, int>(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<ChatWidget *>(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;
}

View file

@ -72,6 +72,8 @@ protected:
private:
void setPreviewRect(QPoint mousePos);
std::pair<int, int> getChatPosition(const ChatWidget *chatWidget);
public:
void load(const boost::property_tree::ptree &tree);
boost::property_tree::ptree save();