📄 qtoolbar.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 <qpainter.h>#include <qrubberband.h>#include <qsignalmapper.h>#include <qstyle.h>#include <qstyleoption.h>#include <qtoolbutton.h>#ifdef Q_WS_MAC#include <private/qt_mac_p.h>#endif#include <private/qmainwindowlayout_p.h>#include "qtoolbar_p.h"#include "qtoolbarextension_p.h"#include "qtoolbarhandle_p.h"#include "qtoolbarseparator_p.h"#include "qdebug.h"static int positionForArea(Qt::ToolBarArea area){ switch (area) { case Qt::LeftToolBarArea: return 0; case Qt::RightToolBarArea: return 1; case Qt::TopToolBarArea: return 2; case Qt::BottomToolBarArea: return 3; default: return -1; }}static QStyleOptionToolBar getStyleOption(QToolBar *toolBar){ QStyleOptionToolBar option; option.init(toolBar); if (toolBar->orientation() == Qt::Horizontal) option.state |= QStyle::State_Horizontal; option.lineWidth = toolBar->style()->pixelMetric(QStyle::PM_ToolBarFrameWidth); option.features = toolBar->isMovable() ? QStyleOptionToolBar::Movable : QStyleOptionToolBar::None; // Add more styleoptions if the toolbar has been added to a mainwindow. QMainWindow *mainWindow = qobject_cast<QMainWindow *>(toolBar->parent()); if (!mainWindow) return option; QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(mainWindow->layout()); Q_ASSERT_X(layout != 0, "QToolBarPrivate::getStyleOption()", "QMainWindow->layout() != QMainWindowLayout"); // Determine the toolbar area. int layoutIndex = layout->indexOf(toolBar); if (layoutIndex != -1) option.toolBarArea = layout->toolBarArea(toolBar); // Find the toolbar line, and position within the line. int toolBarTotalLineCount = 0; int toolBarLineCount = 0; foreach (QMainWindowLayout::ToolBarLineInfo lineInfo, layout->tb_layout_info) { if (lineInfo.pos != positionForArea(option.toolBarArea)) continue; int toolBarIndex = -1; bool lineVisible = false; for (int i = 0; i < lineInfo.list.size(); ++i){ QMainWindowLayout::ToolBarLayoutInfo layoutInfo = lineInfo.list.at(i); if (layoutInfo.item->widget()->isVisible()) lineVisible = true; if (layoutInfo.item->widget() == toolBar) { // This is our toolbar, so we now have the line and position. toolBarLineCount = toolBarTotalLineCount; // We have to determine how many visible toolbars there are in this line int visibleLines = 0; for (int j = 0; j < lineInfo.list.size(); ++j){ QMainWindowLayout::ToolBarLayoutInfo info = lineInfo.list.at(j); if (info.item->widget() == toolBar) toolBarIndex = visibleLines; if (info.item->widget()->isVisible()) visibleLines++; } // Determine the position within this toolbar line if (toolBarIndex == 0) { if (visibleLines == 1) option.positionWithinLine = QStyleOptionToolBar::OnlyOne; else option.positionWithinLine = QStyleOptionToolBar::Beginning; } else if (toolBarIndex < visibleLines - 1) { option.positionWithinLine = QStyleOptionToolBar::Middle; } else if (toolBarIndex == visibleLines - 1) { option.positionWithinLine = QStyleOptionToolBar::End; } break; } } if(lineVisible) ++toolBarTotalLineCount; } Q_ASSERT_X(toolBarLineCount >= 0, "QToolBarPrivate::getStyleOption()", "toolbar not found in layout"); if (option.toolBarArea== Qt::BottomToolBarArea || option.toolBarArea==Qt::RightToolBarArea){ toolBarLineCount = toolBarTotalLineCount-toolBarLineCount-1; } // Determine the line position of this toolbar if (toolBarLineCount == 0) { if (toolBarTotalLineCount == 1) option.positionOfLine = QStyleOptionToolBar::OnlyOne; else option.positionOfLine = QStyleOptionToolBar::Beginning; } else if (toolBarLineCount < toolBarTotalLineCount - 1) { option.positionOfLine = QStyleOptionToolBar::Middle; } else if (toolBarLineCount == toolBarTotalLineCount - 1) { option.positionOfLine = QStyleOptionToolBar::End; } return option;}/*static QStyleOptionFrame getStyleOption(QToolBar *tb){ QStyleOptionFrame opt; opt.init(tb); if (tb->orientation() == Qt::Horizontal) opt.state |= QStyle::State_Horizontal; opt.lineWidth = tb->style()->pixelMetric(QStyle::PM_ToolBarFrameWidth); return opt;}*//* QToolBarPrivate*/void QToolBarPrivate::init(){ Q_Q(QToolBar); movable = true; q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding)); q->setBackgroundRole(QPalette::Button); QStyleOptionToolBar opt = getStyleOption(q); QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight, q); QStyle *style = q->style(); int e = style->pixelMetric(QStyle::PM_ToolBarIconSize); iconSize = QSize(e, e); layout->setAlignment(Qt::AlignLeft); layout->setMargin(style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, q) + style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, q)); layout->setSpacing(style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, q)); handle = new QToolBarHandle(q); QObject::connect(q, SIGNAL(orientationChanged(Qt::Orientation)), handle, SLOT(setOrientation(Qt::Orientation))); layout->addWidget(handle); handle->setVisible(movable && (qobject_cast<QMainWindow *>(q->parentWidget()) != 0)); extension = new QToolBarExtension(q); QObject::connect(q, SIGNAL(orientationChanged(Qt::Orientation)), extension, SLOT(setOrientation(Qt::Orientation))); extension->setFocusPolicy(Qt::NoFocus); extension->hide();#ifdef Q_WS_MAC if (q->parentWidget()) { // Make sure that the window has the "toolbar" button. 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; }}QToolBarItem QToolBarPrivate::createItem(QAction *action){ Q_Q(QToolBar); QToolBarItem item; item.action = action; item.hidden = false; QToolBarWidgetAction *widgetAction = qobject_cast<QToolBarWidgetAction *>(action); if (widgetAction) { item.widget = widgetAction->widget(); } else if (action->isSeparator()) { item.widget = new QToolBarSeparator(q); QObject::connect(q, SIGNAL(orientationChanged(Qt::Orientation)), item.widget, SLOT(setOrientation(Qt::Orientation))); } else { QToolButton *button = new QToolButton(q); button->setAutoRaise(true); button->setFocusPolicy(Qt::NoFocus); button->setIconSize(iconSize); button->setToolButtonStyle(toolButtonStyle); QObject::connect(q, SIGNAL(iconSizeChanged(QSize)), button, SLOT(setIconSize(QSize))); QObject::connect(q, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)), button, SLOT(setToolButtonStyle(Qt::ToolButtonStyle))); button->setDefaultAction(action); QObject::connect(button, SIGNAL(triggered(QAction*)), q, SIGNAL(actionTriggered(QAction*))); item.widget = button; } return item;}/* Returns the position of \a action. This function returns -1 if \a action is not found.*/int QToolBarPrivate::indexOf(QAction *action) const{ for (int i = 0; i < items.size(); ++i) { const QToolBarItem &item = items.at(i); if (item.action == action) return i; } return -1;}/*! \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. Note that only action based items will be shown in the menu. If only non-action based items are to appear in the extension menu (e.g. a QSpinBox), the extension button will appear as usual, but it will be disabled to indicate that some items in the toolbar are currently not visible. \sa QToolButton*//*! \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::actionTriggered(QAction *action) This signal is emitted when a toolbar button is pressed. The parameter holds the toolbar button's associated \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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -