From 09c4508aee96ca20d084b8a60b4c6603de8bff8b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 25 Nov 2018 13:01:17 +0100 Subject: Add Session Dialog --- src/session/sessiondialog.cpp | 87 +++++++++++++++++++++++++++++++++++++++ src/session/sessiondialog.h | 34 +++++++++++++++ src/session/sessiondialog.ui | 96 +++++++++++++++++++++++++++++++++++++++++++ src/session/sessionform.cpp | 36 ++++++++++++++++ src/session/sessionform.h | 37 +++++++++++++++++ src/session/sessionform.ui | 41 ++++++++++++++++++ 6 files changed, 331 insertions(+) create mode 100644 src/session/sessiondialog.cpp create mode 100644 src/session/sessiondialog.h create mode 100644 src/session/sessiondialog.ui create mode 100644 src/session/sessionform.cpp create mode 100644 src/session/sessionform.h create mode 100644 src/session/sessionform.ui (limited to 'src/session') diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp new file mode 100644 index 0000000..e31a42f --- /dev/null +++ b/src/session/sessiondialog.cpp @@ -0,0 +1,87 @@ +/* + * 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 + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "sessiondialog.h" +#include "../browser.h" +#include "../util.h" +#include "sessionform.h" +#include "ui_sessiondialog.h" +#include "ui_sessionform.h" +#include +#include +#include +#include + +SessionDialog::SessionDialog(QWidget *parent) + : QDialog(parent) + , ui(new Ui::SessionDialog) +{ + ui->setupUi(this); + ui->open->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon)); + + auto *browser = qobject_cast(qApp); + 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); + } + +#ifdef QT_DEBUG + ui->listWidget->addItem(tr("Start new session")); +#endif + + connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem *previousItem) { + auto *currentWidget = qobject_cast(ui->listWidget->itemWidget(currentItem)); + if(currentWidget) + currentWidget->ui->delete_toolButton->show(); + + auto *previousWidget = qobject_cast(ui->listWidget->itemWidget(previousItem)); + if(previousWidget) + previousWidget->ui->delete_toolButton->hide(); + }); + + 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(); + } + }); + + connect(this, &SessionDialog::accepted, this, [this]() { + auto *currentWidget = qobject_cast(ui->listWidget->itemWidget(ui->listWidget->currentItem())); + if(currentWidget) + this->openSession(currentWidget->ui->label->text()); + }); +} + +SessionDialog::~SessionDialog() +{ + delete ui; +} + +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 *browser = qobject_cast(qApp); + auto doc = QJsonDocument::fromJson(json.readAll()); + browser->createSession(doc.object()); + json.close(); + } +} diff --git a/src/session/sessiondialog.h b/src/session/sessiondialog.h new file mode 100644 index 0000000..7fea3c2 --- /dev/null +++ b/src/session/sessiondialog.h @@ -0,0 +1,34 @@ +/* + * 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 + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_SESSIONDIALOG_H +#define SMOLBOTE_SESSIONDIALOG_H + +#include + +namespace Ui +{ +class SessionDialog; +} + +class SessionDialog : public QDialog +{ + Q_OBJECT + +public: + explicit SessionDialog(QWidget *parent = nullptr); + ~SessionDialog() override; + +private: + void openSession(const QString &filename); + +private: + Ui::SessionDialog *ui; +}; + +#endif // SMOLBOTE_SESSIONDIALOG_H diff --git a/src/session/sessiondialog.ui b/src/session/sessiondialog.ui new file mode 100644 index 0000000..3a61dc1 --- /dev/null +++ b/src/session/sessiondialog.ui @@ -0,0 +1,96 @@ + + + SessionDialog + + + + 0 + 0 + 1000 + 600 + + + + Select Session + + + + + + + + Filter + + + + + + + Search + + + + + + + + + + + + + + Open from File + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close|QDialogButtonBox::Open + + + + + + + + + + + buttonBox + accepted() + SessionDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + SessionDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/session/sessionform.cpp b/src/session/sessionform.cpp new file mode 100644 index 0000000..761cb42 --- /dev/null +++ b/src/session/sessionform.cpp @@ -0,0 +1,36 @@ +/* + * 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 + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "sessionform.h" +#include "ui_sessionform.h" +#include + +SessionForm::SessionForm(const QString &path, QWidget *parent) + : QWidget(parent) + , ui(new Ui::SessionForm) +{ + ui->setupUi(this); + ui->label->setText(path); + ui->delete_toolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon)); + ui->delete_toolButton->hide(); +} + +SessionForm::~SessionForm() +{ + delete ui; +} + +void SessionForm::showDetails() +{ + ui->delete_toolButton->show(); +} + +void SessionForm::hideDetails() +{ + ui->delete_toolButton->hide(); +} diff --git a/src/session/sessionform.h b/src/session/sessionform.h new file mode 100644 index 0000000..5e5f52e --- /dev/null +++ b/src/session/sessionform.h @@ -0,0 +1,37 @@ +/* + * 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 + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_SESSIONFORM_H +#define SMOLBOTE_SESSIONFORM_H + +#include + +namespace Ui +{ +class SessionForm; +} + +class SessionForm : public QWidget +{ + Q_OBJECT + + friend class SessionDialog; + +public: + explicit SessionForm(const QString &path, QWidget *parent = nullptr); + ~SessionForm() override; + +public slots: + void showDetails(); + void hideDetails(); + +private: + Ui::SessionForm *ui; +}; + +#endif // SMOLBOTE_SESSIONFORM_H diff --git a/src/session/sessionform.ui b/src/session/sessionform.ui new file mode 100644 index 0000000..dbf0237 --- /dev/null +++ b/src/session/sessionform.ui @@ -0,0 +1,41 @@ + + + SessionForm + + + + 0 + 0 + 711 + 34 + + + + Form + + + + 0 + + + 0 + + + + + TextLabel + + + + + + + ... + + + + + + + + -- cgit v1.2.1