diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-30 14:08:02 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-30 14:08:02 +0100 |
commit | fbdaade51ceaea14998c611acf2a09d3198be640 (patch) | |
tree | 0b578c1eb01f259c482dca806bea4dc24dccc480 /src/session | |
parent | Add --session and --pick-session (diff) | |
download | smolbote-fbdaade51ceaea14998c611acf2a09d3198be640.tar.xz |
pick-session: pass session data to existing instance if any
Diffstat (limited to 'src/session')
-rw-r--r-- | src/session/sessiondialog.cpp | 47 | ||||
-rw-r--r-- | src/session/sessiondialog.h | 6 |
2 files changed, 41 insertions, 12 deletions
diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp index 287950d..184c44d 100644 --- a/src/session/sessiondialog.cpp +++ b/src/session/sessiondialog.cpp @@ -28,13 +28,7 @@ SessionDialog::SessionDialog(QWidget *parent) Q_CHECK_PTR(browser); for(const QString &path : Util::files(browser->configuration("browser.session.path"), { "*.json" })) { - 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); + addItem(path); } ui->listWidget->addItem(tr("Create a new window")); @@ -52,13 +46,12 @@ SessionDialog::SessionDialog(QWidget *parent) connect(ui->open, &QPushButton::clicked, this, [this]() { const QString filename = QFileDialog::getOpenFileName(this, tr("Select Session file"), QDir::homePath(), tr("JSON (*.json)")); if(!filename.isEmpty()) { - this->openSession(filename); - // close the dialog window; reject so it doesn't open another session - this->reject(); + ui->listWidget->setCurrentItem(addItem(filename)); + accept(); } }); - connect(this, &SessionDialog::accepted, this, [this, browser]() { + accepted_connection = connect(this, &SessionDialog::accepted, this, [this, browser]() { auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(ui->listWidget->currentItem())); if(currentWidget) this->openSession(currentWidget->ui->label->text()); @@ -74,6 +67,38 @@ SessionDialog::~SessionDialog() delete ui; } +std::optional<QJsonObject> SessionDialog::pickSession() +{ + disconnect(accepted_connection); + + if(this->exec() == QDialog::Rejected) { + return std::nullopt; + } + + auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(ui->listWidget->currentItem())); + if(currentWidget) { + QFile json(currentWidget->ui->label->text()); + if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { + auto doc = QJsonDocument::fromJson(json.readAll()); + return doc.object(); + } + } + + 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) { diff --git a/src/session/sessiondialog.h b/src/session/sessiondialog.h index ff4d3af..9bf8a68 100644 --- a/src/session/sessiondialog.h +++ b/src/session/sessiondialog.h @@ -15,7 +15,7 @@ namespace Ui { class SessionDialog; } - +class QListWidgetItem; class SessionDialog : public QDialog { Q_OBJECT @@ -24,12 +24,16 @@ public: explicit SessionDialog(QWidget *parent = nullptr); ~SessionDialog() override; + std::optional<QJsonObject> pickSession(); + private slots: + QListWidgetItem *addItem(const QString &path); void search(const QString &text); void openSession(const QString &filename); private: Ui::SessionDialog *ui; + QMetaObject::Connection accepted_connection; }; #endif // SMOLBOTE_SESSIONDIALOG_H |