Changeset - b0405ffbfd80
[Not reviewed]
snapshots
0 7 3
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-19 15:24:02
hy@ozderya.net
take snapshot button creates a new snapshot and opens its window
10 files changed with 218 insertions and 10 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -43,38 +43,44 @@ set(QWT_STATIC_INCLUDE "" CACHE PATH "Pa
 

	
 
if (QWT_USE_STATIC)
 
  set(QWT_LIBRARY ${QWT_STATIC_LIBRARY})
 
  set(QWT_INCLUDE_DIR ${QWT_STATIC_INCLUDE})
 
else (QWT_USE_STATIC)
 
  find_package(Qwt 6.1 REQUIRED)
 
endif (QWT_USE_STATIC)
 

	
 
# includes
 
include_directories(${QWT_INCLUDE_DIR})
 

	
 
# wrap UI and resource files
 
qt5_wrap_ui(UI_FILES mainwindow.ui portcontrol.ui about_dialog.ui)
 
qt5_wrap_ui(UI_FILES
 
  mainwindow.ui
 
  portcontrol.ui
 
  about_dialog.ui
 
  snapshotview.ui
 
  )
 
qt5_add_resources(RES_FILES misc/icons.qrc)
 

	
 
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
 
  snapshotview.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")
 

	
framebuffer.cpp
Show inline comments
 
@@ -36,25 +36,25 @@ FrameBuffer::~FrameBuffer()
 
void FrameBuffer::resize(size_t size)
 
{
 
    int offset = size - _size;
 
    if (offset == 0) return;
 

	
 
    double* newData = new double[size];
 

	
 
    // move data to new array
 
    int fill_start = offset > 0 ? offset : 0;
 

	
 
    for (int i = fill_start; i < int(size); i++)
 
    {
 
        newData[i] = _sample(i - offset);
 
        newData[i] = sampleY(i - offset);
 
    }
 

	
 
    // fill the beginning of the new data
 
    if (fill_start > 0)
 
    {
 
        for (int i = 0; i < fill_start; i++)
 
        {
 
            newData[i] = 0;
 
        }
 
    }
 

	
 
    // data is ready, clean and re-point
 
@@ -134,26 +134,26 @@ void FrameBuffer::addSamples(double* sam
 
void FrameBuffer::clear()
 
{
 
    for (size_t i=0; i < _size; i++) data[i] = 0.;
 
}
 

	
 
size_t FrameBuffer::size() const
 
{
 
    return _size;
 
}
 

	
 
QPointF FrameBuffer::sample(size_t i) const
 
{
 
    return QPointF(i, _sample(i));
 
    return QPointF(i, sampleY(i));
 
}
 

	
 
QRectF FrameBuffer::boundingRect() const
 
{
 
    return _boundingRect;
 
}
 

	
 
double FrameBuffer::_sample(size_t i) const
 
double FrameBuffer::sampleY(size_t i) const
 
{
 
    size_t index = headIndex + i;
 
    if (index >= _size) index -= _size;
 
    return data[index];
 
}
framebuffer.h
Show inline comments
 
@@ -28,25 +28,25 @@ class FrameBuffer : public QwtSeriesData
 
{
 
public:
 
    FrameBuffer(size_t size);
 
    ~FrameBuffer();
 

	
 
    void resize(size_t size);
 
    void addSamples(double* samples, size_t size);
 
    void clear(); // fill 0
 

	
 
    // QwtSeriesData implementations
 
    size_t size() const;
 
    QPointF sample(size_t i) const;
 
    double sampleY(size_t i) const;
 
    QRectF boundingRect() const;
 

	
 
private:
 
    size_t _size; // size of `data`
 
    double* data;
 
    size_t headIndex; // indicates the actual `0` index of the ring buffer
 

	
 
    QRectF _boundingRect;
 

	
 
    double _sample(size_t i) const;
 
};
 

	
 
#endif // FRAMEBUFFER_H
mainwindow.cpp
Show inline comments
 
@@ -22,24 +22,26 @@
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QTextStream>
 
#include <QtDebug>
 
#include <QtEndian>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <snapshotview.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;
 
@@ -103,24 +105,27 @@ 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()));
 

	
 
    // 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);
 
    numberFormatButtons.addButton(ui->rbInt32,  NumberFormat_int32);
 
@@ -232,24 +237,30 @@ MainWindow::MainWindow(QWidget *parent) 
 
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();});
 

	
 
@@ -736,12 +747,32 @@ 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()
 
{
 
    qDebug() << "taking a snopshot yay!";
 
    auto snapShot = new SnapShot();
 
    snapShot->name = QString("SnapShot");
 

	
 
    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);
 

	
 
    auto sv = new SnapShotView(this, snapShot);
 
    sv->show();
 
}
mainwindow.h
Show inline comments
 
@@ -26,24 +26,25 @@
 
#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"
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
class MainWindow : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
public:
 
@@ -90,24 +91,27 @@ 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;
 

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

	
 
    QColor makeColor(unsigned int channelIndex);
 

	
 
private slots:
 
    void onPortToggled(bool open);
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
@@ -119,19 +123,21 @@ 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 demoTimerTimeout();
 
    void enableDemo(bool enabled);
 

	
 
    void onExportCsv();
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -7,25 +7,25 @@
 
    <x>0</x>
 
    <y>0</y>
 
    <width>653</width>
 
    <height>534</height>
 
   </rect>
 
  </property>
 
  <property name="windowTitle">
 
   <string>SerialPlot</string>
 
  </property>
 
  <widget class="QWidget" name="centralWidget">
 
   <layout class="QVBoxLayout" name="verticalLayout_2">
 
    <item>
 
     <widget class="Plot" name="plot">
 
     <widget class="Plot" name="plot" native="true">
 
      <property name="sizePolicy">
 
       <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
 
        <horstretch>0</horstretch>
 
        <verstretch>0</verstretch>
 
       </sizepolicy>
 
      </property>
 
     </widget>
 
    </item>
 
    <item>
 
     <widget class="HidableTabWidget" name="tabWidget">
 
      <property name="sizePolicy">
 
       <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
 
@@ -492,24 +492,25 @@
 
   <addaction name="menuView"/>
 
   <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>
 
@@ -595,30 +596,41 @@
 
  </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>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>
 
 </widget>
 
 <layoutdefault spacing="6" margin="11"/>
 
 <customwidgets>
 
  <customwidget>
 
   <class>Plot</class>
 
   <extends>QFrame</extends>
 
   <extends>QWidget</extends>
 
   <header>plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
  <customwidget>
 
   <class>HidableTabWidget</class>
 
   <extends>QTabWidget</extends>
 
   <header>hidabletabwidget.h</header>
 
   <container>1</container>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
serialplot.pro
Show inline comments
 
@@ -33,36 +33,39 @@ TEMPLATE = app
 
CONFIG += qwt
 

	
 

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

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

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

	
 
INCLUDEPATH += qmake/
 

	
 
CONFIG += c++11
 

	
 
RESOURCES += \
 
    misc/icons.qrc
snapshotview.cpp
Show inline comments
 
new file 100644
 
#include "snapshotview.h"
 
#include "ui_snapshotview.h"
 

	
 
SnapShotView::SnapShotView(QWidget *parent, SnapShot* snapShot) :
 
    QMainWindow(parent),
 
    ui(new Ui::SnapShotView)
 
{
 
    ui->setupUi(this);
 

	
 
    unsigned numOfChannels = snapShot->data.size();
 

	
 
    for (unsigned ci = 0; ci < numOfChannels; ci++)
 
    {
 
        QwtPlotCurve* curve = new QwtPlotCurve();
 
        curves.append(curve);
 
        curve->setSamples(snapShot->data[ci]);
 
        curve->attach(ui->plot);
 
    }
 

	
 
    _snapShot = snapShot;
 
}
 

	
 
SnapShotView::~SnapShotView()
 
{
 
    for (auto curve : curves)
 
    {
 
        delete curve;
 
    }
 
    delete ui;
 
}
snapshotview.h
Show inline comments
 
new file 100644
 
#ifndef SNAPSHOTVIEW_H
 
#define SNAPSHOTVIEW_H
 

	
 
#include <QMainWindow>
 
#include <QVector>
 
#include <QPointF>
 
#include <qwt_plot_curve.h>
 
#include "plot.h"
 

	
 
class SnapShotView;
 
struct SnapShot
 
{
 
    QString name;
 
    QVector<QVector<QPointF>> data;
 
    SnapShotView* view;
 
};
 

	
 
namespace Ui {
 
class SnapShotView;
 
}
 

	
 
class SnapShotView : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit SnapShotView(QWidget *parent, SnapShot* snapShot);
 
    ~SnapShotView();
 

	
 
private:
 
    Ui::SnapShotView *ui;
 
    QList<QwtPlotCurve*> curves;
 

	
 
    SnapShot* _snapShot;
 
};
 

	
 
#endif // SNAPSHOTVIEW_H
snapshotview.ui
Show inline comments
 
new file 100644
 
<?xml version="1.0" encoding="UTF-8"?>
 
<ui version="4.0">
 
 <class>SnapShotView</class>
 
 <widget class="QMainWindow" name="SnapShotView">
 
  <property name="geometry">
 
   <rect>
 
    <x>0</x>
 
    <y>0</y>
 
    <width>544</width>
 
    <height>449</height>
 
   </rect>
 
  </property>
 
  <property name="windowTitle">
 
   <string>MainWindow</string>
 
  </property>
 
  <widget class="QWidget" name="centralwidget">
 
   <layout class="QVBoxLayout" name="verticalLayout">
 
    <item>
 
     <widget class="Plot" name="plot" native="true"/>
 
    </item>
 
   </layout>
 
  </widget>
 
  <widget class="QMenuBar" name="menubar">
 
   <property name="geometry">
 
    <rect>
 
     <x>0</x>
 
     <y>0</y>
 
     <width>544</width>
 
     <height>27</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuFile">
 
    <property name="title">
 
     <string>File</string>
 
    </property>
 
    <addaction name="actionExportCSV"/>
 
   </widget>
 
   <widget class="QMenu" name="menuView">
 
    <property name="title">
 
     <string>View</string>
 
    </property>
 
   </widget>
 
   <addaction name="menuFile"/>
 
   <addaction name="menuView"/>
 
  </widget>
 
  <widget class="QToolBar" name="toolBar">
 
   <property name="windowTitle">
 
    <string>toolBar</string>
 
   </property>
 
   <attribute name="toolBarArea">
 
    <enum>TopToolBarArea</enum>
 
   </attribute>
 
   <attribute name="toolBarBreak">
 
    <bool>false</bool>
 
   </attribute>
 
   <addaction name="actionExportCSV"/>
 
   <addaction name="actionDelete"/>
 
  </widget>
 
  <action name="actionExportCSV">
 
   <property name="text">
 
    <string>Export CSV</string>
 
   </property>
 
  </action>
 
  <action name="actionDelete">
 
   <property name="text">
 
    <string>Delete</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Delete this snapshot!</string>
 
   </property>
 
  </action>
 
 </widget>
 
 <customwidgets>
 
  <customwidget>
 
   <class>Plot</class>
 
   <extends>QWidget</extends>
 
   <header>plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
 
</ui>
0 comments (0 inline, 0 general)