1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qprogressdialog.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
45 |
|
---|
46 | #include "qshortcut.h"
|
---|
47 | #include "qpainter.h"
|
---|
48 | #include "qdrawutil.h"
|
---|
49 | #include "qdatetime.h"
|
---|
50 | #include "qlabel.h"
|
---|
51 | #include "qprogressbar.h"
|
---|
52 | #include "qapplication.h"
|
---|
53 | #include "qstyle.h"
|
---|
54 | #include "qpushbutton.h"
|
---|
55 | #include "qcursor.h"
|
---|
56 | #include "qtimer.h"
|
---|
57 | #include <private/qdialog_p.h>
|
---|
58 | #include <limits.h>
|
---|
59 |
|
---|
60 | QT_BEGIN_NAMESPACE
|
---|
61 |
|
---|
62 | // If the operation is expected to take this long (as predicted by
|
---|
63 | // progress time), show the progress dialog.
|
---|
64 | static const int defaultShowTime = 4000;
|
---|
65 | // Wait at least this long before attempting to make a prediction.
|
---|
66 | static const int minWaitTime = 50;
|
---|
67 |
|
---|
68 | class QProgressDialogPrivate : public QDialogPrivate
|
---|
69 | {
|
---|
70 | Q_DECLARE_PUBLIC(QProgressDialog)
|
---|
71 |
|
---|
72 | public:
|
---|
73 | QProgressDialogPrivate() : label(0), cancel(0), bar(0),
|
---|
74 | shown_once(false),
|
---|
75 | cancellation_flag(false),
|
---|
76 | showTime(defaultShowTime),
|
---|
77 | #ifndef QT_NO_SHORTCUT
|
---|
78 | escapeShortcut(0),
|
---|
79 | #endif
|
---|
80 | useDefaultCancelText(false)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | void init(const QString &labelText, const QString &cancelText, int min, int max);
|
---|
85 | void layout();
|
---|
86 | void retranslateStrings();
|
---|
87 | void _q_disconnectOnClose();
|
---|
88 |
|
---|
89 | QLabel *label;
|
---|
90 | QPushButton *cancel;
|
---|
91 | QProgressBar *bar;
|
---|
92 | QTimer *forceTimer;
|
---|
93 | bool shown_once;
|
---|
94 | bool cancellation_flag;
|
---|
95 | QTime starttime;
|
---|
96 | #ifndef QT_NO_CURSOR
|
---|
97 | QCursor parentCursor;
|
---|
98 | #endif
|
---|
99 | int showTime;
|
---|
100 | bool autoClose;
|
---|
101 | bool autoReset;
|
---|
102 | bool forceHide;
|
---|
103 | #ifndef QT_NO_SHORTCUT
|
---|
104 | QShortcut *escapeShortcut;
|
---|
105 | #endif
|
---|
106 | bool useDefaultCancelText;
|
---|
107 | QPointer<QObject> receiverToDisconnectOnClose;
|
---|
108 | QByteArray memberToDisconnectOnClose;
|
---|
109 | };
|
---|
110 |
|
---|
111 | void QProgressDialogPrivate::init(const QString &labelText, const QString &cancelText,
|
---|
112 | int min, int max)
|
---|
113 | {
|
---|
114 | Q_Q(QProgressDialog);
|
---|
115 | label = new QLabel(labelText, q);
|
---|
116 | int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, 0, q);
|
---|
117 | label->setAlignment(Qt::Alignment(align));
|
---|
118 | bar = new QProgressBar(q);
|
---|
119 | bar->setRange(min, max);
|
---|
120 | autoClose = true;
|
---|
121 | autoReset = true;
|
---|
122 | forceHide = false;
|
---|
123 | QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel()));
|
---|
124 | forceTimer = new QTimer(q);
|
---|
125 | QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));
|
---|
126 | if (useDefaultCancelText) {
|
---|
127 | retranslateStrings();
|
---|
128 | } else {
|
---|
129 | q->setCancelButtonText(cancelText);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void QProgressDialogPrivate::layout()
|
---|
134 | {
|
---|
135 | Q_Q(QProgressDialog);
|
---|
136 | int sp = q->style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
|
---|
137 | int mtb = q->style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
|
---|
138 | int mlr = qMin(q->width() / 10, mtb);
|
---|
139 | const bool centered =
|
---|
140 | bool(q->style()->styleHint(QStyle::SH_ProgressDialog_CenterCancelButton, 0, q));
|
---|
141 |
|
---|
142 | QSize cs = cancel ? cancel->sizeHint() : QSize(0,0);
|
---|
143 | QSize bh = bar->sizeHint();
|
---|
144 | int cspc;
|
---|
145 | int lh = 0;
|
---|
146 |
|
---|
147 | // Find spacing and sizes that fit. It is important that a progress
|
---|
148 | // dialog can be made very small if the user demands it so.
|
---|
149 | for (int attempt=5; attempt--;) {
|
---|
150 | cspc = cancel ? cs.height() + sp : 0;
|
---|
151 | lh = qMax(0, q->height() - mtb - bh.height() - sp - cspc);
|
---|
152 |
|
---|
153 | if (lh < q->height()/4) {
|
---|
154 | // Getting cramped
|
---|
155 | sp /= 2;
|
---|
156 | mtb /= 2;
|
---|
157 | if (cancel) {
|
---|
158 | cs.setHeight(qMax(4,cs.height()-sp-2));
|
---|
159 | }
|
---|
160 | bh.setHeight(qMax(4,bh.height()-sp-1));
|
---|
161 | } else {
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (cancel) {
|
---|
167 | cancel->setGeometry(
|
---|
168 | centered ? q->width()/2 - cs.width()/2 : q->width() - mlr - cs.width(),
|
---|
169 | q->height() - mtb - cs.height(),
|
---|
170 | cs.width(), cs.height());
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (label)
|
---|
174 | label->setGeometry(mlr, 0, q->width()-mlr*2, lh);
|
---|
175 | bar->setGeometry(mlr, lh+sp, q->width()-mlr*2, bh.height());
|
---|
176 | }
|
---|
177 |
|
---|
178 | void QProgressDialogPrivate::retranslateStrings()
|
---|
179 | {
|
---|
180 | Q_Q(QProgressDialog);
|
---|
181 | if (useDefaultCancelText)
|
---|
182 | q->setCancelButtonText(QProgressDialog::tr("Cancel"));
|
---|
183 | }
|
---|
184 |
|
---|
185 | void QProgressDialogPrivate::_q_disconnectOnClose()
|
---|
186 | {
|
---|
187 | Q_Q(QProgressDialog);
|
---|
188 | if (receiverToDisconnectOnClose) {
|
---|
189 | QObject::disconnect(q, SIGNAL(canceled()), receiverToDisconnectOnClose,
|
---|
190 | memberToDisconnectOnClose);
|
---|
191 | receiverToDisconnectOnClose = 0;
|
---|
192 | }
|
---|
193 | memberToDisconnectOnClose.clear();
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*!
|
---|
197 | \class QProgressDialog
|
---|
198 | \brief The QProgressDialog class provides feedback on the progress of a slow operation.
|
---|
199 | \ingroup dialogs
|
---|
200 | \mainclass
|
---|
201 |
|
---|
202 | A progress dialog is used to give the user an indication of how long
|
---|
203 | an operation is going to take, and to demonstrate that the
|
---|
204 | application has not frozen. It can also give the user an opportunity
|
---|
205 | to abort the operation.
|
---|
206 |
|
---|
207 | A common problem with progress dialogs is that it is difficult to know
|
---|
208 | when to use them; operations take different amounts of time on different
|
---|
209 | hardware. QProgressDialog offers a solution to this problem:
|
---|
210 | it estimates the time the operation will take (based on time for
|
---|
211 | steps), and only shows itself if that estimate is beyond minimumDuration()
|
---|
212 | (4 seconds by default).
|
---|
213 |
|
---|
214 | Use setMinimum() and setMaximum() or the constructor to set the number of
|
---|
215 | "steps" in the operation and call setValue() as the operation
|
---|
216 | progresses. The number of steps can be chosen arbitrarily. It can be the
|
---|
217 | number of files copied, the number of bytes received, the number of
|
---|
218 | iterations through the main loop of your algorithm, or some other
|
---|
219 | suitable unit. Progress starts at the value set by setMinimum(),
|
---|
220 | and the progress dialog shows that the operation has finished when
|
---|
221 | you call setValue() with the value set by setMaximum() as its argument.
|
---|
222 |
|
---|
223 | The dialog automatically resets and hides itself at the end of the
|
---|
224 | operation. Use setAutoReset() and setAutoClose() to change this
|
---|
225 | behavior. Note that if you set a new maximum (using setMaximum() or
|
---|
226 | setRange()) that equals your current value(), the dialog will not
|
---|
227 | close regardless.
|
---|
228 |
|
---|
229 | There are two ways of using QProgressDialog: modal and modeless.
|
---|
230 |
|
---|
231 | Compared to a modeless QProgressDialog, a modal QProgressDialog is simpler
|
---|
232 | to use for the programmer. Do the operation in a loop, call \l setValue() at
|
---|
233 | intervals, and check for cancellation with wasCanceled(). For example:
|
---|
234 |
|
---|
235 | \snippet doc/src/snippets/dialogs/dialogs.cpp 3
|
---|
236 |
|
---|
237 | A modeless progress dialog is suitable for operations that take
|
---|
238 | place in the background, where the user is able to interact with the
|
---|
239 | application. Such operations are typically based on QTimer (or
|
---|
240 | QObject::timerEvent()), QSocketNotifier, or QUrlOperator; or performed
|
---|
241 | in a separate thread. A QProgressBar in the status bar of your main window
|
---|
242 | is often an alternative to a modeless progress dialog.
|
---|
243 |
|
---|
244 | You need to have an event loop to be running, connect the
|
---|
245 | canceled() signal to a slot that stops the operation, and call \l
|
---|
246 | setValue() at intervals. For example:
|
---|
247 |
|
---|
248 | \snippet doc/src/snippets/dialogs/dialogs.cpp 4
|
---|
249 | \codeline
|
---|
250 | \snippet doc/src/snippets/dialogs/dialogs.cpp 5
|
---|
251 | \codeline
|
---|
252 | \snippet doc/src/snippets/dialogs/dialogs.cpp 6
|
---|
253 |
|
---|
254 | In both modes the progress dialog may be customized by
|
---|
255 | replacing the child widgets with custom widgets by using setLabel(),
|
---|
256 | setBar(), and setCancelButton().
|
---|
257 | The functions setLabelText() and setCancelButtonText()
|
---|
258 | set the texts shown.
|
---|
259 |
|
---|
260 | \image plastique-progressdialog.png A progress dialog shown in the Plastique widget style.
|
---|
261 |
|
---|
262 | \sa QDialog, QProgressBar, {fowler}{GUI Design Handbook: Progress Indicator},
|
---|
263 | {Find Files Example}, {Pixelator Example}
|
---|
264 | */
|
---|
265 |
|
---|
266 |
|
---|
267 | /*!
|
---|
268 | Constructs a progress dialog.
|
---|
269 |
|
---|
270 | Default settings:
|
---|
271 | \list
|
---|
272 | \i The label text is empty.
|
---|
273 | \i The cancel button text is (translated) "Cancel".
|
---|
274 | \i minimum is 0;
|
---|
275 | \i maximum is 100
|
---|
276 | \endlist
|
---|
277 |
|
---|
278 | The \a parent argument is dialog's parent widget. The widget flags, \a f, are
|
---|
279 | passed to the QDialog::QDialog() constructor.
|
---|
280 |
|
---|
281 | \sa setLabelText(), setCancelButtonText(), setCancelButton(),
|
---|
282 | setMinimum(), setMaximum()
|
---|
283 | */
|
---|
284 |
|
---|
285 | QProgressDialog::QProgressDialog(QWidget *parent, Qt::WindowFlags f)
|
---|
286 | : QDialog(*(new QProgressDialogPrivate), parent, f)
|
---|
287 | {
|
---|
288 | Q_D(QProgressDialog);
|
---|
289 | d->useDefaultCancelText = true;
|
---|
290 | d->init(QString::fromLatin1(""), QString(), 0, 100);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*!
|
---|
294 | Constructs a progress dialog.
|
---|
295 |
|
---|
296 | The \a labelText is the text used to remind the user what is progressing.
|
---|
297 |
|
---|
298 | The \a cancelButtonText is the text to display on the cancel button. If
|
---|
299 | QString() is passed then no cancel button is shown.
|
---|
300 |
|
---|
301 | The \a minimum and \a maximum is the number of steps in the operation for
|
---|
302 | which this progress dialog shows progress. For example, if the
|
---|
303 | operation is to examine 50 files, this value minimum value would be 0,
|
---|
304 | and the maximum would be 50. Before examining the first file, call
|
---|
305 | setValue(0). As each file is processed call setValue(1), setValue(2),
|
---|
306 | etc., finally calling setValue(50) after examining the last file.
|
---|
307 |
|
---|
308 | The \a parent argument is the dialog's parent widget. The parent, \a parent, and
|
---|
309 | widget flags, \a f, are passed to the QDialog::QDialog() constructor.
|
---|
310 |
|
---|
311 | \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(),
|
---|
312 | setMinimum(), setMaximum()
|
---|
313 | */
|
---|
314 |
|
---|
315 | QProgressDialog::QProgressDialog(const QString &labelText,
|
---|
316 | const QString &cancelButtonText,
|
---|
317 | int minimum, int maximum,
|
---|
318 | QWidget *parent, Qt::WindowFlags f)
|
---|
319 | : QDialog(*(new QProgressDialogPrivate), parent, f)
|
---|
320 | {
|
---|
321 | Q_D(QProgressDialog);
|
---|
322 | d->init(labelText, cancelButtonText, minimum, maximum);
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /*!
|
---|
327 | Destroys the progress dialog.
|
---|
328 | */
|
---|
329 |
|
---|
330 | QProgressDialog::~QProgressDialog()
|
---|
331 | {
|
---|
332 | }
|
---|
333 |
|
---|
334 | /*!
|
---|
335 | \fn void QProgressDialog::canceled()
|
---|
336 |
|
---|
337 | This signal is emitted when the cancel button is clicked.
|
---|
338 | It is connected to the cancel() slot by default.
|
---|
339 |
|
---|
340 | \sa wasCanceled()
|
---|
341 | */
|
---|
342 |
|
---|
343 |
|
---|
344 | /*!
|
---|
345 | Sets the label to \a label. The progress dialog resizes to fit. The
|
---|
346 | label becomes owned by the progress dialog and will be deleted when
|
---|
347 | necessary, so do not pass the address of an object on the stack.
|
---|
348 |
|
---|
349 | \sa setLabelText()
|
---|
350 | */
|
---|
351 |
|
---|
352 | void QProgressDialog::setLabel(QLabel *label)
|
---|
353 | {
|
---|
354 | Q_D(QProgressDialog);
|
---|
355 | delete d->label;
|
---|
356 | d->label = label;
|
---|
357 | if (label) {
|
---|
358 | if (label->parentWidget() == this) {
|
---|
359 | label->hide(); // until we resize
|
---|
360 | } else {
|
---|
361 | label->setParent(this, 0);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
365 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
366 | resize(w, h);
|
---|
367 | if (label)
|
---|
368 | label->show();
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | /*!
|
---|
373 | \property QProgressDialog::labelText
|
---|
374 | \brief the label's text
|
---|
375 |
|
---|
376 | The default text is an empty string.
|
---|
377 | */
|
---|
378 |
|
---|
379 | QString QProgressDialog::labelText() const
|
---|
380 | {
|
---|
381 | Q_D(const QProgressDialog);
|
---|
382 | if (d->label)
|
---|
383 | return d->label->text();
|
---|
384 | return QString();
|
---|
385 | }
|
---|
386 |
|
---|
387 | void QProgressDialog::setLabelText(const QString &text)
|
---|
388 | {
|
---|
389 | Q_D(QProgressDialog);
|
---|
390 | if (d->label) {
|
---|
391 | d->label->setText(text);
|
---|
392 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
393 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
394 | resize(w, h);
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | /*!
|
---|
400 | Sets the cancel button to the push button, \a cancelButton. The
|
---|
401 | progress dialog takes ownership of this button which will be deleted
|
---|
402 | when necessary, so do not pass the address of an object that is on
|
---|
403 | the stack, i.e. use new() to create the button. If 0 is passed then
|
---|
404 | no cancel button will be shown.
|
---|
405 |
|
---|
406 | \sa setCancelButtonText()
|
---|
407 | */
|
---|
408 |
|
---|
409 | void QProgressDialog::setCancelButton(QPushButton *cancelButton)
|
---|
410 | {
|
---|
411 | Q_D(QProgressDialog);
|
---|
412 | delete d->cancel;
|
---|
413 | d->cancel = cancelButton;
|
---|
414 | if (cancelButton) {
|
---|
415 | if (cancelButton->parentWidget() == this) {
|
---|
416 | cancelButton->hide(); // until we resize
|
---|
417 | } else {
|
---|
418 | cancelButton->setParent(this, 0);
|
---|
419 | }
|
---|
420 | connect(d->cancel, SIGNAL(clicked()), this, SIGNAL(canceled()));
|
---|
421 | #ifndef QT_NO_SHORTCUT
|
---|
422 | d->escapeShortcut = new QShortcut(Qt::Key_Escape, this, SIGNAL(canceled()));
|
---|
423 | #endif
|
---|
424 | } else {
|
---|
425 | #ifndef QT_NO_SHORTCUT
|
---|
426 | delete d->escapeShortcut;
|
---|
427 | d->escapeShortcut = 0;
|
---|
428 | #endif
|
---|
429 | }
|
---|
430 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
431 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
432 | resize(w, h);
|
---|
433 | if (cancelButton)
|
---|
434 | cancelButton->show();
|
---|
435 | }
|
---|
436 |
|
---|
437 | /*!
|
---|
438 | Sets the cancel button's text to \a cancelButtonText. If the text
|
---|
439 | is set to QString() then it will cause the cancel button to be
|
---|
440 | hidden and deleted.
|
---|
441 |
|
---|
442 | \sa setCancelButton()
|
---|
443 | */
|
---|
444 |
|
---|
445 | void QProgressDialog::setCancelButtonText(const QString &cancelButtonText)
|
---|
446 | {
|
---|
447 | Q_D(QProgressDialog);
|
---|
448 | d->useDefaultCancelText = false;
|
---|
449 |
|
---|
450 | if (!cancelButtonText.isNull()) {
|
---|
451 | if (d->cancel)
|
---|
452 | d->cancel->setText(cancelButtonText);
|
---|
453 | else
|
---|
454 | setCancelButton(new QPushButton(cancelButtonText, this));
|
---|
455 | } else {
|
---|
456 | setCancelButton(0);
|
---|
457 | }
|
---|
458 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
459 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
460 | resize(w, h);
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /*!
|
---|
465 | Sets the progress bar widget to \a bar. The progress dialog resizes to
|
---|
466 | fit. The progress dialog takes ownership of the progress \a bar which
|
---|
467 | will be deleted when necessary, so do not use a progress bar
|
---|
468 | allocated on the stack.
|
---|
469 | */
|
---|
470 |
|
---|
471 | void QProgressDialog::setBar(QProgressBar *bar)
|
---|
472 | {
|
---|
473 | Q_D(QProgressDialog);
|
---|
474 | if (!bar) {
|
---|
475 | qWarning("QProgressDialog::setBar: Cannot set a null progress bar");
|
---|
476 | return;
|
---|
477 | }
|
---|
478 | #ifndef QT_NO_DEBUG
|
---|
479 | if (value() > 0)
|
---|
480 | qWarning("QProgressDialog::setBar: Cannot set a new progress bar "
|
---|
481 | "while the old one is active");
|
---|
482 | #endif
|
---|
483 | delete d->bar;
|
---|
484 | d->bar = bar;
|
---|
485 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
486 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
487 | resize(w, h);
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | /*!
|
---|
492 | \property QProgressDialog::wasCanceled
|
---|
493 | \brief whether the dialog was canceled
|
---|
494 | */
|
---|
495 |
|
---|
496 | bool QProgressDialog::wasCanceled() const
|
---|
497 | {
|
---|
498 | Q_D(const QProgressDialog);
|
---|
499 | return d->cancellation_flag;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | /*!
|
---|
504 | \property QProgressDialog::maximum
|
---|
505 | \brief the highest value represented by the progress bar
|
---|
506 |
|
---|
507 | The default is 0.
|
---|
508 |
|
---|
509 | \sa minimum, setRange()
|
---|
510 | */
|
---|
511 |
|
---|
512 | int QProgressDialog::maximum() const
|
---|
513 | {
|
---|
514 | Q_D(const QProgressDialog);
|
---|
515 | return d->bar->maximum();
|
---|
516 | }
|
---|
517 |
|
---|
518 | void QProgressDialog::setMaximum(int maximum)
|
---|
519 | {
|
---|
520 | Q_D(QProgressDialog);
|
---|
521 | d->bar->setMaximum(maximum);
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*!
|
---|
525 | \property QProgressDialog::minimum
|
---|
526 | \brief the lowest value represented by the progress bar
|
---|
527 |
|
---|
528 | The default is 0.
|
---|
529 |
|
---|
530 | \sa maximum, setRange()
|
---|
531 | */
|
---|
532 |
|
---|
533 | int QProgressDialog::minimum() const
|
---|
534 | {
|
---|
535 | Q_D(const QProgressDialog);
|
---|
536 | return d->bar->minimum();
|
---|
537 | }
|
---|
538 |
|
---|
539 | void QProgressDialog::setMinimum(int minimum)
|
---|
540 | {
|
---|
541 | Q_D(QProgressDialog);
|
---|
542 | d->bar->setMinimum(minimum);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /*!
|
---|
546 | Sets the progress dialog's minimum and maximum values
|
---|
547 | to \a minimum and \a maximum, respectively.
|
---|
548 |
|
---|
549 | If \a maximum is smaller than \a minimum, \a minimum becomes the only
|
---|
550 | legal value.
|
---|
551 |
|
---|
552 | If the current value falls outside the new range, the progress
|
---|
553 | dialog is reset with reset().
|
---|
554 |
|
---|
555 | \sa minimum, maximum
|
---|
556 | */
|
---|
557 | void QProgressDialog::setRange(int minimum, int maximum)
|
---|
558 | {
|
---|
559 | Q_D(QProgressDialog);
|
---|
560 | d->bar->setRange(minimum, maximum);
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 | /*!
|
---|
565 | Resets the progress dialog.
|
---|
566 | The progress dialog becomes hidden if autoClose() is true.
|
---|
567 |
|
---|
568 | \sa setAutoClose(), setAutoReset()
|
---|
569 | */
|
---|
570 |
|
---|
571 | void QProgressDialog::reset()
|
---|
572 | {
|
---|
573 | Q_D(QProgressDialog);
|
---|
574 | #ifndef QT_NO_CURSOR
|
---|
575 | if (value() >= 0) {
|
---|
576 | if (parentWidget())
|
---|
577 | parentWidget()->setCursor(d->parentCursor);
|
---|
578 | }
|
---|
579 | #endif
|
---|
580 | if (d->autoClose || d->forceHide)
|
---|
581 | hide();
|
---|
582 | d->bar->reset();
|
---|
583 | d->cancellation_flag = false;
|
---|
584 | d->shown_once = false;
|
---|
585 | d->forceTimer->stop();
|
---|
586 |
|
---|
587 | /*
|
---|
588 | I wish we could disconnect the user slot provided to open() here but
|
---|
589 | unfortunately reset() is usually called before the slot has been invoked.
|
---|
590 | (reset() is itself invoked when canceled() is emitted.)
|
---|
591 | */
|
---|
592 | if (d->receiverToDisconnectOnClose)
|
---|
593 | QMetaObject::invokeMethod(this, "_q_disconnectOnClose", Qt::QueuedConnection);
|
---|
594 | }
|
---|
595 |
|
---|
596 | /*!
|
---|
597 | Resets the progress dialog. wasCanceled() becomes true until
|
---|
598 | the progress dialog is reset.
|
---|
599 | The progress dialog becomes hidden.
|
---|
600 | */
|
---|
601 |
|
---|
602 | void QProgressDialog::cancel()
|
---|
603 | {
|
---|
604 | Q_D(QProgressDialog);
|
---|
605 | d->forceHide = true;
|
---|
606 | reset();
|
---|
607 | d->forceHide = false;
|
---|
608 | d->cancellation_flag = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | int QProgressDialog::value() const
|
---|
613 | {
|
---|
614 | Q_D(const QProgressDialog);
|
---|
615 | return d->bar->value();
|
---|
616 | }
|
---|
617 |
|
---|
618 | /*!
|
---|
619 | \property QProgressDialog::value
|
---|
620 | \brief the current amount of progress made.
|
---|
621 |
|
---|
622 | For the progress dialog to work as expected, you should initially set
|
---|
623 | this property to 0 and finally set it to
|
---|
624 | QProgressDialog::maximum(); you can call setValue() any number of times
|
---|
625 | in-between.
|
---|
626 |
|
---|
627 | \warning If the progress dialog is modal
|
---|
628 | (see QProgressDialog::QProgressDialog()),
|
---|
629 | this function calls QApplication::processEvents(), so take care that
|
---|
630 | this does not cause undesirable re-entrancy in your code. For example,
|
---|
631 | don't use a QProgressDialog inside a paintEvent()!
|
---|
632 |
|
---|
633 | \sa minimum, maximum
|
---|
634 | */
|
---|
635 | void QProgressDialog::setValue(int progress)
|
---|
636 | {
|
---|
637 | Q_D(QProgressDialog);
|
---|
638 | if (progress == d->bar->value()
|
---|
639 | || (d->bar->value() == -1 && progress == d->bar->maximum()))
|
---|
640 | return;
|
---|
641 |
|
---|
642 | d->bar->setValue(progress);
|
---|
643 |
|
---|
644 | if (d->shown_once) {
|
---|
645 | if (isModal())
|
---|
646 | qApp->processEvents();
|
---|
647 | } else {
|
---|
648 | if (progress == 0) {
|
---|
649 | d->starttime.start();
|
---|
650 | d->forceTimer->start(d->showTime);
|
---|
651 | return;
|
---|
652 | } else {
|
---|
653 | bool need_show;
|
---|
654 | int elapsed = d->starttime.elapsed();
|
---|
655 | if (elapsed >= d->showTime) {
|
---|
656 | need_show = true;
|
---|
657 | } else {
|
---|
658 | if (elapsed > minWaitTime) {
|
---|
659 | int estimate;
|
---|
660 | int totalSteps = maximum() - minimum();
|
---|
661 | int myprogress = progress - minimum();
|
---|
662 | if ((totalSteps - myprogress) >= INT_MAX / elapsed)
|
---|
663 | estimate = (totalSteps - myprogress) / myprogress * elapsed;
|
---|
664 | else
|
---|
665 | estimate = elapsed * (totalSteps - myprogress) / myprogress;
|
---|
666 | need_show = estimate >= d->showTime;
|
---|
667 | } else {
|
---|
668 | need_show = false;
|
---|
669 | }
|
---|
670 | }
|
---|
671 | if (need_show) {
|
---|
672 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
673 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
674 | resize(w, h);
|
---|
675 | show();
|
---|
676 | d->shown_once = true;
|
---|
677 | }
|
---|
678 | }
|
---|
679 | #ifdef Q_WS_MAC
|
---|
680 | QApplication::flush();
|
---|
681 | #endif
|
---|
682 | }
|
---|
683 |
|
---|
684 | if (progress == d->bar->maximum() && d->autoReset)
|
---|
685 | reset();
|
---|
686 | }
|
---|
687 |
|
---|
688 | /*!
|
---|
689 | Returns a size that fits the contents of the progress dialog.
|
---|
690 | The progress dialog resizes itself as required, so you should not
|
---|
691 | need to call this yourself.
|
---|
692 | */
|
---|
693 |
|
---|
694 | QSize QProgressDialog::sizeHint() const
|
---|
695 | {
|
---|
696 | Q_D(const QProgressDialog);
|
---|
697 | QSize sh = d->label ? d->label->sizeHint() : QSize(0, 0);
|
---|
698 | QSize bh = d->bar->sizeHint();
|
---|
699 | int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
|
---|
700 | int spacing = style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
|
---|
701 | int h = margin * 2 + bh.height() + sh.height() + spacing;
|
---|
702 | if (d->cancel)
|
---|
703 | h += d->cancel->sizeHint().height() + spacing;
|
---|
704 | return QSize(qMax(200, sh.width() + 2 * margin), h);
|
---|
705 | }
|
---|
706 |
|
---|
707 | /*!\reimp
|
---|
708 | */
|
---|
709 | void QProgressDialog::resizeEvent(QResizeEvent *)
|
---|
710 | {
|
---|
711 | Q_D(QProgressDialog);
|
---|
712 | d->layout();
|
---|
713 | }
|
---|
714 |
|
---|
715 | /*!
|
---|
716 | \reimp
|
---|
717 | */
|
---|
718 | void QProgressDialog::changeEvent(QEvent *ev)
|
---|
719 | {
|
---|
720 | Q_D(QProgressDialog);
|
---|
721 | if (ev->type() == QEvent::StyleChange)
|
---|
722 | d->layout();
|
---|
723 | else if (ev->type() == QEvent::LanguageChange)
|
---|
724 | d->retranslateStrings();
|
---|
725 | QDialog::changeEvent(ev);
|
---|
726 | }
|
---|
727 |
|
---|
728 | /*!
|
---|
729 | \property QProgressDialog::minimumDuration
|
---|
730 | \brief the time that must pass before the dialog appears
|
---|
731 |
|
---|
732 | If the expected duration of the task is less than the
|
---|
733 | minimumDuration, the dialog will not appear at all. This prevents
|
---|
734 | the dialog popping up for tasks that are quickly over. For tasks
|
---|
735 | that are expected to exceed the minimumDuration, the dialog will
|
---|
736 | pop up after the minimumDuration time or as soon as any progress
|
---|
737 | is set.
|
---|
738 |
|
---|
739 | If set to 0, the dialog is always shown as soon as any progress is
|
---|
740 | set. The default is 4000 milliseconds.
|
---|
741 | */
|
---|
742 | void QProgressDialog::setMinimumDuration(int ms)
|
---|
743 | {
|
---|
744 | Q_D(QProgressDialog);
|
---|
745 | d->showTime = ms;
|
---|
746 | if (d->bar->value() == 0) {
|
---|
747 | d->forceTimer->stop();
|
---|
748 | d->forceTimer->start(ms);
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | int QProgressDialog::minimumDuration() const
|
---|
753 | {
|
---|
754 | Q_D(const QProgressDialog);
|
---|
755 | return d->showTime;
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 | /*!
|
---|
760 | \reimp
|
---|
761 | */
|
---|
762 |
|
---|
763 | void QProgressDialog::closeEvent(QCloseEvent *e)
|
---|
764 | {
|
---|
765 | emit canceled();
|
---|
766 | QDialog::closeEvent(e);
|
---|
767 | }
|
---|
768 |
|
---|
769 | /*!
|
---|
770 | \property QProgressDialog::autoReset
|
---|
771 | \brief whether the progress dialog calls reset() as soon as value() equals maximum()
|
---|
772 |
|
---|
773 | The default is true.
|
---|
774 |
|
---|
775 | \sa setAutoClose()
|
---|
776 | */
|
---|
777 |
|
---|
778 | void QProgressDialog::setAutoReset(bool b)
|
---|
779 | {
|
---|
780 | Q_D(QProgressDialog);
|
---|
781 | d->autoReset = b;
|
---|
782 | }
|
---|
783 |
|
---|
784 | bool QProgressDialog::autoReset() const
|
---|
785 | {
|
---|
786 | Q_D(const QProgressDialog);
|
---|
787 | return d->autoReset;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /*!
|
---|
791 | \property QProgressDialog::autoClose
|
---|
792 | \brief whether the dialog gets hidden by reset()
|
---|
793 |
|
---|
794 | The default is true.
|
---|
795 |
|
---|
796 | \sa setAutoReset()
|
---|
797 | */
|
---|
798 |
|
---|
799 | void QProgressDialog::setAutoClose(bool close)
|
---|
800 | {
|
---|
801 | Q_D(QProgressDialog);
|
---|
802 | d->autoClose = close;
|
---|
803 | }
|
---|
804 |
|
---|
805 | bool QProgressDialog::autoClose() const
|
---|
806 | {
|
---|
807 | Q_D(const QProgressDialog);
|
---|
808 | return d->autoClose;
|
---|
809 | }
|
---|
810 |
|
---|
811 | /*!
|
---|
812 | \reimp
|
---|
813 | */
|
---|
814 |
|
---|
815 | void QProgressDialog::showEvent(QShowEvent *e)
|
---|
816 | {
|
---|
817 | Q_D(QProgressDialog);
|
---|
818 | QDialog::showEvent(e);
|
---|
819 | int w = qMax(isVisible() ? width() : 0, sizeHint().width());
|
---|
820 | int h = qMax(isVisible() ? height() : 0, sizeHint().height());
|
---|
821 | resize(w, h);
|
---|
822 | d->forceTimer->stop();
|
---|
823 | }
|
---|
824 |
|
---|
825 | /*!
|
---|
826 | Shows the dialog if it is still hidden after the algorithm has been started
|
---|
827 | and minimumDuration milliseconds have passed.
|
---|
828 |
|
---|
829 | \sa setMinimumDuration()
|
---|
830 | */
|
---|
831 |
|
---|
832 | void QProgressDialog::forceShow()
|
---|
833 | {
|
---|
834 | Q_D(QProgressDialog);
|
---|
835 | d->forceTimer->stop();
|
---|
836 | if (d->shown_once || d->cancellation_flag)
|
---|
837 | return;
|
---|
838 |
|
---|
839 | show();
|
---|
840 | d->shown_once = true;
|
---|
841 | }
|
---|
842 |
|
---|
843 | /*!
|
---|
844 | \since 4.5
|
---|
845 | \overload
|
---|
846 |
|
---|
847 | Opens the dialog and connects its accepted() signal to the slot specified
|
---|
848 | by \a receiver and \a member.
|
---|
849 |
|
---|
850 | The signal will be disconnected from the slot when the dialog is closed.
|
---|
851 | */
|
---|
852 | void QProgressDialog::open(QObject *receiver, const char *member)
|
---|
853 | {
|
---|
854 | Q_D(QProgressDialog);
|
---|
855 | connect(this, SIGNAL(canceled()), receiver, member);
|
---|
856 | d->receiverToDisconnectOnClose = receiver;
|
---|
857 | d->memberToDisconnectOnClose = member;
|
---|
858 | QDialog::open();
|
---|
859 | }
|
---|
860 |
|
---|
861 | QT_END_NAMESPACE
|
---|
862 |
|
---|
863 | #include "moc_qprogressdialog.cpp"
|
---|
864 |
|
---|
865 | #endif // QT_NO_PROGRESSDIALOG
|
---|