kwave  18.07.70
Kwave::FileProgress Class Reference

#include <FileProgress.h>

Inheritance diagram for Kwave::FileProgress:
Inheritance graph
Collaboration diagram for Kwave::FileProgress:
Collaboration graph

Public Slots

void setValue (qreal percent)
 
void setBytePosition (quint64 pos)
 
void setLength (quint64 samples)
 

Signals

void canceled ()
 

Public Member Functions

 FileProgress (QWidget *parent, const QUrl &url, quint64 size, sample_index_t samples, double rate, unsigned int bits, unsigned int tracks)
 
virtual ~FileProgress () Q_DECL_OVERRIDE
 
bool isCanceled ()
 

Protected Slots

void cancel ()
 

Protected Member Functions

virtual void resizeEvent (QResizeEvent *) Q_DECL_OVERRIDE
 
virtual void closeEvent (QCloseEvent *e) Q_DECL_OVERRIDE
 
void fitUrlLabel ()
 
QLabel * addInfoLabel (QGridLayout *layout, const QString text, int row, int column)
 
void updateStatistics (double rate, double rest, quint64 pos)
 

Protected Attributes

QUrl m_url
 
quint64 m_size
 
QLabel * m_lbl_url
 
QLabel * m_lbl_length
 
QProgressBar * m_progress
 
QLabel * m_stat_transfer
 
QLabel * m_stat_bytes
 
QTime m_time
 
bool m_canceled
 
int m_last_percent
 
unsigned int m_bits_per_sample
 
double m_sample_rate
 
unsigned int m_tracks
 

Detailed Description

Definition at line 41 of file FileProgress.h.

Constructor & Destructor Documentation

◆ FileProgress()

Kwave::FileProgress::FileProgress ( QWidget *  parent,
const QUrl &  url,
quint64  size,
sample_index_t  samples,
double  rate,
unsigned int  bits,
unsigned int  tracks 
)

Constructor

Parameters
parentthe parent widget
urlthe URL of the file
sizethe size of the file in bytes
samplesthe number of samples
ratesample rate in samples per second
bitsnumber of bits per sample
tracksnumber of tracks

Definition at line 44 of file FileProgress.cpp.

References _, addInfoLabel(), cancel(), Kwave::connect(), fitUrlLabel(), m_canceled, m_lbl_length, m_lbl_url, m_progress, m_stat_bytes, m_stat_transfer, m_time, m_url, setLength(), and updateStatistics().

48  :QDialog(parent),
49  m_url(url),
50  m_size(size),
51  m_lbl_url(Q_NULLPTR),
52  m_lbl_length(Q_NULLPTR),
53  m_progress(Q_NULLPTR),
54  m_stat_transfer(Q_NULLPTR),
55  m_stat_bytes(Q_NULLPTR),
56  m_time(),
57  m_canceled(true),
58  m_last_percent(0),
59  m_bits_per_sample(bits),
60  m_sample_rate(rate),
61  m_tracks(tracks)
62 {
63  setModal(true);
64 
65  QString text;
66 
67  // start the timer now
68  m_time.start();
69 
70  // set the caption to the url
71  setWindowTitle(m_url.toString());
72 
73  // toplevel uses a vbox layout
74  QVBoxLayout *top_layout = new QVBoxLayout(this);
75  Q_ASSERT(top_layout);
76  if (!top_layout) return;
77  top_layout->setMargin(10);
78  top_layout->setSpacing(10);
79 
80  // sublayout for the lines with the file info
81  QGridLayout *info_layout = new QGridLayout();
82  Q_ASSERT(info_layout);
83  if (!info_layout) return;
84  info_layout->setSpacing(0);
85  info_layout->setColumnStretch(0, 0);
86  info_layout->setColumnStretch(1, 100);
87  top_layout->addLayout(info_layout);
88 
89  // label with "file"
90  if (!addInfoLabel(info_layout,
91  i18nc("file progress dialog", "File: "), 0, 0)) return;
92  text = _("?");
93  m_lbl_url = addInfoLabel(info_layout, text, 0, 1);
94  if (!m_lbl_url) return;
95 
96  // label with "length"
97  if (!addInfoLabel(info_layout,
98  i18nc("file progress dialog", "Length: "), 1, 0)) return;
99 
100  m_lbl_length = addInfoLabel(info_layout, _(""), 1, 1);
101  if (!m_lbl_length) return;
102  setLength(quint64(samples) * quint64(tracks));
103 
104  // label with "rate:"
105  if (!addInfoLabel(info_layout,
106  i18nc("file progress dialog", "Sample Rate: "), 2, 0)) return;
107  text = i18nc("file progress dialog, %1=number of samples per second",
108  "%1 Samples per second", rate);
109  if (!addInfoLabel(info_layout, text, 2, 1)) return;
110 
111  // label with "resolution:"
112  if (!addInfoLabel(info_layout,
113  i18nc("file progress dialog", "Resolution: "), 3, 0)) return;
114  text = i18nc("file progress dialog, "
115  "%1=number of bits per sample (8, 16, 24...)",
116  "%1 Bits per sample", bits);
117  if (!addInfoLabel(info_layout, text, 3, 1)) return;
118 
119  // label with "tracks:"
120  if (!addInfoLabel(info_layout,
121  i18nc("file progress dialog", "Tracks: "), 4, 0)) return;
122  switch (tracks) {
123  case 1:
124  text = i18nc("number of tracks", "1 (mono)");
125  break;
126  case 2:
127  text = i18nc("number of tracks", "2 (stereo)");
128  break;
129  case 4:
130  text = i18nc("number of tracks", "4 (quadro)");
131  break;
132  default:
133  text = text.setNum(tracks);
134  }
135  if (!addInfoLabel(info_layout, text, 4, 1)) return;
136 
137  // progress bar
138  m_progress = new QProgressBar(this);
139  Q_ASSERT(m_progress);
140  if (!m_progress) return;
141  top_layout->addWidget(m_progress, 0);
142  m_progress->setMinimum(0);
143  m_progress->setMaximum(100);
144 
145  // sublayout for the line with estimated time
146  QGridLayout *status_layout = new QGridLayout();
147  Q_ASSERT(status_layout);
148  if (!status_layout) return;
149  status_layout->setSpacing(1);
150  status_layout->setColumnMinimumWidth(1, 20);
151  top_layout->addLayout(status_layout);
152 
153  // create the statistic labels
154  m_stat_transfer = addInfoLabel(status_layout, _("-"), 1, 0);
155  if (!m_stat_transfer) return;
156  m_stat_bytes = addInfoLabel(status_layout, _("-"), 1, 2);
157  if (!m_stat_bytes) return;
158 
159  // some dummy update to get maximum size
160  updateStatistics(9999999.9, static_cast<double>(99*24*60*60), size);
161 
162  // now correct the minimum sizes of the statistic entries
163  m_stat_transfer->adjustSize();
164  m_stat_transfer->setMinimumWidth(m_stat_transfer->sizeHint().width());
165  m_stat_bytes->adjustSize();
166  m_stat_bytes->setMinimumWidth(m_stat_bytes->sizeHint().width());
167 
168  // right lower edge: the "cancel" button
169  QPushButton *bt_cancel = new QPushButton(this);
170  Q_ASSERT(bt_cancel);
171  if (!bt_cancel) return;
172  KGuiItem::assign(bt_cancel, KStandardGuiItem::cancel());
173  bt_cancel->setFixedSize(bt_cancel->sizeHint());
174  bt_cancel->setFocus();
175  bt_cancel->setShortcut(Qt::Key_Escape);
176  connect(bt_cancel, SIGNAL(clicked()), this, SLOT(cancel()));
177  top_layout->addWidget(bt_cancel, 0, Qt::AlignRight);
178 
179  // activate the layout and show the dialog
180  top_layout->activate();
181  setFixedHeight(sizeHint().height());
182  setMinimumWidth((sizeHint().width() * 110) / 100);
183 
184  show();
185  fitUrlLabel();
186 
187  // now set canceled to false, this dialog is ready for use
188  m_canceled = false;
189 }
QProgressBar * m_progress
Definition: FileProgress.h:162
QLabel * m_stat_transfer
Definition: FileProgress.h:165
void setLength(quint64 samples)
void updateStatistics(double rate, double rest, quint64 pos)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
unsigned int m_bits_per_sample
Definition: FileProgress.h:180
QLabel * addInfoLabel(QGridLayout *layout, const QString text, int row, int column)
#define _(m)
Definition: memcpy.c:66
unsigned int m_tracks
Definition: FileProgress.h:186
Here is the call graph for this function:

◆ ~FileProgress()

virtual Kwave::FileProgress::~FileProgress ( )
inlinevirtual

Destructor

Definition at line 61 of file FileProgress.h.

61 {}

Member Function Documentation

◆ addInfoLabel()

QLabel * Kwave::FileProgress::addInfoLabel ( QGridLayout *  layout,
const QString  text,
int  row,
int  column 
)
protected

Adds a label to an info field. Used within the constructor.

Parameters
layoutthe QGridLayout with the labels
textthe content of the label, localized
rowthe row within the layout [0...rows-1]
columnthe column within the layout [0...columns-1]
Returns
the label if successful, 0 if failed

Definition at line 246 of file FileProgress.cpp.

Referenced by FileProgress().

248 {
249  QLabel *label = new QLabel(this);
250  Q_ASSERT(label);
251  if (!label) return Q_NULLPTR;
252 
253  label->setText(text);
254  label->adjustSize();
255  label->setFixedHeight(label->sizeHint().height());
256  label->setMinimumWidth(label->sizeHint().width());
257 
258  layout->addWidget(label, row, col);
259 
260  return label;
261 }
Here is the caller graph for this function:

◆ cancel

void Kwave::FileProgress::cancel ( )
protectedslot

Connected to the "cancel" button to set the "m_canceled" flag if the user wants to abort.

Definition at line 372 of file FileProgress.cpp.

References canceled(), and m_canceled.

Referenced by FileProgress().

373 {
374  close();
375  if (m_canceled) emit canceled();
376 }
Here is the caller graph for this function:

◆ canceled

void Kwave::FileProgress::canceled ( )
signal

Emitted when the user has pressed the Cancel button and has confirmed with "Yes".

Referenced by cancel().

Here is the caller graph for this function:

◆ closeEvent()

void Kwave::FileProgress::closeEvent ( QCloseEvent *  e)
protectedvirtual

Called if the window is to be closed.

Definition at line 198 of file FileProgress.cpp.

References m_canceled, and Kwave::MessageBox::warningYesNo().

199 {
200  Q_ASSERT(e);
201  if (!e) return;
202 
203  // if not already cancelled -> ask user to confirm
204  if (!m_canceled) {
206  i18n("Do you really want to abort the operation?")) !=
207  KMessageBox::Yes)
208  {
209  // the user was wise and said "No"
210  e->ignore();
211  return;
212  } else {
213  // user pressed "Yes", he should know what he has done
214  m_canceled = true;
215  }
216  }
217 
218  // default action: accept
219  e->accept();
220 }
static int warningYesNo(QWidget *widget, QString message, QString caption=QString(), const QString buttonYes=QString(), const QString buttonNo=QString(), const QString &dontAskAgainName=QString())
Definition: MessageBox.cpp:93
Here is the call graph for this function:

◆ fitUrlLabel()

void Kwave::FileProgress::fitUrlLabel ( )
protected

Fits the URL text into the available area, with shortening it if necessary.

Definition at line 223 of file FileProgress.cpp.

References _, m_lbl_url, and m_url.

Referenced by FileProgress(), and resizeEvent().

224 {
225  if (!m_lbl_url) return;
226 
227  int width = m_lbl_url->frameRect().width();
228  QString url = m_url.toString();
229  m_lbl_url->setText(url);
230  int todel = 4;
231  while (m_lbl_url->sizeHint().width() > width) {
232  // delete characters in the middle of the string
233  url = m_url.toString();
234  int len = url.length();
235  if (len <= todel) break;
236  url = url.left((len-todel)/2) + _("...") +
237  url.right((len-todel)/2 + 4);
238  m_lbl_url->setText(url);
239  todel++;
240  }
241 
242  m_lbl_url->adjustSize();
243 }
#define _(m)
Definition: memcpy.c:66
Here is the caller graph for this function:

◆ isCanceled()

bool Kwave::FileProgress::isCanceled ( )
inline

Returns true if the dialog is unusable or the user has pressed the "cancel" button.

Definition at line 67 of file FileProgress.h.

Referenced by Kwave::SignalManager::save().

67 { return m_canceled; }
Here is the caller graph for this function:

◆ resizeEvent()

void Kwave::FileProgress::resizeEvent ( QResizeEvent *  )
protectedvirtual

Fits again the URL label on resize events.

See also
fitUrlLabel()

Definition at line 192 of file FileProgress.cpp.

References fitUrlLabel().

193 {
194  fitUrlLabel();
195 }
Here is the call graph for this function:

◆ setBytePosition

void Kwave::FileProgress::setBytePosition ( quint64  pos)
slot

Like setValue, but takes position in bytes as argument

Parameters
posposition within the file, in bytes [0...m_size-1]

Definition at line 316 of file FileProgress.cpp.

References m_last_percent, m_progress, m_size, m_time, m_url, Kwave::toInt(), and updateStatistics().

Referenced by Kwave::SignalManager::loadFile(), and setValue().

317 {
318  if (!m_progress) return;
319  if (pos > m_size) pos = m_size;
320 
321  // the easiest part: the progress bar and the caption
322  int percent = Kwave::toInt(
323  (static_cast<double>(pos) / static_cast<double>(m_size)) * 100.0);
324 
325  // not enough progress not worth showing ?
326  if (percent <= m_last_percent) return;
327  m_last_percent = percent;
328 
329  if (m_progress->value() != percent) {
330  QString newcap;
331  newcap = i18nc(
332  "%1=Progress in percentage, %2=path to file",
333  "(%1%) %2",
334  percent, m_url.toString()
335  );
336  setWindowTitle(newcap);
337 
338  m_progress->setValue(percent);
339  }
340 
341  // update the transfer statistics
342  double seconds = m_time.elapsed() / 1000.0; // [sec]
343  double rate = pos / seconds; // [bytes/sec]
344  double rest = 0;
345  if (rate > 10) {
346  rest = static_cast<double>(m_size - pos) / rate; // [seconds]
347  }
348  updateStatistics(rate, rest, pos);
349 }
QProgressBar * m_progress
Definition: FileProgress.h:162
void updateStatistics(double rate, double rest, quint64 pos)
int toInt(T x)
Definition: Utils.h:127
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLength

void Kwave::FileProgress::setLength ( quint64  samples)
slot

Updates the length information, needed in stream mode.

Parameters
samplestotal number of samples, must be divided through the number of tracks for getting the real length

Definition at line 352 of file FileProgress.cpp.

References m_lbl_length, m_sample_rate, m_tracks, and Kwave::ms2string().

Referenced by FileProgress(), and Kwave::SignalManager::loadFile().

353 {
354  QString text;
355 
356  // length in samples -> h:m:s
357  if ((m_sample_rate > 0) && m_tracks) {
358  // length in ms
359  text = Kwave::ms2string(
360  1000.0 *
361  qreal(samples / m_tracks) /
362  qreal(m_sample_rate));
363  } else {
364  // fallback if no rate: length in samples
365  text = i18nc("file progress dialog, %1=a number of samples",
366  "%1 samples", samples);
367  }
368  m_lbl_length->setText(text);
369 }
QString Q_DECL_EXPORT ms2string(double ms, int precision=6)
Definition: Utils.cpp:66
unsigned int m_tracks
Definition: FileProgress.h:186
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setValue

void Kwave::FileProgress::setValue ( qreal  percent)
slot

Advances the progress to a given position within the file.

Parameters
percentposition within the file, in percent

Definition at line 308 of file FileProgress.cpp.

References m_size, and setBytePosition().

309 {
310  // position is in samples, we need bytes
311  quint64 pos = static_cast<quint64>(percent * qreal(m_size) / 100.0);
312  setBytePosition(pos);
313 }
void setBytePosition(quint64 pos)
Here is the call graph for this function:

◆ updateStatistics()

void Kwave::FileProgress::updateStatistics ( double  rate,
double  rest,
quint64  pos 
)
protected

Updates the statistics of the transferred bytes and transfer rate.

Parameters
ratetransfer rate in kilobytes per seconds
restremaining time in seconds
posposition in the file

Definition at line 264 of file FileProgress.cpp.

References m_size, m_stat_bytes, and m_stat_transfer.

Referenced by FileProgress(), and setBytePosition().

266 {
267  QString text;
268  QString num;
269 
270  if (!m_stat_transfer) return;
271  if (!m_stat_bytes) return;
272 
273  // left: transfer rate and estimated time
274  num = num.sprintf("%1.1f", rate / 1024.0);
275 
276  quint64 ms = qMin(quint64(rest * 1000.0), 24ULL * 60ULL * 60ULL * 1000ULL);
277  text = i18nc("file progress dialog, "
278  "%1=transfer rate, %2=remaining duration",
279  "%1 kB/s (%2 remaining)", num,
280  KFormat().formatDecimalDuration(ms));
281  m_stat_transfer->setText(text);
282 
283  // right: statistic over the transferred bytes
284  QString num1, num2;
285  text = i18nc("file progress dialog, "
286  "%1=number of loaded/saved megabytes, "
287  "%2=number of total megabytes to load or save",
288  "%1 MB of %2 MB done",
289  num1.sprintf("%1.1f", pos / (1024.0 * 1024.0)),
290  num2.sprintf("%1.1f", m_size / (1024.0 * 1024.0)));
291  m_stat_bytes->setText(text);
292 
293  // process events for some short time, otherwise we would
294  // not have GUI updates and the "cancel" button would not work
295  // check: this must be called from the GUI thread only!
296  Q_ASSERT(this->thread() == QThread::currentThread());
297  Q_ASSERT(this->thread() == qApp->thread());
298  QTimer t;
299  t.setSingleShot(true);
300  t.start(5);
301  while (t.isActive()) {
302  qApp->sendPostedEvents();
303  qApp->processEvents();
304  }
305 }
QLabel * m_stat_transfer
Definition: FileProgress.h:165
Here is the caller graph for this function:

Member Data Documentation

◆ m_bits_per_sample

unsigned int Kwave::FileProgress::m_bits_per_sample
protected

number of bits per sample

Definition at line 180 of file FileProgress.h.

◆ m_canceled

bool Kwave::FileProgress::m_canceled
protected

true if the dialog is unusable or canceled by the user

Definition at line 174 of file FileProgress.h.

Referenced by cancel(), closeEvent(), and FileProgress().

◆ m_last_percent

int Kwave::FileProgress::m_last_percent
protected

last displayed percent value

Definition at line 177 of file FileProgress.h.

Referenced by setBytePosition().

◆ m_lbl_length

QLabel* Kwave::FileProgress::m_lbl_length
protected

label with length info, changes in streaming mode

Definition at line 159 of file FileProgress.h.

Referenced by FileProgress(), and setLength().

◆ m_lbl_url

QLabel* Kwave::FileProgress::m_lbl_url
protected

label with the url, shortened when too long

Definition at line 156 of file FileProgress.h.

Referenced by FileProgress(), and fitUrlLabel().

◆ m_progress

QProgressBar* Kwave::FileProgress::m_progress
protected

progress bar

Definition at line 162 of file FileProgress.h.

Referenced by FileProgress(), and setBytePosition().

◆ m_sample_rate

double Kwave::FileProgress::m_sample_rate
protected

number of samples per second, used for output

Definition at line 183 of file FileProgress.h.

Referenced by setLength().

◆ m_size

quint64 Kwave::FileProgress::m_size
protected

size of the file [Bytes]

Definition at line 153 of file FileProgress.h.

Referenced by setBytePosition(), setValue(), and updateStatistics().

◆ m_stat_bytes

QLabel* Kwave::FileProgress::m_stat_bytes
protected

label with progress statistics

Definition at line 168 of file FileProgress.h.

Referenced by FileProgress(), and updateStatistics().

◆ m_stat_transfer

QLabel* Kwave::FileProgress::m_stat_transfer
protected

label with transfer statistics

Definition at line 165 of file FileProgress.h.

Referenced by FileProgress(), and updateStatistics().

◆ m_time

QTime Kwave::FileProgress::m_time
protected

start time, set on initialization of this dialog

Definition at line 171 of file FileProgress.h.

Referenced by FileProgress(), and setBytePosition().

◆ m_tracks

unsigned int Kwave::FileProgress::m_tracks
protected

number of tracks

Definition at line 186 of file FileProgress.h.

Referenced by setLength().

◆ m_url

QUrl Kwave::FileProgress::m_url
protected

url of the file

Definition at line 150 of file FileProgress.h.

Referenced by FileProgress(), fitUrlLabel(), and setBytePosition().


The documentation for this class was generated from the following files: