📄 qtoolbutton.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtoolbutton.h"#ifndef QT_NO_TOOLBUTTON#include <qapplication.h>#include <qdesktopwidget.h>#include <qdrawutil.h>#include <qevent.h>#include <qicon.h>#include <qmenu.h>#include <qpainter.h>#include <qpointer.h>#include <qstyle.h>#include <qstyleoption.h>#include <qtooltip.h>#include <qmainwindow.h>#include <qtoolbar.h>#include <qvariant.h>#include <qstylepainter.h>#include <private/qabstractbutton_p.h>#include <private/qaction_p.h>#include <private/qmenu_p.h>class QToolButtonPrivate : public QAbstractButtonPrivate{ Q_DECLARE_PUBLIC(QToolButton)public: void init();#ifndef QT_NO_MENU void _q_buttonPressed(); void popupTimerDone(); void _q_updateButtonDown();#endif void _q_actionTriggered(); QPointer<QAction> menuAction; //the menu set by the user (setMenu) QBasicTimer popupTimer; int delay; Qt::ArrowType arrowType; Qt::ToolButtonStyle toolButtonStyle; QToolButton::ToolButtonPopupMode popupMode; enum { NoButtonPressed=0, MenuButtonPressed=1, ToolButtonPressed=2 }; uint buttonPressed : 2; uint menuButtonDown : 1; uint autoRaise : 1; uint repeat : 1; QAction *defaultAction;#ifndef QT_NO_MENU bool hasMenu() const;#endif#ifdef QT3_SUPPORT bool userDefinedPopupDelay;#endif};#ifndef QT_NO_MENUbool QToolButtonPrivate::hasMenu() const{ Q_Q(const QToolButton); return ((defaultAction && defaultAction->menu()) || (menuAction && menuAction->menu()) || q->actions().size() > (defaultAction ? 1 : 0));}#endif/*! \class QToolButton qtoolbutton.h \brief The QToolButton class provides a quick-access button to commands or options, usually used inside a QToolBar. \ingroup basicwidgets \mainclass A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead. Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts. One classic use of a tool button is to select tools; for example, the "pen" tool in a drawing program. This would be implemented by using a QToolButton as a toggle button (see setToggleButton()). QToolButton supports auto-raising. In auto-raise mode, the button draws a 3D frame only when the mouse points at it. The feature is automatically turned on when a button is used inside a QToolBar. Change it with setAutoRaise(). A tool button's icon is set as QIcon. This makes it possible to specify different pixmaps for the disabled and active state. The disabled pixmap is used when the button's functionality is not available. The active pixmap is displayed when the button is auto-raised because the mouse pointer is hovering over it. The button's look and dimension is adjustable with setToolButtonStyle() and setIconSize(). When used inside a QToolBar in a QMainWindow, the button automatically adjusts to QMainWindow's settings (see QMainWindow::setToolButtonStyle() and QMainWindow::setIconSize()). Instead of an icon, a tool button can also display an arrow symbol, specified with \l arrowType. A tool button can offer additional choices in a popup menu. The popup menu can be set using setMenu(). Use setPopupMode() to configure the different modes available for tool buttons with a menu set. The default mode is DelayedPopupMode which is sometimes used with the "Back" button in a web browser. After pressing and holding the button down for a while, a menu pops up showing a list of possible pages to jump to. The default delay is 600 ms; you can adjust it with setPopupDelay(). \table 100% \row \o \inlineimage assistant-toolbar1.png Qt Assistant's toolbar with tool buttons \row \o Qt Assistant's toolbar contains tool buttons that are associated with actions used in other parts of the main window. \endtable \sa QPushButton, QToolBar, QMainWindow, QAction, {fowler}{GUI Design Handbook: Push Button}*//*! \fn void QToolButton::triggered(QAction *action) This signal is emitted when the given \a action is triggered. The action may also be associated with other parts of the user interface, such as menu items and keyboard shortcuts. Sharing actions in this way helps make the user interface more consistent and is often less work to implement.*//*! Constructs an empty tool button with parent \a parent.*/QToolButton::QToolButton(QWidget * parent) : QAbstractButton(*new QToolButtonPrivate, parent){ Q_D(QToolButton); d->init();}#ifdef QT3_SUPPORT/*! Constructs an empty tool button called \a name, with parent \a parent.*/QToolButton::QToolButton(QWidget * parent, const char *name) : QAbstractButton(*new QToolButtonPrivate, parent){ Q_D(QToolButton); setObjectName(QString::fromAscii(name)); d->init();}/*! Constructs a tool button called \a name, that is a child of \a parent. The tool button will display the given \a icon, with its text label and tool tip set to \a textLabel and its status bar message set to \a statusTip. It will be connected to the \a slot in object \a receiver.*/QToolButton::QToolButton(const QIcon& icon, const QString &textLabel, const QString& statusTip, QObject * receiver, const char *slot, QWidget * parent, const char *name) : QAbstractButton(*new QToolButtonPrivate, parent){ Q_D(QToolButton); setObjectName(QString::fromAscii(name)); d->init(); setIcon(icon); setText(textLabel); if (receiver && slot) connect(this, SIGNAL(clicked()), receiver, slot);#ifndef QT_NO_TOOLTIP if (!textLabel.isEmpty()) setToolTip(textLabel);#endif#ifndef QT_NO_STATUSTIP if (!statusTip.isEmpty()) setStatusTip(statusTip);#else Q_UNUSED(statusTip);#endif}/*! Constructs a tool button as an arrow button. The Qt::ArrowType \a type defines the arrow direction. Possible values are Qt::LeftArrow, Qt::RightArrow, Qt::UpArrow, and Qt::DownArrow. An arrow button has auto-repeat turned on by default. The \a parent and \a name arguments are sent to the QWidget constructor.*/QToolButton::QToolButton(Qt::ArrowType type, QWidget *parent, const char *name) : QAbstractButton(*new QToolButtonPrivate, parent){ Q_D(QToolButton); setObjectName(QString::fromAscii(name)); d->init(); setAutoRepeat(true); d->arrowType = type;}#endif/* Set-up code common to all the constructors */void QToolButtonPrivate::init(){ Q_Q(QToolButton); delay = q->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, q);#ifdef QT3_SUPPORT userDefinedPopupDelay = false;#endif defaultAction = 0;#ifndef QT_NO_TOOLBAR if (qobject_cast<QToolBar*>(q->parentWidget())) autoRaise = true; else#endif autoRaise = false; arrowType = Qt::NoArrow; menuButtonDown = false; popupMode = QToolButton::DelayedPopup; buttonPressed = QToolButtonPrivate::NoButtonPressed; toolButtonStyle = Qt::ToolButtonIconOnly; q->setFocusPolicy(Qt::TabFocus); q->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, QSizePolicy::ToolButton)); QObject::connect(q, SIGNAL(pressed()), q, SLOT(_q_buttonPressed())); setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem);}/*! Initialize \a option with the values from this QToolButton. This method is useful for subclasses when they need a QStyleOptionToolButton, but don't want to fill in all the information themselves. \sa QStyleOption::initFrom()*/void QToolButton::initStyleOption(QStyleOptionToolButton *option) const{ if (!option) return; Q_D(const QToolButton); option->initFrom(this); bool forceNoText = false;#ifndef QT_NO_TOOLBAR if (parentWidget()) {#ifdef QT3_SUPPORT if (parentWidget()->inherits("Q3ToolBar")) { int iconSize = style()->pixelMetric(QStyle::PM_ToolBarIconSize, option, this); option->iconSize = d->icon.actualSize(QSize(iconSize, iconSize)); forceNoText = d->toolButtonStyle == Qt::ToolButtonIconOnly; } else#endif if (QToolBar *toolBar = qobject_cast<QToolBar *>(parentWidget())) { option->iconSize = toolBar->iconSize(); } else { option->iconSize = iconSize(); } }#endif // QT_NO_TOOLBAR if (!forceNoText) option->text = d->text; option->icon = d->icon; option->arrowType = d->arrowType; if (d->down) option->state |= QStyle::State_Sunken; if (d->checked) option->state |= QStyle::State_On; if (d->autoRaise) option->state |= QStyle::State_AutoRaise; if (!d->checked && !d->down) option->state |= QStyle::State_Raised; option->subControls = QStyle::SC_ToolButton; option->activeSubControls = QStyle::SC_None;// if (d->down && !d->menuButtonDown)// option->activeSubControls |= QStyle::SC_ToolButton; option->features = QStyleOptionToolButton::None; if (d->popupMode == QToolButton::MenuButtonPopup) { option->subControls |= QStyle::SC_ToolButtonMenu; option->features |= QStyleOptionToolButton::MenuButtonPopup; if (d->menuButtonDown || d->down) { option->state |= QStyle::State_MouseOver; option->activeSubControls |= QStyle::SC_ToolButtonMenu; } } else { if (d->menuButtonDown) option->state |= QStyle::State_Sunken; } if (d->arrowType != Qt::NoArrow) option->features |= QStyleOptionToolButton::Arrow; if (d->popupMode == QToolButton::DelayedPopup) option->features |= QStyleOptionToolButton::PopupDelay;#ifndef QT_NO_MENU if (d->hasMenu()) option->features |= QStyleOptionToolButton::HasMenu;#endif option->toolButtonStyle = d->toolButtonStyle; if (d->icon.isNull() && d->arrowType == Qt::NoArrow && !forceNoText) { if (!d->text.isEmpty()) option->toolButtonStyle = Qt::ToolButtonTextOnly; else if (option->toolButtonStyle != Qt::ToolButtonTextOnly) option->toolButtonStyle = Qt::ToolButtonIconOnly; } else { if (d->text.isEmpty() && option->toolButtonStyle != Qt::ToolButtonIconOnly) option->toolButtonStyle = Qt::ToolButtonIconOnly; } option->pos = pos(); option->font = font();}/*! Destroys the object and frees any allocated resources.*/QToolButton::~QToolButton(){}/*! \reimp*/QSize QToolButton::sizeHint() const{ Q_D(const QToolButton); if (d->sizeHint.isValid()) return d->sizeHint; ensurePolished();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -