[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[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 [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qapplication.h"
|
---|
| 43 | #include "qbitmap.h"
|
---|
| 44 | #include "qdesktopwidget.h"
|
---|
| 45 | #include "qdialog.h"
|
---|
| 46 | #include <private/qdialog_p.h>
|
---|
| 47 | #include "qdrawutil.h"
|
---|
| 48 | #include "qevent.h"
|
---|
| 49 | #include "qfontmetrics.h"
|
---|
| 50 | #include "qmenu.h"
|
---|
| 51 | #include "qstylepainter.h"
|
---|
| 52 | #include "qpixmap.h"
|
---|
| 53 | #include "qpointer.h"
|
---|
| 54 | #include "qpushbutton.h"
|
---|
| 55 | #include "qstyle.h"
|
---|
| 56 | #include "qstyleoption.h"
|
---|
| 57 | #include "qtoolbar.h"
|
---|
| 58 | #include "qdebug.h"
|
---|
| 59 | #include "qlayoutitem.h"
|
---|
| 60 | #include "qdialogbuttonbox.h"
|
---|
[846] | 61 | #ifdef Q_WS_MAC
|
---|
| 62 | #include "qmacstyle_mac.h"
|
---|
| 63 | #include "private/qmacstyle_mac_p.h"
|
---|
| 64 | #endif // Q_WS_MAC
|
---|
[2] | 65 |
|
---|
| 66 | #ifndef QT_NO_ACCESSIBILITY
|
---|
| 67 | #include "qaccessible.h"
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
[561] | 70 | #include "private/qmenu_p.h"
|
---|
[2] | 71 | #include "private/qpushbutton_p.h"
|
---|
| 72 |
|
---|
| 73 | QT_BEGIN_NAMESPACE
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /*!
|
---|
| 77 | \class QPushButton
|
---|
| 78 | \brief The QPushButton widget provides a command button.
|
---|
| 79 |
|
---|
| 80 | \ingroup basicwidgets
|
---|
| 81 |
|
---|
[561] | 82 |
|
---|
[2] | 83 | The push button, or command button, is perhaps the most commonly
|
---|
| 84 | used widget in any graphical user interface. Push (click) a button
|
---|
| 85 | to command the computer to perform some action, or to answer a
|
---|
| 86 | question. Typical buttons are OK, Apply, Cancel, Close, Yes, No
|
---|
| 87 | and Help.
|
---|
| 88 |
|
---|
| 89 | A command button is rectangular and typically displays a text
|
---|
| 90 | label describing its action. A shortcut key can be specified by
|
---|
| 91 | preceding the preferred character with an ampersand in the
|
---|
| 92 | text. For example:
|
---|
| 93 |
|
---|
| 94 | \snippet doc/src/snippets/code/src_gui_widgets_qpushbutton.cpp 0
|
---|
| 95 |
|
---|
| 96 | In this example the shortcut is \e{Alt+D}. See the \l
|
---|
| 97 | {QShortcut#mnemonic}{QShortcut} documentation for details (to
|
---|
| 98 | display an actual ampersand, use '&&').
|
---|
| 99 |
|
---|
| 100 | Push buttons display a textual label, and optionally a small
|
---|
| 101 | icon. These can be set using the constructors and changed later
|
---|
| 102 | using setText() and setIcon(). If the button is disabled the
|
---|
| 103 | appearance of the text and icon will be manipulated with respect
|
---|
| 104 | to the GUI style to make the button look "disabled".
|
---|
| 105 |
|
---|
| 106 | A push button emits the signal clicked() when it is activated by
|
---|
| 107 | the mouse, the Spacebar or by a keyboard shortcut. Connect to
|
---|
| 108 | this signal to perform the button's action. Push buttons also
|
---|
| 109 | provide less commonly used signals, for example, pressed() and
|
---|
| 110 | released().
|
---|
| 111 |
|
---|
| 112 | Command buttons in dialogs are by default auto-default buttons,
|
---|
| 113 | i.e. they become the default push button automatically when they
|
---|
| 114 | receive the keyboard input focus. A default button is a push
|
---|
| 115 | button that is activated when the user presses the Enter or Return
|
---|
| 116 | key in a dialog. You can change this with setAutoDefault(). Note
|
---|
| 117 | that auto-default buttons reserve a little extra space which is
|
---|
| 118 | necessary to draw a default-button indicator. If you do not want
|
---|
| 119 | this space around your buttons, call setAutoDefault(false).
|
---|
| 120 |
|
---|
| 121 | Being so central, the button widget has grown to accommodate a
|
---|
| 122 | great many variations in the past decade. The Microsoft style
|
---|
| 123 | guide now shows about ten different states of Windows push buttons
|
---|
| 124 | and the text implies that there are dozens more when all the
|
---|
| 125 | combinations of features are taken into consideration.
|
---|
| 126 |
|
---|
| 127 | The most important modes or states are:
|
---|
| 128 | \list
|
---|
| 129 | \i Available or not (grayed out, disabled).
|
---|
| 130 | \i Standard push button, toggling push button or menu button.
|
---|
| 131 | \i On or off (only for toggling push buttons).
|
---|
| 132 | \i Default or normal. The default button in a dialog can generally
|
---|
| 133 | be "clicked" using the Enter or Return key.
|
---|
| 134 | \i Auto-repeat or not.
|
---|
| 135 | \i Pressed down or not.
|
---|
| 136 | \endlist
|
---|
| 137 |
|
---|
| 138 | As a general rule, use a push button when the application or
|
---|
| 139 | dialog window performs an action when the user clicks on it (such
|
---|
| 140 | as Apply, Cancel, Close and Help) \e and when the widget is
|
---|
| 141 | supposed to have a wide, rectangular shape with a text label.
|
---|
| 142 | Small, typically square buttons that change the state of the
|
---|
| 143 | window rather than performing an action (such as the buttons in
|
---|
| 144 | the top-right corner of the QFileDialog) are not command buttons,
|
---|
| 145 | but tool buttons. Qt provides a special class (QToolButton) for
|
---|
| 146 | these buttons.
|
---|
| 147 |
|
---|
| 148 | If you need toggle behavior (see setCheckable()) or a button
|
---|
| 149 | that auto-repeats the activation signal when being pushed down
|
---|
| 150 | like the arrows in a scroll bar (see setAutoRepeat()), a command
|
---|
| 151 | button is probably not what you want. When in doubt, use a tool
|
---|
| 152 | button.
|
---|
| 153 |
|
---|
| 154 | A variation of a command button is a menu button. These provide
|
---|
| 155 | not just one command, but several, since when they are clicked
|
---|
| 156 | they pop up a menu of options. Use the method setMenu() to
|
---|
| 157 | associate a popup menu with a push button.
|
---|
| 158 |
|
---|
| 159 | Other classes of buttons are option buttons (see QRadioButton) and
|
---|
| 160 | check boxes (see QCheckBox).
|
---|
| 161 |
|
---|
| 162 | \table 100%
|
---|
| 163 | \row \o \inlineimage macintosh-pushbutton.png Screenshot of a Macintosh style push button
|
---|
| 164 | \o A push button shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.
|
---|
| 165 |
|
---|
| 166 | Note that when a button's width becomes smaller than 50 or
|
---|
| 167 | its height becomes smaller than 30, the button's corners are
|
---|
| 168 | changed from round to square. Use the setMinimumSize()
|
---|
| 169 | function to prevent this behavior.
|
---|
| 170 |
|
---|
| 171 | \row \o \inlineimage windowsxp-pushbutton.png Screenshot of a Windows XP style push button
|
---|
| 172 | \o A push button shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.
|
---|
| 173 | \row \o \inlineimage plastique-pushbutton.png Screenshot of a Plastique style push button
|
---|
| 174 | \o A push button shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.
|
---|
| 175 | \endtable
|
---|
| 176 |
|
---|
| 177 | In Qt, the QAbstractButton base class provides most of the modes
|
---|
| 178 | and other API, and QPushButton provides GUI logic.
|
---|
| 179 | See QAbstractButton for more information about the API.
|
---|
| 180 |
|
---|
| 181 | \sa QToolButton, QRadioButton, QCheckBox, {fowler}{GUI Design Handbook: Push Button}
|
---|
| 182 | */
|
---|
| 183 |
|
---|
| 184 | /*!
|
---|
| 185 | \property QPushButton::autoDefault
|
---|
| 186 | \brief whether the push button is an auto default button
|
---|
| 187 |
|
---|
| 188 | If this property is set to true then the push button is an auto
|
---|
| 189 | default button.
|
---|
| 190 |
|
---|
| 191 | In some GUI styles a default button is drawn with an extra frame
|
---|
| 192 | around it, up to 3 pixels or more. Qt automatically keeps this
|
---|
| 193 | space free around auto-default buttons, i.e. auto-default buttons
|
---|
| 194 | may have a slightly larger size hint.
|
---|
| 195 |
|
---|
| 196 | This property's default is true for buttons that have a QDialog
|
---|
| 197 | parent; otherwise it defaults to false.
|
---|
| 198 |
|
---|
| 199 | See the \l default property for details of how \l default and
|
---|
| 200 | auto-default interact.
|
---|
| 201 | */
|
---|
| 202 |
|
---|
| 203 | /*!
|
---|
| 204 | \property QPushButton::default
|
---|
| 205 | \brief whether the push button is the default button
|
---|
| 206 |
|
---|
| 207 | Default and autodefault buttons decide what happens when the user
|
---|
| 208 | presses enter in a dialog.
|
---|
| 209 |
|
---|
| 210 | A button with this property set to true (i.e., the dialog's
|
---|
| 211 | \e default button,) will automatically be pressed when the user presses enter,
|
---|
| 212 | with one exception: if an \a autoDefault button currently has focus, the autoDefault
|
---|
| 213 | button is pressed. When the dialog has \l autoDefault buttons but no default button,
|
---|
| 214 | pressing enter will press either the \l autoDefault button that currently has focus, or if no
|
---|
| 215 | button has focus, the next \l autoDefault button in the focus chain.
|
---|
| 216 |
|
---|
| 217 | In a dialog, only one push button at a time can be the default
|
---|
| 218 | button. This button is then displayed with an additional frame
|
---|
| 219 | (depending on the GUI style).
|
---|
| 220 |
|
---|
| 221 | The default button behavior is provided only in dialogs. Buttons
|
---|
| 222 | can always be clicked from the keyboard by pressing Spacebar when
|
---|
| 223 | the button has focus.
|
---|
| 224 |
|
---|
| 225 | If the default property is set to false on the current default button
|
---|
| 226 | while the dialog is visible, a new default will automatically be
|
---|
| 227 | assigned the next time a pushbutton in the dialog receives focus.
|
---|
| 228 |
|
---|
| 229 | This property's default is false.
|
---|
| 230 | */
|
---|
| 231 |
|
---|
| 232 | /*!
|
---|
| 233 | \property QPushButton::flat
|
---|
| 234 | \brief whether the button border is raised
|
---|
| 235 |
|
---|
| 236 | This property's default is false. If this property is set, most
|
---|
| 237 | styles will not paint the button background unless the button is
|
---|
| 238 | being pressed. setAutoFillBackground() can be used to ensure that
|
---|
| 239 | the background is filled using the QPalette::Button brush.
|
---|
| 240 | */
|
---|
| 241 |
|
---|
| 242 | /*!
|
---|
| 243 | Constructs a push button with no text and a \a parent.
|
---|
| 244 | */
|
---|
| 245 |
|
---|
| 246 | QPushButton::QPushButton(QWidget *parent)
|
---|
| 247 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 248 | {
|
---|
| 249 | Q_D(QPushButton);
|
---|
| 250 | d->init();
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /*!
|
---|
| 254 | Constructs a push button with the parent \a parent and the text \a
|
---|
| 255 | text.
|
---|
| 256 | */
|
---|
| 257 |
|
---|
| 258 | QPushButton::QPushButton(const QString &text, QWidget *parent)
|
---|
| 259 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 260 | {
|
---|
| 261 | Q_D(QPushButton);
|
---|
| 262 | setText(text);
|
---|
| 263 | d->init();
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | /*!
|
---|
| 268 | Constructs a push button with an \a icon and a \a text, and a \a parent.
|
---|
| 269 |
|
---|
| 270 | Note that you can also pass a QPixmap object as an icon (thanks to
|
---|
| 271 | the implicit type conversion provided by C++).
|
---|
| 272 |
|
---|
| 273 | */
|
---|
| 274 | QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent)
|
---|
| 275 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 276 | {
|
---|
| 277 | Q_D(QPushButton);
|
---|
| 278 | setText(text);
|
---|
| 279 | setIcon(icon);
|
---|
| 280 | d->init();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*! \internal
|
---|
| 284 | */
|
---|
| 285 | QPushButton::QPushButton(QPushButtonPrivate &dd, QWidget *parent)
|
---|
| 286 | : QAbstractButton(dd, parent)
|
---|
| 287 | {
|
---|
| 288 | Q_D(QPushButton);
|
---|
| 289 | d->init();
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*!
|
---|
| 293 | Destroys the push button.
|
---|
| 294 | */
|
---|
| 295 | QPushButton::~QPushButton()
|
---|
| 296 | {
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | QDialog *QPushButtonPrivate::dialogParent() const
|
---|
| 300 | {
|
---|
| 301 | Q_Q(const QPushButton);
|
---|
| 302 | const QWidget *p = q;
|
---|
| 303 | while (p && !p->isWindow()) {
|
---|
| 304 | p = p->parentWidget();
|
---|
| 305 | if (const QDialog *dialog = qobject_cast<const QDialog *>(p))
|
---|
| 306 | return const_cast<QDialog *>(dialog);
|
---|
| 307 | }
|
---|
| 308 | return 0;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /*!
|
---|
| 312 | Initialize \a option with the values from this QPushButton. This method is useful
|
---|
| 313 | for subclasses when they need a QStyleOptionButton, but don't want to fill
|
---|
| 314 | in all the information themselves.
|
---|
| 315 |
|
---|
| 316 | \sa QStyleOption::initFrom()
|
---|
| 317 | */
|
---|
| 318 | void QPushButton::initStyleOption(QStyleOptionButton *option) const
|
---|
| 319 | {
|
---|
| 320 | if (!option)
|
---|
| 321 | return;
|
---|
| 322 |
|
---|
| 323 | Q_D(const QPushButton);
|
---|
| 324 | option->initFrom(this);
|
---|
| 325 | option->features = QStyleOptionButton::None;
|
---|
| 326 | if (d->flat)
|
---|
| 327 | option->features |= QStyleOptionButton::Flat;
|
---|
| 328 | #ifndef QT_NO_MENU
|
---|
| 329 | if (d->menu)
|
---|
| 330 | option->features |= QStyleOptionButton::HasMenu;
|
---|
| 331 | #endif
|
---|
| 332 | if (autoDefault() || d->defaultButton)
|
---|
| 333 | option->features |= QStyleOptionButton::AutoDefaultButton;
|
---|
| 334 | if (d->defaultButton)
|
---|
| 335 | option->features |= QStyleOptionButton::DefaultButton;
|
---|
| 336 | if (d->down || d->menuOpen)
|
---|
| 337 | option->state |= QStyle::State_Sunken;
|
---|
| 338 | if (d->checked)
|
---|
| 339 | option->state |= QStyle::State_On;
|
---|
| 340 | if (!d->flat && !d->down)
|
---|
| 341 | option->state |= QStyle::State_Raised;
|
---|
| 342 | option->text = d->text;
|
---|
| 343 | option->icon = d->icon;
|
---|
| 344 | option->iconSize = iconSize();
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | void QPushButton::setAutoDefault(bool enable)
|
---|
| 348 | {
|
---|
| 349 | Q_D(QPushButton);
|
---|
| 350 | uint state = enable ? QPushButtonPrivate::On : QPushButtonPrivate::Off;
|
---|
| 351 | if (d->autoDefault != QPushButtonPrivate::Auto && d->autoDefault == state)
|
---|
| 352 | return;
|
---|
| 353 | d->autoDefault = state;
|
---|
| 354 | d->sizeHint = QSize();
|
---|
| 355 | update();
|
---|
| 356 | updateGeometry();
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | bool QPushButton::autoDefault() const
|
---|
| 360 | {
|
---|
| 361 | Q_D(const QPushButton);
|
---|
| 362 | if(d->autoDefault == QPushButtonPrivate::Auto)
|
---|
| 363 | return ( d->dialogParent() != 0 );
|
---|
| 364 | return d->autoDefault;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | void QPushButton::setDefault(bool enable)
|
---|
| 368 | {
|
---|
| 369 | Q_D(QPushButton);
|
---|
| 370 | if (d->defaultButton == enable)
|
---|
| 371 | return;
|
---|
| 372 | d->defaultButton = enable;
|
---|
| 373 | if (d->defaultButton) {
|
---|
| 374 | if (QDialog *dlg = d->dialogParent())
|
---|
| 375 | dlg->d_func()->setMainDefault(this);
|
---|
| 376 | }
|
---|
| 377 | update();
|
---|
| 378 | #ifndef QT_NO_ACCESSIBILITY
|
---|
| 379 | QAccessible::updateAccessibility(this, 0, QAccessible::StateChanged);
|
---|
| 380 | #endif
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | bool QPushButton::isDefault() const
|
---|
| 384 | {
|
---|
| 385 | Q_D(const QPushButton);
|
---|
| 386 | return d->defaultButton;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /*!
|
---|
| 390 | \reimp
|
---|
| 391 | */
|
---|
| 392 | QSize QPushButton::sizeHint() const
|
---|
| 393 | {
|
---|
| 394 | Q_D(const QPushButton);
|
---|
[561] | 395 | if (d->sizeHint.isValid() && d->lastAutoDefault == autoDefault())
|
---|
[2] | 396 | return d->sizeHint;
|
---|
[561] | 397 | d->lastAutoDefault = autoDefault();
|
---|
[2] | 398 | ensurePolished();
|
---|
| 399 |
|
---|
| 400 | int w = 0, h = 0;
|
---|
| 401 |
|
---|
| 402 | QStyleOptionButton opt;
|
---|
| 403 | initStyleOption(&opt);
|
---|
| 404 |
|
---|
| 405 | // calculate contents size...
|
---|
| 406 | #ifndef QT_NO_ICON
|
---|
| 407 |
|
---|
| 408 | bool showButtonBoxIcons = qobject_cast<QDialogButtonBox*>(parentWidget())
|
---|
| 409 | && style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons);
|
---|
| 410 |
|
---|
| 411 | if (!icon().isNull() || showButtonBoxIcons) {
|
---|
| 412 | int ih = opt.iconSize.height();
|
---|
| 413 | int iw = opt.iconSize.width() + 4;
|
---|
| 414 | w += iw;
|
---|
| 415 | h = qMax(h, ih);
|
---|
| 416 | }
|
---|
| 417 | #endif
|
---|
| 418 | QString s(text());
|
---|
| 419 | bool empty = s.isEmpty();
|
---|
| 420 | if (empty)
|
---|
| 421 | s = QString::fromLatin1("XXXX");
|
---|
| 422 | QFontMetrics fm = fontMetrics();
|
---|
| 423 | QSize sz = fm.size(Qt::TextShowMnemonic, s);
|
---|
| 424 | if(!empty || !w)
|
---|
| 425 | w += sz.width();
|
---|
| 426 | if(!empty || !h)
|
---|
| 427 | h = qMax(h, sz.height());
|
---|
| 428 | opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
|
---|
| 429 | #ifndef QT_NO_MENU
|
---|
| 430 | if (menu())
|
---|
| 431 | w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
|
---|
| 432 | #endif
|
---|
| 433 | d->sizeHint = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
|
---|
| 434 | expandedTo(QApplication::globalStrut()));
|
---|
| 435 | return d->sizeHint;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | /*!
|
---|
| 439 | \reimp
|
---|
| 440 | */
|
---|
| 441 | QSize QPushButton::minimumSizeHint() const
|
---|
| 442 | {
|
---|
| 443 | return sizeHint();
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | /*!\reimp
|
---|
| 448 | */
|
---|
| 449 | void QPushButton::paintEvent(QPaintEvent *)
|
---|
| 450 | {
|
---|
| 451 | QStylePainter p(this);
|
---|
| 452 | QStyleOptionButton option;
|
---|
| 453 | initStyleOption(&option);
|
---|
| 454 | p.drawControl(QStyle::CE_PushButton, option);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 |
|
---|
| 458 | /*! \reimp */
|
---|
| 459 | void QPushButton::keyPressEvent(QKeyEvent *e)
|
---|
| 460 | {
|
---|
| 461 | Q_D(QPushButton);
|
---|
| 462 | switch (e->key()) {
|
---|
| 463 | case Qt::Key_Enter:
|
---|
| 464 | case Qt::Key_Return:
|
---|
| 465 | if (autoDefault() || d->defaultButton) {
|
---|
| 466 | click();
|
---|
| 467 | break;
|
---|
| 468 | }
|
---|
| 469 | // fall through
|
---|
| 470 | default:
|
---|
| 471 | QAbstractButton::keyPressEvent(e);
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | /*!
|
---|
| 476 | \reimp
|
---|
| 477 | */
|
---|
| 478 | void QPushButton::focusInEvent(QFocusEvent *e)
|
---|
| 479 | {
|
---|
| 480 | Q_D(QPushButton);
|
---|
| 481 | if (e->reason() != Qt::PopupFocusReason && autoDefault() && !d->defaultButton) {
|
---|
| 482 | d->defaultButton = true;
|
---|
| 483 | QDialog *dlg = qobject_cast<QDialog*>(window());
|
---|
| 484 | if (dlg)
|
---|
| 485 | dlg->d_func()->setDefault(this);
|
---|
| 486 | }
|
---|
| 487 | QAbstractButton::focusInEvent(e);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | /*!
|
---|
| 491 | \reimp
|
---|
| 492 | */
|
---|
| 493 | void QPushButton::focusOutEvent(QFocusEvent *e)
|
---|
| 494 | {
|
---|
| 495 | Q_D(QPushButton);
|
---|
| 496 | if (e->reason() != Qt::PopupFocusReason && autoDefault() && d->defaultButton) {
|
---|
| 497 | QDialog *dlg = qobject_cast<QDialog*>(window());
|
---|
| 498 | if (dlg)
|
---|
| 499 | dlg->d_func()->setDefault(0);
|
---|
| 500 | else
|
---|
| 501 | d->defaultButton = false;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | QAbstractButton::focusOutEvent(e);
|
---|
| 505 | #ifndef QT_NO_MENU
|
---|
| 506 | if (d->menu && d->menu->isVisible()) // restore pressed status
|
---|
| 507 | setDown(true);
|
---|
| 508 | #endif
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | #ifndef QT_NO_MENU
|
---|
| 512 | /*!
|
---|
| 513 | Associates the popup menu \a menu with this push button. This
|
---|
| 514 | turns the button into a menu button, which in some styles will
|
---|
| 515 | produce a small triangle to the right of the button's text.
|
---|
| 516 |
|
---|
| 517 | Ownership of the menu is \e not transferred to the push button.
|
---|
| 518 |
|
---|
| 519 | \table 100%
|
---|
| 520 | \row
|
---|
| 521 | \o \inlineimage plastique-pushbutton-menu.png Screenshot of a Plastique style push button with popup menu.
|
---|
| 522 | \o \inlineimage cleanlooks-pushbutton-menu.png Screenshot of a Cleanlooks style push button with popup menu.
|
---|
| 523 | \o Push buttons with popup menus shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}
|
---|
| 524 | (left) and \l{Cleanlooks Style Widget Gallery}{Cleanlooks widget style} (right).
|
---|
| 525 | \endtable
|
---|
| 526 |
|
---|
| 527 | \sa menu()
|
---|
| 528 | */
|
---|
| 529 | void QPushButton::setMenu(QMenu* menu)
|
---|
| 530 | {
|
---|
| 531 | Q_D(QPushButton);
|
---|
| 532 | if (menu == d->menu)
|
---|
| 533 | return;
|
---|
| 534 |
|
---|
| 535 | if (menu && !d->menu) {
|
---|
[561] | 536 | connect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed()), Qt::UniqueConnection);
|
---|
[2] | 537 | }
|
---|
| 538 | if (d->menu)
|
---|
| 539 | removeAction(d->menu->menuAction());
|
---|
| 540 | d->menu = menu;
|
---|
| 541 | if (d->menu)
|
---|
| 542 | addAction(d->menu->menuAction());
|
---|
| 543 |
|
---|
| 544 | d->resetLayoutItemMargins();
|
---|
| 545 | d->sizeHint = QSize();
|
---|
| 546 | update();
|
---|
| 547 | updateGeometry();
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | /*!
|
---|
| 551 | Returns the button's associated popup menu or 0 if no popup menu
|
---|
| 552 | has been set.
|
---|
| 553 |
|
---|
| 554 | \sa setMenu()
|
---|
| 555 | */
|
---|
| 556 | QMenu* QPushButton::menu() const
|
---|
| 557 | {
|
---|
| 558 | Q_D(const QPushButton);
|
---|
| 559 | return d->menu;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /*!
|
---|
| 563 | Shows (pops up) the associated popup menu. If there is no such
|
---|
| 564 | menu, this function does nothing. This function does not return
|
---|
| 565 | until the popup menu has been closed by the user.
|
---|
| 566 | */
|
---|
| 567 | void QPushButton::showMenu()
|
---|
| 568 | {
|
---|
| 569 | Q_D(QPushButton);
|
---|
| 570 | if (!d || !d->menu)
|
---|
| 571 | return;
|
---|
| 572 | setDown(true);
|
---|
| 573 | d->_q_popupPressed();
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | void QPushButtonPrivate::_q_popupPressed()
|
---|
| 577 | {
|
---|
| 578 | Q_Q(QPushButton);
|
---|
| 579 | if (!down || !menu)
|
---|
| 580 | return;
|
---|
| 581 |
|
---|
| 582 | menu->setNoReplayFor(q);
|
---|
[561] | 583 |
|
---|
| 584 | QPoint menuPos = adjustedMenuPosition();
|
---|
| 585 |
|
---|
| 586 | QPointer<QPushButton> guard(q);
|
---|
| 587 | QMenuPrivate::get(menu)->causedPopup.widget = guard;
|
---|
| 588 |
|
---|
| 589 | //Because of a delay in menu effects, we must keep track of the
|
---|
| 590 | //menu visibility to avoid flicker on button release
|
---|
| 591 | menuOpen = true;
|
---|
| 592 | menu->exec(menuPos);
|
---|
| 593 | if (guard) {
|
---|
| 594 | menuOpen = false;
|
---|
| 595 | q->setDown(false);
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | QPoint QPushButtonPrivate::adjustedMenuPosition()
|
---|
| 600 | {
|
---|
| 601 | Q_Q(QPushButton);
|
---|
| 602 |
|
---|
[2] | 603 | bool horizontal = true;
|
---|
| 604 | #if !defined(QT_NO_TOOLBAR)
|
---|
[561] | 605 | QToolBar *tb = qobject_cast<QToolBar*>(parent);
|
---|
[2] | 606 | if (tb && tb->orientation() == Qt::Vertical)
|
---|
| 607 | horizontal = false;
|
---|
| 608 | #endif
|
---|
[561] | 609 |
|
---|
[2] | 610 | QWidgetItem item(q);
|
---|
| 611 | QRect rect = item.geometry();
|
---|
| 612 | rect.setRect(rect.x() - q->x(), rect.y() - q->y(), rect.width(), rect.height());
|
---|
| 613 |
|
---|
| 614 | QSize menuSize = menu->sizeHint();
|
---|
| 615 | QPoint globalPos = q->mapToGlobal(rect.topLeft());
|
---|
| 616 | int x = globalPos.x();
|
---|
| 617 | int y = globalPos.y();
|
---|
| 618 | if (horizontal) {
|
---|
[561] | 619 | if (globalPos.y() + rect.height() + menuSize.height() <= QApplication::desktop()->availableGeometry(q).height()) {
|
---|
[2] | 620 | y += rect.height();
|
---|
| 621 | } else {
|
---|
| 622 | y -= menuSize.height();
|
---|
| 623 | }
|
---|
| 624 | if (q->layoutDirection() == Qt::RightToLeft)
|
---|
| 625 | x += rect.width() - menuSize.width();
|
---|
| 626 | } else {
|
---|
[561] | 627 | if (globalPos.x() + rect.width() + menu->sizeHint().width() <= QApplication::desktop()->availableGeometry(q).width())
|
---|
[2] | 628 | x += rect.width();
|
---|
| 629 | else
|
---|
| 630 | x -= menuSize.width();
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[561] | 633 | return QPoint(x,y);
|
---|
[2] | 634 | }
|
---|
[561] | 635 |
|
---|
[2] | 636 | #endif // QT_NO_MENU
|
---|
| 637 |
|
---|
| 638 | void QPushButtonPrivate::resetLayoutItemMargins()
|
---|
| 639 | {
|
---|
| 640 | Q_Q(QPushButton);
|
---|
| 641 | QStyleOptionButton opt;
|
---|
| 642 | q->initStyleOption(&opt);
|
---|
| 643 | setLayoutItemMargins(QStyle::SE_PushButtonLayoutItem, &opt);
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | void QPushButton::setFlat(bool flat)
|
---|
| 647 | {
|
---|
| 648 | Q_D(QPushButton);
|
---|
| 649 | if (d->flat == flat)
|
---|
| 650 | return;
|
---|
| 651 | d->flat = flat;
|
---|
| 652 | d->resetLayoutItemMargins();
|
---|
| 653 | d->sizeHint = QSize();
|
---|
| 654 | update();
|
---|
| 655 | updateGeometry();
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | bool QPushButton::isFlat() const
|
---|
| 659 | {
|
---|
| 660 | Q_D(const QPushButton);
|
---|
| 661 | return d->flat;
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | /*! \reimp */
|
---|
| 665 | bool QPushButton::event(QEvent *e)
|
---|
| 666 | {
|
---|
| 667 | Q_D(QPushButton);
|
---|
| 668 | if (e->type() == QEvent::ParentChange) {
|
---|
| 669 | if (QDialog *dialog = d->dialogParent()) {
|
---|
| 670 | if (d->defaultButton)
|
---|
| 671 | dialog->d_func()->setMainDefault(this);
|
---|
| 672 | }
|
---|
| 673 | } else if (e->type() == QEvent::StyleChange
|
---|
| 674 | #ifdef Q_WS_MAC
|
---|
| 675 | || e->type() == QEvent::MacSizeChange
|
---|
| 676 | #endif
|
---|
| 677 | ) {
|
---|
| 678 | d->resetLayoutItemMargins();
|
---|
| 679 | updateGeometry();
|
---|
[561] | 680 | } else if (e->type() == QEvent::PolishRequest) {
|
---|
| 681 | updateGeometry();
|
---|
[2] | 682 | }
|
---|
| 683 | return QAbstractButton::event(e);
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[846] | 686 | #ifdef Q_WS_MAC
|
---|
| 687 | /*! \reimp */
|
---|
| 688 | bool QPushButton::hitButton(const QPoint &pos) const
|
---|
| 689 | {
|
---|
| 690 | QStyleOptionButton opt;
|
---|
| 691 | initStyleOption(&opt);
|
---|
| 692 | if (qt_mac_buttonIsRenderedFlat(this, &opt))
|
---|
| 693 | return QAbstractButton::hitButton(pos);
|
---|
| 694 |
|
---|
| 695 | // Now that we know we are using the native style, let's proceed.
|
---|
| 696 | Q_D(const QPushButton);
|
---|
| 697 | QPushButtonPrivate *nonConst = const_cast<QPushButtonPrivate *>(d);
|
---|
| 698 | // In OSX buttons are round, which causes the hit method to be special.
|
---|
| 699 | // We cannot simply relay on detecting if something is inside the rect or not,
|
---|
| 700 | // we need to check if it is inside the "rounded area" or not. A point might
|
---|
| 701 | // be inside the rect but not inside the rounded area.
|
---|
| 702 | // Notice this method is only reimplemented for OSX.
|
---|
| 703 | return nonConst->hitButton(pos);
|
---|
| 704 | }
|
---|
| 705 |
|
---|
| 706 | bool QPushButtonPrivate::hitButton(const QPoint &pos)
|
---|
| 707 | {
|
---|
| 708 | Q_Q(QPushButton);
|
---|
| 709 | QRect roundedRect(q->rect().left() + QMacStylePrivate::PushButtonLeftOffset,
|
---|
| 710 | q->rect().top() + QMacStylePrivate::PushButtonContentPadding,
|
---|
| 711 | q->rect().width() - QMacStylePrivate::PushButtonRightOffset,
|
---|
| 712 | q->rect().height() - QMacStylePrivate::PushButtonBottomOffset);
|
---|
| 713 | return roundedRect.contains(pos);
|
---|
| 714 | }
|
---|
| 715 | #endif // Q_WS_MAC
|
---|
| 716 |
|
---|
[2] | 717 | #ifdef QT3_SUPPORT
|
---|
| 718 | /*!
|
---|
| 719 | Use one of the constructors that doesn't take the \a name
|
---|
| 720 | argument and then use setObjectName() instead.
|
---|
| 721 | */
|
---|
| 722 | QPushButton::QPushButton(QWidget *parent, const char *name)
|
---|
| 723 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 724 | {
|
---|
| 725 | Q_D(QPushButton);
|
---|
| 726 | setObjectName(QString::fromAscii(name));
|
---|
| 727 | d->init();
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | /*!
|
---|
| 731 | Use one of the constructors that doesn't take the \a name
|
---|
| 732 | argument and then use setObjectName() instead.
|
---|
| 733 | */
|
---|
| 734 | QPushButton::QPushButton(const QString &text, QWidget *parent, const char *name)
|
---|
| 735 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 736 | {
|
---|
| 737 | Q_D(QPushButton);
|
---|
| 738 | setObjectName(QString::fromAscii(name));
|
---|
| 739 | setText(text);
|
---|
| 740 | d->init();
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | /*!
|
---|
| 744 | Use one of the constructors that doesn't take the \a name
|
---|
| 745 | argument and then use setObjectName() instead.
|
---|
| 746 | */
|
---|
| 747 | QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent, const char *name)
|
---|
| 748 | : QAbstractButton(*new QPushButtonPrivate, parent)
|
---|
| 749 | {
|
---|
| 750 | Q_D(QPushButton);
|
---|
| 751 | setObjectName(QString::fromAscii(name));
|
---|
| 752 | setText(text);
|
---|
| 753 | setIcon(icon);
|
---|
| 754 | d->init();
|
---|
| 755 | }
|
---|
| 756 | #endif
|
---|
| 757 |
|
---|
| 758 | /*!
|
---|
| 759 | \fn void QPushButton::openPopup()
|
---|
| 760 |
|
---|
| 761 | Use showMenu() instead.
|
---|
| 762 | */
|
---|
| 763 |
|
---|
| 764 | /*!
|
---|
| 765 | \fn bool QPushButton::isMenuButton() const
|
---|
| 766 |
|
---|
| 767 | Use menu() != 0 instead.
|
---|
| 768 | */
|
---|
| 769 |
|
---|
| 770 | /*!
|
---|
| 771 | \fn void QPushButton::setPopup(QMenu* popup)
|
---|
| 772 |
|
---|
| 773 | Use setMenu() instead.
|
---|
| 774 | */
|
---|
| 775 |
|
---|
| 776 | /*!
|
---|
| 777 | \fn QMenu* QPushButton::popup() const
|
---|
| 778 |
|
---|
| 779 | Use menu() instead.
|
---|
| 780 | */
|
---|
| 781 |
|
---|
| 782 | QT_END_NAMESPACE
|
---|
| 783 |
|
---|
| 784 | #include "moc_qpushbutton.cpp"
|
---|