forked from JorgeAparicio/qSerialTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.cpp
150 lines (118 loc) · 3.63 KB
/
plot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 2012 Jorge Aparicio <jorge.aparicio.r@gmail.com>
*
* This file is part of qSerialTerm.
*
* qSerialTerm 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
* any later version.
* qSerialTerm 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 qSerialTerm. If not, see <http://www.gnu.org/licenses/>.
*/
#include "plot.h"
#include "ui_plot.h"
#include "circularbuffer.h"
#include "qwt_plot_curve.h"
#include "qwt_plot_marker.h"
#include "qwt_plot_grid.h"
Q_DECLARE_METATYPE(Qt::GlobalColor)
Plot::Plot(size_t numberOfSamples, qreal minY, qreal maxY, QWidget *parent) :
QWidget(parent),
ui(new Ui::Plot)
{
ui->setupUi(this);
circularBuffer = new CircularBuffer(numberOfSamples,
ui->minYSpinBox->value(),
ui->maxYSpinBox->value());
ui->minYSpinBox->setValue(minY);
ui->maxYSpinBox->setValue(maxY);
curve = new QwtPlotCurve();
curve->setData(circularBuffer);
curve->attach(ui->plot);
ui->colorComboBox->addItem("Black", Qt::black);
ui->colorComboBox->addItem("Dark gray", Qt::darkGray);
ui->colorComboBox->addItem("Gray", Qt::gray);
ui->colorComboBox->addItem("Light gray", Qt::lightGray);
ui->colorComboBox->addItem("Red", Qt::red);
ui->colorComboBox->addItem("Green", Qt::green);
ui->colorComboBox->addItem("Blue", Qt::blue);
ui->colorComboBox->addItem("Cyan", Qt::cyan);
ui->colorComboBox->addItem("Magenta", Qt::magenta);
ui->colorComboBox->addItem("Yellow", Qt::yellow);
ui->colorComboBox->addItem("Dark red", Qt::darkRed);
ui->colorComboBox->addItem("Dark green", Qt::darkGreen);
ui->colorComboBox->addItem("Dark blue", Qt::darkBlue);
ui->colorComboBox->addItem("Dark cyan", Qt::darkCyan);
ui->colorComboBox->addItem("Dark magenta", Qt::darkMagenta);
ui->colorComboBox->addItem("Dark yellow", Qt::darkYellow);
ui->colorComboBox->setCurrentIndex(qrand() % ui->colorComboBox->count());
}
Plot::~Plot()
{
delete ui;
}
void Plot::setId(QString const& str)
{
ui->idLineEdit->setText(str);
}
void Plot::setNumberOfSamples(size_t size)
{
circularBuffer->resize(size);
}
void Plot::pushBack(qreal sample)
{
circularBuffer->pushBack(sample);
}
void Plot::replot()
{
ui->plot->replot();
}
void Plot::on_maxYSpinBox_valueChanged(int value)
{
ui->minYSpinBox->setMaximum(value - 1);
circularBuffer->setMaxY(value);
ui->plot->replot();
}
void Plot::on_minYSpinBox_valueChanged(int value)
{
ui->maxYSpinBox->setMinimum(value + 1);
circularBuffer->setMinY(value);
ui->plot->replot();
}
void Plot::on_xAxisCheckBox_toggled(bool checked)
{
if (checked) {
xAxis = new QwtPlotMarker;
xAxis->setLineStyle(QwtPlotMarker::HLine);
xAxis->setYValue(0);
xAxis->attach(ui->plot);
ui->plot->replot();
} else {
delete xAxis;
xAxis = 0;
ui->plot->replot();
}
}
void Plot::on_colorComboBox_currentIndexChanged(int index)
{
curve->setPen(QPen(ui->colorComboBox->itemData(index).value<QColor>()));
ui->plot->replot();
}
void Plot::on_gridCheckBox_toggled(bool checked)
{
if (checked) {
grid = new QwtPlotGrid;
grid->setMajPen(QPen(Qt::black, 0, Qt::DashLine));
grid->attach(ui->plot);
ui->plot->replot();
} else {
delete grid;
grid = 0;
ui->plot->replot();
}
}