Changeset - 90fb38350cfd
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-08 17:15:46
hy@ozderya.net
implemented plot axis limit controls
3 files changed with 91 insertions and 9 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QtDebug>
 

	
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include "utils.h"
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow)
 
{
 
    ui->setupUi(this);
 

	
 
    // init UI signals
 
    QObject::connect(ui->pbReloadPorts, &QPushButton::clicked,
 
                     this, &MainWindow::loadPortList);
 

	
 
@@ -26,32 +27,48 @@ MainWindow::MainWindow(QWidget *parent) 
 
                     this, &MainWindow::selectPort);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &MainWindow::selectBaudRate);
 

	
 
    QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                     this, &MainWindow::onDataReady);
 

	
 
    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfSamplesChanged);
 

	
 
    QObject::connect(ui->cbAutoScale, &QCheckBox::toggled,
 
                     this, &MainWindow::onAutoScaleChecked);
 

	
 
    QObject::connect(ui->spYmin, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    QObject::connect(ui->spYmax, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    // init port signals
 
    QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)),
 
                     this, SLOT(onPortError(QSerialPort::SerialPortError)));
 

	
 
    loadPortList();
 
    loadBaudRateList();
 
    ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600"));
 

	
 
    // set limits for axis limit boxes
 
    ui->spYmin->setRange(std::numeric_limits<double>::min(),
 
                         std::numeric_limits<double>::max());
 

	
 
    ui->spYmax->setRange(std::numeric_limits<double>::min(),
 
                         std::numeric_limits<double>::max());
 

	
 
    // init plot
 
    numOfSamples = ui->spNumOfSamples->value();
 
    dataArray.resize(numOfSamples);
 
    dataX.resize(numOfSamples);
 
    for (int i = 0; i < dataX.size(); i++)
 
    {
 
        dataX[i] = i;
 
    }
 
    curve.setSamples(dataX, dataArray);
 
    curve.attach(ui->plot);
 
}
 

	
 
@@ -223,12 +240,40 @@ void MainWindow::onNumOfSamplesChanged(i
 
        dataArray.remove(0, oldNum - numOfSamples);
 
    }
 
    else if(numOfSamples > oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        for (int i = oldNum; i < numOfSamples; i++)
 
        {
 
            dataX[i] = i;
 
            dataArray.prepend(0);
 
        }
 
    }
 
}
 

	
 
void MainWindow::onAutoScaleChecked(bool checked)
 
{
 
    if (checked)
 
    {
 
        ui->plot->setAxisAutoScale(QwtPlot::yLeft);
 
        ui->lYmin->setEnabled(false);
 
        ui->lYmax->setEnabled(false);
 
        ui->spYmin->setEnabled(false);
 
        ui->spYmax->setEnabled(false);
 
    }
 
    else
 
    {
 
        ui->lYmin->setEnabled(true);
 
        ui->lYmax->setEnabled(true);
 
        ui->spYmin->setEnabled(true);
 
        ui->spYmax->setEnabled(true);
 

	
 
        ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
 
                               ui->spYmax->value());
 
    }
 
}
 

	
 
void MainWindow::onYScaleChanged()
 
{
 
    ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
 
                           ui->spYmax->value());
 
}
mainwindow.h
Show inline comments
 
@@ -32,18 +32,20 @@ private:
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
 
    void selectBaudRate(QString baudRate);
 

	
 
    void onDataReady();
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
 
    void onNumOfSamplesChanged(int value);
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -431,31 +431,37 @@
 
        <item>
 
         <widget class="Line" name="line">
 
          <property name="orientation">
 
           <enum>Qt::Vertical</enum>
 
          </property>
 
         </widget>
 
        </item>
 
        <item>
 
         <layout class="QFormLayout" name="formLayout_2">
 
          <property name="fieldGrowthPolicy">
 
           <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
 
          </property>
 
          <item row="2" column="1">
 
           <widget class="QDoubleSpinBox" name="spYmax"/>
 
          </item>
 
          <item row="1" column="1">
 
          <item row="3" column="1">
 
           <widget class="QDoubleSpinBox" name="spYmin">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="maximumSize">
 
             <size>
 
              <width>75</width>
 
              <height>16777215</height>
 
             </size>
 
            </property>
 
            <property name="value">
 
             <double>1.000000000000000</double>
 
             <double>0.000000000000000</double>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="0" column="1">
 
           <widget class="QSpinBox" name="spNumOfSamples">
 
            <property name="keyboardTracking">
 
             <bool>false</bool>
 
            </property>
 
            <property name="minimum">
 
             <number>2</number>
 
            </property>
 
            <property name="maximum">
 
@@ -465,52 +471,81 @@
 
             <number>1000</number>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="0" column="0">
 
           <widget class="QLabel" name="label_3">
 
            <property name="text">
 
             <string>Number Of Samples:</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="1" column="0">
 
           <widget class="QCheckBox" name="cbYmin">
 
           <widget class="QCheckBox" name="cbAutoScale">
 
            <property name="text">
 
             <string>Auto Scale Y Axis</string>
 
            </property>
 
            <property name="checked">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="3" column="0">
 
           <widget class="QLabel" name="lYmin">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="text">
 
             <string>Ymin</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="2" column="0">
 
           <widget class="QCheckBox" name="cbYMax">
 
           <widget class="QLabel" name="lYmax">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="text">
 
             <string>Ymax</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="2" column="1">
 
           <widget class="QDoubleSpinBox" name="spYmax">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="maximumSize">
 
             <size>
 
              <width>75</width>
 
              <height>16777215</height>
 
             </size>
 
            </property>
 
           </widget>
 
          </item>
 
         </layout>
 
        </item>
 
       </layout>
 
      </widget>
 
     </widget>
 
    </item>
 
   </layout>
 
  </widget>
 
  <widget class="QMenuBar" name="menuBar">
 
   <property name="geometry">
 
    <rect>
 
     <x>0</x>
 
     <y>0</y>
 
     <width>653</width>
 
     <height>20</height>
 
     <height>23</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuFile">
 
    <property name="title">
 
     <string>File</string>
 
    </property>
 
    <addaction name="actionFileLoadProfile"/>
 
    <addaction name="actionFileSaveProfile"/>
 
   </widget>
 
   <widget class="QMenu" name="menuHelp">
 
    <property name="title">
 
     <string>Help</string>
0 comments (0 inline, 0 general)