/
GenericChart.cpp
284 lines (236 loc) · 9.29 KB
/
GenericChart.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
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
/*
* Copyright (c) 2020 Mark Liversedge (liversedge@gmail.com)
*
* This program 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 2 of the License, or (at your option)
* any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "GenericChart.h"
#include "Colors.h"
#include "TabView.h"
#include "RideFileCommand.h"
#include "Utils.h"
#include <limits>
#include <QScrollArea>
//
// The generic chart manages collections of genericplots
// if the stack option is selected we create a plot for
// each data series.
//
// If series do not have a common x-axis we create a
// separate plot, with series that share a common x-axis
// sharing the same plot
//
// The layout is horizontal or vertical in which case the
// generic chart will use vbox or hbox layouts. XXX TODO
//
// Charts have a minimum width and height that needs to be
// honoured too, since multiple series stacked can get
// cramped, so these are placed into a scroll area
//
GenericChart::GenericChart(QWidget *parent, Context *context) : QWidget(parent), context(context)
{
// for scrollarea, since we see a little of it.
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CRIDEPLOTBACKGROUND)));
// main layout for widget
QVBoxLayout *main=new QVBoxLayout(this);
main->setSpacing(0);
main->setContentsMargins(0,0,0,0);
// main layout used for charts
mainLayout = new QVBoxLayout();
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0,0,0,0);
lrLayout = new QHBoxLayout();
mainLayout->addLayout(lrLayout);
// stack for scrolling
QWidget *stackWidget = new QWidget(this);
stackWidget->setAutoFillBackground(false);
stackWidget->setLayout(mainLayout);
stackWidget->setPalette(palette);
// put everything inside a scrollarea
stackFrame = new QScrollArea(this);
#ifdef Q_OS_WIN
QStyle *cde = QStyleFactory::create(OS_STYLE);
stackFrame->setStyle(cde);
#endif
stackFrame->setAutoFillBackground(false);
stackFrame->setWidgetResizable(true);
stackFrame->setFrameStyle(QFrame::NoFrame);
stackFrame->setContentsMargins(0,0,0,0);
stackFrame->setPalette(palette);
main->addWidget(stackFrame);
stackFrame->setWidget(stackWidget);
// watch for color/themes change
connect(context, SIGNAL(configChanged(qint32)), this, SLOT(configChanged(qint32)));
configChanged(0);
}
void
GenericChart::configChanged(qint32)
{
setUpdatesEnabled(false);
setProperty("color", GColor(CPLOTBACKGROUND));
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CRIDEPLOTBACKGROUND)));
setPalette(palette); // propagates to children
// set style sheets
#ifndef Q_OS_MAC
stackFrame->setStyleSheet(TabView::ourStyleSheet());
#endif
setUpdatesEnabled(true);
}
// set chart settings
bool
GenericChart::initialiseChart(QString title, int type, bool animate, int legendpos, bool stack, int orientation)
{
// Remember the settings, we use them for every new plot
this->title = title;
this->type = type;
this->animate = animate;
this->legendpos = legendpos;
this->stack = stack;
this->orientation = orientation;
// store info as its passed to us
newSeries.clear();
newAxes.clear();
return true;
}
// add a curve, associating an axis
bool
GenericChart::addCurve(QString name, QVector<double> xseries, QVector<double> yseries, QString xname, QString yname,
QStringList labels, QStringList colors,
int line, int symbol, int size, QString color, int opacity, bool opengl)
{
newSeries << GenericSeriesInfo(name, xseries, yseries, xname, yname, labels, colors, line, symbol, size, color, opacity, opengl);
return true;
}
// configure axis, after curves added
bool
GenericChart::configureAxis(QString name, bool visible, int align, double min, double max,
int type, QString labelcolor, QString color, bool log, QStringList categories)
{
newAxes << GenericAxisInfo(name, visible, align, min, max, type, labelcolor, color, log, categories);
return true;
}
// post processing clean up / add decorations / helpers etc
void
GenericChart::finaliseChart()
{
setUpdatesEnabled(false); // lets not open our kimono here
// ok, so lets work out what we need, run through the curves
// and allocate to a plot, where we have separate plots if
// we have stacked set -or- for each xaxis, remembering to
// add the axisinfo each time too.
QList<GenericPlotInfo> newPlots;
QStringList xaxes;
for(int i=0; i<newSeries.count(); i++) {
if (stack) {
// super easy we just have a new plot for each series
GenericPlotInfo add(newSeries[i].xname);
add.series << newSeries[i];
// xaxis info
int ax=GenericAxisInfo::findAxis(newAxes, newSeries[i].xname);
if (ax>=0) add.axes << newAxes[ax];
// yaxis info
ax=GenericAxisInfo::findAxis(newAxes, newSeries[i].yname);
if (ax>=0) add.axes << newAxes[ax];
// add to list
newPlots << add;
xaxes << newSeries[i].xname;
} else {
// otherwise add all series to the same plot
// with a common x axis
int index = xaxes.indexOf(newSeries[i].xname);
if (index >=0) {
// woop, we already got this xaxis
newPlots[index].series << newSeries[i];
// yaxis info
int ax=GenericAxisInfo::findAxis(newAxes, newSeries[i].yname);
if (ax>=0) newPlots[index].axes << newAxes[ax];
} else {
GenericPlotInfo add(newSeries[i].xname);
add.series << newSeries[i];
// xaxis info
int ax=GenericAxisInfo::findAxis(newAxes, newSeries[i].xname);
if (ax>=0) add.axes << newAxes[ax];
// yaxis info
ax=GenericAxisInfo::findAxis(newAxes, newSeries[i].yname);
if (ax>=0) add.axes << newAxes[ax];
// add to list
newPlots << add;
xaxes << newSeries[i].xname;
}
}
}
// lets find existing plots or create new ones
// first we mark all existing plots as deleteme
for(int i=0; i<currentPlots.count(); i++) currentPlots[i].state = GenericPlotInfo::deleteme;
// source and target layouts
QBoxLayout *source, *target;
if (orientation == Qt::Vertical) {
source = lrLayout;
target = mainLayout;
} else {
source = mainLayout;
target = lrLayout;
}
// match what we want with what we got
for(int i=0; i<newPlots.count(); i++) {
int index = GenericPlotInfo::findPlot(currentPlots, newPlots[i]);
if (index <0) {
// new one required
newPlots[i].plot = new GenericPlot(this, context);
target->addWidget(newPlots[i].plot);
target->setStretchFactor(newPlots[i].plot, 10);// make them all the same
} else {
newPlots[i].plot = currentPlots[index].plot; // reuse
currentPlots[index].state = GenericPlotInfo::matched; // don't deleteme !
// make sure its in the right layout (might remove and add back to the same layout!)
source->removeWidget(newPlots[i].plot);
target->addWidget(newPlots[i].plot);
target->setStretchFactor(newPlots[i].plot, 10);// make them all the same
}
}
// delete all the deleteme plots
for(int i=0; i<currentPlots.count(); i++) {
if (currentPlots[i].state == GenericPlotInfo::deleteme) {
delete currentPlots[i].plot; // will remove from layout too
}
}
// now initialise all the newPlots
for(int i=0; i<newPlots.count(); i++) {
// set initial parameters
newPlots[i].plot->initialiseChart(title, type, animate, legendpos);
// add curves
QListIterator<GenericSeriesInfo>s(newPlots[i].series);
while(s.hasNext()) {
GenericSeriesInfo p=s.next();
newPlots[i].plot->addCurve(p.name, p.xseries, p.yseries, p.xname, p.yname, p.labels, p.colors, p.line, p.symbol, p.size, p.color, p.opacity, p.opengl);
}
// set axis
QListIterator<GenericAxisInfo>a(newPlots[i].axes);
while(a.hasNext()) {
GenericAxisInfo p=a.next();
newPlots[i].plot->configureAxis(p.name, p.visible, p.align,p.minx, p.maxx, p.type, p.labelcolorstring, p.axiscolorstring, p.log, p.categories);
}
newPlots[i].plot->finaliseChart();
newPlots[i].state = GenericPlotInfo::active;
}
// set current and wipe rest
currentPlots=newPlots;
// and zap the state
newAxes.clear();
newSeries.clear();
// and display
setUpdatesEnabled(true);
}