1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qgroupbox.h"
|
---|
43 | #ifndef QT_NO_GROUPBOX
|
---|
44 | #include "qapplication.h"
|
---|
45 | #include "qbitmap.h"
|
---|
46 | #include "qdrawutil.h"
|
---|
47 | #include "qevent.h"
|
---|
48 | #include "qlayout.h"
|
---|
49 | #include "qradiobutton.h"
|
---|
50 | #include "qstyle.h"
|
---|
51 | #include "qstyleoption.h"
|
---|
52 | #include "qstylepainter.h"
|
---|
53 | #ifndef QT_NO_ACCESSIBILITY
|
---|
54 | #include "qaccessible.h"
|
---|
55 | #endif
|
---|
56 | #include <private/qwidget_p.h>
|
---|
57 |
|
---|
58 | #include "qdebug.h"
|
---|
59 |
|
---|
60 | QT_BEGIN_NAMESPACE
|
---|
61 |
|
---|
62 | class QGroupBoxPrivate : public QWidgetPrivate
|
---|
63 | {
|
---|
64 | Q_DECLARE_PUBLIC(QGroupBox)
|
---|
65 |
|
---|
66 | public:
|
---|
67 | void skip();
|
---|
68 | void init();
|
---|
69 | void calculateFrame();
|
---|
70 | QString title;
|
---|
71 | int align;
|
---|
72 | #ifndef QT_NO_SHORTCUT
|
---|
73 | int shortcutId;
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | void _q_fixFocus(Qt::FocusReason reason);
|
---|
77 | void _q_setChildrenEnabled(bool b);
|
---|
78 | void click();
|
---|
79 | bool flat;
|
---|
80 | bool checkable;
|
---|
81 | bool checked;
|
---|
82 | bool hover;
|
---|
83 | bool overCheckBox;
|
---|
84 | QStyle::SubControl pressedControl;
|
---|
85 | };
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | Initialize \a option with the values from this QGroupBox. This method
|
---|
89 | is useful for subclasses when they need a QStyleOptionGroupBox, but don't want
|
---|
90 | to fill in all the information themselves.
|
---|
91 |
|
---|
92 | \sa QStyleOption::initFrom()
|
---|
93 | */
|
---|
94 | void QGroupBox::initStyleOption(QStyleOptionGroupBox *option) const
|
---|
95 | {
|
---|
96 | if (!option)
|
---|
97 | return;
|
---|
98 |
|
---|
99 | Q_D(const QGroupBox);
|
---|
100 | option->initFrom(this);
|
---|
101 | option->text = d->title;
|
---|
102 | option->lineWidth = 1;
|
---|
103 | option->midLineWidth = 0;
|
---|
104 | option->textAlignment = Qt::Alignment(d->align);
|
---|
105 | option->activeSubControls |= d->pressedControl;
|
---|
106 | option->subControls = QStyle::SC_GroupBoxFrame;
|
---|
107 |
|
---|
108 | if (d->hover)
|
---|
109 | option->state |= QStyle::State_MouseOver;
|
---|
110 | else
|
---|
111 | option->state &= ~QStyle::State_MouseOver;
|
---|
112 |
|
---|
113 | if (d->flat)
|
---|
114 | option->features |= QStyleOptionFrameV2::Flat;
|
---|
115 |
|
---|
116 | if (d->checkable) {
|
---|
117 | option->subControls |= QStyle::SC_GroupBoxCheckBox;
|
---|
118 | option->state |= (d->checked ? QStyle::State_On : QStyle::State_Off);
|
---|
119 | if ((d->pressedControl == QStyle::SC_GroupBoxCheckBox
|
---|
120 | || d->pressedControl == QStyle::SC_GroupBoxLabel) && (d->hover || d->overCheckBox))
|
---|
121 | option->state |= QStyle::State_Sunken;
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (!option->palette.isBrushSet(isEnabled() ? QPalette::Active :
|
---|
125 | QPalette::Disabled, QPalette::WindowText))
|
---|
126 | option->textColor = QColor(style()->styleHint(QStyle::SH_GroupBox_TextLabelColor,
|
---|
127 | option, this));
|
---|
128 |
|
---|
129 | if (!d->title.isEmpty())
|
---|
130 | option->subControls |= QStyle::SC_GroupBoxLabel;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void QGroupBoxPrivate::click()
|
---|
134 | {
|
---|
135 | Q_Q(QGroupBox);
|
---|
136 |
|
---|
137 | QPointer<QGroupBox> guard(q);
|
---|
138 | q->setChecked(!checked);
|
---|
139 | if (!guard)
|
---|
140 | return;
|
---|
141 | emit q->clicked(checked);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*!
|
---|
145 | \class QGroupBox
|
---|
146 | \brief The QGroupBox widget provides a group box frame with a title.
|
---|
147 |
|
---|
148 | \ingroup organizers
|
---|
149 | \ingroup geomanagement
|
---|
150 | \ingroup appearance
|
---|
151 | \mainclass
|
---|
152 |
|
---|
153 | A group box provides a frame, a title and a keyboard shortcut, and
|
---|
154 | displays various other widgets inside itself. The title is on top,
|
---|
155 | the keyboard shortcut moves keyboard focus to one of the group
|
---|
156 | box's child widgets.
|
---|
157 |
|
---|
158 | QGroupBox also lets you set the \l title (normally set in the
|
---|
159 | constructor) and the title's \l alignment. Group boxes can be
|
---|
160 | \l checkable; child widgets in checkable group boxes are enabled or
|
---|
161 | disabled depending on whether or not the group box is \l checked.
|
---|
162 |
|
---|
163 | You can minimize the space consumption of a group box by enabling
|
---|
164 | the \l flat property. In most \l{QStyle}{styles}, enabling this
|
---|
165 | property results in the removal of the left, right and bottom
|
---|
166 | edges of the frame.
|
---|
167 |
|
---|
168 | QGroupBox doesn't automatically lay out the child widgets (which
|
---|
169 | are often \l{QCheckBox}es or \l{QRadioButton}s but can be any
|
---|
170 | widgets). The following example shows how we can set up a
|
---|
171 | QGroupBox with a layout:
|
---|
172 |
|
---|
173 | \snippet examples/widgets/groupbox/window.cpp 2
|
---|
174 |
|
---|
175 | \table 100%
|
---|
176 | \row \o \inlineimage windowsxp-groupbox.png Screenshot of a Windows XP style group box
|
---|
177 | \o \inlineimage macintosh-groupbox.png Screenshot of a Macintosh style group box
|
---|
178 | \o \inlineimage plastique-groupbox.png Screenshot of a Plastique style group box
|
---|
179 | \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} group box.
|
---|
180 | \o A \l{Macintosh Style Widget Gallery}{Macintosh style} group box.
|
---|
181 | \o A \l{Plastique Style Widget Gallery}{Plastique style} group box.
|
---|
182 | \endtable
|
---|
183 |
|
---|
184 | \sa QButtonGroup, {Group Box Example}
|
---|
185 | */
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | /*!
|
---|
190 | Constructs a group box widget with the given \a parent but with no title.
|
---|
191 | */
|
---|
192 |
|
---|
193 | QGroupBox::QGroupBox(QWidget *parent)
|
---|
194 | : QWidget(*new QGroupBoxPrivate, parent, 0)
|
---|
195 | {
|
---|
196 | Q_D(QGroupBox);
|
---|
197 | d->init();
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*!
|
---|
201 | Constructs a group box with the given \a title and \a parent.
|
---|
202 | */
|
---|
203 |
|
---|
204 | QGroupBox::QGroupBox(const QString &title, QWidget *parent)
|
---|
205 | : QWidget(*new QGroupBoxPrivate, parent, 0)
|
---|
206 | {
|
---|
207 | Q_D(QGroupBox);
|
---|
208 | d->init();
|
---|
209 | setTitle(title);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | Destroys the group box.
|
---|
215 | */
|
---|
216 | QGroupBox::~QGroupBox()
|
---|
217 | {
|
---|
218 | }
|
---|
219 |
|
---|
220 | void QGroupBoxPrivate::init()
|
---|
221 | {
|
---|
222 | Q_Q(QGroupBox);
|
---|
223 | align = Qt::AlignLeft;
|
---|
224 | #ifndef QT_NO_SHORTCUT
|
---|
225 | shortcutId = 0;
|
---|
226 | #endif
|
---|
227 | flat = false;
|
---|
228 | checkable = false;
|
---|
229 | checked = true;
|
---|
230 | hover = false;
|
---|
231 | overCheckBox = false;
|
---|
232 | pressedControl = QStyle::SC_None;
|
---|
233 | calculateFrame();
|
---|
234 | q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred,
|
---|
235 | QSizePolicy::GroupBox));
|
---|
236 | }
|
---|
237 |
|
---|
238 | void QGroupBox::setTitle(const QString &title)
|
---|
239 | {
|
---|
240 | Q_D(QGroupBox);
|
---|
241 | if (d->title == title) // no change
|
---|
242 | return;
|
---|
243 | d->title = title;
|
---|
244 | #ifndef QT_NO_SHORTCUT
|
---|
245 | releaseShortcut(d->shortcutId);
|
---|
246 | d->shortcutId = grabShortcut(QKeySequence::mnemonic(title));
|
---|
247 | #endif
|
---|
248 | d->calculateFrame();
|
---|
249 |
|
---|
250 | update();
|
---|
251 | updateGeometry();
|
---|
252 | #ifndef QT_NO_ACCESSIBILITY
|
---|
253 | QAccessible::updateAccessibility(this, 0, QAccessible::NameChanged);
|
---|
254 | #endif
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*!
|
---|
258 | \property QGroupBox::title
|
---|
259 | \brief the group box title text
|
---|
260 |
|
---|
261 | The group box title text will have a keyboard shortcut if the title
|
---|
262 | contains an ampersand ('&') followed by a letter.
|
---|
263 |
|
---|
264 | \snippet doc/src/snippets/code/src_gui_widgets_qgroupbox.cpp 0
|
---|
265 |
|
---|
266 | In the example above, \key Alt+U moves the keyboard focus to the
|
---|
267 | group box. See the \l {QShortcut#mnemonic}{QShortcut}
|
---|
268 | documentation for details (to display an actual ampersand, use
|
---|
269 | '&&').
|
---|
270 |
|
---|
271 | There is no default title text.
|
---|
272 |
|
---|
273 | \sa alignment
|
---|
274 | */
|
---|
275 |
|
---|
276 | QString QGroupBox::title() const
|
---|
277 | {
|
---|
278 | Q_D(const QGroupBox);
|
---|
279 | return d->title;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*!
|
---|
283 | \property QGroupBox::alignment
|
---|
284 | \brief the alignment of the group box title.
|
---|
285 |
|
---|
286 | Most styles place the title at the top of the frame. The horizontal
|
---|
287 | alignment of the title can be specified using single values from
|
---|
288 | the following list:
|
---|
289 |
|
---|
290 | \list
|
---|
291 | \i Qt::AlignLeft aligns the title text with the left-hand side of the group box.
|
---|
292 | \i Qt::AlignRight aligns the title text with the right-hand side of the group box.
|
---|
293 | \i Qt::AlignHCenter aligns the title text with the horizontal center of the group box.
|
---|
294 | \endlist
|
---|
295 |
|
---|
296 | The default alignment is Qt::AlignLeft.
|
---|
297 |
|
---|
298 | \sa Qt::Alignment
|
---|
299 | */
|
---|
300 | Qt::Alignment QGroupBox::alignment() const
|
---|
301 | {
|
---|
302 | Q_D(const QGroupBox);
|
---|
303 | return QFlag(d->align);
|
---|
304 | }
|
---|
305 |
|
---|
306 | void QGroupBox::setAlignment(int alignment)
|
---|
307 | {
|
---|
308 | Q_D(QGroupBox);
|
---|
309 | d->align = alignment;
|
---|
310 | updateGeometry();
|
---|
311 | update();
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*! \reimp
|
---|
315 | */
|
---|
316 | void QGroupBox::resizeEvent(QResizeEvent *e)
|
---|
317 | {
|
---|
318 | QWidget::resizeEvent(e);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /*! \reimp
|
---|
322 | */
|
---|
323 |
|
---|
324 | void QGroupBox::paintEvent(QPaintEvent *)
|
---|
325 | {
|
---|
326 | QStylePainter paint(this);
|
---|
327 | QStyleOptionGroupBox option;
|
---|
328 | initStyleOption(&option);
|
---|
329 | paint.drawComplexControl(QStyle::CC_GroupBox, option);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*! \reimp */
|
---|
333 | bool QGroupBox::event(QEvent *e)
|
---|
334 | {
|
---|
335 | Q_D(QGroupBox);
|
---|
336 | #ifndef QT_NO_SHORTCUT
|
---|
337 | if (e->type() == QEvent::Shortcut) {
|
---|
338 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
|
---|
339 | if (se->shortcutId() == d->shortcutId) {
|
---|
340 | if (!isCheckable()) {
|
---|
341 | d->_q_fixFocus(Qt::ShortcutFocusReason);
|
---|
342 | } else {
|
---|
343 | d->click();
|
---|
344 | setFocus(Qt::ShortcutFocusReason);
|
---|
345 | }
|
---|
346 | return true;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | #endif
|
---|
350 | QStyleOptionGroupBox box;
|
---|
351 | initStyleOption(&box);
|
---|
352 | switch (e->type()) {
|
---|
353 | case QEvent::HoverEnter:
|
---|
354 | case QEvent::HoverMove: {
|
---|
355 | QStyle::SubControl control = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
|
---|
356 | static_cast<QHoverEvent *>(e)->pos(),
|
---|
357 | this);
|
---|
358 | bool oldHover = d->hover;
|
---|
359 | d->hover = d->checkable && (control == QStyle::SC_GroupBoxLabel || control == QStyle::SC_GroupBoxCheckBox);
|
---|
360 | if (oldHover != d->hover) {
|
---|
361 | QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this)
|
---|
362 | | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this);
|
---|
363 | update(rect);
|
---|
364 | }
|
---|
365 | return true;
|
---|
366 | }
|
---|
367 | case QEvent::HoverLeave:
|
---|
368 | d->hover = false;
|
---|
369 | if (d->checkable) {
|
---|
370 | QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this)
|
---|
371 | | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this);
|
---|
372 | update(rect);
|
---|
373 | }
|
---|
374 | return true;
|
---|
375 | case QEvent::KeyPress: {
|
---|
376 | QKeyEvent *k = static_cast<QKeyEvent*>(e);
|
---|
377 | if (!k->isAutoRepeat() && (k->key() == Qt::Key_Select || k->key() == Qt::Key_Space)) {
|
---|
378 | d->pressedControl = QStyle::SC_GroupBoxCheckBox;
|
---|
379 | update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));
|
---|
380 | return true;
|
---|
381 | }
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | case QEvent::KeyRelease: {
|
---|
385 | QKeyEvent *k = static_cast<QKeyEvent*>(e);
|
---|
386 | if (!k->isAutoRepeat() && (k->key() == Qt::Key_Select || k->key() == Qt::Key_Space)) {
|
---|
387 | bool toggle = (d->pressedControl == QStyle::SC_GroupBoxLabel
|
---|
388 | || d->pressedControl == QStyle::SC_GroupBoxCheckBox);
|
---|
389 | d->pressedControl = QStyle::SC_None;
|
---|
390 | if (toggle)
|
---|
391 | d->click();
|
---|
392 | return true;
|
---|
393 | }
|
---|
394 | break;
|
---|
395 | }
|
---|
396 | default:
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | return QWidget::event(e);
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*!\reimp */
|
---|
403 | void QGroupBox::childEvent(QChildEvent *c)
|
---|
404 | {
|
---|
405 | Q_D(QGroupBox);
|
---|
406 | if (c->type() != QEvent::ChildAdded || !c->child()->isWidgetType())
|
---|
407 | return;
|
---|
408 | QWidget *w = (QWidget*)c->child();
|
---|
409 | if (d->checkable) {
|
---|
410 | if (d->checked) {
|
---|
411 | if (!w->testAttribute(Qt::WA_ForceDisabled))
|
---|
412 | w->setEnabled(true);
|
---|
413 | } else {
|
---|
414 | if (w->isEnabled()) {
|
---|
415 | w->setEnabled(false);
|
---|
416 | w->setAttribute(Qt::WA_ForceDisabled, false);
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /*!
|
---|
424 | \internal
|
---|
425 |
|
---|
426 | This private slot finds a widget in this group box that can accept
|
---|
427 | focus, and gives the focus to that widget.
|
---|
428 | */
|
---|
429 |
|
---|
430 | void QGroupBoxPrivate::_q_fixFocus(Qt::FocusReason reason)
|
---|
431 | {
|
---|
432 | Q_Q(QGroupBox);
|
---|
433 | QWidget *fw = q->focusWidget();
|
---|
434 | if (!fw) {
|
---|
435 | QWidget * best = 0;
|
---|
436 | QWidget * candidate = 0;
|
---|
437 | QWidget * w = q;
|
---|
438 | while ((w = w->nextInFocusChain()) != q) {
|
---|
439 | if (q->isAncestorOf(w) && (w->focusPolicy() & Qt::TabFocus) == Qt::TabFocus && w->isVisibleTo(q)) {
|
---|
440 | if (!best && qobject_cast<QRadioButton*>(w) && ((QRadioButton*)w)->isChecked())
|
---|
441 | // we prefer a checked radio button or a widget that
|
---|
442 | // already has focus, if there is one
|
---|
443 | best = w;
|
---|
444 | else
|
---|
445 | if (!candidate)
|
---|
446 | // but we'll accept anything that takes focus
|
---|
447 | candidate = w;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | if (best)
|
---|
451 | fw = best;
|
---|
452 | else
|
---|
453 | if (candidate)
|
---|
454 | fw = candidate;
|
---|
455 | }
|
---|
456 | if (fw)
|
---|
457 | fw->setFocus(reason);
|
---|
458 | }
|
---|
459 |
|
---|
460 | /*
|
---|
461 | Sets the right frame rect depending on the title.
|
---|
462 | */
|
---|
463 | void QGroupBoxPrivate::calculateFrame()
|
---|
464 | {
|
---|
465 | Q_Q(QGroupBox);
|
---|
466 | QStyleOptionGroupBox box;
|
---|
467 | q->initStyleOption(&box);
|
---|
468 | QRect contentsRect = q->style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxContents, q);
|
---|
469 | q->setContentsMargins(contentsRect.left() - box.rect.left(), contentsRect.top() - box.rect.top(),
|
---|
470 | box.rect.right() - contentsRect.right(), box.rect.bottom() - contentsRect.bottom());
|
---|
471 | setLayoutItemMargins(QStyle::SE_GroupBoxLayoutItem, &box);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /*! \reimp
|
---|
475 | */
|
---|
476 | void QGroupBox::focusInEvent(QFocusEvent *fe)
|
---|
477 | { // note no call to super
|
---|
478 | Q_D(QGroupBox);
|
---|
479 | if (focusPolicy() == Qt::NoFocus) {
|
---|
480 | d->_q_fixFocus(fe->reason());
|
---|
481 | } else {
|
---|
482 | QStyleOptionGroupBox box;
|
---|
483 | initStyleOption(&box);
|
---|
484 | QRect rect = style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this)
|
---|
485 | | style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxLabel, this);
|
---|
486 | update(rect);
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | /*!
|
---|
492 | \reimp
|
---|
493 | */
|
---|
494 | QSize QGroupBox::minimumSizeHint() const
|
---|
495 | {
|
---|
496 | Q_D(const QGroupBox);
|
---|
497 | QStyleOptionGroupBox option;
|
---|
498 | initStyleOption(&option);
|
---|
499 |
|
---|
500 | QFontMetrics metrics(fontMetrics());
|
---|
501 |
|
---|
502 | int baseWidth = metrics.width(d->title) + metrics.width(QLatin1Char(' '));
|
---|
503 | int baseHeight = metrics.height();
|
---|
504 | if (d->checkable) {
|
---|
505 | baseWidth += style()->pixelMetric(QStyle::PM_IndicatorWidth);
|
---|
506 | baseWidth += style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing);
|
---|
507 | baseHeight = qMax(baseHeight, style()->pixelMetric(QStyle::PM_IndicatorHeight));
|
---|
508 | }
|
---|
509 |
|
---|
510 | QSize size = style()->sizeFromContents(QStyle::CT_GroupBox, &option, QSize(baseWidth, baseHeight), this);
|
---|
511 | return size.expandedTo(QWidget::minimumSizeHint());
|
---|
512 | }
|
---|
513 |
|
---|
514 | /*!
|
---|
515 | \property QGroupBox::flat
|
---|
516 | \brief whether the group box is painted flat or has a frame
|
---|
517 |
|
---|
518 | A group box usually consists of a surrounding frame with a title
|
---|
519 | at the top. If this property is enabled, only the top part of the frame is
|
---|
520 | drawn in most styles; otherwise the whole frame is drawn.
|
---|
521 |
|
---|
522 | By default, this property is disabled; i.e. group boxes are not flat unless
|
---|
523 | explicitly specified.
|
---|
524 |
|
---|
525 | \bold{Note:} In some styles, flat and non-flat group boxes have similar
|
---|
526 | representations and may not be as distinguishable as they are in other
|
---|
527 | styles.
|
---|
528 |
|
---|
529 | \sa title
|
---|
530 | */
|
---|
531 | bool QGroupBox::isFlat() const
|
---|
532 | {
|
---|
533 | Q_D(const QGroupBox);
|
---|
534 | return d->flat;
|
---|
535 | }
|
---|
536 |
|
---|
537 | void QGroupBox::setFlat(bool b)
|
---|
538 | {
|
---|
539 | Q_D(QGroupBox);
|
---|
540 | if (d->flat == b)
|
---|
541 | return;
|
---|
542 | d->flat = b;
|
---|
543 | updateGeometry();
|
---|
544 | update();
|
---|
545 | }
|
---|
546 |
|
---|
547 |
|
---|
548 | /*!
|
---|
549 | \property QGroupBox::checkable
|
---|
550 | \brief whether the group box has a checkbox in its title
|
---|
551 |
|
---|
552 | If this property is true, the group box displays its title using
|
---|
553 | a checkbox in place of an ordinary label. If the checkbox is checked,
|
---|
554 | the group box's children are enabled; otherwise they are disabled and
|
---|
555 | inaccessible.
|
---|
556 |
|
---|
557 | By default, group boxes are not checkable.
|
---|
558 |
|
---|
559 | If this property is enabled for a group box, it will also be initially
|
---|
560 | checked to ensure that its contents are enabled.
|
---|
561 |
|
---|
562 | \sa checked
|
---|
563 | */
|
---|
564 | void QGroupBox::setCheckable(bool checkable)
|
---|
565 | {
|
---|
566 | Q_D(QGroupBox);
|
---|
567 |
|
---|
568 | bool wasCheckable = d->checkable;
|
---|
569 | d->checkable = checkable;
|
---|
570 |
|
---|
571 | if (checkable) {
|
---|
572 | setChecked(true);
|
---|
573 | if (!wasCheckable) {
|
---|
574 | setFocusPolicy(Qt::StrongFocus);
|
---|
575 | d->_q_setChildrenEnabled(true);
|
---|
576 | updateGeometry();
|
---|
577 | }
|
---|
578 | } else {
|
---|
579 | if (wasCheckable) {
|
---|
580 | setFocusPolicy(Qt::NoFocus);
|
---|
581 | d->_q_setChildrenEnabled(true);
|
---|
582 | updateGeometry();
|
---|
583 | }
|
---|
584 | d->_q_setChildrenEnabled(true);
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (wasCheckable != checkable) {
|
---|
588 | d->calculateFrame();
|
---|
589 | update();
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | bool QGroupBox::isCheckable() const
|
---|
594 | {
|
---|
595 | Q_D(const QGroupBox);
|
---|
596 | return d->checkable;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | bool QGroupBox::isChecked() const
|
---|
601 | {
|
---|
602 | Q_D(const QGroupBox);
|
---|
603 | return d->checkable && d->checked;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /*!
|
---|
608 | \fn void QGroupBox::toggled(bool on)
|
---|
609 |
|
---|
610 | If the group box is checkable, this signal is emitted when the check box
|
---|
611 | is toggled. \a on is true if the check box is checked; otherwise it is false.
|
---|
612 |
|
---|
613 | \sa checkable
|
---|
614 | */
|
---|
615 |
|
---|
616 |
|
---|
617 | /*!
|
---|
618 | \fn void QGroupBox::clicked(bool checked)
|
---|
619 | \since 4.2
|
---|
620 |
|
---|
621 | This signal is emitted when the check box is activated (i.e. pressed down
|
---|
622 | then released while the mouse cursor is inside the button), or when the
|
---|
623 | shortcut key is typed, Notably, this signal is \e not emitted if you call
|
---|
624 | setChecked().
|
---|
625 |
|
---|
626 | If the check box is checked \a checked is true; it is false if the check
|
---|
627 | box is unchecked.
|
---|
628 |
|
---|
629 | \sa checkable, toggled(), checked
|
---|
630 | */
|
---|
631 |
|
---|
632 | /*!
|
---|
633 | \property QGroupBox::checked
|
---|
634 | \brief whether the group box is checked
|
---|
635 |
|
---|
636 | If the group box is checkable, it is displayed with a check box.
|
---|
637 | If the check box is checked, the group box's children are enabled;
|
---|
638 | otherwise the children are disabled and are inaccessible to the user.
|
---|
639 |
|
---|
640 | By default, checkable group boxes are also checked.
|
---|
641 |
|
---|
642 | \sa checkable
|
---|
643 | */
|
---|
644 | void QGroupBox::setChecked(bool b)
|
---|
645 | {
|
---|
646 | Q_D(QGroupBox);
|
---|
647 | if (d->checkable) {
|
---|
648 | if (d->checked != b)
|
---|
649 | update();
|
---|
650 | bool wasToggled = (b != d->checked);
|
---|
651 | d->checked = b;
|
---|
652 | if (wasToggled) {
|
---|
653 | d->_q_setChildrenEnabled(b);
|
---|
654 | emit toggled(b);
|
---|
655 | }
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | /*
|
---|
660 | sets all children of the group box except the qt_groupbox_checkbox
|
---|
661 | to either disabled/enabled
|
---|
662 | */
|
---|
663 | void QGroupBoxPrivate::_q_setChildrenEnabled(bool b)
|
---|
664 | {
|
---|
665 | Q_Q(QGroupBox);
|
---|
666 | QObjectList childList = q->children();
|
---|
667 | for (int i = 0; i < childList.size(); ++i) {
|
---|
668 | QObject *o = childList.at(i);
|
---|
669 | if (o->isWidgetType()) {
|
---|
670 | QWidget *w = static_cast<QWidget *>(o);
|
---|
671 | if (b) {
|
---|
672 | if (!w->testAttribute(Qt::WA_ForceDisabled))
|
---|
673 | w->setEnabled(true);
|
---|
674 | } else {
|
---|
675 | if (w->isEnabled()) {
|
---|
676 | w->setEnabled(false);
|
---|
677 | w->setAttribute(Qt::WA_ForceDisabled, false);
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | /*! \reimp */
|
---|
685 | void QGroupBox::changeEvent(QEvent *ev)
|
---|
686 | {
|
---|
687 | Q_D(QGroupBox);
|
---|
688 | if (ev->type() == QEvent::EnabledChange) {
|
---|
689 | if (d->checkable && isEnabled()) {
|
---|
690 | // we are being enabled - disable children
|
---|
691 | if (!d->checked)
|
---|
692 | d->_q_setChildrenEnabled(false);
|
---|
693 | }
|
---|
694 | } else if (ev->type() == QEvent::FontChange
|
---|
695 | #ifdef Q_WS_MAC
|
---|
696 | || ev->type() == QEvent::MacSizeChange
|
---|
697 | #endif
|
---|
698 | || ev->type() == QEvent::StyleChange) {
|
---|
699 | d->calculateFrame();
|
---|
700 | }
|
---|
701 | QWidget::changeEvent(ev);
|
---|
702 | }
|
---|
703 |
|
---|
704 | /*! \reimp */
|
---|
705 | void QGroupBox::mousePressEvent(QMouseEvent *event)
|
---|
706 | {
|
---|
707 | if (event->button() != Qt::LeftButton) {
|
---|
708 | event->ignore();
|
---|
709 | return;
|
---|
710 | }
|
---|
711 |
|
---|
712 | Q_D(QGroupBox);
|
---|
713 | QStyleOptionGroupBox box;
|
---|
714 | initStyleOption(&box);
|
---|
715 | d->pressedControl = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
|
---|
716 | event->pos(), this);
|
---|
717 | if (d->checkable && (d->pressedControl & (QStyle::SC_GroupBoxCheckBox | QStyle::SC_GroupBoxLabel))) {
|
---|
718 | d->overCheckBox = true;
|
---|
719 | update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));
|
---|
720 | }
|
---|
721 | }
|
---|
722 |
|
---|
723 | /*! \reimp */
|
---|
724 | void QGroupBox::mouseMoveEvent(QMouseEvent *event)
|
---|
725 | {
|
---|
726 | Q_D(QGroupBox);
|
---|
727 | QStyleOptionGroupBox box;
|
---|
728 | initStyleOption(&box);
|
---|
729 | QStyle::SubControl pressed = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
|
---|
730 | event->pos(), this);
|
---|
731 | bool oldOverCheckBox = d->overCheckBox;
|
---|
732 | d->overCheckBox = (pressed == QStyle::SC_GroupBoxCheckBox || pressed == QStyle::SC_GroupBoxLabel);
|
---|
733 | if (d->checkable && (d->pressedControl == QStyle::SC_GroupBoxCheckBox || d->pressedControl == QStyle::SC_GroupBoxLabel)
|
---|
734 | && (d->overCheckBox != oldOverCheckBox))
|
---|
735 | update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));
|
---|
736 | }
|
---|
737 |
|
---|
738 | /*! \reimp */
|
---|
739 | void QGroupBox::mouseReleaseEvent(QMouseEvent *event)
|
---|
740 | {
|
---|
741 | if (event->button() != Qt::LeftButton) {
|
---|
742 | event->ignore();
|
---|
743 | return;
|
---|
744 | }
|
---|
745 |
|
---|
746 | Q_D(QGroupBox);
|
---|
747 | QStyleOptionGroupBox box;
|
---|
748 | initStyleOption(&box);
|
---|
749 | QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
|
---|
750 | event->pos(), this);
|
---|
751 | bool toggle = d->checkable && (released == QStyle::SC_GroupBoxLabel
|
---|
752 | || released == QStyle::SC_GroupBoxCheckBox);
|
---|
753 | d->pressedControl = QStyle::SC_None;
|
---|
754 | d->overCheckBox = false;
|
---|
755 | if (toggle)
|
---|
756 | d->click();
|
---|
757 | else if (d->checkable)
|
---|
758 | update(style()->subControlRect(QStyle::CC_GroupBox, &box, QStyle::SC_GroupBoxCheckBox, this));
|
---|
759 | }
|
---|
760 |
|
---|
761 | #ifdef QT3_SUPPORT
|
---|
762 | /*!
|
---|
763 | Use one of the constructors that doesn't take the \a name
|
---|
764 | argument and then use setObjectName() instead.
|
---|
765 | */
|
---|
766 | QGroupBox::QGroupBox(QWidget *parent, const char *name)
|
---|
767 | : QWidget(*new QGroupBoxPrivate, parent, 0)
|
---|
768 | {
|
---|
769 | Q_D(QGroupBox);
|
---|
770 | setObjectName(QString::fromAscii(name));
|
---|
771 | d->init();
|
---|
772 | }
|
---|
773 |
|
---|
774 | /*!
|
---|
775 | Use one of the constructors that doesn't take the \a name
|
---|
776 | argument and then use setObjectName() instead.
|
---|
777 | */
|
---|
778 | QGroupBox::QGroupBox(const QString &title, QWidget *parent, const char *name)
|
---|
779 | : QWidget(*new QGroupBoxPrivate, parent, 0)
|
---|
780 | {
|
---|
781 | Q_D(QGroupBox);
|
---|
782 | setObjectName(QString::fromAscii(name));
|
---|
783 | d->init();
|
---|
784 | setTitle(title);
|
---|
785 | }
|
---|
786 | #endif // QT3_SUPPORT
|
---|
787 |
|
---|
788 | QT_END_NAMESPACE
|
---|
789 |
|
---|
790 | #include "moc_qgroupbox.cpp"
|
---|
791 |
|
---|
792 | #endif //QT_NO_GROUPBOX
|
---|