Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qprogressbar.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | - | |||||||||||||
7 | class QProgressBarPrivate : public QWidgetPrivate | - | ||||||||||||
8 | { | - | ||||||||||||
9 | inline QProgressBar* q_func() { return static_cast<QProgressBar *>(q_ptr); } inline const QProgressBar* q_func() const { return static_cast<const QProgressBar *>(q_ptr); } friend class QProgressBar; | - | ||||||||||||
10 | - | |||||||||||||
11 | public: | - | ||||||||||||
12 | QProgressBarPrivate(); | - | ||||||||||||
13 | - | |||||||||||||
14 | void init(); | - | ||||||||||||
15 | void initDefaultFormat(); | - | ||||||||||||
16 | inline void resetLayoutItemMargins(); | - | ||||||||||||
17 | - | |||||||||||||
18 | int minimum; | - | ||||||||||||
19 | int maximum; | - | ||||||||||||
20 | int value; | - | ||||||||||||
21 | Qt::Alignment alignment; | - | ||||||||||||
22 | uint textVisible : 1; | - | ||||||||||||
23 | uint defaultFormat: 1; | - | ||||||||||||
24 | int lastPaintedValue; | - | ||||||||||||
25 | Qt::Orientation orientation; | - | ||||||||||||
26 | bool invertedAppearance; | - | ||||||||||||
27 | QProgressBar::Direction textDirection; | - | ||||||||||||
28 | QString format; | - | ||||||||||||
29 | inline int bound(int val) const { return qMax(minimum-1, qMin(maximum, val)); } | - | ||||||||||||
30 | bool repaintRequired() const; | - | ||||||||||||
31 | }; | - | ||||||||||||
32 | - | |||||||||||||
33 | QProgressBarPrivate::QProgressBarPrivate() | - | ||||||||||||
34 | : minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true), | - | ||||||||||||
35 | defaultFormat(true), lastPaintedValue(-1), orientation(Qt::Horizontal), invertedAppearance(false), | - | ||||||||||||
36 | textDirection(QProgressBar::TopToBottom) | - | ||||||||||||
37 | { | - | ||||||||||||
38 | initDefaultFormat(); | - | ||||||||||||
39 | } | - | ||||||||||||
40 | - | |||||||||||||
41 | void QProgressBarPrivate::initDefaultFormat() | - | ||||||||||||
42 | { | - | ||||||||||||
43 | if (defaultFormat) | - | ||||||||||||
44 | format = QLatin1String("%p") + locale.percent(); | - | ||||||||||||
45 | } | - | ||||||||||||
46 | - | |||||||||||||
47 | void QProgressBarPrivate::init() | - | ||||||||||||
48 | { | - | ||||||||||||
49 | QProgressBar * const q = q_func(); | - | ||||||||||||
50 | QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed); | - | ||||||||||||
51 | if (orientation == Qt::Vertical) | - | ||||||||||||
52 | sp.transpose(); | - | ||||||||||||
53 | q->setSizePolicy(sp); | - | ||||||||||||
54 | q->setAttribute(Qt::WA_WState_OwnSizePolicy, false); | - | ||||||||||||
55 | resetLayoutItemMargins(); | - | ||||||||||||
56 | } | - | ||||||||||||
57 | - | |||||||||||||
58 | void QProgressBarPrivate::resetLayoutItemMargins() | - | ||||||||||||
59 | { | - | ||||||||||||
60 | QProgressBar * const q = q_func(); | - | ||||||||||||
61 | QStyleOptionProgressBar option; | - | ||||||||||||
62 | q->initStyleOption(&option); | - | ||||||||||||
63 | setLayoutItemMargins(QStyle::SE_ProgressBarLayoutItem, &option); | - | ||||||||||||
64 | } | - | ||||||||||||
65 | void QProgressBar::initStyleOption(QStyleOptionProgressBar *option) const | - | ||||||||||||
66 | { | - | ||||||||||||
67 | if (!option) | - | ||||||||||||
68 | return; | - | ||||||||||||
69 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
70 | option->initFrom(this); | - | ||||||||||||
71 | - | |||||||||||||
72 | if (d->orientation == Qt::Horizontal) | - | ||||||||||||
73 | option->state |= QStyle::State_Horizontal; | - | ||||||||||||
74 | option->minimum = d->minimum; | - | ||||||||||||
75 | option->maximum = d->maximum; | - | ||||||||||||
76 | option->progress = d->value; | - | ||||||||||||
77 | option->textAlignment = d->alignment; | - | ||||||||||||
78 | option->textVisible = d->textVisible; | - | ||||||||||||
79 | option->text = text(); | - | ||||||||||||
80 | option->orientation = d->orientation; | - | ||||||||||||
81 | option->invertedAppearance = d->invertedAppearance; | - | ||||||||||||
82 | option->bottomToTop = d->textDirection == QProgressBar::BottomToTop; | - | ||||||||||||
83 | } | - | ||||||||||||
84 | - | |||||||||||||
85 | bool QProgressBarPrivate::repaintRequired() const | - | ||||||||||||
86 | { | - | ||||||||||||
87 | const QProgressBar * const q = q_func(); | - | ||||||||||||
88 | if (value == lastPaintedValue
| 0 | ||||||||||||
89 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||
90 | - | |||||||||||||
91 | const qint64int valueDifference = qAbs(qint64(value )- lastPaintedValue); | - | ||||||||||||
92 | - | |||||||||||||
93 | - | |||||||||||||
94 | if (value == minimum
| 0 | ||||||||||||
95 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||
const qint64 totalSteps = qint64(maximum) - minimum never executed: return true; never executed: ;return true; never executed: return true; | ||||||||||||||
96 | if (textVisible
| 0 | ||||||||||||
97 | if ((
| 0 | ||||||||||||
98 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||
99 | if ((format.contains(QLatin1String("%p"))
| 0 | ||||||||||||
100 | && valueDifference >= qAbs(totalSteps((maximum - minimum) / 100)
| 0 | ||||||||||||
101 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||
102 | } never executed: end of block | 0 | ||||||||||||
103 | - | |||||||||||||
104 | - | |||||||||||||
105 | QStyleOptionProgressBar opt; | - | ||||||||||||
106 | q->initStyleOption(&opt); | - | ||||||||||||
107 | int cw = q->style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, q); | - | ||||||||||||
108 | QRect groove = q->style()->subElementRect(QStyle::SE_ProgressBarGroove, &opt, q); | - | ||||||||||||
109 | - | |||||||||||||
110 | - | |||||||||||||
111 | - | |||||||||||||
112 | int grooveBlock = (
| 0 | ||||||||||||
113 | return never executed: (valueDifference * grooveBlock > cw * totalSteps;(maximum - minimum));return (valueDifference * grooveBlock > cw * (maximum - minimum)); never executed: return (valueDifference * grooveBlock > cw * (maximum - minimum)); | 0 | ||||||||||||
114 | } | - | ||||||||||||
115 | QProgressBar::QProgressBar(QWidget *parent) | - | ||||||||||||
116 | : QWidget(*(new QProgressBarPrivate), parent, 0) | - | ||||||||||||
117 | { | - | ||||||||||||
118 | d_func()->init(); | - | ||||||||||||
119 | } | - | ||||||||||||
120 | - | |||||||||||||
121 | - | |||||||||||||
122 | - | |||||||||||||
123 | - | |||||||||||||
124 | QProgressBar::~QProgressBar() | - | ||||||||||||
125 | { | - | ||||||||||||
126 | } | - | ||||||||||||
127 | - | |||||||||||||
128 | - | |||||||||||||
129 | - | |||||||||||||
130 | - | |||||||||||||
131 | - | |||||||||||||
132 | - | |||||||||||||
133 | void QProgressBar::reset() | - | ||||||||||||
134 | { | - | ||||||||||||
135 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
136 | d->value = d->minimum - 1; | - | ||||||||||||
137 | if (d->minimum == (-2147483647 - 1)
| 0 | ||||||||||||
138 | d->value = (-2147483647 - 1); never executed: d->value = (-2147483647 - 1); | 0 | ||||||||||||
139 | else | - | ||||||||||||
d->value = d->minimum - 1;repaint(); | ||||||||||||||
140 | } never executed: end of block | 0 | ||||||||||||
141 | void QProgressBar::setMinimum(int minimum) | - | ||||||||||||
142 | { | - | ||||||||||||
143 | setRange(minimum, qMax(d_func()->maximum, minimum)); | - | ||||||||||||
144 | } | - | ||||||||||||
145 | - | |||||||||||||
146 | int QProgressBar::minimum() const | - | ||||||||||||
147 | { | - | ||||||||||||
148 | return d_func()->minimum; | - | ||||||||||||
149 | } | - | ||||||||||||
150 | void QProgressBar::setMaximum(int maximum) | - | ||||||||||||
151 | { | - | ||||||||||||
152 | setRange(qMin(d_func()->minimum, maximum), maximum); | - | ||||||||||||
153 | } | - | ||||||||||||
154 | - | |||||||||||||
155 | int QProgressBar::maximum() const | - | ||||||||||||
156 | { | - | ||||||||||||
157 | return d_func()->maximum; | - | ||||||||||||
158 | } | - | ||||||||||||
159 | void QProgressBar::setValue(int value) | - | ||||||||||||
160 | { | - | ||||||||||||
161 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
162 | if (d->value == value | - | ||||||||||||
163 | || ((value > d->maximum || value < d->minimum) | - | ||||||||||||
164 | && (d->maximum != 0 || d->minimum != 0))) | - | ||||||||||||
165 | return; | - | ||||||||||||
166 | d->value = value; | - | ||||||||||||
167 | valueChanged(value); | - | ||||||||||||
168 | - | |||||||||||||
169 | if (isVisible()) { | - | ||||||||||||
170 | QAccessibleValueChangeEvent event(this, value); | - | ||||||||||||
171 | QAccessible::updateAccessibility(&event); | - | ||||||||||||
172 | } | - | ||||||||||||
173 | - | |||||||||||||
174 | if (d->repaintRequired()) | - | ||||||||||||
175 | repaint(); | - | ||||||||||||
176 | } | - | ||||||||||||
177 | - | |||||||||||||
178 | int QProgressBar::value() const | - | ||||||||||||
179 | { | - | ||||||||||||
180 | return d_func()->value; | - | ||||||||||||
181 | } | - | ||||||||||||
182 | void QProgressBar::setRange(int minimum, int maximum) | - | ||||||||||||
183 | { | - | ||||||||||||
184 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
185 | if (minimum != d->minimum
| 0 | ||||||||||||
186 | d->minimum = minimum; | - | ||||||||||||
187 | d->maximum = qMax(minimum, maximum); | - | ||||||||||||
188 | - | |||||||||||||
189 | if (d->value < qint64(d->minimum )- 1)
| 0 | ||||||||||||
190 | reset(); never executed: reset(); | 0 | ||||||||||||
191 | else | - | ||||||||||||
192 | update(); never executed: update(); | 0 | ||||||||||||
193 | } | - | ||||||||||||
194 | } never executed: end of block | 0 | ||||||||||||
195 | void QProgressBar::setTextVisible(bool visible) | - | ||||||||||||
196 | { | - | ||||||||||||
197 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
198 | if (d->textVisible != visible) { | - | ||||||||||||
199 | d->textVisible = visible; | - | ||||||||||||
200 | repaint(); | - | ||||||||||||
201 | } | - | ||||||||||||
202 | } | - | ||||||||||||
203 | - | |||||||||||||
204 | bool QProgressBar::isTextVisible() const | - | ||||||||||||
205 | { | - | ||||||||||||
206 | return d_func()->textVisible; | - | ||||||||||||
207 | } | - | ||||||||||||
208 | - | |||||||||||||
209 | - | |||||||||||||
210 | - | |||||||||||||
211 | - | |||||||||||||
212 | - | |||||||||||||
213 | void QProgressBar::setAlignment(Qt::Alignment alignment) | - | ||||||||||||
214 | { | - | ||||||||||||
215 | if (d_func()->alignment != alignment) { | - | ||||||||||||
216 | d_func()->alignment = alignment; | - | ||||||||||||
217 | repaint(); | - | ||||||||||||
218 | } | - | ||||||||||||
219 | } | - | ||||||||||||
220 | - | |||||||||||||
221 | Qt::Alignment QProgressBar::alignment() const | - | ||||||||||||
222 | { | - | ||||||||||||
223 | return d_func()->alignment; | - | ||||||||||||
224 | } | - | ||||||||||||
225 | - | |||||||||||||
226 | - | |||||||||||||
227 | - | |||||||||||||
228 | - | |||||||||||||
229 | void QProgressBar::paintEvent(QPaintEvent *) | - | ||||||||||||
230 | { | - | ||||||||||||
231 | QStylePainter paint(this); | - | ||||||||||||
232 | QStyleOptionProgressBar opt; | - | ||||||||||||
233 | initStyleOption(&opt); | - | ||||||||||||
234 | paint.drawControl(QStyle::CE_ProgressBar, opt); | - | ||||||||||||
235 | d_func()->lastPaintedValue = d_func()->value; | - | ||||||||||||
236 | } | - | ||||||||||||
237 | - | |||||||||||||
238 | - | |||||||||||||
239 | - | |||||||||||||
240 | - | |||||||||||||
241 | QSize QProgressBar::sizeHint() const | - | ||||||||||||
242 | { | - | ||||||||||||
243 | ensurePolished(); | - | ||||||||||||
244 | QFontMetrics fm = fontMetrics(); | - | ||||||||||||
245 | QStyleOptionProgressBar opt; | - | ||||||||||||
246 | initStyleOption(&opt); | - | ||||||||||||
247 | int cw = style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, this); | - | ||||||||||||
248 | QSize size = QSize(qMax(9, cw) * 7 + fm.width(QLatin1Char('0')) * 4, fm.height() + 8); | - | ||||||||||||
249 | if (opt.orientation == Qt::Vertical) | - | ||||||||||||
250 | size = size.transposed(); | - | ||||||||||||
251 | return style()->sizeFromContents(QStyle::CT_ProgressBar, &opt, size, this); | - | ||||||||||||
252 | } | - | ||||||||||||
253 | - | |||||||||||||
254 | - | |||||||||||||
255 | - | |||||||||||||
256 | - | |||||||||||||
257 | QSize QProgressBar::minimumSizeHint() const | - | ||||||||||||
258 | { | - | ||||||||||||
259 | QSize size; | - | ||||||||||||
260 | if (orientation() == Qt::Horizontal) | - | ||||||||||||
261 | size = QSize(sizeHint().width(), fontMetrics().height() + 2); | - | ||||||||||||
262 | else | - | ||||||||||||
263 | size = QSize(fontMetrics().height() + 2, sizeHint().height()); | - | ||||||||||||
264 | return size; | - | ||||||||||||
265 | } | - | ||||||||||||
266 | QString QProgressBar::text() const | - | ||||||||||||
267 | { | - | ||||||||||||
268 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
269 | if ((d->maximum == 0 && d->minimum == 0) || d->value < d->minimum | - | ||||||||||||
270 | || (d->value == (-2147483647 - 1) && d->minimum == (-2147483647 - 1))) | - | ||||||||||||
271 | return QString(); | - | ||||||||||||
272 | - | |||||||||||||
273 | qint64 totalSteps = qint64(d->maximum) - d->minimum; | - | ||||||||||||
274 | - | |||||||||||||
275 | QString result = d->format; | - | ||||||||||||
276 | QLocale locale = d->locale; | - | ||||||||||||
277 | locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator); | - | ||||||||||||
278 | result.replace(QLatin1String("%m"), locale.toString(totalSteps)); | - | ||||||||||||
279 | result.replace(QLatin1String("%v"), locale.toString(d->value)); | - | ||||||||||||
280 | - | |||||||||||||
281 | - | |||||||||||||
282 | - | |||||||||||||
283 | - | |||||||||||||
284 | if (totalSteps == 0) { | - | ||||||||||||
285 | result.replace(QLatin1String("%p"), locale.toString(int(100))); | - | ||||||||||||
286 | return result; | - | ||||||||||||
287 | } | - | ||||||||||||
288 | - | |||||||||||||
289 | int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps; | - | ||||||||||||
290 | result.replace(QLatin1String("%p"), locale.toString(progress)); | - | ||||||||||||
291 | return result; | - | ||||||||||||
292 | } | - | ||||||||||||
293 | void QProgressBar::setOrientation(Qt::Orientation orientation) | - | ||||||||||||
294 | { | - | ||||||||||||
295 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
296 | if (d->orientation == orientation) | - | ||||||||||||
297 | return; | - | ||||||||||||
298 | d->orientation = orientation; | - | ||||||||||||
299 | if (!testAttribute(Qt::WA_WState_OwnSizePolicy)) { | - | ||||||||||||
300 | QSizePolicy sp = sizePolicy(); | - | ||||||||||||
301 | sp.transpose(); | - | ||||||||||||
302 | setSizePolicy(sp); | - | ||||||||||||
303 | setAttribute(Qt::WA_WState_OwnSizePolicy, false); | - | ||||||||||||
304 | } | - | ||||||||||||
305 | d->resetLayoutItemMargins(); | - | ||||||||||||
306 | update(); | - | ||||||||||||
307 | updateGeometry(); | - | ||||||||||||
308 | } | - | ||||||||||||
309 | - | |||||||||||||
310 | Qt::Orientation QProgressBar::orientation() const | - | ||||||||||||
311 | { | - | ||||||||||||
312 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
313 | return d->orientation; | - | ||||||||||||
314 | } | - | ||||||||||||
315 | void QProgressBar::setInvertedAppearance(bool invert) | - | ||||||||||||
316 | { | - | ||||||||||||
317 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
318 | d->invertedAppearance = invert; | - | ||||||||||||
319 | update(); | - | ||||||||||||
320 | } | - | ||||||||||||
321 | - | |||||||||||||
322 | bool QProgressBar::invertedAppearance() const | - | ||||||||||||
323 | { | - | ||||||||||||
324 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
325 | return d->invertedAppearance; | - | ||||||||||||
326 | } | - | ||||||||||||
327 | void QProgressBar::setTextDirection(QProgressBar::Direction textDirection) | - | ||||||||||||
328 | { | - | ||||||||||||
329 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
330 | d->textDirection = textDirection; | - | ||||||||||||
331 | update(); | - | ||||||||||||
332 | } | - | ||||||||||||
333 | - | |||||||||||||
334 | QProgressBar::Direction QProgressBar::textDirection() const | - | ||||||||||||
335 | { | - | ||||||||||||
336 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
337 | return d->textDirection; | - | ||||||||||||
338 | } | - | ||||||||||||
339 | - | |||||||||||||
340 | - | |||||||||||||
341 | bool QProgressBar::event(QEvent *e) | - | ||||||||||||
342 | { | - | ||||||||||||
343 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
344 | switch (e->type()) { | - | ||||||||||||
345 | case QEvent::StyleChange: | - | ||||||||||||
346 | - | |||||||||||||
347 | - | |||||||||||||
348 | - | |||||||||||||
349 | d->resetLayoutItemMargins(); | - | ||||||||||||
350 | break; | - | ||||||||||||
351 | case QEvent::LocaleChange: | - | ||||||||||||
352 | d->initDefaultFormat(); | - | ||||||||||||
353 | break; | - | ||||||||||||
354 | default: | - | ||||||||||||
355 | break; | - | ||||||||||||
356 | } | - | ||||||||||||
357 | return QWidget::event(e); | - | ||||||||||||
358 | } | - | ||||||||||||
359 | void QProgressBar::setFormat(const QString &format) | - | ||||||||||||
360 | { | - | ||||||||||||
361 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
362 | if (d->format == format) | - | ||||||||||||
363 | return; | - | ||||||||||||
364 | d->format = format; | - | ||||||||||||
365 | d->defaultFormat = false; | - | ||||||||||||
366 | update(); | - | ||||||||||||
367 | } | - | ||||||||||||
368 | - | |||||||||||||
369 | void QProgressBar::resetFormat() | - | ||||||||||||
370 | { | - | ||||||||||||
371 | QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
372 | d->defaultFormat = true; | - | ||||||||||||
373 | d->initDefaultFormat(); | - | ||||||||||||
374 | update(); | - | ||||||||||||
375 | } | - | ||||||||||||
376 | - | |||||||||||||
377 | QString QProgressBar::format() const | - | ||||||||||||
378 | { | - | ||||||||||||
379 | const QProgressBarPrivate * const d = d_func(); | - | ||||||||||||
380 | return d->format; | - | ||||||||||||
381 | } | - | ||||||||||||
382 | - | |||||||||||||
383 | - | |||||||||||||
384 | - | |||||||||||||
Switch to Source code | Preprocessed file |