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

« back to all changes in this revision

Viewing changes to src/gui/widgets/qcheckbox.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 "qcheckbox.h"
 
30
#ifndef QT_NO_CHECKBOX
 
31
#include "qapplication.h"
 
32
#include "qbitmap.h"
 
33
#include "qicon.h"
 
34
#include "qstylepainter.h"
 
35
#include "qstyle.h"
 
36
#include "qstyleoption.h"
 
37
 
 
38
#include "private/qabstractbutton_p.h"
 
39
 
 
40
class QCheckBoxPrivate : public QAbstractButtonPrivate
 
41
{
 
42
    Q_DECLARE_PUBLIC(QCheckBox)
 
43
public:
 
44
    QCheckBoxPrivate():tristate(false), noChange(false){}
 
45
    uint tristate : 1;
 
46
    uint noChange : 1;
 
47
    void init();
 
48
    QStyleOptionButton getStyleOption() const;
 
49
};
 
50
 
 
51
/*!
 
52
    \class QCheckBox
 
53
    \brief The QCheckBox widget provides a checkbox with a text label.
 
54
 
 
55
    \ingroup basic
 
56
    \mainclass
 
57
 
 
58
    A QCheckBox is an option button that can be switched on (checked)
 
59
    or off (unchecked). Checkboxes are typically used to represent
 
60
    features in an application that can be enabled or disabled without
 
61
    affecting others, but different types of behavior can be
 
62
    implemented.
 
63
 
 
64
    A QButtonGroup can be used to group check buttons visually.
 
65
 
 
66
    Whenever a checkbox is checked or cleared it emits the signal
 
67
    stateChanged(). Connect to this signal if you want to trigger an
 
68
    action each time the checkbox changes state. You can use
 
69
    isChecked() to query whether or not a checkbox is checked.
 
70
 
 
71
    In addition to the usual checked and unchecked states, QCheckBox
 
72
    optionally provides a third state to indicate "no change". This
 
73
    is useful whenever you need to give the user the option of neither
 
74
    checking nor unchecking a checkbox. If you need this third state,
 
75
    enable it with setTristate(), and use checkState() to query the current
 
76
    toggle state.
 
77
 
 
78
    Just like QPushButton, a checkbox button displays text, and
 
79
    optionally a small icon. The text can be set in the constructor or
 
80
    with setText(); the icon is set with setIcon().
 
81
 
 
82
    Important inherited functions: text(), setText(), text(),
 
83
    pixmap(), setPixmap(), accel(), setAccel(), isToggleButton(),
 
84
    setDown(), isDown(), isOn(), checkState(), autoRepeat(),
 
85
    isExclusiveToggle(), group(), setAutoRepeat(), toggle(),
 
86
    pressed(), released(), clicked(), toggled(), checkState(), and
 
87
    stateChanged().
 
88
 
 
89
    \inlineimage macintosh-checkbox.png Screenshot in Macintosh style
 
90
    \inlineimage windows-checkbox.png Screenshot in Windows style
 
91
 
 
92
    \sa QAbstractButton, QRadioButton, {fowler}{GUI Design Handbook: Check Box}
 
93
*/
 
94
 
 
95
/*!
 
96
    \enum QCheckBox::ToggleState
 
97
 
 
98
    \value Off
 
99
    \value NoChange
 
100
    \value On
 
101
*/
 
102
 
 
103
/*!
 
104
    \fn void QCheckBox::stateChanged(int state)
 
105
 
 
106
    This signal is emitted whenever the check box's state changes,
 
107
    i.e. whenever the user checks or unchecks it.
 
108
 
 
109
    \a state contains the check box's new ToggleState.
 
110
*/
 
111
 
 
112
/*!
 
113
    \property QCheckBox::tristate
 
114
    \brief whether the checkbox is a tri-state checkbox
 
115
 
 
116
    The default is false; i.e. the checkbox has only two states.
 
117
*/
 
118
 
 
119
 
 
120
 
 
121
void QCheckBoxPrivate::init()
 
122
{
 
123
    Q_Q(QCheckBox);
 
124
    q->setCheckable(true);
 
125
}
 
126
 
 
127
QStyleOptionButton QCheckBoxPrivate::getStyleOption() const
 
128
{
 
129
    Q_Q(const QCheckBox);
 
130
    QStyleOptionButton opt;
 
131
    opt.init(q);
 
132
    if (down)
 
133
        opt.state |= QStyle::State_Sunken;
 
134
    if (tristate && noChange)
 
135
        opt.state |= QStyle::State_NoChange;
 
136
    else
 
137
        opt.state |= checked ? QStyle::State_On : QStyle::State_Off;
 
138
    opt.text = text;
 
139
    opt.icon = icon;
 
140
    opt.iconSize = q->iconSize();
 
141
    return opt;
 
142
}
 
143
 
 
144
/*!
 
145
    Constructs a checkbox with the given \a parent, but with no text.
 
146
 
 
147
    The \a parent argument is passed on to the QAbstractButton constructor.
 
148
*/
 
149
 
 
150
QCheckBox::QCheckBox(QWidget *parent)
 
151
    : QAbstractButton (*new QCheckBoxPrivate, parent)
 
152
{
 
153
    Q_D(QCheckBox);
 
154
    d->init();
 
155
}
 
156
 
 
157
/*!
 
158
    Constructs a checkbox with the given \a parent and \a text.
 
159
 
 
160
    The \a parent argument is passed on to the QAbstractButton constructor.
 
161
*/
 
162
 
 
163
QCheckBox::QCheckBox(const QString &text, QWidget *parent)
 
164
    : QAbstractButton (*new QCheckBoxPrivate, parent)
 
165
{
 
166
    Q_D(QCheckBox);
 
167
    d->init();
 
168
    setText(text);
 
169
}
 
170
 
 
171
void QCheckBox::setTristate(bool y)
 
172
{
 
173
    Q_D(QCheckBox);
 
174
    d->tristate = y;
 
175
}
 
176
 
 
177
bool QCheckBox::isTristate() const
 
178
{
 
179
    Q_D(const QCheckBox);
 
180
    return d->tristate;
 
181
}
 
182
 
 
183
 
 
184
/*!
 
185
    Returns the check box's check state.
 
186
 
 
187
    \sa setCheckState() Qt::CheckState
 
188
*/
 
189
Qt::CheckState QCheckBox::checkState() const
 
190
{
 
191
    Q_D(const QCheckBox);
 
192
    if (d->tristate &&  d->noChange)
 
193
        return Qt::PartiallyChecked;
 
194
    return d->checked ? Qt::Checked : Qt::Unchecked;
 
195
}
 
196
 
 
197
/*!
 
198
    Sets the check box's check state to \a state.
 
199
 
 
200
    \sa checkState() Qt::CheckState
 
201
*/
 
202
void QCheckBox::setCheckState(Qt::CheckState state)
 
203
{
 
204
    Q_D(QCheckBox);
 
205
    if (state == Qt::PartiallyChecked) {
 
206
        d->tristate = true;
 
207
        d->noChange = true;
 
208
    } else {
 
209
        d->noChange = false;
 
210
    }
 
211
    d->blockRefresh = true;
 
212
    setChecked(state != Qt::Unchecked);
 
213
    d->blockRefresh = false;
 
214
    d->refresh();
 
215
    emit stateChanged(state);
 
216
}
 
217
 
 
218
 
 
219
/*!\reimp
 
220
*/
 
221
QSize QCheckBox::sizeHint() const
 
222
{
 
223
    Q_D(const QCheckBox);
 
224
    ensurePolished();
 
225
    QFontMetrics fm = fontMetrics();
 
226
    QStyleOptionButton opt = d->getStyleOption();
 
227
    QSize sz = style()->itemTextRect(fm, QRect(0, 0, 1, 1), Qt::TextShowMnemonic, false,
 
228
                                     text()).size();
 
229
    if (!opt.icon.isNull())
 
230
        sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
 
231
    return (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
 
232
            .expandedTo(QApplication::globalStrut()));
 
233
}
 
234
 
 
235
/*!\reimp
 
236
*/
 
237
void QCheckBox::paintEvent(QPaintEvent *)
 
238
{
 
239
    Q_D(QCheckBox);
 
240
    QStylePainter p(this);
 
241
    QStyleOptionButton opt = d->getStyleOption();
 
242
    p.drawControl(QStyle::CE_CheckBox, opt);
 
243
}
 
244
 
 
245
 
 
246
/*!\reimp*/
 
247
bool QCheckBox::hitButton(const QPoint &pos) const
 
248
{
 
249
    Q_D(const QCheckBox);
 
250
    QStyleOptionButton opt = d->getStyleOption();
 
251
    return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
 
252
}
 
253
 
 
254
/*!\reimp*/
 
255
void QCheckBox::checkStateSet()
 
256
{
 
257
    Q_D(QCheckBox);
 
258
    d->noChange = false;
 
259
    emit stateChanged(checkState());
 
260
}
 
261
 
 
262
/*!\reimp*/
 
263
void QCheckBox::nextCheckState()
 
264
{
 
265
    Q_D(QCheckBox);
 
266
    if (d->tristate)
 
267
        setCheckState((Qt::CheckState)((checkState() + 1) % 3));
 
268
    else {
 
269
        QAbstractButton::nextCheckState();
 
270
        QCheckBox::checkStateSet();
 
271
    }
 
272
}
 
273
 
 
274
#ifdef QT3_SUPPORT
 
275
/*!
 
276
    Use one of the constructors that doesn't take the \a name
 
277
    argument and then use setObjectName() instead.
 
278
*/
 
279
QCheckBox::QCheckBox(QWidget *parent, const char* name)
 
280
    : QAbstractButton (*new QCheckBoxPrivate, parent)
 
281
{
 
282
    Q_D(QCheckBox);
 
283
    setObjectName(name);
 
284
    d->init();
 
285
}
 
286
 
 
287
/*!
 
288
    Use one of the constructors that doesn't take the \a name
 
289
    argument and then use setObjectName() instead.
 
290
*/
 
291
QCheckBox::QCheckBox(const QString &text, QWidget *parent, const char* name)
 
292
    : QAbstractButton (*new QCheckBoxPrivate, parent)
 
293
{
 
294
    Q_D(QCheckBox);
 
295
    setObjectName(name);
 
296
    d->init();
 
297
    setText(text);
 
298
}
 
299
 
 
300
#endif
 
301
 
 
302
#endif
 
303
 
 
304
/*!
 
305
    \fn void QCheckBox::setNoChange()
 
306
    \compat
 
307
 
 
308
    Use setCheckState() instead.
 
309
*/
 
310
 
 
311
/*!
 
312
    \fn void QCheckBox::setState(ToggleState state)
 
313
    \compat
 
314
 
 
315
    Use setCheckState() instead.
 
316
*/
 
317
 
 
318
/*!
 
319
    \fn QCheckBox::ToggleState QCheckBox::state() const
 
320
    \compat
 
321
 
 
322
    Use checkState() instead.
 
323
*/