📄 qprogressdialog.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qprogressdialog.h"#ifndef QT_NO_PROGRESSDIALOG#include "qshortcut.h"#include "qpainter.h"#include "qdrawutil.h"#include "qdatetime.h"#include "qlabel.h"#include "qprogressbar.h"#include "qapplication.h"#include "qstyle.h"#include "qpushbutton.h"#include "qcursor.h"#include "qtimer.h"#include <private/qdialog_p.h>#include <limits.h>// If the operation is expected to take this long (as predicted by// progress time), show the progress dialog.static const int defaultShowTime = 4000;// Wait at least this long before attempting to make a prediction.static const int minWaitTime = 50;class QProgressDialogPrivate : public QDialogPrivate{ Q_DECLARE_PUBLIC(QProgressDialog)public: QProgressDialogPrivate() : label(0), cancel(0), bar(0), shown_once(false), cancellation_flag(false), showTime(defaultShowTime),#ifndef QT_NO_SHORTCUT escapeShortcut(0),#endif useDefaultCancelText(false) { } void init(const QString &labelText, const QString &cancelText, int min, int max); void layout(); void retranslateStrings(); QLabel *label; QPushButton *cancel; QProgressBar *bar; QTimer *forceTimer; bool shown_once; bool cancellation_flag; QTime starttime;#ifndef QT_NO_CURSOR QCursor parentCursor;#endif int showTime; bool autoClose; bool autoReset; bool forceHide;#ifndef QT_NO_SHORTCUT QShortcut *escapeShortcut;#endif bool useDefaultCancelText;};void QProgressDialogPrivate::init(const QString &labelText, const QString &cancelText, int min, int max){ Q_Q(QProgressDialog); label = new QLabel(labelText, q); int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, 0, q); label->setAlignment(Qt::Alignment(align)); bar = new QProgressBar(q); bar->setRange(min, max); autoClose = true; autoReset = true; forceHide = false; QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel())); forceTimer = new QTimer(q); QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow())); if (useDefaultCancelText) { retranslateStrings(); } else { q->setCancelButtonText(cancelText); }}void QProgressDialogPrivate::layout(){ Q_Q(QProgressDialog); int sp = q->style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing); int mtb = q->style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin); int mlr = qMin(q->width() / 10, mtb); const bool centered = bool(q->style()->styleHint(QStyle::SH_ProgressDialog_CenterCancelButton, 0, q)); QSize cs = cancel ? cancel->sizeHint() : QSize(0,0); QSize bh = bar->sizeHint(); int cspc; int lh = 0; // Find spacing and sizes that fit. It is important that a progress // dialog can be made very small if the user demands it so. for (int attempt=5; attempt--;) { cspc = cancel ? cs.height() + sp : 0; lh = qMax(0, q->height() - mtb - bh.height() - sp - cspc); if (lh < q->height()/4) { // Getting cramped sp /= 2; mtb /= 2; if (cancel) { cs.setHeight(qMax(4,cs.height()-sp-2)); } bh.setHeight(qMax(4,bh.height()-sp-1)); } else { break; } } if (cancel) { cancel->setGeometry( centered ? q->width()/2 - cs.width()/2 : q->width() - mlr - cs.width(), q->height() - mtb - cs.height(), cs.width(), cs.height()); } label->setGeometry(mlr, 0, q->width()-mlr*2, lh); bar->setGeometry(mlr, lh+sp, q->width()-mlr*2, bh.height());}void QProgressDialogPrivate::retranslateStrings(){ Q_Q(QProgressDialog); if (useDefaultCancelText) q->setCancelButtonText(QProgressDialog::tr("Cancel"));}/*! \class QProgressDialog \brief The QProgressDialog class provides feedback on the progress of a slow operation. \ingroup dialogs \mainclass A progress dialog is used to give the user an indication of how long an operation is going to take, and to demonstrate that the application has not frozen. It can also give the user an opportunity to abort the operation. A common problem with progress dialogs is that it is difficult to know when to use them; operations take different amounts of time on different hardware. QProgressDialog offers a solution to this problem: it estimates the time the operation will take (based on time for steps), and only shows itself if that estimate is beyond minimumDuration() (4 seconds by default). Use setMinimum() and setMaximum() or the constructor to set the number of "steps" in the operation and call setValue() as the operation progresses. The number of steps can be chosen arbitrarily. It can be the number of files copied, the number of bytes received, the number of iterations through the main loop of your algorithm, or some other suitable unit. Progress starts at the value set by setMinimum(), and the progress dialog shows that the operation has finished when you call setValue() with the value set by setMaximum() as its argument. The dialog automatically resets and hides itself at the end of the operation. Use setAutoReset() and setAutoClose() to change this behavior. There are two ways of using QProgressDialog: modal and modeless. Compared to a modeless QProgressDialog, a modal QProgressDialog is simpler to use for the programmer. Do the operation in a loop, call \l setValue() at intervals, and check for cancellation with wasCanceled(). For example: \quotefromfile snippets/dialogs/dialogs.cpp \skipto QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this); \printuntil progress.setValue(numFiles); A modeless progress dialog is suitable for operations that take place in the background, where the user is able to interact with the application. Such operations are typically based on QTimer (or QObject::timerEvent()), QSocketNotifier, or QUrlOperator; or performed in a separate thread. A QProgressBar in the status bar of your main window is often an alternative to a modeless progress dialog. You need to have an event loop to be running, connect the canceled() signal to a slot that stops the operation, and call \l setValue() at intervals. For example: \skipto Operation constructor \printuntil } \printuntil } \printuntil } In both modes the progress dialog may be customized by replacing the child widgets with custom widgets by using setLabel(), setBar(), and setCancelButton(). The functions setLabelText() and setCancelButtonText() set the texts shown. The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use QProgressDialog as well as other built-in Qt dialogs. \image plastique-progressdialog.png A progress dialog shown in the Plastique widget style. \sa QDialog, QProgressBar, {fowler}{GUI Design Handbook: Progress Indicator}, {Find Files Example}, {Pixelator Example}*//*! Constructs a progress dialog. Default settings: \list \i The label text is empty. \i The cancel button text is (translated) "Cancel". \i minimum is 0; \i maximum is 100 \endlist The \a parent argument is dialog's parent widget. The widget flags, \a f, are passed to the QDialog::QDialog() constructor. \sa setLabelText(), setCancelButtonText(), setCancelButton(), setMinimum(), setMaximum()*/QProgressDialog::QProgressDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(*(new QProgressDialogPrivate), parent, f){ Q_D(QProgressDialog); d->useDefaultCancelText = true; d->init(QString::fromLatin1(""), QString(), 0, 100);}/*! Constructs a progress dialog. The \a labelText is the text used to remind the user what is progressing. The \a cancelButtonText is the text to display on the cancel button, or 0 if no cancel button is to be shown. The \a minimum and \a maximum is the number of steps in the operation for which this progress dialog shows progress. For example, if the operation is to examine 50 files, this value minimum value would be 0, and the maximum would be 50. Before examining the first file, call setValue(0). As each file is processed call setValue(1), setValue(2), etc., finally calling setValue(50) after examining the last file. The \a parent argument is the dialog's parent widget. The and widget flags, \a f, are passed to the QDialog::QDialog() constructor. \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setMinimum(), setMaximum()*/QProgressDialog::QProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent, Qt::WindowFlags f) : QDialog(*(new QProgressDialogPrivate), parent, f){ Q_D(QProgressDialog); d->init(labelText, cancelButtonText, minimum, maximum);}/*! Destroys the progress dialog.*/QProgressDialog::~QProgressDialog(){}/*! \fn void QProgressDialog::canceled() This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default. \sa wasCanceled()*//*! Sets the label to \a label. The progress dialog resizes to fit. The label becomes owned by the progress dialog and will be deleted when necessary, so do not pass the address of an object on the stack. \sa setLabelText()*/void QProgressDialog::setLabel(QLabel *label){ Q_D(QProgressDialog); delete d->label; d->label = label; if (label) { if (label->parentWidget() == this) { label->hide(); // until we resize } else { label->setParent(this, 0); } } int w = qMax(isVisible() ? width() : 0, sizeHint().width()); int h = qMax(isVisible() ? height() : 0, sizeHint().height()); resize(w, h); if (label) label->show();}/*! \property QProgressDialog::labelText \brief the label's text The default text is an empty string.*/QString QProgressDialog::labelText() const{ Q_D(const QProgressDialog); if (d->label) return d->label->text(); return QString();}void QProgressDialog::setLabelText(const QString &text){ Q_D(QProgressDialog); if (d->label) { d->label->setText(text); int w = qMax(isVisible() ? width() : 0, sizeHint().width()); int h = qMax(isVisible() ? height() : 0, sizeHint().height()); resize(w, h); }}/*! Sets the cancel button to the push button, \a cancelButton. The progress dialog takes ownership of this button which will be deleted when necessary, so do not pass the address of an object that is on the stack, i.e. use new() to create the button. \sa setCancelButtonText()*/void QProgressDialog::setCancelButton(QPushButton *cancelButton){ Q_D(QProgressDialog); delete d->cancel; d->cancel = cancelButton; if (cancelButton) { if (cancelButton->parentWidget() == this) { cancelButton->hide(); // until we resize } else { cancelButton->setParent(this, 0); } connect(d->cancel, SIGNAL(clicked()), this, SIGNAL(canceled()));#ifndef QT_NO_SHORTCUT d->escapeShortcut = new QShortcut(Qt::Key_Escape, this, SIGNAL(canceled()));#endif } else {#ifndef QT_NO_SHORTCUT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -