📄 qtoolbarlayout.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 <qaction.h>#include <qwidgetaction.h>#include <qtoolbar.h>#include <qstyleoption.h>#include <qtoolbutton.h>#include <qmenu.h>#include <qdebug.h>#include <math.h>#include "qmainwindowlayout_p.h"#include "qtoolbarextension_p.h"#include "qtoolbarlayout_p.h"#include "qtoolbarseparator_p.h"#ifndef QT_NO_TOOLBAR/******************************************************************************** QToolBarItem*/QToolBarItem::QToolBarItem(QWidget *widget) : QWidgetItem(widget), action(0), customWidget(false){}bool QToolBarItem::isEmpty() const{ return action == 0 || !action->isVisible();}/******************************************************************************** QToolBarLayout*/QToolBarLayout::QToolBarLayout(QWidget *parent) : QLayout(parent), expanded(false), animating(false), dirty(true), expanding(false), empty(true), popupMenu(0){ QToolBar *tb = qobject_cast<QToolBar*>(parent); extension = new QToolBarExtension(tb); extension->setFocusPolicy(Qt::NoFocus); extension->hide(); QObject::connect(tb, SIGNAL(orientationChanged(Qt::Orientation)), extension, SLOT(setOrientation(Qt::Orientation))); setUsePopupMenu(qobject_cast<QMainWindow*>(tb->parentWidget()) == 0);}QToolBarLayout::~QToolBarLayout(){ for (int i = 0; i < items.count(); ++i) { while (!items.isEmpty()) { QToolBarItem *item = items.takeFirst(); if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action)) { if (item->customWidget) widgetAction->releaseWidget(item->widget()); } delete item; } }}void QToolBarLayout::updateMarginAndSpacing(){ QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); QStyle *style = tb->style(); QStyleOptionToolBar opt; tb->initStyleOption(&opt); setMargin(style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, tb) + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, tb)); setSpacing(style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, tb));}void QToolBarLayout::setUsePopupMenu(bool set){ if (!set) { QObject::connect(extension, SIGNAL(clicked(bool)), this, SLOT(setExpanded(bool))); extension->setPopupMode(QToolButton::DelayedPopup); extension->setMenu(0); delete popupMenu; popupMenu = 0; } else { QObject::disconnect(extension, SIGNAL(clicked(bool)), this, SLOT(setExpanded(bool))); extension->setPopupMode(QToolButton::InstantPopup); if (!popupMenu) { popupMenu = new QMenu(extension); } extension->setMenu(popupMenu); } invalidate();}void QToolBarLayout::addItem(QLayoutItem*){ qWarning() << "QToolBarLayout::addItem(): please use addAction() instead"; return;}QLayoutItem *QToolBarLayout::itemAt(int index) const{ if (index < 0 || index >= items.count()) return 0; return items.at(index);}QLayoutItem *QToolBarLayout::takeAt(int index){ if (index < 0 || index >= items.count()) return 0; QToolBarItem *item = items.takeAt(index); QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action); if (widgetAction != 0 && item->customWidget) { widgetAction->releaseWidget(item->widget()); } else { // destroy the QToolButton/QToolBarSeparator item->widget()->hide(); item->widget()->deleteLater(); } invalidate(); return item;}void QToolBarLayout::insertAction(int index, QAction *action){ index = qMax(0, index); index = qMin(items.count(), index); QToolBarItem *item = createItem(action); if (item) { items.insert(index, item); invalidate(); }}int QToolBarLayout::indexOf(QAction *action) const{ for (int i = 0; i < items.count(); ++i) { if (items.at(i)->action == action) return i; } return -1;}int QToolBarLayout::count() const{ return items.count();}bool QToolBarLayout::isEmpty() const{ if (dirty) updateGeomArray(); return empty;}void QToolBarLayout::invalidate(){ dirty = true; QLayout::invalidate();}Qt::Orientations QToolBarLayout::expandingDirections() const{ if (dirty) updateGeomArray(); QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); Qt::Orientation o = tb->orientation(); return expanding ? Qt::Orientations(o) : Qt::Orientations(0);}bool QToolBarLayout::movable() const{ QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()); return tb->isMovable() && win != 0;}void QToolBarLayout::updateGeomArray() const{ if (!dirty) return; QToolBarLayout *that = const_cast<QToolBarLayout*>(this); QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); QStyle *style = tb->style(); QStyleOptionToolBar opt; tb->initStyleOption(&opt); const int handleExtent = movable() ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; const int margin = this->margin(); const int spacing = this->spacing(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); Qt::Orientation o = tb->orientation(); that->minSize = QSize(0, 0); that->hint = QSize(0, 0); rperp(o, that->minSize) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb); rperp(o, that->hint) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb); that->expanding = false; that->empty = false; QVector<QLayoutStruct> a(items.count() + 1); // + 1 for the stretch int count = 0; for (int i = 0; i < items.count(); ++i) { QToolBarItem *item = items.at(i); QSize max = item->maximumSize(); QSize min = item->minimumSize(); QSize hint = item->sizeHint(); Qt::Orientations exp = item->expandingDirections(); bool empty = item->isEmpty(); that->expanding = expanding || exp & o; if (!empty) { if (count == 0) // the minimum size only displays one widget rpick(o, that->minSize) += spacing + pick(o, min); int s = perp(o, minSize); rperp(o, that->minSize) = qMax(s, perp(o, min)); rpick(o, that->hint) += spacing + pick(o, hint); s = perp(o, that->hint); rperp(o, that->hint) = qMax(s, perp(o, hint)); ++count; } a[i].sizeHint = pick(o, hint); a[i].maximumSize = pick(o, max); a[i].minimumSize = pick(o, min); a[i].expansive = exp & o; if (o == Qt::Horizontal) a[i].stretch = item->widget()->sizePolicy().horizontalStretch(); else a[i].stretch = item->widget()->sizePolicy().verticalStretch(); a[i].empty = empty; } that->geomArray = a; that->empty = count == 0; rpick(o, that->minSize) += handleExtent; that->minSize += QSize(2*margin, 2*margin); if (items.count() > 1) rpick(o, that->minSize) += spacing + extensionExtent; rpick(o, that->hint) += handleExtent; that->hint += QSize(2*margin, 2*margin);#ifdef Q_WS_MAC if (QMainWindow *mw = qobject_cast<QMainWindow *>(parentWidget()->parentWidget())) { if (mw->unifiedTitleAndToolBarOnMac() && mw->toolBarArea(static_cast<QToolBar *>(parentWidget())) == Qt::TopToolBarArea) { tb->setMaximumSize(hint); } }#endif that->dirty = false;}static bool defaultWidgetAction(QAction *action){ QWidgetAction *a = qobject_cast<QWidgetAction*>(action); return a != 0 && a->defaultWidget() != 0;}void QToolBarLayout::setGeometry(const QRect &rect){ QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); QStyle *style = tb->style(); QStyleOptionToolBar opt; tb->initStyleOption(&opt); const int handleExtent = movable() ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; const int margin = this->margin(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); Qt::Orientation o = tb->orientation(); QLayout::setGeometry(rect); if (movable()) { if (o == Qt::Horizontal) { handRect = QRect(margin, margin, handleExtent, rect.height() - 2*margin); handRect = QStyle::visualRect(parentWidget()->layoutDirection(), rect, handRect); } else { handRect = QRect(margin, margin, rect.width() - 2*margin, handleExtent); } } else { handRect = QRect(); } bool ranOutOfSpace = false; if (!animating) ranOutOfSpace = layoutActions(rect.size());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -