Qt 4.8
qcheckbox.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 "qcheckbox.h"
43 #include "qapplication.h"
44 #include "qbitmap.h"
45 #include "qicon.h"
46 #include "qstylepainter.h"
47 #include "qstyle.h"
48 #include "qstyleoption.h"
49 #include "qevent.h"
50 
51 #include "private/qabstractbutton_p.h"
52 
54 
56 {
58 public:
60  : QAbstractButtonPrivate(QSizePolicy::CheckBox), tristate(false), noChange(false),
62 
67 
68  void init();
69 };
70 
171 {
172  Q_Q(QCheckBox);
173  q->setCheckable(true);
174  q->setMouseTracking(true);
175  q->setForegroundRole(QPalette::WindowText);
177 }
178 
187 {
188  if (!option)
189  return;
190  Q_D(const QCheckBox);
191  option->initFrom(this);
192  if (d->down)
193  option->state |= QStyle::State_Sunken;
194  if (d->tristate && d->noChange)
195  option->state |= QStyle::State_NoChange;
196  else
197  option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
198  if (testAttribute(Qt::WA_Hover) && underMouse()) {
199  if (d->hovering)
200  option->state |= QStyle::State_MouseOver;
201  else
202  option->state &= ~QStyle::State_MouseOver;
203  }
204  option->text = d->text;
205  option->icon = d->icon;
206  option->iconSize = iconSize();
207 }
208 
216  : QAbstractButton (*new QCheckBoxPrivate, parent)
217 {
218  Q_D(QCheckBox);
219  d->init();
220 }
221 
229  : QAbstractButton (*new QCheckBoxPrivate, parent)
230 {
231  Q_D(QCheckBox);
232  d->init();
233  setText(text);
234 }
235 
237 {
238  Q_D(QCheckBox);
239  d->tristate = y;
240 }
241 
243 {
244  Q_D(const QCheckBox);
245  return d->tristate;
246 }
247 
248 
256 {
257  Q_D(const QCheckBox);
258  if (d->tristate && d->noChange)
259  return Qt::PartiallyChecked;
260  return d->checked ? Qt::Checked : Qt::Unchecked;
261 }
262 
271 {
272  Q_D(QCheckBox);
273  if (state == Qt::PartiallyChecked) {
274  d->tristate = true;
275  d->noChange = true;
276  } else {
277  d->noChange = false;
278  }
279  d->blockRefresh = true;
280  setChecked(state != Qt::Unchecked);
281  d->blockRefresh = false;
282  d->refresh();
283  if ((uint)state != d->publishedState) {
284  d->publishedState = state;
285  emit stateChanged(state);
286  }
287 }
288 
289 
294 {
295  Q_D(const QCheckBox);
296  if (d->sizeHint.isValid())
297  return d->sizeHint;
298  ensurePolished();
299  QFontMetrics fm = fontMetrics();
300  QStyleOptionButton opt;
301  initStyleOption(&opt);
302  QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
303  text()).size();
304  if (!opt.icon.isNull())
305  sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
306  d->sizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
308  return d->sizeHint;
309 }
310 
311 
317 {
318  return sizeHint();
319 }
320 
325 {
326  QStylePainter p(this);
327  QStyleOptionButton opt;
328  initStyleOption(&opt);
330 }
331 
336 {
337  Q_D(QCheckBox);
339  bool hit = false;
340  if (underMouse())
341  hit = hitButton(e->pos());
342 
343  if (hit != d->hovering) {
344  update(rect());
345  d->hovering = hit;
346  }
347  }
348 
350 }
351 
352 
356 bool QCheckBox::hitButton(const QPoint &pos) const
357 {
358  QStyleOptionButton opt;
359  initStyleOption(&opt);
360  return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
361 }
362 
367 {
368  Q_D(QCheckBox);
369  d->noChange = false;
370  Qt::CheckState state = checkState();
371  if ((uint)state != d->publishedState) {
372  d->publishedState = state;
373  emit stateChanged(state);
374  }
375 }
376 
381 {
382  Q_D(QCheckBox);
383  if (d->tristate)
384  setCheckState((Qt::CheckState)((checkState() + 1) % 3));
385  else {
388  }
389 }
390 
395 {
396  Q_D(QCheckBox);
397  if (e->type() == QEvent::StyleChange
398 #ifdef Q_WS_MAC
399  || e->type() == QEvent::MacSizeChange
400 #endif
401  )
402  d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
403  return QAbstractButton::event(e);
404 }
405 
406 #ifdef QT3_SUPPORT
407 
412  : QAbstractButton (*new QCheckBoxPrivate, parent)
413 {
414  Q_D(QCheckBox);
416  d->init();
417 }
418 
423 QCheckBox::QCheckBox(const QString &text, QWidget *parent, const char* name)
424  : QAbstractButton (*new QCheckBoxPrivate, parent)
425 {
426  Q_D(QCheckBox);
428  d->init();
429  setText(text);
430 }
431 
432 #endif
433 
434 
QSize iconSize
the size of the icon for the button
Definition: qstyleoption.h:292
The QAbstractButton class is the abstract base class of button widgets, providing functionality commo...
QPoint pos() const
double d
Definition: qnumeric_p.h:62
CheckState
Definition: qnamespace.h:1607
int y() const
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString text() const
void setText(const QString &text)
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
void drawControl(QStyle::ControlElement ce, const QStyleOption &opt)
Use the widget'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
static QSize globalStrut()
bool event(QEvent *e)
Reimplemented Function
Definition: qcheckbox.cpp:394
bool isTristate() const
Definition: qcheckbox.cpp:242
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
bool underMouse() const
Returns true if the widget is under the mouse cursor; otherwise returns false.
Definition: qwidget.h:996
QString text
the text of the button
Definition: qstyleoption.h:290
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setLayoutItemMargins(int left, int top, int right, int bottom)
Definition: qwidget.cpp:12860
#define Q_D(Class)
Definition: qglobal.h:2482
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
QStyle * style() const
Definition: qwidget.cpp:2742
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define Q_Q(Class)
Definition: qglobal.h:2483
QAbstractButton(QWidget *parent=0)
Constructs an abstract button with a parent.
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
bool event(QEvent *e)
Reimplemented Function
void initStyleOption(QStyleOptionButton *option) const
Initializes option with the values from this QCheckBox.
Definition: qcheckbox.cpp:186
void mouseMoveEvent(QMouseEvent *e)
Reimplemented Function
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
Returns the area within the given rectangle in which to draw the provided text according to the speci...
Definition: qstyle.cpp:470
bool hitButton(const QPoint &pos) const
Reimplemented Function
Definition: qcheckbox.cpp:356
QCheckBox(QWidget *parent=0)
Constructs a checkbox with the given parent, but with no text.
Definition: qcheckbox.cpp:215
void setTristate(bool y=true)
Definition: qcheckbox.cpp:236
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
virtual void nextCheckState()
This virtual handler is called when a button is clicked.
void mouseMoveEvent(QMouseEvent *)
Reimplemented Function
Definition: qcheckbox.cpp:335
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
const char * name
QIcon icon
the icon of the button
Definition: qstyleoption.h:291
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
#define emit
Definition: qobjectdefs.h:76
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
void stateChanged(int)
This signal is emitted whenever the check box's state changes, i.
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition: qwidget.h:984
unsigned int uint
Definition: qglobal.h:996
QRect rect() const
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
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...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=0) const =0
Returns the sub-area for the given element as described in the provided style option.
Qt::CheckState checkState() const
Returns the check box's check state.
Definition: qcheckbox.cpp:255
void checkStateSet()
Reimplemented Function
Definition: qcheckbox.cpp:366
QSize minimumSizeHint() const
Reimplemented Function
Definition: qcheckbox.cpp:316
void nextCheckState()
Reimplemented Function
Definition: qcheckbox.cpp:380
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
QObject * parent
Definition: qobject.h:92
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget...
Definition: qstylepainter.h:55
QSize sizeHint() const
Reimplemented Function
Definition: qcheckbox.cpp:293
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
void setCheckState(Qt::CheckState state)
Sets the check box's check state to state.
Definition: qcheckbox.cpp:270
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:56
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
The QStyleOptionButton class is used to describe the parameters for drawing buttons.
Definition: qstyleoption.h:279
void paintEvent(QPaintEvent *)
Reimplemented Function
Definition: qcheckbox.cpp:324