/*!
    Inserts the given \a widget in front of the toolbar item
    associated with the \a before action.

    Note: You should use QAction::setVisible() to change the
    visibility of the widget. Using QWidget::setVisible(),
    QWidget::show() and QWidget::hide() does not work.

    \sa addWidget()
*/
QAction *QToolBar::insertWidget(QAction *before, QWidget *widget)
{
    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(widget);
    action->d_func()->autoCreated = true;
    insertAction(before, action);
    return action;
}
/*!
    Adds the given \a widget to the toolbar as the toolbar's last
    item.

    The toolbar takes ownership of \a widget.

    If you add a QToolButton with this method, the tools bar's
    Qt::ToolButtonStyle will not be respected.

    \note You should use QAction::setVisible() to change the
    visibility of the widget. Using QWidget::setVisible(),
    QWidget::show() and QWidget::hide() does not work.

    \sa insertWidget()
*/
QAction *QToolBar::addWidget(QWidget *widget)
{
    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(widget);
    action->d_func()->autoCreated = true;
    addAction(action);
    return action;
}
/*! \reimp */
void QToolBar::actionEvent(QActionEvent *event)
{
    Q_D(QToolBar);
    QAction *action = event->action();
    QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action);

    switch (event->type()) {
    case QEvent::ActionAdded: {
        Q_ASSERT_X(widgetAction == 0 || d->layout->indexOf(widgetAction) == -1,
                   "QToolBar", "widgets cannot be inserted multiple times");

        // reparent the action to this toolbar if it has been created
        // using the addAction(text) etc. convenience functions, to
        // preserve Qt 4.1.x behavior. The widget is already
        // reparented to us due to the createWidget call inside
        // createItem()
        if (widgetAction != 0 && widgetAction->d_func()->autoCreated)
            widgetAction->setParent(this);

        int index = d->layout->count();
        if (event->before()) {
            index = d->layout->indexOf(event->before());
            Q_ASSERT_X(index != -1, "QToolBar::insertAction", "internal error");
        }
        d->layout->insertAction(index, action);
        break;
    }

    case QEvent::ActionChanged:
        d->layout->invalidate();
        break;

    case QEvent::ActionRemoved: {
        int index = d->layout->indexOf(action);
        if (index != -1) {
            delete d->layout->takeAt(index);
        }
        break;
    }

    default:
        Q_ASSERT_X(false, "QToolBar::actionEvent", "internal error");
    }
}