src/gui/widgets/qtoolbar.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qtoolbar.h"
       
    43 
       
    44 #ifndef QT_NO_TOOLBAR
       
    45 
       
    46 #include <qapplication.h>
       
    47 #include <qcombobox.h>
       
    48 #include <qevent.h>
       
    49 #include <qlayout.h>
       
    50 #include <qmainwindow.h>
       
    51 #include <qmenu.h>
       
    52 #include <qmenubar.h>
       
    53 #include <qrubberband.h>
       
    54 #include <qsignalmapper.h>
       
    55 #include <qstylepainter.h>
       
    56 #include <qtoolbutton.h>
       
    57 #include <qwidgetaction.h>
       
    58 #include <qtimer.h>
       
    59 #include <private/qwidgetaction_p.h>
       
    60 #ifdef Q_WS_MAC
       
    61 #include <private/qt_mac_p.h>
       
    62 #include <private/qt_cocoa_helpers_mac_p.h>
       
    63 #endif
       
    64 
       
    65 #include <private/qmainwindowlayout_p.h>
       
    66 
       
    67 #include "qtoolbar_p.h"
       
    68 #include "qtoolbarseparator_p.h"
       
    69 #include "qtoolbarlayout_p.h"
       
    70 
       
    71 #include  "qdebug.h"
       
    72 
       
    73 #define POPUP_TIMER_INTERVAL 500
       
    74 
       
    75 QT_BEGIN_NAMESPACE
       
    76 
       
    77 #ifdef Q_WS_MAC
       
    78 static void qt_mac_updateToolBarButtonHint(QWidget *parentWidget)
       
    79 {
       
    80     if (!(parentWidget->windowFlags() & Qt::CustomizeWindowHint))
       
    81         parentWidget->setWindowFlags(parentWidget->windowFlags() | Qt::MacWindowToolBarButtonHint);
       
    82 }
       
    83 #endif
       
    84 
       
    85 /******************************************************************************
       
    86 ** QToolBarPrivate
       
    87 */
       
    88 
       
    89 void QToolBarPrivate::init()
       
    90 {
       
    91     Q_Q(QToolBar);
       
    92     q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
    93     q->setBackgroundRole(QPalette::Button);
       
    94     q->setAttribute(Qt::WA_Hover);
       
    95     q->setAttribute(Qt::WA_X11NetWmWindowTypeToolBar);
       
    96 
       
    97     QStyle *style = q->style();
       
    98     int e = style->pixelMetric(QStyle::PM_ToolBarIconSize, 0, q);
       
    99     iconSize = QSize(e, e);
       
   100 
       
   101     layout = new QToolBarLayout(q);
       
   102     layout->updateMarginAndSpacing();
       
   103 
       
   104 #ifdef Q_WS_MAC
       
   105     if (q->parentWidget() && q->parentWidget()->isWindow()) {
       
   106         // Make sure that the window has the "toolbar" button.
       
   107         QWidget *parentWidget = q->parentWidget();
       
   108         qt_mac_updateToolBarButtonHint(parentWidget);
       
   109         reinterpret_cast<QToolBar *>(parentWidget)->d_func()->createWinId(); // Please let me create your winId...
       
   110         extern OSWindowRef qt_mac_window_for(const QWidget *); // qwidget_mac.cpp
       
   111         macWindowToolbarShow(q->parentWidget(), true);
       
   112     }
       
   113 #endif
       
   114 
       
   115     toggleViewAction = new QAction(q);
       
   116     toggleViewAction->setCheckable(true);
       
   117     q->setMovable(q->style()->styleHint(QStyle::SH_ToolBar_Movable, 0, q ));
       
   118     QObject::connect(toggleViewAction, SIGNAL(triggered(bool)), q, SLOT(_q_toggleView(bool)));
       
   119 }
       
   120 
       
   121 void QToolBarPrivate::_q_toggleView(bool b)
       
   122 {
       
   123     Q_Q(QToolBar);
       
   124     if (b == q->isHidden()) {
       
   125         if (b)
       
   126             q->show();
       
   127         else
       
   128             q->close();
       
   129     }
       
   130 }
       
   131 
       
   132 void QToolBarPrivate::_q_updateIconSize(const QSize &sz)
       
   133 {
       
   134     Q_Q(QToolBar);
       
   135     if (!explicitIconSize) {
       
   136         // iconSize not explicitly set
       
   137         q->setIconSize(sz);
       
   138         explicitIconSize = false;
       
   139     }
       
   140 }
       
   141 
       
   142 void QToolBarPrivate::_q_updateToolButtonStyle(Qt::ToolButtonStyle style)
       
   143 {
       
   144     Q_Q(QToolBar);
       
   145     if (!explicitToolButtonStyle) {
       
   146         q->setToolButtonStyle(style);
       
   147         explicitToolButtonStyle = false;
       
   148     }
       
   149 }
       
   150 
       
   151 void QToolBarPrivate::updateWindowFlags(bool floating, bool unplug)
       
   152 {
       
   153     Q_Q(QToolBar);
       
   154     Qt::WindowFlags flags = floating ? Qt::Tool : Qt::Widget;
       
   155 
       
   156     flags |= Qt::FramelessWindowHint;
       
   157 
       
   158     if (unplug) {
       
   159         flags |= Qt::X11BypassWindowManagerHint;
       
   160 #ifdef Q_WS_MAC
       
   161         flags |= Qt::WindowStaysOnTopHint;
       
   162 #endif
       
   163     }
       
   164 
       
   165     q->setWindowFlags(flags);
       
   166 }
       
   167 
       
   168 void QToolBarPrivate::setWindowState(bool floating, bool unplug, const QRect &rect)
       
   169 {
       
   170     Q_Q(QToolBar);
       
   171     bool visible = !q->isHidden();
       
   172     bool wasFloating = q->isFloating(); // ...is also currently using popup menus
       
   173 
       
   174     q->hide();
       
   175 
       
   176     updateWindowFlags(floating, unplug);
       
   177 
       
   178     if (floating != wasFloating)
       
   179         layout->checkUsePopupMenu();
       
   180 
       
   181     if (!rect.isNull())
       
   182         q->setGeometry(rect);
       
   183 
       
   184     if (visible)
       
   185         q->show();
       
   186 }
       
   187 
       
   188 void QToolBarPrivate::initDrag(const QPoint &pos)
       
   189 {
       
   190     Q_Q(QToolBar);
       
   191 
       
   192     if (state != 0)
       
   193         return;
       
   194 
       
   195     QMainWindow *win = qobject_cast<QMainWindow*>(parent);
       
   196     Q_ASSERT(win != 0);
       
   197     QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout());
       
   198     Q_ASSERT(layout != 0);
       
   199     if (layout->pluggingWidget != 0) // the main window is animating a docking operation
       
   200         return;
       
   201 
       
   202     state = new DragState;
       
   203     state->pressPos = pos;
       
   204     state->dragging = false;
       
   205     state->moving = false;
       
   206     state->widgetItem = 0;
       
   207 
       
   208     if (q->isRightToLeft())
       
   209         state->pressPos = QPoint(q->width() - state->pressPos.x(), state->pressPos.y());
       
   210 }
       
   211 
       
   212 void QToolBarPrivate::startDrag(bool moving)
       
   213 {
       
   214     Q_Q(QToolBar);
       
   215 
       
   216     Q_ASSERT(state != 0);
       
   217 
       
   218     if ((moving && state->moving) || state->dragging)
       
   219         return;
       
   220 
       
   221     QMainWindow *win = qobject_cast<QMainWindow*>(parent);
       
   222     Q_ASSERT(win != 0);
       
   223     QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout());
       
   224     Q_ASSERT(layout != 0);
       
   225 
       
   226     if (!moving) {
       
   227         state->widgetItem = layout->unplug(q);
       
   228 #if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
       
   229         if (q->isWindow()) {
       
   230            setWindowState(true, true); //set it to floating
       
   231         }
       
   232 #endif
       
   233         Q_ASSERT(state->widgetItem != 0);
       
   234     }
       
   235     state->dragging = !moving;
       
   236     state->moving = moving;
       
   237 }
       
   238 
       
   239 void QToolBarPrivate::endDrag()
       
   240 {
       
   241     Q_Q(QToolBar);
       
   242     Q_ASSERT(state != 0);
       
   243 
       
   244     q->releaseMouse();
       
   245 
       
   246     if (state->dragging) {
       
   247         QMainWindowLayout *layout =
       
   248             qobject_cast<QMainWindowLayout *>(q->parentWidget()->layout());
       
   249         Q_ASSERT(layout != 0);
       
   250 
       
   251         if (!layout->plug(state->widgetItem)) {
       
   252             if (q->isFloatable()) {
       
   253                 layout->restore();
       
   254 #if defined(Q_WS_X11) || defined(Q_WS_MAC)
       
   255                 setWindowState(true); // gets rid of the X11BypassWindowManager window flag
       
   256                                       // and activates the resizer
       
   257 #endif
       
   258                 q->activateWindow();
       
   259             } else {
       
   260                 layout->revert(state->widgetItem);
       
   261             }
       
   262         }
       
   263     }
       
   264 
       
   265     delete state;
       
   266     state = 0;
       
   267 }
       
   268 
       
   269 bool QToolBarPrivate::mousePressEvent(QMouseEvent *event)
       
   270 {
       
   271     Q_Q(QToolBar);
       
   272     QStyleOptionToolBar opt;
       
   273     q->initStyleOption(&opt);
       
   274     if (q->style()->subElementRect(QStyle::SE_ToolBarHandle, &opt, q).contains(event->pos()) == false) {
       
   275 #ifdef Q_WS_MAC
       
   276         // When using the unified toolbar on Mac OS X the user can can click and
       
   277         // drag between toolbar contents to move the window. Make this work by
       
   278         // implementing the standard mouse-dragging code and then call
       
   279         // window->move() in mouseMoveEvent below.
       
   280         if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parent)) {
       
   281             if (mainWindow->toolBarArea(q) == Qt::TopToolBarArea
       
   282                     && mainWindow->unifiedTitleAndToolBarOnMac()
       
   283                     && q->childAt(event->pos()) == 0) {
       
   284                 macWindowDragging = true;
       
   285                 macWindowDragPressPosition = event->pos();
       
   286                 return true;
       
   287             }
       
   288         }
       
   289 #endif
       
   290         return false;
       
   291     }
       
   292 
       
   293     if (event->button() != Qt::LeftButton)
       
   294         return true;
       
   295 
       
   296     if (!layout->movable())
       
   297         return true;
       
   298 
       
   299     initDrag(event->pos());
       
   300     return true;
       
   301 }
       
   302 
       
   303 bool QToolBarPrivate::mouseReleaseEvent(QMouseEvent*)
       
   304 {
       
   305     if (state != 0) {
       
   306         endDrag();
       
   307         return true;
       
   308     } else {
       
   309 #ifdef Q_WS_MAC
       
   310         if (!macWindowDragging)
       
   311             return false;
       
   312         macWindowDragging = false;
       
   313         macWindowDragPressPosition = QPoint();
       
   314         return true;
       
   315 #endif
       
   316         return false;
       
   317     }
       
   318 }
       
   319 
       
   320 bool QToolBarPrivate::mouseMoveEvent(QMouseEvent *event)
       
   321 {
       
   322     Q_Q(QToolBar);
       
   323 
       
   324     if (!state) {
       
   325 #ifdef Q_WS_MAC
       
   326         if (!macWindowDragging)
       
   327             return false;
       
   328         QWidget *w = q->window();
       
   329         const QPoint delta = event->pos() - macWindowDragPressPosition;
       
   330         w->move(w->pos() + delta);
       
   331         return true;
       
   332 #endif
       
   333         return false;
       
   334     }
       
   335 
       
   336     QMainWindow *win = qobject_cast<QMainWindow*>(parent);
       
   337     if (win == 0)
       
   338         return true;
       
   339 
       
   340     QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout());
       
   341     Q_ASSERT(layout != 0);
       
   342 
       
   343     if (layout->pluggingWidget == 0
       
   344         && (event->pos() - state->pressPos).manhattanLength() > QApplication::startDragDistance()) {
       
   345             const bool wasDragging = state->dragging;
       
   346             const bool moving = !q->isWindow() && (orientation == Qt::Vertical ?
       
   347                 event->x() >= 0 && event->x() < q->width() :
       
   348                 event->y() >= 0 && event->y() < q->height());
       
   349 
       
   350             startDrag(moving);
       
   351             if (!moving && !wasDragging) {
       
   352 #ifdef Q_OS_WIN
       
   353                 grabMouseWhileInWindow();
       
   354 #else
       
   355                 q->grabMouse();
       
   356 #endif
       
   357             }
       
   358     }
       
   359 
       
   360     if (state->dragging) {
       
   361         QPoint pos = event->globalPos();
       
   362         // if we are right-to-left, we move so as to keep the right edge the same distance
       
   363         // from the mouse
       
   364         if (q->isLeftToRight())
       
   365             pos -= state->pressPos;
       
   366         else
       
   367             pos += QPoint(state->pressPos.x() - q->width(), -state->pressPos.y());
       
   368 
       
   369         q->move(pos);
       
   370         layout->hover(state->widgetItem, event->globalPos());
       
   371     } else if (state->moving) {
       
   372 
       
   373         const QPoint rtl(q->width() - state->pressPos.x(), state->pressPos.y()); //for RTL
       
   374         const QPoint globalPressPos = q->mapToGlobal(q->isRightToLeft() ? rtl : state->pressPos);
       
   375         int pos = 0;
       
   376 
       
   377         QPoint delta = event->globalPos() - globalPressPos;
       
   378         if (orientation == Qt::Vertical) {
       
   379             pos = q->y() + delta.y();
       
   380         } else {
       
   381             if (q->isRightToLeft()) {
       
   382                 pos = win->width() - q->width() - q->x()  - delta.x();
       
   383             } else {
       
   384                 pos = q->x() + delta.x();
       
   385             }
       
   386         }
       
   387 
       
   388         layout->moveToolBar(q, pos);
       
   389     }
       
   390     return true;
       
   391 }
       
   392 
       
   393 void QToolBarPrivate::unplug(const QRect &_r)
       
   394 {
       
   395     Q_Q(QToolBar);
       
   396     layout->setExpanded(false, false);
       
   397     QRect r = _r;
       
   398     r.moveTopLeft(q->mapToGlobal(QPoint(0, 0)));
       
   399     setWindowState(true, true, r);
       
   400 }
       
   401 
       
   402 void QToolBarPrivate::plug(const QRect &r)
       
   403 {
       
   404     setWindowState(false, false, r);
       
   405 }
       
   406 
       
   407 /******************************************************************************
       
   408 ** QToolBar
       
   409 */
       
   410 
       
   411 /*!
       
   412     \class QToolBar
       
   413 
       
   414     \brief The QToolBar class provides a movable panel that contains a
       
   415     set of controls.
       
   416 
       
   417     \ingroup mainwindow-classes
       
   418 
       
   419 
       
   420     Toolbar buttons are added by adding \e actions, using addAction()
       
   421     or insertAction(). Groups of buttons can be separated using
       
   422     addSeparator() or insertSeparator(). If a toolbar button is not
       
   423     appropriate, a widget can be inserted instead using addWidget() or
       
   424     insertWidget(); examples of suitable widgets are QSpinBox,
       
   425     QDoubleSpinBox, and QComboBox. When a toolbar button is pressed it
       
   426     emits the actionTriggered() signal.
       
   427 
       
   428     A toolbar can be fixed in place in a particular area (e.g. at the
       
   429     top of the window), or it can be movable (isMovable()) between
       
   430     toolbar areas; see allowedAreas() and isAreaAllowed().
       
   431 
       
   432     When a toolbar is resized in such a way that it is too small to
       
   433     show all the items it contains, an extension button will appear as
       
   434     the last item in the toolbar. Pressing the extension button will
       
   435     pop up a menu containing the items that does not currently fit in
       
   436     the toolbar.
       
   437 
       
   438     When a QToolBar is not a child of a QMainWindow, it looses the ability
       
   439     to populate the extension pop up with widgets added to the toolbar using
       
   440     addWidget(). Please use widget actions created by inheriting QWidgetAction
       
   441     and implementing QWidgetAction::createWidget() instead. This is a known
       
   442     issue which will be fixed in a future release.
       
   443 
       
   444     \sa QToolButton, QMenu, QAction, {Application Example}
       
   445 */
       
   446 
       
   447 /*!
       
   448     \fn bool QToolBar::isAreaAllowed(Qt::ToolBarArea area) const
       
   449 
       
   450     Returns true if this toolbar is dockable in the given \a area;
       
   451     otherwise returns false.
       
   452 */
       
   453 
       
   454 /*!
       
   455     \fn void QToolBar::addAction(QAction *action)
       
   456     \overload
       
   457 
       
   458     Appends the action \a action to the toolbar's list of actions.
       
   459 
       
   460     \sa QMenu::addAction(), QWidget::addAction()
       
   461 */
       
   462 
       
   463 /*!
       
   464     \fn void QToolBar::actionTriggered(QAction *action)
       
   465 
       
   466     This signal is emitted when an action in this toolbar is triggered.
       
   467     This happens when the action's tool button is pressed, or when the
       
   468     action is triggered in some other way outside the tool bar. The parameter
       
   469     holds the triggered \a action.
       
   470 */
       
   471 
       
   472 /*!
       
   473     \fn void QToolBar::allowedAreasChanged(Qt::ToolBarAreas allowedAreas)
       
   474 
       
   475     This signal is emitted when the collection of allowed areas for the
       
   476     toolbar is changed. The new areas in which the toolbar can be positioned
       
   477     are specified by \a allowedAreas.
       
   478 
       
   479     \sa allowedAreas
       
   480 */
       
   481 
       
   482 /*!
       
   483     \fn void QToolBar::iconSizeChanged(const QSize &iconSize)
       
   484 
       
   485     This signal is emitted when the icon size is changed.  The \a
       
   486     iconSize parameter holds the toolbar's new icon size.
       
   487 
       
   488     \sa iconSize QMainWindow::iconSize
       
   489 */
       
   490 
       
   491 /*!
       
   492     \fn void QToolBar::movableChanged(bool movable)
       
   493 
       
   494     This signal is emitted when the toolbar becomes movable or fixed.
       
   495     If the toolbar can be moved, \a movable is true; otherwise it is
       
   496     false.
       
   497 
       
   498     \sa movable
       
   499 */
       
   500 
       
   501 /*!
       
   502     \fn void QToolBar::orientationChanged(Qt::Orientation orientation)
       
   503 
       
   504     This signal is emitted when the orientation of the toolbar changes.
       
   505     The new orientation is specified by the \a orientation given.
       
   506 
       
   507     \sa orientation
       
   508 */
       
   509 
       
   510 /*!
       
   511     \fn void QToolBar::toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)
       
   512 
       
   513     This signal is emitted when the tool button style is changed. The
       
   514     \a toolButtonStyle parameter holds the toolbar's new tool button
       
   515     style.
       
   516 
       
   517     \sa toolButtonStyle QMainWindow::toolButtonStyle
       
   518 */
       
   519 
       
   520 /*!
       
   521     Constructs a QToolBar with the given \a parent.
       
   522 */
       
   523 QToolBar::QToolBar(QWidget *parent)
       
   524     : QWidget(*new QToolBarPrivate, parent, 0)
       
   525 {
       
   526     Q_D(QToolBar);
       
   527     d->init();
       
   528 }
       
   529 
       
   530 /*!
       
   531     Constructs a QToolBar with the given \a parent.
       
   532 
       
   533     The given window \a title identifies the toolbar and is shown in
       
   534     the context menu provided by QMainWindow.
       
   535 
       
   536     \sa setWindowTitle()
       
   537 */
       
   538 QToolBar::QToolBar(const QString &title, QWidget *parent)
       
   539     : QWidget(*new QToolBarPrivate, parent, 0)
       
   540 {
       
   541     Q_D(QToolBar);
       
   542     d->init();
       
   543     setWindowTitle(title);
       
   544 }
       
   545 
       
   546 #ifdef QT3_SUPPORT
       
   547 /*! \obsolete
       
   548     Constructs a QToolBar with the given \a parent and \a name.
       
   549 */
       
   550 QToolBar::QToolBar(QWidget *parent, const char *name)
       
   551     : QWidget(*new QToolBarPrivate, parent, 0)
       
   552 {
       
   553     Q_D(QToolBar);
       
   554     d->init();
       
   555     setObjectName(QString::fromAscii(name));
       
   556 }
       
   557 #endif
       
   558 
       
   559 /*!
       
   560     Destroys the toolbar.
       
   561 */
       
   562 QToolBar::~QToolBar()
       
   563 {
       
   564     // Remove the toolbar button if there is nothing left.
       
   565     QMainWindow *mainwindow = qobject_cast<QMainWindow *>(parentWidget());
       
   566     if (mainwindow) {
       
   567 #ifdef Q_WS_MAC
       
   568         QMainWindowLayout *mainwin_layout = qobject_cast<QMainWindowLayout *>(mainwindow->layout());
       
   569         if (mainwin_layout && mainwin_layout->layoutState.toolBarAreaLayout.isEmpty()
       
   570                 && mainwindow->testAttribute(Qt::WA_WState_Created))
       
   571             macWindowToolbarShow(mainwindow, false);
       
   572 #endif
       
   573     }
       
   574 }
       
   575 
       
   576 /*! \property QToolBar::movable
       
   577     \brief whether the user can move the toolbar within the toolbar area,
       
   578     or between toolbar areas
       
   579 
       
   580     By default, this property is true.
       
   581 
       
   582     This property only makes sense if the toolbar is in a
       
   583     QMainWindow.
       
   584 
       
   585     \sa allowedAreas
       
   586 */
       
   587 
       
   588 void QToolBar::setMovable(bool movable)
       
   589 {
       
   590     Q_D(QToolBar);
       
   591     if (!movable == !d->movable)
       
   592         return;
       
   593     d->movable = movable;
       
   594     d->layout->invalidate();
       
   595     emit movableChanged(d->movable);
       
   596 }
       
   597 
       
   598 bool QToolBar::isMovable() const
       
   599 {
       
   600     Q_D(const QToolBar);
       
   601     return d->movable;
       
   602 }
       
   603 
       
   604 /*!
       
   605     \property QToolBar::floatable
       
   606     \brief whether the toolbar can be dragged and dropped as an independent window.
       
   607 
       
   608     The default is true.
       
   609 */
       
   610 bool QToolBar::isFloatable() const
       
   611 {
       
   612     Q_D(const QToolBar);
       
   613     return d->floatable;
       
   614 }
       
   615 
       
   616 void QToolBar::setFloatable(bool floatable)
       
   617 {
       
   618     Q_D(QToolBar);
       
   619     d->floatable = floatable;
       
   620 }
       
   621 
       
   622 /*!
       
   623     \property QToolBar::floating
       
   624     \brief whether the toolbar is an independent window.
       
   625 
       
   626     By default, this property is true.
       
   627 
       
   628     \sa QWidget::isWindow()
       
   629 */
       
   630 bool QToolBar::isFloating() const
       
   631 {
       
   632     return isWindow();
       
   633 }
       
   634 
       
   635 /*!
       
   636     \property QToolBar::allowedAreas
       
   637     \brief areas where the toolbar may be placed
       
   638 
       
   639     The default is Qt::AllToolBarAreas.
       
   640 
       
   641     This property only makes sense if the toolbar is in a
       
   642     QMainWindow.
       
   643 
       
   644     \sa movable
       
   645 */
       
   646 
       
   647 void QToolBar::setAllowedAreas(Qt::ToolBarAreas areas)
       
   648 {
       
   649     Q_D(QToolBar);
       
   650     areas &= Qt::ToolBarArea_Mask;
       
   651     if (areas == d->allowedAreas)
       
   652         return;
       
   653     d->allowedAreas = areas;
       
   654     emit allowedAreasChanged(d->allowedAreas);
       
   655 }
       
   656 
       
   657 Qt::ToolBarAreas QToolBar::allowedAreas() const
       
   658 {
       
   659     Q_D(const QToolBar);
       
   660 #ifdef Q_WS_MAC
       
   661     if (QMainWindow *window = qobject_cast<QMainWindow *>(parentWidget())) {
       
   662         if (window->unifiedTitleAndToolBarOnMac()) // Don't allow drags to the top (for now).
       
   663             return (d->allowedAreas & ~Qt::TopToolBarArea);
       
   664     }
       
   665 #endif
       
   666     return d->allowedAreas;
       
   667 }
       
   668 
       
   669 /*! \property QToolBar::orientation
       
   670     \brief orientation of the toolbar
       
   671 
       
   672     The default is Qt::Horizontal.
       
   673 
       
   674     This function should not be used when the toolbar is managed
       
   675     by QMainWindow. You can use QMainWindow::addToolBar() or
       
   676     QMainWindow::insertToolBar() if you wish to move a toolbar (that
       
   677     is already added to a main window) to another Qt::ToolBarArea.
       
   678 */
       
   679 
       
   680 void QToolBar::setOrientation(Qt::Orientation orientation)
       
   681 {
       
   682     Q_D(QToolBar);
       
   683     if (orientation == d->orientation)
       
   684         return;
       
   685 
       
   686     d->orientation = orientation;
       
   687 
       
   688     if (orientation == Qt::Vertical)
       
   689  	setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
   690     else
       
   691  	setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
   692 
       
   693     d->layout->invalidate();
       
   694     d->layout->activate();
       
   695 
       
   696     emit orientationChanged(d->orientation);
       
   697 }
       
   698 
       
   699 Qt::Orientation QToolBar::orientation() const
       
   700 { Q_D(const QToolBar); return d->orientation; }
       
   701 
       
   702 /*!
       
   703     \property QToolBar::iconSize
       
   704     \brief size of icons in the toolbar.
       
   705 
       
   706     The default size is determined by the application's style and is
       
   707     derived from the QStyle::PM_ToolBarIconSize pixel metric. It is
       
   708     the maximum size an icon can have. Icons of smaller size will not
       
   709     be scaled up.
       
   710 */
       
   711 
       
   712 QSize QToolBar::iconSize() const
       
   713 { Q_D(const QToolBar); return d->iconSize; }
       
   714 
       
   715 void QToolBar::setIconSize(const QSize &iconSize)
       
   716 {
       
   717     Q_D(QToolBar);
       
   718     QSize sz = iconSize;
       
   719     if (!sz.isValid()) {
       
   720         QMainWindow *mw = qobject_cast<QMainWindow *>(parentWidget());
       
   721         if (mw && mw->layout()) {
       
   722             QLayout *layout = mw->layout();
       
   723             int i = 0;
       
   724             QLayoutItem *item = 0;
       
   725             do {
       
   726                 item = layout->itemAt(i++);
       
   727                 if (item && (item->widget() == this))
       
   728                     sz = mw->iconSize();
       
   729             } while (!sz.isValid() && item != 0);
       
   730         }
       
   731     }
       
   732     if (!sz.isValid()) {
       
   733         const int metric = style()->pixelMetric(QStyle::PM_ToolBarIconSize, 0, this);
       
   734         sz = QSize(metric, metric);
       
   735     }
       
   736     if (d->iconSize != sz) {
       
   737         d->iconSize = sz;
       
   738         setMinimumSize(0, 0);
       
   739         emit iconSizeChanged(d->iconSize);
       
   740     }
       
   741     d->explicitIconSize = iconSize.isValid();
       
   742 
       
   743     d->layout->invalidate();
       
   744 }
       
   745 
       
   746 /*!
       
   747     \property QToolBar::toolButtonStyle
       
   748     \brief the style of toolbar buttons
       
   749 
       
   750     This property defines the style of all tool buttons that are added
       
   751     as \l{QAction}s. Note that if you add a QToolButton with the
       
   752     addWidget() method, it will not get this button style.
       
   753 
       
   754     The default is Qt::ToolButtonIconOnly.
       
   755 */
       
   756 
       
   757 Qt::ToolButtonStyle QToolBar::toolButtonStyle() const
       
   758 { Q_D(const QToolBar); return d->toolButtonStyle; }
       
   759 
       
   760 void QToolBar::setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
       
   761 {
       
   762     Q_D(QToolBar);
       
   763     d->explicitToolButtonStyle = true;
       
   764     if (d->toolButtonStyle == toolButtonStyle)
       
   765         return;
       
   766     d->toolButtonStyle = toolButtonStyle;
       
   767     setMinimumSize(0, 0);
       
   768     emit toolButtonStyleChanged(d->toolButtonStyle);
       
   769 }
       
   770 
       
   771 /*!
       
   772     Removes all actions from the toolbar.
       
   773 
       
   774     \sa removeAction()
       
   775 */
       
   776 void QToolBar::clear()
       
   777 {
       
   778     QList<QAction *> actions = this->actions();
       
   779     for(int i = 0; i < actions.size(); i++)
       
   780         removeAction(actions.at(i));
       
   781 }
       
   782 
       
   783 /*!
       
   784     \overload
       
   785 
       
   786     Creates a new action with the given \a text. This action is added to
       
   787     the end of the toolbar.
       
   788 */
       
   789 QAction *QToolBar::addAction(const QString &text)
       
   790 {
       
   791     QAction *action = new QAction(text, this);
       
   792     addAction(action);
       
   793     return action;
       
   794 }
       
   795 
       
   796 /*!
       
   797     \overload
       
   798 
       
   799     Creates a new action with the given \a icon and \a text. This
       
   800     action is added to the end of the toolbar.
       
   801 */
       
   802 QAction *QToolBar::addAction(const QIcon &icon, const QString &text)
       
   803 {
       
   804     QAction *action = new QAction(icon, text, this);
       
   805     addAction(action);
       
   806     return action;
       
   807 }
       
   808 
       
   809 /*!
       
   810     \overload
       
   811 
       
   812     Creates a new action with the given \a text. This action is added to
       
   813     the end of the toolbar. The action's \link QAction::triggered()
       
   814     triggered()\endlink signal is connected to \a member in \a
       
   815     receiver.
       
   816 */
       
   817 QAction *QToolBar::addAction(const QString &text,
       
   818                              const QObject *receiver, const char* member)
       
   819 {
       
   820     QAction *action = new QAction(text, this);
       
   821     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
       
   822     addAction(action);
       
   823     return action;
       
   824 }
       
   825 
       
   826 /*!
       
   827     \overload
       
   828 
       
   829     Creates a new action with the icon \a icon and text \a text. This
       
   830     action is added to the end of the toolbar. The action's \link
       
   831     QAction::triggered() triggered()\endlink signal is connected to \a
       
   832     member in \a receiver.
       
   833 */
       
   834 QAction *QToolBar::addAction(const QIcon &icon, const QString &text,
       
   835                              const QObject *receiver, const char* member)
       
   836 {
       
   837     QAction *action = new QAction(icon, text, this);
       
   838     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
       
   839     addAction(action);
       
   840     return action;
       
   841 }
       
   842 
       
   843 /*!
       
   844      Adds a separator to the end of the toolbar.
       
   845 
       
   846      \sa insertSeparator()
       
   847 */
       
   848 QAction *QToolBar::addSeparator()
       
   849 {
       
   850     QAction *action = new QAction(this);
       
   851     action->setSeparator(true);
       
   852     addAction(action);
       
   853     return action;
       
   854 }
       
   855 
       
   856 /*!
       
   857     Inserts a separator into the toolbar in front of the toolbar
       
   858     item associated with the \a before action.
       
   859 
       
   860     \sa addSeparator()
       
   861 */
       
   862 QAction *QToolBar::insertSeparator(QAction *before)
       
   863 {
       
   864     QAction *action = new QAction(this);
       
   865     action->setSeparator(true);
       
   866     insertAction(before, action);
       
   867     return action;
       
   868 }
       
   869 
       
   870 /*!
       
   871     Adds the given \a widget to the toolbar as the toolbar's last
       
   872     item.
       
   873 
       
   874     The toolbar takes ownership of \a widget.
       
   875 
       
   876     If you add a QToolButton with this method, the tools bar's
       
   877     Qt::ToolButtonStyle will not be respected.
       
   878 
       
   879     \note You should use QAction::setVisible() to change the
       
   880     visibility of the widget. Using QWidget::setVisible(),
       
   881     QWidget::show() and QWidget::hide() does not work.
       
   882 
       
   883     \sa insertWidget()
       
   884 */
       
   885 QAction *QToolBar::addWidget(QWidget *widget)
       
   886 {
       
   887     QWidgetAction *action = new QWidgetAction(this);
       
   888     action->setDefaultWidget(widget);
       
   889     action->d_func()->autoCreated = true;
       
   890     addAction(action);
       
   891     return action;
       
   892 }
       
   893 
       
   894 /*!
       
   895     Inserts the given \a widget in front of the toolbar item
       
   896     associated with the \a before action.
       
   897 
       
   898     Note: You should use QAction::setVisible() to change the
       
   899     visibility of the widget. Using QWidget::setVisible(),
       
   900     QWidget::show() and QWidget::hide() does not work.
       
   901 
       
   902     \sa addWidget()
       
   903 */
       
   904 QAction *QToolBar::insertWidget(QAction *before, QWidget *widget)
       
   905 {
       
   906     QWidgetAction *action = new QWidgetAction(this);
       
   907     action->setDefaultWidget(widget);
       
   908     action->d_func()->autoCreated = true;
       
   909     insertAction(before, action);
       
   910     return action;
       
   911 }
       
   912 
       
   913 /*!
       
   914     \internal
       
   915 
       
   916     Returns the geometry of the toolbar item associated with the given
       
   917     \a action, or an invalid QRect if no matching item is found.
       
   918 */
       
   919 QRect QToolBar::actionGeometry(QAction *action) const
       
   920 {
       
   921     Q_D(const QToolBar);
       
   922 
       
   923     int index = d->layout->indexOf(action);
       
   924     if (index == -1)
       
   925         return QRect();
       
   926     return d->layout->itemAt(index)->widget()->geometry();
       
   927 }
       
   928 
       
   929 /*!
       
   930     Returns the action at point \a p. This function returns zero if no
       
   931     action was found.
       
   932 
       
   933     \sa QWidget::childAt()
       
   934 */
       
   935 QAction *QToolBar::actionAt(const QPoint &p) const
       
   936 {
       
   937     Q_D(const QToolBar);
       
   938     QWidget *widget = childAt(p);
       
   939     int index = d->layout->indexOf(widget);
       
   940     if (index == -1)
       
   941         return 0;
       
   942     QLayoutItem *item = d->layout->itemAt(index);
       
   943     return static_cast<QToolBarItem*>(item)->action;
       
   944 }
       
   945 
       
   946 /*! \fn QAction *QToolBar::actionAt(int x, int y) const
       
   947     \overload
       
   948 
       
   949     Returns the action at the point \a x, \a y. This function returns
       
   950     zero if no action was found.
       
   951 */
       
   952 
       
   953 /*! \reimp */
       
   954 void QToolBar::actionEvent(QActionEvent *event)
       
   955 {
       
   956     Q_D(QToolBar);
       
   957     QAction *action = event->action();
       
   958     QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action);
       
   959 
       
   960     switch (event->type()) {
       
   961         case QEvent::ActionAdded: {
       
   962             Q_ASSERT_X(widgetAction == 0 || d->layout->indexOf(widgetAction) == -1,
       
   963                         "QToolBar", "widgets cannot be inserted multiple times");
       
   964 
       
   965             // reparent the action to this toolbar if it has been created
       
   966             // using the addAction(text) etc. convenience functions, to
       
   967             // preserve Qt 4.1.x behavior. The widget is already
       
   968             // reparented to us due to the createWidget call inside
       
   969             // createItem()
       
   970             if (widgetAction != 0 && widgetAction->d_func()->autoCreated)
       
   971                 widgetAction->setParent(this);
       
   972 
       
   973             int index = d->layout->count();
       
   974             if (event->before()) {
       
   975                 index = d->layout->indexOf(event->before());
       
   976                 Q_ASSERT_X(index != -1, "QToolBar::insertAction", "internal error");
       
   977             }
       
   978             d->layout->insertAction(index, action);
       
   979             break;
       
   980         }
       
   981 
       
   982         case QEvent::ActionChanged:
       
   983             d->layout->invalidate();
       
   984             break;
       
   985 
       
   986         case QEvent::ActionRemoved: {
       
   987             int index = d->layout->indexOf(action);
       
   988             if (index != -1) {
       
   989                 delete d->layout->takeAt(index);
       
   990             }
       
   991             break;
       
   992         }
       
   993 
       
   994         default:
       
   995             Q_ASSERT_X(false, "QToolBar::actionEvent", "internal error");
       
   996     }
       
   997 }
       
   998 
       
   999 /*! \reimp */
       
  1000 void QToolBar::changeEvent(QEvent *event)
       
  1001 {
       
  1002     Q_D(QToolBar);
       
  1003     switch (event->type()) {
       
  1004     case QEvent::WindowTitleChange:
       
  1005         d->toggleViewAction->setText(windowTitle());
       
  1006         break;
       
  1007     case QEvent::StyleChange:
       
  1008         d->layout->invalidate();
       
  1009         if (!d->explicitIconSize)
       
  1010             setIconSize(QSize());
       
  1011         d->layout->updateMarginAndSpacing();
       
  1012         break;
       
  1013     case QEvent::LayoutDirectionChange:
       
  1014         d->layout->invalidate();
       
  1015         break;
       
  1016     default:
       
  1017         break;
       
  1018     }
       
  1019     QWidget::changeEvent(event);
       
  1020 }
       
  1021 
       
  1022 /*! \reimp */
       
  1023 void QToolBar::paintEvent(QPaintEvent *)
       
  1024 {
       
  1025     Q_D(QToolBar);
       
  1026 
       
  1027     QPainter p(this);
       
  1028     QStyle *style = this->style();
       
  1029     QStyleOptionToolBar opt;
       
  1030     initStyleOption(&opt);
       
  1031 
       
  1032     if (d->layout->expanded || d->layout->animating || isWindow()) {
       
  1033         //if the toolbar is expended, we need to fill the background with the window color
       
  1034         //because some styles may expects that.
       
  1035         p.fillRect(opt.rect, palette().background());
       
  1036         style->drawControl(QStyle::CE_ToolBar, &opt, &p, this);
       
  1037         style->drawPrimitive(QStyle::PE_FrameMenu, &opt, &p, this);
       
  1038     } else {
       
  1039         style->drawControl(QStyle::CE_ToolBar, &opt, &p, this);
       
  1040     }
       
  1041 
       
  1042     opt.rect = style->subElementRect(QStyle::SE_ToolBarHandle, &opt, this);
       
  1043     if (opt.rect.isValid())
       
  1044         style->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &opt, &p, this);
       
  1045 }
       
  1046 
       
  1047 /*
       
  1048     Checks if an expanded toolbar has to wait for this popup to close before
       
  1049     the toolbar collapses. This is true if
       
  1050     1) the popup has the toolbar in its parent chain,
       
  1051     2) the popup is a menu whose menuAction is somewhere in the toolbar.
       
  1052 */
       
  1053 static bool waitForPopup(QToolBar *tb, QWidget *popup)
       
  1054 {
       
  1055     if (popup == 0 || popup->isHidden())
       
  1056         return false;
       
  1057 
       
  1058     QWidget *w = popup;
       
  1059     while (w != 0) {
       
  1060         if (w == tb)
       
  1061             return true;
       
  1062         w = w->parentWidget();
       
  1063     }
       
  1064 
       
  1065     QMenu *menu = qobject_cast<QMenu*>(popup);
       
  1066     if (menu == 0)
       
  1067         return false;
       
  1068 
       
  1069     QAction *action = menu->menuAction();
       
  1070     QList<QWidget*> widgets = action->associatedWidgets();
       
  1071     for (int i = 0; i < widgets.count(); ++i) {
       
  1072         if (waitForPopup(tb, widgets.at(i)))
       
  1073             return true;
       
  1074     }
       
  1075 
       
  1076     return false;
       
  1077 }
       
  1078 
       
  1079 #if defined(Q_WS_MAC)
       
  1080 static bool toolbarInUnifiedToolBar(QToolBar *toolbar)
       
  1081 {
       
  1082     const QMainWindow *mainWindow = qobject_cast<const QMainWindow *>(toolbar->parentWidget());
       
  1083     return mainWindow && mainWindow->unifiedTitleAndToolBarOnMac()
       
  1084             && mainWindow->toolBarArea(toolbar) == Qt::TopToolBarArea;
       
  1085 }
       
  1086 #endif
       
  1087 
       
  1088 /*! \reimp */
       
  1089 bool QToolBar::event(QEvent *event)
       
  1090 {
       
  1091     Q_D(QToolBar);
       
  1092 
       
  1093     switch (event->type()) {
       
  1094     case QEvent::Timer:
       
  1095         if (d->waitForPopupTimer.timerId() == static_cast<QTimerEvent*>(event)->timerId()) {
       
  1096             QWidget *w = QApplication::activePopupWidget();
       
  1097             if (!waitForPopup(this, w)) {
       
  1098                 d->waitForPopupTimer.stop();
       
  1099                 if (!this->underMouse())
       
  1100                     d->layout->setExpanded(false);
       
  1101             }
       
  1102         }
       
  1103         break;
       
  1104     case QEvent::Hide:
       
  1105         if (!isHidden())
       
  1106             break;
       
  1107         // fallthrough intended
       
  1108     case QEvent::Show:
       
  1109         d->toggleViewAction->setChecked(event->type() == QEvent::Show);
       
  1110 #if defined(Q_WS_MAC)
       
  1111         if (toolbarInUnifiedToolBar(this)) {
       
  1112              // I can static_cast because I did the qobject_cast in the if above, therefore
       
  1113             // we must have a QMainWindowLayout here.
       
  1114             QMainWindowLayout *mwLayout = static_cast<QMainWindowLayout *>(parentWidget()->layout());
       
  1115             mwLayout->fixSizeInUnifiedToolbar(this);
       
  1116             mwLayout->syncUnifiedToolbarVisibility();
       
  1117         }
       
  1118 #  if !defined(QT_MAC_USE_COCOA)
       
  1119         // Fall through
       
  1120     case QEvent::LayoutRequest: {
       
  1121         // There's currently no way to invalidate the size and let
       
  1122         // HIToolbar know about it. This forces a re-check.
       
  1123         int earlyResult = -1;
       
  1124         if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget())) {
       
  1125             bool needUpdate = true;
       
  1126             if (event->type() == QEvent::LayoutRequest) {
       
  1127                 QSize oldSizeHint = sizeHint();
       
  1128                 earlyResult = QWidget::event(event) ? 1 : 0;
       
  1129                 needUpdate = oldSizeHint != sizeHint();
       
  1130             }
       
  1131 
       
  1132             if (needUpdate) {
       
  1133                 OSWindowRef windowRef = qt_mac_window_for(mainWindow);
       
  1134                 if (toolbarInUnifiedToolBar(this)
       
  1135                         && macWindowToolbarIsVisible(windowRef))   {
       
  1136                     DisableScreenUpdates();
       
  1137                     macWindowToolbarShow(this, false);
       
  1138                     macWindowToolbarShow(this, true);
       
  1139                     EnableScreenUpdates();
       
  1140                 }
       
  1141             }
       
  1142 
       
  1143             if (earlyResult != -1)
       
  1144                 return earlyResult;
       
  1145         }
       
  1146     }
       
  1147 #  endif // !QT_MAC_USE_COCOA
       
  1148 #endif // Q_WS_MAC
       
  1149         break;
       
  1150     case QEvent::ParentChange:
       
  1151         d->layout->checkUsePopupMenu();
       
  1152 #if defined(Q_WS_MAC)
       
  1153         if (parentWidget() && parentWidget()->isWindow())
       
  1154             qt_mac_updateToolBarButtonHint(parentWidget());
       
  1155 #endif
       
  1156         break;
       
  1157 
       
  1158     case QEvent::MouseButtonPress: {
       
  1159         if (d->mousePressEvent(static_cast<QMouseEvent*>(event)))
       
  1160             return true;
       
  1161         break;
       
  1162     }
       
  1163     case QEvent::MouseButtonRelease:
       
  1164         if (d->mouseReleaseEvent(static_cast<QMouseEvent*>(event)))
       
  1165             return true;
       
  1166         break;
       
  1167     case QEvent::HoverEnter:
       
  1168     case QEvent::HoverLeave:
       
  1169         // there's nothing special to do here and we don't want to update the whole widget
       
  1170         return true;
       
  1171     case QEvent::HoverMove: {
       
  1172 #ifndef QT_NO_CURSOR
       
  1173         QHoverEvent *e = static_cast<QHoverEvent*>(event);
       
  1174         QStyleOptionToolBar opt;
       
  1175         initStyleOption(&opt);
       
  1176         if (style()->subElementRect(QStyle::SE_ToolBarHandle, &opt, this).contains(e->pos()))
       
  1177             setCursor(Qt::SizeAllCursor);
       
  1178         else
       
  1179             unsetCursor();
       
  1180 #endif
       
  1181         break;
       
  1182     }
       
  1183     case QEvent::MouseMove:
       
  1184         if (d->mouseMoveEvent(static_cast<QMouseEvent*>(event)))
       
  1185             return true;
       
  1186         break;
       
  1187 #ifdef Q_WS_WINCE
       
  1188     case QEvent::ContextMenu:
       
  1189         {
       
  1190             QContextMenuEvent* contextMenuEvent = static_cast<QContextMenuEvent*>(event);
       
  1191             QWidget* child = childAt(contextMenuEvent->pos());
       
  1192             QAbstractButton* button = qobject_cast<QAbstractButton*>(child);
       
  1193             if (button)
       
  1194                 button->setDown(false);
       
  1195         }
       
  1196         break;
       
  1197 #endif
       
  1198     case QEvent::Leave:
       
  1199         if (d->state != 0 && d->state->dragging) {
       
  1200 #ifdef Q_OS_WIN
       
  1201             // This is a workaround for loosing the mouse on Vista.
       
  1202             QPoint pos = QCursor::pos();
       
  1203             QMouseEvent fake(QEvent::MouseMove, mapFromGlobal(pos), pos, Qt::NoButton,
       
  1204                              QApplication::mouseButtons(), QApplication::keyboardModifiers());
       
  1205             d->mouseMoveEvent(&fake);
       
  1206 #endif
       
  1207         } else {
       
  1208             if (!d->layout->expanded)
       
  1209                 break;
       
  1210 
       
  1211             QWidget *w = QApplication::activePopupWidget();
       
  1212             if (waitForPopup(this, w)) {
       
  1213                 d->waitForPopupTimer.start(POPUP_TIMER_INTERVAL, this);
       
  1214                 break;
       
  1215             }
       
  1216 
       
  1217             d->waitForPopupTimer.stop();
       
  1218             d->layout->setExpanded(false);
       
  1219             break;
       
  1220         }
       
  1221     default:
       
  1222         break;
       
  1223     }
       
  1224     return QWidget::event(event);
       
  1225 }
       
  1226 
       
  1227 /*!
       
  1228     Returns a checkable action that can be used to show or hide this
       
  1229     toolbar.
       
  1230 
       
  1231     The action's text is set to the toolbar's window title.
       
  1232 
       
  1233     \sa QAction::text QWidget::windowTitle
       
  1234 */
       
  1235 QAction *QToolBar::toggleViewAction() const
       
  1236 { Q_D(const QToolBar); return d->toggleViewAction; }
       
  1237 
       
  1238 /*!
       
  1239     \fn void QToolBar::setLabel(const QString &label)
       
  1240 
       
  1241     Use setWindowTitle() instead.
       
  1242 */
       
  1243 
       
  1244 /*!
       
  1245     \fn QString QToolBar::label() const
       
  1246 
       
  1247     Use windowTitle() instead.
       
  1248 */
       
  1249 
       
  1250 /*!
       
  1251     \since 4.2
       
  1252 
       
  1253     Returns the widget associated with the specified \a action.
       
  1254 
       
  1255     \sa addWidget()
       
  1256 */
       
  1257 QWidget *QToolBar::widgetForAction(QAction *action) const
       
  1258 {
       
  1259     Q_D(const QToolBar);
       
  1260 
       
  1261     int index = d->layout->indexOf(action);
       
  1262     if (index == -1)
       
  1263         return 0;
       
  1264 
       
  1265     return d->layout->itemAt(index)->widget();
       
  1266 }
       
  1267 
       
  1268 /*!
       
  1269     \internal
       
  1270 */
       
  1271 void QToolBar::initStyleOption(QStyleOptionToolBar *option) const
       
  1272 {
       
  1273     Q_D(const QToolBar);
       
  1274 
       
  1275     if (!option)
       
  1276         return;
       
  1277 
       
  1278     option->initFrom(this);
       
  1279     if (orientation() == Qt::Horizontal)
       
  1280         option->state |= QStyle::State_Horizontal;
       
  1281     option->lineWidth = style()->pixelMetric(QStyle::PM_ToolBarFrameWidth, 0, this);
       
  1282     option->features = d->layout->movable()
       
  1283                         ? QStyleOptionToolBar::Movable
       
  1284                         : QStyleOptionToolBar::None;
       
  1285     // if the tool bar is not in a QMainWindow, this will make the painting right
       
  1286     option->toolBarArea = Qt::NoToolBarArea;
       
  1287 
       
  1288     // Add more styleoptions if the toolbar has been added to a mainwindow.
       
  1289     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
       
  1290 
       
  1291     if (!mainWindow)
       
  1292         return;
       
  1293 
       
  1294     QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(mainWindow->layout());
       
  1295     Q_ASSERT_X(layout != 0, "QToolBar::initStyleOption()",
       
  1296                "QMainWindow->layout() != QMainWindowLayout");
       
  1297 
       
  1298     layout->getStyleOptionInfo(option, const_cast<QToolBar *>(this));
       
  1299 }
       
  1300 
       
  1301 /*!
       
  1302     \reimp
       
  1303 */
       
  1304 void QToolBar::childEvent(QChildEvent *event) // ### remove me in 5.0
       
  1305 {
       
  1306     QWidget::childEvent(event);
       
  1307 }
       
  1308 
       
  1309 /*!
       
  1310     \reimp
       
  1311 */
       
  1312 void QToolBar::resizeEvent(QResizeEvent *event) // ### remove me in 5.0
       
  1313 {
       
  1314     QWidget::resizeEvent(event);
       
  1315 }
       
  1316 
       
  1317 QT_END_NAMESPACE
       
  1318 
       
  1319 #include "moc_qtoolbar.cpp"
       
  1320 
       
  1321 #endif // QT_NO_TOOLBAR