tools/designer/src/components/taskmenu/button_taskmenu.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 "button_taskmenu.h"
       
    43 #include "inplace_editor.h"
       
    44 #include <qdesigner_formwindowcommand_p.h>
       
    45 #include <formwindowbase_p.h>
       
    46 
       
    47 #include <QtDesigner/QDesignerFormWindowInterface>
       
    48 #include <QtDesigner/QDesignerFormWindowCursorInterface>
       
    49 #include <QtDesigner/QDesignerFormEditorInterface>
       
    50 #include <QtDesigner/QDesignerMetaDataBaseInterface>
       
    51 #include <QtDesigner/QDesignerObjectInspectorInterface>
       
    52 #include <QtDesigner/QDesignerPropertyEditorInterface>
       
    53 
       
    54 #include <QtGui/QAction>
       
    55 #include <QtGui/QActionGroup>
       
    56 #include <QtGui/QMenu>
       
    57 #include <QtGui/QStyle>
       
    58 #include <QtGui/QStyleOption>
       
    59 #include <QtGui/QAbstractButton>
       
    60 #include <QtGui/QButtonGroup>
       
    61 #include <QtGui/QApplication>
       
    62 #include <QtCore/QDebug>
       
    63 
       
    64 Q_DECLARE_METATYPE(QButtonGroup*)
       
    65 
       
    66 QT_BEGIN_NAMESPACE
       
    67 
       
    68 namespace qdesigner_internal {
       
    69 
       
    70 enum { debugButtonMenu = 0 };
       
    71 
       
    72 typedef QList<QAbstractButton *> ButtonList;
       
    73 typedef QList<QButtonGroup *> ButtonGroupList;
       
    74 
       
    75 // ButtonGroupCommand: Base for commands handling button groups and button lists
       
    76 // addButtonsToGroup() and removeButtonsFromGroup() are low-level helpers for
       
    77 // adding/removing members to/from existing groups.
       
    78 //
       
    79 // createButtonGroup()/breakButtonGroup() create and remove the groups from scratch.
       
    80 // When using them in a command, the command must be executed within
       
    81 // a macro since it makes the form emit objectRemoved() which might cause other components
       
    82 // to add commands (for example, removal of signals and slots)
       
    83 class ButtonGroupCommand : public QDesignerFormWindowCommand {
       
    84 
       
    85 protected:
       
    86     ButtonGroupCommand(const QString &description, QDesignerFormWindowInterface *formWindow);
       
    87 
       
    88     void initialize(const ButtonList &bl, QButtonGroup *buttonGroup);
       
    89 
       
    90     // Helper: Add the buttons to the group
       
    91     void addButtonsToGroup();
       
    92     // Helper; Remove the buttons
       
    93     void removeButtonsFromGroup();
       
    94 
       
    95     // Create the button group in Designer
       
    96     void createButtonGroup();
       
    97     // Remove the button group from Designer
       
    98     void breakButtonGroup();
       
    99 
       
   100 public:
       
   101     static QString nameList(const ButtonList& bl);
       
   102     static ButtonGroupList managedButtonGroups(const QDesignerFormWindowInterface *formWindow);
       
   103 
       
   104 private:
       
   105     ButtonList m_buttonList;
       
   106     QButtonGroup *m_buttonGroup;
       
   107 };
       
   108 
       
   109 ButtonGroupCommand::ButtonGroupCommand(const QString &description, QDesignerFormWindowInterface *formWindow) :
       
   110     QDesignerFormWindowCommand(description, formWindow),
       
   111     m_buttonGroup(0)
       
   112 {
       
   113 }
       
   114 
       
   115 void ButtonGroupCommand::initialize(const ButtonList &bl, QButtonGroup *buttonGroup)
       
   116 {
       
   117     m_buttonList = bl;
       
   118     m_buttonGroup = buttonGroup;
       
   119 }
       
   120 
       
   121 void ButtonGroupCommand::addButtonsToGroup()
       
   122 {
       
   123     if (debugButtonMenu)
       
   124         qDebug() << "Adding " << m_buttonList << " to " << m_buttonGroup;
       
   125     const ButtonList::const_iterator cend = m_buttonList.constEnd();
       
   126     for (ButtonList::const_iterator it = m_buttonList.constBegin(); it != cend; ++it)
       
   127         m_buttonGroup->addButton(*it);
       
   128 }
       
   129 
       
   130 void ButtonGroupCommand::removeButtonsFromGroup()
       
   131 {
       
   132     if (debugButtonMenu)
       
   133         qDebug() << "Removing " << m_buttonList << " from " << m_buttonGroup;
       
   134     const ButtonList::const_iterator cend = m_buttonList.constEnd();
       
   135     for (ButtonList::const_iterator it = m_buttonList.constBegin(); it != cend; ++it)
       
   136         m_buttonGroup->removeButton(*it);
       
   137 }
       
   138 
       
   139 void ButtonGroupCommand::createButtonGroup()
       
   140 {
       
   141     if (debugButtonMenu)
       
   142         qDebug() << "Creating " <<  m_buttonGroup << " from " <<  m_buttonList;
       
   143 
       
   144     QDesignerFormWindowInterface *fw = formWindow();
       
   145     QDesignerFormEditorInterface *core = fw->core();
       
   146     core->metaDataBase()->add(m_buttonGroup);
       
   147     addButtonsToGroup();
       
   148     // Make button group visible
       
   149     core->objectInspector()->setFormWindow(fw);
       
   150 }
       
   151 
       
   152 void ButtonGroupCommand::breakButtonGroup()
       
   153 {
       
   154     if (debugButtonMenu)
       
   155         qDebug() << "Removing " <<  m_buttonGroup << " consisting of " <<  m_buttonList;
       
   156 
       
   157     QDesignerFormWindowInterface *fw = formWindow();
       
   158     QDesignerFormEditorInterface *core = fw->core();
       
   159     // Button group was selected, that is, break was invoked via its context menu. Remove it from property editor, select the buttons
       
   160     if (core->propertyEditor()->object() == m_buttonGroup) {
       
   161         fw->clearSelection(false);
       
   162         const ButtonList::const_iterator cend = m_buttonList.constEnd();
       
   163         for (ButtonList::const_iterator it = m_buttonList.constBegin(); it != cend; ++it)
       
   164             fw->selectWidget(*it, true);
       
   165     }
       
   166     // Now remove and refresh object inspector
       
   167     removeButtonsFromGroup();
       
   168     // Notify components (for example, signal slot editor)
       
   169     if (qdesigner_internal::FormWindowBase *fwb = qobject_cast<qdesigner_internal::FormWindowBase *>(fw))
       
   170         fwb->emitObjectRemoved(m_buttonGroup);
       
   171     core->metaDataBase()->remove(m_buttonGroup);
       
   172     core->objectInspector()->setFormWindow(fw);
       
   173 }
       
   174 
       
   175 QString ButtonGroupCommand::nameList(const ButtonList& bl)
       
   176 {
       
   177     QString rc;
       
   178     const QChar quote = QLatin1Char('\'');
       
   179     const QString separator =  QLatin1String(", ");
       
   180     const int size = bl.size();
       
   181     for (int i = 0; i < size; i++) {
       
   182         if (i)
       
   183             rc += separator;
       
   184         rc += quote;
       
   185         rc += bl[i]->objectName();
       
   186         rc += quote;
       
   187     }
       
   188     return rc;
       
   189 
       
   190 }
       
   191 
       
   192 ButtonGroupList ButtonGroupCommand::managedButtonGroups(const QDesignerFormWindowInterface *formWindow)
       
   193 {
       
   194     const QDesignerMetaDataBaseInterface *mdb = formWindow->core()->metaDataBase();
       
   195     ButtonGroupList bl;
       
   196     // Check 1st order children for managed button groups
       
   197     const QObjectList children = formWindow->mainContainer()->children();
       
   198     const QObjectList::const_iterator cend =  children.constEnd();
       
   199     for (QObjectList::const_iterator it =  children.constBegin(); it != cend; ++it) {
       
   200         if (!(*it)->isWidgetType())
       
   201             if (QButtonGroup *bg = qobject_cast<QButtonGroup *>(*it))
       
   202                 if (mdb->item(bg))
       
   203                     bl.push_back(bg);
       
   204     }
       
   205     return bl;
       
   206 }
       
   207 
       
   208 // --------------- CreateButtonGroupCommand
       
   209 // This command might be executed in a macro with a remove
       
   210 // command to move buttons from one group to a new one.
       
   211 class CreateButtonGroupCommand : public ButtonGroupCommand {
       
   212 public:
       
   213     CreateButtonGroupCommand(QDesignerFormWindowInterface *formWindow);
       
   214     bool init(const ButtonList &bl);
       
   215 
       
   216     virtual void undo() { breakButtonGroup(); }
       
   217     virtual void redo() { createButtonGroup(); }
       
   218 };
       
   219 
       
   220 CreateButtonGroupCommand::CreateButtonGroupCommand(QDesignerFormWindowInterface *formWindow) :
       
   221     ButtonGroupCommand(QApplication::translate("Command", "Create button group"), formWindow)
       
   222 {
       
   223 }
       
   224 
       
   225 bool CreateButtonGroupCommand::init(const ButtonList &bl)
       
   226 {
       
   227     if (bl.empty())
       
   228         return false;
       
   229     QDesignerFormWindowInterface *fw = formWindow();
       
   230     QButtonGroup *buttonGroup = new QButtonGroup(fw->mainContainer());
       
   231     buttonGroup->setObjectName(QLatin1String("buttonGroup"));
       
   232     fw->ensureUniqueObjectName(buttonGroup);
       
   233     initialize(bl, buttonGroup);
       
   234     return true;
       
   235 }
       
   236 
       
   237 // --------------- BreakButtonGroupCommand
       
   238 class BreakButtonGroupCommand : public ButtonGroupCommand {
       
   239 public:
       
   240     BreakButtonGroupCommand(QDesignerFormWindowInterface *formWindow);
       
   241     bool init(QButtonGroup *group);
       
   242 
       
   243     virtual void undo() { createButtonGroup(); }
       
   244     virtual void redo() { breakButtonGroup(); }
       
   245 };
       
   246 
       
   247 BreakButtonGroupCommand::BreakButtonGroupCommand(QDesignerFormWindowInterface *formWindow) :
       
   248     ButtonGroupCommand(QApplication::translate("Command", "Break button group"), formWindow)
       
   249 {
       
   250 }
       
   251 
       
   252 bool BreakButtonGroupCommand::init(QButtonGroup *group)
       
   253 {
       
   254     if (!group)
       
   255         return false;
       
   256     initialize(group->buttons(), group);
       
   257     setText(QApplication::translate("Command", "Break button group '%1'").arg(group->objectName()));
       
   258     return true;
       
   259 }
       
   260 
       
   261 // --------------- AddButtonsToGroupCommand
       
   262 // This command might be executed in a macro with a remove
       
   263 // command to move buttons from one group to a new one.
       
   264 class AddButtonsToGroupCommand : public ButtonGroupCommand {
       
   265 public:
       
   266     AddButtonsToGroupCommand(QDesignerFormWindowInterface *formWindow);
       
   267     void init(const ButtonList &bl, QButtonGroup *group);
       
   268 
       
   269     virtual void undo() { removeButtonsFromGroup(); }
       
   270     virtual void redo() { addButtonsToGroup(); }
       
   271 };
       
   272 
       
   273 AddButtonsToGroupCommand::AddButtonsToGroupCommand(QDesignerFormWindowInterface *formWindow) :
       
   274     ButtonGroupCommand(QApplication::translate("Command", "Add buttons to group"), formWindow)
       
   275 {
       
   276 }
       
   277 
       
   278 void AddButtonsToGroupCommand::init(const ButtonList &bl, QButtonGroup *group)
       
   279 {
       
   280     initialize(bl, group);
       
   281     //: Command description for adding buttons to a QButtonGroup
       
   282     setText(QApplication::translate("Command", "Add '%1' to '%2'").arg(nameList(bl), group->objectName()));
       
   283 }
       
   284 
       
   285 //--------------------  RemoveButtonsFromGroupCommand
       
   286 class RemoveButtonsFromGroupCommand : public ButtonGroupCommand {
       
   287 public:
       
   288     RemoveButtonsFromGroupCommand(QDesignerFormWindowInterface *formWindow);
       
   289     bool init(const ButtonList &bl);
       
   290 
       
   291     virtual void undo() {  addButtonsToGroup(); }
       
   292     virtual void redo() {  removeButtonsFromGroup(); }
       
   293 };
       
   294 
       
   295 RemoveButtonsFromGroupCommand::RemoveButtonsFromGroupCommand(QDesignerFormWindowInterface *formWindow) :
       
   296     ButtonGroupCommand(QApplication::translate("Command", "Remove buttons from group"), formWindow)
       
   297 {
       
   298 }
       
   299 
       
   300 bool RemoveButtonsFromGroupCommand::init(const ButtonList &bl)
       
   301 {
       
   302     if (bl.empty())
       
   303         return false;
       
   304     QButtonGroup *group = bl.front()->group();
       
   305     if (!group)
       
   306         return false;
       
   307     if (bl.size() >= group->buttons().size())
       
   308         return false;
       
   309     initialize(bl, group);
       
   310     //: Command description for removing buttons from a QButtonGroup
       
   311     setText(QApplication::translate("Command", "Remove '%1' from '%2'").arg(nameList(bl), group->objectName()));
       
   312     return true;
       
   313 }
       
   314 
       
   315 // --------  ButtonGroupMenu
       
   316 ButtonGroupMenu::ButtonGroupMenu(QObject *parent) :
       
   317     QObject(parent),
       
   318     m_selectGroupAction(new QAction(tr("Select members"), this)),
       
   319     m_breakGroupAction(new QAction(tr("Break"), this)),
       
   320     m_formWindow(0),
       
   321     m_buttonGroup(0),
       
   322     m_currentButton(0)
       
   323 {
       
   324     connect(m_breakGroupAction, SIGNAL(triggered()), this, SLOT(breakGroup()));
       
   325     connect(m_selectGroupAction, SIGNAL(triggered()), this, SLOT(selectGroup()));
       
   326 }
       
   327 
       
   328 void ButtonGroupMenu::initialize(QDesignerFormWindowInterface *formWindow, QButtonGroup *buttonGroup, QAbstractButton *currentButton)
       
   329 {
       
   330     m_buttonGroup = buttonGroup;
       
   331     m_currentButton = currentButton;
       
   332     m_formWindow = formWindow;
       
   333     Q_ASSERT(m_formWindow);
       
   334 
       
   335     const bool canBreak = buttonGroup != 0;
       
   336     m_breakGroupAction->setEnabled(canBreak);
       
   337     m_selectGroupAction->setEnabled(canBreak);
       
   338 }
       
   339 
       
   340 void ButtonGroupMenu::selectGroup()
       
   341 {
       
   342     // Select and make current button "current" again by selecting it last (if there is any)
       
   343     const ButtonList buttons = m_buttonGroup->buttons();
       
   344     m_formWindow->clearSelection(false);
       
   345     const ButtonList::const_iterator cend = buttons.constEnd();
       
   346     for (ButtonList::const_iterator it = buttons.constBegin(); it != cend; ++it)
       
   347         if (*it != m_currentButton)
       
   348             m_formWindow->selectWidget(*it, true);
       
   349     if (m_currentButton)
       
   350         m_formWindow->selectWidget(m_currentButton, true);
       
   351 }
       
   352 
       
   353 void ButtonGroupMenu::breakGroup()
       
   354 {
       
   355     BreakButtonGroupCommand *cmd = new BreakButtonGroupCommand(m_formWindow);
       
   356     if (cmd->init(m_buttonGroup)) {
       
   357         // Need a macro since the command might trigger additional commands
       
   358         QUndoStack *history = m_formWindow->commandHistory();
       
   359         history->beginMacro(cmd->text());
       
   360         history->push(cmd);
       
   361         history->endMacro();
       
   362     } else {
       
   363         qWarning("** WARNING Failed to initialize BreakButtonGroupCommand!");
       
   364         delete cmd;
       
   365     }
       
   366 }
       
   367 
       
   368 // ButtonGroupTaskMenu
       
   369 ButtonGroupTaskMenu::ButtonGroupTaskMenu(QButtonGroup *buttonGroup, QObject *parent) :
       
   370     QObject(parent),
       
   371     m_buttonGroup(buttonGroup)
       
   372 {
       
   373     m_taskActions.push_back(m_menu.breakGroupAction());
       
   374     m_taskActions.push_back(m_menu.selectGroupAction());
       
   375 }
       
   376 
       
   377 QAction *ButtonGroupTaskMenu::preferredEditAction() const
       
   378 {
       
   379     return m_menu.selectGroupAction();
       
   380 }
       
   381 
       
   382 QList<QAction*> ButtonGroupTaskMenu::taskActions() const
       
   383 {
       
   384     m_menu.initialize(QDesignerFormWindowInterface::findFormWindow(m_buttonGroup), m_buttonGroup);
       
   385     return m_taskActions;
       
   386 }
       
   387 
       
   388 // -------- Text area editor
       
   389 class ButtonTextTaskMenuInlineEditor : public  TaskMenuInlineEditor
       
   390 {
       
   391 public:
       
   392     ButtonTextTaskMenuInlineEditor(QAbstractButton *button, QObject *parent);
       
   393 
       
   394 protected:
       
   395     virtual QRect editRectangle() const;
       
   396 };
       
   397 
       
   398 ButtonTextTaskMenuInlineEditor::ButtonTextTaskMenuInlineEditor(QAbstractButton *button, QObject *parent) :
       
   399       TaskMenuInlineEditor(button, ValidationMultiLine, QLatin1String("text"), parent)
       
   400 {
       
   401 }
       
   402 
       
   403 QRect ButtonTextTaskMenuInlineEditor::editRectangle() const
       
   404 {
       
   405     QWidget *w = widget();
       
   406     QStyleOptionButton opt;
       
   407     opt.init(w);
       
   408     return w->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, w);
       
   409 }
       
   410 
       
   411 // -------- Command link button description editor
       
   412 class LinkDescriptionTaskMenuInlineEditor : public  TaskMenuInlineEditor
       
   413 {
       
   414 public:
       
   415     LinkDescriptionTaskMenuInlineEditor(QAbstractButton *button, QObject *parent);
       
   416 
       
   417 protected:
       
   418     virtual QRect editRectangle() const;
       
   419 };
       
   420 
       
   421 LinkDescriptionTaskMenuInlineEditor::LinkDescriptionTaskMenuInlineEditor(QAbstractButton *button, QObject *parent) :
       
   422       TaskMenuInlineEditor(button, ValidationMultiLine, QLatin1String("description"), parent)
       
   423 {
       
   424 }
       
   425 
       
   426 QRect LinkDescriptionTaskMenuInlineEditor::editRectangle() const
       
   427 {
       
   428     QWidget *w = widget(); // TODO: What is the exact description area?
       
   429     QStyleOptionButton opt;
       
   430     opt.init(w);
       
   431     return w->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, w);
       
   432 }
       
   433 
       
   434 // ----------- ButtonTaskMenu:
       
   435 
       
   436 ButtonTaskMenu::ButtonTaskMenu(QAbstractButton *button, QObject *parent)  :
       
   437     QDesignerTaskMenu(button, parent),
       
   438     m_assignGroupSubMenu(new QMenu),
       
   439     m_assignActionGroup(0),
       
   440     m_assignToGroupSubMenuAction(new QAction(tr("Assign to button group"), this)),
       
   441     m_currentGroupSubMenu(new QMenu),
       
   442     m_currentGroupSubMenuAction(new QAction(tr("Button group"), this)),
       
   443     m_createGroupAction(new QAction(tr("New button group"), this)),
       
   444     m_preferredEditAction(new QAction(tr("Change text..."), this)),
       
   445     m_removeFromGroupAction(new QAction(tr("None"), this))
       
   446 {
       
   447     connect(m_createGroupAction, SIGNAL(triggered()), this, SLOT(createGroup()));
       
   448     TaskMenuInlineEditor *textEditor = new ButtonTextTaskMenuInlineEditor(button, this);
       
   449     connect(m_preferredEditAction, SIGNAL(triggered()), textEditor, SLOT(editText()));
       
   450     connect(m_removeFromGroupAction, SIGNAL(triggered()), this, SLOT(removeFromGroup()));
       
   451 
       
   452     m_assignToGroupSubMenuAction->setMenu(m_assignGroupSubMenu);
       
   453 
       
   454     m_currentGroupSubMenu->addAction(m_groupMenu.breakGroupAction());
       
   455     m_currentGroupSubMenu->addAction(m_groupMenu.selectGroupAction());
       
   456     m_currentGroupSubMenuAction->setMenu(m_currentGroupSubMenu);
       
   457 
       
   458 
       
   459     m_taskActions.append(m_preferredEditAction);
       
   460     m_taskActions.append(m_assignToGroupSubMenuAction);
       
   461     m_taskActions.append(m_currentGroupSubMenuAction);
       
   462     m_taskActions.append(createSeparator());
       
   463 }
       
   464 
       
   465 ButtonTaskMenu::~ButtonTaskMenu()
       
   466 {
       
   467     delete m_assignGroupSubMenu;
       
   468     delete m_currentGroupSubMenu;
       
   469 }
       
   470 
       
   471 QAction *ButtonTaskMenu::preferredEditAction() const
       
   472 {
       
   473     return m_preferredEditAction;
       
   474 }
       
   475 
       
   476 bool ButtonTaskMenu::refreshAssignMenu(const QDesignerFormWindowInterface *fw, int buttonCount, SelectionType st, QButtonGroup *currentGroup)
       
   477 {
       
   478     // clear
       
   479     if (m_assignActionGroup) {
       
   480         delete m_assignActionGroup;
       
   481         m_assignActionGroup = 0;
       
   482     }
       
   483     m_assignGroupSubMenu->clear();
       
   484     if (st == OtherSelection)
       
   485         return false;
       
   486 
       
   487 
       
   488     // Assign to new: Need several
       
   489     const bool canAssignToNewGroup = buttonCount > 1;
       
   490     m_createGroupAction->setEnabled(canAssignToNewGroup);
       
   491     if (canAssignToNewGroup)
       
   492         m_assignGroupSubMenu->addAction(m_createGroupAction);
       
   493 
       
   494     // Assign to other
       
   495     const ButtonGroupList bl = ButtonGroupCommand::managedButtonGroups(fw);
       
   496     // Groups: Any groups to add to except the current?
       
   497     const int groupCount = bl.size();
       
   498     const bool hasAddGroups = groupCount > 1 || (groupCount == 1 && !bl.contains(currentGroup));
       
   499     if (hasAddGroups) {
       
   500         if (!m_assignGroupSubMenu->isEmpty())
       
   501             m_assignGroupSubMenu->addSeparator();
       
   502         // Create a new action group
       
   503         m_assignActionGroup = new QActionGroup(this);
       
   504         connect(m_assignActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(addToGroup(QAction*)));
       
   505 
       
   506         const ButtonGroupList::const_iterator cend = bl.constEnd();
       
   507         for (ButtonGroupList::const_iterator it = bl.constBegin(); it != cend; ++it) {
       
   508             QButtonGroup *bg = *it;
       
   509             if (*it != currentGroup) {
       
   510                 QAction *a = new QAction(bg->objectName(), m_assignGroupSubMenu);
       
   511                 a->setData(qVariantFromValue(bg));
       
   512                 m_assignActionGroup->addAction(a);
       
   513                 m_assignGroupSubMenu->addAction(a);
       
   514             }
       
   515         }
       
   516     }
       
   517     // Can remove: A homogenous selection of another group that does not completely break it.
       
   518     const bool canRemoveFromGroup = st == GroupedButtonSelection;
       
   519     m_removeFromGroupAction->setEnabled(canRemoveFromGroup);
       
   520     if (canRemoveFromGroup) {
       
   521         if (!m_assignGroupSubMenu->isEmpty())
       
   522             m_assignGroupSubMenu->addSeparator();
       
   523         m_assignGroupSubMenu->addAction(m_removeFromGroupAction);
       
   524     }
       
   525     return !m_assignGroupSubMenu->isEmpty();
       
   526 }
       
   527 
       
   528 QList<QAction*> ButtonTaskMenu::taskActions() const
       
   529 {
       
   530     ButtonTaskMenu *ncThis = const_cast<ButtonTaskMenu*>(this);
       
   531     QButtonGroup *buttonGroup;
       
   532 
       
   533     QDesignerFormWindowInterface *fw = formWindow();
       
   534     const SelectionType st = selectionType(fw->cursor(), &buttonGroup);
       
   535 
       
   536     m_groupMenu.initialize(fw, buttonGroup, button());
       
   537     const bool hasAssignOptions = ncThis->refreshAssignMenu(fw, fw->cursor()->selectedWidgetCount(), st, buttonGroup);
       
   538     m_assignToGroupSubMenuAction->setVisible(hasAssignOptions);
       
   539     // add/remove
       
   540     switch (st) {
       
   541     case UngroupedButtonSelection:
       
   542     case OtherSelection:
       
   543         m_currentGroupSubMenuAction->setVisible(false);
       
   544         break;
       
   545     case GroupedButtonSelection:
       
   546         m_currentGroupSubMenuAction->setText(tr("Button group '%1'").arg(buttonGroup->objectName()));
       
   547         m_currentGroupSubMenuAction->setVisible(true);
       
   548         break;
       
   549     }
       
   550 
       
   551     return m_taskActions + QDesignerTaskMenu::taskActions();
       
   552 }
       
   553 
       
   554 
       
   555 void ButtonTaskMenu::insertAction(int index, QAction *a)
       
   556 {
       
   557     m_taskActions.insert(index, a);
       
   558 }
       
   559 
       
   560 /* Create a button list from the cursor selection */
       
   561 static ButtonList buttonList(const QDesignerFormWindowCursorInterface *cursor)
       
   562 {
       
   563     ButtonList rc;
       
   564     const int selectionCount = cursor->selectedWidgetCount();
       
   565     for (int i = 0; i < selectionCount; i++) {
       
   566         QAbstractButton *ab = qobject_cast<QAbstractButton *>(cursor->selectedWidget(i));
       
   567         Q_ASSERT(ab);
       
   568         rc += ab;
       
   569     }
       
   570     return rc;
       
   571 }
       
   572 
       
   573 // Create a command to remove the buttons from their group
       
   574 // If it would leave an empty or 1-member group behind, create a break command instead
       
   575 
       
   576 static QUndoCommand *createRemoveButtonsCommand(QDesignerFormWindowInterface *fw, const ButtonList &bl)
       
   577 {
       
   578 
       
   579     QButtonGroup *bg = bl.front()->group();
       
   580     // Complete group or 1-member group?
       
   581     if (bl.size() >= bg->buttons().size() - 1) {
       
   582         BreakButtonGroupCommand *breakCmd = new BreakButtonGroupCommand(fw);
       
   583         if (!breakCmd->init(bg)) {
       
   584             qWarning("** WARNING Failed to initialize BreakButtonGroupCommand!");
       
   585             delete breakCmd;
       
   586             return 0;
       
   587         }
       
   588         return breakCmd;
       
   589     }
       
   590     // Just remove the buttons
       
   591 
       
   592     RemoveButtonsFromGroupCommand *removeCmd  = new RemoveButtonsFromGroupCommand(fw);
       
   593     if (!removeCmd->init(bl)) {
       
   594         qWarning("** WARNING Failed to initialize RemoveButtonsFromGroupCommand!");
       
   595         delete removeCmd;
       
   596         return 0;
       
   597     }
       
   598     return removeCmd;
       
   599 }
       
   600 
       
   601 void ButtonTaskMenu::createGroup()
       
   602 {
       
   603     QDesignerFormWindowInterface *fw = formWindow();
       
   604     const ButtonList bl = buttonList(fw->cursor());
       
   605     // Do we need to remove the buttons from an existing group?
       
   606     QUndoCommand *removeCmd = 0;
       
   607     if (bl.front()->group()) {
       
   608         removeCmd = createRemoveButtonsCommand(fw, bl);
       
   609         if (!removeCmd)
       
   610             return;
       
   611     }
       
   612     // Add cmd
       
   613     CreateButtonGroupCommand *addCmd = new CreateButtonGroupCommand(fw);
       
   614     if (!addCmd->init(bl)) {
       
   615         qWarning("** WARNING Failed to initialize CreateButtonGroupCommand!");
       
   616         delete addCmd;
       
   617         return;
       
   618     }
       
   619     // Need a macro [even if we only have the add command] since the command might trigger additional commands
       
   620     QUndoStack *history = fw->commandHistory();
       
   621     history->beginMacro(addCmd->text());
       
   622     if (removeCmd)
       
   623         history->push(removeCmd);
       
   624     history->push(addCmd);
       
   625     history->endMacro();
       
   626 }
       
   627 
       
   628 QAbstractButton *ButtonTaskMenu::button() const
       
   629 {
       
   630      return qobject_cast<QAbstractButton *>(widget());
       
   631 }
       
   632 
       
   633 // Figure out if we have a homogenous selections (buttons of the same group or no group)
       
   634 ButtonTaskMenu::SelectionType ButtonTaskMenu::selectionType(const QDesignerFormWindowCursorInterface *cursor, QButtonGroup **ptrToGroup) const
       
   635 {
       
   636     const int selectionCount = cursor->selectedWidgetCount();
       
   637     if (!selectionCount)
       
   638         return OtherSelection;
       
   639 
       
   640     QButtonGroup *commonGroup = 0;
       
   641     for (int i = 0; i < selectionCount; i++) {
       
   642         if (const QAbstractButton *ab = qobject_cast<const QAbstractButton *>(cursor->selectedWidget(i))) {
       
   643             QButtonGroup *buttonGroup = ab->group();
       
   644             if (i) {
       
   645                 if (buttonGroup != commonGroup)
       
   646                     return OtherSelection;
       
   647             } else {
       
   648                 commonGroup = buttonGroup;
       
   649             }
       
   650         } else {
       
   651             return OtherSelection;
       
   652         }
       
   653     }
       
   654 
       
   655     if (ptrToGroup)
       
   656         *ptrToGroup = commonGroup;
       
   657 
       
   658     return commonGroup ? GroupedButtonSelection : UngroupedButtonSelection;
       
   659 }
       
   660 
       
   661 void ButtonTaskMenu::addToGroup(QAction *a)
       
   662 {
       
   663     QButtonGroup *bg = qvariant_cast<QButtonGroup *>(a->data());
       
   664     Q_ASSERT(bg);
       
   665 
       
   666     QDesignerFormWindowInterface *fw = formWindow();
       
   667     const ButtonList bl = buttonList(fw->cursor());
       
   668     // Do we need to remove the buttons from an existing group?
       
   669     QUndoCommand *removeCmd = 0;
       
   670     if (bl.front()->group()) {
       
   671         removeCmd = createRemoveButtonsCommand(fw, bl);
       
   672         if (!removeCmd)
       
   673             return;
       
   674     }
       
   675     AddButtonsToGroupCommand *addCmd = new AddButtonsToGroupCommand(fw);
       
   676     addCmd->init(bl, bg);
       
   677 
       
   678     QUndoStack *history = fw->commandHistory();
       
   679     if (removeCmd) {
       
   680         history->beginMacro(addCmd->text());
       
   681         history->push(removeCmd);
       
   682         history->push(addCmd);
       
   683         history->endMacro();
       
   684     } else {
       
   685         history->push(addCmd);
       
   686     }
       
   687 }
       
   688 
       
   689 void ButtonTaskMenu::removeFromGroup()
       
   690 {
       
   691     QDesignerFormWindowInterface *fw = formWindow();
       
   692     if (QUndoCommand *cmd = createRemoveButtonsCommand(fw, buttonList(fw->cursor())))
       
   693         fw->commandHistory()->push(cmd);
       
   694 }
       
   695 
       
   696 // -------------- CommandLinkButtonTaskMenu
       
   697 
       
   698 CommandLinkButtonTaskMenu::CommandLinkButtonTaskMenu(QCommandLinkButton *button, QObject *parent) :
       
   699     ButtonTaskMenu(button, parent)
       
   700 {
       
   701     TaskMenuInlineEditor *descriptonEditor = new LinkDescriptionTaskMenuInlineEditor(button, this);
       
   702     QAction *descriptionAction = new QAction(tr("Change description..."), this);
       
   703     connect(descriptionAction, SIGNAL(triggered()), descriptonEditor, SLOT(editText()));
       
   704     insertAction(1, descriptionAction);
       
   705 }
       
   706 
       
   707 }
       
   708 
       
   709 QT_END_NAMESPACE