/
EditUserMetricDialog.cpp
313 lines (259 loc) · 10 KB
/
EditUserMetricDialog.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
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
/*
* Copyright (c) 2015 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 "UserMetricSettings.h"
#include "LTMTool.h" // formula edit
#include "Context.h"
#include "Athlete.h"
#include "MainWindow.h"
#include "Tab.h"
#include "RideNavigator.h"
#include "DataFilter.h"
#include "Zones.h"
#include "HrZones.h"
#include <QFont>
#include <QFontMetrics>
static bool insensitiveLessThan(const QString &a, const QString &b)
{
return a.toLower() < b.toLower();
}
// although we edit global user metrics we do so in the current
// context, using the current ride as a basis for the computation
// and refreshing it when the current ride changes etc.
EditUserMetricDialog::EditUserMetricDialog(QWidget *parent, Context *context, UserMetricSettings &here)
: QDialog(parent, Qt::Dialog), context(context), settings(here)
{
setWindowTitle(tr("User Defined Metric"));
setMinimumHeight(680 *dpiYFactor);
//HelpWhatsThis *help = new HelpWhatsThis(this);
//this->setWhatsThis(help->getWhatsThisText(HelpWhatsThis::ChartTrends_MetricTrends_Curves_Settings));
QVBoxLayout *mainLayout = new QVBoxLayout(this);
// Setup all the widgets
QLabel *symbolLabel = new QLabel(tr("Symbol"), this);
symbol = new QLineEdit(this);
symbol->setText(settings.symbol);
QLabel *nameLabel = new QLabel(tr("Name"), this);
name = new QLineEdit(this);
name->setText(settings.name);
QLabel *unitsMetricLabel = new QLabel(tr("Metric Units"), this);
unitsMetric = new QLineEdit(this);
unitsMetric->setText(settings.unitsMetric);
QLabel *unitsImperialLabel = new QLabel(tr("Imperial Units"), this);
unitsImperial = new QLineEdit(this);
unitsImperial->setText(settings.unitsImperial);
QLabel *descriptionLabel = new QLabel(tr("Description"), this);
description = new QTextEdit(this);
description->setText(settings.description);
QLabel *typeLabel = new QLabel(tr("Type"), this);
type = new QComboBox(this);
type->addItem(tr("Total"));
type->addItem(tr("Average"));
type->addItem(tr("Peak"));
type->addItem(tr("Low"));
type->setCurrentIndex(settings.type);
QLabel *conversionLabel = new QLabel(tr("Conversion Factor"), this);
conversion = new QDoubleSpinBox(this);
conversion->setDecimals(3);
conversion->setValue(settings.conversion);
QLabel *conversionSumLabel = new QLabel(tr("Conversion Sum"), this);
conversionSum = new QDoubleSpinBox(this);
conversionSum->setDecimals(3);
conversionSum->setValue(settings.conversionSum);
QLabel *precisionLabel = new QLabel(tr("Precision"), this);
precision = new QDoubleSpinBox(this);
precision->setDecimals(0);
precision->setValue(settings.precision);
aggzero = new QCheckBox(tr("Aggregate Zero"));
aggzero->setChecked(settings.aggzero);
istime = new QCheckBox(tr("Time"));
istime->setChecked(settings.istime);
QLabel *formulaEditLabel = new QLabel(tr("Program"), this);
formulaEdit = new DataFilterEdit(this, context);
QFont courier("Courier", QFont().pointSize());
QFontMetrics fm(courier);
formulaEdit->setFont(courier);
formulaEdit->setTabStopWidth(4 * fm.width(' ')); // 4 char tabstop
formulaEdit->setText(settings.program);
// get suitably formated list XXX XXX ffs, refactor this into FormulEdit !!! XXX XXX
QList<QString> list;
QString last;
SpecialFields sp;
// get sorted list
QStringList names = context->tab->rideNavigator()->logicalHeadings;
// start with just a list of functions
list = DataFilter::builtins();
// ridefile data series symbols
list += RideFile::symbols();
// add special functions (older code needs fixing !)
list << "config(cranklength)";
list << "config(cp)";
list << "config(ftp)";
list << "config(w')";
list << "config(pmax)";
list << "config(cv)";
list << "config(scv)";
list << "config(height)";
list << "config(weight)";
list << "config(lthr)";
list << "config(maxhr)";
list << "config(rhr)";
list << "config(units)";
list << "const(e)";
list << "const(pi)";
list << "daterange(start)";
list << "daterange(stop)";
list << "ctl";
list << "tsb";
list << "atl";
list << "sb(BikeStress)";
list << "lts(BikeStress)";
list << "sts(BikeStress)";
list << "rr(BikeStress)";
list << "tiz(power, 1)";
list << "tiz(hr, 1)";
list << "best(power, 3600)";
list << "best(hr, 3600)";
list << "best(cadence, 3600)";
list << "best(speed, 3600)";
list << "best(torque, 3600)";
list << "best(isopower, 3600)";
list << "best(xpower, 3600)";
list << "best(vam, 3600)";
list << "best(wpk, 3600)";
qSort(names.begin(), names.end(), insensitiveLessThan);
foreach(QString name, names) {
// handle dups
if (last == name) continue;
last = name;
// Handle bikescore tm
if (name.startsWith("BikeScore")) name = QString("BikeScore");
// Always use the "internalNames" in Filter expressions
name = sp.internalName(name);
// we do very little to the name, just space to _ and lower case it for now...
name.replace(' ', '_');
list << name;
}
// set new list
// create an empty completer, configchanged will fix it
DataFilterCompleter *completer = new DataFilterCompleter(list, this);
formulaEdit->setCompleter(completer);
QLabel *eval = new QLabel(tr("Evaluates"), this);
QLabel *metric = new QLabel(tr("Metric"), this);
QLabel *imperial = new QLabel(tr("Imperial"), this);
//QLabel *elapsed = new QLabel(tr("Elapsed"), this);
mValue = new QLabel("", this);
iValue = new QLabel("", this);
elapsed = new QLabel("", this);
okButton = new QPushButton(tr("OK"));
cancelButton = new QPushButton(tr("Cancel"));
test = new QPushButton(tr("Test"));
QGridLayout *head = new QGridLayout;
// first row; symbol and name
head->addWidget(symbolLabel, 0,0);
head->addWidget(symbol, 0,1);
head->addWidget(nameLabel, 0,2);
head->addWidget(name, 0,3,1,2);
// second row; type and top of description
head->addWidget(typeLabel, 1,0);
head->addWidget(type, 1,1);
head->addWidget(descriptionLabel, 1,2);
head->addWidget(description, 1,3,2,3);
// third row; precision and middle of description
head->addWidget(precisionLabel, 2,0);
head->addWidget(precision, 2,1);
// fourth row; aggzero and istime and bottom of description
head->addWidget(istime, 3,1);
head->addWidget(aggzero, 3,2);
// fifth row; labels for units etc
head->addWidget(unitsMetricLabel, 4,0);
head->addWidget(unitsImperialLabel, 4,1);
head->addWidget(conversionLabel, 4,3);
head->addWidget(conversionSumLabel, 4,4);
// sixth row; entry for units etc
head->addWidget(unitsMetric, 5,0);
head->addWidget(unitsImperial, 5,1);
head->addWidget(conversion, 5,3);
head->addWidget(conversionSum, 5,4);
// seventh row; Code label
head->addWidget(formulaEditLabel, 6,0);
// 8 - 16 row; program editor
head->addWidget(formulaEdit, 7,0, 8, 5);
// 17th row; labels for estimates
head->addWidget(test, 16, 0);
head->addWidget(metric, 16, 1);
head->addWidget(imperial, 16, 2);
// 18th row; eval values and pushbuttons
head->addWidget(eval, 17,0);
head->addWidget(mValue, 17,1);
head->addWidget(iValue, 17,2);
head->addWidget(cancelButton, 17,3);
head->addWidget(okButton, 17,4);
head->setSpacing(5 *dpiXFactor);
head->setColumnStretch(0, 15);
head->setColumnStretch(1, 20);
head->setColumnStretch(2, 15);
head->setColumnStretch(3, 20);
head->setColumnStretch(4, 20);
mainLayout->addLayout(head);
connect(test, SIGNAL(clicked()), this, SLOT(refreshStats()));
connect(context, SIGNAL(rideSelected(RideItem*)), this, SLOT(refreshStats()));
connect (cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect (okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
}
void
EditUserMetricDialog::setSettings(UserMetricSettings &here)
{
// set settings from editing widgets
// Get current values
here.symbol = symbol->text();
here.name = name->text();
here.description = description->document()->toPlainText();
here.unitsMetric = unitsMetric->text();
here.unitsImperial = unitsImperial->text();
here.type = type->currentIndex();
here.conversion = conversion->value();
here.conversionSum = conversionSum->value();
here.aggzero = aggzero->isChecked();
here.istime = istime->isChecked();
here.precision = precision->value();
here.program = formulaEdit->document()->toPlainText();
here.fingerprint = here.symbol + DataFilter::fingerprint(here.program);
}
void
EditUserMetricDialog::okClicked()
{
// fetch current state
setSettings(settings);
// ok to accept these
accept();
}
void
EditUserMetricDialog::refreshStats()
{
// test against currently selected ride
if (context->rideItem() == NULL || context->rideItem()->ride() == NULL) return;
// get current state
UserMetricSettings here;
setSettings(here);
// Build a metric
UserMetric test(context, here);
// no spec and no deps, pass empty on stack
test.compute(context->rideItem(), Specification(), QHash<QString,RideMetric*>());
// get the value out
mValue->setText(test.toString(true));
iValue->setText(test.toString(false));
}