Skip to content

Commit

Permalink
GUI: Make window appear faster by running the 'populate tree' functio…
Browse files Browse the repository at this point in the history
…n concurrently
  • Loading branch information
fdde authored and DrMcCoy committed Dec 27, 2017
1 parent fbcf307 commit 24476d7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,11 @@ find_package(Qt5Core REQUIRED)
find_package(Qt5MultimediaWidgets REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Concurrent REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5MultimediaWidgets_INCLUDE_DIRS})
list(APPEND PHAETHON_LIBRARIES Qt5::Core Qt5::Widgets Qt5::MultimediaWidgets Qt5::Gui)
include_directories(${Qt5Concurrent_INCLUDE_DIRS})
list(APPEND PHAETHON_LIBRARIES Qt5::Core Qt5::Widgets Qt5::MultimediaWidgets Qt5::Gui Qt5::Concurrent)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

find_package(Iconv REQUIRED)
Expand Down
8 changes: 7 additions & 1 deletion m4/ax_check_qt5.m4
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ dnl @version 2017-02-02
dnl @license Creative Commons CC0 1.0 Universal Public Domain Dedication

AC_DEFUN([AX_CHECK_QT5], [
PKG_CHECK_MODULES([QT5], [Qt5Core >= $1 Qt5Gui >= $1 Qt5Widgets >= $1 Qt5Multimedia >= $1], [$2], [$3])
PKG_CHECK_MODULES([QT5], [ \
Qt5Core >= $1 \
Qt5Gui >= $1 \
Qt5Widgets >= $1 \
Qt5Multimedia >= $1 \
Qt5Concurrent >= $1 \
], [$2], [$3])
# Qt5 needs -fPIC...
QT5_CFLAGS="$QT5_CFLAGS -fPIC"
Expand Down
38 changes: 20 additions & 18 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,23 +251,7 @@ void MainWindow::slotLog(const QString &text) {
_log->append(text);
}

void MainWindow::setTreeViewModel(const QString &path) {
if (_rootPath == path)
return;
_rootPath = path;

_status->push("Populating resource tree...");
BOOST_SCOPE_EXIT((&_status)) {
_status->pop();
} BOOST_SCOPE_EXIT_END

_treeView->setModel(nullptr);

if (_treeModel)
delete _treeModel;

_treeModel = new ResourceTree(this, path, _treeView);

void MainWindow::finishTree() {
if (_proxyModel)
delete _proxyModel;

Expand All @@ -283,10 +267,28 @@ void MainWindow::setTreeViewModel(const QString &path) {

_treeView->resizeColumnToContents(0);


QObject::connect(_treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &MainWindow::resourceSelect);

_status->setText("Idle...");
}

void MainWindow::setTreeViewModel(const QString &path) {
if (_rootPath == path)
return;
_rootPath = path;

_status->push("Populating resource tree...");
BOOST_SCOPE_EXIT((&_status)) {
_status->pop();
} BOOST_SCOPE_EXIT_END

_treeView->setModel(nullptr);

if (_treeModel)
delete _treeModel;

_treeModel = new ResourceTree(this, path, _treeView);

_log->append(tr("Set root: %1").arg(path));

Expand Down
1 change: 1 addition & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class MainWindow : public QMainWindow {
~MainWindow();

std::shared_ptr<StatusBar> status();
void finishTree();

private /*slots*/:
void setTreeViewModel(const QString &path);
Expand Down
12 changes: 9 additions & 3 deletions src/gui/resourcetree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/

#include <QDir>
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrentRun>
#include <boost/scope_exit.hpp>

#include "verdigris/wobjectimpl.h"
Expand Down Expand Up @@ -52,7 +55,7 @@ ResourceTree::ResourceTree(MainWindow *mainWindow, const QString &path, QObject

_root = new ResourceTreeItem("Filename", nullptr);

_mainWindow->status()->push("Populating resource tree...");
_mainWindow->status()->setText("Populating resource tree...");

ResourceTreeItem *top = _root;

Expand All @@ -63,9 +66,12 @@ ResourceTree::ResourceTree(MainWindow *mainWindow, const QString &path, QObject
_root->appendChild(top);
}

populate(path, top);
QFutureWatcher<void> *watcher = new QFutureWatcher<void>;

_mainWindow->status()->pop();
connect(watcher, &QFutureWatcher<void>::finished, _mainWindow, &MainWindow::finishTree);

QFuture<void> future = QtConcurrent::run(this, &ResourceTree::populate, path, top);
watcher->setFuture(future);
}

ResourceTree::~ResourceTree() {
Expand Down

0 comments on commit 24476d7

Please sign in to comment.