[3e4acc]: libkwave / FileProgress.cpp Maximize Restore History

Download this file

FileProgress.cpp    365 lines (310 with data), 10.9 kB

  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
/***************************************************************************
FileProgress.cpp - progress window for loading/saving files
-------------------
begin : Mar 11 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "config.h"
#include <math.h>
#include <QApplication>
#include <QCloseEvent>
#include <QGridLayout>
#include <QLabel>
#include <QProgressBar>
#include <QTimer>
#include <QVBoxLayout>
#include <kpushbutton.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandardguiitem.h>
#include "libkwave/FileProgress.h"
#include "libkwave/Utils.h" // for ms2string
#include "libkwave/MessageBox.h"
//***************************************************************************
FileProgress::FileProgress(QWidget *parent,
const QUrl &url, unsigned int size,
sample_index_t samples, double rate, unsigned int bits,
unsigned int tracks)
:QDialog(parent),
m_url(url),
m_size(size),
m_lbl_url(0),
m_lbl_length(0),
m_progress(0),
m_stat_transfer(0),
m_stat_bytes(0),
m_time(),
m_canceled(true),
m_last_percent(0),
m_bits_per_sample(bits),
m_sample_rate(rate),
m_tracks(tracks)
{
setModal(true);
QString text;
// start the timer now
m_time.start();
// set the caption to the url
setWindowTitle(m_url.toString());
// toplevel uses a vbox layout
QVBoxLayout *top_layout = new QVBoxLayout(this);
Q_ASSERT(top_layout);
if (!top_layout) return;
top_layout->setMargin(10);
top_layout->setSpacing(10);
// sublayout for the lines with the file info
QGridLayout *info_layout = new QGridLayout();
Q_ASSERT(info_layout);
if (!info_layout) return;
info_layout->setSpacing(0);
info_layout->setColumnStretch(0, 0);
info_layout->setColumnStretch(1, 100);
top_layout->addLayout(info_layout);
// label with "source"
if (!addInfoLabel(info_layout, i18n("Source: "), 0, 0)) return;
text = "?";
m_lbl_url = addInfoLabel(info_layout, text, 0, 1);
if (!m_lbl_url) return;
// label with "length"
if (!addInfoLabel(info_layout, i18n("Length: "), 1, 0)) return;
m_lbl_length = addInfoLabel(info_layout, "", 1, 1);
if (!m_lbl_length) return;
setLength(quint64(samples) * quint64(tracks));
// label with "rate:"
if (!addInfoLabel(info_layout, i18n("Sample rate: "), 2, 0)) return;
text = i18n("%1 Samples per second", rate);
if (!addInfoLabel(info_layout, text, 2, 1)) return;
// label with "resolution:"
if (!addInfoLabel(info_layout, i18n("Resolution: "), 3, 0)) return;
text = i18n("%1 Bits per sample", bits);
if (!addInfoLabel(info_layout, text, 3, 1)) return;
// label with "tracks:"
if (!addInfoLabel(info_layout, i18n("Tracks: "), 4, 0)) return;
switch (tracks) {
case 1:
text = i18n("1 (mono)");
break;
case 2:
text = i18n("2 (stereo)");
break;
case 4:
text = i18n("4 (quadro)");
break;
default:
text = text.setNum(tracks);
}
if (!addInfoLabel(info_layout, text, 4, 1)) return;
// progress bar
m_progress = new QProgressBar(this);
Q_ASSERT(m_progress);
if (!m_progress) return;
top_layout->addWidget(m_progress, 0, 0);
m_progress->setMinimum(0);
m_progress->setMaximum(100);
// sublayout for the line with estimated time
QGridLayout *status_layout = new QGridLayout();
Q_ASSERT(status_layout);
if (!status_layout) return;
status_layout->setSpacing(1);
status_layout->setColumnMinimumWidth(1, 20);
top_layout->addLayout(status_layout);
// create the statistic labels
m_stat_transfer = addInfoLabel(status_layout, "-", 1, 0);
if (!m_stat_transfer) return;
m_stat_bytes = addInfoLabel(status_layout, "-", 1, 2);
if (!m_stat_bytes) return;
// some dummy update to get maximum size
updateStatistics(9999999.9, static_cast<double>(99*24*60*60), size);
// now correct the minimum sizes of the statistic entries
m_stat_transfer->adjustSize();
m_stat_transfer->setMinimumWidth(m_stat_transfer->sizeHint().width());
m_stat_bytes->adjustSize();
m_stat_bytes->setMinimumWidth(m_stat_bytes->sizeHint().width());
// right lower edge: the "cancel" button
KPushButton *bt_cancel =
new KPushButton(KStandardGuiItem::cancel(), this);
Q_ASSERT(bt_cancel);
if (!bt_cancel) return;
bt_cancel->setFixedSize(bt_cancel->sizeHint());
bt_cancel->setFocus();
bt_cancel->setShortcut(Qt::Key_Escape);
connect(bt_cancel, SIGNAL(clicked()), this, SLOT(cancel()));
top_layout->addWidget(bt_cancel, 0, Qt::AlignRight);
// activate the layout and show the dialog
top_layout->activate();
setFixedHeight(sizeHint().height());
setMinimumWidth((sizeHint().width() * 110) / 100);
show();
fitUrlLabel();
// now set canceled to false, this dialog is ready for use
m_canceled = false;
}
//***************************************************************************
void FileProgress::resizeEvent(QResizeEvent *)
{
fitUrlLabel();
}
//***************************************************************************
void FileProgress::closeEvent(QCloseEvent *e)
{
Q_ASSERT(e);
if (!e) return;
// if not already cancelled -> ask user to confirm
if (!m_canceled) {
if (Kwave::MessageBox::warningYesNo(this,
i18n("Do you really want to abort the operation?")) !=
KMessageBox::Yes)
{
// the user was wise and said "No"
e->ignore();
return;
} else {
// user pressed "Yes", he should know what he has done
m_canceled = true;
}
}
// default action: accept
e->accept();
}
//***************************************************************************
void FileProgress::fitUrlLabel()
{
if (!m_lbl_url) return;
int width = m_lbl_url->frameRect().width();
QString url = m_url.toString();
m_lbl_url->setText(url);
int todel = 4;
while (m_lbl_url->sizeHint().width() > width) {
// delete characters in the middle of the string
url = m_url.toString();
int len = url.length();
if (len <= todel) break;
url = url.left((len-todel)/2) + "..." +
url.right((len-todel)/2 + 4);
m_lbl_url->setText(url);
todel++;
}
m_lbl_url->adjustSize();
}
//***************************************************************************
QLabel *FileProgress::addInfoLabel(QGridLayout *layout, const QString text,
int row, int col)
{
QLabel *label = new QLabel(this);
Q_ASSERT(label);
if (!label) return 0;
label->setText(text);
label->adjustSize();
label->setFixedHeight(label->sizeHint().height());
label->setMinimumWidth(label->sizeHint().width());
layout->addWidget(label, row, col);
return label;
}
//***************************************************************************
void FileProgress::updateStatistics(double rate, double rest, quint64 pos)
{
QString text;
QString num;
if (!m_stat_transfer) return;
if (!m_stat_bytes) return;
// left: transfer rate and estimated time
num = num.sprintf("%1.1f", rate/1024.0);
int h = static_cast<int>(floor(rest)) / (60*60);
int m = (static_cast<int>(floor(rest)) / 60) % 60;
int s = static_cast<int>(floor(rest)) % 60;
if (h > 23) {
h = 23;
m = s = 59;
}
QTime time(h,m,s,0);
text = i18n("%1 kB/s (%2 remaining)", num, time.toString());
m_stat_transfer->setText(text);
// right: statistic over the transferred bytes
QString num1, num2;
text = i18n("%1 MB of %2 MB done",
num1.sprintf("%1.1f", pos / (1024.0*1024.0)),
num2.sprintf("%1.1f", m_size / (1024.0*1024.0)));
m_stat_bytes->setText(text);
// process events for some short time, otherwise we would
// not have GUI updates and the "cancel" button would not work
QTimer t;
t.setSingleShot(true);
t.start(5);
while (t.isActive()) {
qApp->sendPostedEvents();
qApp->processEvents();
qApp->syncX();
}
}
//***************************************************************************
void FileProgress::setValue(qreal percent)
{
// position is in samples, we need bytes
quint64 pos = percent * qreal(m_size) / qreal(100.0);
setBytePosition(pos);
}
//***************************************************************************
void FileProgress::setBytePosition(quint64 pos)
{
if (!m_progress) return;
// the easiest part: the progress bar and the caption
int percent = static_cast<int>(
static_cast<double>(pos) / static_cast<double>(m_size) * 100.0);
// not enough progress not worth showing ?
if (percent <= m_last_percent) return;
m_last_percent = percent;
if (m_progress->value() != percent) {
QString newcap;
newcap = i18nc(
"%1=Progress in percentage, %2=path to file",
"(%1%) %2",
percent, m_url.toString()
);
setWindowTitle(newcap);
m_progress->setValue(percent);
}
// update the transfer statistics
double seconds = m_time.elapsed() / 1000.0; // [sec]
double rate = pos / seconds; // [bytes/sec]
double rest = 0;
if (rate > 10) {
rest = double(m_size - pos) / rate; // [seconds]
}
updateStatistics(rate, rest, pos);
}
//***************************************************************************
void FileProgress::setLength(quint64 samples)
{
QString text;
// length in samples -> h:m:s
if (m_sample_rate && m_tracks) {
// length in ms
text = Kwave::ms2string(
1000.0 *
qreal(samples / m_tracks) /
qreal(m_sample_rate));
} else {
// fallback if no rate: length in samples
text = i18n("%1 samples", samples);
}
m_lbl_length->setText(text);
}
//***************************************************************************
void FileProgress::cancel()
{
close();
if (m_canceled) emit canceled();
}
//***************************************************************************
#include "FileProgress.moc"
//***************************************************************************
//***************************************************************************