📄 qtoolbar.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 "qtoolbar.h"#ifndef QT_NO_TOOLBAR#include <qapplication.h>#include <qcombobox.h>#include <qevent.h>#include <qlayout.h>#include <qmainwindow.h>#include <qmenu.h>#include <qmenubar.h>#include <qrubberband.h>#include <qsignalmapper.h>#include <qstylepainter.h>#include <qtoolbutton.h>#include <qwidgetaction.h>#include <qtimer.h>#include <private/qwidgetaction_p.h>#ifdef Q_WS_MAC#include <private/qt_mac_p.h>#endif#include <private/qmainwindowlayout_p.h>#include "qtoolbar_p.h"#include "qtoolbarseparator_p.h"#include "qtoolbarlayout_p.h"#include "qdebug.h"/******************************************************************************** QToolBarPrivate*/void QToolBarPrivate::init(){ Q_Q(QToolBar); waitForPopupTimer = new QTimer(q); waitForPopupTimer->setSingleShot(false); waitForPopupTimer->setInterval(500); QObject::connect(waitForPopupTimer, SIGNAL(timeout()), q, SLOT(_q_waitForPopup())); floatable = true; movable = true; q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); q->setBackgroundRole(QPalette::Button); q->setAttribute(Qt::WA_Hover); QStyle *style = q->style(); int e = style->pixelMetric(QStyle::PM_ToolBarIconSize, 0, q); iconSize = QSize(e, e); layout = new QToolBarLayout(q); layout->updateMarginAndSpacing();#ifdef Q_WS_MAC if (q->parentWidget() && q->parentWidget()->isWindow()) { // Make sure that the window has the "toolbar" button. reinterpret_cast<QToolBar *>(q->parentWidget())->d_func()->createWinId(); // Please let me create your winId... extern WindowPtr qt_mac_window_for(const QWidget *); // qwidget_mac.cpp ChangeWindowAttributes(qt_mac_window_for(q->parentWidget()), kWindowToolbarButtonAttribute, kWindowNoAttributes); }#endif toggleViewAction = new QAction(q); toggleViewAction->setCheckable(true); QObject::connect(toggleViewAction, SIGNAL(triggered(bool)), q, SLOT(_q_toggleView(bool)));}void QToolBarPrivate::_q_toggleView(bool b){ Q_Q(QToolBar); if (b == q->isHidden()) { if (b) q->show(); else q->close(); }}void QToolBarPrivate::_q_updateIconSize(const QSize &sz){ Q_Q(QToolBar); if (!explicitIconSize) { // iconSize not explicitly set q->setIconSize(sz); explicitIconSize = false; }}void QToolBarPrivate::_q_updateToolButtonStyle(Qt::ToolButtonStyle style){ Q_Q(QToolBar); if (!explicitToolButtonStyle) { q->setToolButtonStyle(style); explicitToolButtonStyle = false; }}void QToolBarPrivate::setWindowState(bool floating, bool unplug, const QRect &rect){ Q_Q(QToolBar); bool visible = !q->isHidden(); q->hide(); Qt::WindowFlags flags = floating ? Qt::Tool : Qt::Widget; flags |= Qt::FramelessWindowHint; if (unplug) flags |= Qt::X11BypassWindowManagerHint; q->setWindowFlags(flags); if (!rect.isNull()) q->setGeometry(rect); if (visible) q->show();}void QToolBarPrivate::initDrag(const QPoint &pos){ Q_Q(QToolBar); if (state != 0) return; QMainWindow *win = qobject_cast<QMainWindow*>(q->parentWidget()); Q_ASSERT(win != 0); QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout()); Q_ASSERT(layout != 0); if (layout->pluggingWidget != 0) // the main window is animating a docking operation return; state = new DragState; state->pressPos = pos; state->dragging = false; state->widgetItem = 0;}void QToolBarPrivate::startDrag(){ Q_Q(QToolBar); Q_ASSERT(state != 0); if (state->dragging) return; QMainWindow *win = qobject_cast<QMainWindow*>(q->parentWidget()); Q_ASSERT(win != 0); QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout()); Q_ASSERT(layout != 0); int w = q->width(); state->widgetItem = layout->unplug(q); Q_ASSERT(state->widgetItem != 0); state->dragging = true; if (q->layoutDirection() == Qt::RightToLeft) state->pressPos = QPoint(w - state->pressPos.x(), state->pressPos.y());}void QToolBarPrivate::endDrag(){ Q_Q(QToolBar); Q_ASSERT(state != 0); q->releaseMouse(); if (state->dragging) { QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(q->parentWidget()->layout()); Q_ASSERT(layout != 0); if (!layout->plug(state->widgetItem)) { if (q->isFloatable()) { layout->restore();#ifdef Q_WS_X11 setWindowState(true); // gets rid of the X11BypassWindowManager window flag // and activates the resizer#endif q->activateWindow(); } else { layout->revert(state->widgetItem); } } } delete state; state = 0;}void QToolBarPrivate::mousePressEvent(QMouseEvent *event){ if (event->button() != Qt::LeftButton) return; if (!layout->movable()) return; initDrag(event->pos());}void QToolBarPrivate::mouseReleaseEvent(QMouseEvent*){ endDrag();}void QToolBarPrivate::mouseMoveEvent(QMouseEvent *event){ Q_Q(QToolBar); if (!state) return; QMainWindow *win = qobject_cast<QMainWindow*>(q->parentWidget()); if (win == 0) return; QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout()); Q_ASSERT(layout != 0); if (!state->dragging && layout->pluggingWidget == 0 && (event->pos() - state->pressPos).manhattanLength() > QApplication::startDragDistance()) { startDrag();#ifdef Q_OS_WIN grabMouseWhileInWindow();#else q->grabMouse();#endif } if (state->dragging) { QPoint pos = event->globalPos(); // if we are right-to-left, we move so as to keep the right edge the same distance // from the mouse if (q->layoutDirection() == Qt::LeftToRight) pos -= state->pressPos; else pos += QPoint(state->pressPos.x() - q->width(), -state->pressPos.y()); q->move(pos); layout->hover(state->widgetItem, event->globalPos()); }}void QToolBarPrivate::unplug(const QRect &_r){ Q_Q(QToolBar); QRect r = _r; r.moveTopLeft(q->mapToGlobal(QPoint(0, 0))); setWindowState(true, true, r);}void QToolBarPrivate::plug(const QRect &r){ setWindowState(false, false, r);}/******************************************************************************** QToolBar*//*! \class QToolBar \brief The QToolBar class provides a movable panel that contains a set of controls. \ingroup application \mainclass Toolbar buttons are added by adding \e actions, using addAction() or insertAction(). Groups of buttons can be separated using addSeparator() or insertSeparator(). If a toolbar button is not appropriate, a widget can be inserted instead using addWidget() or insertWidget(); examples of suitable widgets are QSpinBox, QDoubleSpinBox, and QComboBox. When a toolbar button is pressed it emits the actionTriggered() signal. A toolbar can be fixed in place in a particular area (e.g. at the top of the window), or it can be movable (isMovable()) between toolbar areas; see allowedAreas() and isAreaAllowed(). When a toolbar is resized in such a way that it is too small to show all the items it contains, an extension button will appear as the last item in the toolbar. Pressing the extension button will pop up a menu containing the items that does not currently fit in the toolbar. When a QToolBar is not a child of a QMainWindow, it looses the ability to populate the extension pop up with widgets added to the toolbar using addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead. This is a known issue which will be fixed in a future release. \sa QToolButton, QMenu, QAction, {Application Example}*//*! \fn bool QToolBar::isAreaAllowed(Qt::ToolBarArea area) const Returns true if this toolbar is dockable in the given \a area; otherwise returns false.*//*! \fn void QToolBar::addAction(QAction *action) \overload Appends the action \a action to the toolbar's list of actions. \sa QMenu::addAction(), QWidget::addAction()*//*! \fn void QToolBar::actionTriggered(QAction *action) This signal is emitted when an action in this toolbar is triggered. This happens when the action's tool button is pressed, or when the action is triggered in some other way outside the tool bar. The parameter holds the triggered \a action.*//*! \fn void QToolBar::allowedAreasChanged(Qt::ToolBarAreas allowedAreas) This signal is emitted when the collection of allowed areas for the toolbar is changed. The new areas in which the toolbar can be positioned are specified by \a allowedAreas. \sa allowedAreas*//*! \fn void QToolBar::iconSizeChanged(const QSize &iconSize) This signal is emitted when the icon size is changed. The \a iconSize parameter holds the toolbar's new icon size. \sa iconSize QMainWindow::iconSize*//*! \fn void QToolBar::movableChanged(bool movable) This signal is emitted when the toolbar becomes movable or fixed. If the toolbar can be moved, \a movable is true; otherwise it is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -