Move data into model.
authorMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 09:48:20 +0000 (11:48 +0200)
committerMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 09:48:20 +0000 (11:48 +0200)
gui/mainwindow.cpp
gui/mainwindow.h
gui/model.cpp
gui/model.h

index b7b2a5f..9689ea4 100644 (file)
 #include <ui_mainwindow.h>
 
 #include <iostream>
-#include <sstream>
 
 #include "model.h"
-#include "../accumulatedtracedata.h"
 
 using namespace std;
 
-namespace {
-QString generateSummary(const AccumulatedTraceData& data)
-{
-    stringstream stream;
-    const double totalTimeS = 0.001 * data.totalTime;
-    stream << "<qt>"
-           << "<strong>total runtime</strong>: " << fixed << totalTimeS << "s.<br/>"
-           << "<strong>bytes allocated in total</strong> (ignoring deallocations): " << formatBytes(data.totalAllocated)
-             << " (" << formatBytes(data.totalAllocated / totalTimeS) << "/s)<br/>"
-           << "<strong>calls to allocation functions</strong>: " << data.totalAllocations
-             << " (" << size_t(data.totalAllocations / totalTimeS) << "/s)<br/>"
-           << "<strong>peak heap memory consumption</strong>: " << formatBytes(data.peak) << "<br/>"
-           << "<strong>total memory leaked</strong>: " << formatBytes(data.leaked) << "<br/>";
-    stream << "</qt>";
-    return QString::fromStdString(stream.str());
-}
-}
-
 MainWindow::MainWindow(QWidget* parent)
     : m_ui(new Ui::MainWindow)
     , m_model(new Model(this))
 {
     m_ui->setupUi(this);
     m_ui->results->setModel(m_model);
+    connect(m_model, &Model::dataReady,
+            this, &MainWindow::dataReady);
 }
 
 MainWindow::~MainWindow()
 {
 }
 
+void MainWindow::dataReady(const QString& summary)
+{
+    m_ui->summary->setText(summary);
+}
+
 void MainWindow::loadFile(const QString& file)
 {
     cout << "Loading file " << qPrintable(file) << ", this might take some time - please wait." << endl;
-    AccumulatedTraceData data;
-    data.read(file.toStdString());
 
-    m_ui->summary->setText(generateSummary(data));
+    m_model->loadFile(file);
 }
index f8abf08..cf0bd07 100644 (file)
@@ -36,6 +36,9 @@ public:
 
     void loadFile(const QString& path);
 
+private slots:
+    void dataReady(const QString& summary);
+
 private:
     QScopedPointer<Ui::MainWindow> m_ui;
     Model* m_model;
index d85ebf4..7e17692 100644 (file)
 
 #include "model.h"
 
+#include <sstream>
+
+using namespace std;
+
+namespace {
+QString generateSummary(const AccumulatedTraceData& data)
+{
+    stringstream stream;
+    const double totalTimeS = 0.001 * data.totalTime;
+    stream << "<qt>"
+           << "<strong>total runtime</strong>: " << fixed << totalTimeS << "s.<br/>"
+           << "<strong>bytes allocated in total</strong> (ignoring deallocations): " << formatBytes(data.totalAllocated)
+             << " (" << formatBytes(data.totalAllocated / totalTimeS) << "/s)<br/>"
+           << "<strong>calls to allocation functions</strong>: " << data.totalAllocations
+             << " (" << size_t(data.totalAllocations / totalTimeS) << "/s)<br/>"
+           << "<strong>peak heap memory consumption</strong>: " << formatBytes(data.peak) << "<br/>"
+           << "<strong>total memory leaked</strong>: " << formatBytes(data.leaked) << "<br/>";
+    stream << "</qt>";
+    return QString::fromStdString(stream.str());
+}
+}
+
 Model::Model(QObject* parent)
 {
 
@@ -74,3 +96,9 @@ int Model::columnCount(const QModelIndex& parent) const
 {
     return NUM_COLUMNS;
 }
+
+void Model::loadFile(const QString& file)
+{
+    m_data.read(file.toStdString());
+    emit dataReady(generateSummary(m_data));
+}
index d518a2f..fae921b 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <QAbstractItemModel>
 
+#include "../accumulatedtracedata.h"
+
 class Model : public QAbstractItemModel
 {
     Q_OBJECT
@@ -39,14 +41,19 @@ public:
     };
 
     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
-    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
-    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
-    QModelIndex parent(const QModelIndex &child) const override;
-    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
-    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
+    QVariant data(const QModelIndexindex, int role = Qt::DisplayRole) const override;
+    QModelIndex index(int row, int column, const QModelIndexparent = QModelIndex()) const override;
+    QModelIndex parent(const QModelIndexchild) const override;
+    int rowCount(const QModelIndexparent = QModelIndex()) const override;
+    int columnCount(const QModelIndexparent = QModelIndex()) const override;
 
-private:
+    void loadFile(const QString& file);
 
+signals:
+    void dataReady(const QString& summary);
+
+private:
+    AccumulatedTraceData m_data;
 };
 
 #endif // MODEL_H