aboutsummaryrefslogtreecommitdiffstats
path: root/src/mainwindow.cpp
diff options
context:
space:
mode:
authorConfuSomu2023-07-10 13:27:34 +0200
committerConfuSomu2023-07-10 13:42:16 +0200
commit5befd0666aef0b96e3e4bd41ed704ccaa3ace5d5 (patch)
tree76402efb62ecf9bc1817910179afc7f71b646e32 /src/mainwindow.cpp
parent9b70e3c5a208a881188bc6468fe27933cbdb02f8 (diff)
downloadActorViewer-5befd0666aef0b96e3e4bd41ed704ccaa3ace5d5.tar
ActorViewer-5befd0666aef0b96e3e4bd41ed704ccaa3ace5d5.tar.gz
ActorViewer-5befd0666aef0b96e3e4bd41ed704ccaa3ace5d5.zip
Implement basic multithreading support
A few CPU heavy operations, which are opening an archive and displaying a selected Activity, have been moved to another thread to avoid having long-running operations on the main thread.
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r--src/mainwindow.cpp40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index ede24f8..363f2b1 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -9,6 +9,7 @@
#include <QRandomGenerator>
#include <QClipboard>
#include <QMimeData>
+#include <QtConcurrent/QtConcurrent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@@ -17,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this);
connect(ui->buttonCopy, &QPushButton::clicked, ui->actionCopy_status, &QAction::trigger);
connect(ui->buttonRandom, &QPushButton::clicked, ui->actionRandom_status, &QAction::trigger);
+ connect(&archive_thread_watcher, &QFutureWatcher<std::variant<QString, Archive::InitError>>::finished, this, &MainWindow::archive_thread_watcher_done);
}
MainWindow::~MainWindow()
@@ -60,15 +62,22 @@ void MainWindow::on_actionAbout_triggered(bool checked) {
QMessageBox::information(this, "title", "text");
}
+std::variant<QString, Archive::InitError> start_listWidget_itemActivated(StatusListItem* status, int text_zone_width, QLocale* locale) {
+ return (QString)status->get_info_html(text_zone_width, locale);
+}
+
void MainWindow::on_listWidget_itemActivated(QListWidgetItem *item) {
StatusListItem* status = dynamic_cast<StatusListItem*>(item);
if (status != nullptr) {
- // TODO: do this in another thread
- QString status_info = status->get_info_html(ui->statusInfoText->width(), &locale_context);
- ui->statusInfoText->setHtml(status_info);
+ const QFuture<std::variant<QString, Archive::InitError>> status_info = QtConcurrent::run(start_listWidget_itemActivated, status, ui->statusInfoText->width(), &locale_context);
+ archive_thread_watcher.setFuture(status_info);
}
}
+void MainWindow::finish_listWidget_itemActivated(const QString& status_info) {
+ ui->statusInfoText->setHtml(status_info);
+}
+
void MainWindow::on_actionRandom_status_triggered(bool checked) {
if (data_archive == nullptr) return; // No archive open, avoids crashing
@@ -201,14 +210,19 @@ void MainWindow::open_file(const QString &filename) {
data_archive = nullptr;
}
- // TODO: Do this in another thread
+ open_file_filename = filename;
+
QApplication::setOverrideCursor(Qt::WaitCursor);
data_archive = new Archive(filename, ArchiveType::MASTODON);
- auto parse_error = data_archive->init();
+ QFuture<std::variant<QString, Archive::InitError>> parse_error = QtConcurrent::run(data_archive, &Archive::init);
+ archive_thread_watcher.setFuture(parse_error);
+}
+
+void MainWindow::finish_open_file(const Archive::InitError& parse_error) {
switch (parse_error) {
case Archive::FailedOpeningFile:
- QMessageBox::warning(this, tr("Archive Parser"), tr("Failed opening file %1").arg(filename)); break;
+ QMessageBox::warning(this, tr("Archive Parser"), tr("Failed opening file %1").arg(open_file_filename)); break;
case Archive::JsonParseError:
QMessageBox::warning(this, tr("Archive Parser"), tr("Failed parsing outbox JSON.")); break;
case Archive::JsonEmpty:
@@ -228,3 +242,17 @@ void MainWindow::open_file(const QString &filename) {
QApplication::restoreOverrideCursor();
}
+
+void MainWindow::archive_thread_watcher_done() {
+ std::variant<QString, Archive::InitError> result = archive_thread_watcher.result();
+
+ // For MainWindow::on_listWidget_itemActivated
+ if (const QString* status_info = std::get_if<QString>(&result)) {
+ finish_listWidget_itemActivated(*status_info);
+ } else
+ // For MainWindow::open_file
+ if (const Archive::InitError* parse_error = std::get_if<Archive::InitError>(&result)) {
+ finish_open_file(*parse_error);
+ } else
+ qDebug() << "What, the variant is weird";
+}