diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ add_executable(${PROGRAM_NAME} WIN32 zoomer.cpp hidabletabwidget.cpp framebuffer.cpp + scalepicker.cpp ${UI_FILES} misc/windows_icon.rc ) diff --git a/plot.cpp b/plot.cpp --- a/plot.cpp +++ b/plot.cpp @@ -21,7 +21,8 @@ Plot::Plot(QWidget* parent) : QwtPlot(parent), - zoomer(this->canvas(), false) + zoomer(this->canvas(), false), + scalePicker(this->axisWidget(QwtPlot::xBottom)) { isAutoScaled = false; diff --git a/plot.h b/plot.h --- a/plot.h +++ b/plot.h @@ -23,6 +23,7 @@ #include #include #include "zoomer.h" +#include "scalepicker.h" class Plot : public QwtPlot { @@ -37,6 +38,7 @@ private: double yMin, yMax; Zoomer zoomer; QwtPlotGrid grid; + ScalePicker scalePicker; void resetAxes(); diff --git a/scalepicker.cpp b/scalepicker.cpp new file mode 100644 --- /dev/null +++ b/scalepicker.cpp @@ -0,0 +1,92 @@ +/* + 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 +#include +#include + +#include "scalepicker.h" + +ScalePicker::ScalePicker(QwtScaleWidget* scaleWidget) : + QObject(scaleWidget) +{ + scaleWidget->installEventFilter(this); + _scaleWidget = scaleWidget; + started = false; +} + +bool ScalePicker::eventFilter(QObject* object, QEvent* event) +{ + if (event->type() == QEvent::MouseButtonPress || + event->type() == QEvent::MouseButtonRelease || + event->type() == QEvent::MouseMove) + { + QMouseEvent* mouseEvent = (QMouseEvent*) event; + double pos = this->position(mouseEvent); + + if (event->type() == QEvent::MouseButtonPress) + { + started = true; + firstPos = pos; + qDebug() << "Pick started:" << firstPos; + emit pickStarted(pos); + } + else if (event->type() == QEvent::MouseMove) + { + if (started) + { + qDebug() << "Picking:" << firstPos << pos; + emit picking(firstPos, pos); + } + } + else // event->type() == QEvent::MouseButtonRelease + { + if (started) + { + started = false; + qDebug() << "Picked:" << firstPos << pos; + emit picked(firstPos, pos); + } + } + return true; + } + else + { + return QObject::eventFilter(object, event); + } +} + +double ScalePicker::position(QMouseEvent* mouseEvent) +{ + // capture and convert the position of the click to the plot coordinates + double pos; + if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale || + _scaleWidget->alignment() == QwtScaleDraw::TopScale) + { + pos = mouseEvent->pos().x(); + } + else // left or right scale + { + pos = mouseEvent->pos().y(); + } + pos = _scaleWidget->scaleDraw()->scaleMap().invTransform(pos); + return pos; +} diff --git a/scalepicker.h b/scalepicker.h new file mode 100644 --- /dev/null +++ b/scalepicker.h @@ -0,0 +1,47 @@ +/* + 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 SCALEPICKER_H +#define SCALEPICKER_H + +#include +#include +#include + +class ScalePicker : public QObject +{ + Q_OBJECT +public: + ScalePicker(QwtScaleWidget* scaleWidget); + virtual bool eventFilter(QObject*, QEvent*); + +signals: + void pickStarted(double pos); + void picking(double firstPos, double lastPos); + void picked(double firstPos, double lastPos); + +private: + QwtScaleWidget* _scaleWidget; + double position(QMouseEvent*); // returns the mouse position relative to plot coordinates + + bool started; + double firstPos; +}; + +#endif // SCALEPICKER_H