/
RideNavigator.cpp
1086 lines (906 loc) · 38.2 KB
/
RideNavigator.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 "RideNavigator.h"
#include "RideNavigatorProxy.h"
#include "SearchFilterBox.h"
#include <QtGui>
#include <QString>
RideNavigator::RideNavigator(MainWindow *parent, bool mainwindow) : main(parent), active(false), _groupBy(-1)
{
// get column headings
// default column layouts etc
_columns = QString(tr("*|Workout Code|TSS|Date|"));
_widths = QString("0|80|50|50|");
_sortByIndex = 2;
_sortByOrder = 0;
currentColumn = -1;
_groupBy = -1;
fontHeight = QFontMetrics(QFont()).height();
init = false;
mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(0);
if (mainwindow) mainLayout->setContentsMargins(0,0,0,0);
else mainLayout->setContentsMargins(2,2,2,2); // so we can resize!
sqlModel = new QSqlTableModel(this, main->metricDB->db()->connection());
sqlModel->setTable("metrics");
sqlModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
sqlModel->select();
while (sqlModel->canFetchMore(QModelIndex())) sqlModel->fetchMore(QModelIndex());
searchFilter = new SearchFilter(this);
searchFilter->setSourceModel(sqlModel); // filter out/in search results
groupByModel = new GroupByModel(this);
groupByModel->setSourceModel(searchFilter);
sortModel = new BUGFIXQSortFilterProxyModel(this);
sortModel->setSourceModel(groupByModel);
sortModel->setDynamicSortFilter(true);
#ifdef GC_HAVE_LUCENE
if (!mainwindow) {
searchFilterBox = new SearchFilterBox(this, main);
mainLayout->addWidget(searchFilterBox);
}
#endif
// get setup
tableView = new QTreeView;
delegate = new NavigatorCellDelegate(this);
tableView->setItemDelegate(delegate);
tableView->setModel(sortModel);
tableView->setSortingEnabled(true);
tableView->setAlternatingRowColors(false);
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); // read-only
mainLayout->addWidget(tableView);
tableView->expandAll();
tableView->header()->setCascadingSectionResizes(true); // easier to resize this way
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
tableView->header()->setStretchLastSection(false);
tableView->header()->setMinimumSectionSize(0);
tableView->header()->setFocusPolicy(Qt::NoFocus);
#ifdef Q_OS_MAC
tableView->header()->setSortIndicatorShown(false); // blue looks nasty
tableView->setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
tableView->installEventFilter(this);
tableView->viewport()->installEventFilter(this);
tableView->setMouseTracking(true);
tableView->setFrameStyle(QFrame::NoFrame);
// good to go
tableView->show();
resetView();
// refresh when database is updated
connect(main->metricDB, SIGNAL(dataChanged()), this, SLOT(refresh()));
// refresh when config changes (metric/imperial?)
connect(main, SIGNAL(configChanged()), this, SLOT(refresh()));
// refresh when rides added/removed
connect(main, SIGNAL(rideAdded(RideItem*)), this, SLOT(refresh()));
connect(main, SIGNAL(rideDeleted(RideItem*)), this, SLOT(refresh()));
connect(main->rideTreeWidget(), SIGNAL(itemSelectionChanged()), this, SLOT(rideTreeSelectionChanged()));
// selection of a ride by double clicking it, we need to update the ride list
connect(tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(selectRide(QModelIndex)));
connect(tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(cursorRide()));
// user selected a ride on the ride list, we should reflect that too..
// user moved columns
connect(tableView->header(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(columnsChanged()));
connect(tableView->header(), SIGNAL(sectionResized(int,int,int)), this, SLOT(columnsChanged()));
// user sorted by column
connect(tableView->header(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this, SLOT(selectRow()));
// context menu is provided by mainWindow
connect(tableView,SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(showTreeContextMenuPopup(const QPoint &)));
connect(tableView->header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(setSortBy(int,Qt::SortOrder)));
#ifdef GC_HAVE_LUCENE
if (!mainwindow) {
connect(searchFilterBox, SIGNAL(searchResults(QStringList)), this, SLOT(searchStrings(QStringList)));
connect(searchFilterBox, SIGNAL(searchClear()), this, SLOT(clearSearch()));
}
#endif
// we accept drag and drop operations
setAcceptDrops(true);
}
RideNavigator::~RideNavigator()
{
delete tableView;
delete groupByModel;
delete sqlModel;
}
void
RideNavigator::refresh()
{
fontHeight = QFontMetrics(QFont()).height();
sqlModel->select();
while (sqlModel->canFetchMore(QModelIndex()))
sqlModel->fetchMore(QModelIndex());
active=false;
rideTreeSelectionChanged();
}
void
RideNavigator::resizeEvent(QResizeEvent*)
{
setWidth(geometry().width());
}
void
RideNavigator::resetView()
{
active = true;
QList<QString> cols = _columns.split("|", QString::SkipEmptyParts);
// to account for translations
QMap <QString, QString> internalNameMap;
nameMap.clear();
columnMetrics.clear();
// add the standard columns to the map
nameMap.insert("filename", tr("File"));
internalNameMap.insert("File", tr("File"));
nameMap.insert("timestamp", tr("Last updated"));
internalNameMap.insert("Last updated", tr("Last updated"));
nameMap.insert("ride_date", tr("Date"));
internalNameMap.insert("Date", tr("Date"));
nameMap.insert("ride_time", tr("Time")); // virtual columns show time from ride_date
internalNameMap.insert("Time", tr("Time"));
nameMap.insert("fingerprint", tr("Config Checksum"));
internalNameMap.insert("Config Checksum", tr("Config Checksum"));
// add metrics to the map
const RideMetricFactory &factory = RideMetricFactory::instance();
for (int i=0; i<factory.metricCount(); i++) {
QString converted = QTextEdit(factory.rideMetric(factory.metricName(i))->name()).toPlainText();
// from sql column name to friendly metric name
nameMap.insert(QString("X%1").arg(factory.metricName(i)), converted);
// from (english) internalName to (translated) Name
internalNameMap.insert(factory.rideMetric(factory.metricName(i))->internalName(), converted);
// from friendly metric name to metric pointer
columnMetrics.insert(converted, factory.rideMetric(factory.metricName(i)));
}
// add metadata fields...
SpecialFields sp; // all the special fields are in here...
foreach(FieldDefinition field, main->rideMetadata()->getFields()) {
if (!sp.isMetric(field.name) && (field.type < 5 || field.type == 7)) {
nameMap.insert(QString("Z%1").arg(sp.makeTechName(field.name)), sp.displayName(field.name));
internalNameMap.insert(field.name, sp.displayName(field.name));
}
}
// cols list needs to be mapped to match logicalHeadings
for (int i = 0; i < cols.count(); i++)
cols[i] = internalNameMap.value(cols[i], cols[i]);
logicalHeadings.clear();
tableView->reset();
tableView->header()->reset();
// setup the logical heading list
for (int i=0; i<tableView->header()->count(); i++) {
QString friendly, techname = sortModel->headerData(i, Qt::Horizontal).toString();
if ((friendly = nameMap.value(techname, "unknown")) != "unknown") {
sortModel->setHeaderData(i, Qt::Horizontal, friendly);
logicalHeadings << friendly;
} else
logicalHeadings << techname;
}
// hide everything, we show what we want later
for (int i=0; i<tableView->header()->count(); i++) {
int index = tableView->header()->logicalIndex(i);
tableView->setColumnHidden(index, true);
tableView->setColumnWidth(index, 0);
}
// now re-order the columns according to the
// prevailing preferences. They are listed in
// the order we want them, column zero is the
// group by column, so we leave that alone
for(int i=1; i<cols.count(); i++) {
tableView->header()->moveSection(tableView->header()->visualIndex(logicalHeadings.indexOf(cols[i])), i);
}
// initialise to whatever groupBy we want to start with
tableView->sortByColumn(sortByIndex(), static_cast<Qt::SortOrder>(sortByOrder()));;
//tableView->setColumnHidden(0, true);
tableView->setColumnWidth(0,0);
// set the column widths
int columnnumber=0;
foreach(QString size, _widths.split("|", QString::SkipEmptyParts)) {
if (columnnumber >= cols.count()) break;
int index = tableView->header()->logicalIndex(columnnumber);
tableView->setColumnHidden(index, false);
tableView->setColumnWidth(index, columnnumber ? size.toInt() : 0);
columnnumber++;
}
setGroupByColumn();
active = false;
resizeEvent(NULL);
// Select the current ride
rideTreeSelectionChanged();
columnsChanged();
}
void RideNavigator::searchStrings(QStringList list)
{
searchFilter->setStrings(list);
setWidth(geometry().width());
}
void RideNavigator::clearSearch()
{
searchFilter->clearStrings();
QApplication::processEvents(); // repaint/resize list view - scrollbar..
setWidth(geometry().width()); // before we update column sizes!
}
void RideNavigator::setWidth(int x)
{
if (init == false) return;
active = true;
if (tableView->verticalScrollBar()->isVisible())
x -= tableView->verticalScrollBar()->width()
+ 0 ; // !! no longer account for content margins of 3,3,3,3 was + 6
// take the margins into accopunt top
x -= mainLayout->contentsMargins().left() + mainLayout->contentsMargins().right();
// ** NOTE **
// When iterating over the section headings we
// always use the tableview not the sortmodel
// so we can skip over the virtual column 0
// which is used to group by, is visible but
// must have a width of 0. This is why all
// the for loops start with i=1
tableView->setColumnWidth(0,0); // in case use grabbed it
// is it narrower than the headings?
int headwidth=0;
int n=0;
for (int i=1; i<tableView->header()->count(); i++)
if (tableView->header()->isSectionHidden(i) == false) {
headwidth += tableView->columnWidth(i);
n++;
}
// headwidth is no, x is to-be width
// we need to 'stretch' the sections
// proportionally to fit into new
// layout
int setwidth=0;
int newwidth=0;
for (int i=1; i<tableView->header()->count(); i++) {
if (tableView->header()->isSectionHidden(i) == false) {
newwidth = (double)((((double)tableView->columnWidth(i)/(double)headwidth)) * (double)x);
if (newwidth < 20) newwidth = 20;
QString columnName = tableView->model()->headerData(i, Qt::Horizontal).toString();
if (columnName == "*") newwidth = 0;
tableView->setColumnWidth(i, newwidth);
setwidth += newwidth;
}
}
// UGH. Now account for the fact that the smaller columns
// didn't take their fair share of a negative resize
// so we need to snip off from the larger columns.
if (setwidth != x) {
// how many columns we got to snip from?
int colsleft = 0;
for (int i=1; i<tableView->header()->count(); i++)
if (tableView->header()->isSectionHidden(i) == false && tableView->columnWidth(i)>20)
colsleft++;
// run through ... again.. snipping off some pixels
if (colsleft) {
int snip = (setwidth-x) / colsleft; //could be negative too
for (int i=1; i<tableView->header()->count(); i++) {
if (tableView->header()->isSectionHidden(i) == false && tableView->columnWidth(i)>20) {
tableView->setColumnWidth(i, tableView->columnWidth(i)-snip);
setwidth -= snip;
}
}
}
}
if (setwidth < x)
delegate->setWidth(pwidth=setwidth);
else
delegate->setWidth(pwidth=x);
active = false;
}
// make sure the columns are all neat and tidy when the ride navigator is shown
void
RideNavigator::showEvent(QShowEvent *)
{
init = true;
setWidth(geometry().width());
}
// routines called by the sidebar to let the user
// update the columns/grouping without using right-click
QStringList
RideNavigator::columnNames() const
{
return visualHeadings;
}
void
RideNavigator::setGroupByColumnName(QString name)
{
if (name == "") {
noGroups();
} else {
int logical = logicalHeadings.indexOf(name);
if (logical >= 0) {
currentColumn = logical;
setGroupByColumn();
}
}
}
void
RideNavigator::columnsChanged()
{
if (active == true) return;
active = true;
visualHeadings.clear(); // they have moved
// get the names used
for (int i=0; i<tableView->header()->count(); i++) {
if (tableView->header()->isSectionHidden(tableView->header()->logicalIndex(i)) != true) {
int index = tableView->header()->logicalIndex(i);
visualHeadings << logicalHeadings[index];
}
}
// write to config
QString headings;
foreach(QString name, visualHeadings)
headings += name + "|";
_columns = headings;
// get column widths
QString widths;
for (int i=0; i<tableView->header()->count(); i++) {
int index = tableView->header()->logicalIndex(i);
if (tableView->header()->isSectionHidden(index) != true) {
widths += QString("%1|").arg(tableView->columnWidth(index));
}
}
// clean up
active = false;
setWidth(geometry().width()); // calculate width...
_widths = widths;
}
bool
RideNavigator::eventFilter(QObject *object, QEvent *e)
{
// if in doubt hide the tooltip, but paint events are
// always generated in the viewport when the popup is shown
// so we ignore those
if (e->type() != QEvent::ToolTip && e->type() != QEvent::Paint &&
e->type() != QEvent::WinIdChange && e->type() != QEvent::Destroy) {
main->setBubble("");
}
// not for the table?
if (object != (QObject *)tableView) return false;
// what happenned?
switch(e->type())
{
case QEvent::ContextMenu:
{
borderMenu(((QMouseEvent *)e)->pos());
return true; // I'll take that thanks
break;
}
case QEvent::KeyPress:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
if (keyEvent->modifiers() & Qt::ControlModifier) {
// Ctrl+Key
switch (keyEvent->key()) {
case Qt::Key_C: // defacto standard for copy
return true;
case Qt::Key_V: // defacto standard for paste
return true;
case Qt::Key_X: // defacto standard for cut
return true;
case Qt::Key_Y: // emerging standard for redo
return true;
case Qt::Key_Z: // common standard for undo
return true;
case Qt::Key_0:
return true;
default:
return false;
}
} else {
// Not Ctrl ...
switch (keyEvent->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
selectRide(tableView->currentIndex());
return true;
default:
return false;
}
}
break;
}
case QEvent::WindowActivate:
{
active=true;
// set the column widths
int columnnumber=0;
foreach(QString size, _widths.split("|", QString::SkipEmptyParts)) {
tableView->setColumnWidth(columnnumber, size.toInt());
}
active=false;
setWidth(geometry().width()); // calculate width...
}
break;
case QEvent::ToolTip:
case QEvent::ToolTipChange:
{
QPoint global = dynamic_cast<QHelpEvent*>(e)->globalPos();
QPoint local = tableView->viewport()->mapFromGlobal(global);
// off view port!
if (local.y() <= 0 || local.x() <= 0) {
main->setBubble("");
return false;
}
QModelIndex index = tableView->indexAt(local);
if (index.isValid()) {
QString hoverFileName = tableView->model()->data(index, Qt::UserRole+1).toString();
e->accept();
QPoint p = local;
p.setX(width()-20);
main->setBubble(hoverFileName, tableView->viewport()->mapToGlobal(p));
}
}
break;
default:
break;
}
return false;
}
void
RideNavigator::borderMenu(const QPoint &pos)
{
// Which column did we right click on?
//
// if not in the border then do nothing, this
// context menu should only be shown when
// the user right clicks on a column heading.
int column=0;
if (pos.y() <= tableView->header()->height())
column = tableView->header()->logicalIndexAt(pos);
else return; // not in the border
QMenu menu(tableView);
// reset viaual headings first
columnsChanged();
// don't allow user to delete last column!
// need to also include '*' column 0 wide in count hence 2 not 1
if (visualHeadings.count() > 2) {
QAction *delCol = new QAction(tr("Remove Column"), tableView);
delCol->setEnabled(true);
menu.addAction(delCol);
connect(delCol, SIGNAL(triggered()), this, SLOT(removeColumn()));
}
QAction *insCol = new QAction(tr("Column Chooser"), tableView);
insCol->setEnabled(true);
menu.addAction(insCol);
connect(insCol, SIGNAL(triggered()), this, SLOT(showColumnChooser()));
QAction *toggleGroupBy = new QAction(_groupBy >= 0 ? tr("Do Not Show in Groups") : tr("Show In Groups"), tableView);
toggleGroupBy->setEnabled(column!=1?true:false); // No group for Ride Time
menu.addAction(toggleGroupBy);
connect(toggleGroupBy, SIGNAL(triggered()), this, SLOT(setGroupByColumn()));
// set current column...
currentColumn = column;
menu.exec(tableView->mapToGlobal(QPoint(pos.x(), pos.y())));
}
void
RideNavigator::setGroupByColumn()
{
// toggle
setGroupBy(_groupBy >= 0 ? -1 : currentColumn);
// set proxy model
groupByModel->setGroupBy(_groupBy);
// lets expand column 0 for the groupBy heading
for (int i=0; i < groupByModel->groupCount(); i++) {
tableView->setFirstColumnSpanned (i, QModelIndex(), true);
}
// now show em
tableView->expandAll();
// reselect current ride - since selectionmodel
// is changed by setGroupBy()
rideTreeSelectionChanged();
}
void
RideNavigator::setSortBy(int index, Qt::SortOrder order)
{
_sortByIndex = index;
_sortByOrder = static_cast<int>(order);
}
//
// This function is called for every row in the metricDB
// and wants to know what group string or 'name' you want
// to put this row into. It is passed the heading value
// as a string, and the row value for this column.
//
// It should return a string that will be used in the first
// column tree to group rows together.
//
// It is intended to allow us to do clever groupings, such
// as grouping dates as 'This week', 'Last week' etc or
// Power values into zones etc.
//
class groupRange {
public:
class range {
public:
double low, high;
QString name;
range(double low, double high, QString name) : low(low), high(high), name(name) {}
range() : low(0), high(0), name("") {}
};
QString column; // column name
QList<range> ranges; // list of ranges we can put them in
};
static QList<groupRange> groupRanges;
bool
GroupByModel::initGroupRanges()
{
groupRange::range add;
groupRange addColumn;
// TSS
addColumn.column = "TSS";
add = groupRange::range( 0.0, 0.0, tr("Zero or not present")); addColumn.ranges << add;
add = groupRange::range( 0, 150, tr("Low Stress")); addColumn.ranges << add;
add = groupRange::range( 150, 300, tr("Medium Stress")); addColumn.ranges << add;
add = groupRange::range( 300, 450, tr("High Stress")); addColumn.ranges << add;
add = groupRange::range( 450, 0.00, tr("Very High Stress")); addColumn.ranges << add;
groupRanges << addColumn;
addColumn.ranges.clear();
// Intensity Factor
addColumn.column = "IF";
add = groupRange::range( 0.0, 0.0, tr("Zero or not present")); addColumn.ranges << add;
add = groupRange::range( 0.0, 0.55, tr("Active Recovery")); addColumn.ranges << add;
add = groupRange::range( 0.55, 0.75, tr("Endurance")); addColumn.ranges << add;
add = groupRange::range( 0.75, 0.90, tr("Tempo")); addColumn.ranges << add;
add = groupRange::range( 0.90, 1.05, tr("Threshold")); addColumn.ranges << add;
add = groupRange::range( 1.05, 1.2, tr("VO2Max")); addColumn.ranges << add;
add = groupRange::range( 1.2, 1.5, tr("Anaerobic Capacity")); addColumn.ranges << add;
add = groupRange::range( 1.5, 0.0, tr("Maximal")); addColumn.ranges << add;
groupRanges << addColumn;
addColumn.ranges.clear();
// Variability Index
addColumn.column = "VI";
add = groupRange::range( 0.0, 1.0, tr("Zero or not present")); addColumn.ranges << add;
add = groupRange::range( 1.0, 1.05, tr("Isopower")); addColumn.ranges << add;
add = groupRange::range( 1.05, 1.1, tr("Steady")); addColumn.ranges << add;
add = groupRange::range( 1.1, 1.2, tr("Variable")); addColumn.ranges << add;
add = groupRange::range( 1.2, 0.0, tr("Highly Variable")); addColumn.ranges << add;
groupRanges << addColumn;
addColumn.ranges.clear();
// Duration (seconds)
addColumn.column = "Duration";
add = groupRange::range( 0.0, 3600.0, tr("Less than an hour")); addColumn.ranges << add;
add = groupRange::range( 3600, 5400, tr("Less than 90 minutes")); addColumn.ranges << add;
add = groupRange::range( 5400, 10800, tr("Less than 3 hours")); addColumn.ranges << add;
add = groupRange::range( 10800, 18000, tr("Less than 5 hours")); addColumn.ranges << add;
add = groupRange::range( 18000, 0.0, tr("More than 5 hours")); addColumn.ranges << add;
groupRanges << addColumn;
addColumn.ranges.clear();
// Distance (km)
addColumn.column = "Distance";
add = groupRange::range( 0.0, 0.0, tr("Zero or not present")); addColumn.ranges << add;
add = groupRange::range( 0.0, 40.0, tr("Short")); addColumn.ranges << add;
add = groupRange::range( 40, 80.00, tr("Medium")); addColumn.ranges << add;
add = groupRange::range( 80, 140, tr("Long")); addColumn.ranges << add;
add = groupRange::range( 140, 0, tr("Very Long")); addColumn.ranges << add;
groupRanges << addColumn;
addColumn.ranges.clear();
return true;
}
static bool _initGroupRanges = false;
// Perhaps a groupName function on the metrics would be useful
QString
GroupByModel::groupFromValue(QString headingName, QString value, double rank, double count) const
{
if (!_initGroupRanges)
_initGroupRanges = initGroupRanges();
// Check for predefined thresholds / zones / bands for this metric/column
foreach (groupRange orange, groupRanges) {
if (orange.column == headingName) {
double number = value.toDouble();
// use thresholds defined for this column/metric
foreach(groupRange::range range, orange.ranges) {
// 0-x is lower, x-0 is upper, 0-0 is no data and x-x is a range
if (range.low == 0.0 && range.high == 0.0 && number == 0.0) return range.name;
else if (range.high != 0.0 && range.low == 0.0 && number < range.high) return range.name;
else if (range.low != 0.0 && range.high == 0.0 && number >= range.low) return range.name;
else if (number < range.high && number >= range.low) return range.name;
}
return "Undefined";
}
}
// Use upper quartile for anything left that is a metric
if (rideNavigator->columnMetrics.value(headingName, NULL) != NULL) {
double quartile = rank / count;
if (value.toDouble() == 0) return QString("Zero or not present");
else if (rank < 10) return QString("Best 10");
else if (quartile <= 0.25) return QString ("Quartile 1: 0% - 25%");
else if (quartile <= 0.50) return QString ("Quartile 2: 25% - 50%");
else if (quartile <= 0.75) return QString ("Quartile 3: 50% - 75%");
else if (quartile <= 1) return QString ("Quartile 4: 75% - 100%");
} else {
if (headingName == tr("Date")) {
// get the date from value string
QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate);
QDateTime today = QDateTime::currentDateTime();
if (today.date().weekNumber() == dateTime.date().weekNumber()
&& today.date().year() == dateTime.date().year())
return "This week";
else if (today.date().month() == dateTime.date().month()
&& today.date().year() == dateTime.date().year())
return "This month";
else if (today.date().month() == (dateTime.date().month()+1)
&& today.date().year() == dateTime.date().year())
return "Last month";
else {
return dateTime.toString("yyyy-MM (MMMM)");
}
}
// not a metric, i.e. metadata
return value;
}
// if all else fails just return the value and group by
// that. Which is a fair approach for text fields for example
return value;
}
void
RideNavigator::removeColumn()
{
active = true;
tableView->setColumnHidden(currentColumn, true);
active = false;
setWidth(geometry().width()); // calculate width...
columnsChanged(); // need to do after, just once
}
void
RideNavigator::showColumnChooser()
{
ColumnChooser *selector = new ColumnChooser(logicalHeadings);
selector->show();
}
// user double clicked a ride, we need to update the main window ride list
void
RideNavigator::selectRide(const QModelIndex &index)
{
QModelIndex fileIndex = tableView->model()->index(index.row(), 2, index.parent()); // column 2 for filename ?
QString filename = tableView->model()->data(fileIndex, Qt::DisplayRole).toString();
main->selectRideFile(filename);
}
// user cursor moved to ride
void
RideNavigator::cursorRide()
{
if (active == true) return;
else active = true;
selectRide(tableView->currentIndex());
active = false;
}
// fixup selection lost when columns sorted etc
void
RideNavigator::selectRow()
{
// this is fugly and either a bug in QtreeView sorting
// or a bug in our QAbstractProxyModel.
rideTreeSelectionChanged(); // reset from ridelist
}
// the main window ride list changed, we need to reflect it
void
RideNavigator::rideTreeSelectionChanged()
{
if (active == true) return;
else active = true;
QTreeWidgetItem *which;
if (main->rideTreeWidget()->selectedItems().count())
which = main->rideTreeWidget()->selectedItems().first();
else // no rides slected
which = NULL;
if (which && which->type() == RIDE_TYPE) {
RideItem *rideItem = static_cast<RideItem *>(which);
// now find rideItem->fileName in the model
// and then select it in the view!
for (int i=0; i<tableView->model()->rowCount(); i++) {
QModelIndex group = tableView->model()->index(i,0,QModelIndex());
for (int j=0; j<tableView->model()->rowCount(group); j++) {
QString fileName = tableView->model()->data(tableView->model()->index(j,2, group), Qt::DisplayRole).toString();
if (fileName == rideItem->fileName) {
// we set current index to column 2 (date/time) since we can be guaranteed it is always show (all others are removable)
QItemSelection row(tableView->model()->index(j,0,group),
tableView->model()->index(j,tableView->model()->columnCount()-1, group));
tableView->selectionModel()->select(row, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
tableView->selectionModel()->setCurrentIndex(tableView->model()->index(j,0,group), QItemSelectionModel::NoUpdate);
tableView->scrollTo(tableView->model()->index(j,3,group), QAbstractItemView::PositionAtCenter);
active = false;
return;
}
}
}
}
active = false;
}
// Drag and drop columns from the chooser...
void
RideNavigator::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->data("application/x-columnchooser") != "")
event->acceptProposedAction(); // whatever you wanna drop we will try and process!
else
event->ignore();
}
void
RideNavigator::dropEvent(QDropEvent *event)
{
QString name = event->mimeData()->data("application/x-columnchooser");
// fugly, but it works for BikeScore with the (TM) in it...
if (name == "BikeScore?") name = QTextEdit("BikeScore™").toPlainText();
tableView->setColumnHidden(logicalHeadings.indexOf(name), false);
tableView->setColumnWidth(logicalHeadings.indexOf(name), 50);
tableView->header()->moveSection(tableView->header()->visualIndex(logicalHeadings.indexOf(name)), 1);
columnsChanged();
}
NavigatorCellDelegate::NavigatorCellDelegate(RideNavigator *rideNavigator, QObject *parent) :
QItemDelegate(parent), rideNavigator(rideNavigator), pwidth(300)
{
}
// Editing functions are null since the model is read-only
QWidget *NavigatorCellDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const { return NULL; }
void NavigatorCellDelegate::commitAndCloseEditor() { }
void NavigatorCellDelegate::setEditorData(QWidget *, const QModelIndex &) const { }
void NavigatorCellDelegate::updateEditorGeometry(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const {}
void NavigatorCellDelegate::setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const { }
QSize NavigatorCellDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QModelIndex &index) const
{
QSize s;
if (rideNavigator->groupByModel->mapToSource(rideNavigator->sortModel->mapToSource(index)) != QModelIndex() &&
rideNavigator->groupByModel->data(rideNavigator->sortModel->mapToSource(index), Qt::UserRole).toString() != "") {
s.setHeight((rideNavigator->fontHeight+2) * 3);
} else s.setHeight(rideNavigator->fontHeight + 2);
return s;
}
// anomalies are underlined in red, otherwise straight paintjob
void NavigatorCellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// format the cell depending upon what it is...
QString columnName = rideNavigator->tableView->model()->headerData(index.column(), Qt::Horizontal).toString();
const RideMetric *m;
QString value;
if ((m=rideNavigator->columnMetrics.value(columnName, NULL)) != NULL) {
// format as a metric
// get double from sqlmodel
double metricValue = index.model()->data(index, Qt::DisplayRole).toDouble();
if (metricValue) {
// metric / imperial converstion
metricValue *= (rideNavigator->main->useMetricUnits) ? 1 : m->conversion();
metricValue += (rideNavigator->main->useMetricUnits) ? 0 : m->conversionSum();
// format with the right precision
if (m->units(true) == "seconds" || m->units(true) == tr("seconds")) {
value = QTime(0,0,0,0).addSecs(metricValue).toString("hh:mm:ss");
} else {
value = QString("%1").arg(metricValue, 0, 'f', m->precision());
}
} else {
// blank out zero values, they look ugly and are distracting
value = "";
}
} else {
// is this the ride date/time ?
value = index.model()->data(index, Qt::DisplayRole).toString();
if (columnName == tr("Date")) {
QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate);
value = dateTime.toString("MMM d, yyyy"); // same format as ride list
} else if (columnName == tr("Time")) {
QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate);
value = dateTime.toString("hh:mm:ss"); // same format as ride list
} else if (columnName == tr("Last updated")) {
QDateTime dateTime;
dateTime.setTime_t(index.model()->data(index, Qt::DisplayRole).toInt());
value = dateTime.toString("ddd MMM d, yyyy h:mm AP"); // same format as ride list
}
}
QStyleOptionViewItem myOption = option;
// groupBy in bold please
if (columnName == "*") {
QFont enbolden = option.font;
enbolden.setWeight(QFont::Bold);
myOption.font = enbolden;
}
// normal render
QString calendarText = rideNavigator->tableView->model()->data(index, Qt::UserRole).toString();
QBrush background = rideNavigator->tableView->model()->data(index, Qt::BackgroundRole).value<QBrush>();
if (columnName != "*") {
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
painter->fillRect(myOption.rect, background);
// clear first
drawDisplay(painter, myOption, myOption.rect, ""); //added
// draw border of each cell
QPen rpen;
rpen.setWidth(0.5);
rpen.setColor(Qt::lightGray);
painter->setPen(rpen);
painter->drawLine(0,myOption.rect.y(),rideNavigator->pwidth,myOption.rect.y());
painter->drawLine(0,myOption.rect.y()+myOption.rect.height(),rideNavigator->pwidth,myOption.rect.y()+myOption.rect.height());
painter->drawLine(0,myOption.rect.y()+myOption.rect.height(),0,myOption.rect.y()+myOption.rect.height());
// indent first column and draw all in bold
myOption.rect.setHeight(rideNavigator->fontHeight + 2); //added
myOption.font.setWeight(QFont::Bold);
if (myOption.rect.x() == 0) {
QRect indented(myOption.rect.x()+5, myOption.rect.y(), myOption.rect.width()-5, myOption.rect.height());
drawDisplay(painter, myOption, indented, value); //added
} else drawDisplay(painter, myOption, myOption.rect, value); //added
// now get the calendar text to appear ...
if (calendarText != "") {
myOption.rect.setX(0);
myOption.rect.setY(myOption.rect.y() + rideNavigator->fontHeight + 2);//was +23
myOption.rect.setWidth(rideNavigator->pwidth);
myOption.rect.setHeight(rideNavigator->fontHeight * 2); //was 36
myOption.font.setPointSize(myOption.font.pointSize());
myOption.font.setWeight(QFont::Normal);
painter->fillRect(myOption.rect, background);