Changeset - 70f83253cf6c
[Not reviewed]
default
0 2 0
Hasan Yavuz ÖZDERYA - 11 years ago 2015-04-28 16:19:06
hy@ozderya.net
implemented automatic coloring of different channels with auto color selection
2 files changed with 37 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -21,12 +21,13 @@
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QApplication>
 
#include <QtDebug>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include "utils.h"
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow)
 
{
 
@@ -164,12 +165,13 @@ MainWindow::MainWindow(QWidget *parent) 
 
    // init channel data and curve list
 
    for (int i = 0; i < numOfChannels; i++)
 
    {
 
        channelsData.append(DataArray(numOfSamples, 0.0));
 
        curves.append(new QwtPlotCurve());
 
        curves[i]->setSamples(dataX, channelsData[i]);
 
        curves[i]->setPen(makeColor(i));
 
        curves[i]->attach(ui->plot);
 
    }
 

	
 
    // init number format
 
    if (numberFormatButtons.checkedId() >= 0)
 
    {
 
@@ -525,12 +527,13 @@ void MainWindow::onNumOfChannelsChanged(
 
        // add new channels
 
        for (int i = 0; i < numOfChannels - oldNum; i++)
 
        {
 
            channelsData.append(DataArray(numOfSamples, 0.0));
 
            curves.append(new QwtPlotCurve());
 
            curves.last()->setSamples(dataX, channelsData.last());
 
            curves.last()->setPen(makeColor(curves.length()-1));
 
            curves.last()->attach(ui->plot);
 
        }
 
    }
 
    else if(numOfChannels < oldNum)
 
    {
 
        // remove channels
 
@@ -652,6 +655,37 @@ void MainWindow::enableDemo(bool enabled
 
    else
 
    {
 
        demoTimer.stop();
 
        ui->actionDemoMode->setChecked(false);
 
    }
 
}
 

	
 
/*
 
  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, 255);
 
    }
 
    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, 255);
 
    }
 
}
mainwindow.h
Show inline comments
 
@@ -25,12 +25,13 @@
 
#include <QString>
 
#include <QVector>
 
#include <QList>
 
#include <QSerialPort>
 
#include <QSignalMapper>
 
#include <QTimer>
 
#include <QColor>
 
#include <qwt_plot_curve.h>
 

	
 
#include "ui_about_dialog.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
@@ -89,12 +90,14 @@ private:
 

	
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 
    bool isDemoRunning();
 

	
 
    QColor makeColor(unsigned int channelIndex);
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
0 comments (0 inline, 0 general)