int QStackedWidgetProto::indexOf(QWidget *widget) const
{
  QStackedWidget *item = qscriptvalue_cast<QStackedWidget*>(thisObject());
  if (item)
    return item->indexOf(widget);
  return 0;
}
void InsertPageCommand::undo()
{
    QWidget *page = m_form->objectTree()->lookup(m_name)->widget();
    QWidget *parent = m_form->objectTree()->lookup(m_parentname)->widget();

    QWidgetList list;
    list.append(page);
    KFormDesigner::Command *com = new KFormDesigner::DeleteWidgetCommand(*m_form, list);

    QByteArray classname = parent->metaObject()->className();
    if (classname == "KFDTabWidget") {
        TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(parent);
        tab->removeTab(tab->indexOf(page));
    } else if (classname == "QStackedWidget" || /* compat */ classname == "QWidgetStack") {
        QStackedWidget *stack = dynamic_cast<QStackedWidget*>(parent);
        int index = stack->indexOf(page);
        if (index > 0)
            index--;
        else if (index < (stack->count()-1))
            index++;
        else
            index = -1;

        if (index >= 0)
            stack->setCurrentIndex(index);
        stack->removeWidget(page);
    }

    com->execute();
    delete com;
}
void RemoveStackPageAction::slotTriggered()
{
    if (   !KexiUtils::objectIsA(m_receiver, "QStackedWidget")
        && /* compat */ !KexiUtils::objectIsA(m_receiver, "QWidgetStack"))
    {
        return;
    }
    QStackedWidget *stack = qobject_cast<QStackedWidget*>(m_receiver);
    QWidget *page = stack->currentWidget();

    QWidgetList list;
    list.append(page);
    KFormDesigner::Command *com = new KFormDesigner::DeleteWidgetCommand(*m_container->form(), list);

    // raise prev/next widget
    int index = stack->indexOf(page);
    if (index > 0) {
        index--;
    }
    else if (index < (stack->count()-1)) {
        index++;
    }
    else {
        index = -1;
    }
    if (index >= 0) {
        stack->setCurrentIndex(index);
    }
    stack->removeWidget(page);
    m_container->form()->addCommand(com);
}
void
InsertPageCommand::execute()
{
    KFormDesigner::Container *container = m_form->objectTree()->lookup(m_containername)->container();
    QWidget *parent = m_form->objectTree()->lookup(m_parentname)->widget();
    if (m_name.isEmpty()) {
        m_name = container->form()->objectTree()->generateUniqueName(
                     container->form()->library()->displayName("QWidget").toLatin1(),
                     /*!numberSuffixRequired*/false);
    }

    QWidget *page = container->form()->library()->createWidget(
        "QWidget", parent, m_name.toLatin1(), container);
    page->setAutoFillBackground(true);
//    page->setPaletteBackgroundColor(Qt::red);
    KFormDesigner::ObjectTreeItem *item = container->form()->objectTree()->lookup(m_name);

    QByteArray classname = parent->metaObject()->className();
    if (classname == "KFDTabWidget") {
        TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(parent);
        QString n = i18n("Page %1", tab->count() + 1);
        tab->addTab(page, n);
        tab->setCurrentWidget(page);
        item->addModifiedProperty("title", n);
    } else if (classname == "QStackedWidget" || /* compat */ classname == "QWidgetStack") {
        QStackedWidget *stack = dynamic_cast<QStackedWidget*>(parent);
        stack->addWidget(page);
        stack->setCurrentWidget(page);
        item->addModifiedProperty("stackIndex", stack->indexOf(page));
    }
}