-
Notifications
You must be signed in to change notification settings - Fork 0
/
rideselectionwindow.cpp
285 lines (237 loc) · 9.56 KB
/
rideselectionwindow.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
#include "rideselectionwindow.h"
#include "datalog.h"
#include "tcxparser.h"
#include "fitparser.h"
#include "dataprocessing.h"
#include "logdirectorysummary.h"
#include "user.h"
#include <QTreeView.h>
#include <QStandardItemModel.h>
#include <QDir.h>
#include <QProgressDialog.h>
#include <QBoxLayout.h>
#include <QLabel.h>
#include <QMessageBox.h>
#include <iostream>
/******************************************************/
RideSelectionWindow::RideSelectionWindow():
_current_data_log(),
_log_dir_summary()
{
// Create dummy model with just the headers
QStandardItemModel* model = new QStandardItemModel;
QStandardItem* header0 = new QStandardItem(QString("Date"));
QStandardItem* header1 = new QStandardItem(QString("Time (min)"));
QStandardItem* header2 = new QStandardItem(QString("Dist (km)"));
model->setHorizontalHeaderItem(0,header0);
model->setHorizontalHeaderItem(1,header1);
model->setHorizontalHeaderItem(2,header2);
// Create and show the tree view
_tree = new QTreeView(this);
_tree->setModel(model);
formatTreeView();
_tree->show();
_head_label = new QLabel;
_head_label->setTextFormat(Qt::RichText);
_head_label->setText("<b>Ride Selector</b>");
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(_head_label);
layout->addWidget(_tree);
setLayout(layout);
setFixedSize(270,290);
// Create parser and setup log directory summary
_tcx_parser = new TcxParser();
_fit_parser = new FitParser();
}
/******************************************************/
RideSelectionWindow::~RideSelectionWindow()
{
}
/******************************************************/
void RideSelectionWindow::formatTreeView()
{
_tree->setAlternatingRowColors(true);
_tree->setColumnHidden(3,true);
_tree->setColumnWidth(0,123);
_tree->setColumnWidth(1,60);
_tree->setColumnWidth(2,60);
_tree->sortByColumn(0,Qt::DescendingOrder);
_tree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
/******************************************************/
void RideSelectionWindow::setUser(boost::shared_ptr<User> user)
{
const QString path = user->logDirectory();
// Read tcx files from specified directory
QDir log_directory;
QStringList filter;
filter << "*.tcx" << "*.fit";
log_directory.setNameFilters(filter);
log_directory.setPath(path);
QStringList filenames = log_directory.entryList();
// Try to load log summary file if it exists
_log_dir_summary.reset(new LogDirectorySummary(path));
_log_dir_summary->readFromFile();
// Compare files in directory with those in summary and remove if already in summary
for (int j=0; j < _log_dir_summary->numLogs(); ++j)
{
QFileInfo file(_log_dir_summary->log(j)._filename);
filenames.removeAll(file.fileName());
}
// Create a small progress bar
QProgressDialog load_progress("Registering new log:", "Cancel", 0, filenames.size()-1, this);
load_progress.setWindowModality(Qt::WindowModal);
load_progress.setMinimumDuration(0); //msec
load_progress.setWindowTitle("RideViewer");
// Load new log files in the directory
std::vector<boost::shared_ptr<DataLog> > data_logs;
for (int i=0; i < filenames.size(); ++i)
{
boost::shared_ptr<DataLog> data_log(new DataLog);
const QString filename_with_path = log_directory.path() + "/" + filenames[i];
if (parse(filename_with_path, data_log))
{
data_logs.push_back(data_log);
_current_data_log = data_log;
}
load_progress.setValue(i);
load_progress.setLabelText("Registering new log: " + filename_with_path);
if (load_progress.wasCanceled())
break;
}
// Add the newly read rides to the summary
_log_dir_summary->addLogsToSummary(data_logs);
_log_dir_summary->writeToFile();
// Display information about the user
_head_label->setText("<b>Ride Selector For: </b>" + user->name() + " (" + QString::number(_log_dir_summary->numLogs()) + " rides)");
populateTableWithRides();
}
/******************************************************/
void RideSelectionWindow::populateTableWithRides()
{
_model = new QStandardItemModel;
QStandardItem *parent_item = _model->invisibleRootItem();
for (int i = 0; i < _log_dir_summary->numLogs(); ++i)
{
// Data and time
QString date = _log_dir_summary->log(i)._date;
date.chop(3); // remove seconds
QStandardItem *ride_name = new QStandardItem(date);
ride_name->setFlags(ride_name->flags() & ~Qt::ItemIsEditable);
// Ride time length
QStandardItem *ride_time = new QStandardItem(DataProcessing::minsFromSecs(_log_dir_summary->log(i)._time));
ride_time->setFlags(ride_time->flags() & ~Qt::ItemIsEditable);
// Ride distance
QStandardItem *ride_dist = new QStandardItem(DataProcessing::kmFromMeters(_log_dir_summary->log(i)._dist));
ride_dist->setFlags(ride_dist->flags() & ~Qt::ItemIsEditable);
// Index of ride in vector of all rides
QStandardItem *ride_index = new QStandardItem(QString::number(i));
QList<QStandardItem*> ride_list;
ride_list << ride_name << ride_time << ride_dist << ride_index;
parent_item->appendRow(ride_list);
if (_log_dir_summary->log(i)._laps.size() > 1) // all rides are 1 lap, so only show laps for rides with > 1 lap
{
for (unsigned int lap = 0; lap < _log_dir_summary->log(i)._laps.size(); ++lap)
{
// Format lap text so they display in numerical order (prepend a 0)
QString lap_number = QString::number(lap+1);
if (_log_dir_summary->log(i)._laps.size() > 10 && lap+1 < 10)
lap_number.prepend('0');
QStandardItem *lap_name = new QStandardItem("Lap " + lap_number);
lap_name->setFlags(lap_name->flags() & ~Qt::ItemIsEditable);
// Compute lap summary info
const double time = _log_dir_summary->log(i)._laps[lap]._time;
const double dist = _log_dir_summary->log(i)._laps[lap]._dist;
QStandardItem *lap_time = new QStandardItem(DataProcessing::minsFromSecs(time));
lap_name->setFlags(lap_name->flags() & ~Qt::ItemIsEditable);
QStandardItem *lap_dist = new QStandardItem(DataProcessing::kmFromMeters(dist));
lap_dist->setFlags(lap_dist->flags() & ~Qt::ItemIsEditable);
// Index of ride in vector of all rides
QStandardItem *lap_index = new QStandardItem(QString::number(lap));
QList<QStandardItem*> lap_list;
lap_list << lap_name << lap_time << lap_dist << lap_index;
ride_name->appendRow(lap_list);
}
}
}
QStandardItem* header0 = new QStandardItem(QString("Date"));
QStandardItem* header1 = new QStandardItem(QString("Time (min)"));
QStandardItem* header2 = new QStandardItem(QString("Dist (km)"));
_model->setHorizontalHeaderItem(0,header0);
_model->setHorizontalHeaderItem(1,header1);
_model->setHorizontalHeaderItem(2,header2);
_tree->setModel(_model);
formatTreeView();
connect(_tree, SIGNAL(clicked(const QModelIndex&)),this,SLOT(rideSelected(const QModelIndex&)));
_tree->setCurrentIndex(_model->index(0,0));
rideSelected(_model->index(0,0)); // display first ride
}
/******************************************************/
void RideSelectionWindow::refresh()
{
if (_log_dir_summary)
{
if (_log_dir_summary->numLogs() > 0)
rideSelected(_tree->currentIndex());
}
}
/******************************************************/
boost::shared_ptr<DataLog> RideSelectionWindow::currentDataLog()
{
return _current_data_log;
}
/******************************************************/
void RideSelectionWindow::rideSelected(const QModelIndex& index)
{
if (index.parent() == QModelIndex()) // user selected the entire ride
{
// Get the item which represents the index
QStandardItem* item = _model->item(index.row(),3); // 4th element is data log index (not displayed)
if (!item)
return;
// Parse complete ride details
if (_current_data_log == 0 ||
_current_data_log->filename() != _log_dir_summary->log(item->text().toInt())._filename)
{
_current_data_log.reset(new DataLog);
if (!parse(_log_dir_summary->log(item->text().toInt())._filename, _current_data_log))
QMessageBox::warning(this, tr("RideViewer"), tr("Log file doesn't exist! Suggest you manually delete it from the logsummary.xml"));
}
// Notify to display the selected ride
emit displayRide(_current_data_log);
}
else // user seletected a lap
{
// Get the item which represents the index
QStandardItem* ride_item = _model->item(index.parent().row(),3); // 4th element is data log index (not displayed)
if (_current_data_log == 0 ||
_current_data_log->filename() != _log_dir_summary->log(ride_item->text().toInt())._filename)
{
_current_data_log.reset(new DataLog);
if (!parse(_log_dir_summary->log(ride_item->text().toInt())._filename, _current_data_log))
QMessageBox::warning(this, tr("RideViewer"), tr("Log file doesn't exist! Suggest you manually delete it from the logsummary.xml"));
// Notify to display the selected ride
emit displayRide(_current_data_log);
}
// Get the item which represents the index
QStandardItem* lap_item = _model->item(index.parent().row())->child(index.row(),3); // 4th element is lap index (not displayed)
// Notif to display the selected lap
emit displayLap(lap_item->text().toInt());
}
}
/******************************************************/
bool RideSelectionWindow::parse(const QString filename, boost::shared_ptr<DataLog> data_log)
{
if (filename.contains(".fit", Qt::CaseInsensitive))
{
return _fit_parser->parse(filename, data_log);
}
else if (filename.contains(".tcx", Qt::CaseInsensitive))
{
return _tcx_parser->parse(filename, data_log);
}
else
{
return false; // unknown log type
}
}