KDEUI
kprogressdialog.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1996 Martynas Kunigelis // krazy:exclude=copyright (email unknown) 00003 Copyright (C) 2006-2007 Urs Wolfer <uwolfer at kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kprogressdialog.h" 00021 00022 #include <QLabel> 00023 #include <QLayout> 00024 #include <QProgressBar> 00025 #include <QTimer> 00026 00027 #include <kguiitem.h> 00028 #include <kpushbutton.h> 00029 00030 class KProgressDialog::KProgressDialogPrivate 00031 { 00032 public: 00033 KProgressDialogPrivate(KProgressDialog *q) 00034 : q(q), 00035 cancelButtonShown(true), 00036 mAutoClose(true), 00037 mAutoReset(false), 00038 mCancelled(false), 00039 mAllowCancel(true), 00040 mShown(false), 00041 mMinDuration(2000) 00042 { 00043 } 00044 00045 void slotAutoShow(); 00046 void slotAutoActions(int percentage); 00047 00048 KProgressDialog *q; 00049 bool cancelButtonShown : 1; 00050 bool mAutoClose : 1; 00051 bool mAutoReset : 1; 00052 bool mCancelled : 1; 00053 bool mAllowCancel : 1; 00054 bool mShown : 1; 00055 QString mCancelText; 00056 QLabel* mLabel; 00057 QProgressBar* mProgressBar; 00058 QTimer* mShowTimer; 00059 int mMinDuration; 00060 }; 00061 00062 KProgressDialog::KProgressDialog(QWidget* parent, const QString& caption, 00063 const QString& text, Qt::WFlags flags) 00064 : KDialog(parent, flags), 00065 d(new KProgressDialogPrivate(this)) 00066 { 00067 setCaption(caption); 00068 setButtons(KDialog::Cancel); 00069 00070 d->mShowTimer = new QTimer(this); 00071 00072 d->mCancelText = KDialog::buttonText(KDialog::Cancel); 00073 00074 QWidget *mainWidget = new QWidget(this); 00075 QVBoxLayout* layout = new QVBoxLayout(mainWidget); 00076 layout->setMargin(10); 00077 00078 d->mLabel = new QLabel(text, mainWidget); 00079 layout->addWidget(d->mLabel); 00080 00081 d->mProgressBar = new QProgressBar(mainWidget); 00082 layout->addWidget(d->mProgressBar); 00083 00084 setMainWidget(mainWidget); 00085 00086 connect(d->mProgressBar, SIGNAL(valueChanged(int)), 00087 this, SLOT(slotAutoActions(int))); 00088 connect(d->mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow())); 00089 d->mShowTimer->setSingleShot(true); 00090 d->mShowTimer->start(d->mMinDuration); 00091 } 00092 00093 KProgressDialog::~KProgressDialog() 00094 { 00095 delete d; 00096 } 00097 00098 void KProgressDialog::KProgressDialogPrivate::slotAutoShow() 00099 { 00100 if (mShown || mCancelled) 00101 { 00102 return; 00103 } 00104 00105 q->show(); 00106 } 00107 00108 void KProgressDialog::showEvent(QShowEvent *event) 00109 { 00110 d->mShown = true; 00111 KDialog::showEvent(event); 00112 } 00113 00114 void KProgressDialog::reject() 00115 { 00116 d->mCancelled = true; 00117 00118 if (d->mAllowCancel) 00119 { 00120 KDialog::reject(); 00121 } 00122 } 00123 00124 bool KProgressDialog::wasCancelled() const 00125 { 00126 return d->mCancelled; 00127 } 00128 00129 void KProgressDialog::ignoreCancel() 00130 { 00131 d->mCancelled = false; 00132 } 00133 00134 void KProgressDialog::setMinimumDuration(int ms) 00135 { 00136 d->mMinDuration = ms; 00137 if (!d->mShown) 00138 { 00139 d->mShowTimer->stop(); 00140 d->mShowTimer->setSingleShot(true); 00141 d->mShowTimer->start(d->mMinDuration); 00142 } 00143 } 00144 00145 int KProgressDialog::minimumDuration() const 00146 { 00147 return d->mMinDuration; 00148 } 00149 00150 void KProgressDialog::setAllowCancel(bool allowCancel) 00151 { 00152 d->mAllowCancel = allowCancel; 00153 showCancelButton(allowCancel); 00154 } 00155 00156 bool KProgressDialog::allowCancel() const 00157 { 00158 return d->mAllowCancel; 00159 } 00160 00161 QProgressBar* KProgressDialog::progressBar() 00162 { 00163 return d->mProgressBar; 00164 } 00165 00166 const QProgressBar* KProgressDialog::progressBar() const 00167 { 00168 return d->mProgressBar; 00169 } 00170 00171 void KProgressDialog::setLabelText(const QString& text) 00172 { 00173 d->mLabel->setText(text); 00174 } 00175 00176 QString KProgressDialog::labelText() const 00177 { 00178 return d->mLabel->text(); 00179 } 00180 00181 void KProgressDialog::showCancelButton(bool show) 00182 { 00183 showButton(Cancel, show); 00184 } 00185 00186 bool KProgressDialog::autoClose() const 00187 { 00188 return d->mAutoClose; 00189 } 00190 00191 void KProgressDialog::setAutoClose(bool autoClose) 00192 { 00193 d->mAutoClose = autoClose; 00194 } 00195 00196 bool KProgressDialog::autoReset() const 00197 { 00198 return d->mAutoReset; 00199 } 00200 00201 void KProgressDialog::setAutoReset(bool autoReset) 00202 { 00203 d->mAutoReset = autoReset; 00204 } 00205 00206 void KProgressDialog::setButtonText(const QString& text) 00207 { 00208 d->mCancelText = text; 00209 setButtonGuiItem(Cancel, KGuiItem(text)); 00210 } 00211 00212 QString KProgressDialog::buttonText() const 00213 { 00214 return d->mCancelText; 00215 } 00216 00217 void KProgressDialog::KProgressDialogPrivate::slotAutoActions(int percentage) 00218 { 00219 if (percentage < mProgressBar->maximum() || 00220 (mProgressBar->minimum() == mProgressBar->maximum())) // progress dialogs with busy indicators (see #178648) 00221 { 00222 if (!cancelButtonShown) 00223 { 00224 q->setButtonGuiItem(KDialog::Cancel, KGuiItem(mCancelText)); 00225 cancelButtonShown = true; 00226 } 00227 return; 00228 } 00229 00230 mShowTimer->stop(); 00231 00232 if (mAutoReset) 00233 { 00234 mProgressBar->setValue(0); 00235 } 00236 else 00237 { 00238 q->setAllowCancel(true); 00239 q->setButtonGuiItem(Cancel, KStandardGuiItem::close()); 00240 cancelButtonShown = false; 00241 } 00242 00243 if (mAutoClose) 00244 { 00245 if (mShown) 00246 { 00247 q->hide(); 00248 } 00249 else 00250 { 00251 emit q->finished(); 00252 } 00253 } 00254 } 00255 00256 #include "kprogressdialog.moc"
KDE 4.6 API Reference