This commit is contained in:
fourtf 2017-01-29 13:23:42 +01:00
commit 04e1ce4ba5
7 changed files with 154 additions and 20 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("columns", pChats);
}
tabs.push_back(std::make_pair("", pTab)); tabs.push_back(std::make_pair("", pTab));
} }

View file

@ -6,6 +6,7 @@
#include <QDebug> #include <QDebug>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QMimeData> #include <QMimeData>
#include <QObject>
#include <QPainter> #include <QPainter>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QWidget> #include <QWidget>
@ -124,7 +125,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));
} }
@ -241,12 +243,106 @@ NotebookPage::paintEvent(QPaintEvent *)
} }
} }
void static std::pair<int, int>
NotebookPage::load(const boost::property_tree::ptree &v) getWidgetPositionInLayout(QLayout *layout, const ChatWidget *chatWidget)
{ {
const std::string &tabName = v.get<std::string>("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<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 {
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<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;
} }
} // namespace widgets } // namespace widgets

View file

@ -72,8 +72,11 @@ protected:
private: private:
void setPreviewRect(QPoint mousePos); void setPreviewRect(QPoint mousePos);
std::pair<int, int> getChatPosition(const ChatWidget *chatWidget);
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

View file

@ -14,7 +14,7 @@ NotebookTab::NotebookTab(Notebook *notebook)
, posAnimated(false) , posAnimated(false)
, posAnimationDesired() , posAnimationDesired()
, notebook(notebook) , notebook(notebook)
, text("<no title>") , title("<no title>")
, selected(false) , selected(false)
, mouseOver(false) , mouseOver(false)
, mouseDown(false) , mouseDown(false)
@ -52,9 +52,9 @@ void
NotebookTab::calcSize() NotebookTab::calcSize()
{ {
if (Settings::getInstance().hideTabX.get()) { if (Settings::getInstance().hideTabX.get()) {
this->resize(this->fontMetrics().width(this->text) + 8, 24); this->resize(this->fontMetrics().width(this->title) + 8, 24);
} else { } 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), width() - (Settings::getInstance().hideTabX.get() ? 0 : 16),
height()); height());
painter.drawText(rect, this->text, QTextOption(Qt::AlignCenter)); painter.drawText(rect, this->title, QTextOption(Qt::AlignCenter));
if (!Settings::getInstance().hideTabX.get() && if (!Settings::getInstance().hideTabX.get() &&
(this->mouseOver || this->selected)) { (this->mouseOver || this->selected)) {
@ -207,9 +207,9 @@ NotebookTab::mouseMoveEvent(QMouseEvent *event)
void void
NotebookTab::load(const boost::property_tree::ptree &tree) NotebookTab::load(const boost::property_tree::ptree &tree)
{ {
// Load tab text // Load tab title
try { try {
this->setText(QString::fromStdString(tree.get<std::string>("text"))); this->setTitle(QString::fromStdString(tree.get<std::string>("title")));
} catch (boost::property_tree::ptree_error) { } catch (boost::property_tree::ptree_error) {
} }
} }
@ -219,7 +219,7 @@ NotebookTab::save()
{ {
boost::property_tree::ptree tree; boost::property_tree::ptree tree;
tree.put("text", this->getText().toStdString()); tree.put("title", this->getTitle().toStdString());
return tree; return tree;
} }

View file

@ -30,15 +30,15 @@ public:
NotebookPage *page; NotebookPage *page;
const QString & const QString &
getText() const getTitle() const
{ {
return this->text; return this->title;
} }
void void
setText(const QString &text) setTitle(const QString &title)
{ {
this->text = text; this->title = title;
} }
bool bool
@ -95,7 +95,7 @@ private:
Notebook *notebook; Notebook *notebook;
QString text; QString title;
bool selected; bool selected;
bool mouseOver; bool mouseOver;