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