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