src/gui/widgets/qradiobutton.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 class QRadioButtonPrivate : public QAbstractButtonPrivate
       
    56 {
       
    57     Q_DECLARE_PUBLIC(QRadioButton)
       
    58 
       
    59 public:
       
    60     QRadioButtonPrivate() : QAbstractButtonPrivate(QSizePolicy::RadioButton), hovering(true) {}
       
    61     void init();
       
    62     uint hovering : 1;
       
    63 };
       
    64 
       
    65 /*
       
    66     Initializes the radio button.
       
    67 */
       
    68 void QRadioButtonPrivate::init()
       
    69 {
       
    70     Q_Q(QRadioButton);
       
    71     q->setCheckable(true);
       
    72     q->setAutoExclusive(true);
       
    73     q->setMouseTracking(true);
       
    74     q->setForegroundRole(QPalette::WindowText);
       
    75     setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
       
    76 }
       
    77 
       
    78 /*!
       
    79     \class QRadioButton
       
    80     \brief The QRadioButton widget provides a radio button with a text label.
       
    81 
       
    82     \ingroup basicwidgets
       
    83 
       
    84 
       
    85     A QRadioButton is an option button that can be switched on (checked) or
       
    86     off (unchecked). Radio buttons typically present the user with a "one
       
    87     of many" choice. In a group of radio buttons only one radio button at
       
    88     a time can be checked; if the user selects another button, the
       
    89     previously selected button is switched off.
       
    90 
       
    91     Radio buttons are autoExclusive by default. If auto-exclusive is
       
    92     enabled, radio buttons that belong to the same parent widget
       
    93     behave as if they were part of the same exclusive button group. If
       
    94     you need multiple exclusive button groups for radio buttons that
       
    95     belong to the same parent widget, put them into a QButtonGroup.
       
    96 
       
    97     Whenever a button is switched on or off it emits the toggled() signal.
       
    98     Connect to this signal if you want to trigger an action each time the
       
    99     button changes state. Use isChecked() to see if a particular button is
       
   100     selected.
       
   101 
       
   102     Just like QPushButton, a radio button displays text, and
       
   103     optionally a small icon. The icon is set with setIcon(). The text
       
   104     can be set in the constructor or with setText(). A shortcut key
       
   105     can be specified by preceding the preferred character with an
       
   106     ampersand in the text. For example:
       
   107 
       
   108     \snippet doc/src/snippets/code/src_gui_widgets_qradiobutton.cpp 0
       
   109 
       
   110     In this example the shortcut is \e{Alt+c}. See the \l
       
   111     {QShortcut#mnemonic}{QShortcut} documentation for details (to
       
   112     display an actual ampersand, use '&&').
       
   113 
       
   114     Important inherited members: text(), setText(), text(),
       
   115     setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(),
       
   116     toggle(), pressed(), released(), clicked(), and toggled().
       
   117 
       
   118     \table 100%
       
   119     \row \o \inlineimage plastique-radiobutton.png Screenshot of a Plastique radio button
       
   120          \o A radio button shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.
       
   121     \row \o \inlineimage windows-radiobutton.png Screenshot of a Windows XP radio button
       
   122          \o A radio button shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.
       
   123     \row \o \inlineimage macintosh-radiobutton.png Screenshot of a Macintosh radio button
       
   124          \o A radio button shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.
       
   125     \endtable
       
   126 
       
   127     \sa QPushButton, QToolButton, QCheckBox, {fowler}{GUI Design Handbook: Radio Button},
       
   128         {Group Box Example}
       
   129 */
       
   130 
       
   131 
       
   132 /*!
       
   133     Constructs a radio button with the given \a parent, but with no text or
       
   134     pixmap.
       
   135 
       
   136     The \a parent argument is passed on to the QAbstractButton constructor.
       
   137 */
       
   138 
       
   139 QRadioButton::QRadioButton(QWidget *parent)
       
   140     : QAbstractButton(*new QRadioButtonPrivate, parent)
       
   141 {
       
   142     Q_D(QRadioButton);
       
   143     d->init();
       
   144 }
       
   145 
       
   146 /*!
       
   147     Constructs a radio button with the given \a parent and a \a text string.
       
   148 
       
   149     The \a parent argument is passed on to the QAbstractButton constructor.
       
   150 */
       
   151 
       
   152 QRadioButton::QRadioButton(const QString &text, QWidget *parent)
       
   153     : QAbstractButton(*new QRadioButtonPrivate, parent)
       
   154 {
       
   155     Q_D(QRadioButton);
       
   156     d->init();
       
   157     setText(text);
       
   158 }
       
   159 
       
   160 /*!
       
   161     Initialize \a option with the values from this QRadioButton. This method is useful
       
   162     for subclasses when they need a QStyleOptionButton, but don't want to fill
       
   163     in all the information themselves.
       
   164 
       
   165     \sa QStyleOption::initFrom()
       
   166 */
       
   167 void QRadioButton::initStyleOption(QStyleOptionButton *option) const
       
   168 {
       
   169     if (!option)
       
   170         return;
       
   171     Q_D(const QRadioButton);
       
   172     option->initFrom(this);
       
   173     option->text = d->text;
       
   174     option->icon = d->icon;
       
   175     option->iconSize = iconSize();
       
   176     if (d->down)
       
   177         option->state |= QStyle::State_Sunken;
       
   178     option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
       
   179     if (testAttribute(Qt::WA_Hover) && underMouse()) {
       
   180         if (d->hovering)
       
   181             option->state |= QStyle::State_MouseOver;
       
   182         else
       
   183             option->state &= ~QStyle::State_MouseOver;
       
   184     }
       
   185 }
       
   186 
       
   187 /*!
       
   188     \reimp
       
   189 */
       
   190 QSize QRadioButton::sizeHint() const
       
   191 {
       
   192     Q_D(const QRadioButton);
       
   193     if (d->sizeHint.isValid())
       
   194         return d->sizeHint;
       
   195     ensurePolished();
       
   196     QStyleOptionButton opt;
       
   197     initStyleOption(&opt);
       
   198     QSize sz = style()->itemTextRect(fontMetrics(), QRect(0, 0, 1, 1), Qt::TextShowMnemonic,
       
   199                                      false, text()).size();
       
   200     if (!opt.icon.isNull())
       
   201         sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
       
   202     d->sizeHint = (style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this).
       
   203                   expandedTo(QApplication::globalStrut()));
       
   204     return d->sizeHint;
       
   205 }
       
   206 
       
   207 /*!
       
   208     \reimp
       
   209 */
       
   210 bool QRadioButton::hitButton(const QPoint &pos) const
       
   211 {
       
   212     QStyleOptionButton opt;
       
   213     initStyleOption(&opt);
       
   214     return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos);
       
   215 }
       
   216 
       
   217 /*!
       
   218     \reimp
       
   219 */
       
   220 void QRadioButton::mouseMoveEvent(QMouseEvent *e)
       
   221 {
       
   222     Q_D(QRadioButton);
       
   223     if (testAttribute(Qt::WA_Hover)) {
       
   224         bool hit = false;
       
   225         if (underMouse())
       
   226             hit = hitButton(e->pos());
       
   227 
       
   228         if (hit != d->hovering) {
       
   229             update();
       
   230             d->hovering = hit;
       
   231         }
       
   232     }
       
   233 
       
   234     QAbstractButton::mouseMoveEvent(e);
       
   235 }
       
   236 
       
   237 /*!\reimp
       
   238  */
       
   239 void QRadioButton::paintEvent(QPaintEvent *)
       
   240 {
       
   241     QStylePainter p(this);
       
   242     QStyleOptionButton opt;
       
   243     initStyleOption(&opt);
       
   244     p.drawControl(QStyle::CE_RadioButton, opt);
       
   245 }
       
   246 
       
   247 /*! \reimp */
       
   248 bool QRadioButton::event(QEvent *e)
       
   249 {
       
   250     Q_D(QRadioButton);
       
   251     if (e->type() == QEvent::StyleChange
       
   252 #ifdef Q_WS_MAC
       
   253             || e->type() == QEvent::MacSizeChange
       
   254 #endif
       
   255             )
       
   256         d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
       
   257     return QAbstractButton::event(e);
       
   258 }
       
   259 
       
   260 #ifdef QT3_SUPPORT
       
   261 /*!
       
   262     Use one of the constructors that doesn't take the \a name
       
   263     argument and then use setObjectName() instead.
       
   264 */
       
   265 QRadioButton::QRadioButton(QWidget *parent, const char* name)
       
   266     : QAbstractButton(*new QRadioButtonPrivate, parent)
       
   267 {
       
   268     Q_D(QRadioButton);
       
   269     d->init();
       
   270     setObjectName(QString::fromAscii(name));
       
   271 }
       
   272 
       
   273 /*!
       
   274     Use one of the constructors that doesn't take the \a name
       
   275     argument and then use setObjectName() instead.
       
   276 */
       
   277 QRadioButton::QRadioButton(const QString &text, QWidget *parent, const char* name)
       
   278     : QAbstractButton(*new QRadioButtonPrivate, parent)
       
   279 {
       
   280     Q_D(QRadioButton);
       
   281     d->init();
       
   282     setObjectName(QString::fromAscii(name));
       
   283     setText(text);
       
   284 }
       
   285 
       
   286 #endif
       
   287 
       
   288 QT_END_NAMESPACE