1 | /* |
2 | Copyright (c) 2013 Montel Laurent <montel@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "progressindicatorwidget.h" |
21 | |
22 | #include <QTimer> |
23 | |
24 | namespace KPIMUtils { |
25 | |
26 | IndicatorProgress::IndicatorProgress(ProgressIndicatorWidget *widget, QObject *parent) |
27 | : QObject(parent), |
28 | mProgressCount(0), |
29 | mIndicator(widget), |
30 | mIsActive(false) |
31 | { |
32 | mProgressPix = KPixmapSequence(QLatin1String("process-working" ), KIconLoader::SizeSmallMedium); |
33 | mProgressTimer = new QTimer(this); |
34 | connect(mProgressTimer, SIGNAL(timeout()), this, SLOT(slotTimerDone())); |
35 | } |
36 | |
37 | IndicatorProgress::~IndicatorProgress() |
38 | { |
39 | } |
40 | |
41 | void IndicatorProgress::slotTimerDone() |
42 | { |
43 | mIndicator->setPixmap(mProgressPix.frameAt(mProgressCount)); |
44 | ++mProgressCount; |
45 | if (mProgressCount == 8) |
46 | mProgressCount = 0; |
47 | |
48 | mProgressTimer->start(300); |
49 | } |
50 | |
51 | void IndicatorProgress::startAnimation() |
52 | { |
53 | mProgressCount = 0; |
54 | mProgressTimer->start(300); |
55 | mIsActive = true; |
56 | } |
57 | |
58 | void IndicatorProgress::stopAnimation() |
59 | { |
60 | mIsActive = false; |
61 | if (mProgressTimer->isActive()) |
62 | mProgressTimer->stop(); |
63 | mIndicator->clear(); |
64 | } |
65 | |
66 | bool IndicatorProgress::isActive() const |
67 | { |
68 | return mIsActive; |
69 | } |
70 | |
71 | class ProgressIndicatorWidgetPrivate |
72 | { |
73 | public: |
74 | ProgressIndicatorWidgetPrivate(ProgressIndicatorWidget *qq) |
75 | : q(qq) |
76 | { |
77 | indicator = new IndicatorProgress(q); |
78 | } |
79 | |
80 | ~ProgressIndicatorWidgetPrivate() |
81 | { |
82 | delete indicator; |
83 | } |
84 | |
85 | IndicatorProgress *indicator; |
86 | ProgressIndicatorWidget *q; |
87 | }; |
88 | |
89 | ProgressIndicatorWidget::ProgressIndicatorWidget(QWidget *parent) |
90 | : QLabel(parent), |
91 | d(new ProgressIndicatorWidgetPrivate(this)) |
92 | { |
93 | setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) ); |
94 | } |
95 | |
96 | ProgressIndicatorWidget::~ProgressIndicatorWidget() |
97 | { |
98 | delete d; |
99 | } |
100 | |
101 | void ProgressIndicatorWidget::start() |
102 | { |
103 | d->indicator->startAnimation(); |
104 | } |
105 | |
106 | void ProgressIndicatorWidget::stop() |
107 | { |
108 | d->indicator->stopAnimation(); |
109 | } |
110 | |
111 | bool ProgressIndicatorWidget::isActive() const |
112 | { |
113 | return d->indicator->isActive(); |
114 | } |
115 | |
116 | |
117 | } |
118 | |
119 | |