source: trunk/src/gui/dialogs/qprogressdialog.cpp@ 324

Last change on this file since 324 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 24.3 KB
Line 
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
60QT_BEGIN_NAMESPACE
61
62// If the operation is expected to take this long (as predicted by
63// progress time), show the progress dialog.
64static const int defaultShowTime = 4000;
65// Wait at least this long before attempting to make a prediction.
66static const int minWaitTime = 50;
67
68class QProgressDialogPrivate : public QDialogPrivate
69{
70 Q_DECLARE_PUBLIC(QProgressDialog)
71
72public:
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
111void 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
133void 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
178void QProgressDialogPrivate::retranslateStrings()
179{
180 Q_Q(QProgressDialog);
181 if (useDefaultCancelText)
182 q->setCancelButtonText(QProgressDialog::tr("Cancel"));
183}
184
185void 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
285QProgressDialog::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
315QProgressDialog::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
330QProgressDialog::~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
352void 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
379QString QProgressDialog::labelText() const
380{
381 Q_D(const QProgressDialog);
382 if (d->label)
383 return d->label->text();
384 return QString();
385}
386
387void 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
409void 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
445void 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
471void 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