Changeset - 6c4c2a001b76
[Not reviewed]
snapshots
0 8 0
Hasan Yavuz ÖZDERYA - 10 years ago 2015-09-30 05:20:27
hy@ozderya.net
code cleanup

- consistent usage of "snapshot" instead of "snapShot"
- removed some dead code
- added file headers
8 files changed with 173 insertions and 68 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -105,15 +105,12 @@ MainWindow::MainWindow(QWidget *parent) 
 
    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
snapshot.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <stddef.h>
 

	
 
#include "snapshot.h"
 
#include "snapshotview.h"
 

	
 
#include <QtDebug>
 

	
 
SnapShot::SnapShot(QMainWindow* parent, QString name) :
 
Snapshot::Snapshot(QMainWindow* parent, QString name) :
 
    QObject(parent),
 
    _showAction(name, this),
 
    _deleteAction("Delete", this)
 
{
 
    _name = name;
 

	
 
    view = NULL;
 
    mainWindow = parent;
 
    connect(&_showAction, &QAction::triggered, this, &SnapShot::show);
 
    connect(&_showAction, &QAction::triggered, this, &Snapshot::show);
 

	
 
    _deleteAction.setToolTip(QString("Delete ") + _name);
 
    connect(&_deleteAction, &QAction::triggered, this, &SnapShot::onDeleteTriggered);
 
    connect(&_deleteAction, &QAction::triggered, this, &Snapshot::onDeleteTriggered);
 
}
 

	
 
SnapShot::~SnapShot()
 
Snapshot::~Snapshot()
 
{
 
    if (view != NULL)
 
    {
 
        delete view;
 
    }
 
}
 

	
 
QAction* SnapShot::showAction()
 
QAction* Snapshot::showAction()
 
{
 
    return &_showAction;
 
}
 

	
 
QAction* SnapShot::deleteAction()
 
QAction* Snapshot::deleteAction()
 
{
 
    return &_deleteAction;
 
}
 

	
 
void SnapShot::show()
 
void Snapshot::show()
 
{
 
    if (view == NULL)
 
    {
 
        view = new SnapShotView(mainWindow, this);
 
        connect(view, &SnapShotView::closed, this, &SnapShot::viewClosed);
 
        view = new SnapshotView(mainWindow, this);
 
        connect(view, &SnapshotView::closed, this, &Snapshot::viewClosed);
 
    }
 
    view->show();
 
    view->activateWindow();
 
    view->raise();
 
}
 

	
 
void SnapShot::viewClosed()
 
void Snapshot::viewClosed()
 
{
 
    view->deleteLater();
 
    view = NULL;
 
}
 

	
 
void SnapShot::onDeleteTriggered()
 
void Snapshot::onDeleteTriggered()
 
{
 
    emit deleteRequested(this);
 
}
 

	
 
QString SnapShot::name()
 
QString Snapshot::name()
 
{
 
    return _name;
 
}
 

	
 
void SnapShot::setName(QString name)
 
void Snapshot::setName(QString name)
 
{
 
    _name = name;
 
    _showAction.setText(_name);
 
    emit nameChanged(this);
 
}
snapshot.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef SNAPSHOT_H
 
#define SNAPSHOT_H
 

	
 
#include <QObject>
 
#include <QMainWindow>
 
#include <QAction>
 
#include <QVector>
 
#include <QString>
 

	
 
class SnapShotView;
 
class SnapshotView;
 

	
 
class SnapShot : public QObject
 
class Snapshot : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    SnapShot(QMainWindow* parent, QString name);
 
    ~SnapShot();
 
    Snapshot(QMainWindow* parent, QString name);
 
    ~Snapshot();
 

	
 
    QVector<QVector<QPointF>> data;
 
    QAction* showAction();
 
    QAction* deleteAction();
 

	
 
    QString name();
 
    void setName(QString name);
 

	
 
signals:
 
    void deleteRequested(SnapShot*);
 
    void nameChanged(SnapShot*);
 
    void deleteRequested(Snapshot*);
 
    void nameChanged(Snapshot*);
 

	
 
private:
 
    QString _name;
 
    QAction _showAction;
 
    QAction _deleteAction;
 
    QMainWindow* mainWindow;
 
    SnapShotView* view;
 
    SnapshotView* view;
 

	
 
private slots:
 
    void show();
 
    void viewClosed();
 

	
 
    void onDeleteTriggered();
snapshotmanager.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QTime>
 
#include <QMenuBar>
 
#include <QKeySequence>
 
#include <QFileDialog>
 
#include <QFile>
 
@@ -41,33 +59,33 @@ SnapshotManager::~SnapshotManager()
 
    }
 
}
 

	
 
void SnapshotManager::takeSnapshot()
 
{
 
    QString name = QTime::currentTime().toString("'Snapshot ['HH:mm:ss']'");
 
    auto snapShot = new SnapShot(_mainWindow, name);
 
    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));
 
        snapshot->data.append(QVector<QPointF>(numOfSamples));
 
        for (unsigned i = 0; i < numOfSamples; i++)
 
        {
 
            snapShot->data[ci][i] = _channelBuffers->at(ci)->sample(i);
 
            snapshot->data[ci][i] = _channelBuffers->at(ci)->sample(i);
 
        }
 
    }
 

	
 
    addSnapshot(snapShot);
 
    addSnapshot(snapshot);
 
}
 

	
 
void SnapshotManager::addSnapshot(SnapShot* snapshot, bool update_menu)
 
void SnapshotManager::addSnapshot(Snapshot* snapshot, bool update_menu)
 
{
 
    snapshots.append(snapshot);
 
    QObject::connect(snapshot, &SnapShot::deleteRequested,
 
    QObject::connect(snapshot, &Snapshot::deleteRequested,
 
                     this, &SnapshotManager::deleteSnapshot);
 
    if (update_menu) updateMenu();
 
}
 

	
 
void SnapshotManager::updateMenu()
 
{
 
@@ -93,13 +111,13 @@ void SnapshotManager::clearSnapshots()
 
        delete snapshot;
 
    }
 
    snapshots.clear();
 
    updateMenu();
 
}
 

	
 
void SnapshotManager::deleteSnapshot(SnapShot* snapshot)
 
void SnapshotManager::deleteSnapshot(Snapshot* snapshot)
 
{
 
    snapshots.removeOne(snapshot);
 
    snapshot->deleteLater(); // regular delete causes a crash when triggered from menu
 
    updateMenu();
 
}
 

	
 
@@ -160,13 +178,13 @@ void SnapshotManager::loadSnapshotFromFi
 
            }
 
            data[ci].append(QPointF(lineNum-1, y));
 
        }
 
        lineNum++;
 
    }
 

	
 
    auto snapshot = new SnapShot(_mainWindow, QFileInfo(fileName).baseName());
 
    auto snapshot = new Snapshot(_mainWindow, QFileInfo(fileName).baseName());
 
    snapshot->data = data;
 

	
 
    addSnapshot(snapshot, false);
 
}
 

	
 
QMenu* SnapshotManager::menu()
snapshotmanager.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#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);
 
@@ -23,25 +39,25 @@ public:
 
    QAction* takeSnapshotAction();
 

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

	
 
    QList<SnapShot*> snapshots;
 
    QList<Snapshot*> snapshots;
 

	
 
    QMenu _menu;
 
    QAction _takeSnapshotAction;
 
    QAction loadSnapshotAction;
 
    QAction clearAction;
 

	
 
    void addSnapshot(SnapShot* snapshot, bool update_menu=true);
 
    void addSnapshot(Snapshot* snapshot, bool update_menu=true);
 
    void updateMenu();
 

	
 
private slots:
 
    void takeSnapshot();
 
    void clearSnapshots();
 
    void deleteSnapshot(SnapShot* snapshot);
 
    void deleteSnapshot(Snapshot* snapshot);
 
    void loadSnapshots();
 
    void loadSnapshotFromFile(QString fileName);
 
};
 

	
 
#endif /* SNAPSHOTMANAGER_H */
snapshotview.cpp
Show inline comments
 
#include "snapshotview.h"
 
#include "ui_snapshotview.h"
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QSaveFile>
 

	
 
SnapShotView::SnapShotView(QWidget *parent, SnapShot* snapShot) :
 
#include "snapshotview.h"
 
#include "ui_snapshotview.h"
 

	
 
SnapshotView::SnapshotView(QWidget *parent, Snapshot* snapshot) :
 
    QMainWindow(parent),
 
    ui(new Ui::SnapShotView),
 
    ui(new Ui::SnapshotView),
 
    renameDialog(this)
 
{
 
    _snapShot = snapShot;
 
    _snapshot = snapshot;
 

	
 
    ui->setupUi(this);
 
    ui->menuSnapshot->insertAction(ui->actionClose, snapShot->deleteAction());
 
    this->setWindowTitle(snapShot->name());
 
    ui->menuSnapshot->insertAction(ui->actionClose, snapshot->deleteAction());
 
    this->setWindowTitle(snapshot->name());
 

	
 
    unsigned numOfChannels = snapShot->data.size();
 
    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->setSamples(snapshot->data[ci]);
 
        curve->setPen(Plot::makeColor(ci));
 
        curve->attach(ui->plot);
 
    }
 

	
 
    renameDialog.setWindowTitle("Rename Snapshot");
 
    renameDialog.setLabelText("Enter new name:");
 
    connect(ui->actionRename, &QAction::triggered,
 
            this, &SnapShotView::showRenameDialog);
 
            this, &SnapshotView::showRenameDialog);
 

	
 
    connect(ui->actionExport, &QAction::triggered,
 
            this, &SnapShotView::save);
 
            this, &SnapshotView::save);
 

	
 
    for (auto a : ui->plot->menuActions())
 
    {
 
        ui->menuView->addAction(a);
 
    }
 
}
 

	
 
SnapShotView::~SnapShotView()
 
SnapshotView::~SnapshotView()
 
{
 
    for (auto curve : curves)
 
    {
 
        delete curve;
 
    }
 
    delete ui;
 
}
 

	
 
void SnapShotView::closeEvent(QCloseEvent *event)
 
void SnapshotView::closeEvent(QCloseEvent *event)
 
{
 
    QMainWindow::closeEvent(event);
 
    emit closed();
 
}
 

	
 
void SnapShotView::showRenameDialog()
 
void SnapshotView::showRenameDialog()
 
{
 
    renameDialog.setTextValue(_snapShot->name());
 
    renameDialog.setTextValue(_snapshot->name());
 
    renameDialog.open(this, SLOT(renameSnapshot(QString)));
 
}
 

	
 
void SnapShotView::renameSnapshot(QString name)
 
void SnapshotView::renameSnapshot(QString name)
 
{
 
    _snapShot->setName(name);
 
    _snapshot->setName(name);
 
    setWindowTitle(name);
 
}
 

	
 
void SnapShotView::save()
 
void SnapshotView::save()
 
{
 
    QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File"));
 

	
 
    if (fileName.isNull()) return; // user canceled
 

	
 
    // TODO: remove code duplication (MainWindow::onExportCsv)
 
    QSaveFile file(fileName);
 

	
 
    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
 
    {
 
        QTextStream fileStream(&file);
 

	
 
        unsigned numOfChannels = _snapShot->data.size();
 
        unsigned numOfSamples = _snapShot->data[0].size();
 
        unsigned numOfChannels = _snapshot->data.size();
 
        unsigned numOfSamples = _snapshot->data[0].size();
 

	
 
        // print header
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            fileStream << "Channel " << ci;
 
            if (ci != numOfChannels-1) fileStream << ",";
 
@@ -92,13 +111,13 @@ void SnapShotView::save()
 

	
 
        // print rows
 
        for (unsigned int i = 0; i < numOfSamples; i++)
 
        {
 
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
            {
 
                fileStream << _snapShot->data[ci][i].y();
 
                fileStream << _snapshot->data[ci][i].y();
 
                if (ci != numOfChannels-1) fileStream << ",";
 
            }
 
            fileStream << '\n';
 
        }
 

	
 
        if (!file.commit())
snapshotview.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef SNAPSHOTVIEW_H
 
#define SNAPSHOTVIEW_H
 

	
 
#include <QMainWindow>
 
#include <QInputDialog>
 
#include <QFileDialog>
 
#include <QVector>
 
#include <QPointF>
 
#include <QPen>
 
#include <QCloseEvent>
 
#include <qwt_plot_curve.h>
 

	
 
#include "plot.h"
 
#include "snapshot.h"
 

	
 
namespace Ui {
 
class SnapShotView;
 
class SnapshotView;
 
}
 

	
 
class SnapShotView : public QMainWindow
 
class SnapshotView : public QMainWindow
 
{
 
    Q_OBJECT
 

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

	
 
signals:
 
    void closed();
 

	
 
private:
 
    Ui::SnapShotView *ui;
 
    Ui::SnapshotView *ui;
 
    QList<QwtPlotCurve*> curves;
 
    SnapShot* _snapShot;
 
    Snapshot* _snapshot;
 
    QInputDialog renameDialog;
 

	
 
    void closeEvent(QCloseEvent *event);
 

	
 
private slots:
 
    void showRenameDialog();
snapshotview.ui
Show inline comments
 
<?xml version="1.0" encoding="UTF-8"?>
 
<ui version="4.0">
 
 <class>SnapShotView</class>
 
 <widget class="QMainWindow" name="SnapShotView">
 
 <class>SnapshotView</class>
 
 <widget class="QMainWindow" name="SnapshotView">
 
  <property name="geometry">
 
   <rect>
 
    <x>0</x>
 
    <y>0</y>
 
    <width>544</width>
 
    <height>449</height>
 
@@ -83,13 +83,13 @@
 
 </customwidgets>
 
 <resources/>
 
 <connections>
 
  <connection>
 
   <sender>actionClose</sender>
 
   <signal>triggered()</signal>
 
   <receiver>SnapShotView</receiver>
 
   <receiver>SnapshotView</receiver>
 
   <slot>close()</slot>
 
   <hints>
 
    <hint type="sourcelabel">
 
     <x>-1</x>
 
     <y>-1</y>
 
    </hint>
0 comments (0 inline, 0 general)