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