Add open/open new/close/quit actions to file menu
authorMilian Wolff <mail@milianw.de>
Mon, 13 Feb 2017 23:16:02 +0000 (00:16 +0100)
committerMilian Wolff <mail@milianw.de>
Mon, 13 Feb 2017 23:16:02 +0000 (00:16 +0100)
13 files changed:
src/analyze/gui/callercalleemodel.cpp
src/analyze/gui/callercalleemodel.h
src/analyze/gui/chartmodel.cpp
src/analyze/gui/chartmodel.h
src/analyze/gui/flamegraph.cpp
src/analyze/gui/flamegraph.h
src/analyze/gui/histogrammodel.cpp
src/analyze/gui/histogrammodel.h
src/analyze/gui/mainwindow.cpp
src/analyze/gui/mainwindow.h
src/analyze/gui/mainwindow.ui
src/analyze/gui/treemodel.cpp
src/analyze/gui/treemodel.h

index dde6e92..6529bac 100644 (file)
@@ -290,3 +290,11 @@ void CallerCalleeModel::setSummary(const SummaryData& data)
     m_maxCost.selfCost = data.cost;
     endResetModel();
 }
+
+void CallerCalleeModel::clearData()
+{
+    beginResetModel();
+    m_rows = {};
+    m_maxCost = {};
+    endResetModel();
+}
index 5f2314c..4762a6d 100644 (file)
@@ -83,6 +83,7 @@ public:
 
     void resetData(const QVector<CallerCalleeData>& rows);
     void setSummary(const SummaryData& data);
+    void clearData();
 
 private:
     QVector<CallerCalleeData> m_rows;
index 8c8bd7b..514859b 100644 (file)
@@ -190,3 +190,12 @@ void ChartModel::resetData(const ChartData& data)
     }
     endResetModel();
 }
+
+void ChartModel::clearData()
+{
+    beginResetModel();
+    m_data = {};
+    m_columnDataSetBrushes = {};
+    m_columnDataSetPens = {};
+    endResetModel();
+}
index c1fcece..5e9fde1 100644 (file)
@@ -73,6 +73,7 @@ public:
 
 public slots:
     void resetData(const ChartData& data);
+    void clearData();
 
 private:
     ChartData m_data;
index 0537bba..a16690b 100644 (file)
@@ -504,6 +504,14 @@ void FlameGraph::setBottomUpData(const TreeData& bottomUpData)
     m_bottomUpData = bottomUpData;
 }
 
+void FlameGraph::clearData()
+{
+    m_topDownData = {};
+    m_bottomUpData = {};
+
+    setData(nullptr);
+}
+
 void FlameGraph::showData()
 {
     setData(nullptr);
index b238914..5d3f020 100644 (file)
@@ -42,6 +42,8 @@ public:
     void setTopDownData(const TreeData& topDownData);
     void setBottomUpData(const TreeData& bottomUpData);
 
+    void clearData();
+
 protected:
     bool eventFilter(QObject* object, QEvent* event) override;
 
index 7c978d2..65b9fd2 100644 (file)
@@ -106,3 +106,10 @@ void HistogramModel::resetData(const HistogramData& data)
     m_data = data;
     endResetModel();
 }
+
+void HistogramModel::clearData()
+{
+    beginResetModel();
+    m_data = {};
+    endResetModel();
+}
index 27ab677..9406756 100644 (file)
@@ -63,6 +63,7 @@ public:
     int columnCount(const QModelIndex& parent = {}) const override;
 
     void resetData(const HistogramData& data);
+    void clearData();
 
 private:
     HistogramData m_data;
index 586d87d..12dd8f0 100644 (file)
@@ -26,6 +26,7 @@
 #include <KConfigGroup>
 #include <KLocalizedString>
 #include <KRecursiveFilterProxyModel>
+#include <KStandardAction>
 
 #include <QAction>
 #include <QDebug>
@@ -107,7 +108,7 @@ void setupTopView(TreeModel* source, QTreeView* view, TopProxy::Type type)
 
 #if KChart_FOUND
 void addChartTab(QTabWidget* tabWidget, const QString& title, ChartModel::Type type, const Parser* parser,
-                 void (Parser::*dataReady)(const ChartData&))
+                 void (Parser::*dataReady)(const ChartData&), MainWindow* window)
 {
     auto tab = new ChartWidget(tabWidget->parentWidget());
     tabWidget->addTab(tab, title);
@@ -118,6 +119,7 @@ void addChartTab(QTabWidget* tabWidget, const QString& title, ChartModel::Type t
         model->resetData(data);
         tabWidget->setTabEnabled(tabWidget->indexOf(tab), true);
     });
+    QObject::connect(window, &MainWindow::clearData, model, &ChartModel::clearData);
 }
 #endif
 
@@ -195,6 +197,10 @@ MainWindow::MainWindow(QWidget* parent)
     auto bottomUpModel = new TreeModel(this);
     auto topDownModel = new TreeModel(this);
     auto callerCalleeModel = new CallerCalleeModel(this);
+    connect(this, &MainWindow::clearData, bottomUpModel, &TreeModel::clearData);
+    connect(this, &MainWindow::clearData, topDownModel, &TreeModel::clearData);
+    connect(this, &MainWindow::clearData, callerCalleeModel, &CallerCalleeModel::clearData);
+    connect(this, &MainWindow::clearData, m_ui->flameGraphTab, &FlameGraph::clearData);
 
     m_ui->tabWidget->setTabEnabled(m_ui->tabWidget->indexOf(m_ui->callerCalleeTab), false);
     m_ui->tabWidget->setTabEnabled(m_ui->tabWidget->indexOf(m_ui->topDownTab), false);
@@ -209,6 +215,7 @@ MainWindow::MainWindow(QWidget* parent)
         statusBar()->addWidget(m_ui->progressLabel, 1);
         statusBar()->addWidget(m_ui->loadingProgress);
         m_ui->pages->setCurrentWidget(m_ui->resultsPage);
+        m_ui->tabWidget->setTabEnabled(m_ui->tabWidget->indexOf(m_ui->bottomUpTab), true);
     });
     connect(m_parser, &Parser::callerCalleeDataAvailable, this, [=](const CallerCalleeRows& data) {
         callerCalleeModel->resetData(data);
@@ -277,11 +284,18 @@ MainWindow::MainWindow(QWidget* parent)
         m_ui->summaryLeft->setText(textLeft);
         m_ui->summaryCenter->setText(textCenter);
         m_ui->summaryRight->setText(textRight);
+        m_ui->tabWidget->setTabEnabled(m_ui->tabWidget->indexOf(m_ui->summaryTab), true);
     });
     connect(m_parser, &Parser::progressMessageAvailable, m_ui->progressLabel, &QLabel::setText);
     auto removeProgress = [this] {
-        statusBar()->removeWidget(m_ui->progressLabel);
-        statusBar()->removeWidget(m_ui->loadingProgress);
+        auto layout = qobject_cast<QVBoxLayout*>(m_ui->loadingPage->layout());
+        Q_ASSERT(layout);
+        const auto idx = layout->indexOf(m_ui->loadingLabel) + 1;
+        layout->insertWidget(idx, m_ui->loadingProgress);
+        layout->insertWidget(idx + 1, m_ui->progressLabel);
+        m_ui->progressLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
+        m_closeAction->setEnabled(true);
+        m_openAction->setEnabled(true);
     };
     connect(m_parser, &Parser::finished, this, removeProgress);
     connect(m_parser, &Parser::failedToOpen, this, [this, removeProgress](const QString& failedFile) {
@@ -292,19 +306,21 @@ MainWindow::MainWindow(QWidget* parent)
     m_ui->messages->hide();
 
 #if KChart_FOUND
-    addChartTab(m_ui->tabWidget, tr("Consumed"), ChartModel::Consumed, m_parser, &Parser::consumedChartDataAvailable);
+    addChartTab(m_ui->tabWidget, tr("Consumed"), ChartModel::Consumed, m_parser, &Parser::consumedChartDataAvailable,
+                this);
     addChartTab(m_ui->tabWidget, tr("Allocations"), ChartModel::Allocations, m_parser,
-                &Parser::allocationsChartDataAvailable);
+                &Parser::allocationsChartDataAvailable, this);
     addChartTab(m_ui->tabWidget, tr("Temporary Allocations"), ChartModel::Temporary, m_parser,
-                &Parser::temporaryChartDataAvailable);
-    addChartTab(m_ui->tabWidget, tr("Allocated"), ChartModel::Allocated, m_parser,
-                &Parser::allocatedChartDataAvailable);
+                &Parser::temporaryChartDataAvailable, this);
+    addChartTab(m_ui->tabWidget, tr("Allocated"), ChartModel::Allocated, m_parser, &Parser::allocatedChartDataAvailable,
+                this);
 
     auto sizesTab = new HistogramWidget(this);
     m_ui->tabWidget->addTab(sizesTab, tr("Sizes"));
     m_ui->tabWidget->setTabEnabled(m_ui->tabWidget->indexOf(sizesTab), false);
     auto sizeHistogramModel = new HistogramModel(this);
     sizesTab->setModel(sizeHistogramModel);
+    connect(this, &MainWindow::clearData, sizeHistogramModel, &HistogramModel::clearData);
 
     connect(m_parser, &Parser::sizeHistogramDataAvailable, this, [=](const HistogramData& data) {
         sizeHistogramModel->resetData(data);
@@ -370,6 +386,16 @@ MainWindow::MainWindow(QWidget* parent)
     m_ui->topAllocated->setItemDelegate(costDelegate);
 
     setWindowTitle(i18n("Heaptrack"));
+    // closing the current file shows the stack page to open a new one
+    m_openAction = KStandardAction::open(this, &MainWindow::closeFile, this);
+    m_ui->menu_File->addAction(m_openAction);
+    m_openNewAction = KStandardAction::openNew(this, &MainWindow::openNewFile, this);
+    m_ui->menu_File->addAction(m_openNewAction);
+    m_closeAction = KStandardAction::close(this, &MainWindow::close, this);
+    m_closeAction->setEnabled(false); // TODO: support canceling of ongoing parse jobs
+    m_ui->menu_File->addAction(m_closeAction);
+    m_quitAction = KStandardAction::quit(qApp, &QCoreApplication::quit, this);
+    m_ui->menu_File->addAction(m_quitAction);
 }
 
 MainWindow::~MainWindow()
@@ -381,6 +407,7 @@ MainWindow::~MainWindow()
 
 void MainWindow::loadFile(const QString& file, const QString& diffBase)
 {
+    m_openAction->setEnabled(false);
     m_ui->loadingLabel->setText(i18n("Loading file %1, please wait...", file));
     if (diffBase.isEmpty()) {
         setWindowTitle(i18nc("%1: file name that is open", "Heaptrack - %1", QFileInfo(file).fileName()));
@@ -394,14 +421,24 @@ void MainWindow::loadFile(const QString& file, const QString& diffBase)
     m_parser->parse(file, diffBase);
 }
 
-void MainWindow::openFile()
+void MainWindow::openNewFile()
 {
-    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, [this](const QString& file) { loadFile(file); });
-    dialog->show();
+    auto window = new MainWindow;
+    window->setAttribute(Qt::WA_DeleteOnClose, true);
+    window->show();
+}
+
+void MainWindow::closeFile()
+{
+    m_ui->pages->setCurrentWidget(m_ui->openPage);
+
+    m_ui->tabWidget->setCurrentIndex(m_ui->tabWidget->indexOf(m_ui->summaryTab));
+    for (int i = 0, c = m_ui->tabWidget->count(); i < c; ++i) {
+        m_ui->tabWidget->setTabEnabled(i, false);
+    }
+
+    m_closeAction->setEnabled(false);
+    emit clearData();
 }
 
 void MainWindow::showError(const QString& message)
index 358b5c2..79b8632 100644 (file)
@@ -41,7 +41,11 @@ public:
 
 public slots:
     void loadFile(const QString& path, const QString& diffBase = {});
-    void openFile();
+    void openNewFile();
+    void closeFile();
+
+signals:
+    void clearData();
 
 private:
     void showError(const QString& message);
@@ -51,6 +55,11 @@ private:
     Parser* m_parser;
     KSharedConfig::Ptr m_config;
     bool m_diffMode = false;
+
+    QAction* m_openAction = nullptr;
+    QAction* m_openNewAction = nullptr;
+    QAction* m_closeAction = nullptr;
+    QAction* m_quitAction = nullptr;
 };
 
 #endif // MAINWINDOW_H
index 0899475..5c8381f 100644 (file)
     </layout>
    </widget>
   </widget>
+  <widget class="QMenuBar" name="menuBar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>1332</width>
+     <height>30</height>
+    </rect>
+   </property>
+   <widget class="QMenu" name="menu_File">
+    <property name="title">
+     <string>&amp;File</string>
+    </property>
+   </widget>
+   <addaction name="menu_File"/>
+  </widget>
  </widget>
  <customwidgets>
   <customwidget>
index c939a11..9b7a01a 100644 (file)
@@ -314,6 +314,14 @@ void TreeModel::setSummary(const SummaryData& data)
     endResetModel();
 }
 
+void TreeModel::clearData()
+{
+    beginResetModel();
+    m_data = {};
+    m_maxCost = {};
+    endResetModel();
+}
+
 const RowData* TreeModel::toRow(const QModelIndex& index) const
 {
     if (!index.isValid()) {
index 01f95ee..97a3100 100644 (file)
@@ -84,6 +84,7 @@ public:
 public slots:
     void resetData(const TreeData& data);
     void setSummary(const SummaryData& data);
+    void clearData();
 
 private:
     /// @return the row resembled by @p index