~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/widgets/qradiobutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the widgets module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qradiobutton.h"
 
30
#ifndef QT_NO_RADIOBUTTON
 
31
#include "qapplication.h"
 
32
#include "qbitmap.h"
 
33
#include "qbuttongroup.h"
 
34
#include "qstylepainter.h"
 
35
#include "qstyle.h"
 
36
#include "qstyleoption.h"
 
37
 
 
38
/*!
 
39
    \class QRadioButton
 
40
    \brief The QRadioButton widget provides a radio button with a text label.
 
41
 
 
42
    \ingroup basic
 
43
    \mainclass
 
44
 
 
45
    A QRadioButton is an option button that can be switched on (checked) or
 
46
    off (unchecked). Radio buttons typically present the user with a "one
 
47
    of many" choice. In a group of radio buttons only one radio button at
 
48
    a time can be checked; if the user selects another button, the
 
49
    previously selected button is switched off.
 
50
 
 
51
    Radio buttons are autoExclusive by default. If auto-exclusive is
 
52
    enabled, radio buttons that belong to the same parent widget
 
53
    behave as if they were part of the same exclusive button group. If
 
54
    you need multiple exclusive button groups for radio buttons that
 
55
    belong to the same parent widget, put them into a QButtonGroup.
 
56
 
 
57
    Whenever a button is switched on or off it emits the toggled() signal.
 
58
    Connect to this signal if you want to trigger an action each time the
 
59
    button changes state. Otherwise, use isChecked() to see if a particular
 
60
    button is selected.
 
61
 
 
62
    Just like QPushButton, a radio button displays text, and
 
63
    optionally a small icon. The text can be set in the constructor or
 
64
    with setText(); the icon is set with setIcon().
 
65
 
 
66
    Important inherited members: text(), setText(), text(),
 
67
    setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(),
 
68
    toggle(), pressed(), released(), clicked(), and toggled().
 
69
 
 
70
    \inlineimage macintosh-radiobutton.png Screenshot in Macintosh style
 
71
    \inlineimage windows-radiobutton.png Screenshot in Windows style
 
72
 
 
73
    \sa QPushButton, QToolButton, QCheckBox, {fowler}{GUI Design Handbook: Radio Button}
 
74
*/
 
75
 
 
76
 
 
77
/*
 
78
    Initializes the radio button.
 
79
*/
 
80
static void qRadioButtonInit(QRadioButton *button)
 
81
{
 
82
    button->setCheckable(true);
 
83
    button->setAutoExclusive(true);
 
84
}
 
85
 
 
86
 
 
87
/*!
 
88
    Constructs a radio button with the given \a parent, but with no text or
 
89
    pixmap.
 
90
 
 
91
    The \a parent argument is passed on to the QAbstractButton constructor.
 
92
*/
 
93
 
 
94
QRadioButton::QRadioButton(QWidget *parent)
 
95
        : QAbstractButton(parent)
 
96
{
 
97
    qRadioButtonInit(this);
 
98
}
 
99
 
 
100
/*!
 
101
    Constructs a radio button with the given \a parent and a \a text string.
 
102
 
 
103
    The \a parent argument is passed on to the QAbstractButton constructor.
 
104
*/
 
105
 
 
106
QRadioButton::QRadioButton(const QString &text, QWidget *parent)
 
107
        : QAbstractButton(parent)
 
108
{
 
109
    qRadioButtonInit(this);
 
110
    setText(text);
 
111
}
 
112
 
 
113
static QStyleOptionButton getStyleOption(const QRadioButton *btn)
 
114
{
 
115
    QStyleOptionButton opt;
 
116
    opt.init(btn);
 
117
    opt.text = btn->text();
 
118
    opt.icon = btn->icon();
 
119
    opt.iconSize = btn->iconSize();
 
120
    if (btn->isDown())
 
121
        opt.state |= QStyle::State_Sunken;
 
122
    opt.state |= (btn->isChecked() ? QStyle::State_On : QStyle::State_Off);
 
123
    return opt;
 
124
}
 
125
 
 
126
/*!
 
127
    \reimp
 
128
*/
 
129
QSize QRadioButton::sizeHint() const
 
130
{
 
131
    ensurePolished();
 
132
    QStyleOptionButton opt = getStyleOption(this);
 
133
    QSize sz = style()->itemTextRect(fontMetrics(), QRect(0, 0, 1, 1), Qt::TextShowMnemonic,
 
134
                                     false, text()).size();
 
135
    if (!opt.icon.isNull())
 
136
        sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
 
137
    return (style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this).
 
138
            expandedTo(QApplication::globalStrut()));
 
139
}
 
140
 
 
141
/*!
 
142
    \reimp
 
143
*/
 
144
bool QRadioButton::hitButton(const QPoint &pos) const
 
145
{
 
146
    QStyleOptionButton opt = getStyleOption(this);
 
147
    return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos);
 
148
}
 
149
 
 
150
/*!\reimp
 
151
 */
 
152
void QRadioButton::paintEvent(QPaintEvent *)
 
153
{
 
154
    QStylePainter p(this);
 
155
    QStyleOptionButton opt = getStyleOption(this);
 
156
    p.drawControl(QStyle::CE_RadioButton, opt);
 
157
}
 
158
 
 
159
#ifdef QT3_SUPPORT
 
160
/*!
 
161
    Use one of the constructors that doesn't take the \a name
 
162
    argument and then use setObjectName() instead.
 
163
*/
 
164
QRadioButton::QRadioButton(QWidget *parent, const char* name)
 
165
    :QAbstractButton(parent)
 
166
{
 
167
    setObjectName(name);
 
168
    qRadioButtonInit(this);
 
169
}
 
170
 
 
171
/*!
 
172
    Use one of the constructors that doesn't take the \a name
 
173
    argument and then use setObjectName() instead.
 
174
*/
 
175
QRadioButton::QRadioButton(const QString &text, QWidget *parent, const char* name)
 
176
    :QAbstractButton(parent)
 
177
{
 
178
    setObjectName(name);
 
179
    qRadioButtonInit(this);
 
180
    setText(text);
 
181
}
 
182
 
 
183
#endif
 
184
#endif