-
Notifications
You must be signed in to change notification settings - Fork 4
/
FTSPrepGUI.cpp
423 lines (383 loc) · 14.1 KB
/
FTSPrepGUI.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
/* FTSPlot - fast time series dataset plotter
Copyright (C) 2013 Michael Riss <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include "FTSPrepGUI.h"
using namespace FTSPlot;
FTSPrepGUI::FTSPrepGUI()
{
ui.setupUi(this);
state = dth_missing;
connect( ui.AddFileButton, SIGNAL(pressed()), this, SLOT(addFileHandler()) );
connect( ui.RemoveFileButton, SIGNAL(pressed()), this, SLOT(RemoveFileHandler()) );
connect( ui.StartStopButton, SIGNAL(pressed()), this, SLOT(StartStopHandlder()) );
connect( ui.ResumeButton, SIGNAL(pressed()), this, SLOT(ResumeHandler()) );
updateGUI();
}
void FTSPrepGUI::setDataProcessor(FTSPrep* dth)
{
this->dth = dth;
state = idle_noFile;
updateGUI();
}
void FTSPrepGUI::addFileHandler()
{
// Spawn filedialog
QStringList tmp = QFileDialog::getOpenFileNames( this,
tr("Open Resume File"), "", tr("Binary Files (*.bin);;All Files (*)"));
for ( int i = 0; i < tmp.size(); i++ )
{
ui.listWidget->addItem(tmp.at(i));
}
// change state
state = idle_File;
updateGUI();
}
void FTSPrepGUI::RemoveFileHandler()
{
// Remove current item in listWidget
QListWidgetItem* tmp = ui.listWidget->takeItem(ui.listWidget->currentRow());
delete( tmp );
if ( ui.listWidget->count() == 0 )
{
state = idle_noFile;
updateGUI();
}
}
void FTSPrepGUI::StartStopHandlder()
{
QMessageBox msgBox;
QFile tmpFile;
quint64 divideFactor;
switch ( state )
{
case idle_File:
// Set up total job file length, index, processed bytes
abortion = false;
totalFileIdx = 0;
FileIdxAcc = 0;
fileIdxs.clear();
fileFactors.clear();
for ( int i = 0; i < ui.listWidget->count(); i++ )
{
tmpFile.setFileName(ui.listWidget->item(i)->text());
if ( !tmpFile.open(QIODevice::ReadOnly) )
{
msgBox.setText( QString( "Error: Cannot open file " ) +
ui.listWidget->item(i)->text() + QString( "!") );
msgBox.exec();
}
quint64 tmpIdx = tmpFile.size();
tmpFile.close();
fileIdxs.append( tmpIdx );
totalFileIdx += tmpIdx;
}
for ( int i = 0; i < ui.listWidget->count(); i++ )
{
fileFactors.append( (double) fileIdxs[i] / (double) totalFileIdx );
}
jobFileIdx = 0;
if ( dth->setNewFile( ui.listWidget->item(jobFileIdx)->text(),
ui.ThinFactorBox->value(), FILESIZELIMIT ) )
{
// Set up total job file length, index, processed bytes
// setup finshed() handler
connect( this, SIGNAL(abortThread()), dth, SLOT(stopforResume()));
connect( dth, SIGNAL(finished()), this, SLOT(handleThreadFinish()) );
dth->setProgressGranularity( 2048 );
ui.totalBar->setMinimum( 0 );
ui.totalBar->setMaximum( 2048 );
ui.fileLabel->setText( QString( "File: " ) + ui.listWidget->item(jobFileIdx)->text() );
ui.FileBar->setMaximum( 0 );
ui.FileBar->setMaximum( 2048 );
ui.reductionBar->setMinimum( 0 );
ui.reductionBar->setMaximum( 2048 );
connect( dth, SIGNAL(progressUpdate(int,int,int)),
this, SLOT(handleProgressUpdate(int,int,int)) );
// start first job
dth->start();
state = Running;
updateGUI();
}
else
{
msgBox.setText("Error: FTSPrep object is not ready!");
msgBox.exec();
}
break;
case Running:
// send a stop signal
// set a flag so that the handleThreadFinish routine knows that it has to
// check for the resume information and write the resume file
abortion = true;
emit abortThread();
break;
default:
qDebug("FTSPrepGUI::StartStopHandlder(): invalid state!");
break;
}
}
void FTSPrepGUI::ResumeHandler()
{
// FileDialog for ResumeFileButton
QString resumeFileName = QFileDialog::getOpenFileName( this,
tr("Open Resume File"), "", tr("Resume Files (*.resume)"));
QFile resumeFile( resumeFileName );
if ( !resumeFile.open( QIODevice::ReadOnly ) )
{
QMessageBox msgBox;
msgBox.setText( QString( "Cannot open resume file " ) + resumeFileName );
msgBox.exec();
return;
}
QTextStream resumeText( &resumeFile );
// read current file, enter into listWidget, save level, position
QString thinFactorString;
QString fileName;
QString levelString;
QString positionString;
resumeText >> thinFactorString;
resumeText >> fileName >> levelString >> positionString;
quint64 level = levelString.toULongLong();
quint64 position = positionString.toULongLong();
ui.ThinFactorBox->setValue( thinFactorString.toULongLong() );
ui.listWidget->clear();
ui.listWidget->addItem( fileName );
// read rest of file listWidget
while ( !resumeText.atEnd() )
{
resumeText >> fileName;
if ( !fileName.isEmpty() )
{
ui.listWidget->addItem( fileName );
}
}
resumeFile.close();
// Set up total job file length, index, processed bytes
abortion = false;
totalFileIdx = 0;
FileIdxAcc = 0;
fileIdxs.clear();
fileFactors.clear();
QFile tmpFile;
for ( int i = 0; i < ui.listWidget->count(); i++ )
{
tmpFile.setFileName(ui.listWidget->item(i)->text());
if ( !tmpFile.open(QIODevice::ReadOnly) )
{
QMessageBox msgBox;
msgBox.setText( QString( "ResumeHandler error: Cannot open file " ) +
ui.listWidget->item(i)->text() + QString( "!") );
msgBox.exec();
return;
}
quint64 tmpIdx = tmpFile.size();
tmpFile.close();
fileIdxs.append( tmpIdx );
totalFileIdx += tmpIdx;
}
for ( int i = 0; i < ui.listWidget->count(); i++ )
{
fileFactors.append( (double) fileIdxs[i] / (double) totalFileIdx );
}
// call setupresumeFile
jobFileIdx = 0;
if ( !dth->setresumeFile( ui.listWidget->item(0)->text(), ui.ThinFactorBox->value(),
FILESIZELIMIT, level, position ) )
{
QMessageBox msgBox;
msgBox.setText( "FTSPrep is not ready." );
msgBox.exec();
return;
}
// connect signals
connect( this, SIGNAL(abortThread()), dth, SLOT(stopforResume()));
connect( dth, SIGNAL(finished()), this, SLOT(handleThreadFinish()) );
dth->setProgressGranularity( 2048 );
ui.totalBar->setMinimum( 0 );
ui.totalBar->setMaximum( 2048 );
ui.fileLabel->setText( QString( "File: " ) + ui.listWidget->item(jobFileIdx)->text() );
ui.FileBar->setMaximum( 0 );
ui.FileBar->setMaximum( 2048 );
ui.reductionBar->setMinimum( 0 );
ui.reductionBar->setMaximum( 2048 );
connect( dth, SIGNAL(progressUpdate(int,int,int)),
this, SLOT(handleProgressUpdate(int,int,int)) );
// start first job
dth->start();
state = Running;
updateGUI();
}
void FTSPrepGUI::handleThreadFinish()
{
QMessageBox msgBox;
// - cleanup old job
if ( abortion )
{
// ask if we should resume
msgBox.setText("Data Thinning aborted.");
msgBox.setInformativeText("Do you want to save the progress into a resume file?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No );
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if( ret == QMessageBox::Yes )
{
QFile ResumeFile;
bool FileTestResult;
do {
// get filename
QString tmp = QFileDialog::getSaveFileName( this,
tr("Save Resume File"), "", tr("Resume Files (*.resume);;All Files (*)"));
// add resume suffix if not existing
if( !tmp.endsWith( ".resume", Qt::CaseInsensitive ))
{
tmp.append( ".resume" );
}
// check if file can be created
ResumeFile.setFileName( tmp );
FileTestResult = !ResumeFile.open(QIODevice::WriteOnly);
if( FileTestResult )
{
msgBox.setText( QString( "Error: Cannot open ResumeFile for writing " ) + tmp );
msgBox.exec();
}
}
while( FileTestResult );
// save resume information
ResumeData resData = dth->getResumeData();
QString resumeText;
resumeText.append( ui.ThinFactorBox->text() );
resumeText.append( "\n" );
resumeText.append( ui.listWidget->item(jobFileIdx)->text() );
resumeText.append( " " );
resumeText.append( QString::number( resData.ResumethinFactor ) );
resumeText.append( " " );
resumeText.append( QString::number( resData.ResumePosition ) );
resumeText.append( "\n" );
for ( int i = jobFileIdx+1; i < ui.listWidget->count(); i++ )
{
resumeText.append( ui.listWidget->item(i)->text() );
resumeText.append( "\n" );
}
QTextStream resumeStream( &ResumeFile );
resumeStream << resumeText;
ResumeFile.close();
}
// - if finished disconnect signals, change state, updateGUI()
disconnect(dth, SIGNAL(progressUpdate(int,int,int)),
this, SLOT(handleProgressUpdate(int,int,int)));
disconnect(dth, SIGNAL(finished()), this, SLOT(handleThreadFinish()));
ui.listWidget->clear();
ui.fileLabel->setText( "File:" );
ui.reductionLevelLabel->setText( "ReductionLevel:" );
state = idle_noFile;
updateGUI();
// quit
return;
}
// - update variables
FileIdxAcc += 2048 * fileFactors[jobFileIdx];
if ( ++jobFileIdx < ui.listWidget->count() )
{
// - if not finished start next job
if ( dth->setNewFile( ui.listWidget->item(jobFileIdx)->text(),
ui.ThinFactorBox->value(), FILESIZELIMIT ) )
{
ui.fileLabel->setText( QString( "File: " ) + ui.listWidget->item(jobFileIdx)->text() );
dth->start();
}
else
{
msgBox.setText("Error: FTSPrep object is not ready!");
msgBox.exec();
}
}
else
{
// - if finished disconnect signals, change state, updateGUI()
disconnect(dth, SIGNAL(progressUpdate(int,int,int)),
this, SLOT(handleProgressUpdate(int,int,int)));
disconnect(dth, SIGNAL(finished()), this, SLOT(handleThreadFinish()));
ui.fileLabel->setText( "File: " );
ui.reductionLevelLabel->setText( "Reductionlevel: " );
ui.listWidget->clear();
state = idle_noFile;
updateGUI();
}
}
void FTSPrepGUI::handleProgressUpdate(int totalProgress, int level, int fileProgress)
{
quint64 tmp = FileIdxAcc + (quint64) ((double) totalProgress * fileFactors[jobFileIdx]);
ui.totalBar->setValue( tmp );
ui.FileBar->setValue( totalProgress);
ui.reductionLevelLabel->setText( QString( "ReductionLevel: " ) + QString::number( level ) );
ui.reductionBar->setValue( fileProgress );
}
void FTSPrepGUI::updateGUI()
{
switch ( state ) {
case dth_missing:
ui.ThinFactorBox->setEnabled(false);
ui.listWidget->setEnabled(false);
ui.AddFileButton->setEnabled(false);
ui.RemoveFileButton->setEnabled(false);
ui.StartStopButton->setText("Start");
ui.StartStopButton->setEnabled(false);
ui.ResumeButton->setEnabled(false);
ui.totalBar->reset();
ui.FileBar->reset();
ui.reductionBar->reset();
break;
case idle_noFile:
ui.ThinFactorBox->setEnabled(true);
ui.listWidget->setEnabled(true);
ui.AddFileButton->setEnabled(true);
ui.RemoveFileButton->setEnabled(false);
ui.StartStopButton->setText("Start");
ui.StartStopButton->setEnabled(false);
ui.ResumeButton->setEnabled(true);
ui.totalBar->reset();
ui.FileBar->reset();
ui.reductionBar->reset();
break;
case idle_File:
ui.ThinFactorBox->setEnabled(true);
ui.listWidget->setEnabled(true);
ui.AddFileButton->setEnabled(true);
ui.RemoveFileButton->setEnabled(true);
ui.StartStopButton->setText("Start");
ui.StartStopButton->setEnabled(true);
ui.ResumeButton->setEnabled(true);
ui.totalBar->reset();
ui.FileBar->reset();
ui.reductionBar->reset();
break;
case Running:
ui.ThinFactorBox->setEnabled(false);
ui.listWidget->setEnabled(false);
ui.AddFileButton->setEnabled(false);
ui.RemoveFileButton->setEnabled(false);
ui.StartStopButton->setText("Stop");
ui.StartStopButton->setEnabled(true);
ui.ResumeButton->setEnabled(false);
break;
default:
qDebug("Invalid GUI state!");
break;
}
}
//#include "FTSPrepGUI.moc"
// kate: indent-mode cstyle; space-indent on; indent-width 0;