Changeset - 19375fc29267
[Not reviewed]
scalezoomer
0 3 2
Hasan Yavuz ÖZDERYA - 10 years ago 2015-08-26 08:44:02
hy@ozderya.net
adding ScalePicker
5 files changed with 144 insertions and 1 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -54,24 +54,25 @@ include_directories(${QWT_INCLUDE_DIR})
 
# wrap UI files
 
qt5_wrap_ui(UI_FILES mainwindow.ui portcontrol.ui about_dialog.ui)
 

	
 
add_executable(${PROGRAM_NAME} WIN32
 
  main.cpp
 
  mainwindow.cpp
 
  portcontrol.cpp
 
  customcheckablebutton.cpp
 
  plot.cpp
 
  zoomer.cpp
 
  hidabletabwidget.cpp
 
  framebuffer.cpp
 
  scalepicker.cpp
 
  ${UI_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")
 

	
 
# Enable C++11 support, fail if not supported
plot.cpp
Show inline comments
 
@@ -12,25 +12,26 @@
 
  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 "plot.h"
 

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false)
 
    zoomer(this->canvas(), false),
 
    scalePicker(this->axisWidget(QwtPlot::xBottom))
 
{
 
    isAutoScaled = false;
 

	
 
    QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed);
 

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 

	
 
    darkBackground(false);
 
}
 

	
 
void Plot::setAxis(bool autoScaled, double yAxisMin, double yAxisMax)
plot.h
Show inline comments
 
@@ -14,38 +14,40 @@
 
  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 PLOT_H
 
#define PLOT_H
 

	
 
#include <qwt_plot.h>
 
#include <qwt_plot_grid.h>
 
#include "zoomer.h"
 
#include "scalepicker.h"
 

	
 
class Plot : public QwtPlot
 
{
 
    Q_OBJECT
 

	
 
public:
 
    Plot(QWidget* parent = 0);
 
    void setAxis(bool autoScaled, double yMin = 0, double yMax = 1);
 

	
 
private:
 
    bool isAutoScaled;
 
    double yMin, yMax;
 
    Zoomer zoomer;
 
    QwtPlotGrid grid;
 
    ScalePicker scalePicker;
 

	
 
    void resetAxes();
 

	
 
public slots:
 
    void showGrid(bool show = true);
 
    void showMinorGrid(bool show = true);
 
    void unzoom();
 
    void darkBackground(bool enabled = true);
 

	
 
private slots:
 
    void unzoomed();
 
};
scalepicker.cpp
Show inline comments
 
new file 100644
 
/*
 
  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 <QEvent>
 
#include <QMouseEvent>
 
#include <qwt_scale_widget.h>
 
#include <qwt_scale_map.h>
 
#include <QtDebug>
 

	
 
#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;
 
}
scalepicker.h
Show inline comments
 
new file 100644
 
/*
 
  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 SCALEPICKER_H
 
#define SCALEPICKER_H
 

	
 
#include <QObject>
 
#include <QMouseEvent>
 
#include <qwt_scale_widget.h>
 

	
 
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
0 comments (0 inline, 0 general)