Qt 4.8
qradiobutton.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 "qradiobutton.h"
43 #include "qapplication.h"
44 #include "qbitmap.h"
45 #include "qbuttongroup.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 
59 public:
61  void init();
63 };
64 
65 /*
66  Initializes the radio button.
67 */
69 {
71  q->setCheckable(true);
72  q->setAutoExclusive(true);
73  q->setMouseTracking(true);
74  q->setForegroundRole(QPalette::WindowText);
76 }
77 
142  : QAbstractButton(*new QRadioButtonPrivate, parent)
143 {
144  Q_D(QRadioButton);
145  d->init();
146 }
147 
155  : QAbstractButton(*new QRadioButtonPrivate, parent)
156 {
157  Q_D(QRadioButton);
158  d->init();
159  setText(text);
160 }
161 
170 {
171  if (!option)
172  return;
173  Q_D(const QRadioButton);
174  option->initFrom(this);
175  option->text = d->text;
176  option->icon = d->icon;
177  option->iconSize = iconSize();
178  if (d->down)
179  option->state |= QStyle::State_Sunken;
180  option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
182  if (d->hovering)
183  option->state |= QStyle::State_MouseOver;
184  else
185  option->state &= ~QStyle::State_MouseOver;
186  }
187 }
188 
193 {
194  Q_D(const QRadioButton);
195  if (d->sizeHint.isValid())
196  return d->sizeHint;
197  ensurePolished();
198  QStyleOptionButton opt;
199  initStyleOption(&opt);
201  false, text()).size();
202  if (!opt.icon.isNull())
203  sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
204  d->sizeHint = (style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this).
205  expandedTo(QApplication::globalStrut()));
206  return d->sizeHint;
207 }
208 
214 {
215  return sizeHint();
216 }
217 
222 {
223  QStyleOptionButton opt;
224  initStyleOption(&opt);
225  return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos);
226 }
227 
232 {
233  Q_D(QRadioButton);
235  bool hit = false;
236  if (underMouse())
237  hit = hitButton(e->pos());
238 
239  if (hit != d->hovering) {
240  update();
241  d->hovering = hit;
242  }
243  }
244 
246 }
247 
251 {
252  QStylePainter p(this);
253  QStyleOptionButton opt;
254  initStyleOption(&opt);
256 }
257 
260 {
261  Q_D(QRadioButton);
262  if (e->type() == QEvent::StyleChange
263 #ifdef Q_WS_MAC
264  || e->type() == QEvent::MacSizeChange
265 #endif
266  )
267  d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
268  return QAbstractButton::event(e);
269 }
270 
271 #ifdef QT3_SUPPORT
272 
277  : QAbstractButton(*new QRadioButtonPrivate, parent)
278 {
279  Q_D(QRadioButton);
280  d->init();
282 }
283 
288 QRadioButton::QRadioButton(const QString &text, QWidget *parent, const char* name)
289  : QAbstractButton(*new QRadioButtonPrivate, parent)
290 {
291  Q_D(QRadioButton);
292  d->init();
294  setText(text);
295 }
296 
297 #endif
298 
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
#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()
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
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
void mouseMoveEvent(QMouseEvent *)
Reimplemented Function
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
bool hitButton(const QPoint &) const
Reimplemented Function
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 mouseMoveEvent(QMouseEvent *e)
Reimplemented Function
int width() const
Returns the width.
Definition: qsize.h:126
The QRadioButton widget provides a radio button with a text label.
Definition: qradiobutton.h:56
#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 testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
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
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition: qwidget.h:984
unsigned int uint
Definition: qglobal.h:996
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...
QSize sizeHint() const
Reimplemented Function
QSize iconSize() const
#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.
QRadioButton(QWidget *parent=0)
Constructs a radio button with the given parent, but with no text or pixmap.
QSize minimumSizeHint() const
Reimplemented Function
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
void paintEvent(QPaintEvent *)
Reimplemented Function
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
void initStyleOption(QStyleOptionButton *button) const
Initialize option with the values from this QRadioButton.
bool event(QEvent *e)
Reimplemented Function
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
QObject * parent
Definition: qobject.h:92
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
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
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