# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-09-20 16:10:31 # Node ID 84f4fcdcd0178ec8a444e45d37c8595415aa2051 # Parent b616fb48842c7d2ec87eeccb1dd81ca24f7918ba moved snapshot functions to a new class (SnapshotManager) from mainwindow diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ add_executable(${PROGRAM_NAME} WIN32 portlist.cpp snapshot.cpp snapshotview.cpp + snapshotmanager.cpp ${UI_FILES} ${RES_FILES} misc/windows_icon.rc diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -26,13 +26,11 @@ #include #include #include -#include #include #include #include #include -#include #include #include "utils.h" @@ -55,13 +53,17 @@ Q_DECLARE_METATYPE(Range); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), - portControl(&serialPort) + portControl(&serialPort), + snapshotMan(this, &channelBuffers) { ui->setupUi(this); ui->tabWidget->insertTab(0, &portControl, "Port"); ui->tabWidget->setCurrentIndex(0); addToolBar(portControl.toolBar()); + ui->mainToolBar->addAction(snapshotMan.takeSnapshotAction()); + ui->menuBar->insertMenu(ui->menuHelp->menuAction(), snapshotMan.menu()); + setupAboutDialog(); // init UI signals @@ -116,11 +118,8 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionClear, SIGNAL(triggered(bool)), this, SLOT(clearPlot())); - QObject::connect(ui->actionSnapShot, SIGNAL(triggered(bool)), - this, SLOT(takeSnapShot())); - - QObject::connect(ui->actionClearSnapShots, SIGNAL(triggered(bool)), - this, SLOT(clearSnapshots())); + // QObject::connect(ui->actionSnapShot, SIGNAL(triggered(bool)), + // this, SLOT(takeSnapShot())); // setup number of channels spinbox QObject::connect(ui->spNumOfChannels, @@ -252,11 +251,6 @@ MainWindow::~MainWindow() serialPort.close(); } - for (auto snapshot : snapshots) - { - delete snapshot; - } - delete ui; ui = NULL; // we check if ui is deleted in messageHandler } @@ -730,55 +724,3 @@ void MainWindow::messageHandler(QtMsgTyp ui->statusBar->showMessage(msg, 5000); } } - -void MainWindow::takeSnapShot() -{ - QString name = QTime::currentTime().toString("'Snapshot ['HH:mm:ss']'"); - auto snapShot = new SnapShot(this, name); - - for (unsigned ci = 0; ci < numOfChannels; ci++) - { - snapShot->data.append(QVector(numOfSamples)); - for (unsigned i = 0; i < numOfSamples; i++) - { - snapShot->data[ci][i] = channelBuffers[ci]->sample(i); - } - } - snapshots.append(snapShot); - QObject::connect(snapShot, &SnapShot::deleteRequested, - this, &MainWindow::deleteSnapshot); - - updateSnapShotMenu(); -} - -void MainWindow::updateSnapShotMenu() -{ - ui->menuSnapShots->clear(); - ui->menuSnapShots->addAction(ui->actionSnapShot); - if (snapshots.size()) - { - ui->menuSnapShots->addSeparator(); - for (auto ss : snapshots) - { - ui->menuSnapShots->addAction(ss->showAction()); - } - ui->menuSnapShots->addSeparator(); - ui->menuSnapShots->addAction(ui->actionClearSnapShots); - } -} - -void MainWindow::clearSnapshots() -{ - for (auto snapshot : snapshots) - { - delete snapshot; - } - snapshots.clear(); - updateSnapShotMenu(); -} - -void MainWindow::deleteSnapshot(SnapShot* snapshot) -{ - delete snapshots.takeAt(snapshots.indexOf(snapshot)); - updateSnapShotMenu(); -} diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -35,9 +35,9 @@ #include #include "portcontrol.h" -#include "snapshotview.h" #include "ui_about_dialog.h" #include "framebuffer.h" +#include "snapshotmanager.h" namespace Ui { class MainWindow; @@ -100,10 +100,7 @@ private: unsigned int sampleCount; QTimer spsTimer; - // snapshots - QList snapshots; - - void updateSnapShotMenu(); + SnapshotManager snapshotMan; // demo QTimer demoTimer; @@ -132,10 +129,6 @@ private slots: void spsTimerTimeout(); - void takeSnapShot(); - void clearSnapshots(); - void deleteSnapshot(SnapShot* snapshot); - void demoTimerTimeout(); void enableDemo(bool enabled); diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -488,15 +488,8 @@ - - - SnapShots - - - - @@ -508,7 +501,6 @@ - @@ -612,25 +604,6 @@ Toggle Dark Background - - - Take Snapshot - - - Take a snapshot of the current plot (F5) - - - F5 - - - - - Clear SnapShots - - - Delete all snapshots - - diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -43,7 +43,9 @@ SOURCES += main.cpp\ scalepicker.cpp \ scalezoomer.cpp \ portlist.cpp \ - snapshotview.cpp + snapshotview.cpp \ + snapshotmanager.cpp \ + snapshot.cpp HEADERS += mainwindow.h \ utils.h \ @@ -56,7 +58,9 @@ HEADERS += mainwindow.h \ scalepicker.h \ scalezoomer.h \ portlist.h \ - snapshotview.h + snapshotview.h \ + snapshotmanager.h \ + snapshot.h FORMS += mainwindow.ui \ about_dialog.ui \ diff --git a/snapshotmanager.cpp b/snapshotmanager.cpp new file mode 100644 --- /dev/null +++ b/snapshotmanager.cpp @@ -0,0 +1,99 @@ + +#include +#include +#include + +#include "snapshotmanager.h" + +SnapshotManager::SnapshotManager(QMainWindow* mainWindow, + QList* channelBuffers) : + _menu("Snapshots"), + _takeSnapshotAction("Take Snapshot", this), + clearAction("Clear Snapshots", this) +{ + _mainWindow = mainWindow; + _channelBuffers = channelBuffers; + + _takeSnapshotAction.setToolTip("Take a snapshot of current plot (F5)"); + _takeSnapshotAction.setShortcut(QKeySequence("F5")); + clearAction.setToolTip("Delete all snapshots"); + connect(&_takeSnapshotAction, SIGNAL(triggered(bool)), + this, SLOT(takeSnapshot())); + connect(&clearAction, SIGNAL(triggered(bool)), + this, SLOT(clearSnapshots())); + + updateMenu(); +} + +SnapshotManager::~SnapshotManager() +{ + for (auto snapshot : snapshots) + { + delete snapshot; + } +} + +void SnapshotManager::takeSnapshot() +{ + QString name = QTime::currentTime().toString("'Snapshot ['HH:mm:ss']'"); + auto snapShot = new SnapShot(_mainWindow, name); + + unsigned numOfChannels = _channelBuffers->size(); + unsigned numOfSamples = _channelBuffers->at(0)->size(); + + for (unsigned ci = 0; ci < numOfChannels; ci++) + { + snapShot->data.append(QVector(numOfSamples)); + for (unsigned i = 0; i < numOfSamples; i++) + { + snapShot->data[ci][i] = _channelBuffers->at(ci)->sample(i); + } + } + snapshots.append(snapShot); + QObject::connect(snapShot, &SnapShot::deleteRequested, + this, &SnapshotManager::deleteSnapshot); + + updateMenu(); +} + +void SnapshotManager::updateMenu() +{ + _menu.clear(); + _menu.addAction(&_takeSnapshotAction); + if (snapshots.size()) + { + _menu.addSeparator(); + for (auto ss : snapshots) + { + _menu.addAction(ss->showAction()); + } + _menu.addSeparator(); + _menu.addAction(&clearAction); + } +} + +void SnapshotManager::clearSnapshots() +{ + for (auto snapshot : snapshots) + { + delete snapshot; + } + snapshots.clear(); + updateMenu(); +} + +void SnapshotManager::deleteSnapshot(SnapShot* snapshot) +{ + delete snapshots.takeAt(snapshots.indexOf(snapshot)); + updateMenu(); +} + +QMenu* SnapshotManager::menu() +{ + return &_menu; +} + +QAction* SnapshotManager::takeSnapshotAction() +{ + return &_takeSnapshotAction; +} diff --git a/snapshotmanager.h b/snapshotmanager.h new file mode 100644 --- /dev/null +++ b/snapshotmanager.h @@ -0,0 +1,44 @@ + +#ifndef SNAPSHOTMANAGER_H +#define SNAPSHOTMANAGER_H + +#include +#include +#include + +#include "framebuffer.h" +#include "snapshot.h" + +// class MainWindow; + +class SnapshotManager : public QObject +{ + Q_OBJECT + +public: + SnapshotManager(QMainWindow* mainWindow, QList* channelBuffers); + ~SnapshotManager(); + + QMenu* menu(); + QAction* takeSnapshotAction(); + +private: + QMainWindow* _mainWindow; + QList* _channelBuffers; + + QList snapshots; + + QMenu _menu; + QAction _takeSnapshotAction; + QAction clearAction; + + void updateMenu(); + +private slots: + void takeSnapshot(); + void clearSnapshots(); + void deleteSnapshot(SnapShot* snapshot); + +}; + +#endif /* SNAPSHOTMANAGER_H */