Qt 4.8
qprogressbar.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qprogressbar.h"
43 #ifndef QT_NO_PROGRESSBAR
44 #include <qevent.h>
45 #include <qpainter.h>
46 #include <qstylepainter.h>
47 #include <qstyleoption.h>
48 #include <private/qwidget_p.h>
49 #ifndef QT_NO_ACCESSIBILITY
50 #include <qaccessible.h>
51 #endif
52 #include <limits.h>
53 
55 
57 {
59 
60 public:
62 
63  void init();
64  inline void resetLayoutItemMargins();
65 
66  int minimum;
67  int maximum;
68  int value;
69  Qt::Alignment alignment;
76  inline int bound(int val) const { return qMax(minimum-1, qMin(maximum, val)); }
77  bool repaintRequired() const;
78 };
79 
81  : minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true),
83  textDirection(QProgressBar::TopToBottom), format(QLatin1String("%p%"))
84 {
85 }
86 
88 {
92  sp.transpose();
93  q->setSizePolicy(sp);
94  q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
96 }
97 
99 {
100  Q_Q(QProgressBar);
102  q->initStyleOption(&option);
104 }
105 
116 {
117  if (!option)
118  return;
119  Q_D(const QProgressBar);
120  option->initFrom(this);
121 
122  if (d->orientation == Qt::Horizontal)
123  option->state |= QStyle::State_Horizontal;
124  option->minimum = d->minimum;
125  option->maximum = d->maximum;
126  option->progress = d->value;
127  option->textAlignment = d->alignment;
128  option->textVisible = d->textVisible;
129  option->text = text();
130 
131  if (QStyleOptionProgressBarV2 *optionV2
132  = qstyleoption_cast<QStyleOptionProgressBarV2 *>(option)) {
133  optionV2->orientation = d->orientation; // ### Qt 5: use State_Horizontal instead
134  optionV2->invertedAppearance = d->invertedAppearance;
135  optionV2->bottomToTop = (d->textDirection == QProgressBar::BottomToTop);
136  }
137 }
138 
140 {
141  Q_Q(const QProgressBar);
142  if (value == lastPaintedValue)
143  return false;
144 
145  int valueDifference = qAbs(value - lastPaintedValue);
146 
147  // Check if the text needs to be repainted
148  if (value == minimum || value == maximum)
149  return true;
150  if (textVisible) {
151  if ((format.contains(QLatin1String("%v"))))
152  return true;
153  if ((format.contains(QLatin1String("%p"))
154  && valueDifference >= qAbs((maximum - minimum) / 100)))
155  return true;
156  }
157 
158  // Check if the bar needs to be repainted
160  q->initStyleOption(&opt);
161  int cw = q->style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, q);
162  QRect groove = q->style()->subElementRect(QStyle::SE_ProgressBarGroove, &opt, q);
163  // This expression is basically
164  // (valueDifference / (maximum - minimum) > cw / groove.width())
165  // transformed to avoid integer division.
166  int grooveBlock = (q->orientation() == Qt::Horizontal) ? groove.width() : groove.height();
167  return (valueDifference * grooveBlock > cw * (maximum - minimum));
168 }
169 
251  : QWidget(*(new QProgressBarPrivate), parent, 0)
252 {
253  d_func()->init();
254 }
255 
262 {
263  Q_D(QProgressBar);
264  d->value = d->minimum - 1;
265  if (d->minimum == INT_MIN)
266  d->value = INT_MIN;
267  repaint();
268 }
269 
283 {
284  setRange(minimum, qMax(d_func()->maximum, minimum));
285 }
286 
287 int QProgressBar::minimum() const
288 {
289  return d_func()->minimum;
290 }
291 
292 
307 {
308  setRange(qMin(d_func()->minimum, maximum), maximum);
309 }
310 
311 int QProgressBar::maximum() const
312 {
313  return d_func()->maximum;
314 }
315 
327 {
328  Q_D(QProgressBar);
329  if (d->value == value
330  || ((value > d->maximum || value < d->minimum)
331  && (d->maximum != 0 || d->minimum != 0)))
332  return;
333  d->value = value;
334  emit valueChanged(value);
335 #ifndef QT_NO_ACCESSIBILITY
336  if (isVisible())
338 #endif
339  if (d->repaintRequired())
340  repaint();
341 }
342 
343 int QProgressBar::value() const
344 {
345  return d_func()->value;
346 }
347 
361 {
362  Q_D(QProgressBar);
363  if (minimum != d->minimum || maximum != d->maximum) {
364  d->minimum = minimum;
365  d->maximum = qMax(minimum, maximum);
366 
367  if (d->value < (d->minimum - 1) || d->value > d->maximum)
368  reset();
369  else
370  update();
371  }
372 }
373 
386 {
387  Q_D(QProgressBar);
388  if (d->textVisible != visible) {
389  d->textVisible = visible;
390  repaint();
391  }
392 }
393 
395 {
396  return d_func()->textVisible;
397 }
398 
407 {
408  if (d_func()->alignment != alignment) {
409  d_func()->alignment = alignment;
410  repaint();
411  }
412 }
413 
414 Qt::Alignment QProgressBar::alignment() const
415 {
416  return d_func()->alignment;
417 }
418 
423 {
424  QStylePainter paint(this);
426  initStyleOption(&opt);
428  d_func()->lastPaintedValue = d_func()->value;
429 }
430 
435 {
436  ensurePolished();
437  QFontMetrics fm = fontMetrics();
439  initStyleOption(&opt);
440  int cw = style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, this);
441  QSize size = QSize(qMax(9, cw) * 7 + fm.width(QLatin1Char('0')) * 4, fm.height() + 8);
442  if (opt.orientation == Qt::Vertical)
443  size.transpose();
444  return style()->sizeFromContents(QStyle::CT_ProgressBar, &opt, size, this);
445 }
446 
451 {
452  QSize size;
453  if (orientation() == Qt::Horizontal)
454  size = QSize(sizeHint().width(), fontMetrics().height() + 2);
455  else
456  size = QSize(fontMetrics().height() + 2, sizeHint().height());
457  return size;
458 }
459 
479 {
480  Q_D(const QProgressBar);
481  if ((d->maximum == 0 && d->minimum == 0) || d->value < d->minimum
482  || (d->value == INT_MIN && d->minimum == INT_MIN))
483  return QString();
484 
485  qint64 totalSteps = qint64(d->maximum) - d->minimum;
486 
487  QString result = d->format;
488  result.replace(QLatin1String("%m"), QString::number(totalSteps));
489  result.replace(QLatin1String("%v"), QString::number(d->value));
490 
491  // If max and min are equal and we get this far, it means that the
492  // progress bar has one step and that we are on that step. Return
493  // 100% here in order to avoid division by zero further down.
494  if (totalSteps == 0) {
495  result.replace(QLatin1String("%p"), QString::number(100));
496  return result;
497  }
498 
499  int progress = (qreal(d->value) - d->minimum) * qreal(100.0) / totalSteps;
500  result.replace(QLatin1String("%p"), QString::number(progress));
501  return result;
502 }
503 
518 {
519  Q_D(QProgressBar);
520  if (d->orientation == orientation)
521  return;
522  d->orientation = orientation;
524  QSizePolicy sp = sizePolicy();
525  sp.transpose();
526  setSizePolicy(sp);
528  }
529  d->resetLayoutItemMargins();
530  update();
531  updateGeometry();
532 }
533 
535 {
536  Q_D(const QProgressBar);
537  return d->orientation;
538 }
539 
556 {
557  Q_D(QProgressBar);
558  d->invertedAppearance = invert;
559  update();
560 }
561 
563 {
564  Q_D(QProgressBar);
565  return d->invertedAppearance;
566 }
567 
582 {
583  Q_D(QProgressBar);
584  d->textDirection = textDirection;
585  update();
586 }
587 
589 {
590  Q_D(QProgressBar);
591  return d->textDirection;
592 }
593 
596 {
597  Q_D(QProgressBar);
598  if (e->type() == QEvent::StyleChange
599 #ifdef Q_WS_MAC
600  || e->type() == QEvent::MacSizeChange
601 #endif
602  )
603  d->resetLayoutItemMargins();
604  return QWidget::event(e);
605 }
606 
624 {
625  Q_D(QProgressBar);
626  if (d->format == format)
627  return;
628  d->format = format;
629  update();
630 }
631 
633 {
634  Q_D(const QProgressBar);
635  return d->format;
636 }
637 
639 
640 #endif // QT_NO_PROGRESSBAR
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
void setTextVisible(bool visible)
double d
Definition: qnumeric_p.h:62
void setOrientation(Qt::Orientation)
QString text
the text for the progress bar
Definition: qstyleoption.h:405
Qt::Orientation orientation() const
Direction
Specifies the reading direction of the text for vertical progress bars.
Definition: qprogressbar.h:74
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSize size() const
int maximum
the maximum value for the progress bar
Definition: qstyleoption.h:403
void drawControl(QStyle::ControlElement ce, const QStyleOption &opt)
Use the widget&#39;s style to draw a control element ce specified by QStyleOption option.
Definition: qstylepainter.h:87
void ensurePolished() const
Ensures that the widget has been polished by QStyle (i.e., has a proper font and palette).
Definition: qwidget.cpp:10024
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
Qt::Alignment alignment() const
bool isVisible() const
Definition: qwidget.h:1005
int bound(int val) const
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
Qt::Orientation orientation
the progress bar&#39;s orientation (horizontal or vertical); the default orentation is Qt::Horizontal ...
Definition: qstyleoption.h:422
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QSize sizeHint() const
Reimplemented Function
int value() const
QProgressBar::Direction textDirection
The QProgressBar widget provides a horizontal or vertical progress bar.
Definition: qprogressbar.h:58
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
bool event(QEvent *e)
Reimplemented Function
void setLayoutItemMargins(int left, int top, int right, int bottom)
Definition: qwidget.cpp:12860
#define Q_D(Class)
Definition: qglobal.h:2482
int height() const
QProgressBar(QWidget *parent=0)
Constructs a progress bar with the given parent.
void setFormat(const QString &format)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QStyle * style() const
Definition: qwidget.cpp:2742
#define Q_Q(Class)
Definition: qglobal.h:2483
void initStyleOption(QStyleOptionProgressBar *option) const
Initialize option with the values from this QProgressBar.
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
QSizePolicy sizePolicy() const
void setMaximum(int maximum)
The QStyleOptionProgressBarV2 class is used to describe the parameters necessary for drawing a progre...
Definition: qstyleoption.h:417
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool visible
whether the widget is visible
Definition: qwidget.h:191
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
int width() const
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
void setInvertedAppearance(bool invert)
#define emit
Definition: qobjectdefs.h:76
int progress
the current progress for the progress bar
Definition: qstyleoption.h:404
QFontMetrics fontMetrics() const
Returns the font metrics for the widget&#39;s current font.
Definition: qwidget.h:984
void transpose()
Swaps the width and height values.
Definition: qsize.cpp:196
unsigned int uint
Definition: qglobal.h:996
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
Qt::Alignment alignment
Qt::Alignment textAlignment
the text alignment for the text in the QProgressBar
Definition: qstyleoption.h:406
void setTextDirection(QProgressBar::Direction textDirection)
int minimum
the minimum value for the progress bar
Definition: qstyleoption.h:402
__int64 qint64
Definition: qglobal.h:942
bool repaintRequired() const
void repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the ...
Definition: qwidget.cpp:10761
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=0) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
void setRange(int minimum, int maximum)
Sets the progress bar&#39;s minimum and maximum values to minimum and maximum respectively.
bool textVisible
a flag indicating whether or not text is visible
Definition: qstyleoption.h:407
void paintEvent(QPaintEvent *)
Reimplemented Function
int maximum() const
void setMinimum(int minimum)
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
bool invertedAppearance()
void setValue(int value)
QString format() const
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
QProgressBar::Direction textDirection()
QSize minimumSizeHint() const
Reimplemented Function
void transpose()
Definition: qsizepolicy.h:229
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QObject * parent
Definition: qobject.h:92
bool isTextVisible() const
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget...
Definition: qstylepainter.h:55
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void reset()
Reset the progress bar.
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
Definition: qwidget.cpp:10372
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
Qt::Orientation orientation
The QStyleOptionProgressBar class is used to describe the parameters necessary for drawing a progress...
Definition: qstyleoption.h:396
int height() const
Returns the height of the font.
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
void setSizePolicy(QSizePolicy)
Definition: qwidget.cpp:10198
Orientation
Definition: qnamespace.h:174
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void setAlignment(Qt::Alignment alignment)
int minimum() const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define text
Definition: qobjectdefs.h:80
void valueChanged(int value)
This signal is emitted when the value shown in the progress bar changes.
virtual QString text() const