Tabs are now loaded and saved (no chats yet)

This commit is contained in:
Rasmus Karlsson 2017-01-28 22:35:23 +01:00
parent 6fb4305162
commit 1c076b803e
11 changed files with 236 additions and 18 deletions

View file

@ -26,6 +26,8 @@ main(int argc, char *argv[])
ColorScheme::getInstance().setColors(0, -0.8);
Windows::load();
MainWindow &w = Windows::getMainWindow();
w.show();
@ -35,5 +37,7 @@ main(int argc, char *argv[])
Settings::getInstance().save();
Windows::save();
return ret;
}

View file

@ -3,7 +3,9 @@
#include "widgets/chatwidget.h"
#include "widgets/notebook.h"
#include <QDebug>
#include <QPalette>
#include <boost/foreach.hpp>
namespace chatterino {
namespace widgets {
@ -68,5 +70,34 @@ MainWindow::repaintVisibleChatWidgets(Channel *channel)
}
}
}
void
MainWindow::load(const boost::property_tree::ptree &tree)
{
this->notebook.load(tree);
this->loaded = true;
}
boost::property_tree::ptree
MainWindow::save()
{
boost::property_tree::ptree child;
child.put("type", "main");
this->notebook.save(child);
return child;
}
void
MainWindow::loadDefaults()
{
this->notebook.loadDefaults();
this->loaded = true;
}
} // namespace widgets
} // namespace chatterino

View file

@ -4,6 +4,7 @@
#include "widgets/notebook.h"
#include <QMainWindow>
#include <boost/property_tree/ptree.hpp>
namespace chatterino {
namespace widgets {
@ -19,8 +20,22 @@ public:
void layoutVisibleChatWidgets(Channel *channel = NULL);
void repaintVisibleChatWidgets(Channel *channel = NULL);
void load(const boost::property_tree::ptree &tree);
boost::property_tree::ptree save();
void loadDefaults();
bool
isLoaded() const
{
return this->loaded;
}
private:
bool loaded = false;
};
}
}
} // namespace widgets
} // namespace chatterino
#endif // MAINWINDOW_H

View file

@ -5,10 +5,14 @@
#include "widgets/notebooktab.h"
#include "widgets/settingsdialog.h"
#include <QDebug>
#include <QFile>
#include <QFormLayout>
#include <QLayout>
#include <QList>
#include <QStandardPaths>
#include <QWidget>
#include <boost/foreach.hpp>
namespace chatterino {
namespace widgets {
@ -35,8 +39,6 @@ Notebook::Notebook(QWidget *parent)
this->userButton.icon = NotebookButton::IconUser;
this->addButton.resize(24, 24);
this->addPage();
}
NotebookPage *
@ -191,5 +193,50 @@ Notebook::addPageButtonClicked()
{
addPage(true);
}
void
Notebook::load(const boost::property_tree::ptree &tree)
{
// Read a list of tabs
try {
BOOST_FOREACH (const boost::property_tree::ptree::value_type &v,
tree.get_child("tabs.")) {
bool select = v.second.get<bool>("selected", false);
auto page = this->addPage(select);
auto tab = page->tab;
tab->load(v.second);
page->load(v.second);
}
} catch (boost::property_tree::ptree_error &) {
// can't read tabs
}
if (this->pages.size() == 0) {
// No pages saved, show default stuff
this->loadDefaults();
}
}
void
Notebook::save(boost::property_tree::ptree &tree)
{
boost::property_tree::ptree tabs;
// 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();
tabs.push_back(std::make_pair("", pTab));
}
tree.add_child("tabs", tabs);
}
void
Notebook::loadDefaults()
{
this->addPage();
}
} // namespace widgets
} // namespace chatterino

View file

@ -7,6 +7,7 @@
#include <QList>
#include <QWidget>
#include <boost/property_tree/ptree.hpp>
namespace chatterino {
namespace widgets {
@ -54,8 +55,14 @@ private:
NotebookButton userButton;
NotebookPage *selectedPage;
public:
void load(const boost::property_tree::ptree &tree);
void save(boost::property_tree::ptree &tree);
void loadDefaults();
};
}
}
} // namespace widgets
} // namespace chatterino
#endif // NOTEBOOK_H

View file

@ -3,11 +3,13 @@
#include "widgets/chatwidget.h"
#include "widgets/notebooktab.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QMimeData>
#include <QPainter>
#include <QVBoxLayout>
#include <QWidget>
#include <boost/foreach.hpp>
namespace chatterino {
namespace widgets {
@ -73,9 +75,8 @@ NotebookPage::removeFromLayout(ChatWidget *widget)
}
void
NotebookPage::addToLayout(
ChatWidget *widget,
std::pair<int, int> position = std::pair<int, int>(-1, -1))
NotebookPage::addToLayout(ChatWidget *widget, std::pair<int, int> position =
std::pair<int, int>(-1, -1))
{
this->chatWidgets.push_back(widget);
@ -239,5 +240,14 @@ NotebookPage::paintEvent(QPaintEvent *)
ColorScheme::getInstance().TabSelectedBackground);
}
}
void
NotebookPage::load(const boost::property_tree::ptree &v)
{
const std::string &tabName = v.get<std::string>("name", "UNNAMED");
qDebug() << "tab name :" << tabName.c_str();
}
}
} // namespace widgets
} // namespace chatterino

View file

@ -12,6 +12,7 @@
#include <QVBoxLayout>
#include <QVector>
#include <QWidget>
#include <boost/property_tree/ptree.hpp>
namespace chatterino {
namespace widgets {
@ -70,8 +71,12 @@ protected:
private:
void setPreviewRect(QPoint mousePos);
public:
void load(const boost::property_tree::ptree &v);
};
}
}
} // namespace widgets
} // namespace chatterino
#endif // NOTEBOOKPAGE_H

View file

@ -203,5 +203,26 @@ NotebookTab::mouseMoveEvent(QMouseEvent *event)
}
}
}
void
NotebookTab::load(const boost::property_tree::ptree &tree)
{
// Load tab text
try {
this->setText(QString::fromStdString(tree.get<std::string>("text")));
} catch (boost::property_tree::ptree_error) {
}
}
boost::property_tree::ptree
NotebookTab::save()
{
boost::property_tree::ptree tree;
tree.put("text", this->getText().toStdString());
return tree;
}
} // namespace widgets
} // namespace chatterino

View file

@ -3,6 +3,7 @@
#include <QPropertyAnimation>
#include <QWidget>
#include <boost/property_tree/ptree.hpp>
namespace chatterino {
namespace widgets {
@ -117,8 +118,13 @@ private slots:
calcSize();
update();
}
public:
void load(const boost::property_tree::ptree &tree);
boost::property_tree::ptree save();
};
}
}
} // namespace widgets
} // namespace chatterino
#endif // NOTEBOOKTAB_H

View file

@ -1,7 +1,23 @@
#include "windows.h"
#include <QDebug>
#include <QStandardPaths>
#include <boost/foreach.hpp>
#include <boost/property_tree/json_parser.hpp>
namespace chatterino {
static const std::string &
getSettingsPath()
{
static std::string path =
(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
"/windows.json")
.toStdString();
return path;
}
QMutex Windows::windowMutex;
widgets::MainWindow *Windows::mainWindow(nullptr);
@ -23,12 +39,67 @@ Windows::repaintVisibleChatWidgets(Channel *channel)
}
void
Windows::save()
Windows::load()
{
const auto &settingsPath = getSettingsPath();
boost::property_tree::ptree tree;
try {
boost::property_tree::read_json(settingsPath, tree);
} catch (const boost::property_tree::json_parser_error &ex) {
qDebug() << "Error using property_tree::readJson: "
<< QString::fromStdString(ex.message());
Windows::getMainWindow().loadDefaults();
return;
}
// Read a list of windows
try {
BOOST_FOREACH (const boost::property_tree::ptree::value_type &v,
tree.get_child("windows.")) {
qDebug() << QString::fromStdString(v.first.data());
const auto &type = v.second.get<std::string>("type", "unknown");
if (type == "main") {
Windows::getMainWindow().load(v.second);
} else {
qDebug() << "Unhandled window type: " << type.c_str();
}
}
} catch (boost::property_tree::ptree_error &) {
// can't read windows
}
// if the main window was not loaded properly, load defaults
if (!Windows::getMainWindow().isLoaded()) {
Windows::getMainWindow().loadDefaults();
}
// If there are no windows, create a default main window
}
void
Windows::load()
Windows::save()
{
const auto &settingsPath = getSettingsPath();
boost::property_tree::ptree tree;
// Create windows array
boost::property_tree::ptree windows;
{
// save main window
auto child = Windows::getMainWindow().save();
windows.push_back(std::make_pair("", child));
}
// TODO: iterate through rest of windows and add them to the "windows" ptree
tree.add_child("windows", windows);
boost::property_tree::write_json(settingsPath, tree);
}
}
} // namespace chatterino

View file

@ -37,6 +37,7 @@ private:
static widgets::MainWindow *mainWindow;
};
}
} // namespace chatterino
#endif // WINDOWS_H