File indexing completed on 2024-04-14 14:20:24
0001 /* This file is part of the KDE libraries 0002 Copyright (C) 1996 Martynas Kunigelis // krazy:exclude=copyright (email unknown) 0003 Copyright (C) 2006-2007 Urs Wolfer <uwolfer at kde.org> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License version 2 as published by the Free Software Foundation. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "kprogressdialog.h" 0021 0022 #include <QLabel> 0023 #include <QLayout> 0024 #include <QTimer> 0025 0026 #include <kguiitem.h> 0027 #include <kstandardguiitem.h> 0028 0029 class Q_DECL_HIDDEN KProgressDialog::KProgressDialogPrivate 0030 { 0031 public: 0032 KProgressDialogPrivate(KProgressDialog *q) 0033 : q(q), 0034 cancelButtonShown(true), 0035 mAutoClose(true), 0036 mAutoReset(false), 0037 mCancelled(false), 0038 mAllowCancel(true), 0039 mShown(false), 0040 mMinDuration(2000) 0041 { 0042 } 0043 0044 void slotAutoShow(); 0045 void slotAutoActions(int percentage); 0046 0047 KProgressDialog *q; 0048 bool cancelButtonShown : 1; 0049 bool mAutoClose : 1; 0050 bool mAutoReset : 1; 0051 bool mCancelled : 1; 0052 bool mAllowCancel : 1; 0053 bool mShown : 1; 0054 QString mCancelText; 0055 QLabel *mLabel; 0056 QProgressBar *mProgressBar; 0057 QTimer *mShowTimer; 0058 int mMinDuration; 0059 }; 0060 0061 KProgressDialog::KProgressDialog(QWidget *parent, const QString &caption, 0062 const QString &text, Qt::WindowFlags flags) 0063 : KDialog(parent, flags), 0064 d(new KProgressDialogPrivate(this)) 0065 { 0066 setCaption(caption); 0067 setButtons(KDialog::Cancel); 0068 0069 d->mShowTimer = new QTimer(this); 0070 0071 d->mCancelText = KDialog::buttonText(KDialog::Cancel); 0072 0073 QWidget *mainWidget = new QWidget(this); 0074 QVBoxLayout *layout = new QVBoxLayout(mainWidget); 0075 layout->setMargin(10); 0076 0077 d->mLabel = new QLabel(text, mainWidget); 0078 layout->addWidget(d->mLabel); 0079 0080 d->mProgressBar = new QProgressBar(mainWidget); 0081 layout->addWidget(d->mProgressBar); 0082 0083 setMainWidget(mainWidget); 0084 0085 connect(d->mProgressBar, SIGNAL(valueChanged(int)), 0086 this, SLOT(slotAutoActions(int))); 0087 connect(d->mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow())); 0088 d->mShowTimer->setSingleShot(true); 0089 d->mShowTimer->start(d->mMinDuration); 0090 } 0091 0092 KProgressDialog::~KProgressDialog() 0093 { 0094 delete d; 0095 } 0096 0097 void KProgressDialog::KProgressDialogPrivate::slotAutoShow() 0098 { 0099 if (mShown || mCancelled) { 0100 return; 0101 } 0102 0103 q->show(); 0104 } 0105 0106 void KProgressDialog::showEvent(QShowEvent *event) 0107 { 0108 d->mShown = true; 0109 KDialog::showEvent(event); 0110 } 0111 0112 void KProgressDialog::reject() 0113 { 0114 d->mCancelled = true; 0115 0116 if (d->mAllowCancel) { 0117 KDialog::reject(); 0118 } 0119 } 0120 0121 bool KProgressDialog::wasCancelled() const 0122 { 0123 return d->mCancelled; 0124 } 0125 0126 void KProgressDialog::ignoreCancel() 0127 { 0128 d->mCancelled = false; 0129 } 0130 0131 void KProgressDialog::setMinimumDuration(int ms) 0132 { 0133 d->mMinDuration = ms; 0134 if (!d->mShown) { 0135 d->mShowTimer->stop(); 0136 d->mShowTimer->setSingleShot(true); 0137 d->mShowTimer->start(d->mMinDuration); 0138 } 0139 } 0140 0141 int KProgressDialog::minimumDuration() const 0142 { 0143 return d->mMinDuration; 0144 } 0145 0146 void KProgressDialog::setAllowCancel(bool allowCancel) 0147 { 0148 d->mAllowCancel = allowCancel; 0149 showCancelButton(allowCancel); 0150 } 0151 0152 bool KProgressDialog::allowCancel() const 0153 { 0154 return d->mAllowCancel; 0155 } 0156 0157 QProgressBar *KProgressDialog::progressBar() 0158 { 0159 return d->mProgressBar; 0160 } 0161 0162 const QProgressBar *KProgressDialog::progressBar() const 0163 { 0164 return d->mProgressBar; 0165 } 0166 0167 void KProgressDialog::setLabelText(const QString &text) 0168 { 0169 d->mLabel->setText(text); 0170 } 0171 0172 QString KProgressDialog::labelText() const 0173 { 0174 return d->mLabel->text(); 0175 } 0176 0177 void KProgressDialog::showCancelButton(bool show) 0178 { 0179 showButton(Cancel, show); 0180 } 0181 0182 bool KProgressDialog::autoClose() const 0183 { 0184 return d->mAutoClose; 0185 } 0186 0187 void KProgressDialog::setAutoClose(bool autoClose) 0188 { 0189 d->mAutoClose = autoClose; 0190 } 0191 0192 bool KProgressDialog::autoReset() const 0193 { 0194 return d->mAutoReset; 0195 } 0196 0197 void KProgressDialog::setAutoReset(bool autoReset) 0198 { 0199 d->mAutoReset = autoReset; 0200 } 0201 0202 void KProgressDialog::setButtonText(const QString &text) 0203 { 0204 d->mCancelText = text; 0205 setButtonGuiItem(Cancel, KGuiItem(text)); 0206 } 0207 0208 QString KProgressDialog::buttonText() const 0209 { 0210 return d->mCancelText; 0211 } 0212 0213 void KProgressDialog::KProgressDialogPrivate::slotAutoActions(int percentage) 0214 { 0215 if (percentage < mProgressBar->maximum() || 0216 (mProgressBar->minimum() == mProgressBar->maximum())) { // progress dialogs with busy indicators (see #178648) 0217 if (!cancelButtonShown) { 0218 q->setButtonGuiItem(KDialog::Cancel, KGuiItem(mCancelText)); 0219 cancelButtonShown = true; 0220 } 0221 return; 0222 } 0223 0224 mShowTimer->stop(); 0225 0226 if (mAutoReset) { 0227 mProgressBar->setValue(0); 0228 } else { 0229 q->setAllowCancel(true); 0230 q->setButtonGuiItem(Cancel, KStandardGuiItem::close()); 0231 cancelButtonShown = false; 0232 } 0233 0234 if (mAutoClose) { 0235 if (mShown) { 0236 q->hide(); 0237 } else { 0238 emit q->finished(); 0239 } 0240 } 0241 } 0242 0243 #include "moc_kprogressdialog.cpp"