Changeset - 84f4fcdcd017
[Not reviewed]
snapshots
0 5 2
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-20 16:10:31
hy@ozderya.net
moved snapshot functions to a new class (SnapshotManager) from mainwindow
7 files changed with 159 insertions and 103 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -64,24 +64,25 @@ add_executable(${PROGRAM_NAME} WIN32
 
  main.cpp
 
  mainwindow.cpp
 
  portcontrol.cpp
 
  plot.cpp
 
  zoomer.cpp
 
  hidabletabwidget.cpp
 
  framebuffer.cpp
 
  scalepicker.cpp
 
  scalezoomer.cpp
 
  portlist.cpp
 
  snapshot.cpp
 
  snapshotview.cpp
 
  snapshotmanager.cpp
 
  ${UI_FILES}
 
  ${RES_FILES}
 
  misc/windows_icon.rc
 
  )
 

	
 
# Use the Widgets module from Qt 5.
 
target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY})
 
qt5_use_modules(${PROGRAM_NAME} Widgets SerialPort Svg)
 

	
 
# set compiler flags
 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
 

	
mainwindow.cpp
Show inline comments
 
@@ -17,60 +17,62 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QTextStream>
 
#include <QtDebug>
 
#include <QtEndian>
 
#include <QTime>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <snapshotview.h>
 
#include <plot.h>
 

	
 
#include "utils.h"
 
#include "version.h"
 
#include "floatswap.h"
 

	
 
#if defined(Q_OS_WIN) && defined(QT_STATIC)
 
#include <QtPlugin>
 
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
 
#endif
 

	
 
struct Range
 
{
 
    double rmin;
 
    double rmax;
 
};
 

	
 
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
 

	
 
    // menu signals
 
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
 
              &aboutDialog, &QWidget::show);
 

	
 
    QObject::connect(ui->actionExportCsv, &QAction::triggered,
 
                     this, &MainWindow::onExportCsv);
 

	
 
    QObject::connect(ui->actionQuit, &QAction::triggered,
 
@@ -107,29 +109,26 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(ui->cbAutoScale, &QCheckBox::toggled,
 
                     this, &MainWindow::onAutoScaleChecked);
 

	
 
    QObject::connect(ui->spYmin, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    QObject::connect(ui->spYmax, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    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,
 
                     SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfChannelsChanged);
 

	
 
    // setup number format buttons
 
    numberFormatButtons.addButton(ui->rbUint8,  NumberFormat_uint8);
 
    numberFormatButtons.addButton(ui->rbUint16, NumberFormat_uint16);
 
    numberFormatButtons.addButton(ui->rbUint32, NumberFormat_uint32);
 
    numberFormatButtons.addButton(ui->rbInt8,   NumberFormat_int8);
 
    numberFormatButtons.addButton(ui->rbInt16,  NumberFormat_int16);
 
@@ -243,29 +242,24 @@ MainWindow::~MainWindow()
 
{
 
    for (auto curve : curves)
 
    {
 
        // also deletes respective FrameBuffer
 
        delete curve;
 
    }
 

	
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
    }
 

	
 
    for (auto snapshot : snapshots)
 
    {
 
        delete snapshot;
 
    }
 

	
 
    delete ui;
 
    ui = NULL; // we check if ui is deleted in messageHandler
 
}
 

	
 
void MainWindow::setupAboutDialog()
 
{
 
    Ui_AboutDialog uiAboutDialog;
 
    uiAboutDialog.setupUi(&aboutDialog);
 

	
 
    QObject::connect(uiAboutDialog.pbAboutQt, &QPushButton::clicked,
 
                     [](){ QApplication::aboutQt();});
 

	
 
@@ -721,64 +715,12 @@ void MainWindow::messageHandler(QtMsgTyp
 
            logString = "[Fatal] " + msg;
 
            break;
 
    }
 

	
 
    if (ui != NULL) ui->ptLog->appendPlainText(logString);
 
    std::cerr << logString.toStdString() << std::endl;
 

	
 
    if (type != QtDebugMsg && ui != NULL)
 
    {
 
        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<QPointF>(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();
 
}
mainwindow.h
Show inline comments
 
@@ -26,27 +26,27 @@
 
#include <QString>
 
#include <QVector>
 
#include <QList>
 
#include <QSerialPort>
 
#include <QSignalMapper>
 
#include <QTimer>
 
#include <QColor>
 
#include <QtGlobal>
 
#include <qwt_plot_curve.h>
 
#include <qwt_plot_textlabel.h>
 

	
 
#include "portcontrol.h"
 
#include "snapshotview.h"
 
#include "ui_about_dialog.h"
 
#include "framebuffer.h"
 
#include "snapshotmanager.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
class MainWindow : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit MainWindow(QWidget *parent = 0);
 
    ~MainWindow();
 
@@ -91,28 +91,25 @@ private:
 
    double (MainWindow::*readSample)();
 

	
 
    // note that serialPort should already have enough bytes present
 
    template<typename T> double readSampleAs();
 

	
 
    bool skipByteRequested;
 

	
 
    const int SPS_UPDATE_TIMEOUT = 1;  // second
 
    QLabel spsLabel;
 
    unsigned int sampleCount;
 
    QTimer spsTimer;
 

	
 
    // snapshots
 
    QList<SnapShot*> snapshots;
 

	
 
    void updateSnapShotMenu();
 
    SnapshotManager snapshotMan;
 

	
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 
    bool isDemoRunning();
 
    QwtPlotTextLabel demoIndicator;
 

	
 
private slots:
 
    void onPortToggled(bool open);
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
    void onPortError(QSerialPort::SerialPortError error);
 
@@ -123,23 +120,19 @@ private slots:
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 
    void onRangeSelected();
 

	
 
    void onNumOfChannelsChanged(int value);
 
    void onNumberFormatButtonToggled(int numberFormatId, bool checked);
 
    void selectNumberFormat(NumberFormat numberFormatId);
 

	
 
    void clearPlot();
 

	
 
    void spsTimerTimeout();
 

	
 
    void takeSnapShot();
 
    void clearSnapshots();
 
    void deleteSnapshot(SnapShot* snapshot);
 

	
 
    void demoTimerTimeout();
 
    void enableDemo(bool enabled);
 

	
 
    void onExportCsv();
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -479,45 +479,37 @@
 
    <addaction name="separator"/>
 
    <addaction name="actionQuit"/>
 
   </widget>
 
   <widget class="QMenu" name="menuView">
 
    <property name="title">
 
     <string>View</string>
 
    </property>
 
    <addaction name="actionGrid"/>
 
    <addaction name="actionMinorGrid"/>
 
    <addaction name="actionUnzoom"/>
 
    <addaction name="actionDarkBackground"/>
 
   </widget>
 
   <widget class="QMenu" name="menuSnapShots">
 
    <property name="title">
 
     <string>SnapShots</string>
 
    </property>
 
    <addaction name="actionSnapShot"/>
 
   </widget>
 
   <addaction name="menuFile"/>
 
   <addaction name="menuView"/>
 
   <addaction name="menuSnapShots"/>
 
   <addaction name="menuHelp"/>
 
  </widget>
 
  <widget class="QToolBar" name="mainToolBar">
 
   <attribute name="toolBarArea">
 
    <enum>TopToolBarArea</enum>
 
   </attribute>
 
   <attribute name="toolBarBreak">
 
    <bool>false</bool>
 
   </attribute>
 
   <addaction name="actionPause"/>
 
   <addaction name="actionClear"/>
 
   <addaction name="actionSnapShot"/>
 
  </widget>
 
  <widget class="QStatusBar" name="statusBar"/>
 
  <action name="actionPause">
 
   <property name="checkable">
 
    <bool>true</bool>
 
   </property>
 
   <property name="checked">
 
    <bool>false</bool>
 
   </property>
 
   <property name="text">
 
    <string>Pause</string>
 
   </property>
 
@@ -603,43 +595,24 @@
 
  </action>
 
  <action name="actionDarkBackground">
 
   <property name="checkable">
 
    <bool>true</bool>
 
   </property>
 
   <property name="text">
 
    <string>Dark Background</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Toggle Dark Background</string>
 
   </property>
 
  </action>
 
  <action name="actionSnapShot">
 
   <property name="text">
 
    <string>Take Snapshot</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Take a snapshot of the current plot (F5)</string>
 
   </property>
 
   <property name="shortcut">
 
    <string>F5</string>
 
   </property>
 
  </action>
 
  <action name="actionClearSnapShots">
 
   <property name="text">
 
    <string>Clear SnapShots</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Delete all snapshots</string>
 
   </property>
 
  </action>
 
 </widget>
 
 <layoutdefault spacing="6" margin="11"/>
 
 <customwidgets>
 
  <customwidget>
 
   <class>Plot</class>
 
   <extends>QWidget</extends>
 
   <header>plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
  <customwidget>
 
   <class>HidableTabWidget</class>
 
   <extends>QTabWidget</extends>
serialplot.pro
Show inline comments
 
@@ -34,38 +34,42 @@ CONFIG += qwt
 

	
 

	
 
SOURCES += main.cpp\
 
        mainwindow.cpp \
 
    portcontrol.cpp \
 
    plot.cpp \
 
    zoomer.cpp \
 
    hidabletabwidget.cpp \
 
    framebuffer.cpp \
 
    scalepicker.cpp \
 
    scalezoomer.cpp \
 
    portlist.cpp \
 
    snapshotview.cpp
 
    snapshotview.cpp \
 
    snapshotmanager.cpp \
 
    snapshot.cpp
 

	
 
HEADERS  += mainwindow.h \
 
    utils.h \
 
    portcontrol.h \
 
    floatswap.h \
 
    plot.h \
 
    zoomer.h \
 
    hidabletabwidget.h \
 
    framebuffer.h \
 
    scalepicker.h \
 
    scalezoomer.h \
 
    portlist.h \
 
    snapshotview.h
 
    snapshotview.h \
 
    snapshotmanager.h \
 
    snapshot.h
 

	
 
FORMS    += mainwindow.ui \
 
    about_dialog.ui \
 
    portcontrol.ui \
 
    snapshotview.ui
 

	
 
INCLUDEPATH += qmake/
 

	
 
CONFIG += c++11
 

	
 
RESOURCES += \
 
    misc/icons.qrc
snapshotmanager.cpp
Show inline comments
 
new file 100644
 

	
 
#include <QTime>
 
#include <QMenuBar>
 
#include <QKeySequence>
 

	
 
#include "snapshotmanager.h"
 

	
 
SnapshotManager::SnapshotManager(QMainWindow* mainWindow,
 
                                 QList<FrameBuffer*>* 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<QPointF>(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;
 
}
snapshotmanager.h
Show inline comments
 
new file 100644
 

	
 
#ifndef SNAPSHOTMANAGER_H
 
#define SNAPSHOTMANAGER_H
 

	
 
#include <QObject>
 
#include <QAction>
 
#include <QMenu>
 

	
 
#include "framebuffer.h"
 
#include "snapshot.h"
 

	
 
// class MainWindow;
 

	
 
class SnapshotManager : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    SnapshotManager(QMainWindow* mainWindow, QList<FrameBuffer*>* channelBuffers);
 
    ~SnapshotManager();
 

	
 
    QMenu* menu();
 
    QAction* takeSnapshotAction();
 

	
 
private:
 
    QMainWindow* _mainWindow;
 
    QList<FrameBuffer*>* _channelBuffers;
 

	
 
    QList<SnapShot*> snapshots;
 

	
 
    QMenu _menu;
 
    QAction _takeSnapshotAction;
 
    QAction clearAction;
 

	
 
    void updateMenu();
 

	
 
private slots:
 
    void takeSnapshot();
 
    void clearSnapshots();
 
    void deleteSnapshot(SnapShot* snapshot);
 

	
 
};
 

	
 
#endif /* SNAPSHOTMANAGER_H */
0 comments (0 inline, 0 general)