Changeset - 52f54b71dc6b
[Not reviewed]
snapshots
0 6 0
Hasan Yavuz ÖZDERYA - 10 years ago 2015-09-19 15:46:37
hy@ozderya.net
plot snapshots with color
6 files changed with 38 insertions and 35 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -29,12 +29,13 @@
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <snapshotview.h>
 
#include <plot.h>
 

	
 
#include "utils.h"
 
#include "version.h"
 
#include "floatswap.h"
 

	
 
#if defined(Q_OS_WIN) && defined(QT_STATIC)
 
@@ -154,13 +155,13 @@ MainWindow::MainWindow(QWidget *parent) 
 
    // init channel data and curve list
 
    for (unsigned int i = 0; i < numOfChannels; i++)
 
    {
 
        channelBuffers.append(new FrameBuffer(numOfSamples));
 
        curves.append(new QwtPlotCurve());
 
        curves[i]->setSamples(channelBuffers[i]);
 
        curves[i]->setPen(makeColor(i));
 
        curves[i]->setPen(Plot::makeColor(i));
 
        curves[i]->attach(ui->plot);
 
    }
 

	
 
    // init auto scale
 
    ui->plot->setAxis(ui->cbAutoScale->isChecked(),
 
                      ui->spYmin->value(), ui->spYmax->value());
 
@@ -468,13 +469,13 @@ void MainWindow::onNumOfChannelsChanged(
 
        // add new channels
 
        for (unsigned int i = 0; i < numOfChannels - oldNum; i++)
 
        {
 
            channelBuffers.append(new FrameBuffer(numOfSamples));
 
            curves.append(new QwtPlotCurve());
 
            curves.last()->setSamples(channelBuffers.last());
 
            curves.last()->setPen(makeColor(curves.length()-1));
 
            curves.last()->setPen(Plot::makeColor(curves.length()-1));
 
            curves.last()->attach(ui->plot);
 
        }
 
    }
 
    else if(numOfChannels < oldNum)
 
    {
 
        // remove channels
 
@@ -650,43 +651,12 @@ void MainWindow::enableDemo(bool enabled
 
        ui->actionDemoMode->setChecked(false);
 
        demoIndicator.hide();
 
        ui->plot->replot();
 
    }
 
}
 

	
 
/*
 
  Below crude drawing demostrates how color selection occurs for
 
  given channel index
 

	
 
  0°                     <--Hue Value-->                           360°
 
  |* . o . + . o . * . o . + . o . * . o . + . o . * . o . + . o . |
 

	
 
  * -> 0-3
 
  + -> 4-7
 
  o -> 8-15
 
  . -> 16-31
 

	
 
 */
 

	
 
QColor MainWindow::makeColor(unsigned int channelIndex)
 
{
 
    auto i = channelIndex;
 

	
 
    if (i < 4)
 
    {
 
        return QColor::fromHsv(360*i/4, 255, 230);
 
    }
 
    else
 
    {
 
        double p = floor(log2(i));
 
        double n = pow(2, p);
 
        i = i - n;
 
        return QColor::fromHsv(360*i/n + 360/pow(2,p+1), 255, 230);
 
    }
 
}
 

	
 
void MainWindow::onExportCsv()
 
{
 
    bool wasPaused = ui->actionPause->isChecked();
 
    ui->actionPause->setChecked(true); // pause plotting
 

	
 
    QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File"));
mainwindow.h
Show inline comments
 
@@ -106,14 +106,12 @@ private:
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 
    bool isDemoRunning();
 
    QwtPlotTextLabel demoIndicator;
 

	
 
    QColor makeColor(unsigned int channelIndex);
 

	
 
private slots:
 
    void onPortToggled(bool open);
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
plot.cpp
Show inline comments
 
@@ -107,6 +107,36 @@ void Plot::darkBackground(bool enabled)
 
        zoomer.setRubberBandPen(QPen(Qt::black));
 
        zoomer.setTrackerPen(QPen(Qt::black));
 
        sZoomer.setPickerPen(QPen(Qt::black));
 
    }
 
    replot();
 
}
 

	
 
/*
 
  Below crude drawing demostrates how color selection occurs for
 
  given channel index
 

	
 
  0°                     <--Hue Value-->                           360°
 
  |* . o . + . o . * . o . + . o . * . o . + . o . * . o . + . o . |
 

	
 
  * -> 0-3
 
  + -> 4-7
 
  o -> 8-15
 
  . -> 16-31
 

	
 
 */
 
QColor Plot::makeColor(unsigned int channelIndex)
 
{
 
    auto i = channelIndex;
 

	
 
    if (i < 4)
 
    {
 
        return QColor::fromHsv(360*i/4, 255, 230);
 
    }
 
    else
 
    {
 
        double p = floor(log2(i));
 
        double n = pow(2, p);
 
        i = i - n;
 
        return QColor::fromHsv(360*i/n + 360/pow(2,p+1), 255, 230);
 
    }
 
}
plot.h
Show inline comments
 
@@ -17,12 +17,13 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef PLOT_H
 
#define PLOT_H
 

	
 
#include <QColor>
 
#include <qwt_plot.h>
 
#include <qwt_plot_grid.h>
 
#include <qwt_plot_shapeitem.h>
 
#include "zoomer.h"
 
#include "scalezoomer.h"
 

	
 
@@ -31,12 +32,14 @@ class Plot : public QwtPlot
 
    Q_OBJECT
 

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

	
 
    static QColor makeColor(unsigned int channelIndex);
 

	
 
private:
 
    bool isAutoScaled;
 
    double yMin, yMax;
 
    Zoomer zoomer;
 
    ScaleZoomer sZoomer;
 
    QwtPlotGrid grid;
snapshotview.cpp
Show inline comments
 
@@ -11,12 +11,13 @@ SnapShotView::SnapShotView(QWidget *pare
 

	
 
    for (unsigned ci = 0; ci < numOfChannels; ci++)
 
    {
 
        QwtPlotCurve* curve = new QwtPlotCurve();
 
        curves.append(curve);
 
        curve->setSamples(snapShot->data[ci]);
 
        curve->setPen(Plot::makeColor(ci));
 
        curve->attach(ui->plot);
 
    }
 

	
 
    _snapShot = snapShot;
 
}
 

	
snapshotview.h
Show inline comments
 
#ifndef SNAPSHOTVIEW_H
 
#define SNAPSHOTVIEW_H
 

	
 
#include <QMainWindow>
 
#include <QVector>
 
#include <QPointF>
 
#include <QPen>
 
#include <qwt_plot_curve.h>
 
#include "plot.h"
 

	
 
class SnapShotView;
 
struct SnapShot
 
{
0 comments (0 inline, 0 general)