| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 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 | ** | 
|---|
| 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. | 
|---|
| 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 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <qaction.h> | 
|---|
| 43 | #include <qwidgetaction.h> | 
|---|
| 44 | #include <qtoolbar.h> | 
|---|
| 45 | #include <qstyleoption.h> | 
|---|
| 46 | #include <qtoolbutton.h> | 
|---|
| 47 | #include <qmenu.h> | 
|---|
| 48 | #include <qdebug.h> | 
|---|
| 49 | #include <qmath.h> | 
|---|
| 50 |  | 
|---|
| 51 | #include "qmainwindowlayout_p.h" | 
|---|
| 52 | #include "qtoolbarextension_p.h" | 
|---|
| 53 | #include "qtoolbarlayout_p.h" | 
|---|
| 54 | #include "qtoolbarseparator_p.h" | 
|---|
| 55 |  | 
|---|
| 56 | #ifndef QT_NO_TOOLBAR | 
|---|
| 57 |  | 
|---|
| 58 | QT_BEGIN_NAMESPACE | 
|---|
| 59 |  | 
|---|
| 60 | // qmainwindow.cpp | 
|---|
| 61 | extern QMainWindowLayout *qt_mainwindow_layout(const QMainWindow *window); | 
|---|
| 62 |  | 
|---|
| 63 | /****************************************************************************** | 
|---|
| 64 | ** QToolBarItem | 
|---|
| 65 | */ | 
|---|
| 66 |  | 
|---|
| 67 | QToolBarItem::QToolBarItem(QWidget *widget) | 
|---|
| 68 | : QWidgetItem(widget), action(0), customWidget(false) | 
|---|
| 69 | { | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | bool QToolBarItem::isEmpty() const | 
|---|
| 73 | { | 
|---|
| 74 | return action == 0 || !action->isVisible(); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | /****************************************************************************** | 
|---|
| 78 | ** QToolBarLayout | 
|---|
| 79 | */ | 
|---|
| 80 |  | 
|---|
| 81 | QToolBarLayout::QToolBarLayout(QWidget *parent) | 
|---|
| 82 | : QLayout(parent), expanded(false), animating(false), dirty(true), | 
|---|
| 83 | expanding(false), empty(true), expandFlag(false), popupMenu(0) | 
|---|
| 84 | { | 
|---|
| 85 | QToolBar *tb = qobject_cast<QToolBar*>(parent); | 
|---|
| 86 | if (!tb) | 
|---|
| 87 | return; | 
|---|
| 88 |  | 
|---|
| 89 | extension = new QToolBarExtension(tb); | 
|---|
| 90 | extension->setFocusPolicy(Qt::NoFocus); | 
|---|
| 91 | extension->hide(); | 
|---|
| 92 | QObject::connect(tb, SIGNAL(orientationChanged(Qt::Orientation)), | 
|---|
| 93 | extension, SLOT(setOrientation(Qt::Orientation))); | 
|---|
| 94 |  | 
|---|
| 95 | setUsePopupMenu(qobject_cast<QMainWindow*>(tb->parentWidget()) == 0); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | QToolBarLayout::~QToolBarLayout() | 
|---|
| 99 | { | 
|---|
| 100 | while (!items.isEmpty()) { | 
|---|
| 101 | QToolBarItem *item = items.takeFirst(); | 
|---|
| 102 | if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action)) { | 
|---|
| 103 | if (item->customWidget) | 
|---|
| 104 | widgetAction->releaseWidget(item->widget()); | 
|---|
| 105 | } | 
|---|
| 106 | delete item; | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void QToolBarLayout::updateMarginAndSpacing() | 
|---|
| 111 | { | 
|---|
| 112 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 113 | if (!tb) | 
|---|
| 114 | return; | 
|---|
| 115 | QStyle *style = tb->style(); | 
|---|
| 116 | QStyleOptionToolBar opt; | 
|---|
| 117 | tb->initStyleOption(&opt); | 
|---|
| 118 | setMargin(style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, tb) | 
|---|
| 119 | + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, tb)); | 
|---|
| 120 | setSpacing(style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, tb)); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | bool QToolBarLayout::hasExpandFlag() const | 
|---|
| 124 | { | 
|---|
| 125 | return expandFlag; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void QToolBarLayout::setUsePopupMenu(bool set) | 
|---|
| 129 | { | 
|---|
| 130 | if (!dirty && ((popupMenu == 0) == set)) | 
|---|
| 131 | invalidate(); | 
|---|
| 132 | if (!set) { | 
|---|
| 133 | QObject::connect(extension, SIGNAL(clicked(bool)), | 
|---|
| 134 | this, SLOT(setExpanded(bool)), Qt::UniqueConnection); | 
|---|
| 135 | extension->setPopupMode(QToolButton::DelayedPopup); | 
|---|
| 136 | extension->setMenu(0); | 
|---|
| 137 | delete popupMenu; | 
|---|
| 138 | popupMenu = 0; | 
|---|
| 139 | } else { | 
|---|
| 140 | QObject::disconnect(extension, SIGNAL(clicked(bool)), | 
|---|
| 141 | this, SLOT(setExpanded(bool))); | 
|---|
| 142 | extension->setPopupMode(QToolButton::InstantPopup); | 
|---|
| 143 | if (!popupMenu) { | 
|---|
| 144 | popupMenu = new QMenu(extension); | 
|---|
| 145 | } | 
|---|
| 146 | extension->setMenu(popupMenu); | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void QToolBarLayout::checkUsePopupMenu() | 
|---|
| 151 | { | 
|---|
| 152 | QToolBar *tb = static_cast<QToolBar *>(parent()); | 
|---|
| 153 | QMainWindow *mw = qobject_cast<QMainWindow *>(tb->parent()); | 
|---|
| 154 | Qt::Orientation o = tb->orientation(); | 
|---|
| 155 | setUsePopupMenu(!mw || tb->isFloating() || perp(o, expandedSize(mw->size())) >= perp(o, mw->size())); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void QToolBarLayout::addItem(QLayoutItem*) | 
|---|
| 159 | { | 
|---|
| 160 | qWarning() << "QToolBarLayout::addItem(): please use addAction() instead"; | 
|---|
| 161 | return; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | QLayoutItem *QToolBarLayout::itemAt(int index) const | 
|---|
| 165 | { | 
|---|
| 166 | if (index < 0 || index >= items.count()) | 
|---|
| 167 | return 0; | 
|---|
| 168 | return items.at(index); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | QLayoutItem *QToolBarLayout::takeAt(int index) | 
|---|
| 172 | { | 
|---|
| 173 | if (index < 0 || index >= items.count()) | 
|---|
| 174 | return 0; | 
|---|
| 175 | QToolBarItem *item = items.takeAt(index); | 
|---|
| 176 |  | 
|---|
| 177 | if (popupMenu) | 
|---|
| 178 | popupMenu->removeAction(item->action); | 
|---|
| 179 |  | 
|---|
| 180 | QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action); | 
|---|
| 181 | if (widgetAction != 0 && item->customWidget) { | 
|---|
| 182 | widgetAction->releaseWidget(item->widget()); | 
|---|
| 183 | } else { | 
|---|
| 184 | // destroy the QToolButton/QToolBarSeparator | 
|---|
| 185 | item->widget()->hide(); | 
|---|
| 186 | item->widget()->deleteLater(); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | invalidate(); | 
|---|
| 190 | return item; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | void QToolBarLayout::insertAction(int index, QAction *action) | 
|---|
| 194 | { | 
|---|
| 195 | index = qMax(0, index); | 
|---|
| 196 | index = qMin(items.count(), index); | 
|---|
| 197 |  | 
|---|
| 198 | QToolBarItem *item = createItem(action); | 
|---|
| 199 | if (item) { | 
|---|
| 200 | items.insert(index, item); | 
|---|
| 201 | invalidate(); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | int QToolBarLayout::indexOf(QAction *action) const | 
|---|
| 206 | { | 
|---|
| 207 | for (int i = 0; i < items.count(); ++i) { | 
|---|
| 208 | if (items.at(i)->action == action) | 
|---|
| 209 | return i; | 
|---|
| 210 | } | 
|---|
| 211 | return -1; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | int QToolBarLayout::count() const | 
|---|
| 215 | { | 
|---|
| 216 | return items.count(); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | bool QToolBarLayout::isEmpty() const | 
|---|
| 220 | { | 
|---|
| 221 | if (dirty) | 
|---|
| 222 | updateGeomArray(); | 
|---|
| 223 | return empty; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void QToolBarLayout::invalidate() | 
|---|
| 227 | { | 
|---|
| 228 | dirty = true; | 
|---|
| 229 | QLayout::invalidate(); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | Qt::Orientations QToolBarLayout::expandingDirections() const | 
|---|
| 233 | { | 
|---|
| 234 | if (dirty) | 
|---|
| 235 | updateGeomArray(); | 
|---|
| 236 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 237 | if (!tb) | 
|---|
| 238 | return Qt::Orientations(0); | 
|---|
| 239 | Qt::Orientation o = tb->orientation(); | 
|---|
| 240 | return expanding ? Qt::Orientations(o) : Qt::Orientations(0); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | bool QToolBarLayout::movable() const | 
|---|
| 244 | { | 
|---|
| 245 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 246 | if (!tb) | 
|---|
| 247 | return false; | 
|---|
| 248 | QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()); | 
|---|
| 249 | return tb->isMovable() && win != 0; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | void QToolBarLayout::updateGeomArray() const | 
|---|
| 253 | { | 
|---|
| 254 | if (!dirty) | 
|---|
| 255 | return; | 
|---|
| 256 |  | 
|---|
| 257 | QToolBarLayout *that = const_cast<QToolBarLayout*>(this); | 
|---|
| 258 |  | 
|---|
| 259 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 260 | if (!tb) | 
|---|
| 261 | return; | 
|---|
| 262 | QStyle *style = tb->style(); | 
|---|
| 263 | QStyleOptionToolBar opt; | 
|---|
| 264 | tb->initStyleOption(&opt); | 
|---|
| 265 | const int handleExtent = movable() | 
|---|
| 266 | ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; | 
|---|
| 267 | const int margin = this->margin(); | 
|---|
| 268 | const int spacing = this->spacing(); | 
|---|
| 269 | const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); | 
|---|
| 270 | Qt::Orientation o = tb->orientation(); | 
|---|
| 271 |  | 
|---|
| 272 | that->minSize = QSize(0, 0); | 
|---|
| 273 | that->hint = QSize(0, 0); | 
|---|
| 274 | rperp(o, that->minSize) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb); | 
|---|
| 275 | rperp(o, that->hint) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb); | 
|---|
| 276 |  | 
|---|
| 277 | that->expanding = false; | 
|---|
| 278 | that->empty = false; | 
|---|
| 279 |  | 
|---|
| 280 | QVector<QLayoutStruct> a(items.count() + 1); // + 1 for the stretch | 
|---|
| 281 |  | 
|---|
| 282 | int count = 0; | 
|---|
| 283 | for (int i = 0; i < items.count(); ++i) { | 
|---|
| 284 | QToolBarItem *item = items.at(i); | 
|---|
| 285 |  | 
|---|
| 286 | QSize max = item->maximumSize(); | 
|---|
| 287 | QSize min = item->minimumSize(); | 
|---|
| 288 | QSize hint = item->sizeHint(); | 
|---|
| 289 | Qt::Orientations exp = item->expandingDirections(); | 
|---|
| 290 | bool empty = item->isEmpty(); | 
|---|
| 291 |  | 
|---|
| 292 | that->expanding = expanding || exp & o; | 
|---|
| 293 |  | 
|---|
| 294 |  | 
|---|
| 295 | if (item->widget()) { | 
|---|
| 296 | if ((item->widget()->sizePolicy().horizontalPolicy() & QSizePolicy::ExpandFlag)) { | 
|---|
| 297 | that->expandFlag = true; | 
|---|
| 298 | } | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | if (!empty) { | 
|---|
| 302 | if (count == 0) // the minimum size only displays one widget | 
|---|
| 303 | rpick(o, that->minSize) += pick(o, min); | 
|---|
| 304 | int s = perp(o, minSize); | 
|---|
| 305 | rperp(o, that->minSize) = qMax(s, perp(o, min)); | 
|---|
| 306 |  | 
|---|
| 307 | //we only add spacing before item (ie never before the first one) | 
|---|
| 308 | rpick(o, that->hint) += (count == 0 ? 0 : spacing) + pick(o, hint); | 
|---|
| 309 | s = perp(o, that->hint); | 
|---|
| 310 | rperp(o, that->hint) = qMax(s, perp(o, hint)); | 
|---|
| 311 | ++count; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | a[i].sizeHint = pick(o, hint); | 
|---|
| 315 | a[i].maximumSize = pick(o, max); | 
|---|
| 316 | a[i].minimumSize = pick(o, min); | 
|---|
| 317 | a[i].expansive = exp & o; | 
|---|
| 318 | if (o == Qt::Horizontal) | 
|---|
| 319 | a[i].stretch = item->widget()->sizePolicy().horizontalStretch(); | 
|---|
| 320 | else | 
|---|
| 321 | a[i].stretch = item->widget()->sizePolicy().verticalStretch(); | 
|---|
| 322 | a[i].empty = empty; | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | that->geomArray = a; | 
|---|
| 326 | that->empty = count == 0; | 
|---|
| 327 |  | 
|---|
| 328 | rpick(o, that->minSize) += handleExtent; | 
|---|
| 329 | that->minSize += QSize(2*margin, 2*margin); | 
|---|
| 330 | if (items.count() > 1) | 
|---|
| 331 | rpick(o, that->minSize) += spacing + extensionExtent; | 
|---|
| 332 |  | 
|---|
| 333 | rpick(o, that->hint) += handleExtent; | 
|---|
| 334 | that->hint += QSize(2*margin, 2*margin); | 
|---|
| 335 | that->dirty = false; | 
|---|
| 336 | #ifdef Q_WS_MAC | 
|---|
| 337 | if (QMainWindow *mw = qobject_cast<QMainWindow *>(parentWidget()->parentWidget())) { | 
|---|
| 338 | if (mw->unifiedTitleAndToolBarOnMac() | 
|---|
| 339 | && mw->toolBarArea(static_cast<QToolBar *>(parentWidget())) == Qt::TopToolBarArea) { | 
|---|
| 340 | if (expandFlag) { | 
|---|
| 341 | tb->setMaximumSize(0xFFFFFF, 0xFFFFFF); | 
|---|
| 342 | } else { | 
|---|
| 343 | tb->setMaximumSize(hint); | 
|---|
| 344 | } | 
|---|
| 345 | } | 
|---|
| 346 | } | 
|---|
| 347 | #endif | 
|---|
| 348 |  | 
|---|
| 349 | that->dirty = false; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | static bool defaultWidgetAction(QToolBarItem *item) | 
|---|
| 353 | { | 
|---|
| 354 | QWidgetAction *a = qobject_cast<QWidgetAction*>(item->action); | 
|---|
| 355 | return a != 0 && a->defaultWidget() == item->widget(); | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | void QToolBarLayout::setGeometry(const QRect &rect) | 
|---|
| 359 | { | 
|---|
| 360 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 361 | if (!tb) | 
|---|
| 362 | return; | 
|---|
| 363 | QStyle *style = tb->style(); | 
|---|
| 364 | QStyleOptionToolBar opt; | 
|---|
| 365 | tb->initStyleOption(&opt); | 
|---|
| 366 | const int margin = this->margin(); | 
|---|
| 367 | const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); | 
|---|
| 368 | Qt::Orientation o = tb->orientation(); | 
|---|
| 369 |  | 
|---|
| 370 | QLayout::setGeometry(rect); | 
|---|
| 371 |  | 
|---|
| 372 | bool ranOutOfSpace = false; | 
|---|
| 373 | if (!animating) | 
|---|
| 374 | ranOutOfSpace = layoutActions(rect.size()); | 
|---|
| 375 |  | 
|---|
| 376 | if (expanded || animating || ranOutOfSpace) { | 
|---|
| 377 | Qt::ToolBarArea area = Qt::TopToolBarArea; | 
|---|
| 378 | if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget())) | 
|---|
| 379 | area = win->toolBarArea(tb); | 
|---|
| 380 | QSize hint = sizeHint(); | 
|---|
| 381 |  | 
|---|
| 382 | QPoint pos; | 
|---|
| 383 | rpick(o, pos) = pick(o, rect.bottomRight()) - margin - extensionExtent + 2; | 
|---|
| 384 | if (area == Qt::LeftToolBarArea || area == Qt::TopToolBarArea) | 
|---|
| 385 | rperp(o, pos) = perp(o, rect.topLeft()) + margin; | 
|---|
| 386 | else | 
|---|
| 387 | rperp(o, pos) = perp(o, rect.bottomRight()) - margin - (perp(o, hint) - 2*margin) + 1; | 
|---|
| 388 | QSize size; | 
|---|
| 389 | rpick(o, size) = extensionExtent; | 
|---|
| 390 | rperp(o, size) = perp(o, hint) - 2*margin; | 
|---|
| 391 | QRect r(pos, size); | 
|---|
| 392 |  | 
|---|
| 393 | if (o == Qt::Horizontal) | 
|---|
| 394 | r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r); | 
|---|
| 395 |  | 
|---|
| 396 | extension->setGeometry(r); | 
|---|
| 397 |  | 
|---|
| 398 | if (extension->isHidden()) | 
|---|
| 399 | extension->show(); | 
|---|
| 400 | } else { | 
|---|
| 401 | if (!extension->isHidden()) | 
|---|
| 402 | extension->hide(); | 
|---|
| 403 | } | 
|---|
| 404 | #ifdef Q_WS_MAC | 
|---|
| 405 | // Nothing to do for Carbon... probably | 
|---|
| 406 | #  ifdef QT_MAC_USE_COCOA | 
|---|
| 407 | if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget())) { | 
|---|
| 408 | Qt::ToolBarArea area = win->toolBarArea(tb); | 
|---|
| 409 | if (win->unifiedTitleAndToolBarOnMac() && area == Qt::TopToolBarArea) { | 
|---|
| 410 | qt_mainwindow_layout(win)->fixSizeInUnifiedToolbar(tb); | 
|---|
| 411 | } | 
|---|
| 412 | } | 
|---|
| 413 | #  endif | 
|---|
| 414 | #endif | 
|---|
| 415 |  | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | bool QToolBarLayout::layoutActions(const QSize &size) | 
|---|
| 419 | { | 
|---|
| 420 | if (dirty) | 
|---|
| 421 | updateGeomArray(); | 
|---|
| 422 |  | 
|---|
| 423 | QRect rect(0, 0, size.width(), size.height()); | 
|---|
| 424 |  | 
|---|
| 425 | QList<QWidget*> showWidgets, hideWidgets; | 
|---|
| 426 |  | 
|---|
| 427 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 428 | if (!tb) | 
|---|
| 429 | return false; | 
|---|
| 430 | QStyle *style = tb->style(); | 
|---|
| 431 | QStyleOptionToolBar opt; | 
|---|
| 432 | tb->initStyleOption(&opt); | 
|---|
| 433 | const int handleExtent = movable() | 
|---|
| 434 | ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; | 
|---|
| 435 | const int margin = this->margin(); | 
|---|
| 436 | const int spacing = this->spacing(); | 
|---|
| 437 | const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); | 
|---|
| 438 | Qt::Orientation o = tb->orientation(); | 
|---|
| 439 | bool extensionMenuContainsOnlyWidgetActions = true; | 
|---|
| 440 |  | 
|---|
| 441 | int space = pick(o, rect.size()) - 2*margin - handleExtent; | 
|---|
| 442 | if (space <= 0) | 
|---|
| 443 | return false;  // nothing to do. | 
|---|
| 444 |  | 
|---|
| 445 | if(popupMenu) | 
|---|
| 446 | popupMenu->clear(); | 
|---|
| 447 |  | 
|---|
| 448 | bool ranOutOfSpace = false; | 
|---|
| 449 | int rows = 0; | 
|---|
| 450 | int rowPos = perp(o, rect.topLeft()) + margin; | 
|---|
| 451 | int i = 0; | 
|---|
| 452 | while (i < items.count()) { | 
|---|
| 453 | QVector<QLayoutStruct> a = geomArray; | 
|---|
| 454 |  | 
|---|
| 455 | int start = i; | 
|---|
| 456 | int size = 0; | 
|---|
| 457 | int prev = -1; | 
|---|
| 458 | int rowHeight = 0; | 
|---|
| 459 | int count = 0; | 
|---|
| 460 | int maximumSize = 0; | 
|---|
| 461 | bool expansiveRow = false; | 
|---|
| 462 | for (; i < items.count(); ++i) { | 
|---|
| 463 | if (a[i].empty) | 
|---|
| 464 | continue; | 
|---|
| 465 |  | 
|---|
| 466 | int newSize = size + (count == 0 ? 0 : spacing) + a[i].minimumSize; | 
|---|
| 467 | if (prev != -1 && newSize > space) { | 
|---|
| 468 | if (rows == 0) | 
|---|
| 469 | ranOutOfSpace = true; | 
|---|
| 470 | // do we have to move the previous item to the next line to make space for | 
|---|
| 471 | // the extension button? | 
|---|
| 472 | if (count > 1 && size + spacing + extensionExtent > space) | 
|---|
| 473 | i = prev; | 
|---|
| 474 | break; | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | if (expanded) | 
|---|
| 478 | rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint())); | 
|---|
| 479 | expansiveRow = expansiveRow || a[i].expansive; | 
|---|
| 480 | size = newSize; | 
|---|
| 481 | maximumSize += spacing + (a[i].expansive ? a[i].maximumSize : a[i].smartSizeHint()); | 
|---|
| 482 | prev = i; | 
|---|
| 483 | ++count; | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | // stretch at the end | 
|---|
| 487 | a[i].sizeHint = 0; | 
|---|
| 488 | a[i].maximumSize = QWIDGETSIZE_MAX; | 
|---|
| 489 | a[i].minimumSize = 0; | 
|---|
| 490 | a[i].expansive = true; | 
|---|
| 491 | a[i].stretch = 0; | 
|---|
| 492 | a[i].empty = true; | 
|---|
| 493 |  | 
|---|
| 494 | if (expansiveRow && maximumSize < space) { | 
|---|
| 495 | expansiveRow = false; | 
|---|
| 496 | a[i].maximumSize = space - maximumSize; | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | qGeomCalc(a, start, i - start + (expansiveRow ? 0 : 1), 0, | 
|---|
| 500 | space - (ranOutOfSpace ? (extensionExtent + spacing) : 0), | 
|---|
| 501 | spacing); | 
|---|
| 502 |  | 
|---|
| 503 | for (int j = start; j < i; ++j) { | 
|---|
| 504 | QToolBarItem *item = items.at(j); | 
|---|
| 505 |  | 
|---|
| 506 | if (a[j].empty) { | 
|---|
| 507 | if (!item->widget()->isHidden()) | 
|---|
| 508 | hideWidgets << item->widget(); | 
|---|
| 509 | continue; | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | QPoint pos; | 
|---|
| 513 | rpick(o, pos) = margin + handleExtent + a[j].pos; | 
|---|
| 514 | rperp(o, pos) = rowPos; | 
|---|
| 515 | QSize size; | 
|---|
| 516 | rpick(o, size) = a[j].size; | 
|---|
| 517 | if (expanded) | 
|---|
| 518 | rperp(o, size) = rowHeight; | 
|---|
| 519 | else | 
|---|
| 520 | rperp(o, size) = perp(o, rect.size()) - 2*margin; | 
|---|
| 521 | QRect r(pos, size); | 
|---|
| 522 |  | 
|---|
| 523 | if (o == Qt::Horizontal) | 
|---|
| 524 | r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r); | 
|---|
| 525 |  | 
|---|
| 526 | item->setGeometry(r); | 
|---|
| 527 |  | 
|---|
| 528 | if (item->widget()->isHidden()) | 
|---|
| 529 | showWidgets << item->widget(); | 
|---|
| 530 | } | 
|---|
| 531 |  | 
|---|
| 532 | if (!expanded) { | 
|---|
| 533 | for (int j = i; j < items.count(); ++j) { | 
|---|
| 534 | QToolBarItem *item = items.at(j); | 
|---|
| 535 | if (!item->widget()->isHidden()) | 
|---|
| 536 | hideWidgets << item->widget(); | 
|---|
| 537 | if (popupMenu) { | 
|---|
| 538 | if (!defaultWidgetAction(item)) { | 
|---|
| 539 | popupMenu->addAction(item->action); | 
|---|
| 540 | extensionMenuContainsOnlyWidgetActions = false; | 
|---|
| 541 | } | 
|---|
| 542 | } | 
|---|
| 543 | } | 
|---|
| 544 | break; | 
|---|
| 545 | } | 
|---|
| 546 |  | 
|---|
| 547 | rowPos += rowHeight + spacing; | 
|---|
| 548 | ++rows; | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 | // if we are using a popup menu, not the expadning toolbar effect, we cannot move custom | 
|---|
| 552 | // widgets into the menu. If only custom widget actions are chopped off, the popup menu | 
|---|
| 553 | // is empty. So we show the little extension button to show something is chopped off, | 
|---|
| 554 | // but we make it disabled. | 
|---|
| 555 | extension->setEnabled(popupMenu == 0 || !extensionMenuContainsOnlyWidgetActions); | 
|---|
| 556 |  | 
|---|
| 557 | // we have to do the show/hide here, because it triggers more calls to setGeometry :( | 
|---|
| 558 | for (int i = 0; i < showWidgets.count(); ++i) | 
|---|
| 559 | showWidgets.at(i)->show(); | 
|---|
| 560 | for (int i = 0; i < hideWidgets.count(); ++i) | 
|---|
| 561 | hideWidgets.at(i)->hide(); | 
|---|
| 562 |  | 
|---|
| 563 | return ranOutOfSpace; | 
|---|
| 564 | } | 
|---|
| 565 |  | 
|---|
| 566 | QSize QToolBarLayout::expandedSize(const QSize &size) const | 
|---|
| 567 | { | 
|---|
| 568 | if (dirty) | 
|---|
| 569 | updateGeomArray(); | 
|---|
| 570 |  | 
|---|
| 571 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 572 | if (!tb) | 
|---|
| 573 | return QSize(0, 0); | 
|---|
| 574 | QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()); | 
|---|
| 575 | Qt::Orientation o = tb->orientation(); | 
|---|
| 576 | QStyle *style = tb->style(); | 
|---|
| 577 | QStyleOptionToolBar opt; | 
|---|
| 578 | tb->initStyleOption(&opt); | 
|---|
| 579 | const int handleExtent = movable() | 
|---|
| 580 | ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; | 
|---|
| 581 | const int margin = this->margin(); | 
|---|
| 582 | const int spacing = this->spacing(); | 
|---|
| 583 | const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); | 
|---|
| 584 |  | 
|---|
| 585 | int total_w = 0; | 
|---|
| 586 | int count = 0; | 
|---|
| 587 | for (int x = 0; x < items.count(); ++x) { | 
|---|
| 588 | if (!geomArray[x].empty) { | 
|---|
| 589 | total_w += (count == 0 ? 0 : spacing) + geomArray[x].minimumSize; | 
|---|
| 590 | ++count; | 
|---|
| 591 | } | 
|---|
| 592 | } | 
|---|
| 593 | if (count == 0) | 
|---|
| 594 | return QSize(0, 0); | 
|---|
| 595 |  | 
|---|
| 596 | int min_w = pick(o, size); | 
|---|
| 597 | int rows = (int)qSqrt(qreal(count)); | 
|---|
| 598 | if (rows == 1) | 
|---|
| 599 | ++rows;      // we want to expand to at least two rows | 
|---|
| 600 | int space = total_w/rows + spacing + extensionExtent; | 
|---|
| 601 | space = qMax(space, min_w - 2*margin - handleExtent); | 
|---|
| 602 | if (win != 0) | 
|---|
| 603 | space = qMin(space, pick(o, win->size()) - 2*margin - handleExtent); | 
|---|
| 604 |  | 
|---|
| 605 | int w = 0; | 
|---|
| 606 | int h = 0; | 
|---|
| 607 | int i = 0; | 
|---|
| 608 | while (i < items.count()) { | 
|---|
| 609 | int count = 0; | 
|---|
| 610 | int size = 0; | 
|---|
| 611 | int prev = -1; | 
|---|
| 612 | int rowHeight = 0; | 
|---|
| 613 | for (; i < items.count(); ++i) { | 
|---|
| 614 | if (geomArray[i].empty) | 
|---|
| 615 | continue; | 
|---|
| 616 |  | 
|---|
| 617 | int newSize = size + (count == 0 ? 0 : spacing) + geomArray[i].minimumSize; | 
|---|
| 618 | rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint())); | 
|---|
| 619 | if (prev != -1 && newSize > space) { | 
|---|
| 620 | if (count > 1 && size + spacing + extensionExtent > space) { | 
|---|
| 621 | size -= spacing + geomArray[prev].minimumSize; | 
|---|
| 622 | i = prev; | 
|---|
| 623 | } | 
|---|
| 624 | break; | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | size = newSize; | 
|---|
| 628 | prev = i; | 
|---|
| 629 | ++count; | 
|---|
| 630 | } | 
|---|
| 631 |  | 
|---|
| 632 | w = qMax(size, w); | 
|---|
| 633 | h += rowHeight + spacing; | 
|---|
| 634 | } | 
|---|
| 635 |  | 
|---|
| 636 | w += 2*margin + handleExtent + spacing + extensionExtent; | 
|---|
| 637 | w = qMax(w, min_w); | 
|---|
| 638 | if (win != 0) | 
|---|
| 639 | w = qMin(w, pick(o, win->size())); | 
|---|
| 640 | h += 2*margin - spacing; //there is no spacing before the first row | 
|---|
| 641 |  | 
|---|
| 642 | QSize result; | 
|---|
| 643 | rpick(o, result) = w; | 
|---|
| 644 | rperp(o, result) = h; | 
|---|
| 645 | return result; | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | void QToolBarLayout::setExpanded(bool exp) | 
|---|
| 649 | { | 
|---|
| 650 | if (exp == expanded) | 
|---|
| 651 | return; | 
|---|
| 652 |  | 
|---|
| 653 | expanded = exp; | 
|---|
| 654 | extension->setChecked(expanded); | 
|---|
| 655 |  | 
|---|
| 656 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 657 | if (!tb) | 
|---|
| 658 | return; | 
|---|
| 659 | if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget())) { | 
|---|
| 660 | #ifdef QT_NO_DOCKWIDGET | 
|---|
| 661 | animating = false; | 
|---|
| 662 | #else | 
|---|
| 663 | animating = !tb->isWindow() && win->isAnimated(); | 
|---|
| 664 | #endif | 
|---|
| 665 | QMainWindowLayout *layout = qt_mainwindow_layout(win); | 
|---|
| 666 | if (expanded) { | 
|---|
| 667 | tb->raise(); | 
|---|
| 668 | } else { | 
|---|
| 669 | QList<int> path = layout->layoutState.indexOf(tb); | 
|---|
| 670 | if (!path.isEmpty()) { | 
|---|
| 671 | QRect rect = layout->layoutState.itemRect(path); | 
|---|
| 672 | layoutActions(rect.size()); | 
|---|
| 673 | } | 
|---|
| 674 | } | 
|---|
| 675 | layout->layoutState.toolBarAreaLayout.apply(animating); | 
|---|
| 676 | } | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | QSize QToolBarLayout::minimumSize() const | 
|---|
| 680 | { | 
|---|
| 681 | if (dirty) | 
|---|
| 682 | updateGeomArray(); | 
|---|
| 683 | return minSize; | 
|---|
| 684 | } | 
|---|
| 685 |  | 
|---|
| 686 | QSize QToolBarLayout::sizeHint() const | 
|---|
| 687 | { | 
|---|
| 688 | if (dirty) | 
|---|
| 689 | updateGeomArray(); | 
|---|
| 690 | return hint; | 
|---|
| 691 | } | 
|---|
| 692 |  | 
|---|
| 693 | QToolBarItem *QToolBarLayout::createItem(QAction *action) | 
|---|
| 694 | { | 
|---|
| 695 | bool customWidget = false; | 
|---|
| 696 | bool standardButtonWidget = false; | 
|---|
| 697 | QWidget *widget = 0; | 
|---|
| 698 | QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); | 
|---|
| 699 | if (!tb) | 
|---|
| 700 | return (QToolBarItem *)0; | 
|---|
| 701 |  | 
|---|
| 702 | if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action)) { | 
|---|
| 703 | widget = widgetAction->requestWidget(tb); | 
|---|
| 704 | if (widget != 0) { | 
|---|
| 705 | widget->setAttribute(Qt::WA_LayoutUsesWidgetRect); | 
|---|
| 706 | customWidget = true; | 
|---|
| 707 | } | 
|---|
| 708 | } else if (action->isSeparator()) { | 
|---|
| 709 | QToolBarSeparator *sep = new QToolBarSeparator(tb); | 
|---|
| 710 | connect(tb, SIGNAL(orientationChanged(Qt::Orientation)), | 
|---|
| 711 | sep, SLOT(setOrientation(Qt::Orientation))); | 
|---|
| 712 | widget = sep; | 
|---|
| 713 | } | 
|---|
| 714 |  | 
|---|
| 715 | if (!widget) { | 
|---|
| 716 | QToolButton *button = new QToolButton(tb); | 
|---|
| 717 | button->setAutoRaise(true); | 
|---|
| 718 | button->setFocusPolicy(Qt::NoFocus); | 
|---|
| 719 | button->setIconSize(tb->iconSize()); | 
|---|
| 720 | button->setToolButtonStyle(tb->toolButtonStyle()); | 
|---|
| 721 | QObject::connect(tb, SIGNAL(iconSizeChanged(QSize)), | 
|---|
| 722 | button, SLOT(setIconSize(QSize))); | 
|---|
| 723 | QObject::connect(tb, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)), | 
|---|
| 724 | button, SLOT(setToolButtonStyle(Qt::ToolButtonStyle))); | 
|---|
| 725 | button->setDefaultAction(action); | 
|---|
| 726 | QObject::connect(button, SIGNAL(triggered(QAction*)), tb, SIGNAL(actionTriggered(QAction*))); | 
|---|
| 727 | widget = button; | 
|---|
| 728 | standardButtonWidget = true; | 
|---|
| 729 | } | 
|---|
| 730 |  | 
|---|
| 731 | widget->hide(); | 
|---|
| 732 | QToolBarItem *result = new QToolBarItem(widget); | 
|---|
| 733 | if (standardButtonWidget) | 
|---|
| 734 | result->setAlignment(Qt::AlignJustify); | 
|---|
| 735 | result->customWidget = customWidget; | 
|---|
| 736 | result->action = action; | 
|---|
| 737 | return result; | 
|---|
| 738 | } | 
|---|
| 739 |  | 
|---|
| 740 | QT_END_NAMESPACE | 
|---|
| 741 |  | 
|---|
| 742 | #endif // QT_NO_TOOLBAR | 
|---|