krita

kis_label_progress.cc

00001 /*
00002  *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
00003  *                2004 Adrian Page <adrian@pagenet.plus.com>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 #include <qlayout.h>
00020 #include <qtooltip.h>
00021 #include <qtoolbutton.h>
00022 #include <qcursor.h>
00023 #include <qeventloop.h>
00024 
00025 #include <kdebug.h>
00026 #include <kapplication.h>
00027 #include <klocale.h>
00028 #include <kprogress.h>
00029 #include <kiconloader.h>
00030 
00031 #include "kis_progress_subject.h"
00032 #include "kis_label_progress.h"
00033 #include "kis_cursor.h"
00034 
00035 class EscapeButton : public QToolButton {
00036 
00037 public:
00038 
00039     EscapeButton(QWidget * parent, const char * name) : QToolButton(parent, name) {};
00040 
00041     void keyReleaseEvent(QKeyEvent *e)
00042     {
00043         if (e->key()==Qt::Key_Escape)
00044             emit clicked();
00045     }
00046 };
00047 
00048 KisLabelProgress::KisLabelProgress(QWidget *parent, const char *name, WFlags f) : super(parent, name, f)
00049 {
00050     m_subject = 0;
00051     m_modal = false;
00052 
00053     QHBoxLayout *box = new QHBoxLayout(this);
00054     box->setAutoAdd(true);
00055 
00056     QIconSet cancelIconSet = SmallIconSet("stop");
00057 
00058     m_cancelButton = new EscapeButton(this, "cancel_button");
00059     m_cancelButton->setIconSet(cancelIconSet);
00060     QToolTip::add(m_cancelButton, i18n("Cancel"));
00061     connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(cancelPressed()));
00062 
00063     m_bar = new KProgress(100, this);
00064 }
00065 
00066 KisLabelProgress::~KisLabelProgress()
00067 {
00068 }
00069 
00070 void KisLabelProgress::setSubject(KisProgressSubject *subject, bool modal, bool canCancel)
00071 {
00072     reset();
00073 
00074     if (subject) {
00075         m_subject = subject;
00076         m_modal = modal;
00077 
00078         connect(subject, SIGNAL(notifyProgress(int)), this, SLOT(update(int)));
00079         connect(subject, SIGNAL(notifyProgressStage(const QString&, int)), this, SLOT(updateStage(const QString&, int)));
00080         connect(subject, SIGNAL(notifyProgressDone()), this, SLOT(done()));
00081         connect(subject, SIGNAL(notifyProgressError()), this, SLOT(error()));
00082         connect(subject, SIGNAL(destroyed()), this, SLOT(subjectDestroyed()));
00083 
00084         show();
00085 
00086         if (canCancel) {
00087             if (modal) {
00088                 kdDebug() << "grabbing 1\n";
00089                 m_cancelButton->grabMouse();
00090                 m_cancelButton->grabKeyboard();
00091             }
00092         }
00093         else {
00094             m_cancelButton->hide();
00095 
00096             if (modal) {
00097                 // Only visible widgets can grab.
00098                 kdDebug() << "grabbing 2\n";
00099                 grabMouse();
00100                 grabKeyboard();
00101             }
00102         }
00103 
00104         if (modal) {
00105             QApplication::setOverrideCursor(KisCursor::waitCursor());
00106         }
00107 
00108         m_bar->setValue(0);
00109     }
00110 }
00111 
00112 bool KisLabelProgress::event(QEvent * e)
00113 {
00114 
00115     if (!e) return false;
00116 
00117     int type = e->type();
00118 
00119     switch (type) {
00120         case(KisProgress::ProgressEventBase + 1):
00121             {
00122                 KisProgress::UpdateEvent * ue = dynamic_cast<KisProgress::UpdateEvent*>(e);
00123                 update(ue->m_percent);
00124                 break;
00125             }
00126         case(KisProgress::ProgressEventBase + 2):
00127             {
00128                 KisProgress::UpdateStageEvent * use = dynamic_cast<KisProgress::UpdateStageEvent*>(e);
00129                 updateStage(use->m_stage, use->m_percent);
00130                 break;
00131             }
00132         case(KisProgress::ProgressEventBase + 3):
00133             done();
00134             break;
00135         case(KisProgress::ProgressEventBase + 4):
00136             error();
00137             break;
00138         case(KisProgress::ProgressEventBase + 5):
00139             subjectDestroyed();
00140             break;
00141         default:
00142             return QLabel::event(e);
00143     };
00144 
00145     return true;
00146 }
00147 
00148 void KisLabelProgress::reset()
00149 {
00150     if (m_subject) {
00151         m_subject->disconnect(this);
00152         m_subject = 0;
00153 
00154         if (m_modal) {
00155             QApplication::restoreOverrideCursor();
00156         }
00157 
00158         m_modal = false;
00159     }
00160 
00161     releaseMouse();
00162     releaseKeyboard();
00163     m_cancelButton->releaseMouse();
00164     m_cancelButton->releaseKeyboard();
00165     hide();
00166 }
00167 
00168 void KisLabelProgress::update(int percent)
00169 {
00170     m_bar->setValue(percent);
00171 
00172     KApplication *app = KApplication::kApplication();
00173 
00174     app->processEvents();
00175     // The following is safer, but makes cancel impossible:
00176     //QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput |
00177     //                                         QEventLoop::ExcludeSocketNotifiers);
00178 }
00179 
00180 void KisLabelProgress::updateStage(const QString&, int percent)
00181 {
00182     m_bar->setValue(percent);
00183 
00184     KApplication *app = KApplication::kApplication();
00185     Q_ASSERT(app);
00186 
00187     app->processEvents();
00188 }
00189 
00190 void KisLabelProgress::cancelPressed()
00191 {
00192     if (m_subject) {
00193         m_subject->cancel();
00194         reset();
00195     }
00196 }
00197 
00198 void KisLabelProgress::subjectDestroyed()
00199 {
00200     reset();
00201 }
00202 
00203 void KisLabelProgress::done()
00204 {
00205     reset();
00206 }
00207 
00208 void KisLabelProgress::error()
00209 {
00210     reset();
00211 }
00212 
00213 #include "kis_label_progress.moc"
00214 
KDE Home | KDE Accessibility Home | Description of Access Keys