tools/designer/src/lib/shared/actioneditor.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 "actioneditor_p.h"
       
    43 #include "filterwidget_p.h"
       
    44 #include "actionrepository_p.h"
       
    45 #include "iconloader_p.h"
       
    46 #include "newactiondialog_p.h"
       
    47 #include "qdesigner_menu_p.h"
       
    48 #include "qdesigner_command_p.h"
       
    49 #include "qdesigner_propertycommand_p.h"
       
    50 #include "qdesigner_objectinspector_p.h"
       
    51 #include "qdesigner_utils_p.h"
       
    52 #include "qsimpleresource_p.h"
       
    53 #include "formwindowbase_p.h"
       
    54 #include "qdesigner_taskmenu_p.h"
       
    55 
       
    56 #include <QtDesigner/QDesignerFormEditorInterface>
       
    57 #include <QtDesigner/QDesignerPropertyEditorInterface>
       
    58 #include <QtDesigner/QDesignerPropertySheetExtension>
       
    59 #include <QtDesigner/QExtensionManager>
       
    60 #include <QtDesigner/QDesignerMetaDataBaseInterface>
       
    61 #include <QtDesigner/QDesignerIconCacheInterface>
       
    62 #include <QtDesigner/private/abstractsettings_p.h>
       
    63 
       
    64 #include <QtGui/QMenu>
       
    65 #include <QtGui/QToolBar>
       
    66 #include <QtGui/QSplitter>
       
    67 #include <QtGui/QAction>
       
    68 #include <QtGui/QApplication>
       
    69 #include <QtGui/QClipboard>
       
    70 #include <QtGui/QItemDelegate>
       
    71 #include <QtGui/QPainter>
       
    72 #include <QtGui/QVBoxLayout>
       
    73 #include <QtGui/QLineEdit>
       
    74 #include <QtGui/QLabel>
       
    75 #include <QtGui/QPushButton>
       
    76 #include <QtGui/QToolButton>
       
    77 #include <QtGui/QContextMenuEvent>
       
    78 #include <QtGui/QItemSelection>
       
    79 
       
    80 #include <QtCore/QRegExp>
       
    81 #include <QtCore/QDebug>
       
    82 #include <QtCore/QSignalMapper>
       
    83 #include <QtCore/QBuffer>
       
    84 
       
    85 Q_DECLARE_METATYPE(QAction*)
       
    86 
       
    87 QT_BEGIN_NAMESPACE
       
    88 
       
    89 static const char *actionEditorViewModeKey = "ActionEditorViewMode";
       
    90 
       
    91 static const char *iconPropertyC = "icon";
       
    92 static const char *shortcutPropertyC = "shortcut";
       
    93 static const char *toolTipPropertyC = "toolTip";
       
    94 static const char *checkablePropertyC = "checkable";
       
    95 static const char *objectNamePropertyC = "objectName";
       
    96 static const char *textPropertyC = "text";
       
    97 
       
    98 namespace qdesigner_internal {
       
    99 //--------  ActionGroupDelegate
       
   100 class ActionGroupDelegate: public QItemDelegate
       
   101 {
       
   102 public:
       
   103     ActionGroupDelegate(QObject *parent)
       
   104         : QItemDelegate(parent) {}
       
   105 
       
   106     virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
       
   107     {
       
   108         if (option.state & QStyle::State_Selected)
       
   109             painter->fillRect(option.rect, option.palette.highlight());
       
   110 
       
   111         QItemDelegate::paint(painter, option, index);
       
   112     }
       
   113 
       
   114     virtual void drawFocus(QPainter * /*painter*/, const QStyleOptionViewItem &/*option*/, const QRect &/*rect*/) const {}
       
   115 };
       
   116 
       
   117 //--------  ActionEditor
       
   118 ActionEditor::ActionEditor(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags) :
       
   119     QDesignerActionEditorInterface(parent, flags),
       
   120     m_core(core),
       
   121     m_actionGroups(0),
       
   122     m_actionView(new ActionView),
       
   123     m_actionNew(new QAction(tr("New..."), this)),
       
   124     m_actionEdit(new QAction(tr("Edit..."), this)),
       
   125     m_actionNavigateToSlot(new QAction(tr("Go to slot..."), this)),
       
   126     m_actionCopy(new QAction(tr("Copy"), this)),
       
   127     m_actionCut(new QAction(tr("Cut"), this)),
       
   128     m_actionPaste(new QAction(tr("Paste"), this)),
       
   129     m_actionSelectAll(new QAction(tr("Select all"), this)),
       
   130     m_actionDelete(new QAction(tr("Delete"), this)),
       
   131     m_viewModeGroup(new  QActionGroup(this)),
       
   132     m_iconViewAction(0),
       
   133     m_listViewAction(0),
       
   134     m_filterWidget(0),
       
   135     m_selectAssociatedWidgetsMapper(0)
       
   136 {
       
   137     m_actionView->initialize(m_core);
       
   138     m_actionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
       
   139     setWindowTitle(tr("Actions"));
       
   140 
       
   141     QVBoxLayout *l = new QVBoxLayout(this);
       
   142     l->setMargin(0);
       
   143     l->setSpacing(0);
       
   144 
       
   145     QToolBar *toolbar = new QToolBar;
       
   146     toolbar->setIconSize(QSize(22, 22));
       
   147     toolbar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
       
   148     l->addWidget(toolbar);
       
   149     // edit actions
       
   150     QIcon documentNewIcon = QIcon::fromTheme("document-new", createIconSet(QLatin1String("filenew.png")));
       
   151     m_actionNew->setIcon(documentNewIcon);
       
   152     m_actionNew->setEnabled(false);
       
   153     connect(m_actionNew, SIGNAL(triggered()), this, SLOT(slotNewAction()));
       
   154     toolbar->addAction(m_actionNew);
       
   155 
       
   156     connect(m_actionSelectAll, SIGNAL(triggered()), m_actionView, SLOT(selectAll()));
       
   157 
       
   158     m_actionCut->setEnabled(false);
       
   159     connect(m_actionCut, SIGNAL(triggered()), this, SLOT(slotCut()));
       
   160     QIcon editCutIcon = QIcon::fromTheme("edit-cut", createIconSet(QLatin1String("editcut.png")));
       
   161     m_actionCut->setIcon(editCutIcon);
       
   162 
       
   163     m_actionCopy->setEnabled(false);
       
   164     connect(m_actionCopy, SIGNAL(triggered()), this, SLOT(slotCopy()));
       
   165     QIcon editCopyIcon = QIcon::fromTheme("edit-copy", createIconSet(QLatin1String("editcopy.png")));
       
   166     m_actionCopy->setIcon(editCopyIcon);
       
   167     toolbar->addAction(m_actionCopy);
       
   168 
       
   169     connect(m_actionPaste, SIGNAL(triggered()), this, SLOT(slotPaste()));
       
   170     QIcon editPasteIcon = QIcon::fromTheme("edit-paste", createIconSet(QLatin1String("editpaste.png")));
       
   171     m_actionPaste->setIcon(editPasteIcon);
       
   172     toolbar->addAction(m_actionPaste);
       
   173 
       
   174     m_actionEdit->setEnabled(false);
       
   175     connect(m_actionEdit, SIGNAL(triggered()), this, SLOT(editCurrentAction()));
       
   176 
       
   177     connect(m_actionNavigateToSlot, SIGNAL(triggered()), this, SLOT(navigateToSlotCurrentAction()));
       
   178 
       
   179     QIcon editDeleteIcon = QIcon::fromTheme("edit-delete", createIconSet(QLatin1String("editdelete.png")));
       
   180     m_actionDelete->setIcon(editDeleteIcon);
       
   181     m_actionDelete->setEnabled(false);
       
   182     connect(m_actionDelete, SIGNAL(triggered()), this, SLOT(slotDelete()));
       
   183     toolbar->addAction(m_actionDelete);
       
   184 
       
   185     // Toolbutton with menu containing action group for detailed/icon view. Steal the icons from the file dialog.
       
   186     //
       
   187     QMenu *configureMenu;
       
   188     toolbar->addWidget(createConfigureMenuButton(tr("Configure Action Editor"), &configureMenu));
       
   189 
       
   190     connect(m_viewModeGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotViewMode(QAction*)));
       
   191     m_iconViewAction = m_viewModeGroup->addAction(tr("Icon View"));
       
   192     m_iconViewAction->setData(QVariant(ActionView::IconView));
       
   193     m_iconViewAction->setCheckable(true);
       
   194     m_iconViewAction->setIcon(style()->standardIcon (QStyle::SP_FileDialogListView));
       
   195     configureMenu->addAction(m_iconViewAction);
       
   196 
       
   197     m_listViewAction = m_viewModeGroup->addAction(tr("Detailed View"));
       
   198     m_listViewAction->setData(QVariant(ActionView::DetailedView));
       
   199     m_listViewAction->setCheckable(true);
       
   200     m_listViewAction->setIcon(style()->standardIcon (QStyle::SP_FileDialogDetailedView));
       
   201     configureMenu->addAction(m_listViewAction);
       
   202     // filter
       
   203     m_filterWidget = new FilterWidget(toolbar);
       
   204     connect(m_filterWidget, SIGNAL(filterChanged(QString)), this, SLOT(setFilter(QString)));
       
   205     m_filterWidget->setEnabled(false);
       
   206     toolbar->addWidget(m_filterWidget);
       
   207 
       
   208     // main layout
       
   209     QSplitter *splitter = new QSplitter(Qt::Horizontal);
       
   210     splitter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   211 
       
   212     splitter->addWidget(m_actionView);
       
   213     l->addWidget(splitter);
       
   214 
       
   215 #if 0 // ### implement me
       
   216     m_actionGroups = new QListWidget(splitter);
       
   217     splitter->addWidget(m_actionGroups);
       
   218     m_actionGroups->setItemDelegate(new ActionGroupDelegate(m_actionGroups));
       
   219     m_actionGroups->setMovement(QListWidget::Static);
       
   220     m_actionGroups->setResizeMode(QListWidget::Fixed);
       
   221     m_actionGroups->setIconSize(QSize(48, 48));
       
   222     m_actionGroups->setFlow(QListWidget::TopToBottom);
       
   223     m_actionGroups->setViewMode(QListWidget::IconMode);
       
   224     m_actionGroups->setWrapping(false);
       
   225 #endif
       
   226 
       
   227     connect(m_actionView, SIGNAL(resourceImageDropped(QString,QAction*)),
       
   228             this, SLOT(resourceImageDropped(QString,QAction*)));
       
   229 
       
   230     connect(m_actionView, SIGNAL(currentChanged(QAction*)),this, SLOT(slotCurrentItemChanged(QAction*)));
       
   231     // make it possible for vs integration to reimplement edit action dialog
       
   232     connect(m_actionView, SIGNAL(activated(QAction*)), this, SIGNAL(itemActivated(QAction*)));
       
   233 
       
   234     connect(m_actionView,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
       
   235             this, SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));
       
   236 
       
   237     connect(m_actionView, SIGNAL(contextMenuRequested(QContextMenuEvent*, QAction*)),
       
   238             this, SLOT(slotContextMenuRequested(QContextMenuEvent*, QAction*)));
       
   239 
       
   240     connect(this, SIGNAL(itemActivated(QAction*)), this, SLOT(editAction(QAction*)));
       
   241 
       
   242     restoreSettings();
       
   243     updateViewModeActions();
       
   244 }
       
   245 
       
   246 // Utility to create a configure button with menu for usage on toolbars
       
   247 QToolButton *ActionEditor::createConfigureMenuButton(const QString &t, QMenu **ptrToMenu)
       
   248 {
       
   249     QToolButton *configureButton = new QToolButton;
       
   250     QAction *configureAction = new QAction(t, configureButton);
       
   251     QIcon configureIcon = QIcon::fromTheme("document-properties", createIconSet(QLatin1String("configure.png")));
       
   252     configureAction->setIcon(configureIcon);
       
   253     QMenu *configureMenu = new QMenu;
       
   254     configureAction->setMenu(configureMenu);
       
   255     configureButton->setDefaultAction(configureAction);
       
   256     configureButton->setPopupMode(QToolButton::InstantPopup);
       
   257     *ptrToMenu = configureMenu;
       
   258     return configureButton;
       
   259 }
       
   260 
       
   261 ActionEditor::~ActionEditor()
       
   262 {
       
   263     saveSettings();
       
   264 }
       
   265 
       
   266 QAction *ActionEditor::actionNew() const
       
   267 {
       
   268     return m_actionNew;
       
   269 }
       
   270 
       
   271 QAction *ActionEditor::actionDelete() const
       
   272 {
       
   273     return m_actionDelete;
       
   274 }
       
   275 
       
   276 QDesignerFormWindowInterface *ActionEditor::formWindow() const
       
   277 {
       
   278     return m_formWindow;
       
   279 }
       
   280 
       
   281 void ActionEditor::setFormWindow(QDesignerFormWindowInterface *formWindow)
       
   282 {
       
   283     if (formWindow != 0 && formWindow->mainContainer() == 0)
       
   284         formWindow = 0;
       
   285 
       
   286     // we do NOT rely on this function to update the action editor
       
   287     if (m_formWindow == formWindow)
       
   288         return;
       
   289 
       
   290     if (m_formWindow != 0) {
       
   291         const ActionList actionList = qFindChildren<QAction*>(m_formWindow->mainContainer());
       
   292         foreach (QAction *action, actionList)
       
   293             disconnect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));
       
   294     }
       
   295 
       
   296     m_formWindow = formWindow;
       
   297 
       
   298     m_actionView->model()->clearActions();
       
   299 
       
   300     m_actionEdit->setEnabled(false);
       
   301     m_actionCopy->setEnabled(false);
       
   302     m_actionCut->setEnabled(false);
       
   303     m_actionDelete->setEnabled(false);
       
   304 
       
   305     if (!formWindow || !formWindow->mainContainer()) {
       
   306         m_actionNew->setEnabled(false);
       
   307         m_filterWidget->setEnabled(false);
       
   308         return;
       
   309     }
       
   310 
       
   311     m_actionNew->setEnabled(true);
       
   312     m_filterWidget->setEnabled(true);
       
   313 
       
   314     const ActionList actionList = qFindChildren<QAction*>(formWindow->mainContainer());
       
   315     foreach (QAction *action, actionList)
       
   316         if (!action->isSeparator() && core()->metaDataBase()->item(action) != 0) {
       
   317             // Show unless it has a menu. However, listen for change on menu actions also as it might be removed
       
   318             if (!action->menu())
       
   319                 m_actionView->model()->addAction(action);
       
   320             connect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));
       
   321         }
       
   322 
       
   323     setFilter(m_filter);
       
   324 }
       
   325 
       
   326 void  ActionEditor::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& /*deselected*/)
       
   327 {
       
   328     const bool hasSelection = !selected.indexes().empty();
       
   329     m_actionCopy->setEnabled(hasSelection);
       
   330     m_actionCut->setEnabled(hasSelection);
       
   331     m_actionDelete->setEnabled(hasSelection);
       
   332 }
       
   333 
       
   334 void ActionEditor::slotCurrentItemChanged(QAction *action)
       
   335 {
       
   336     QDesignerFormWindowInterface *fw = formWindow();
       
   337     if (!fw)
       
   338         return;
       
   339 
       
   340     const bool hasCurrentAction = action != 0;
       
   341     m_actionEdit->setEnabled(hasCurrentAction);
       
   342 
       
   343     if (!action) {
       
   344         fw->clearSelection();
       
   345         return;
       
   346     }
       
   347 
       
   348     QDesignerObjectInspector *oi = qobject_cast<QDesignerObjectInspector *>(core()->objectInspector());
       
   349 
       
   350     if (action->associatedWidgets().empty()) {
       
   351         // Special case: action not in object tree. Deselect all and set in property editor
       
   352         fw->clearSelection(false);
       
   353         if (oi)
       
   354             oi->clearSelection();
       
   355         core()->propertyEditor()->setObject(action);
       
   356     } else {
       
   357         if (oi)
       
   358             oi->selectObject(action);
       
   359     }
       
   360 }
       
   361 
       
   362 void ActionEditor::slotActionChanged()
       
   363 {
       
   364     QAction *action = qobject_cast<QAction*>(sender());
       
   365     Q_ASSERT(action != 0);
       
   366 
       
   367     ActionModel *model = m_actionView->model();
       
   368     const int row = model->findAction(action);
       
   369     if (row == -1) {
       
   370         if (action->menu() == 0) // action got its menu deleted, create item
       
   371             model->addAction(action);
       
   372     } else if (action->menu() != 0) { // action got its menu created, remove item
       
   373         model->removeRow(row);
       
   374     } else {
       
   375         // action text or icon changed, update item
       
   376         model->update(row);
       
   377     }
       
   378 }
       
   379 
       
   380 QDesignerFormEditorInterface *ActionEditor::core() const
       
   381 {
       
   382     return m_core;
       
   383 }
       
   384 
       
   385 QString ActionEditor::filter() const
       
   386 {
       
   387     return m_filter;
       
   388 }
       
   389 
       
   390 void ActionEditor::setFilter(const QString &f)
       
   391 {
       
   392     m_filter = f;
       
   393     m_actionView->filter(m_filter);
       
   394 }
       
   395 
       
   396 // Set changed state of icon property,  reset when icon is cleared
       
   397 static void refreshIconPropertyChanged(const QAction *action, QDesignerPropertySheetExtension *sheet)
       
   398 {
       
   399     sheet->setChanged(sheet->indexOf(QLatin1String(iconPropertyC)), !action->icon().isNull());
       
   400 }
       
   401 
       
   402 void ActionEditor::manageAction(QAction *action)
       
   403 {
       
   404     action->setParent(formWindow()->mainContainer());
       
   405     core()->metaDataBase()->add(action);
       
   406 
       
   407     if (action->isSeparator() || action->menu() != 0)
       
   408         return;
       
   409 
       
   410     QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), action);
       
   411     sheet->setChanged(sheet->indexOf(QLatin1String(objectNamePropertyC)), true);
       
   412     sheet->setChanged(sheet->indexOf(QLatin1String(textPropertyC)), true);
       
   413     refreshIconPropertyChanged(action, sheet);
       
   414 
       
   415     m_actionView->setCurrentIndex(m_actionView->model()->addAction(action));
       
   416     connect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));
       
   417 }
       
   418 
       
   419 void ActionEditor::unmanageAction(QAction *action)
       
   420 {
       
   421     core()->metaDataBase()->remove(action);
       
   422     action->setParent(0);
       
   423 
       
   424     disconnect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));
       
   425 
       
   426     const int row = m_actionView->model()->findAction(action);
       
   427     if (row != -1)
       
   428         m_actionView->model()->remove(row);
       
   429 }
       
   430 
       
   431 // Set an intial property and mark it as changed in the sheet
       
   432 static void setInitialProperty(QDesignerPropertySheetExtension *sheet, const QString &name, const QVariant &value)
       
   433 {
       
   434     const int index = sheet->indexOf(name);
       
   435     Q_ASSERT(index != -1);
       
   436     sheet->setProperty(index, value);
       
   437     sheet->setChanged(index, true);
       
   438 }
       
   439 
       
   440 void ActionEditor::slotNewAction()
       
   441 {
       
   442     NewActionDialog dlg(this);
       
   443     dlg.setWindowTitle(tr("New action"));
       
   444 
       
   445     if (dlg.exec() == QDialog::Accepted) {
       
   446         const ActionData actionData = dlg.actionData();
       
   447         m_actionView->clearSelection();
       
   448 
       
   449         QAction *action = new QAction(formWindow());
       
   450         action->setObjectName(actionData.name);
       
   451         formWindow()->ensureUniqueObjectName(action);
       
   452         action->setText(actionData.text);
       
   453 
       
   454         QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), action);
       
   455         if (!actionData.toolTip.isEmpty())
       
   456             setInitialProperty(sheet, QLatin1String(toolTipPropertyC), actionData.toolTip);
       
   457 
       
   458         if (actionData.checkable)
       
   459             setInitialProperty(sheet, QLatin1String(checkablePropertyC), QVariant(true));
       
   460 
       
   461         if (!actionData.keysequence.value().isEmpty())
       
   462             setInitialProperty(sheet, QLatin1String(shortcutPropertyC), qVariantFromValue(actionData.keysequence));
       
   463 
       
   464         sheet->setProperty(sheet->indexOf(QLatin1String(iconPropertyC)), qVariantFromValue(actionData.icon));
       
   465 
       
   466         AddActionCommand *cmd = new AddActionCommand(formWindow());
       
   467         cmd->init(action);
       
   468         formWindow()->commandHistory()->push(cmd);
       
   469     }
       
   470 }
       
   471 
       
   472 static inline bool isSameIcon(const QIcon &i1, const QIcon &i2)
       
   473 {
       
   474     return i1.serialNumber() == i2.serialNumber();
       
   475 }
       
   476 
       
   477 // return a FormWindow command to apply an icon or a reset command in case it
       
   478 //  is empty.
       
   479 
       
   480 static QDesignerFormWindowCommand *setIconPropertyCommand(const PropertySheetIconValue &newIcon, QAction *action, QDesignerFormWindowInterface *fw)
       
   481 {
       
   482     const QString iconProperty = QLatin1String(iconPropertyC);
       
   483     if (newIcon.paths().isEmpty()) {
       
   484         ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);
       
   485         cmd->init(action, iconProperty);
       
   486         return cmd;
       
   487     }
       
   488     SetPropertyCommand *cmd = new SetPropertyCommand(fw);
       
   489     cmd->init(action, iconProperty, qVariantFromValue(newIcon));
       
   490     return cmd;
       
   491 }
       
   492 
       
   493 // return a FormWindow command to apply a QKeySequence or a reset command
       
   494 // in case it is empty.
       
   495 
       
   496 static QDesignerFormWindowCommand *setKeySequencePropertyCommand(const PropertySheetKeySequenceValue &ks, QAction *action, QDesignerFormWindowInterface *fw)
       
   497 {
       
   498     const QString shortcutProperty = QLatin1String(shortcutPropertyC);
       
   499     if (ks.value().isEmpty()) {
       
   500         ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);
       
   501         cmd->init(action, shortcutProperty);
       
   502         return cmd;
       
   503     }
       
   504     SetPropertyCommand *cmd = new SetPropertyCommand(fw);
       
   505     cmd->init(action, shortcutProperty, qVariantFromValue(ks));
       
   506     return cmd;
       
   507 }
       
   508 
       
   509 // return a FormWindow command to apply a POD value or reset command in case
       
   510 // it is equal to the default value.
       
   511 
       
   512 template <class T>
       
   513 QDesignerFormWindowCommand *setPropertyCommand(const QString &name, T value, T defaultValue,
       
   514                                                QObject *o, QDesignerFormWindowInterface *fw)
       
   515 {
       
   516     if (value == defaultValue) {
       
   517         ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);
       
   518         cmd->init(o, name);
       
   519         return cmd;
       
   520     }
       
   521     SetPropertyCommand *cmd = new SetPropertyCommand(fw);
       
   522     cmd->init(o, name, QVariant(value));
       
   523     return cmd;
       
   524 }
       
   525 
       
   526 // Return the text value of a string property via PropertySheetStringValue
       
   527 static inline QString textPropertyValue(const QDesignerPropertySheetExtension *sheet, const QString &name)
       
   528 {
       
   529     const int index = sheet->indexOf(name);
       
   530     Q_ASSERT(index != -1);
       
   531     const PropertySheetStringValue ps = qVariantValue<PropertySheetStringValue>(sheet->property(index));
       
   532     return ps.value();
       
   533 }
       
   534 
       
   535 void ActionEditor::editAction(QAction *action)
       
   536 {
       
   537     if (!action)
       
   538         return;
       
   539 
       
   540     NewActionDialog dlg(this);
       
   541     dlg.setWindowTitle(tr("Edit action"));
       
   542 
       
   543     ActionData oldActionData;
       
   544     QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), action);
       
   545     oldActionData.name = action->objectName();
       
   546     oldActionData.text = action->text();
       
   547     oldActionData.toolTip = textPropertyValue(sheet, QLatin1String(toolTipPropertyC));
       
   548     oldActionData.icon = qVariantValue<PropertySheetIconValue>(sheet->property(sheet->indexOf(QLatin1String(iconPropertyC))));
       
   549     oldActionData.keysequence = ActionModel::actionShortCut(sheet);
       
   550     oldActionData.checkable =  action->isCheckable();
       
   551     dlg.setActionData(oldActionData);
       
   552 
       
   553     if (!dlg.exec())
       
   554         return;
       
   555 
       
   556     // figure out changes and whether to start a macro
       
   557     const ActionData newActionData = dlg.actionData();
       
   558     const unsigned changeMask = newActionData.compare(oldActionData);
       
   559     if (changeMask == 0u)
       
   560         return;
       
   561 
       
   562     const bool severalChanges = (changeMask != ActionData::TextChanged)      && (changeMask != ActionData::NameChanged)
       
   563                              && (changeMask != ActionData::ToolTipChanged)   && (changeMask != ActionData::IconChanged)
       
   564                              && (changeMask != ActionData::CheckableChanged) && (changeMask != ActionData::KeysequenceChanged);
       
   565 
       
   566     QDesignerFormWindowInterface *fw = formWindow();
       
   567     QUndoStack *undoStack = fw->commandHistory();
       
   568     if (severalChanges)
       
   569         fw->beginCommand(QLatin1String("Edit action"));
       
   570 
       
   571     if (changeMask & ActionData::NameChanged)
       
   572         undoStack->push(createTextPropertyCommand(QLatin1String(objectNamePropertyC), newActionData.name, action, fw));
       
   573 
       
   574     if (changeMask & ActionData::TextChanged)
       
   575         undoStack->push(createTextPropertyCommand(QLatin1String(textPropertyC), newActionData.text, action, fw));
       
   576 
       
   577     if (changeMask & ActionData::ToolTipChanged)
       
   578         undoStack->push(createTextPropertyCommand(QLatin1String(toolTipPropertyC), newActionData.toolTip, action, fw));
       
   579 
       
   580     if (changeMask & ActionData::IconChanged)
       
   581         undoStack->push(setIconPropertyCommand(newActionData.icon, action, fw));
       
   582 
       
   583     if (changeMask & ActionData::CheckableChanged)
       
   584         undoStack->push(setPropertyCommand(QLatin1String(checkablePropertyC), newActionData.checkable, false, action, fw));
       
   585 
       
   586     if (changeMask & ActionData::KeysequenceChanged)
       
   587         undoStack->push(setKeySequencePropertyCommand(newActionData.keysequence, action, fw));
       
   588 
       
   589     if (severalChanges)
       
   590         fw->endCommand();
       
   591 }
       
   592 
       
   593 void ActionEditor::editCurrentAction()
       
   594 {
       
   595     if (QAction *a = m_actionView->currentAction())
       
   596         editAction(a);
       
   597 }
       
   598 
       
   599 void ActionEditor::navigateToSlotCurrentAction()
       
   600 {
       
   601     if (QAction *a = m_actionView->currentAction())
       
   602         QDesignerTaskMenu::navigateToSlot(m_core, a, QLatin1String("triggered()"));
       
   603 }
       
   604 
       
   605 void ActionEditor::deleteActions(QDesignerFormWindowInterface *fw, const ActionList &actions)
       
   606 {
       
   607     // We need a macro even in the case of single action because the commands might cause the
       
   608     // scheduling of other commands (signal slots connections)
       
   609     const QString description = actions.size() == 1 ?
       
   610         tr("Remove action '%1'").arg(actions.front()->objectName()) : tr("Remove actions");
       
   611     fw->beginCommand(description);
       
   612     foreach(QAction *action, actions) {
       
   613         RemoveActionCommand *cmd = new RemoveActionCommand(fw);
       
   614         cmd->init(action);
       
   615         fw->commandHistory()->push(cmd);
       
   616     }
       
   617     fw->endCommand();
       
   618 }
       
   619 
       
   620 void ActionEditor::copyActions(QDesignerFormWindowInterface *fwi, const ActionList &actions)
       
   621 {
       
   622     FormWindowBase *fw = qobject_cast<FormWindowBase *>(fwi);
       
   623     if (!fw )
       
   624         return;
       
   625 
       
   626     FormBuilderClipboard clipboard;
       
   627     clipboard.m_actions = actions;
       
   628 
       
   629     if (clipboard.empty())
       
   630         return;
       
   631 
       
   632     QEditorFormBuilder *formBuilder = fw->createFormBuilder();
       
   633     Q_ASSERT(formBuilder);
       
   634 
       
   635     QBuffer buffer;
       
   636     if (buffer.open(QIODevice::WriteOnly))
       
   637         if (formBuilder->copy(&buffer, clipboard))
       
   638             qApp->clipboard()->setText(QString::fromUtf8(buffer.buffer()), QClipboard::Clipboard);
       
   639     delete formBuilder;
       
   640 }
       
   641 
       
   642 void ActionEditor::slotDelete()
       
   643 {
       
   644     QDesignerFormWindowInterface *fw =  formWindow();
       
   645     if (!fw)
       
   646         return;
       
   647 
       
   648     const ActionView::ActionList selection = m_actionView->selectedActions();
       
   649     if (selection.empty())
       
   650         return;
       
   651 
       
   652     deleteActions(fw,  selection);
       
   653 }
       
   654 
       
   655 QString ActionEditor::actionTextToName(const QString &text, const QString &prefix)
       
   656 {
       
   657     QString name = text;
       
   658     if (name.isEmpty())
       
   659         return QString();
       
   660 
       
   661     name[0] = name.at(0).toUpper();
       
   662     name.prepend(prefix);
       
   663     const QString underscore = QString(QLatin1Char('_'));
       
   664     name.replace(QRegExp(QString(QLatin1String("[^a-zA-Z_0-9]"))), underscore);
       
   665     name.replace(QRegExp(QLatin1String("__*")), underscore);
       
   666     if (name.endsWith(underscore.at(0)))
       
   667         name.truncate(name.size() - 1);
       
   668 
       
   669     return name;
       
   670 }
       
   671 
       
   672 void  ActionEditor::resourceImageDropped(const QString &path, QAction *action)
       
   673 {
       
   674     QDesignerFormWindowInterface *fw =  formWindow();
       
   675     if (!fw)
       
   676         return;
       
   677 
       
   678     QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), action);
       
   679     const PropertySheetIconValue oldIcon =
       
   680             qVariantValue<PropertySheetIconValue>(sheet->property(sheet->indexOf(QLatin1String(iconPropertyC))));
       
   681     PropertySheetIconValue newIcon;
       
   682     newIcon.setPixmap(QIcon::Normal, QIcon::Off, PropertySheetPixmapValue(path));
       
   683     if (newIcon.paths().isEmpty() || newIcon.paths() == oldIcon.paths())
       
   684         return;
       
   685 
       
   686     fw->commandHistory()->push(setIconPropertyCommand(newIcon , action, fw));
       
   687 }
       
   688 
       
   689 void ActionEditor::mainContainerChanged()
       
   690 {
       
   691     // Invalidate references to objects kept in model
       
   692     if (sender() == formWindow())
       
   693         setFormWindow(0);
       
   694 }
       
   695 
       
   696 void ActionEditor::slotViewMode(QAction *a)
       
   697 {
       
   698     m_actionView->setViewMode(a->data().toInt());
       
   699     updateViewModeActions();
       
   700 }
       
   701 
       
   702 void ActionEditor::slotSelectAssociatedWidget(QWidget *w)
       
   703 {
       
   704     QDesignerFormWindowInterface *fw = formWindow();
       
   705     if (!fw )
       
   706         return;
       
   707 
       
   708     QDesignerObjectInspector *oi = qobject_cast<QDesignerObjectInspector *>(core()->objectInspector());
       
   709     if (!oi)
       
   710         return;
       
   711 
       
   712     fw->clearSelection(); // Actually, there are no widgets selected due to focus in event handling. Just to be sure.
       
   713     oi->selectObject(w);
       
   714 }
       
   715 
       
   716 void ActionEditor::restoreSettings()
       
   717 {
       
   718     QDesignerSettingsInterface *settings = m_core->settingsManager();
       
   719     m_actionView->setViewMode(settings->value(QLatin1String(actionEditorViewModeKey), 0).toInt());
       
   720     updateViewModeActions();
       
   721 }
       
   722 
       
   723 void ActionEditor::saveSettings()
       
   724 {
       
   725     QDesignerSettingsInterface *settings = m_core->settingsManager();
       
   726     settings->setValue(QLatin1String(actionEditorViewModeKey), m_actionView->viewMode());
       
   727 }
       
   728 
       
   729 void ActionEditor::updateViewModeActions()
       
   730 {
       
   731     switch (m_actionView->viewMode()) {
       
   732     case ActionView::IconView:
       
   733         m_iconViewAction->setChecked(true);
       
   734         break;
       
   735     case ActionView::DetailedView:
       
   736         m_listViewAction->setChecked(true);
       
   737         break;
       
   738     }
       
   739 }
       
   740 
       
   741 void ActionEditor::slotCopy()
       
   742 {
       
   743     QDesignerFormWindowInterface *fw = formWindow();
       
   744     if (!fw )
       
   745         return;
       
   746 
       
   747     const ActionView::ActionList selection = m_actionView->selectedActions();
       
   748     if (selection.empty())
       
   749         return;
       
   750 
       
   751     copyActions(fw, selection);
       
   752 }
       
   753 
       
   754 void ActionEditor::slotCut()
       
   755 {
       
   756     QDesignerFormWindowInterface *fw = formWindow();
       
   757     if (!fw )
       
   758         return;
       
   759 
       
   760     const ActionView::ActionList selection = m_actionView->selectedActions();
       
   761     if (selection.empty())
       
   762         return;
       
   763 
       
   764     copyActions(fw, selection);
       
   765     deleteActions(fw, selection);
       
   766 }
       
   767 
       
   768 void ActionEditor::slotPaste()
       
   769 {
       
   770     FormWindowBase *fw = qobject_cast<FormWindowBase *>(formWindow());
       
   771     if (!fw)
       
   772         return;
       
   773     m_actionView->clearSelection();
       
   774     fw->paste(FormWindowBase::PasteActionsOnly);
       
   775 }
       
   776 
       
   777 void ActionEditor::slotContextMenuRequested(QContextMenuEvent *e, QAction *item)
       
   778 {
       
   779     // set up signal mapper
       
   780     if (!m_selectAssociatedWidgetsMapper) {
       
   781         m_selectAssociatedWidgetsMapper = new QSignalMapper(this);
       
   782         connect(m_selectAssociatedWidgetsMapper, SIGNAL(mapped(QWidget*)), this, SLOT(slotSelectAssociatedWidget(QWidget*)));
       
   783     }
       
   784 
       
   785     QMenu menu(this);
       
   786     menu.addAction(m_actionNew);
       
   787     menu.addSeparator();
       
   788     menu.addAction(m_actionEdit);
       
   789     if (QDesignerTaskMenu::isSlotNavigationEnabled(m_core))
       
   790         menu.addAction(m_actionNavigateToSlot);
       
   791 
       
   792     // Associated Widgets
       
   793     if (QAction *action = m_actionView->currentAction()) {
       
   794         const QWidgetList associatedWidgets = ActionModel::associatedWidgets(action);
       
   795         if (!associatedWidgets.empty()) {
       
   796             QMenu *associatedWidgetsSubMenu =  menu.addMenu(tr("Used In"));
       
   797             foreach (QWidget *w, associatedWidgets) {
       
   798                 QAction *action = associatedWidgetsSubMenu->addAction(w->objectName());
       
   799                 m_selectAssociatedWidgetsMapper->setMapping(action, w);
       
   800                 connect(action, SIGNAL(triggered()), m_selectAssociatedWidgetsMapper, SLOT(map()));
       
   801             }
       
   802         }
       
   803     }
       
   804 
       
   805     menu.addSeparator();
       
   806     menu.addAction(m_actionCut);
       
   807     menu.addAction(m_actionCopy);
       
   808     menu.addAction(m_actionPaste);
       
   809     menu.addAction(m_actionSelectAll);
       
   810     menu.addAction(m_actionDelete);
       
   811     menu.addSeparator();
       
   812     menu.addAction(m_iconViewAction);
       
   813     menu.addAction(m_listViewAction);
       
   814 
       
   815     emit contextMenuRequested(&menu, item);
       
   816 
       
   817     menu.exec(e->globalPos());
       
   818     e->accept();
       
   819 }
       
   820 
       
   821 } // namespace qdesigner_internal
       
   822 
       
   823 QT_END_NAMESPACE
       
   824