1 | /****************************************************************************
|
---|
2 | ** $Id: qradiobutton.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QRadioButton class
|
---|
5 | **
|
---|
6 | ** Created : 940222
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the widgets module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qradiobutton.h"
|
---|
39 | #ifndef QT_NO_RADIOBUTTON
|
---|
40 | #include "qbuttongroup.h"
|
---|
41 | #include "qpainter.h"
|
---|
42 | #include "qdrawutil.h"
|
---|
43 | #include "qpixmap.h"
|
---|
44 | #include "qpixmapcache.h"
|
---|
45 | #include "qbitmap.h"
|
---|
46 | #include "qtextstream.h"
|
---|
47 | #include "qapplication.h"
|
---|
48 | #include "qstyle.h"
|
---|
49 |
|
---|
50 | /*!
|
---|
51 | \class QRadioButton qradiobutton.h
|
---|
52 | \brief The QRadioButton widget provides a radio button with a text or pixmap label.
|
---|
53 |
|
---|
54 | \ingroup basic
|
---|
55 | \mainclass
|
---|
56 |
|
---|
57 | QRadioButton and QCheckBox are both option buttons. That is, they
|
---|
58 | can be switched on (checked) or off (unchecked). The classes
|
---|
59 | differ in how the choices for the user are restricted. Check boxes
|
---|
60 | define "many of many" choices, whereas radio buttons provide a
|
---|
61 | "one of many" choice. In a group of radio buttons only one radio
|
---|
62 | button at a time can be checked; if the user selects another
|
---|
63 | button, the previously selected button is switched off.
|
---|
64 |
|
---|
65 | The easiest way to implement a "one of many" choice is simply to
|
---|
66 | put the radio buttons into QButtonGroup.
|
---|
67 |
|
---|
68 | Whenever a button is switched on or off it emits the signal
|
---|
69 | toggled(). Connect to this signal if you want to trigger an action
|
---|
70 | each time the button changes state. Otherwise, use isChecked() to
|
---|
71 | see if a particular button is selected.
|
---|
72 |
|
---|
73 | Just like QPushButton, a radio button can display text or a
|
---|
74 | pixmap. The text can be set in the constructor or with setText();
|
---|
75 | the pixmap is set with setPixmap().
|
---|
76 |
|
---|
77 | <img src=qradiobt-m.png> <img src=qradiobt-w.png>
|
---|
78 |
|
---|
79 | \important text, setText, text, pixmap, setPixmap, accel, setAccel, isToggleButton, setDown, isDown, isOn, state, autoRepeat, isExclusiveToggle, group, setAutoRepeat, toggle, pressed, released, clicked, toggled, state stateChanged
|
---|
80 |
|
---|
81 | \sa QPushButton QToolButton
|
---|
82 | \link guibooks.html#fowler GUI Design Handbook: Radio Button\endlink
|
---|
83 | */
|
---|
84 |
|
---|
85 | /*!
|
---|
86 | \property QRadioButton::checked \brief Whether the radio button is
|
---|
87 | checked
|
---|
88 |
|
---|
89 | This property will not effect any other radio buttons unless they
|
---|
90 | have been placed in the same QButtonGroup. The default value is
|
---|
91 | FALSE (unchecked).
|
---|
92 | */
|
---|
93 |
|
---|
94 | /*!
|
---|
95 | \property QRadioButton::autoMask \brief whether the radio button
|
---|
96 | is automatically masked
|
---|
97 |
|
---|
98 | \sa QWidget::setAutoMask()
|
---|
99 | */
|
---|
100 |
|
---|
101 | /*!
|
---|
102 | Constructs a radio button with no text.
|
---|
103 |
|
---|
104 | The \a parent and \a name arguments are sent on to the QWidget
|
---|
105 | constructor.
|
---|
106 | */
|
---|
107 |
|
---|
108 | QRadioButton::QRadioButton( QWidget *parent, const char *name )
|
---|
109 | : QButton( parent, name, WNoAutoErase | WMouseNoMask )
|
---|
110 | {
|
---|
111 | init();
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*!
|
---|
115 | Constructs a radio button with the text \a text.
|
---|
116 |
|
---|
117 | The \a parent and \a name arguments are sent on to the QWidget
|
---|
118 | constructor.
|
---|
119 | */
|
---|
120 |
|
---|
121 | QRadioButton::QRadioButton( const QString &text, QWidget *parent,
|
---|
122 | const char *name )
|
---|
123 | : QButton( parent, name, WNoAutoErase | WMouseNoMask )
|
---|
124 | {
|
---|
125 | init();
|
---|
126 | setText( text );
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /*
|
---|
131 | Initializes the radio button.
|
---|
132 | */
|
---|
133 |
|
---|
134 | void QRadioButton::init()
|
---|
135 | {
|
---|
136 | setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
|
---|
137 | setToggleButton( TRUE );
|
---|
138 | #ifndef QT_NO_BUTTONGROUP
|
---|
139 | QButtonGroup *bgrp = ::qt_cast<QButtonGroup*>(parentWidget());
|
---|
140 | if ( bgrp )
|
---|
141 | bgrp->setRadioButtonExclusive( TRUE );
|
---|
142 | #endif
|
---|
143 | }
|
---|
144 |
|
---|
145 | void QRadioButton::setChecked( bool check )
|
---|
146 | {
|
---|
147 | setOn( check );
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | \reimp
|
---|
155 | */
|
---|
156 | QSize QRadioButton::sizeHint() const
|
---|
157 | {
|
---|
158 | // Any more complex, and we will use style().itemRect()
|
---|
159 | // NB: QCheckBox::sizeHint() is similar
|
---|
160 | constPolish();
|
---|
161 |
|
---|
162 | QPainter p(this);
|
---|
163 | QSize sz = style().itemRect(&p, QRect(0, 0, 1, 1), ShowPrefix, FALSE,
|
---|
164 | pixmap(), text()).size();
|
---|
165 |
|
---|
166 | return (style().sizeFromContents(QStyle::CT_RadioButton, this, sz).
|
---|
167 | expandedTo(QApplication::globalStrut()));
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /*!
|
---|
172 | \reimp
|
---|
173 | */
|
---|
174 | bool QRadioButton::hitButton( const QPoint &pos ) const
|
---|
175 | {
|
---|
176 | QRect r =
|
---|
177 | QStyle::visualRect( style().subRect( QStyle::SR_RadioButtonFocusRect,
|
---|
178 | this ), this );
|
---|
179 | if ( qApp->reverseLayout() ) {
|
---|
180 | r.setRight( width() );
|
---|
181 | } else {
|
---|
182 | r.setLeft( 0 );
|
---|
183 | }
|
---|
184 | return r.contains( pos );
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /*!
|
---|
189 | \reimp
|
---|
190 | */
|
---|
191 | void QRadioButton::drawButton( QPainter *paint )
|
---|
192 | {
|
---|
193 | QPainter *p = paint;
|
---|
194 | QRect irect = QStyle::visualRect( style().subRect(QStyle::SR_RadioButtonIndicator, this), this );
|
---|
195 | const QColorGroup &cg = colorGroup();
|
---|
196 |
|
---|
197 | #if !defined( QT_NO_TEXTSTREAM ) && !defined( Q_WS_MACX )
|
---|
198 | # define SAVE_RADIOBUTTON_PIXMAPS
|
---|
199 | #endif
|
---|
200 | #if defined(SAVE_RADIOBUTTON_PIXMAPS)
|
---|
201 | QString pmkey; // pixmap key
|
---|
202 | int kf = 0;
|
---|
203 | if ( isDown() )
|
---|
204 | kf |= 1;
|
---|
205 | if ( isOn() )
|
---|
206 | kf |= 2;
|
---|
207 | if ( isEnabled() )
|
---|
208 | kf |= 4;
|
---|
209 | if( isActiveWindow() )
|
---|
210 | kf |= 8;
|
---|
211 | if ( hasMouse() )
|
---|
212 | kf |= 16;
|
---|
213 | if ( hasFocus() )
|
---|
214 | kf |= 32;
|
---|
215 |
|
---|
216 | QTextOStream os(&pmkey);
|
---|
217 | os << "$qt_radio_" << style().className() << "_"
|
---|
218 | << palette().serialNumber() << "_" << irect.width() << "x" << irect.height() << "_" << kf;
|
---|
219 | QPixmap *pm = QPixmapCache::find( pmkey );
|
---|
220 | if ( pm ) { // pixmap exists
|
---|
221 | drawButtonLabel( p );
|
---|
222 | p->drawPixmap( irect.topLeft(), *pm );
|
---|
223 | return;
|
---|
224 | }
|
---|
225 | bool use_pm = TRUE;
|
---|
226 | QPainter pmpaint;
|
---|
227 | int wx, wy;
|
---|
228 | if ( use_pm ) {
|
---|
229 | pm = new QPixmap( irect.size() ); // create new pixmap
|
---|
230 | Q_CHECK_PTR( pm );
|
---|
231 | pm->fill(paletteBackgroundColor());
|
---|
232 | QPainter::redirect(this, pm);
|
---|
233 | pmpaint.begin(this);
|
---|
234 | p = &pmpaint; // draw in pixmap
|
---|
235 | wx = irect.x(); // save x,y coords
|
---|
236 | wy = irect.y();
|
---|
237 | irect.moveTopLeft(QPoint(0, 0));
|
---|
238 | p->setBackgroundColor(paletteBackgroundColor());
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | QStyle::SFlags flags = QStyle::Style_Default;
|
---|
243 | if ( isEnabled() )
|
---|
244 | flags |= QStyle::Style_Enabled;
|
---|
245 | if ( hasFocus() )
|
---|
246 | flags |= QStyle::Style_HasFocus;
|
---|
247 | if ( isDown() )
|
---|
248 | flags |= QStyle::Style_Down;
|
---|
249 | if ( hasMouse() )
|
---|
250 | flags |= QStyle::Style_MouseOver;
|
---|
251 | if ( state() == QButton::On )
|
---|
252 | flags |= QStyle::Style_On;
|
---|
253 | else if ( state() == QButton::Off )
|
---|
254 | flags |= QStyle::Style_Off;
|
---|
255 |
|
---|
256 | style().drawControl(QStyle::CE_RadioButton, p, this, irect, cg, flags);
|
---|
257 |
|
---|
258 | #if defined(SAVE_RADIOBUTTON_PIXMAPS)
|
---|
259 | if ( use_pm ) {
|
---|
260 | pmpaint.end();
|
---|
261 | QPainter::redirect(this, NULL);
|
---|
262 | if ( backgroundPixmap() || backgroundMode() == X11ParentRelative ) {
|
---|
263 | QBitmap bm( pm->size() );
|
---|
264 | bm.fill( color0 );
|
---|
265 | pmpaint.begin( &bm );
|
---|
266 | style().drawControlMask(QStyle::CE_RadioButton, &pmpaint, this, irect);
|
---|
267 | pmpaint.end();
|
---|
268 | pm->setMask( bm );
|
---|
269 | }
|
---|
270 | p = paint; // draw in default device
|
---|
271 | p->drawPixmap( wx, wy, *pm );
|
---|
272 | if (!QPixmapCache::insert(pmkey, pm) ) // save in cache
|
---|
273 | delete pm;
|
---|
274 | }
|
---|
275 | #endif
|
---|
276 |
|
---|
277 | drawButtonLabel( p );
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 | /*!
|
---|
283 | \reimp
|
---|
284 | */
|
---|
285 | void QRadioButton::drawButtonLabel( QPainter *p )
|
---|
286 | {
|
---|
287 | QRect r =
|
---|
288 | QStyle::visualRect( style().subRect(QStyle::SR_RadioButtonContents,
|
---|
289 | this), this );
|
---|
290 |
|
---|
291 | QStyle::SFlags flags = QStyle::Style_Default;
|
---|
292 | if (isEnabled())
|
---|
293 | flags |= QStyle::Style_Enabled;
|
---|
294 | if (hasFocus())
|
---|
295 | flags |= QStyle::Style_HasFocus;
|
---|
296 | if (isDown())
|
---|
297 | flags |= QStyle::Style_Down;
|
---|
298 | if (state() == QButton::On)
|
---|
299 | flags |= QStyle::Style_On;
|
---|
300 | else if (state() == QButton::Off)
|
---|
301 | flags |= QStyle::Style_Off;
|
---|
302 |
|
---|
303 | style().drawControl(QStyle::CE_RadioButtonLabel, p, this, r, colorGroup(), flags);
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | /*!
|
---|
308 | \reimp
|
---|
309 | */
|
---|
310 | void QRadioButton::resizeEvent( QResizeEvent* e )
|
---|
311 | {
|
---|
312 | QButton::resizeEvent(e);
|
---|
313 | if ( isVisible() ) {
|
---|
314 | QPainter p(this);
|
---|
315 | QSize isz = style().itemRect(&p, QRect(0, 0, 1, 1), ShowPrefix, FALSE,
|
---|
316 | pixmap(), text()).size();
|
---|
317 | QSize wsz = (style().sizeFromContents(QStyle::CT_RadioButton, this, isz).
|
---|
318 | expandedTo(QApplication::globalStrut()));
|
---|
319 |
|
---|
320 | update(wsz.width(), isz.width(), 0, wsz.height());
|
---|
321 | }
|
---|
322 | if (autoMask())
|
---|
323 | updateMask();
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*!
|
---|
327 | \reimp
|
---|
328 | */
|
---|
329 | void QRadioButton::updateMask()
|
---|
330 | {
|
---|
331 | QRect irect =
|
---|
332 | QStyle::visualRect( style().subRect( QStyle::SR_RadioButtonIndicator,
|
---|
333 | this ), this );
|
---|
334 |
|
---|
335 | QBitmap bm(width(), height());
|
---|
336 | bm.fill(color0);
|
---|
337 |
|
---|
338 | QPainter p( &bm, this );
|
---|
339 | style().drawControlMask(QStyle::CE_RadioButton, &p, this, irect);
|
---|
340 | if ( ! text().isNull() || ( pixmap() && ! pixmap()->isNull() ) ) {
|
---|
341 | QRect crect =
|
---|
342 | QStyle::visualRect( style().subRect( QStyle::SR_RadioButtonContents,
|
---|
343 | this ), this );
|
---|
344 | QRect frect =
|
---|
345 | QStyle::visualRect( style().subRect( QStyle::SR_RadioButtonFocusRect,
|
---|
346 | this ), this );
|
---|
347 | QRect label(crect.unite(frect));
|
---|
348 | p.fillRect(label, color1);
|
---|
349 | }
|
---|
350 | p.end();
|
---|
351 |
|
---|
352 | setMask(bm);
|
---|
353 | }
|
---|
354 |
|
---|
355 | #endif
|
---|