Changeset - e1ce86573eef
[Not reviewed]
default
0 5 2
Hasan Yavuz ÖZDERYA - 10 years ago 2015-07-24 02:23:59
hy@ozderya.net
moved some plot related code to a new class
7 files changed with 118 insertions and 36 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -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)
 

	
mainwindow.cpp
Show inline comments
 
@@ -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();
 
}
mainwindow.h
Show inline comments
 
@@ -33,7 +33,6 @@
 
#include <QtGlobal>
 
#include <qwt_plot_curve.h>
 
#include <qwt_plot_textlabel.h>
 
#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();
 

	
mainwindow.ui
Show inline comments
 
@@ -16,7 +16,7 @@
 
  <widget class="QWidget" name="centralWidget">
 
   <layout class="QVBoxLayout" name="verticalLayout_2">
 
    <item>
 
     <widget class="QwtPlot" name="plot">
 
     <widget class="Plot" name="plot">
 
      <property name="sizePolicy">
 
       <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
 
        <horstretch>0</horstretch>
 
@@ -451,7 +451,7 @@
 
     <x>0</x>
 
     <y>0</y>
 
     <width>653</width>
 
     <height>20</height>
 
     <height>23</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuHelp">
 
@@ -538,9 +538,9 @@
 
 <layoutdefault spacing="6" margin="11"/>
 
 <customwidgets>
 
  <customwidget>
 
   <class>QwtPlot</class>
 
   <class>Plot</class>
 
   <extends>QFrame</extends>
 
   <header>qwt_plot.h</header>
 
   <header>plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
 </customwidgets>
plot.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 "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();
 
}
plot.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/>.
 
*/
 

	
 
#include <qwt_plot.h>
 
#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();
 
};
serialplot.pro
Show inline comments
 
@@ -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 \
0 comments (0 inline, 0 general)