Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qprogressbar.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qprogressbar.h"
5
6#include <qlocale.h>
7#include <qevent.h>
8#include <qpainter.h>
9#include <qstylepainter.h>
10#include <qstyleoption.h>
11#include <private/qwidget_p.h>
12#if QT_CONFIG(accessibility)
13#include <qaccessible.h>
14#endif
15#include <limits.h>
16
18
19using namespace Qt::StringLiterals;
20
22{
23 Q_DECLARE_PUBLIC(QProgressBar)
24
25public:
27
28 void init();
29 void initDefaultFormat();
30 inline void resetLayoutItemMargins();
31
34 int value;
35 Qt::Alignment alignment;
43 inline int bound(int val) const { return qMax(minimum-1, qMin(maximum, val)); }
44 bool repaintRequired() const;
45};
46
48 : minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true),
49 defaultFormat(true), lastPaintedValue(-1), orientation(Qt::Horizontal), invertedAppearance(false),
50 textDirection(QProgressBar::TopToBottom)
51{
53}
54
60
62{
63 Q_Q(QProgressBar);
66 sp.transpose();
67 q->setSizePolicy(sp);
68 q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
70}
71
73{
74 Q_Q(QProgressBar);
76 // ### It seems like this can be called directly from the constructor which should be avoided
77 // ### if possible, since we will not call a possible re-implemented version
78 q->initStyleOption(&option);
80}
81
90{
91 if (!option)
92 return;
93 Q_D(const QProgressBar);
94 option->initFrom(this);
95
96 if (d->orientation == Qt::Horizontal)
98 option->minimum = d->minimum;
99 option->maximum = d->maximum;
100 option->progress = d->value;
101 option->textAlignment = d->alignment;
102 option->textVisible = d->textVisible;
103 option->text = text();
104 option->invertedAppearance = d->invertedAppearance;
105 option->bottomToTop = d->textDirection == QProgressBar::BottomToTop;
106}
107
109{
110 Q_Q(const QProgressBar);
111 if (value == lastPaintedValue)
112 return false;
113
114 const auto valueDifference = qAbs(qint64(value) - lastPaintedValue);
115 // Check if the text needs to be repainted
116 if (value == minimum || value == maximum)
117 return true;
118
119 const auto totalSteps = qint64(maximum) - minimum;
120 if (textVisible) {
121 if (format.contains("%v"_L1))
122 return true;
123 if (format.contains("%p"_L1) && valueDifference >= qAbs(totalSteps / 100))
124 return true;
125 }
126
127 // Check if the bar needs to be repainted
129 q->initStyleOption(&opt);
130 int cw = q->style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, q);
131 QRect groove = q->style()->subElementRect(QStyle::SE_ProgressBarGroove, &opt, q);
132 // This expression is basically
133 // (valueDifference / (maximum - minimum) > cw / groove.width())
134 // transformed to avoid integer division.
135 int grooveBlock = (q->orientation() == Qt::Horizontal) ? groove.width() : groove.height();
136 return valueDifference * grooveBlock > cw * totalSteps;
137}
138
203 : QWidget(*(new QProgressBarPrivate), parent, { })
204{
205 d_func()->init();
206}
207
214
221{
222 Q_D(QProgressBar);
223 if (d->minimum == INT_MIN)
224 d->value = INT_MIN;
225 else
226 d->value = d->minimum - 1;
227 repaint();
228}
229
240{
241 setRange(minimum, qMax(d_func()->maximum, minimum));
242}
243
245{
246 return d_func()->minimum;
247}
248
249
261{
262 setRange(qMin(d_func()->minimum, maximum), maximum);
263}
264
266{
267 return d_func()->maximum;
268}
269
278{
279 Q_D(QProgressBar);
280 if (d->value == value
281 || ((value > d->maximum || value < d->minimum)
282 && (d->maximum != 0 || d->minimum != 0)))
283 return;
284 d->value = value;
286#if QT_CONFIG(accessibility)
287 if (isVisible()) {
288 QAccessibleValueChangeEvent event(this, value);
289 QAccessible::updateAccessibility(&event);
290 }
291#endif
292 if (d->repaintRequired())
293 repaint();
294}
295
297{
298 return d_func()->value;
299}
300
315void QProgressBar::setRange(int minimum, int maximum)
316{
317 Q_D(QProgressBar);
318 if (minimum != d->minimum || maximum != d->maximum) {
319 d->minimum = minimum;
320 d->maximum = qMax(minimum, maximum);
321
322 if (d->value < qint64(d->minimum) - 1 || d->value > d->maximum)
323 reset();
324 else
325 update();
326 }
327}
328
338{
339 Q_D(QProgressBar);
340 if (d->textVisible != visible) {
341 d->textVisible = visible;
342 repaint();
343 }
344}
345
347{
348 return d_func()->textVisible;
349}
350
356{
357 if (d_func()->alignment != alignment) {
358 d_func()->alignment = alignment;
359 repaint();
360 }
361}
362
363Qt::Alignment QProgressBar::alignment() const
364{
365 return d_func()->alignment;
366}
367
372{
373 QStylePainter paint(this);
376 paint.drawControl(QStyle::CE_ProgressBar, opt);
377 d_func()->lastPaintedValue = d_func()->value;
378}
379
384{
390 QSize size = QSize(qMax(9, cw) * 7 + fm.horizontalAdvance(u'0') * 4, fm.height() + 8);
392 size = size.transposed();
394}
395
400{
401 QSize size;
403 size = QSize(sizeHint().width(), fontMetrics().height() + 2);
404 else
405 size = QSize(fontMetrics().height() + 2, sizeHint().height());
406 return size;
407}
408
425{
426 Q_D(const QProgressBar);
427 if ((d->maximum == 0 && d->minimum == 0) || d->value < d->minimum
428 || (d->value == INT_MIN && d->minimum == INT_MIN))
429 return QString();
430
431 qint64 totalSteps = qint64(d->maximum) - d->minimum;
432
433 QString result = d->format;
434 QLocale locale = d->locale; // Omit group separators for compatibility with previous versions that were non-localized.
436 result.replace("%m"_L1, locale.toString(totalSteps));
437 result.replace("%v"_L1, locale.toString(d->value));
438
439 // If max and min are equal and we get this far, it means that the
440 // progress bar has one step and that we are on that step. Return
441 // 100% here in order to avoid division by zero further down.
442 if (totalSteps == 0) {
443 result.replace("%p"_L1, locale.toString(100));
444 return result;
445 }
446
447 const auto progress = static_cast<int>((qint64(d->value) - d->minimum) * 100.0 / totalSteps);
448 result.replace("%p"_L1, locale.toString(progress));
449 return result;
450}
451
464{
465 Q_D(QProgressBar);
466 if (d->orientation == orientation)
467 return;
468 d->orientation = orientation;
470 setSizePolicy(sizePolicy().transposed());
472 }
473 d->resetLayoutItemMargins();
474 update();
476}
477
479{
480 Q_D(const QProgressBar);
481 return d->orientation;
482}
483
497{
498 Q_D(QProgressBar);
499 d->invertedAppearance = invert;
500 update();
501}
502
504{
505 Q_D(const QProgressBar);
506 return d->invertedAppearance;
507}
508
520{
521 Q_D(QProgressBar);
522 d->textDirection = textDirection;
523 update();
524}
525
527{
528 Q_D(const QProgressBar);
529 return d->textDirection;
530}
531
534{
535 Q_D(QProgressBar);
536 switch (e->type()) {
538#ifdef Q_OS_MAC
540#endif
541 d->resetLayoutItemMargins();
542 break;
544 d->initDefaultFormat();
545 break;
546 default:
547 break;
548 }
549 return QWidget::event(e);
550}
551
566{
567 Q_D(QProgressBar);
568 if (d->format == format)
569 return;
570 d->format = format;
571 d->defaultFormat = false;
572 update();
573}
574
576{
577 Q_D(QProgressBar);
578 d->defaultFormat = true;
579 d->initDefaultFormat();
580 update();
581}
582
584{
585 Q_D(const QProgressBar);
586 return d->format;
587}
588
590
591#include "moc_qprogressbar.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ LocaleChange
Definition qcoreevent.h:122
@ MacSizeChange
Definition qcoreevent.h:217
Type type() const
Returns the event type.
Definition qcoreevent.h:304
\reentrant \inmodule QtGui
void setNumberOptions(NumberOptions options)
Definition qlocale.cpp:1177
QString percent() const
Definition qlocale.cpp:2691
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition qlocale.cpp:2052
NumberOptions numberOptions() const
Definition qlocale.cpp:1193
@ OmitGroupSeparator
Definition qlocale.h:879
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
bool repaintRequired() const
QProgressBar::Direction textDirection
Qt::Alignment alignment
int bound(int val) const
Qt::Orientation orientation
The QProgressBar widget provides a horizontal or vertical progress bar.
int minimum
the progress bar's minimum value
void setAlignment(Qt::Alignment alignment)
bool event(QEvent *e) override
\reimp
Direction
Specifies the reading direction of the \l text for vertical progress bars.
void reset()
Reset the progress bar.
QSize minimumSizeHint() const override
\reimp
QSize sizeHint() const override
\reimp
bool isTextVisible() const
int value
the progress bar's current value
void setTextDirection(QProgressBar::Direction textDirection)
void setInvertedAppearance(bool invert)
Qt::Orientation orientation
the orientation of the progress bar
void setTextVisible(bool visible)
void setMinimum(int minimum)
QString format
the string used to generate the current text
void setFormat(const QString &format)
void valueChanged(int value)
This signal is emitted when the value shown in the progress bar changes.
~QProgressBar()
Destructor.
Qt::Alignment alignment
the alignment of the progress bar
QString text
the descriptive text shown with the progress bar
int maximum
the progress bar's maximum value
bool invertedAppearance
whether or not a progress bar shows its progress inverted
virtual void initStyleOption(QStyleOptionProgressBar *option) const
Initialize option with the values from this QProgressBar.
void setValue(int value)
void setOrientation(Qt::Orientation)
void paintEvent(QPaintEvent *) override
\reimp
Direction textDirection
the reading direction of the \l text for vertical progress bars
QProgressBar(QWidget *parent=nullptr)
Constructs a progress bar with the given parent.
void setRange(int minimum, int maximum)
Sets the progress bar's minimum and maximum values to minimum and maximum respectively.
void setMaximum(int maximum)
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\variable QStyleOptionButton::features
QStyle::State state
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
@ State_Horizontal
Definition qstyle.h:74
@ CT_ProgressBar
Definition qstyle.h:553
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
@ CE_ProgressBar
Definition qstyle.h:185
@ PM_ProgressBarChunkWidth
Definition qstyle.h:445
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
Returns the value of the given pixel metric.
@ SE_ProgressBarGroove
Definition qstyle.h:260
@ SE_ProgressBarLayoutItem
Definition qstyle.h:293
void setLayoutItemMargins(int left, int top, int right, int bottom)
QLocale locale
Definition qwidget_p.h:706
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
void repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the ...
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
void setSizePolicy(QSizePolicy)
QSize size
the size of the widget excluding any window frame
Definition qwidget.h:113
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition qwidget.h:847
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition qwidget.h:119
QLocale locale
the widget's locale
Definition qwidget.h:176
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
void ensurePolished() const
Ensures that the widget and its children have been polished by QStyle (i.e., have a proper font and p...
void update()
Updates the widget unless updates are disabled or the widget is hidden.
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8866
QStyle * style() const
Definition qwidget.cpp:2600
bool isVisible() const
Definition qwidget.h:874
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition qwidget.h:910
bool visible
whether the widget is visible
Definition qwidget.h:144
QPainter paint
uint alignment
QStyleOptionButton opt
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ WA_WState_OwnSizePolicy
Definition qnamespace.h:334
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum format
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
GLuint GLenum option
GLboolean invert
Definition qopenglext.h:226
static const struct TessellationWindingOrderTab cw[]
#define sp
#define emit
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60