📄 q3progressdialog.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support 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 "q3progressdialog.h"#ifndef QT_NO_PROGRESSDIALOG#include "q3progressbar.h"#include "qapplication.h"#include "qcursor.h"#include "qdatetime.h"#include "qlabel.h"#include "qpainter.h"#include "qpushbutton.h"#include "qshortcut.h"#include "qstyle.h"#include "qtimer.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;// Various layout valuesstatic const int margin_lr = 10;static const int margin_tb = 10;static const int spacing = 4;class Q3ProgressDialogData{public: Q3ProgressDialogData(Q3ProgressDialog* that, QWidget* parent, const QString& labelText, int totalSteps) : creator(parent), label(new QLabel(labelText,that)), cancel(0), bar(new Q3ProgressBar(totalSteps, that)), shown_once(false), cancellation_flag(false), showTime(defaultShowTime) { int align = that->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, 0, that); label->setAlignment(Qt::Alignment(align)); } QWidget *creator; QLabel *label; QPushButton *cancel; Q3ProgressBar *bar; bool shown_once; bool cancellation_flag; QTime starttime;#ifndef QT_NO_CURSOR QCursor parentCursor;#endif int showTime; bool autoClose; bool autoReset; bool forceHide;};/*! \class Q3ProgressDialog qprogressdialog.h \brief The Q3ProgressDialog class provides feedback on the progress of a slow operation. \compat 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. Q3ProgressDialog 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 setTotalSteps() (or the constructor) to set the number of "steps" in the operation and call setProgress() as the operation progresses. The step value 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 0, and the progress dialog shows that the operation has finished when you call setProgress() with totalSteps() 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 Q3ProgressDialog: modal and modeless. Using a modal Q3ProgressDialog is simpler for the programmer, but you must call QApplication::processEvents() or QEventLoop::processEvents(ExcludeUserInput) to keep the event loop running to ensure that the application doesn't freeze. Do the operation in a loop, call \l setProgress() at intervals, and check for cancellation with wasCanceled(). For example:\codeQ3ProgressDialog progress("Copying files...", "Abort Copy", numFiles, this, "progress", true);for (int i = 0; i < numFiles; i++) { progress.setProgress(i); qApp->processEvents(); if (progress.wasCanceled()) break; //... copy one file}progress.setProgress(numFiles);\endcode 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 Q3ProgressBar 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 setProgress() at intervals. For example:\codeOperation::Operation(QObject *parent = 0) : QObject(parent), steps(0){ pd = new Q3ProgressDialog("Operation in progress.", "Cancel", 100); connect(pd, SIGNAL(canceled()), this, SLOT(cancel())); t = new QTimer(this); connect(t, SIGNAL(timeout()), this, SLOT(perform())); t->start(0);}void Operation::perform(){ pd->setProgress(steps); //... perform one percent of the operation steps++; if (steps > pd->totalSteps()) t->stop();}void Operation::cancel(){ t->stop(); //... cleanup}\endcode 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. \inlineimage qprogdlg-m.png Screenshot in Motif style \inlineimage qprogdlg-w.png Screenshot in Windows style \sa QDialog, Q3ProgressBar, {fowler}{GUI Design Handbook: Progress Indicator}*//*! Returns the QLabel currently being displayed above the progress bar. This QLabel is owned by the Q3ProgressDialog. \sa setLabel()*/QLabel *Q3ProgressDialog::label() const{ return d->label;}/*! Returns the Q3ProgressBar currently being used to display progress. This Q3ProgressBar is owned by the Q3ProgressDialog. \sa setBar()*/Q3ProgressBar *Q3ProgressDialog::bar() const{ return d->bar;}/*! Constructs a progress dialog. Default settings: \list \i The label text is empty. \i The cancel button text is (translated) "Cancel". \i The total number of steps is 100. \endlist The \a creator argument is the widget to use as the dialog's parent. The \a name, \a modal, and the widget flags, \a f, are passed to the QDialog::QDialog() constructor. If \a modal is false (the default), you must have an event loop proceeding for any redrawing of the dialog to occur. If \a modal is true, the dialog ensures that events are processed when needed. \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setTotalSteps()*/Q3ProgressDialog::Q3ProgressDialog(QWidget *creator, const char *name, bool modal, Qt::WindowFlags f) : QDialog(creator, f){ setObjectName(QLatin1String(name)); setModal(modal); init(creator, QString::fromLatin1(""), tr("Cancel"), 100);}/*! Constructs a progress dialog. The \a labelText is 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 totalSteps is the total 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 would be 50. Before examining the first file, call setProgress(0). As each file is processed call setProgress(1), setProgress(2), etc., finally calling setProgress(50) after examining the last file. The \a creator argument is the widget to use as the dialog's parent. The \a name, \a modal, and widget flags, \a f, are passed to the QDialog::QDialog() constructor. If \a modal is false (the default), you will must have an event loop proceeding for any redrawing of the dialog to occur. If \a modal is true, the dialog ensures that events are processed when needed. \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setTotalSteps()*/Q3ProgressDialog::Q3ProgressDialog(const QString &labelText, const QString &cancelButtonText, int totalSteps, QWidget *creator, const char *name, bool modal, Qt::WindowFlags f) : QDialog(creator, f){ setObjectName(QLatin1String(name)); setModal(modal); init(creator, labelText, cancelButtonText, totalSteps);}/*! Constructs a progress dialog. Default settings: \list \i The label text is empty. \i The cancel button text is (translated) "Cancel". \i The total number of steps is 100. \endlist The \a creator argument is the widget to use as the dialog's parent. The widget flags, \a f, are passed to the QDialog::QDialog() constructor. \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setTotalSteps()*/Q3ProgressDialog::Q3ProgressDialog(QWidget *creator, Qt::WindowFlags f) : QDialog(creator, f){ init(creator, QString::fromLatin1(""), tr("Cancel"), 100);}/*! Constructs a progress dialog. The \a labelText is 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 totalSteps is the total 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 would be 50. Before examining the first file, call setProgress(0). As each file is processed call setProgress(1), setProgress(2), etc., finally calling setProgress(50) after examining the last file. The \a creator argument is the widget to use as the dialog's parent. The widget flags, \a f, are passed to the QDialog::QDialog() constructor. \sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setTotalSteps()*/Q3ProgressDialog::Q3ProgressDialog(const QString &labelText, const QString &cancelButtonText, int totalSteps, QWidget *creator, Qt::WindowFlags f) : QDialog(creator, f){ init(creator, labelText, cancelButtonText, totalSteps);}/*! Destroys the progress dialog.*/Q3ProgressDialog::~Q3ProgressDialog(){#ifndef QT_NO_CURSOR if (d->creator) d->creator->setCursor(d->parentCursor);#endif delete d;}void Q3ProgressDialog::init(QWidget *creator, const QString& lbl, const QString& canc, int totstps){ d = new Q3ProgressDialogData(this, creator, lbl, totstps); d->autoClose = true; d->autoReset = true; d->forceHide = false; setCancelButtonText(canc); connect(this, SIGNAL(canceled()), this, SIGNAL(cancelled())); connect(this, SIGNAL(canceled()), this, SLOT(cancel())); forceTimer = new QTimer(this); connect(forceTimer, SIGNAL(timeout()), this, SLOT(forceShow())); layout();}/*! \fn void Q3ProgressDialog::canceled() This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default. \sa wasCanceled()*//*! \fn void Q3ProgressDialog::cancelled() Use canceled() instead.*//*! 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 Q3ProgressDialog::setLabel(QLabel *label){ 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 Q3ProgressDialog::labelText \brief the label's text The default text is an empty string.*/QString Q3ProgressDialog::labelText() const{ if (label()) return label()->text(); return QString();}void Q3ProgressDialog::setLabelText(const QString &text){ if (label()) { label()->setText(text); int w = qMax(isVisible() ? width() : 0, sizeHint().width()); int h = qMax(isVisible() ? height() : 0, sizeHint().height()); resize(w, h); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -