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