# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2015-07-24 02:23:59 # Node ID e1ce86573eef9b254dbabd14515434fb4e782a48 # Parent eec08765e92039f1061c0cf7f1e40787ad6890c4 moved some plot related code to a new class diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ qt5_wrap_ui(UI_FILES mainwindow.ui portc # Tell CMake to create the helloworld executable add_executable(serialplot WIN32 main.cpp mainwindow.cpp portcontrol.cpp customcheckablebutton.cpp + plot.cpp zoomer.cpp ${UI_FILES} misc/windows_icon.rc) diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -134,11 +134,9 @@ MainWindow::MainWindow(QWidget *parent) curves[i]->attach(ui->plot); } - // init zoomer - zoomer = new Zoomer(ui->plot->canvas(), false); - zoomer->setZoomBase(); - QObject::connect(zoomer, &Zoomer::unzoomed, - this, &MainWindow::unzoomed); + // init auto scale + ui->plot->setAxis(ui->cbAutoScale->isChecked(), + ui->spYmin->value(), ui->spYmax->value()); // init number format if (numberFormatButtons.checkedId() >= 0) @@ -477,7 +475,7 @@ void MainWindow::onAutoScaleChecked(bool { if (checked) { - ui->plot->setAxisAutoScale(QwtPlot::yLeft); + ui->plot->setAxis(true); ui->lYmin->setEnabled(false); ui->lYmax->setEnabled(false); ui->spYmin->setEnabled(false); @@ -490,18 +488,13 @@ void MainWindow::onAutoScaleChecked(bool ui->spYmin->setEnabled(true); ui->spYmax->setEnabled(true); - zoomer->zoom(0); // unzoom first, since zoomer plays with scales - - ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(), - ui->spYmax->value()); + ui->plot->setAxis(false, ui->spYmin->value(), ui->spYmax->value()); } } void MainWindow::onYScaleChanged() { - ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(), - ui->spYmax->value()); - zoomer->zoom(0); // unzoom first, since zoomer plays with scales + ui->plot->setAxis(false, ui->spYmin->value(), ui->spYmax->value()); } void MainWindow::onNumberFormatButtonToggled(int numberFormatId, bool checked) @@ -737,18 +730,3 @@ void MainWindow::messageHandler(QtMsgTyp ui->statusBar->showMessage(msg, 5000); } } - -void MainWindow::unzoomed() -{ - if (ui->cbAutoScale->isChecked()) - { - ui->plot->setAxisAutoScale(QwtPlot::yLeft); - } - else - { - ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(), - ui->spYmax->value()); - } - ui->plot->setAxisAutoScale(QwtPlot::xBottom); - ui->plot->replot(); -} diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -33,7 +33,6 @@ #include #include #include -#include "zoomer.h" #include "portcontrol.h" #include "ui_about_dialog.h" @@ -86,8 +85,6 @@ private: // `data` contains i th channels data void addChannelData(unsigned int channel, DataArray data); - Zoomer* zoomer; - NumberFormat numberFormat; unsigned int sampleSize; // number of bytes in the selected number format double (MainWindow::*readSample)(); @@ -127,7 +124,6 @@ private slots: void selectNumberFormat(NumberFormat numberFormatId); void clearPlot(); - void unzoomed(); void spsTimerTimeout(); diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -16,7 +16,7 @@ - + 0 @@ -451,7 +451,7 @@ 0 0 653 - 20 + 23 @@ -538,9 +538,9 @@ - QwtPlot + Plot QFrame -
qwt_plot.h
+
plot.h
1
diff --git a/plot.cpp b/plot.cpp new file mode 100644 --- /dev/null +++ b/plot.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 "plot.h" + +Plot::Plot(QWidget* parent) : + QwtPlot(parent), + zoomer(this->canvas(), false) +{ + isAutoScaled = false; + + QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed); + + zoomer.setZoomBase(); +} + +void Plot::setAxis(bool autoScaled, double yAxisMin, double yAxisMax) +{ + this->isAutoScaled = autoScaled; + + if (!autoScaled) + { + yMin = yAxisMin; + yMax = yAxisMax; + } + + zoomer.zoom(0); + resetAxes(); +} + +void Plot::resetAxes() +{ + if (isAutoScaled) + { + setAxisAutoScale(QwtPlot::yLeft); + } + else + { + setAxisScale(QwtPlot::yLeft, yMin, yMax); + } + + replot(); +} + +void Plot::unzoomed() +{ + setAxisAutoScale(QwtPlot::xBottom); + resetAxes(); +} diff --git a/plot.h b/plot.h new file mode 100644 --- /dev/null +++ b/plot.h @@ -0,0 +1,40 @@ +/* + 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 "zoomer.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; + + void resetAxes(); + +private slots: + void unzoomed(); +}; diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -37,6 +37,7 @@ SOURCES += main.cpp\ mainwindow.cpp \ customcheckablebutton.cpp \ portcontrol.cpp \ + plot.cpp \ zoomer.cpp HEADERS += mainwindow.h \ @@ -44,6 +45,7 @@ HEADERS += mainwindow.h \ customcheckablebutton.h \ portcontrol.h \ floatswap.h \ + plot.h \ zoomer.h FORMS += mainwindow.ui \