This repository was archived by the owner on Sep 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathplotselectionwidget.cpp
More file actions
379 lines (316 loc) · 12.8 KB
/
plotselectionwidget.cpp
File metadata and controls
379 lines (316 loc) · 12.8 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/// @author Shannon Fenn
#include "plotselectionwidget.h"
// Qt Includes
#include <QMdiArea>
#include <QComboBox>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QSlider>
#include <QSpinBox>
#include <QMdiSubWindow>
#include <QPixmap>
#include <QPushButton>
#include <QSignalMapper>
#include <QPainter>
#include <QToolButton>
#include <QColorDialog>
#include <qwt/qwt_symbol.h>
#include <qwt/qwt_plot_curve.h>
#include <boost/foreach.hpp>
#include <typeinfo>
#include "GLDisplay.h"
#include <limits>
PlotSelectionWidget::PlotSelectionWidget(QMdiArea* parentMdiWidget, QWidget *parent):
QWidget(parent),
mdiWidget(parentMdiWidget),
selectedSymbol(QwtSymbol::NoSymbol)
{
newSymbolDialog = new CreateQwtSymbolDialog;
newSymbolDialog->hide();
setObjectName(tr("Plot Selection"));
setWindowTitle(tr("Plot Selection"));
createWidgets();
createLayout();
createConnections();
this->setEnabled(false); // Dafault to disabled, until a valid window is selected.
currentDisplay = 0;
}
PlotSelectionWidget::~PlotSelectionWidget()
{
delete curveSelectionlayout;
delete symbolSelectionLayout;
delete styleSelectionLayout;
delete axisLimitsLayout;
delete overallLayout;
delete curveComboBox;
delete curveVisibilityCheckBox;
delete symbolLabel;
delete styleLabel;
delete styleCombo;
delete symbolButton;
delete styleColourButton;
delete newSymbolDialog;
}
void PlotSelectionWidget::createWidgets()
{
curveComboBox = new QComboBox();
// Checkboxes
curveVisibilityCheckBox = new QCheckBox("Visible");
symbolLabel = new QLabel("Symbol");
symbolButton = new QToolButton();
styleLabel = new QLabel("Style");
styleCombo = new QComboBox();
styleColourButton = new QToolButton();
for(int i=0; i<5; i++) {
styleCombo->addItem(getStyleName(getStyleFromInt(i)));
}
autoScaleBox = new QCheckBox();
autoScaleBox->setText("Automatic scale");
xMinSpin = new QDoubleSpinBox();
xMaxSpin = new QDoubleSpinBox();
yMinSpin = new QDoubleSpinBox();
yMaxSpin = new QDoubleSpinBox();
autoScaleBox->setChecked(true);
xMinSpin->setRange(-std::numeric_limits<double>::max(), 250);
xMaxSpin->setRange(-250, std::numeric_limits<double>::max());
yMinSpin->setRange(-std::numeric_limits<double>::max(), 250);
yMaxSpin->setRange(-250, std::numeric_limits<double>::max());
xMinSpin->setValue(-250);
yMinSpin->setValue(-250);
xMaxSpin->setValue(250);
yMaxSpin->setValue(250);
xMinSpin->setEnabled(false);
yMinSpin->setEnabled(false);
xMaxSpin->setEnabled(false);
yMaxSpin->setEnabled(false);
}
void PlotSelectionWidget::createLayout()
{
// Setup layer toggle controls
curveSelectionlayout = new QHBoxLayout();
curveSelectionlayout->addWidget(curveVisibilityCheckBox,0,Qt::AlignHCenter);
curveSelectionlayout->addStretch(1);
// Setup symbol selection row
symbolSelectionLayout = new QHBoxLayout();
symbolSelectionLayout->addWidget(symbolLabel,0,Qt::AlignHCenter);
symbolSelectionLayout->addWidget(symbolButton,0,Qt::AlignHCenter);
symbolSelectionLayout->addStretch(1);
// Setup style selection row
styleSelectionLayout = new QHBoxLayout();
styleSelectionLayout->addWidget(styleLabel,0,Qt::AlignHCenter);
styleSelectionLayout->addWidget(styleCombo,0,Qt::AlignHCenter);
styleSelectionLayout->addWidget(styleColourButton,0,Qt::AlignHCenter);
styleSelectionLayout->addStretch(1);
axisLimitsLayout = new QGridLayout();
axisLimitsLayout->addWidget(xMinSpin, 0, 0);
axisLimitsLayout->addWidget(xMaxSpin, 0, 1);
axisLimitsLayout->addWidget(yMinSpin, 1, 0);
axisLimitsLayout->addWidget(yMaxSpin, 1, 1);
// Setup overall layout
overallLayout = new QVBoxLayout();
overallLayout->addWidget(curveComboBox);
overallLayout->addLayout(curveSelectionlayout);
overallLayout->addLayout(symbolSelectionLayout);
overallLayout->addLayout(styleSelectionLayout);
overallLayout->addWidget(autoScaleBox);
overallLayout->addLayout(axisLimitsLayout);
setLayout(overallLayout);
}
void PlotSelectionWidget::createConnections()
{
// Setup signal to trigger a reload of current settings when a layer is selected.
connect(curveComboBox,SIGNAL(activated(int)),this,SLOT(updateSelectedCurveSettings()));
// Setup signal to notify when the currently selected window has changed.
connect(mdiWidget,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(focusWindowChanged(QMdiSubWindow*)));
// Signals for layer primary and enabled selections
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),this,SLOT(displaySettingsChanged()));
// Signals to disable/enable elements when enabled layer is toggled.
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),symbolLabel,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),symbolButton,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),styleLabel,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),styleCombo,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),styleColourButton,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),xMinSpin,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),xMaxSpin,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),yMinSpin,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),yMaxSpin,SLOT(setEnabled(bool)));
connect(curveVisibilityCheckBox,SIGNAL(toggled(bool)),autoScaleBox,SLOT(setEnabled(bool)));
// Setup symbol and style selection signals
connect(symbolButton, SIGNAL(clicked()), this, SLOT(selectSymbolClicked()));
connect(styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(displaySettingsChanged()));
connect(styleColourButton, SIGNAL(clicked()), this, SLOT(selectStyleColourClicked()));
connect(newSymbolDialog, SIGNAL(newSymbolCreated(QwtSymbol)), this, SLOT(setSymbol(QwtSymbol)));
connect(autoScaleBox, SIGNAL(toggled(bool)), this, SLOT(axisUpdated()));
connect(xMinSpin, SIGNAL(valueChanged(double)), this, SLOT(axisUpdated()));
connect(xMaxSpin, SIGNAL(valueChanged(double)), this, SLOT(axisUpdated()));
connect(yMinSpin, SIGNAL(valueChanged(double)), this, SLOT(axisUpdated()));
connect(yMaxSpin, SIGNAL(valueChanged(double)), this, SLOT(axisUpdated()));
}
void PlotSelectionWidget::setStyleColour(const QColor &newColour)
{
if(newColour.isValid())
{
selectedStyleColour = newColour;
displaySettingsChanged();
}
}
void PlotSelectionWidget::setStyleColour(const QString& newColourName)
{
setStyleColour(QColor(newColourName));
}
void PlotSelectionWidget::selectSymbolClicked()
{
newSymbolDialog->show();
newSymbolDialog->setFocus();
}
void PlotSelectionWidget::selectStyleColourClicked()
{
setStyleColour(QColorDialog::getColor(selectedStyleColour,this));
}
void PlotSelectionWidget::setSymbol(QwtSymbol symbol)
{
selectedSymbol = symbol;
displaySettingsChanged();
}
void PlotSelectionWidget::axisUpdated()
{
xMinSpin->setMaximum(xMaxSpin->value());
xMaxSpin->setMinimum(xMinSpin->value());
yMinSpin->setMaximum(yMaxSpin->value());
yMaxSpin->setMinimum(yMinSpin->value());
xMinSpin->setEnabled(!autoScaleBox->isChecked());
xMaxSpin->setEnabled(!autoScaleBox->isChecked());
yMinSpin->setEnabled(!autoScaleBox->isChecked());
yMaxSpin->setEnabled(!autoScaleBox->isChecked());
if(currentDisplay) {
if(autoScaleBox->isChecked()) {
currentDisplay->setAxisAutoScale(QwtPlot::xBottom, true);
currentDisplay->setAxisAutoScale(QwtPlot::yLeft, true);
}
else {
currentDisplay->setAxisScale(QwtPlot::xBottom, xMinSpin->value(), xMaxSpin->value());
currentDisplay->setAxisScale(QwtPlot::yLeft, yMinSpin->value(), yMaxSpin->value());
}
}
}
void PlotSelectionWidget::curveNamesUpdated(vector<QString> curveNames)
{
QString cur = curveComboBox->currentText(); //using text handles the potential for a name to be removed
curveComboBox->clear();
BOOST_FOREACH(QString& s, curveNames) {
if(curveComboBox->findText(s) == -1)
curveComboBox->addItem(s);
}
curveComboBox->setCurrentIndex(curveComboBox->findText(cur));
}
void PlotSelectionWidget::setSelectedCurve(QString newCurveName)
{
curveComboBox->setCurrentIndex(curveComboBox->findText(newCurveName));
}
void PlotSelectionWidget::focusWindowChanged(QMdiSubWindow* focusWindow)
{
if(focusWindow) {
static QMdiSubWindow* lastWindow = NULL;
if(lastWindow != focusWindow) {
QWidget* widget = focusWindow->widget();
bool correctWindowType = (typeid(*widget) == typeid(PlotDisplay));
// Enable if the selected window is the right type, disable otherwise.
this->setEnabled(correctWindowType);
currentDisplay = NULL;
if(correctWindowType)
{
// Assign to the current display
currentDisplay = (PlotDisplay*)widget;
setSelectedCurve(selectedCurveName);
// Get the current settings for the selected layers from the new window.
updateSelectedCurveSettings();
}
lastWindow = focusWindow;
}
}
}
void PlotSelectionWidget::updateSelectedCurveSettings()
{
selectedCurveName = curveComboBox->currentText();
if(currentDisplay) {
if(currentDisplay->nameExists(selectedCurveName)) {
curveVisibilityCheckBox->setEnabled(true);
// get values from curve
bool visible = currentDisplay->isCurveVisible(selectedCurveName);
selectedSymbol = currentDisplay->getSymbol(selectedCurveName);
selectedStyle = currentDisplay->getLineStyle(selectedCurveName);
selectedStyleColour = currentDisplay->getLineColour(selectedCurveName);
// Update controls with stored values.
styleCombo->setCurrentIndex(styleCombo->findText(getStyleName(selectedStyle)));
updateButtonSymbol(symbolButton, selectedSymbol);
updateButtonColour(styleColourButton, selectedStyleColour);
curveVisibilityCheckBox->setChecked(visible);
displaySettingsChanged();
}
else {
curveVisibilityCheckBox->setChecked(true);
curveVisibilityCheckBox->toggle();
curveVisibilityCheckBox->setEnabled(false);
}
}
}
void PlotSelectionWidget::displaySettingsChanged()
{
if(currentDisplay) {
if(currentDisplay->nameExists(selectedCurveName)) {
updateButtonSymbol(symbolButton, selectedSymbol);
updateButtonColour(styleColourButton, selectedStyleColour);
//can add alpha later if required
//colour.setAlpha(alphaSlider->value());
currentDisplay->setCurveVisibility(selectedCurveName, curveVisibilityCheckBox->isChecked());
selectedStyle = getStyleFromInt(styleCombo->currentIndex());
//assumes curve is already attached to plot (should be)
currentDisplay->updateCurveProperties(selectedCurveName, selectedSymbol, selectedStyle, selectedStyleColour);
}
}
}
void PlotSelectionWidget::updateButtonColour(QToolButton *button, QColor colour)
{
QPixmap tempPixmap(22,22);
QPainter painter(&tempPixmap);
tempPixmap.fill(colour);
painter.drawRect(0,0,tempPixmap.width(),tempPixmap.height());
button->setIcon(QIcon(tempPixmap));
button->setAutoRaise(true);
}
void PlotSelectionWidget::updateButtonSymbol(QToolButton *button, const QwtSymbol& symbol)
{
if(button != NULL) {
QPixmap tempPixmap(22,22);
QPainter painter(&tempPixmap);
tempPixmap.fill(Qt::lightGray);
painter.drawRect(0,0,tempPixmap.width(),tempPixmap.height());
symbol.drawSymbol(&painter, QPointF(tempPixmap.width()*0.5,tempPixmap.height()*0.5));
QIcon ButtonIcon(tempPixmap);
button->setIcon(ButtonIcon);
button->setAutoRaise(true);
}
}
QwtPlotCurve::CurveStyle PlotSelectionWidget::getStyleFromInt(int i)
{
switch(i) {
case 1: return QwtPlotCurve::Lines;
case 2: return QwtPlotCurve::Sticks;
case 3: return QwtPlotCurve::Steps;
case 4: return QwtPlotCurve::Dots;
default: return QwtPlotCurve::NoCurve;
}
}
QString PlotSelectionWidget::getStyleName(QwtPlotCurve::CurveStyle id)
{
switch(id) {
case QwtPlotCurve::NoCurve: return "No Curve";
case QwtPlotCurve::Lines: return "Lines";
case QwtPlotCurve::Sticks: return "Sticks";
case QwtPlotCurve::Steps: return "Steps";
case QwtPlotCurve::Dots: return "Dots";
default: return "Invalid";
}
}