chalk

kis_label_progress.cpp
1/*
2 * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
3 * 2004 Adrian Page <adrian@pagenet.plus.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include <tqlayout.h>
20#include <tqtooltip.h>
21#include <tqtoolbutton.h>
22#include <tqcursor.h>
23#include <tqeventloop.h>
24
25#include <kdebug.h>
26#include <tdeapplication.h>
27#include <tdelocale.h>
28#include <kprogress.h>
29#include <kiconloader.h>
30
31#include "kis_progress_subject.h"
32#include "kis_label_progress.h"
33#include "kis_cursor.h"
34
35class EscapeButton : public TQToolButton {
36
37public:
38
39 EscapeButton(TQWidget * parent, const char * name) : TQToolButton(parent, name) {};
40
41 void keyReleaseEvent(TQKeyEvent *e)
42 {
43 if (e->key()==TQt::Key_Escape)
44 emit clicked();
45 }
46};
47
48KisLabelProgress::KisLabelProgress(TQWidget *parent, const char *name, WFlags f) : super(parent, name, f)
49{
50 m_subject = 0;
51 m_modal = false;
52
53 TQHBoxLayout *box = new TQHBoxLayout(this);
54 box->setAutoAdd(true);
55
56 TQIconSet cancelIconSet = SmallIconSet("process-stop");
57
58 m_cancelButton = new EscapeButton(this, "cancel_button");
59 m_cancelButton->setIconSet(cancelIconSet);
60 TQToolTip::add(m_cancelButton, i18n("Cancel"));
61 connect(m_cancelButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(cancelPressed()));
62
63 m_bar = new KProgress(100, this);
64}
65
66KisLabelProgress::~KisLabelProgress()
67{
68}
69
70void KisLabelProgress::setSubject(KisProgressSubject *subject, bool modal, bool canCancel)
71{
72 reset();
73
74 if (subject) {
75 m_subject = subject;
76 m_modal = modal;
77
78 connect(subject, TQ_SIGNAL(notifyProgress(int)), this, TQ_SLOT(update(int)));
79 connect(subject, TQ_SIGNAL(notifyProgressStage(const TQString&, int)), this, TQ_SLOT(updateStage(const TQString&, int)));
80 connect(subject, TQ_SIGNAL(notifyProgressDone()), this, TQ_SLOT(done()));
81 connect(subject, TQ_SIGNAL(notifyProgressError()), this, TQ_SLOT(error()));
82 connect(subject, TQ_SIGNAL(destroyed()), this, TQ_SLOT(subjectDestroyed()));
83
84 show();
85
86 if (canCancel) {
87 if (modal) {
88 kdDebug() << "grabbing 1\n";
89 m_cancelButton->grabMouse();
90 m_cancelButton->grabKeyboard();
91 }
92 }
93 else {
94 m_cancelButton->hide();
95
96 if (modal) {
97 // Only visible widgets can grab.
98 kdDebug() << "grabbing 2\n";
99 grabMouse();
100 grabKeyboard();
101 }
102 }
103
104 if (modal) {
105 TQApplication::setOverrideCursor(KisCursor::waitCursor());
106 }
107
108 m_bar->setValue(0);
109 }
110}
111
112bool KisLabelProgress::event(TQEvent * e)
113{
114
115 if (!e) return false;
116
117 int type = e->type();
118
119 switch (type) {
120 case(KisProgress::ProgressEventBase + 1):
121 {
122 KisProgress::UpdateEvent * ue = dynamic_cast<KisProgress::UpdateEvent*>(e);
123 update(ue->m_percent);
124 break;
125 }
126 case(KisProgress::ProgressEventBase + 2):
127 {
128 KisProgress::UpdateStageEvent * use = dynamic_cast<KisProgress::UpdateStageEvent*>(e);
129 updateStage(use->m_stage, use->m_percent);
130 break;
131 }
132 case(KisProgress::ProgressEventBase + 3):
133 done();
134 break;
135 case(KisProgress::ProgressEventBase + 4):
136 error();
137 break;
138 case(KisProgress::ProgressEventBase + 5):
139 subjectDestroyed();
140 break;
141 default:
142 return TQLabel::event(e);
143 };
144
145 return true;
146}
147
148void KisLabelProgress::reset()
149{
150 if (m_subject) {
151 m_subject->disconnect(this);
152 m_subject = 0;
153
154 if (m_modal) {
155 TQApplication::restoreOverrideCursor();
156 }
157
158 m_modal = false;
159 }
160
161 releaseMouse();
162 releaseKeyboard();
163 m_cancelButton->releaseMouse();
164 m_cancelButton->releaseKeyboard();
165 hide();
166}
167
168void KisLabelProgress::update(int percent)
169{
170 m_bar->setValue(percent);
171
172 TDEApplication *app = TDEApplication::kApplication();
173
174 app->processEvents();
175 // The following is safer, but makes cancel impossible:
176 //TQApplication::eventLoop()->processEvents(TQEventLoop::ExcludeUserInput |
177 // TQEventLoop::ExcludeSocketNotifiers);
178}
179
180void KisLabelProgress::updateStage(const TQString&, int percent)
181{
182 m_bar->setValue(percent);
183
184 TDEApplication *app = TDEApplication::kApplication();
185 Q_ASSERT(app);
186
187 app->processEvents();
188}
189
190void KisLabelProgress::cancelPressed()
191{
192 if (m_subject) {
193 m_subject->cancel();
194 reset();
195 }
196}
197
198void KisLabelProgress::subjectDestroyed()
199{
200 reset();
201}
202
203void KisLabelProgress::done()
204{
205 reset();
206}
207
208void KisLabelProgress::error()
209{
210 reset();
211}
212
213#include "kis_label_progress.moc"
214