tools/designer/src/components/taskmenu/containerwidget_taskmenu.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 Qt Designer 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 "containerwidget_taskmenu.h"
       
    43 
       
    44 #include <QtDesigner/QDesignerFormEditorInterface>
       
    45 #include <QtDesigner/QDesignerFormWindowInterface>
       
    46 #include <QtDesigner/QExtensionManager>
       
    47 #include <QtDesigner/QDesignerContainerExtension>
       
    48 
       
    49 #include <qdesigner_command_p.h>
       
    50 #include <qdesigner_dockwidget_p.h>
       
    51 #include <promotiontaskmenu_p.h>
       
    52 #include <widgetdatabase_p.h>
       
    53 
       
    54 #include <QtGui/QAction>
       
    55 #include <QtGui/QMainWindow>
       
    56 #include <QtGui/QToolBox>
       
    57 #include <QtGui/QStackedWidget>
       
    58 #include <QtGui/QTabWidget>
       
    59 #include <QtGui/QScrollArea>
       
    60 #include <QtGui/QMdiArea>
       
    61 #include <QtGui/QWorkspace>
       
    62 #include <QtGui/QWizard>
       
    63 #include <QtGui/QMenu>
       
    64 
       
    65 #include <QtCore/qdebug.h>
       
    66 
       
    67 QT_BEGIN_NAMESPACE
       
    68 
       
    69 namespace qdesigner_internal {
       
    70 
       
    71 ContainerWidgetTaskMenu::ContainerWidgetTaskMenu(QWidget *widget, ContainerType type, QObject *parent) :
       
    72     QDesignerTaskMenu(widget, parent),
       
    73     m_type(type),
       
    74     m_containerWidget(widget),
       
    75     m_core(formWindow()->core()),
       
    76     m_pagePromotionTaskMenu(new PromotionTaskMenu(0, PromotionTaskMenu::ModeSingleWidget, this)),
       
    77     m_pageMenuAction(new QAction(this)),
       
    78     m_pageMenu(new QMenu),
       
    79     m_actionDeletePage(new QAction(tr("Delete"), this))
       
    80 {
       
    81     Q_ASSERT(m_core);
       
    82     m_taskActions.append(createSeparator());
       
    83 
       
    84     connect(m_actionDeletePage, SIGNAL(triggered()), this, SLOT(removeCurrentPage()));
       
    85 
       
    86     QAction *actionInsertPageAfter = new QAction(this);
       
    87     connect(actionInsertPageAfter, SIGNAL(triggered()), this, SLOT(addPageAfter()));
       
    88     // Empty Per-Page submenu, deletion and promotion. Updated on demand due to promotion state
       
    89     switch (m_type) {
       
    90     case WizardContainer:
       
    91     case PageContainer:
       
    92         m_taskActions.append(createSeparator()); // for the browse actions
       
    93         break;
       
    94     case MdiContainer:
       
    95         break;
       
    96     }
       
    97     // submenu
       
    98     m_pageMenuAction->setMenu(m_pageMenu);
       
    99     m_taskActions.append(m_pageMenuAction);
       
   100     // Insertion
       
   101     switch (m_type) {
       
   102     case WizardContainer:
       
   103     case PageContainer: { // Before and after in a submenu
       
   104         QAction *insertMenuAction = new QAction(tr("Insert"), this);
       
   105         QMenu *insertMenu = new QMenu;
       
   106         // before
       
   107         QAction *actionInsertPage = new QAction(tr("Insert Page Before Current Page"), this);
       
   108         connect(actionInsertPage, SIGNAL(triggered()), this, SLOT(addPage()));
       
   109         insertMenu->addAction(actionInsertPage);
       
   110         // after
       
   111         actionInsertPageAfter->setText(tr("Insert Page After Current Page"));
       
   112         insertMenu->addAction(actionInsertPageAfter);
       
   113 
       
   114         insertMenuAction->setMenu(insertMenu);
       
   115         m_taskActions.append(insertMenuAction);
       
   116     }
       
   117         break;
       
   118     case MdiContainer: // No concept of order
       
   119         actionInsertPageAfter->setText(tr("Add Subwindow"));
       
   120         m_taskActions.append(actionInsertPageAfter);
       
   121         break;
       
   122     }
       
   123 }
       
   124 
       
   125 ContainerWidgetTaskMenu::~ContainerWidgetTaskMenu()
       
   126 {
       
   127 }
       
   128 
       
   129 QAction *ContainerWidgetTaskMenu::preferredEditAction() const
       
   130 {
       
   131     return 0;
       
   132 }
       
   133 
       
   134 bool ContainerWidgetTaskMenu::canDeletePage() const
       
   135 {
       
   136     switch (pageCount()) {
       
   137     case 0:
       
   138         return false;
       
   139     case 1:
       
   140         return m_type != PageContainer; // Do not delete last page of page-type container
       
   141     default:
       
   142         break;
       
   143     }
       
   144     return true;
       
   145 }
       
   146 
       
   147 int ContainerWidgetTaskMenu::pageCount() const
       
   148 {
       
   149     if (const QDesignerContainerExtension *ce = containerExtension())
       
   150         return ce->count();
       
   151     return 0;
       
   152 }
       
   153 
       
   154 QString ContainerWidgetTaskMenu::pageMenuText(ContainerType ct, int index, int count)
       
   155 {
       
   156     if (ct == MdiContainer)
       
   157         return tr("Subwindow"); // No concept of order, same text everywhere
       
   158     if (index < 0)
       
   159         return tr("Page");
       
   160     return tr("Page %1 of %2").arg(index + 1).arg(count);
       
   161 }
       
   162 
       
   163 QList<QAction*> ContainerWidgetTaskMenu::taskActions() const
       
   164 {
       
   165     QList<QAction*> actions = QDesignerTaskMenu::taskActions();
       
   166     actions += m_taskActions;
       
   167     // Update the page submenu, deletion and promotion. Updated on demand due to promotion state.
       
   168     m_pageMenu->clear();
       
   169     m_pageMenu->addAction(m_actionDeletePage);
       
   170     m_actionDeletePage->setEnabled(canDeletePage());
       
   171     const QDesignerContainerExtension *ce = containerExtension();
       
   172     const int index = ce->currentIndex();
       
   173     m_pageMenuAction->setText(pageMenuText(m_type, index, ce->count()));
       
   174     if (index != -1) { // Has a page
       
   175         m_pageMenuAction->setEnabled(true);
       
   176         m_pagePromotionTaskMenu->setWidget(ce->widget(index));
       
   177         m_pagePromotionTaskMenu->addActions(PromotionTaskMenu::LeadingSeparator|PromotionTaskMenu::SuppressGlobalEdit, m_pageMenu);
       
   178     } else { // No page
       
   179         m_pageMenuAction->setEnabled(false);
       
   180     }
       
   181 
       
   182     return actions;
       
   183 }
       
   184 
       
   185 QDesignerFormWindowInterface *ContainerWidgetTaskMenu::formWindow() const
       
   186 {
       
   187     return QDesignerFormWindowInterface::findFormWindow(m_containerWidget);
       
   188 }
       
   189 
       
   190 QDesignerContainerExtension *ContainerWidgetTaskMenu::containerExtension() const
       
   191 {
       
   192     QExtensionManager *mgr = m_core->extensionManager();
       
   193     return qt_extension<QDesignerContainerExtension*>(mgr, m_containerWidget);
       
   194 }
       
   195 
       
   196 void ContainerWidgetTaskMenu::removeCurrentPage()
       
   197 {
       
   198     if (QDesignerContainerExtension *c = containerExtension()) {
       
   199         if (c->currentIndex() == -1)
       
   200             return;
       
   201 
       
   202         QDesignerFormWindowInterface *fw = formWindow();
       
   203         DeleteContainerWidgetPageCommand *cmd = new DeleteContainerWidgetPageCommand(fw);
       
   204         cmd->init(m_containerWidget, m_type);
       
   205         fw->commandHistory()->push(cmd);
       
   206     }
       
   207 }
       
   208 
       
   209 void ContainerWidgetTaskMenu::addPage()
       
   210 {
       
   211     if (containerExtension()) {
       
   212         QDesignerFormWindowInterface *fw = formWindow();
       
   213         AddContainerWidgetPageCommand *cmd = new AddContainerWidgetPageCommand(fw);
       
   214         cmd->init(m_containerWidget, m_type, AddContainerWidgetPageCommand::InsertBefore);
       
   215         fw->commandHistory()->push(cmd);
       
   216     }
       
   217 }
       
   218 
       
   219 void ContainerWidgetTaskMenu::addPageAfter()
       
   220 {
       
   221     if (containerExtension()) {
       
   222         QDesignerFormWindowInterface *fw = formWindow();
       
   223         AddContainerWidgetPageCommand *cmd = new AddContainerWidgetPageCommand(fw);
       
   224         cmd->init(m_containerWidget, m_type, AddContainerWidgetPageCommand::InsertAfter);
       
   225         fw->commandHistory()->push(cmd);
       
   226     }
       
   227 }
       
   228 
       
   229 // -------------- WizardContainerWidgetTaskMenu
       
   230 WizardContainerWidgetTaskMenu::WizardContainerWidgetTaskMenu(QWizard *w, QObject *parent) :
       
   231     ContainerWidgetTaskMenu(w, WizardContainer, parent),
       
   232     m_nextAction(new QAction(tr("Next"), this)),
       
   233     m_previousAction(new QAction(tr("Back"), this))
       
   234 {
       
   235     connect(m_nextAction, SIGNAL(triggered()), w, SLOT(next()));
       
   236     connect(m_previousAction, SIGNAL(triggered()), w, SLOT(back()));
       
   237     QList<QAction*> &l = containerActions();
       
   238     l.push_front(createSeparator());
       
   239     l.push_front(m_nextAction);
       
   240     l.push_front(m_previousAction);
       
   241     l.push_front(createSeparator());
       
   242 }
       
   243 
       
   244 QList<QAction*> WizardContainerWidgetTaskMenu::taskActions() const
       
   245 {
       
   246     // Enable
       
   247     const QDesignerContainerExtension *ce = containerExtension();
       
   248     const int index = ce->currentIndex();
       
   249     m_previousAction->setEnabled(index > 0);
       
   250     m_nextAction->setEnabled(index >= 0 && index < (ce->count() - 1));
       
   251     return ContainerWidgetTaskMenu::taskActions();
       
   252 }
       
   253 
       
   254 // -------------- MdiContainerWidgetTaskMenu
       
   255 
       
   256 MdiContainerWidgetTaskMenu::MdiContainerWidgetTaskMenu(QMdiArea *m, QObject *parent) :
       
   257     ContainerWidgetTaskMenu(m, MdiContainer, parent)
       
   258 {
       
   259     initializeActions();
       
   260     connect(m_nextAction, SIGNAL(triggered()), m, SLOT( activateNextSubWindow ()));
       
   261     connect(m_previousAction, SIGNAL(triggered()), m , SLOT(activatePreviousSubWindow()));
       
   262     connect(m_tileAction, SIGNAL(triggered()), m, SLOT(tileSubWindows()));
       
   263     connect(m_cascadeAction, SIGNAL(triggered()), m, SLOT(cascadeSubWindows ()));
       
   264 }
       
   265 
       
   266 MdiContainerWidgetTaskMenu::MdiContainerWidgetTaskMenu(QWorkspace *m, QObject *parent) :
       
   267     ContainerWidgetTaskMenu(m, MdiContainer, parent)
       
   268 {
       
   269     initializeActions();
       
   270     connect(m_nextAction, SIGNAL(triggered()), m, SLOT(activateNextWindow()));
       
   271     connect(m_previousAction, SIGNAL(triggered()), m, SLOT(activatePreviousWindow()));
       
   272     connect(m_tileAction, SIGNAL(triggered()),m , SLOT(tile()));
       
   273     connect(m_cascadeAction, SIGNAL(triggered()), m, SLOT(cascade()));
       
   274 }
       
   275 
       
   276 void MdiContainerWidgetTaskMenu::initializeActions()
       
   277 {
       
   278     m_nextAction =new QAction(tr("Next Subwindow"), this);
       
   279     m_previousAction = new QAction(tr("Previous Subwindow"), this);
       
   280     m_tileAction = new QAction(tr("Tile"), this);
       
   281     m_cascadeAction = new QAction(tr("Cascade"), this);
       
   282 
       
   283     QList<QAction*> &l = containerActions();
       
   284     l.push_front(createSeparator());
       
   285     l.push_front(m_tileAction);
       
   286     l.push_front(m_cascadeAction);
       
   287     l.push_front(m_previousAction);
       
   288     l.push_front(m_nextAction);
       
   289     l.push_front(createSeparator());
       
   290 }
       
   291 
       
   292 QList<QAction*> MdiContainerWidgetTaskMenu::taskActions() const
       
   293 {
       
   294     const QList<QAction*> rc = ContainerWidgetTaskMenu::taskActions();
       
   295     // Enable
       
   296     const int count = pageCount();
       
   297     m_nextAction->setEnabled(count > 1);
       
   298     m_previousAction->setEnabled(count > 1);
       
   299     m_tileAction->setEnabled(count);
       
   300     m_cascadeAction->setEnabled(count);
       
   301     return rc;
       
   302 }
       
   303 
       
   304 // --------------  ContainerWidgetTaskMenuFactory
       
   305 
       
   306 ContainerWidgetTaskMenuFactory::ContainerWidgetTaskMenuFactory(QDesignerFormEditorInterface *core, QExtensionManager *extensionManager) :
       
   307     QExtensionFactory(extensionManager),
       
   308     m_core(core)
       
   309 {
       
   310 }
       
   311 
       
   312 QObject *ContainerWidgetTaskMenuFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
       
   313 {
       
   314     if (iid != QLatin1String("QDesignerInternalTaskMenuExtension") || !object->isWidgetType())
       
   315         return 0;
       
   316 
       
   317     QWidget *widget = qobject_cast<QWidget*>(object);
       
   318 
       
   319     if (qobject_cast<QStackedWidget*>(widget)
       
   320             || qobject_cast<QToolBox*>(widget)
       
   321             || qobject_cast<QTabWidget*>(widget)
       
   322             || qobject_cast<QDesignerDockWidget*>(widget)
       
   323             || qobject_cast<QScrollArea*>(widget)
       
   324             || qobject_cast<QMainWindow*>(widget)) {
       
   325         // Are we using Designer's own container extensions and task menus or did
       
   326         // someone provide an extra one with an addpage method, for example for a QScrollArea?
       
   327         if (const WidgetDataBase *wb = qobject_cast<const WidgetDataBase *>(m_core->widgetDataBase())) {
       
   328             const int idx = wb->indexOfObject(widget);
       
   329             const WidgetDataBaseItem *item = static_cast<const WidgetDataBaseItem *>(wb->item(idx));
       
   330             if (item->addPageMethod().isEmpty())
       
   331                 return 0;
       
   332         }
       
   333     }
       
   334 
       
   335     if (qt_extension<QDesignerContainerExtension*>(extensionManager(), object) == 0)
       
   336         return 0;
       
   337 
       
   338     if (QMdiArea* ma = qobject_cast<QMdiArea*>(widget))
       
   339         return new MdiContainerWidgetTaskMenu(ma, parent);
       
   340     if (QWorkspace *ws = qobject_cast<QWorkspace*>(widget))
       
   341         return new MdiContainerWidgetTaskMenu(ws, parent);
       
   342     if (QWizard *wz = qobject_cast<QWizard *>(widget))
       
   343         return new WizardContainerWidgetTaskMenu(wz, parent);
       
   344     return new ContainerWidgetTaskMenu(widget, PageContainer, parent);
       
   345 }
       
   346 
       
   347 }
       
   348 QT_END_NAMESPACE