| 153 |
david |
1 |
#include "qmdialoguploadlegtimes.h"
|
|
|
2 |
#include "ui_qmdialoguploadlegtimes.h"
|
|
|
3 |
#include "QTableWidgetItem"
|
|
|
4 |
#include "QFile"
|
|
|
5 |
#include "QString"
|
|
|
6 |
#include "QFileDialog"
|
|
|
7 |
#include "mainwindow.h"
|
|
|
8 |
#include "QTextStream"
|
|
|
9 |
#include "timedelegate.h"
|
|
|
10 |
|
|
|
11 |
#include "consts.h"
|
|
|
12 |
#include "structs.h"
|
|
|
13 |
#include "proto.h"
|
|
|
14 |
|
|
|
15 |
QmDialogUploadLegTimes::QmDialogUploadLegTimes(QWidget *parent) :
|
|
|
16 |
QDialog(parent),
|
|
|
17 |
ui(new Ui::QmDialogUploadLegTimes)
|
|
|
18 |
{
|
|
|
19 |
ui->setupUi(this);
|
|
|
20 |
ui->legNumber->setMaximum(config.num_legs);
|
|
|
21 |
|
|
|
22 |
// Connect up buttons
|
|
|
23 |
connect(ui->load, SIGNAL(clicked()), this, SLOT(load()));
|
|
|
24 |
connect(ui->cancel,SIGNAL(clicked()), this, SLOT(close()));
|
| 155 |
- |
25 |
connect(ui->update,SIGNAL(clicked()), this, SLOT(update()));
|
|
|
26 |
connect(ui->deltaTime, SIGNAL(valueChanged(int)), this, SLOT(updateDeltaDisplay(int)));
|
| 153 |
david |
27 |
|
|
|
28 |
|
|
|
29 |
// Set up the table
|
|
|
30 |
ui->tableWidget->setColumnCount(5);
|
|
|
31 |
|
|
|
32 |
QStringList labels;
|
| 154 |
david |
33 |
labels << "Team" << "Delta" << "Time" << "Time" << "Time";
|
| 153 |
david |
34 |
ui->tableWidget->setHorizontalHeaderLabels(labels);
|
|
|
35 |
|
| 157 |
david |
36 |
ui->tableWidget->setItemDelegateForColumn(1, new timeDelegate());
|
| 153 |
david |
37 |
ui->tableWidget->setItemDelegateForColumn(2, new timeDelegate());
|
|
|
38 |
ui->tableWidget->setItemDelegateForColumn(3, new timeDelegate());
|
|
|
39 |
ui->tableWidget->setItemDelegateForColumn(4, new timeDelegate());
|
| 155 |
- |
40 |
|
|
|
41 |
ui->status->setText("Load Leg File");
|
| 153 |
david |
42 |
}
|
|
|
43 |
|
|
|
44 |
QmDialogUploadLegTimes::~QmDialogUploadLegTimes()
|
|
|
45 |
{
|
|
|
46 |
delete ui;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
void QmDialogUploadLegTimes::load(void)
|
|
|
50 |
{
|
|
|
51 |
QString fileName = QFileDialog::getOpenFileName(this, tr("Load File"),
|
| 174 |
- |
52 |
filepath,
|
| 153 |
david |
53 |
tr("Data (*.leg);;All (*.*)"),
|
|
|
54 |
0,
|
|
|
55 |
QFileDialog::ReadOnly);
|
|
|
56 |
if ( fileName.isEmpty() )
|
|
|
57 |
{
|
|
|
58 |
MainWindow::showMessage("No File Specified");
|
|
|
59 |
return;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// Open the file
|
|
|
63 |
|
|
|
64 |
QFile file(fileName);
|
|
|
65 |
if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) )
|
|
|
66 |
{
|
|
|
67 |
MainWindow::showMessage("Cannot open Leg Data file");
|
|
|
68 |
return;
|
|
|
69 |
}
|
| 155 |
- |
70 |
ui->status->setText("Loading Data");
|
| 153 |
david |
71 |
// Insert column headers
|
|
|
72 |
ui->tableWidget->clearContents();
|
|
|
73 |
ui->tableWidget->setRowCount(0);
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
// Process Each line of the file
|
|
|
77 |
// Format is TeamNumber Time
|
|
|
78 |
QTextStream in(&file);
|
|
|
79 |
while (!in.atEnd())
|
|
|
80 |
{
|
|
|
81 |
QString line = in.readLine();
|
|
|
82 |
line = line.trimmed(); // Remove leading and training White Space
|
|
|
83 |
QStringList parts = line.split(" ");
|
|
|
84 |
QString first = parts.value(0);
|
|
|
85 |
|
|
|
86 |
bool ok;
|
|
|
87 |
int team = first.toInt(&ok);
|
|
|
88 |
if ( ! ok || team <= 0 )
|
|
|
89 |
continue;
|
|
|
90 |
if (team > ui->tableWidget->rowCount())
|
|
|
91 |
{
|
|
|
92 |
ui->tableWidget->setRowCount(team);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
QTableWidgetItem *item = ui->tableWidget->item(team-1,0);
|
|
|
96 |
if (!item)
|
|
|
97 |
{
|
|
|
98 |
item = new QTableWidgetItem(QString::number(team));
|
|
|
99 |
item->setFlags(item->flags() & ~(Qt::ItemIsEditable|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled));
|
|
|
100 |
ui->tableWidget->setItem(team-1,0,item);
|
| 154 |
david |
101 |
item->setData(Qt::ToolTipRole,"Team Number");
|
|
|
102 |
|
|
|
103 |
item = new QTableWidgetItem("");
|
|
|
104 |
item->setFlags(item->flags() & ~(Qt::ItemIsEditable|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled));
|
|
|
105 |
ui->tableWidget->setItem(team-1,1,item);
|
|
|
106 |
item->setData(Qt::ToolTipRole,"Max delta");
|
| 153 |
david |
107 |
}
|
|
|
108 |
|
| 154 |
david |
109 |
// Insert the time at the first available slot
|
|
|
110 |
// Scan for max an min as we go
|
| 153 |
david |
111 |
int ii;
|
|
|
112 |
QTableWidgetItem *titem = NULL;
|
| 154 |
david |
113 |
QTime ltime = QTime::fromString(parts.value(1),"hh:mm:ss");
|
|
|
114 |
unsigned int lsecs = QTime(0,0,0).secsTo(ltime);
|
|
|
115 |
unsigned int min_time = lsecs;
|
|
|
116 |
unsigned int max_time = lsecs;
|
|
|
117 |
|
|
|
118 |
for (ii=2 ; ii < ui->tableWidget->columnCount(); ii++)
|
| 153 |
david |
119 |
{
|
|
|
120 |
titem = ui->tableWidget->item(team-1,ii);
|
|
|
121 |
if (! titem)
|
|
|
122 |
{
|
|
|
123 |
break;
|
|
|
124 |
}
|
| 154 |
david |
125 |
|
|
|
126 |
QTime entry = titem->data(Qt::EditRole).toTime();
|
|
|
127 |
unsigned int secs = QTime(0,0,0).secsTo(entry);
|
|
|
128 |
if ( secs < min_time )
|
|
|
129 |
min_time = secs;
|
|
|
130 |
if (secs > max_time)
|
|
|
131 |
max_time = secs;
|
| 153 |
david |
132 |
}
|
| 154 |
david |
133 |
titem = new QTableWidgetItem();
|
|
|
134 |
titem->setData(Qt::EditRole, ltime);
|
| 153 |
david |
135 |
ui->tableWidget->setItem(team-1,ii,titem);
|
|
|
136 |
|
| 154 |
david |
137 |
int delta = max_time - min_time;
|
|
|
138 |
if ( delta )
|
|
|
139 |
{
|
|
|
140 |
ui->tableWidget->item(team-1,1)->setData(Qt::EditRole, QTime().addSecs(delta));
|
|
|
141 |
}
|
| 153 |
david |
142 |
}
|
|
|
143 |
|
| 155 |
- |
144 |
// Update Error information
|
|
|
145 |
updateDeltaDisplay( ui->deltaTime->value() );
|
|
|
146 |
|
| 153 |
david |
147 |
}
|
| 155 |
- |
148 |
|
|
|
149 |
void QmDialogUploadLegTimes::updateDeltaDisplay( int delta_secs )
|
|
|
150 |
{
|
|
|
151 |
bool isFlagged = false;
|
|
|
152 |
QTime delta = QTime().addSecs(delta_secs);
|
|
|
153 |
for ( int ii = 0; ii < ui->tableWidget->rowCount(); ii++)
|
|
|
154 |
{
|
|
|
155 |
QTableWidgetItem *item;
|
|
|
156 |
item = ui->tableWidget->item(ii, 1);
|
|
|
157 |
if (item)
|
|
|
158 |
{
|
|
|
159 |
QTime time = item->data(Qt::EditRole).toTime();
|
|
|
160 |
if ( time > delta )
|
|
|
161 |
{
|
|
|
162 |
item->setBackgroundColor(QColor(255,0,0,50));
|
|
|
163 |
isFlagged = true;
|
|
|
164 |
}
|
|
|
165 |
else
|
|
|
166 |
{
|
|
|
167 |
item->setBackgroundColor(QColor(255,255,255));
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
if (isFlagged)
|
|
|
172 |
{
|
|
|
173 |
ui->status->setText("Large Delta Times");
|
|
|
174 |
}
|
|
|
175 |
else
|
|
|
176 |
{
|
|
|
177 |
ui->status->setText("");
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
void QmDialogUploadLegTimes::update(void)
|
|
|
182 |
{
|
| 170 |
- |
183 |
team_type team_buf;
|
| 155 |
- |
184 |
int leg = ui->legNumber->value() ;
|
|
|
185 |
if (leg <= 0 || leg > config.num_legs)
|
|
|
186 |
{
|
|
|
187 |
ui->status->setText("Must select leg number");
|
|
|
188 |
return;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// Insert the first time into the desired leg
|
|
|
192 |
for ( int ii = 0; ii < ui->tableWidget->rowCount(); ii++)
|
|
|
193 |
{
|
|
|
194 |
QTableWidgetItem *item;
|
|
|
195 |
item = ui->tableWidget->item(ii, 0);
|
|
|
196 |
if (item)
|
|
|
197 |
{
|
|
|
198 |
int team = item->data(Qt::EditRole).toInt();
|
|
|
199 |
if( valid_field( team ) && g_record( team, &team_buf ) )
|
|
|
200 |
{
|
|
|
201 |
item = ui->tableWidget->item(ii, 2);
|
|
|
202 |
if (item)
|
|
|
203 |
{
|
|
|
204 |
QTime time = item->data(Qt::EditRole).toTime();
|
|
|
205 |
int secs = QTime(0,0,0).secsTo(time);
|
|
|
206 |
|
|
|
207 |
if ( ui->legStart->isChecked())
|
|
|
208 |
{
|
|
|
209 |
team_buf.leg[leg].start = secs;
|
|
|
210 |
team_buf.leg[leg].manual = TRUE;
|
|
|
211 |
}
|
|
|
212 |
else
|
|
|
213 |
{
|
|
|
214 |
team_buf.leg[leg].end = secs;
|
|
|
215 |
}
|
|
|
216 |
set_times( &team_buf ); /* Calc start of next leg */
|
|
|
217 |
test_times( &team_buf, 0 ); /* Calc elapsed times etc */
|
|
|
218 |
put_team_record( team, &team_buf );
|
|
|
219 |
ui->tableWidget->hideRow(ii);
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
}
|