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