taskbarprogress.cpp
1 //------------------------------------------------------------------------------
2 // taskbarprogress.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "taskbarprogress.h"
24 
25 #include <cassert>
26 #include <QSysInfo>
27 
28 // [Zalewa] I suppose the #ifdef checks could be a bit more abstract
29 // just in case if we ever have another platform that supports similar
30 // functionality, but let's worry about that when it comes to that.
31 
32 #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) && defined(Q_OS_WIN32)
33 #define WIN_TASKBAR
34 #endif
35 
36 #ifdef WIN_TASKBAR
37 #include <QWinTaskbarProgress>
38 #endif
39 
40 DClass<TaskbarProgress>
41 {
42 public:
43  #ifdef WIN_TASKBAR
44  QWinTaskbarProgress *progress;
45  #endif
46 
47  bool isAllowedOsVersion() const
48  {
49  #ifdef WIN_TASKBAR
50  return QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS7;
51  #else
52  return false;
53  #endif
54  }
55 };
56 DPointered(TaskbarProgress)
57 
58 TaskbarProgress::TaskbarProgress(QWinTaskbarProgress *progress, QObject *parent)
59  : QObject(parent)
60 {
61  #ifdef WIN_TASKBAR
62  d->progress = nullptr;
63  assert(d->isAllowedOsVersion());
64  if (d->isAllowedOsVersion())
65  d->progress = progress;
66  #else
67  Q_UNUSED(progress);
68  #endif
69  construct();
70 }
71 
72 TaskbarProgress::TaskbarProgress(QObject *parent)
73  : QObject(parent)
74 {
75  #ifdef WIN_TASKBAR
76  d->progress = nullptr;
77  if (d->isAllowedOsVersion())
78  d->progress = new QWinTaskbarProgress(this);
79 
80  #endif
81  construct();
82 }
83 
84 void TaskbarProgress::construct()
85 {
86  #ifdef WIN_TASKBAR
87  if (d->progress != nullptr)
88  {
89  this->connect(d->progress, SIGNAL(maximumChanged(int)), SIGNAL(maximumChanged(int)));
90  this->connect(d->progress, SIGNAL(minimumChanged(int)), SIGNAL(minimumChanged(int)));
91  this->connect(d->progress, SIGNAL(valueChanged(int)), SIGNAL(valueChanged(int)));
92  this->connect(d->progress, SIGNAL(visibilityChanged(bool)), SIGNAL(visibilityChanged(bool)));
93  }
94  #endif
95 }
96 
97 bool TaskbarProgress::isPaused() const
98 {
99  #ifdef WIN_TASKBAR
100  if (d->progress != nullptr)
101  return d->progress->isPaused();
102 
103  #endif
104  return false;
105 }
106 
107 bool TaskbarProgress::isStopped() const
108 {
109  #ifdef WIN_TASKBAR
110  if (d->progress != nullptr)
111  return d->progress->isStopped();
112 
113  #endif
114  return false;
115 }
116 
117 bool TaskbarProgress::isVisible() const
118 {
119  #ifdef WIN_TASKBAR
120  if (d->progress != nullptr)
121  return d->progress->isVisible();
122 
123  #endif
124  return false;
125 }
126 
127 int TaskbarProgress::maximum() const
128 {
129  #ifdef WIN_TASKBAR
130  if (d->progress != nullptr)
131  return d->progress->maximum();
132 
133  #endif
134  return 0;
135 }
136 
137 int TaskbarProgress::minimum() const
138 {
139  #ifdef WIN_TASKBAR
140  if (d->progress != nullptr)
141  return d->progress->minimum();
142 
143  #endif
144  return 0;
145 }
146 
147 int TaskbarProgress::value() const
148 {
149  #ifdef WIN_TASKBAR
150  if (d->progress != nullptr)
151  return d->progress->value();
152 
153  #endif
154  return 0;
155 }
156 
157 void TaskbarProgress::hide()
158 {
159  #ifdef WIN_TASKBAR
160  if (d->progress != nullptr)
161  d->progress->hide();
162 
163  #endif
164 }
165 
166 void TaskbarProgress::pause()
167 {
168  #ifdef WIN_TASKBAR
169  if (d->progress != nullptr)
170  d->progress->pause();
171 
172  #endif
173 }
174 
175 void TaskbarProgress::reset()
176 {
177  #ifdef WIN_TASKBAR
178  if (d->progress != nullptr)
179  d->progress->reset();
180 
181  #endif
182 }
183 
184 void TaskbarProgress::resume()
185 {
186  #ifdef WIN_TASKBAR
187  if (d->progress != nullptr)
188  d->progress->resume();
189 
190  #endif
191 }
192 
193 void TaskbarProgress::setMaximum(int maximum)
194 {
195  #ifdef WIN_TASKBAR
196  if (d->progress != nullptr)
197  d->progress->setMaximum(maximum);
198  #else
199  Q_UNUSED(maximum)
200  #endif
201 }
202 
203 void TaskbarProgress::setMinimum(int minimum)
204 {
205  #ifdef WIN_TASKBAR
206  if (d->progress != nullptr)
207  d->progress->setMinimum(minimum);
208  #else
209  Q_UNUSED(minimum);
210  #endif
211 }
212 
213 void TaskbarProgress::setPaused(bool paused)
214 {
215  #ifdef WIN_TASKBAR
216  if (d->progress != nullptr)
217  d->progress->setPaused(paused);
218  #else
219  Q_UNUSED(paused);
220  #endif
221 }
222 
223 void TaskbarProgress::setRange(int minimum, int maximum)
224 {
225  #ifdef WIN_TASKBAR
226  if (d->progress != nullptr)
227  d->progress->setRange(minimum, maximum);
228  #else
229  Q_UNUSED(minimum);
230  Q_UNUSED(maximum);
231  #endif
232 }
233 
234 void TaskbarProgress::setValue(int value)
235 {
236  #ifdef WIN_TASKBAR
237  if (d->progress != nullptr)
238  d->progress->setValue(value);
239  #else
240  Q_UNUSED(value);
241  #endif
242 }
243 
244 void TaskbarProgress::setVisible(bool visible)
245 {
246  #ifdef WIN_TASKBAR
247  if (d->progress != nullptr)
248  d->progress->setVisible(visible);
249  #else
250  Q_UNUSED(visible);
251  #endif
252 }
253 
254 void TaskbarProgress::show()
255 {
256  #ifdef WIN_TASKBAR
257  if (d->progress != nullptr)
258  d->progress->show();
259 
260  #endif
261 }
262 
263 void TaskbarProgress::stop()
264 {
265  #ifdef WIN_TASKBAR
266  if (d->progress != nullptr)
267  d->progress->stop();
268 
269  #endif
270 }
Platform-agnostic wrapper for QWinTaskbarProgress.