Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #include "qprogressbar.h" | - |
41 | #ifndef QT_NO_PROGRESSBAR | - |
42 | #include <qlocale.h> | - |
43 | #include <qevent.h> | - |
44 | #include <qpainter.h> | - |
45 | #include <qstylepainter.h> | - |
46 | #include <qstyleoption.h> | - |
47 | #include <private/qwidget_p.h> | - |
48 | #ifndef QT_NO_ACCESSIBILITY | - |
49 | #include <qaccessible.h> | - |
50 | #endif | - |
51 | #include <limits.h> | - |
52 | | - |
53 | QT_BEGIN_NAMESPACE | - |
54 | | - |
55 | class QProgressBarPrivate : public QWidgetPrivate | - |
56 | { | - |
57 | Q_DECLARE_PUBLIC(QProgressBar) | - |
58 | | - |
59 | public: | - |
60 | QProgressBarPrivate(); | - |
61 | | - |
62 | void init(); | - |
63 | void initDefaultFormat(); | - |
64 | inline void resetLayoutItemMargins(); | - |
65 | | - |
66 | int minimum; | - |
67 | int maximum; | - |
68 | int value; | - |
69 | Qt::Alignment alignment; | - |
70 | uint textVisible : 1; | - |
71 | uint defaultFormat: 1; | - |
72 | int lastPaintedValue; | - |
73 | Qt::Orientation orientation; | - |
74 | bool invertedAppearance; | - |
75 | QProgressBar::Direction textDirection; | - |
76 | QString format; | - |
77 | inline int bound(int val) const { return qMax(minimum-1, qMin(maximum, val)); } | - |
78 | bool repaintRequired() const; | - |
79 | }; | - |
80 | | - |
81 | QProgressBarPrivate::QProgressBarPrivate() | - |
82 | : minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true), | - |
83 | defaultFormat(true), lastPaintedValue(-1), orientation(Qt::Horizontal), invertedAppearance(false), | - |
84 | textDirection(QProgressBar::TopToBottom) | - |
85 | { | - |
86 | initDefaultFormat(); | - |
87 | } | - |
88 | | - |
89 | void QProgressBarPrivate::initDefaultFormat() | - |
90 | { | - |
91 | if (defaultFormat) | - |
92 | format = QLatin1String("%p") + locale.percent(); | - |
93 | } | - |
94 | | - |
95 | void QProgressBarPrivate::init() | - |
96 | { | - |
97 | Q_Q(QProgressBar); | - |
98 | QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed); | - |
99 | if (orientation == Qt::Vertical) | - |
100 | sp.transpose(); | - |
101 | q->setSizePolicy(sp); | - |
102 | q->setAttribute(Qt::WA_WState_OwnSizePolicy, false); | - |
103 | resetLayoutItemMargins(); | - |
104 | } | - |
105 | | - |
106 | void QProgressBarPrivate::resetLayoutItemMargins() | - |
107 | { | - |
108 | Q_Q(QProgressBar); | - |
109 | QStyleOptionProgressBar option; | - |
110 | q->initStyleOption(&option); | - |
111 | setLayoutItemMargins(QStyle::SE_ProgressBarLayoutItem, &option); | - |
112 | } | - |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
117 | | - |
118 | | - |
119 | | - |
120 | | - |
121 | void QProgressBar::initStyleOption(QStyleOptionProgressBar *option) const | - |
122 | { | - |
123 | if (!option) | - |
124 | return; | - |
125 | Q_D(const QProgressBar); | - |
126 | option->initFrom(this); | - |
127 | | - |
128 | if (d->orientation == Qt::Horizontal) | - |
129 | option->state |= QStyle::State_Horizontal; | - |
130 | option->minimum = d->minimum; | - |
131 | option->maximum = d->maximum; | - |
132 | option->progress = d->value; | - |
133 | option->textAlignment = d->alignment; | - |
134 | option->textVisible = d->textVisible; | - |
135 | option->text = text(); | - |
136 | option->orientation = d->orientation; | - |
137 | option->invertedAppearance = d->invertedAppearance; | - |
138 | option->bottomToTop = d->textDirection == QProgressBar::BottomToTop; | - |
139 | } | - |
140 | | - |
141 | bool QProgressBarPrivate::repaintRequired() const | - |
142 | { | - |
143 | Q_Q(const QProgressBar); | - |
144 | if (value == lastPaintedValue)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
145 | return false; never executed: return false; | 0 |
146 | | - |
147 | const qint64int valueDifference = qAbs(qint64(value )- lastPaintedValue); | - |
148 | | - |
149 | | - |
150 | if (value == minimum || value == maximum)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
151 | return true; never executed: return true; | 0 |
| | |
| const qint64 totalSteps = qint64(maximum) - minimum never executed: return true; ;never executed: return true; | |
152 | if (textVisible) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
153 | if ((format.contains(QLatin1String("%v"))))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
154 | return true; never executed: return true; | 0 |
155 | if ((format.contains(QLatin1String("%p"))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
156 | && valueDifference >= qAbs(totalSteps((maximum - minimum) / 100)))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
157 | return true; never executed: return true; | 0 |
158 | } never executed: end of block | 0 |
159 | | - |
160 | | - |
161 | QStyleOptionProgressBar opt; | - |
162 | q->initStyleOption(&opt); | - |
163 | int cw = q->style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, q); | - |
164 | QRect groove = q->style()->subElementRect(QStyle::SE_ProgressBarGroove, &opt, q); | - |
165 | | - |
166 | | - |
167 | | - |
168 | int grooveBlock = (q->orientation() == Qt::Horizontal) ? groove.width() : groove.height();TRUE | never evaluated | FALSE | never evaluated |
| 0 |
169 | return (valueDifference * grooveBlock > cw * totalSteps;(maximum - minimum)); never executed: return (valueDifference * grooveBlock > cw * (maximum - minimum)); | 0 |
170 | } | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | | - |
181 | | - |
182 | | - |
183 | | - |
184 | | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | | - |
190 | | - |
191 | | - |
192 | | - |
193 | | - |
194 | | - |
195 | | - |
196 | | - |
197 | | - |
198 | | - |
199 | | - |
200 | | - |
201 | | - |
202 | | - |
203 | | - |
204 | | - |
205 | | - |
206 | | - |
207 | | - |
208 | | - |
209 | | - |
210 | | - |
211 | | - |
212 | | - |
213 | | - |
214 | | - |
215 | | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | | - |
225 | | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
230 | | - |
231 | | - |
232 | | - |
233 | | - |
234 | | - |
235 | | - |
236 | | - |
237 | | - |
238 | | - |
239 | | - |
240 | | - |
241 | | - |
242 | QProgressBar::QProgressBar(QWidget *parent) | - |
243 | : QWidget(*(new QProgressBarPrivate), parent, 0) | - |
244 | { | - |
245 | d_func()->init(); | - |
246 | } | - |
247 | | - |
248 | | - |
249 | | - |
250 | | - |
251 | QProgressBar::~QProgressBar() | - |
252 | { | - |
253 | } | - |
254 | | - |
255 | | - |
256 | | - |
257 | | - |
258 | | - |
259 | | - |
260 | void QProgressBar::reset() | - |
261 | { | - |
262 | Q_D(QProgressBar); | - |
263 | d->value = d->minimum - 1; | - |
264 | if (d->minimum == INT_MIN)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
265 | d->value = INT_MIN; never executed: d->value = (-2147483647 - 1); | 0 |
| else never executed: d->value = (-2147483647 - 1); | |
| d->value = d->minimum - 1 never executed: d->value = (-2147483647 - 1); ;never executed: d->value = (-2147483647 - 1); | |
266 | repaint(); | - |
267 | } never executed: end of block | 0 |
268 | | - |
269 | | - |
270 | | - |
271 | | - |
272 | | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | void QProgressBar::setMinimum(int minimum) | - |
279 | { | - |
280 | setRange(minimum, qMax(d_func()->maximum, minimum)); | - |
281 | } | - |
282 | | - |
283 | int QProgressBar::minimum() const | - |
284 | { | - |
285 | return d_func()->minimum; | - |
286 | } | - |
287 | | - |
288 | | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | | - |
298 | | - |
299 | void QProgressBar::setMaximum(int maximum) | - |
300 | { | - |
301 | setRange(qMin(d_func()->minimum, maximum), maximum); | - |
302 | } | - |
303 | | - |
304 | int QProgressBar::maximum() const | - |
305 | { | - |
306 | return d_func()->maximum; | - |
307 | } | - |
308 | | - |
309 | | - |
310 | | - |
311 | | - |
312 | | - |
313 | | - |
314 | | - |
315 | | - |
316 | void QProgressBar::setValue(int value) | - |
317 | { | - |
318 | Q_D(QProgressBar); | - |
319 | if (d->value == value | - |
320 | || ((value > d->maximum || value < d->minimum) | - |
321 | && (d->maximum != 0 || d->minimum != 0))) | - |
322 | return; | - |
323 | d->value = value; | - |
324 | emit valueChanged(value); | - |
325 | #ifndef QT_NO_ACCESSIBILITY | - |
326 | if (isVisible()) { | - |
327 | QAccessibleValueChangeEvent event(this, value); | - |
328 | QAccessible::updateAccessibility(&event); | - |
329 | } | - |
330 | #endif | - |
331 | if (d->repaintRequired()) | - |
332 | repaint(); | - |
333 | } | - |
334 | | - |
335 | int QProgressBar::value() const | - |
336 | { | - |
337 | return d_func()->value; | - |
338 | } | - |
339 | | - |
340 | | - |
341 | | - |
342 | | - |
343 | | - |
344 | | - |
345 | | - |
346 | | - |
347 | | - |
348 | | - |
349 | | - |
350 | | - |
351 | | - |
352 | | - |
353 | | - |
354 | void QProgressBar::setRange(int minimum, int maximum) | - |
355 | { | - |
356 | Q_D(QProgressBar); | - |
357 | if (minimum != d->minimum || maximum != d->maximum) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
358 | d->minimum = minimum; | - |
359 | d->maximum = qMax(minimum, maximum); | - |
360 | | - |
361 | if (d->value < qint64(d->minimum )- 1) || d->value > d->maximum)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
362 | reset(); never executed: reset(); | 0 |
363 | else | - |
364 | update(); never executed: update(); | 0 |
365 | } | - |
366 | } never executed: end of block | 0 |
367 | | - |
368 | | - |
369 | | - |
370 | | - |
371 | | - |
372 | | - |
373 | | - |
374 | | - |
375 | | - |
376 | void QProgressBar::setTextVisible(bool visible) | - |
377 | { | - |
378 | Q_D(QProgressBar); | - |
379 | if (d->textVisible != visible) { | - |
380 | d->textVisible = visible; | - |
381 | repaint(); | - |
382 | } | - |
383 | } | - |
384 | | - |
385 | bool QProgressBar::isTextVisible() const | - |
386 | { | - |
387 | return d_func()->textVisible; | - |
388 | } | - |
389 | | - |
390 | | - |
391 | | - |
392 | | - |
393 | | - |
394 | void QProgressBar::setAlignment(Qt::Alignment alignment) | - |
395 | { | - |
396 | if (d_func()->alignment != alignment) { | - |
397 | d_func()->alignment = alignment; | - |
398 | repaint(); | - |
399 | } | - |
400 | } | - |
401 | | - |
402 | Qt::Alignment QProgressBar::alignment() const | - |
403 | { | - |
404 | return d_func()->alignment; | - |
405 | } | - |
406 | | - |
407 | | - |
408 | | - |
409 | | - |
410 | void QProgressBar::paintEvent(QPaintEvent *) | - |
411 | { | - |
412 | QStylePainter paint(this); | - |
413 | QStyleOptionProgressBar opt; | - |
414 | initStyleOption(&opt); | - |
415 | paint.drawControl(QStyle::CE_ProgressBar, opt); | - |
416 | d_func()->lastPaintedValue = d_func()->value; | - |
417 | } | - |
418 | | - |
419 | | - |
420 | | - |
421 | | - |
422 | QSize QProgressBar::sizeHint() const | - |
423 | { | - |
424 | ensurePolished(); | - |
425 | QFontMetrics fm = fontMetrics(); | - |
426 | QStyleOptionProgressBar opt; | - |
427 | initStyleOption(&opt); | - |
428 | int cw = style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, this); | - |
429 | QSize size = QSize(qMax(9, cw) * 7 + fm.width(QLatin1Char('0')) * 4, fm.height() + 8); | - |
430 | if (opt.orientation == Qt::Vertical) | - |
431 | size = size.transposed(); | - |
432 | return style()->sizeFromContents(QStyle::CT_ProgressBar, &opt, size, this); | - |
433 | } | - |
434 | | - |
435 | | - |
436 | | - |
437 | | - |
438 | QSize QProgressBar::minimumSizeHint() const | - |
439 | { | - |
440 | QSize size; | - |
441 | if (orientation() == Qt::Horizontal) | - |
442 | size = QSize(sizeHint().width(), fontMetrics().height() + 2); | - |
443 | else | - |
444 | size = QSize(fontMetrics().height() + 2, sizeHint().height()); | - |
445 | return size; | - |
446 | } | - |
447 | | - |
448 | | - |
449 | | - |
450 | | - |
451 | | - |
452 | | - |
453 | | - |
454 | | - |
455 | | - |
456 | | - |
457 | | - |
458 | | - |
459 | | - |
460 | | - |
461 | | - |
462 | | - |
463 | QString QProgressBar::text() const | - |
464 | { | - |
465 | Q_D(const QProgressBar); | - |
466 | if ((d->maximum == 0 && d->minimum == 0) || d->value < d->minimum | - |
467 | || (d->value == INT_MIN && d->minimum == INT_MIN)) | - |
468 | return QString(); | - |
469 | | - |
470 | qint64 totalSteps = qint64(d->maximum) - d->minimum; | - |
471 | | - |
472 | QString result = d->format; | - |
473 | QLocale locale = d->locale; | - |
474 | locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator); | - |
475 | result.replace(QLatin1String("%m"), locale.toString(totalSteps)); | - |
476 | result.replace(QLatin1String("%v"), locale.toString(d->value)); | - |
477 | | - |
478 | | - |
479 | | - |
480 | | - |
481 | if (totalSteps == 0) { | - |
482 | result.replace(QLatin1String("%p"), locale.toString(int(100))); | - |
483 | return result; | - |
484 | } | - |
485 | | - |
486 | int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps; | - |
487 | result.replace(QLatin1String("%p"), locale.toString(progress)); | - |
488 | return result; | - |
489 | } | - |
490 | | - |
491 | | - |
492 | | - |
493 | | - |
494 | | - |
495 | | - |
496 | | - |
497 | | - |
498 | | - |
499 | | - |
500 | | - |
501 | | - |
502 | void QProgressBar::setOrientation(Qt::Orientation orientation) | - |
503 | { | - |
504 | Q_D(QProgressBar); | - |
505 | if (d->orientation == orientation) | - |
506 | return; | - |
507 | d->orientation = orientation; | - |
508 | if (!testAttribute(Qt::WA_WState_OwnSizePolicy)) { | - |
509 | QSizePolicy sp = sizePolicy(); | - |
510 | sp.transpose(); | - |
511 | setSizePolicy(sp); | - |
512 | setAttribute(Qt::WA_WState_OwnSizePolicy, false); | - |
513 | } | - |
514 | d->resetLayoutItemMargins(); | - |
515 | update(); | - |
516 | updateGeometry(); | - |
517 | } | - |
518 | | - |
519 | Qt::Orientation QProgressBar::orientation() const | - |
520 | { | - |
521 | Q_D(const QProgressBar); | - |
522 | return d->orientation; | - |
523 | } | - |
524 | | - |
525 | | - |
526 | | - |
527 | | - |
528 | | - |
529 | | - |
530 | | - |
531 | | - |
532 | | - |
533 | | - |
534 | | - |
535 | | - |
536 | | - |
537 | void QProgressBar::setInvertedAppearance(bool invert) | - |
538 | { | - |
539 | Q_D(QProgressBar); | - |
540 | d->invertedAppearance = invert; | - |
541 | update(); | - |
542 | } | - |
543 | | - |
544 | bool QProgressBar::invertedAppearance() const | - |
545 | { | - |
546 | Q_D(const QProgressBar); | - |
547 | return d->invertedAppearance; | - |
548 | } | - |
549 | | - |
550 | | - |
551 | | - |
552 | | - |
553 | | - |
554 | | - |
555 | | - |
556 | | - |
557 | | - |
558 | | - |
559 | | - |
560 | void QProgressBar::setTextDirection(QProgressBar::Direction textDirection) | - |
561 | { | - |
562 | Q_D(QProgressBar); | - |
563 | d->textDirection = textDirection; | - |
564 | update(); | - |
565 | } | - |
566 | | - |
567 | QProgressBar::Direction QProgressBar::textDirection() const | - |
568 | { | - |
569 | Q_D(const QProgressBar); | - |
570 | return d->textDirection; | - |
571 | } | - |
572 | | - |
573 | | - |
574 | bool QProgressBar::event(QEvent *e) | - |
575 | { | - |
576 | Q_D(QProgressBar); | - |
577 | switch (e->type()) { | - |
578 | case QEvent::StyleChange: | - |
579 | #ifdef Q_OS_MAC | - |
580 | case QEvent::MacSizeChange: | - |
581 | #endif | - |
582 | d->resetLayoutItemMargins(); | - |
583 | break; | - |
584 | case QEvent::LocaleChange: | - |
585 | d->initDefaultFormat(); | - |
586 | break; | - |
587 | default: | - |
588 | break; | - |
589 | } | - |
590 | return QWidget::event(e); | - |
591 | } | - |
592 | | - |
593 | | - |
594 | | - |
595 | | - |
596 | | - |
597 | | - |
598 | | - |
599 | | - |
600 | | - |
601 | | - |
602 | | - |
603 | | - |
604 | | - |
605 | | - |
606 | void QProgressBar::setFormat(const QString &format) | - |
607 | { | - |
608 | Q_D(QProgressBar); | - |
609 | if (d->format == format) | - |
610 | return; | - |
611 | d->format = format; | - |
612 | d->defaultFormat = false; | - |
613 | update(); | - |
614 | } | - |
615 | | - |
616 | void QProgressBar::resetFormat() | - |
617 | { | - |
618 | Q_D(QProgressBar); | - |
619 | d->defaultFormat = true; | - |
620 | d->initDefaultFormat(); | - |
621 | update(); | - |
622 | } | - |
623 | | - |
624 | QString QProgressBar::format() const | - |
625 | { | - |
626 | Q_D(const QProgressBar); | - |
627 | return d->format; | - |
628 | } | - |
629 | | - |
630 | QT_END_NAMESPACE | - |
631 | | - |
632 | #include "moc_qprogressbar.cpp" | - |
633 | | - |
634 | #endif // QT_NO_PROGRESSBAR | - |
| | |