-
Notifications
You must be signed in to change notification settings - Fork 446
/
LTMWindow.cpp
1377 lines (1130 loc) · 42.4 KB
/
LTMWindow.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010 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 "LTMWindow.h"
#include "LTMTool.h"
#include "LTMPlot.h"
#include "LTMSettings.h"
#include "LTMChartParser.h"
#include "TabView.h"
#include "Context.h"
#include "Context.h"
#include "Athlete.h"
#include "RideCache.h"
#include "RideFileCache.h"
#include "Settings.h"
#include "cmath"
#include "Units.h" // for MILES_PER_KM
#include "HelpWhatsThis.h"
#ifdef NOWEBKIT
#include <QWebEngineSettings>
#include <QDesktopWidget>
#endif
#include <QtGui>
#include <QString>
#include <QDebug>
#include <QStyle>
#include <QStyleFactory>
// span slider specials
#include <qxtspanslider.h>
#include <QStyleFactory>
#include <QStyle>
#include <QScrollBar>
#include <qwt_plot_panner.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_picker.h>
#include <qwt_plot_marker.h>
LTMWindow::LTMWindow(Context *context) :
GcChartWindow(context), context(context), dirty(true), stackDirty(true), compareDirty(true)
{
useToToday = useCustom = false;
plotted = DateRange(QDate(01,01,01), QDate(01,01,01));
lastRefresh = QTime::currentTime().addSecs(-10);
// the plot
QVBoxLayout *mainLayout = new QVBoxLayout;
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CTRENDPLOTBACKGROUND)));
// single plot
plotWidget = new QWidget(this);
plotWidget->setPalette(palette);
QVBoxLayout *plotLayout = new QVBoxLayout(plotWidget);
plotLayout->setSpacing(0);
plotLayout->setContentsMargins(0,0,0,0);
ltmPlot = new LTMPlot(this, context, true);
spanSlider = new QxtSpanSlider(Qt::Horizontal, this);
spanSlider->setFocusPolicy(Qt::NoFocus);
spanSlider->setHandleMovementMode(QxtSpanSlider::NoOverlapping);
spanSlider->setLowerValue(0);
spanSlider->setUpperValue(15);
QFont smallFont;
smallFont.setPointSize(6);
scrollLeft = new QPushButton("<", this);
scrollLeft->setFont(smallFont);
scrollLeft->setAutoRepeat(true);
scrollLeft->setFixedHeight(16);
scrollLeft->setFixedWidth(16);
scrollLeft->setContentsMargins(0,0,0,0);
scrollRight = new QPushButton(">", this);
scrollRight->setFont(smallFont);
scrollRight->setAutoRepeat(true);
scrollRight->setFixedHeight(16);
scrollRight->setFixedWidth(16);
scrollRight->setContentsMargins(0,0,0,0);
QHBoxLayout *span = new QHBoxLayout;
span->addWidget(scrollLeft);
span->addWidget(spanSlider);
span->addWidget(scrollRight);
plotLayout->addWidget(ltmPlot);
plotLayout->addLayout(span);
#ifdef Q_OS_MAC
// BUG in QMacStyle and painting of spanSlider
// so we use a plain style to avoid it, but only
// on a MAC, since win and linux are fine
#if QT_VERSION > 0x5000
QStyle *style = QStyleFactory::create("fusion");
#else
QStyle *style = QStyleFactory::create("Cleanlooks");
#endif
spanSlider->setStyle(style);
scrollLeft->setStyle(style);
scrollRight->setStyle(style);
#endif
// the stack of plots
plotsWidget = new QWidget(this);
plotsWidget->setPalette(palette);
plotsLayout = new QVBoxLayout(plotsWidget);
plotsLayout->setSpacing(0);
plotsLayout->setContentsMargins(0,0,0,0);
plotArea = new QScrollArea(this);
#ifdef Q_OS_WIN
QStyle *cde = QStyleFactory::create(OS_STYLE);
plotArea->setStyle(cde);
#endif
plotArea->setAutoFillBackground(false);
plotArea->setWidgetResizable(true);
plotArea->setWidget(plotsWidget);
plotArea->setFrameStyle(QFrame::NoFrame);
plotArea->setContentsMargins(0,0,0,0);
plotArea->setPalette(palette);
// the data table
QFont defaultFont; // mainwindow sets up the defaults.. we need to apply
#ifdef NOWEBKIT
dataSummary = new QWebEngineView(this);
//XXXdataSummary->setEnabled(false); // stop grabbing focus
if (dpiXFactor > 1) {
// 80 lines per page on hidpi screens (?)
int pixelsize = pixelSizeForFont(defaultFont, QApplication::desktop()->geometry().height()/80);
dataSummary->settings()->setFontSize(QWebEngineSettings::DefaultFontSize, pixelsize);
} else {
dataSummary->settings()->setFontSize(QWebEngineSettings::DefaultFontSize, defaultFont.pointSize()+1);
}
dataSummary->settings()->setFontFamily(QWebEngineSettings::StandardFont, defaultFont.family());
#else
dataSummary = new QWebView(this);
dataSummary->settings()->setFontSize(QWebSettings::DefaultFontSize, defaultFont.pointSize()+1);
dataSummary->settings()->setFontFamily(QWebSettings::StandardFont, defaultFont.family());
#endif
dataSummary->setContentsMargins(0,0,0,0);
dataSummary->page()->view()->setContentsMargins(0,0,0,0);
dataSummary->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
dataSummary->setAcceptDrops(false);
// compare plot page
compareplotsWidget = new QWidget(this);
compareplotsWidget->setPalette(palette);
compareplotsLayout = new QVBoxLayout(compareplotsWidget);
compareplotsLayout->setSpacing(0);
compareplotsLayout->setContentsMargins(0,0,0,0);
compareplotArea = new QScrollArea(this);
#ifdef Q_OS_WIN
cde = QStyleFactory::create(OS_STYLE);
compareplotArea->setStyle(cde);
#endif
compareplotArea->setAutoFillBackground(false);
compareplotArea->setWidgetResizable(true);
compareplotArea->setWidget(compareplotsWidget);
compareplotArea->setFrameStyle(QFrame::NoFrame);
compareplotArea->setContentsMargins(0,0,0,0);
compareplotArea->setPalette(palette);
// the stack
stackWidget = new QStackedWidget(this);
stackWidget->addWidget(plotWidget);
stackWidget->addWidget(dataSummary);
stackWidget->addWidget(plotArea);
stackWidget->addWidget(compareplotArea);
stackWidget->setCurrentIndex(0);
mainLayout->addWidget(stackWidget);
setChartLayout(mainLayout);
HelpWhatsThis *helpStack = new HelpWhatsThis(stackWidget);
stackWidget->setWhatsThis(helpStack->getWhatsThisText(HelpWhatsThis::ChartTrends_MetricTrends));
// reveal controls
QHBoxLayout *revealLayout = new QHBoxLayout;
revealLayout->setContentsMargins(0,0,0,0);
revealLayout->addStretch();
revealLayout->addWidget(new QLabel(tr("Group by"),this));
rGroupBy = new QxtStringSpinBox(this);
QStringList strings;
strings << tr("Days")
<< tr("Weeks")
<< tr("Months")
<< tr("Years")
<< tr("Time Of Day")
<< tr("All");
rGroupBy->setStrings(strings);
rGroupBy->setValue(0);
revealLayout->addWidget(rGroupBy);
rData = new QCheckBox(tr("Data Table"), this);
rStack = new QCheckBox(tr("Stacked"), this);
QVBoxLayout *checks = new QVBoxLayout;
checks->setSpacing(2 *dpiXFactor);
checks->setContentsMargins(0,0,0,0);
checks->addWidget(rData);
checks->addWidget(rStack);
revealLayout->addLayout(checks);
revealLayout->addStretch();
setRevealLayout(revealLayout);
// add additional menu items before setting
// controls since the menu is SET from setControls
QAction *exportData = new QAction(tr("Export Chart Data..."), this);
addAction(exportData);
// the controls
QWidget *c = new QWidget;
c->setContentsMargins(0,0,0,0);
HelpWhatsThis *helpConfig = new HelpWhatsThis(c);
c->setWhatsThis(helpConfig->getWhatsThisText(HelpWhatsThis::ChartTrends_MetricTrends));
QVBoxLayout *cl = new QVBoxLayout(c);
cl->setContentsMargins(0,0,0,0);
cl->setSpacing(0);
setControls(c);
// the popup
popup = new GcPane();
ltmPopup = new LTMPopup(context);
QVBoxLayout *popupLayout = new QVBoxLayout();
popupLayout->addWidget(ltmPopup);
popup->setLayout(popupLayout);
ltmTool = new LTMTool(context, &settings);
// initialise
settings.ltmTool = ltmTool;
settings.groupBy = LTM_DAY;
settings.legend = ltmTool->showLegend->isChecked();
settings.events = ltmTool->showEvents->isChecked();
settings.shadeZones = ltmTool->shadeZones->isChecked();
settings.showData = ltmTool->showData->isChecked();
settings.stack = ltmTool->showStack->isChecked();
settings.stackWidth = ltmTool->stackSlider->value();
rData->setChecked(ltmTool->showData->isChecked());
rStack->setChecked(ltmTool->showStack->isChecked());
cl->addWidget(ltmTool);
connect(this, SIGNAL(dateRangeChanged(DateRange)), this, SLOT(dateRangeChanged(DateRange)));
connect(this, SIGNAL(styleChanged(int)), this, SLOT(styleChanged(int)));
connect(ltmTool, SIGNAL(filterChanged()), this, SLOT(filterChanged()));
connect(context, SIGNAL(homeFilterChanged()), this, SLOT(filterChanged()));
connect(ltmTool->groupBy, SIGNAL(currentIndexChanged(int)), this, SLOT(groupBySelected(int)));
connect(rGroupBy, SIGNAL(valueChanged(int)), this, SLOT(rGroupBySelected(int)));
connect(ltmTool->applyButton, SIGNAL(clicked(bool)), this, SLOT(applyClicked(void)));
connect(ltmTool->shadeZones, SIGNAL(stateChanged(int)), this, SLOT(shadeZonesClicked(int)));
connect(ltmTool->showData, SIGNAL(stateChanged(int)), this, SLOT(showDataClicked(int)));
connect(rData, SIGNAL(stateChanged(int)), this, SLOT(showDataClicked(int)));
connect(ltmTool->showStack, SIGNAL(stateChanged(int)), this, SLOT(showStackClicked(int)));
connect(rStack, SIGNAL(stateChanged(int)), this, SLOT(showStackClicked(int)));
connect(ltmTool->stackSlider, SIGNAL(valueChanged(int)), this, SLOT(zoomSliderChanged()));
connect(ltmTool->showLegend, SIGNAL(stateChanged(int)), this, SLOT(showLegendClicked(int)));
connect(ltmTool->showEvents, SIGNAL(stateChanged(int)), this, SLOT(showEventsClicked(int)));
connect(ltmTool, SIGNAL(useCustomRange(DateRange)), this, SLOT(useCustomRange(DateRange)));
connect(ltmTool, SIGNAL(useThruToday()), this, SLOT(useThruToday()));
connect(ltmTool, SIGNAL(useStandardRange()), this, SLOT(useStandardRange()));
connect(ltmTool, SIGNAL(curvesChanged()), this, SLOT(refresh()));
connect(context, SIGNAL(filterChanged()), this, SLOT(filterChanged()));
connect(context, SIGNAL(refreshUpdate(QDate)), this, SLOT(refreshUpdate(QDate)));
connect(context, SIGNAL(refreshEnd()), this, SLOT(refresh()));
// comparing things
connect(context, SIGNAL(compareDateRangesStateChanged(bool)), this, SLOT(compareChanged()));
connect(context, SIGNAL(compareDateRangesChanged()), this, SLOT(compareChanged()));
connect(context, SIGNAL(rideAdded(RideItem*)), this, SLOT(refresh(void)));
connect(context, SIGNAL(rideDeleted(RideItem*)), this, SLOT(refresh(void)));
connect(context, SIGNAL(rideSaved(RideItem*)), this, SLOT(refresh(void)));
connect(context, SIGNAL(configChanged(qint32)), this, SLOT(configChanged(qint32)));
connect(context, SIGNAL(presetSelected(int)), this, SLOT(presetSelected(int)));
// custom menu item
connect(exportData, SIGNAL(triggered()), this, SLOT(exportData()));
// normal view
connect(spanSlider, SIGNAL(lowerPositionChanged(int)), this, SLOT(spanSliderChanged()));
connect(spanSlider, SIGNAL(upperPositionChanged(int)), this, SLOT(spanSliderChanged()));
connect(scrollLeft, SIGNAL(clicked()), this, SLOT(moveLeft()));
connect(scrollRight, SIGNAL(clicked()), this, SLOT(moveRight()));
configChanged(CONFIG_APPEARANCE);
}
LTMWindow::~LTMWindow()
{
delete popup;
}
void
LTMWindow::hideBasic()
{
ltmTool->hideBasic();
}
void
LTMWindow::configChanged(qint32)
{
#ifndef Q_OS_MAC
plotArea->setStyleSheet(TabView::ourStyleSheet());
compareplotArea->setStyleSheet(TabView::ourStyleSheet());
#endif
refresh();
}
void
LTMWindow::styleChanged(int style)
{
if (style) {
// hide spanslider
spanSlider->hide();
scrollLeft->hide();
scrollRight->hide();
} else {
// show spanslider
spanSlider->show();
scrollLeft->show();
scrollRight->show();
}
}
void
LTMWindow::compareChanged()
{
if (!amVisible()) {
compareDirty = true;
return;
}
if (isCompare()) {
// refresh plot handles the compare case
refreshPlot();
} else {
// forced refresh back to normal
stackDirty = dirty = true;
filterChanged(); // forces reread etc
}
repaint();
}
void
LTMWindow::rideSelected() { } // deprecated
void
LTMWindow::presetSelected(int index)
{
// apply a preset if we are configured to do that...
if (ltmTool->usePreset->isChecked()) {
// save chart setup
int groupBy = settings.groupBy;
bool legend = settings.legend;
bool events = settings.events;
bool stack = settings.stack;
bool shadeZones = settings.shadeZones;
QDateTime start = settings.start;
QDateTime end = settings.end;
// apply preset
settings = context->athlete->presets[index];
// now get back the local chart setup
settings.ltmTool = ltmTool;
settings.bests = &bestsresults;
settings.groupBy = groupBy;
settings.legend = legend;
settings.events = events;
settings.stack = stack;
settings.shadeZones = shadeZones;
settings.start = start;
settings.end = end;
// Set the specification
FilterSet fs;
fs.addFilter(context->isfiltered, context->filters);
fs.addFilter(context->ishomefiltered, context->homeFilters);
fs.addFilter(ltmTool->isFiltered(), ltmTool->filters());
settings.specification.setFilterSet(fs);
settings.specification.setDateRange(DateRange(settings.start.date(), settings.end.date()));
ltmTool->applySettings();
refresh();
setProperty("subtitle", settings.name);
} else {
setProperty("subtitle", property("title").toString());
}
}
void
LTMWindow::refreshUpdate(QDate here)
{
if (isVisible() && here > settings.start.date() && lastRefresh.secsTo(QTime::currentTime()) > 5) {
lastRefresh = QTime::currentTime();
refresh();
}
}
void
LTMWindow::moveLeft()
{
// move across by 5% of the span, or to zero if not much left
int span = spanSlider->upperValue() - spanSlider->lowerValue();
int delta = span / 20;
if (delta > (spanSlider->lowerValue() - spanSlider->minimum()))
delta = spanSlider->lowerValue() - spanSlider->minimum();
spanSlider->setLowerValue(spanSlider->lowerValue()-delta);
spanSlider->setUpperValue(spanSlider->upperValue()-delta);
spanSliderChanged();
}
void
LTMWindow::moveRight()
{
// move across by 5% of the span, or to zero if not much left
int span = spanSlider->upperValue() - spanSlider->lowerValue();
int delta = span / 20;
if (delta > (spanSlider->maximum() - spanSlider->upperValue()))
delta = spanSlider->maximum() - spanSlider->upperValue();
spanSlider->setLowerValue(spanSlider->lowerValue()+delta);
spanSlider->setUpperValue(spanSlider->upperValue()+delta);
spanSliderChanged();
}
void
LTMWindow::spanSliderChanged()
{
// so reset the axis range for ltmPlot
ltmPlot->setAxisScale(QwtPlot::xBottom, spanSlider->lowerValue(), spanSlider->upperValue());
ltmPlot->replot();
}
void
LTMWindow::refreshPlot()
{
if (amVisible() == true) {
if (isCompare()) {
// COMPARE PLOTS
stackWidget->setCurrentIndex(3);
refreshCompare();
} else if (ltmTool->showData->isChecked()) {
// DATA TABLE
stackWidget->setCurrentIndex(1);
refreshDataTable();
} else {
if (ltmTool->showStack->isChecked()) {
// STACK PLOTS
refreshStackPlots();
stackWidget->setCurrentIndex(2);
stackDirty = false;
} else {
// NORMAL PLOTS
plotted = DateRange(settings.start.date(), settings.end.date());
ltmPlot->setData(&settings);
stackWidget->setCurrentIndex(0);
dirty = false;
spanSlider->setMinimum(ltmPlot->axisScaleDiv(QwtPlot::xBottom).lowerBound());
spanSlider->setMaximum(ltmPlot->axisScaleDiv(QwtPlot::xBottom).upperBound());
spanSlider->setLowerValue(spanSlider->minimum());
spanSlider->setUpperValue(spanSlider->maximum());
}
}
}
}
void
LTMWindow::refreshCompare()
{
// not if in compare mode
if (!isCompare()) return;
// setup stacks but only if needed
//if (!stackDirty) return; // lets come back to that!
setUpdatesEnabled(false);
// delete old and create new...
// QScrollArea *plotArea;
// QWidget *plotsWidget;
// QVBoxLayout *plotsLayout;
// QList<LTMSettings> plotSettings;
foreach (LTMPlot *p, compareplots) {
compareplotsLayout->removeWidget(p);
delete p;
}
compareplots.clear();
compareplotSettings.clear();
if (compareplotsLayout->count() == 1) {
compareplotsLayout->takeAt(0); // remove the stretch
}
// now lets create them all again
// based upon the current settings
// we create a plot for each curve
// but where they are stacked we put
// them all in the SAME plot
// so we go once through picking out
// the stacked items and once through
// for all the rest of the curves
LTMSettings plotSetting = settings;
plotSetting.metrics.clear();
foreach(MetricDetail m, settings.metrics) {
if (m.stack) plotSetting.metrics << m;
}
bool first = true;
// create ltmPlot with this
if (plotSetting.metrics.count()) {
compareplotSettings << plotSetting;
// create and setup the plot
LTMPlot *stacked = new LTMPlot(this, context, first);
stacked->setCompareData(&compareplotSettings.last()); // setData using the compare data
stacked->setFixedHeight(200); // maybe make this adjustable later
// no longer first
first = false;
// now add
compareplotsLayout->addWidget(stacked);
compareplots << stacked;
}
// OK, now one plot for each curve
// that isn't stacked!
foreach(MetricDetail m, settings.metrics) {
// ignore stacks
if (m.stack) continue;
plotSetting = settings;
plotSetting.metrics.clear();
plotSetting.metrics << m;
compareplotSettings << plotSetting;
// create and setup the plot
LTMPlot *plot = new LTMPlot(this, context, first);
plot->setCompareData(&compareplotSettings.last()); // setData using the compare data
// no longer first
first = false;
// now add
compareplotsLayout->addWidget(plot);
compareplots << plot;
}
// squash em up
compareplotsLayout->addStretch();
// set a common X-AXIS
if (settings.groupBy != LTM_TOD) {
int MAXX=0;
foreach(LTMPlot *p, compareplots) {
if (p->getMaxX() > MAXX) MAXX=p->getMaxX();
}
foreach(LTMPlot *p, compareplots) {
p->setMaxX(MAXX);
}
}
// resize to choice
zoomSliderChanged();
// we no longer dirty
compareDirty = false;
setUpdatesEnabled(true);
}
void
LTMWindow::refreshStackPlots()
{
// not if in compare mode
if (isCompare()) return;
// setup stacks but only if needed
//if (!stackDirty) return; // lets come back to that!
setUpdatesEnabled(false);
// delete old and create new...
// QScrollArea *plotArea;
// QWidget *plotsWidget;
// QVBoxLayout *plotsLayout;
// QList<LTMSettings> plotSettings;
foreach (LTMPlot *p, plots) {
plotsLayout->removeWidget(p);
delete p;
}
plots.clear();
plotSettings.clear();
if (plotsLayout->count() == 1) {
plotsLayout->takeAt(0); // remove the stretch
}
// now lets create them all again
// based upon the current settings
// we create a plot for each curve
// but where they are stacked we put
// them all in the SAME plot
// so we go once through picking out
// the stacked items and once through
// for all the rest of the curves
LTMSettings plotSetting = settings;
plotSetting.metrics.clear();
foreach(MetricDetail m, settings.metrics) {
if (m.stack) plotSetting.metrics << m;
}
bool first = true;
// create ltmPlot with this
if (plotSetting.metrics.count()) {
plotSettings << plotSetting;
// create and setup the plot
LTMPlot *stacked = new LTMPlot(this, context, first);
stacked->setData(&plotSettings.last());
stacked->setFixedHeight(200); // maybe make this adjustable later
// no longer first
first = false;
// now add
plotsLayout->addWidget(stacked);
plots << stacked;
}
// OK, now one plot for each curve
// that isn't stacked!
foreach(MetricDetail m, settings.metrics) {
// ignore stacks
if (m.stack) continue;
plotSetting = settings;
plotSetting.metrics.clear();
plotSetting.metrics << m;
plotSettings << plotSetting;
// create and setup the plot
LTMPlot *plot = new LTMPlot(this, context, first);
plot->setData(&plotSettings.last());
// no longer first
first = false;
// now add
plotsLayout->addWidget(plot);
plots << plot;
}
// squash em up
plotsLayout->addStretch();
// resize to choice
zoomSliderChanged();
// we no longer dirty
stackDirty = false;
setUpdatesEnabled(true);
}
void
LTMWindow::zoomSliderChanged()
{
static int add[] = { 0, 20, 50, 80, 100, 150, 200, 400 };
int index = ltmTool->stackSlider->value();
settings.stackWidth = ltmTool->stackSlider->value();
setUpdatesEnabled(false);
// do the compare and the noncompare plots
// at the same time, as we don't need to worry
// about optimising out as its fast anyway
foreach(LTMPlot *plot, plots) {
plot->setFixedHeight(150 + add[index]);
}
foreach(LTMPlot *plot, compareplots) {
plot->setFixedHeight(150 + add[index]);
}
setUpdatesEnabled(true);
}
void
LTMWindow::useCustomRange(DateRange range)
{
// plot using the supplied range
useCustom = true;
useToToday = false;
custom = range;
dateRangeChanged(custom);
}
void
LTMWindow::useStandardRange()
{
useToToday = useCustom = false;
dateRangeChanged(myDateRange);
}
void
LTMWindow::useThruToday()
{
// plot using the supplied range
useCustom = false;
useToToday = true;
custom = myDateRange;
if (custom.to > QDate::currentDate()) custom.to = QDate::currentDate();
dateRangeChanged(custom);
}
// total redraw, reread data etc
void
LTMWindow::refresh()
{
setProperty("color", GColor(CTRENDPLOTBACKGROUND)); // called on config change
// not if in compare mode
if (isCompare()) return;
// refresh for changes to ridefiles / zones
if (amVisible() == true) {
bestsresults.clear();
bestsresults = RideFileCache::getAllBestsFor(context, settings.metrics, settings.specification);
refreshPlot();
repaint(); // title changes color when filters change
// set spanslider to limits of ltmPlot
} else {
stackDirty = dirty = true;
}
}
void
LTMWindow::dateRangeChanged(DateRange range)
{
// do we need to use custom range?
if (useCustom || useToToday) range = custom;
// we already plotted that date range
if (amVisible() || dirty || range.from != plotted.from || range.to != plotted.to) {
settings.bests = &bestsresults;
// we let all the state get updated, but lets not actually plot
// whilst in compare mode -- but when compare mode ends we will
// call filterChanged, so need to record the fact that the date
// range changed whilst we were in compare mode
if (!isCompare()) {
// apply filter to new date range too -- will also refresh plot
filterChanged();
} else {
// we've been told to redraw so maybe
// compare mode was switched whilst we were
// not visible, lets refresh
if (compareDirty) compareChanged();
}
}
}
void
LTMWindow::filterChanged()
{
// ignore in compare mode
if (isCompare()) return;
if (amVisible() == false) return;
if (useCustom) {
settings.start = QDateTime(custom.from, QTime(0,0));
settings.end = QDateTime(custom.to, QTime(24,0,0));
} else if (useToToday) {
settings.start = QDateTime(myDateRange.from, QTime(0,0));
settings.end = QDateTime(myDateRange.to, QTime(24,0,0));
QDate today = QDate::currentDate();
if (settings.end.date() > today) settings.end = QDateTime(today, QTime(24,0,0));
} else {
settings.start = QDateTime(myDateRange.from, QTime(0,0));
settings.end = QDateTime(myDateRange.to, QTime(24,0,0));
}
settings.title = myDateRange.name;
// Set the specification
FilterSet fs;
fs.addFilter(context->isfiltered, context->filters);
fs.addFilter(context->ishomefiltered, context->homeFilters);
fs.addFilter(ltmTool->isFiltered(), ltmTool->filters());
settings.specification.setFilterSet(fs);
settings.specification.setDateRange(DateRange(settings.start.date(), settings.end.date()));
// if we want weeks and start is not a monday go back to the monday
int dow = settings.start.date().dayOfWeek();
if (settings.groupBy == LTM_WEEK && dow >1 && settings.start != QDateTime(QDate(), QTime(0,0)))
settings.start = settings.start.addDays(-1*(dow-1));
// we need to get data again and apply filter
bestsresults.clear();
bestsresults = RideFileCache::getAllBestsFor(context, settings.metrics, settings.specification);
settings.bests = &bestsresults;
refreshPlot();
repaint(); // just for the title..
}
void
LTMWindow::rGroupBySelected(int selected)
{
if (selected >= 0) {
settings.groupBy = ltmTool->groupBy->itemData(selected).toInt();
ltmTool->groupBy->setCurrentIndex(selected);
}
}
void
LTMWindow::groupBySelected(int selected)
{
if (selected >= 0) {
settings.groupBy = ltmTool->groupBy->itemData(selected).toInt();
rGroupBy->setValue(selected);
refreshPlot();
}
}
void
LTMWindow::showDataClicked(int state)
{
bool checked = state;
// only change if changed, to avoid endless looping
if (ltmTool->showData->isChecked() != checked) ltmTool->showData->setChecked(checked);
if (rData->isChecked()!=checked) rData->setChecked(checked);
if (settings.showData != checked) {
settings.showData = checked;
refreshPlot();
}
}
void
LTMWindow::shadeZonesClicked(int state)
{
bool checked = state;
// only change if changed, to avoid endless looping
if (ltmTool->shadeZones->isChecked() != checked) ltmTool->shadeZones->setChecked(checked);
settings.shadeZones = state;
refreshPlot();
}
void
LTMWindow::showLegendClicked(int state)
{
settings.legend = state;
refreshPlot();
}
void
LTMWindow::showEventsClicked(int state)
{
settings.events = bool(state);
refreshPlot();
}
void
LTMWindow::showStackClicked(int state)
{
bool checked = state;
// only change if changed, to avoid endless looping
if (ltmTool->showStack->isChecked() != checked) ltmTool->showStack->setChecked(checked);
if (rStack->isChecked() != checked) rStack->setChecked(checked);
settings.stack = checked;
refreshPlot();
}
void
LTMWindow::applyClicked()
{
if (ltmTool->charts->selectedItems().count() == 0) return;
int selected = ltmTool->charts->invisibleRootItem()->indexOfChild(ltmTool->charts->selectedItems().first());
if (selected >= 0) {
// save chart setup
int groupBy = settings.groupBy;
bool legend = settings.legend;
bool events = settings.events;
bool stack = settings.stack;
bool shadeZones = settings.shadeZones;
QDateTime start = settings.start;
QDateTime end = settings.end;
// apply preset
settings = context->athlete->presets[selected];
// now get back the local chart setup
settings.ltmTool = ltmTool;
settings.bests = &bestsresults;
settings.groupBy = groupBy;
settings.legend = legend;
settings.events = events;
settings.stack = stack;
settings.shadeZones = shadeZones;
settings.start = start;
settings.end = end;
// Set the specification
FilterSet fs;
fs.addFilter(context->isfiltered, context->filters);
fs.addFilter(context->ishomefiltered, context->homeFilters);
fs.addFilter(ltmTool->isFiltered(), ltmTool->filters());
settings.specification.setFilterSet(fs);
settings.specification.setDateRange(DateRange(settings.start.date(), settings.end.date()));
ltmTool->applySettings();
refresh();
}
}
int
LTMWindow::groupForDate(QDate date)
{
switch(settings.groupBy) {
case LTM_WEEK:
{
// must start from 1 not zero!
return 1 + ((date.toJulianDay() - settings.start.date().toJulianDay()) / 7);
}
case LTM_MONTH: return (date.year()*12) + date.month();
case LTM_YEAR: return date.year();
case LTM_DAY:
default:
return date.toJulianDay();
}
}