diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ add_executable(${PROGRAM_NAME} WIN32 snapshot.cpp snapshotview.cpp snapshotmanager.cpp + plotsnapshotoverlay.cpp ${UI_FILES} ${RES_FILES} misc/windows_icon.rc diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -108,6 +108,9 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionClear, SIGNAL(triggered(bool)), this, SLOT(clearPlot())); + QObject::connect(snapshotMan.takeSnapshotAction(), &QAction::triggered, + ui->plot, &Plot::flashSnapshotOverlay); + // setup number of channels spinbox QObject::connect(ui->spNumOfChannels, SELECT::OVERLOAD_OF(&QSpinBox::valueChanged), diff --git a/plot.cpp b/plot.cpp --- a/plot.cpp +++ b/plot.cpp @@ -72,6 +72,13 @@ Plot::Plot(QWidget* parent) : connect(&_unzoomAction, &QAction::triggered, this, &Plot::unzoom); connect(&_darkBackgroundAction, SELECT::OVERLOAD_OF(&QAction::triggered), this, &Plot::darkBackground); + + snapshotOverlay = NULL; +} + +Plot::~Plot() +{ + if (snapshotOverlay != NULL) delete snapshotOverlay; } void Plot::setAxis(bool autoScaled, double yAxisMin, double yAxisMax) @@ -187,3 +194,26 @@ QColor Plot::makeColor(unsigned int chan return QColor::fromHsv(360*i/n + 360/pow(2,p+1), 255, 230); } } + +void Plot::flashSnapshotOverlay() +{ + if (snapshotOverlay != NULL) delete snapshotOverlay; + + QColor color; + if (_darkBackgroundAction.isChecked()) + { + color = QColor(Qt::white); + } + else + { + color = QColor(Qt::black); + } + + snapshotOverlay = new PlotSnapshotOverlay(this->canvas(), color); + connect(snapshotOverlay, &PlotSnapshotOverlay::done, + [this]() + { + delete snapshotOverlay; + snapshotOverlay = NULL; + }); +} diff --git a/plot.h b/plot.h --- a/plot.h +++ b/plot.h @@ -26,8 +26,10 @@ #include #include #include + #include "zoomer.h" #include "scalezoomer.h" +#include "plotsnapshotoverlay.h" class Plot : public QwtPlot { @@ -35,6 +37,7 @@ class Plot : public QwtPlot public: Plot(QWidget* parent = 0); + ~Plot(); void setAxis(bool autoScaled, double yMin = 0, double yMax = 1); QList menuActions(); @@ -48,6 +51,7 @@ private: ScaleZoomer sZoomer; QwtPlotGrid grid; QwtPlotShapeItem rectItem; + PlotSnapshotOverlay* snapshotOverlay; QAction _showGridAction; QAction _showMinorGridAction; @@ -62,6 +66,8 @@ public slots: void unzoom(); void darkBackground(bool enabled = true); + void flashSnapshotOverlay(); + private slots: void unzoomed(); }; diff --git a/plotsnapshotoverlay.cpp b/plotsnapshotoverlay.cpp new file mode 100644 --- /dev/null +++ b/plotsnapshotoverlay.cpp @@ -0,0 +1,65 @@ +/* + 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 . +*/ + +#include +#include + +#include "plotsnapshotoverlay.h" + +#define LINE_WIDTH (10) +#define ANIM_LENGTH (500) // milliseconds +#define UPDATE_PERIOD (20) // milliseconds + +PlotSnapshotOverlay::PlotSnapshotOverlay(QWidget* widget, QColor color) : + QwtWidgetOverlay(widget) +{ + _color = color; + animTimer.setSingleShot(true); + animTimer.setInterval(ANIM_LENGTH); + updateTimer.setSingleShot(false); + updateTimer.setInterval(UPDATE_PERIOD); + + connect(&updateTimer, &QTimer::timeout, [this](){this->updateOverlay();}); + connect(&animTimer, &QTimer::timeout, &updateTimer, &QTimer::stop); + connect(&animTimer, &QTimer::timeout, this, &PlotSnapshotOverlay::done); + + animTimer.start(); + updateTimer.start(); +} + +void PlotSnapshotOverlay::drawOverlay(QPainter* painter) const +{ + if (!animTimer.isActive()) return; + QColor lineColor = _color; + + double fadingRatio = ((double) animTimer.remainingTime()) / ANIM_LENGTH; + lineColor.setAlpha(std::min(255, (int) (255*fadingRatio))); + + QPen pen(lineColor); + pen.setWidth(LINE_WIDTH); + pen.setJoinStyle(Qt::MiterJoin); + + int width = painter->device()->width(); + int height = painter->device()->height(); + + painter->save(); + painter->setPen(pen); + painter->drawRect(LINE_WIDTH/2, LINE_WIDTH/2, width-LINE_WIDTH, height-LINE_WIDTH); + painter->restore(); +} diff --git a/plotsnapshotoverlay.h b/plotsnapshotoverlay.h new file mode 100644 --- /dev/null +++ b/plotsnapshotoverlay.h @@ -0,0 +1,48 @@ +/* + 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 . +*/ + +#ifndef PLOTSNAPSHOTOVERLAY_H +#define PLOTSNAPSHOTOVERLAY_H + +#include +#include +#include +#include + +// Draws a sort of flashing effect on plot widget when snapshot taken +class PlotSnapshotOverlay : public QwtWidgetOverlay +{ + Q_OBJECT + +public: + PlotSnapshotOverlay(QWidget* widget, QColor color); + +protected: + void drawOverlay(QPainter*) const; + +signals: + void done(); // indicates that animation completed + +private: + QColor _color; + QTimer animTimer; // controls fading + QTimer updateTimer; // need to force repaint the canvas +}; + +#endif // PLOTSNAPSHOTOVERLAY_H