From 396bc0c1721af8d3ee970228e7df457f6b2c73d5 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 13 Apr 2020 15:44:09 +0300 Subject: Rewrite Session backend Add session.hpp, containing structs that describe session data MainWindow, SubWindow and WebView can be created from Session::structs Opening new window will automatically open a default subwindow and tab if none were specified Add lib/session_formats Add JsonSession, to serialize/deserialize Session structs into JSON - add some tests clang-tidy: - fix various warnings - disable modernize-use-trailing-return-type check --- src/session/sessiondialog.cpp | 89 +++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 53 deletions(-) (limited to 'src/session/sessiondialog.cpp') diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp index 301b4b6..36cf9cb 100644 --- a/src/session/sessiondialog.cpp +++ b/src/session/sessiondialog.cpp @@ -1,7 +1,7 @@ /* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ @@ -9,56 +9,59 @@ #include "sessiondialog.h" #include "../browser.h" #include "../util.h" -#include "sessionform.h" +#include "configuration.h" +#include "session_json.hpp" #include "ui_sessiondialog.h" -#include "ui_sessionform.h" #include #include -#include #include -#include "configuration.h" +#include SessionDialog::SessionDialog(QWidget *parent) : QDialog(parent) + , create_window_str(QObject::tr("Create a new window")) , ui(new Ui::SessionDialog) { ui->setupUi(this); - ui->open->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon)); auto *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); Configuration conf; for(const QString &path : Util::files(conf.value("session.path").value(), { "*.json" })) { - addItem(path); + ui->listWidget->addItem(path); } - ui->listWidget->addItem(tr("Create a new window")); + ui->listWidget->addItem(create_window_str); - connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem *previousItem) { - auto *currentWidget = qobject_cast(ui->listWidget->itemWidget(currentItem)); - if(currentWidget != nullptr) - currentWidget->ui->delete_toolButton->show(); - - auto *previousWidget = qobject_cast(ui->listWidget->itemWidget(previousItem)); - if(previousWidget != nullptr) - previousWidget->ui->delete_toolButton->hide(); + connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem * /*previousItem*/) { + ui->delete_toolButton->setEnabled(currentItem->text() != create_window_str); }); - connect(ui->open, &QPushButton::clicked, this, [this]() { + connect(ui->open_toolButton, &QToolButton::clicked, this, [this]() { const QString filename = QFileDialog::getOpenFileName(this, tr("Select Session file"), QDir::homePath(), tr("JSON (*.json)")); if(!filename.isEmpty()) { - ui->listWidget->setCurrentItem(addItem(filename)); + ui->listWidget->addItem(filename); + ui->listWidget->setCurrentRow(ui->listWidget->count() - 1); accept(); } }); + connect(ui->delete_toolButton, &QToolButton::clicked, this, [this]() { + const auto path = ui->listWidget->currentItem()->text(); + if(path != create_window_str) { + QFile(path).remove(); + delete ui->listWidget->currentItem(); + } + }); + accepted_connection = connect(this, &SessionDialog::accepted, this, [this, browser]() { - auto *currentWidget = qobject_cast(ui->listWidget->itemWidget(ui->listWidget->currentItem())); - if(currentWidget != nullptr) - this->openSession(currentWidget->ui->label->text()); - else - browser->createWindow(); + const auto path = ui->listWidget->currentItem()->text(); + if(path != create_window_str) { + openSession(path); + } else { + browser->open({}); + } }); connect(ui->search, &QLineEdit::textEdited, this, &SessionDialog::search); @@ -69,7 +72,7 @@ SessionDialog::~SessionDialog() delete ui; } -std::optional SessionDialog::pickSession() +std::optional> SessionDialog::pickSession() { disconnect(accepted_connection); @@ -77,52 +80,32 @@ std::optional SessionDialog::pickSession() return std::nullopt; } - auto *currentWidget = qobject_cast(ui->listWidget->itemWidget(ui->listWidget->currentItem())); - if(currentWidget != nullptr) { - QFile json(currentWidget->ui->label->text()); + const auto path = ui->listWidget->currentItem()->text(); + if(path != create_window_str) { + QFile json(path); if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto doc = QJsonDocument::fromJson(json.readAll()); - return doc.object(); + JsonSession session(json.readAll()); + return session.get(); } } return std::nullopt; } -QListWidgetItem *SessionDialog::addItem(const QString &path) -{ - auto *item = new QListWidgetItem(ui->listWidget); - auto *widget = new SessionForm(path, this); - connect(widget->ui->delete_toolButton, &QToolButton::clicked, this, [item, widget]() { - delete item; - delete widget; - }); - ui->listWidget->setItemWidget(item, widget); - return item; -} - void SessionDialog::search(const QString &text) { for(int i = 0; i < ui->listWidget->count(); ++i) { auto *item = ui->listWidget->item(i); - auto *widget = qobject_cast(ui->listWidget->itemWidget(item)); - - if(widget == nullptr) - item->setHidden(!text.isEmpty()); - else - item->setHidden(!widget->ui->label->text().contains(text)); + item->setHidden(!item->text().contains(text)); } } void SessionDialog::openSession(const QString &filename) { auto *browser = qobject_cast(qApp); - Q_CHECK_PTR(browser); - QFile json(filename); - if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto doc = QJsonDocument::fromJson(json.readAll()); - Session::restoreSession(doc.object()); - json.close(); + if(json.open(QIODevice::ReadOnly | QIODevice::Text) && browser != nullptr) { + JsonSession session(json.readAll()); + browser->open(session.get()); } } -- cgit v1.2.1