Copy the massif-visualizer GUI pattern with open/load/result pages.
authorMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 18:27:09 +0000 (20:27 +0200)
committerMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 18:37:42 +0000 (20:37 +0200)
Also load the data in a background thread with ThreadWeaver.

CMakeLists.txt
gui/CMakeLists.txt
gui/gui.cpp
gui/mainwindow.cpp
gui/mainwindow.h
gui/mainwindow.ui
gui/model.cpp

index 58ff41a..f7a4b71 100644 (file)
@@ -16,7 +16,7 @@ include(FeatureSummary)
 
 if(Qt5_FOUND AND ECM_FOUND)
     set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
-    find_package(KF5 OPTIONAL_COMPONENTS CoreAddons I18n ItemModels)
+    find_package(KF5 OPTIONAL_COMPONENTS CoreAddons I18n ItemModels ThreadWeaver ConfigWidgets I18n)
     if (KF5_FOUND)
         add_subdirectory(gui)
     endif()
index ce1f6a8..f2af535 100644 (file)
@@ -30,6 +30,9 @@ add_definitions(-Wall
 target_link_libraries(heaptrack_gui
     Qt5::Widgets
     KF5::ItemModels
+    KF5::ThreadWeaver
+    KF5::ConfigWidgets
+    KF5::I18n
     ${Boost_LIBRARIES}
     sharedprint)
 
index 1304f9e..c65054e 100644 (file)
@@ -29,13 +29,10 @@ int main(int argc, char** argv)
 {
     QApplication app(argc, argv);
 
-    if (app.arguments().size() != 2) {
-        cerr << "Missing path to heaptrack data file." << endl;
-        return 1;
-    }
-
     MainWindow window;
-    window.loadFile(app.arguments().last());
+    if (app.arguments().size() > 1) {
+        window.loadFile(app.arguments().last());
+    }
     window.show();
 
     return app.exec();
index ac4bf69..76f7375 100644 (file)
 #include <ui_mainwindow.h>
 
 #include <KRecursiveFilterProxyModel>
+#include <KStandardAction>
+#include <KLocalizedString>
 
-#include <iostream>
+#include <QFileDialog>
 
 #include "model.h"
 #include "proxy.h"
@@ -40,6 +42,11 @@ MainWindow::MainWindow(QWidget* parent)
     proxy->setSourceModel(m_model);
     m_ui->results->setModel(proxy);
 
+    m_ui->pages->setCurrentWidget(m_ui->openPage);
+    // TODO: proper progress report
+    m_ui->loadingProgress->setMinimum(0);
+    m_ui->loadingProgress->setMaximum(0);
+
     connect(m_model, &Model::dataReady,
             this, &MainWindow::dataReady);
 
@@ -49,6 +56,11 @@ MainWindow::MainWindow(QWidget* parent)
             proxy, &Proxy::setFileFilter);
     connect(m_ui->filterModule, &QLineEdit::textChanged,
             proxy, &Proxy::setModuleFilter);
+
+    auto openFile = KStandardAction::open(this, SLOT(openFile()), this);
+    m_ui->openFile->setDefaultAction(openFile);
+
+    setWindowTitle(i18n("Heaptrack"));
 }
 
 MainWindow::~MainWindow()
@@ -58,11 +70,23 @@ MainWindow::~MainWindow()
 void MainWindow::dataReady(const QString& summary)
 {
     m_ui->summary->setText(summary);
+    m_ui->pages->setCurrentWidget(m_ui->resultsPage);
 }
 
 void MainWindow::loadFile(const QString& file)
 {
-    cout << "Loading file " << qPrintable(file) << ", this might take some time - please wait." << endl;
-
+    m_ui->loadingLabel->setText(i18n("Loading file %1, please wait...", file));
+    setWindowTitle(i18nc("%1: file name that is open", "Heaptrack - %1", file));
+    m_ui->pages->setCurrentWidget(m_ui->loadingPage);
     m_model->loadFile(file);
 }
+
+void MainWindow::openFile()
+{
+    auto dialog = new QFileDialog(this, i18n("Open Heaptrack Output File"), {}, i18n("Heaptrack data files (heaptrack.*)"));
+    dialog->setAttribute(Qt::WA_DeleteOnClose, true);
+    dialog->setFileMode(QFileDialog::ExistingFile);
+    connect(dialog, &QFileDialog::fileSelected,
+            this, &MainWindow::loadFile);
+    dialog->show();
+}
index cf0bd07..4250a88 100644 (file)
@@ -30,11 +30,14 @@ class Model;
 
 class MainWindow : public QMainWindow
 {
+    Q_OBJECT
 public:
     MainWindow(QWidget* parent = nullptr);
     virtual ~MainWindow();
 
+public slots:
     void loadFile(const QString& path);
+    void openFile();
 
 private slots:
     void dataReady(const QString& summary);
index 21e30b9..22d2a79 100644 (file)
    <string>MainWindow</string>
   </property>
   <widget class="QWidget" name="centralwidget">
-   <layout class="QVBoxLayout" name="verticalLayout">
+   <layout class="QHBoxLayout" name="horizontalLayout_2">
     <item>
-     <widget class="QLabel" name="summary">
-      <property name="text">
-       <string/>
-      </property>
-     </widget>
-    </item>
-    <item>
-     <widget class="QWidget" name="widget" native="true">
-      <layout class="QHBoxLayout" name="horizontalLayout">
-       <property name="spacing">
-        <number>0</number>
-       </property>
-       <property name="leftMargin">
-        <number>0</number>
-       </property>
-       <property name="topMargin">
-        <number>0</number>
-       </property>
-       <property name="rightMargin">
-        <number>0</number>
-       </property>
-       <property name="bottomMargin">
-        <number>0</number>
-       </property>
-       <item>
-        <widget class="QLineEdit" name="filterFunction">
-         <property name="placeholderText">
-          <string>filter by function...</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QLineEdit" name="filterFile">
-         <property name="placeholderText">
-          <string>filter by file...</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QLineEdit" name="filterModule">
-         <property name="placeholderText">
-          <string>filter by module...</string>
-         </property>
-        </widget>
-       </item>
-      </layout>
-     </widget>
-    </item>
-    <item>
-     <widget class="QTreeView" name="results">
-      <property name="alternatingRowColors">
-       <bool>true</bool>
-      </property>
-      <property name="rootIsDecorated">
-       <bool>true</bool>
-      </property>
-      <property name="uniformRowHeights">
-       <bool>true</bool>
-      </property>
-      <property name="sortingEnabled">
-       <bool>true</bool>
-      </property>
+     <widget class="QStackedWidget" name="pages">
+      <widget class="QWidget" name="openPage">
+       <layout class="QHBoxLayout" name="horizontalLayout_3">
+        <item>
+         <widget class="QToolButton" name="openFile">
+          <property name="text">
+           <string notr="true">...</string>
+          </property>
+          <property name="toolButtonStyle">
+           <enum>Qt::ToolButtonTextUnderIcon</enum>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </widget>
+      <widget class="QWidget" name="loadingPage">
+       <layout class="QVBoxLayout" name="verticalLayout_3">
+        <item>
+         <spacer name="verticalSpacer">
+          <property name="orientation">
+           <enum>Qt::Vertical</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>20</width>
+            <height>40</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+        <item>
+         <widget class="QLabel" name="loadingLabel">
+          <property name="text">
+           <string>Loading file %1, please wait...</string>
+          </property>
+          <property name="alignment">
+           <set>Qt::AlignCenter</set>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QProgressBar" name="loadingProgress">
+          <property name="value">
+           <number>24</number>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="verticalSpacer_2">
+          <property name="orientation">
+           <enum>Qt::Vertical</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>20</width>
+            <height>40</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+       </layout>
+      </widget>
+      <widget class="QWidget" name="resultsPage">
+       <layout class="QVBoxLayout" name="verticalLayout_2">
+        <item>
+         <widget class="QLabel" name="summary">
+          <property name="text">
+           <string/>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QWidget" name="widget" native="true">
+          <layout class="QHBoxLayout" name="horizontalLayout">
+           <property name="spacing">
+            <number>0</number>
+           </property>
+           <property name="leftMargin">
+            <number>0</number>
+           </property>
+           <property name="topMargin">
+            <number>0</number>
+           </property>
+           <property name="rightMargin">
+            <number>0</number>
+           </property>
+           <property name="bottomMargin">
+            <number>0</number>
+           </property>
+           <item>
+            <widget class="QLineEdit" name="filterFunction">
+             <property name="placeholderText">
+              <string>filter by function...</string>
+             </property>
+            </widget>
+           </item>
+           <item>
+            <widget class="QLineEdit" name="filterFile">
+             <property name="placeholderText">
+              <string>filter by file...</string>
+             </property>
+            </widget>
+           </item>
+           <item>
+            <widget class="QLineEdit" name="filterModule">
+             <property name="placeholderText">
+              <string>filter by module...</string>
+             </property>
+            </widget>
+           </item>
+          </layout>
+         </widget>
+        </item>
+        <item>
+         <widget class="QTreeView" name="results">
+          <property name="alternatingRowColors">
+           <bool>true</bool>
+          </property>
+          <property name="rootIsDecorated">
+           <bool>true</bool>
+          </property>
+          <property name="uniformRowHeights">
+           <bool>true</bool>
+          </property>
+          <property name="sortingEnabled">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </widget>
      </widget>
     </item>
    </layout>
index 45e11ef..5c146eb 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <QDebug>
 
+#include <ThreadWeaver/ThreadWeaver>
+
 #include <sstream>
 
 using namespace std;
@@ -46,7 +48,6 @@ int parentRow(const QModelIndex& child)
 {
     return child.isValid() ? static_cast<int>(child.internalId()) : -1;
 }
-
 }
 
 Model::Model(QObject* parent)
@@ -161,10 +162,13 @@ int Model::columnCount(const QModelIndex& /*parent*/) const
 
 void Model::loadFile(const QString& file)
 {
-    beginResetModel();
-    m_data.read(file.toStdString());
-    endResetModel();
-    emit dataReady(generateSummary(m_data));
+    using namespace ThreadWeaver;
+    stream() << make_job([=]() {
+        beginResetModel();
+        m_data.read(file.toStdString());
+        endResetModel();
+        emit dataReady(generateSummary(m_data));
+    });
 }
 
 QVariant Model::allocationData(const AllocationData& allocation, const IpIndex& ipIndex, Columns column) const