source: trunk/tools/designer/src/lib/shared/qdesigner_command.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 90.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt Designer of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdesigner_command_p.h"
43#include "qdesigner_propertycommand_p.h"
44#include "qdesigner_utils_p.h"
45#include "layout_p.h"
46#include "qlayout_widget_p.h"
47#include "qdesigner_widget_p.h"
48#include "qdesigner_menu_p.h"
49#include "shared_enums_p.h"
50#include "metadatabase_p.h"
51#include "formwindowbase_p.h"
52#include <abstractformbuilder.h>
53
54#include <QtDesigner/QDesignerFormWindowInterface>
55#include <QtDesigner/QDesignerFormEditorInterface>
56#include <QtDesigner/QDesignerPropertySheetExtension>
57#include <QtDesigner/QDesignerActionEditorInterface>
58#include <QtDesigner/QDesignerPropertyEditorInterface>
59#include <QtDesigner/QExtensionManager>
60#include <QtDesigner/QDesignerContainerExtension>
61#include <QtDesigner/QDesignerLayoutDecorationExtension>
62#include <QtDesigner/QDesignerWidgetFactoryInterface>
63#include <QtDesigner/QDesignerObjectInspectorInterface>
64#include <QtDesigner/QDesignerIntegrationInterface>
65#include <QtDesigner/QDesignerFormWindowCursorInterface>
66#include <QtCore/qdebug.h>
67#include <QtCore/QTextStream>
68#include <QtCore/QQueue>
69
70#include <QtGui/QMenuBar>
71#include <QtGui/QStatusBar>
72#include <QtGui/QToolBar>
73#include <QtGui/QToolBox>
74#include <QtGui/QStackedWidget>
75#include <QtGui/QTabWidget>
76#include <QtGui/QTableWidget>
77#include <QtGui/QTreeWidget>
78#include <QtGui/QListWidget>
79#include <QtGui/QComboBox>
80#include <QtGui/QSplitter>
81#include <QtGui/QDockWidget>
82#include <QtGui/QMainWindow>
83#include <QtGui/QWizardPage>
84#include <QtGui/QApplication>
85#include <QtGui/QFormLayout>
86
87Q_DECLARE_METATYPE(QWidgetList)
88
89QT_BEGIN_NAMESPACE
90
91static inline void setPropertySheetWindowTitle(const QDesignerFormEditorInterface *core, QObject *o, const QString &t)
92{
93 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), o)) {
94 const int idx = sheet->indexOf(QLatin1String("windowTitle"));
95 if (idx != -1) {
96 sheet->setProperty(idx, t);
97 sheet->setChanged(idx, true);
98 }
99 }
100}
101
102namespace qdesigner_internal {
103
104// Helpers for the dynamic properties that store Z/Widget order
105static const char *widgetOrderPropertyC = "_q_widgetOrder";
106static const char *zOrderPropertyC = "_q_zOrder";
107
108static void addToWidgetListDynamicProperty(QWidget *parentWidget, QWidget *widget, const char *name, int index = -1)
109{
110 QWidgetList list = qVariantValue<QWidgetList>(parentWidget->property(name));
111 list.removeAll(widget);
112 if (index >= 0 && index < list.size()) {
113 list.insert(index, widget);
114 } else {
115 list.append(widget);
116 }
117 parentWidget->setProperty(name, qVariantFromValue(list));
118}
119
120static int removeFromWidgetListDynamicProperty(QWidget *parentWidget, QWidget *widget, const char *name)
121{
122 QWidgetList list = qVariantValue<QWidgetList>(parentWidget->property(name));
123 const int firstIndex = list.indexOf(widget);
124 if (firstIndex != -1) {
125 list.removeAll(widget);
126 parentWidget->setProperty(name, qVariantFromValue(list));
127 }
128 return firstIndex;
129}
130
131// ---- InsertWidgetCommand ----
132InsertWidgetCommand::InsertWidgetCommand(QDesignerFormWindowInterface *formWindow) :
133 QDesignerFormWindowCommand(QString(), formWindow),
134 m_insertMode(QDesignerLayoutDecorationExtension::InsertWidgetMode),
135 m_layoutHelper(0),
136 m_widgetWasManaged(false)
137{
138}
139
140InsertWidgetCommand::~InsertWidgetCommand()
141{
142 delete m_layoutHelper;
143}
144
145void InsertWidgetCommand::init(QWidget *widget, bool already_in_form, int layoutRow, int layoutColumn)
146{
147 m_widget = widget;
148
149 setText(QApplication::translate("Command", "Insert '%1'").arg(widget->objectName()));
150
151 QWidget *parentWidget = m_widget->parentWidget();
152 QDesignerFormEditorInterface *core = formWindow()->core();
153 QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
154
155 m_insertMode = deco ? deco->currentInsertMode() : QDesignerLayoutDecorationExtension::InsertWidgetMode;
156 if (layoutRow >= 0 && layoutColumn >= 0) {
157 m_cell.first = layoutRow;
158 m_cell.second = layoutColumn;
159 } else {
160 m_cell = deco ? deco->currentCell() : qMakePair(0, 0);
161 }
162 m_widgetWasManaged = already_in_form;
163}
164
165static void recursiveUpdate(QWidget *w)
166{
167 w->update();
168
169 const QObjectList &l = w->children();
170 const QObjectList::const_iterator cend = l.end();
171 for ( QObjectList::const_iterator it = l.begin(); it != cend; ++it) {
172 if (QWidget *w = qobject_cast<QWidget*>(*it))
173 recursiveUpdate(w);
174 }
175}
176
177void InsertWidgetCommand::redo()
178{
179 QWidget *parentWidget = m_widget->parentWidget();
180 Q_ASSERT(parentWidget);
181
182 addToWidgetListDynamicProperty(parentWidget, m_widget, widgetOrderPropertyC);
183 addToWidgetListDynamicProperty(parentWidget, m_widget, zOrderPropertyC);
184
185 QDesignerFormEditorInterface *core = formWindow()->core();
186 QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
187
188 if (deco != 0) {
189 const LayoutInfo::Type type = LayoutInfo::layoutType(core, LayoutInfo::managedLayout(core, parentWidget));
190 m_layoutHelper = LayoutHelper::createLayoutHelper(type);
191 m_layoutHelper->pushState(core, parentWidget);
192 if (type == LayoutInfo::Grid) {
193 switch (m_insertMode) {
194 case QDesignerLayoutDecorationExtension::InsertRowMode: {
195 deco->insertRow(m_cell.first);
196 } break;
197
198 case QDesignerLayoutDecorationExtension::InsertColumnMode: {
199 deco->insertColumn(m_cell.second);
200 } break;
201
202 default: break;
203 } // end switch
204 }
205 deco->insertWidget(m_widget, m_cell);
206 }
207
208 if (!m_widgetWasManaged)
209 formWindow()->manageWidget(m_widget);
210 m_widget->show();
211 formWindow()->emitSelectionChanged();
212
213 if (parentWidget && parentWidget->layout()) {
214 recursiveUpdate(parentWidget);
215 parentWidget->layout()->invalidate();
216 }
217
218 refreshBuddyLabels();
219}
220
221void InsertWidgetCommand::undo()
222{
223 QWidget *parentWidget = m_widget->parentWidget();
224
225 QDesignerFormEditorInterface *core = formWindow()->core();
226 QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
227
228 if (deco) {
229 deco->removeWidget(m_widget);
230 m_layoutHelper->popState(core, parentWidget);
231 }
232
233 if (!m_widgetWasManaged) {
234 formWindow()->unmanageWidget(m_widget);
235 m_widget->hide();
236 }
237
238 removeFromWidgetListDynamicProperty(parentWidget, m_widget, widgetOrderPropertyC);
239 removeFromWidgetListDynamicProperty(parentWidget, m_widget, zOrderPropertyC);
240
241 formWindow()->emitSelectionChanged();
242
243 refreshBuddyLabels();
244}
245
246void InsertWidgetCommand::refreshBuddyLabels()
247{
248 typedef QList<QLabel*> LabelList;
249
250 const LabelList label_list = qFindChildren<QLabel*>(formWindow());
251 if (label_list.empty())
252 return;
253
254 const QString buddyProperty = QLatin1String("buddy");
255 const QByteArray objectNameU8 = m_widget->objectName().toUtf8();
256 // Re-set the buddy (The sheet locates the object by name and sets it)
257 const LabelList::const_iterator cend = label_list.constEnd();
258 for (LabelList::const_iterator it = label_list.constBegin(); it != cend; ++it ) {
259 if (QDesignerPropertySheetExtension* sheet = propertySheet(*it)) {
260 const int idx = sheet->indexOf(buddyProperty);
261 if (idx != -1) {
262 const QVariant value = sheet->property(idx);
263 if (value.toByteArray() == objectNameU8)
264 sheet->setProperty(idx, value);
265 }
266 }
267 }
268}
269
270// ---- ChangeZOrderCommand ----
271ChangeZOrderCommand::ChangeZOrderCommand(QDesignerFormWindowInterface *formWindow)
272 : QDesignerFormWindowCommand(QString(), formWindow)
273{
274}
275
276void ChangeZOrderCommand::init(QWidget *widget)
277{
278 Q_ASSERT(widget);
279
280 m_widget = widget;
281
282 setText(QApplication::translate("Command", "Change Z-order of '%1'").arg(widget->objectName()));
283
284 m_oldParentZOrder = qVariantValue<QWidgetList>(widget->parentWidget()->property("_q_zOrder"));
285 const int index = m_oldParentZOrder.indexOf(m_widget);
286 if (index != -1 && index + 1 < m_oldParentZOrder.count())
287 m_oldPreceding = m_oldParentZOrder.at(index + 1);
288}
289
290void ChangeZOrderCommand::redo()
291{
292 m_widget->parentWidget()->setProperty("_q_zOrder", qVariantFromValue(reorderWidget(m_oldParentZOrder, m_widget)));
293
294 reorder(m_widget);
295}
296
297void ChangeZOrderCommand::undo()
298{
299 m_widget->parentWidget()->setProperty("_q_zOrder", qVariantFromValue(m_oldParentZOrder));
300
301 if (m_oldPreceding)
302 m_widget->stackUnder(m_oldPreceding);
303 else
304 m_widget->raise();
305}
306
307// ---- RaiseWidgetCommand ----
308RaiseWidgetCommand::RaiseWidgetCommand(QDesignerFormWindowInterface *formWindow)
309 : ChangeZOrderCommand(formWindow)
310{
311}
312
313void RaiseWidgetCommand::init(QWidget *widget)
314{
315 ChangeZOrderCommand::init(widget);
316 setText(QApplication::translate("Command", "Raise '%1'").arg(widget->objectName()));
317}
318
319QWidgetList RaiseWidgetCommand::reorderWidget(const QWidgetList &list, QWidget *widget) const
320{
321 QWidgetList l = list;
322 l.removeAll(widget);
323 l.append(widget);
324 return l;
325}
326
327void RaiseWidgetCommand::reorder(QWidget *widget) const
328{
329 widget->raise();
330}
331
332// ---- LowerWidgetCommand ----
333LowerWidgetCommand::LowerWidgetCommand(QDesignerFormWindowInterface *formWindow)
334 : ChangeZOrderCommand(formWindow)
335{
336}
337
338QWidgetList LowerWidgetCommand::reorderWidget(const QWidgetList &list, QWidget *widget) const
339{
340 QWidgetList l = list;
341 l.removeAll(widget);
342 l.prepend(widget);
343 return l;
344}
345
346void LowerWidgetCommand::init(QWidget *widget)
347{
348 ChangeZOrderCommand::init(widget);
349 setText(QApplication::translate("Command", "Lower '%1'").arg(widget->objectName()));
350}
351
352void LowerWidgetCommand::reorder(QWidget *widget) const
353{
354 widget->lower();
355}
356
357// ---- ManageWidgetCommandHelper
358ManageWidgetCommandHelper::ManageWidgetCommandHelper() :
359 m_widget(0)
360{
361}
362
363void ManageWidgetCommandHelper::init(const QDesignerFormWindowInterface *fw, QWidget *widget)
364{
365 m_widget = widget;
366 m_managedChildren.clear();
367
368 const QWidgetList children = qFindChildren<QWidget *>(m_widget);
369 if (children.empty())
370 return;
371
372 m_managedChildren.reserve(children.size());
373 const QWidgetList::const_iterator lcend = children.constEnd();
374 for (QWidgetList::const_iterator it = children.constBegin(); it != lcend; ++it)
375 if (fw->isManaged(*it))
376 m_managedChildren.push_back(*it);
377}
378
379void ManageWidgetCommandHelper::init(QWidget *widget, const WidgetVector &managedChildren)
380{
381 m_widget = widget;
382 m_managedChildren = managedChildren;
383}
384
385void ManageWidgetCommandHelper::manage(QDesignerFormWindowInterface *fw)
386{
387 // Manage the managed children after parent
388 fw->manageWidget(m_widget);
389 if (!m_managedChildren.empty()) {
390 const WidgetVector::const_iterator lcend = m_managedChildren.constEnd();
391 for (WidgetVector::const_iterator it = m_managedChildren.constBegin(); it != lcend; ++it)
392 fw->manageWidget(*it);
393 }
394}
395
396void ManageWidgetCommandHelper::unmanage(QDesignerFormWindowInterface *fw)
397{
398 // Unmanage the managed children first
399 if (!m_managedChildren.empty()) {
400 const WidgetVector::const_iterator lcend = m_managedChildren.constEnd();
401 for (WidgetVector::const_iterator it = m_managedChildren.constBegin(); it != lcend; ++it)
402 fw->unmanageWidget(*it);
403 }
404 fw->unmanageWidget(m_widget);
405}
406
407// ---- DeleteWidgetCommand ----
408DeleteWidgetCommand::DeleteWidgetCommand(QDesignerFormWindowInterface *formWindow) :
409 QDesignerFormWindowCommand(QString(), formWindow),
410 m_layoutType(LayoutInfo::NoLayout),
411 m_layoutHelper(0),
412 m_flags(0),
413 m_splitterIndex(-1),
414 m_layoutSimplified(false),
415 m_formItem(0),
416 m_tabOrderIndex(-1),
417 m_widgetOrderIndex(-1),
418 m_zOrderIndex(-1)
419{
420}
421
422DeleteWidgetCommand::~DeleteWidgetCommand()
423{
424 delete m_layoutHelper;
425}
426
427void DeleteWidgetCommand::init(QWidget *widget, unsigned flags)
428{
429 m_widget = widget;
430 m_parentWidget = widget->parentWidget();
431 m_geometry = widget->geometry();
432 m_flags = flags;
433 m_layoutType = LayoutInfo::NoLayout;
434 m_splitterIndex = -1;
435 bool isManaged; // Check for a managed layout
436 QLayout *layout;
437 m_layoutType = LayoutInfo::laidoutWidgetType(formWindow()->core(), m_widget, &isManaged, &layout);
438 if (!isManaged)
439 m_layoutType = LayoutInfo::NoLayout;
440 switch (m_layoutType) {
441 case LayoutInfo::HSplitter:
442 case LayoutInfo::VSplitter: {
443 QSplitter *splitter = qobject_cast<QSplitter *>(m_parentWidget);
444 Q_ASSERT(splitter);
445 m_splitterIndex = splitter->indexOf(widget);
446 }
447 break;
448 case LayoutInfo::NoLayout:
449 break;
450 default:
451 m_layoutHelper = LayoutHelper::createLayoutHelper(m_layoutType);
452 m_layoutPosition = m_layoutHelper->itemInfo(layout, m_widget);
453 break;
454 }
455
456 m_formItem = formWindow()->core()->metaDataBase()->item(formWindow());
457 m_tabOrderIndex = m_formItem->tabOrder().indexOf(widget);
458
459 // Build the list of managed children
460 m_manageHelper.init(formWindow(), m_widget);
461
462 setText(QApplication::translate("Command", "Delete '%1'").arg(widget->objectName()));
463}
464
465void DeleteWidgetCommand::redo()
466{
467 formWindow()->clearSelection();
468 QDesignerFormEditorInterface *core = formWindow()->core();
469
470 if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_parentWidget)) {
471 const int count = c->count();
472 for (int i=0; i<count; ++i) {
473 if (c->widget(i) == m_widget) {
474 c->remove(i);
475 return;
476 }
477 }
478 }
479
480 m_widgetOrderIndex = removeFromWidgetListDynamicProperty(m_parentWidget, m_widget, widgetOrderPropertyC);
481 m_zOrderIndex = removeFromWidgetListDynamicProperty(m_parentWidget, m_widget, zOrderPropertyC);
482
483 if (QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), m_parentWidget))
484 deco->removeWidget(m_widget);
485
486 if (m_layoutHelper)
487 switch (m_layoutType) {
488 case LayoutInfo::NoLayout:
489 case LayoutInfo::HSplitter:
490 case LayoutInfo::VSplitter:
491 break;
492 default:
493 // Attempt to simplify grids if a row/column becomes empty
494 m_layoutSimplified = (m_flags & DoNotSimplifyLayout) ? false : m_layoutHelper->canSimplify(core, m_parentWidget, m_layoutPosition);
495 if (m_layoutSimplified) {
496 m_layoutHelper->pushState(core, m_parentWidget);
497 m_layoutHelper->simplify(core, m_parentWidget, m_layoutPosition);
498 }
499 break;
500 }
501
502 if (!(m_flags & DoNotUnmanage))
503 m_manageHelper.unmanage(formWindow());
504
505 m_widget->setParent(formWindow());
506 m_widget->hide();
507
508 if (m_tabOrderIndex != -1) {
509 QList<QWidget*> tab_order = m_formItem->tabOrder();
510 tab_order.removeAt(m_tabOrderIndex);
511 m_formItem->setTabOrder(tab_order);
512 }
513}
514
515void DeleteWidgetCommand::undo()
516{
517 QDesignerFormEditorInterface *core = formWindow()->core();
518 formWindow()->clearSelection();
519
520 m_widget->setParent(m_parentWidget);
521
522 if (QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_parentWidget)) {
523 c->addWidget(m_widget);
524 return;
525 }
526
527 addToWidgetListDynamicProperty(m_parentWidget, m_widget, widgetOrderPropertyC, m_widgetOrderIndex);
528 addToWidgetListDynamicProperty(m_parentWidget, m_widget, zOrderPropertyC, m_zOrderIndex);
529
530 m_widget->setGeometry(m_geometry);
531
532 if (!(m_flags & DoNotUnmanage))
533 m_manageHelper.manage(formWindow());
534 // ### set up alignment
535 switch (m_layoutType) {
536 case LayoutInfo::NoLayout:
537 break;
538 case LayoutInfo::HSplitter:
539 case LayoutInfo::VSplitter: {
540 QSplitter *splitter = qobject_cast<QSplitter *>(m_widget->parent());
541 Q_ASSERT(splitter);
542 splitter->insertWidget(m_splitterIndex, m_widget);
543 } break;
544 default: {
545 Q_ASSERT(m_layoutHelper);
546 if (m_layoutSimplified)
547 m_layoutHelper->popState(core, m_parentWidget);
548 QLayout *layout = LayoutInfo::managedLayout(core, m_parentWidget);
549 Q_ASSERT(m_layoutType == LayoutInfo::layoutType(core, layout));
550 m_layoutHelper->insertWidget(layout, m_layoutPosition, m_widget);
551 }
552 break;
553 }
554
555 m_widget->show();
556
557 if (m_tabOrderIndex != -1) {
558 QList<QWidget*> tab_order = m_formItem->tabOrder();
559 tab_order.insert(m_tabOrderIndex, m_widget);
560 m_formItem->setTabOrder(tab_order);
561 }
562}
563
564// ---- ReparentWidgetCommand ----
565ReparentWidgetCommand::ReparentWidgetCommand(QDesignerFormWindowInterface *formWindow)
566 : QDesignerFormWindowCommand(QString(), formWindow)
567{
568}
569
570void ReparentWidgetCommand::init(QWidget *widget, QWidget *parentWidget)
571{
572 Q_ASSERT(widget);
573
574 m_widget = widget;
575 m_oldParentWidget = widget->parentWidget();
576 m_newParentWidget = parentWidget;
577
578 m_oldPos = m_widget->pos();
579 m_newPos = m_newParentWidget->mapFromGlobal(m_oldParentWidget->mapToGlobal(m_oldPos));
580
581 setText(QApplication::translate("Command", "Reparent '%1'").arg(widget->objectName()));
582
583 m_oldParentList = qVariantValue<QWidgetList>(m_oldParentWidget->property("_q_widgetOrder"));
584 m_oldParentZOrder = qVariantValue<QWidgetList>(m_oldParentWidget->property("_q_zOrder"));
585}
586
587void ReparentWidgetCommand::redo()
588{
589 m_widget->setParent(m_newParentWidget);
590 m_widget->move(m_newPos);
591
592 QWidgetList oldList = m_oldParentList;
593 oldList.removeAll(m_widget);
594 m_oldParentWidget->setProperty("_q_widgetOrder", qVariantFromValue(oldList));
595
596 QWidgetList newList = qVariantValue<QWidgetList>(m_newParentWidget->property("_q_widgetOrder"));
597 newList.append(m_widget);
598 m_newParentWidget->setProperty("_q_widgetOrder", qVariantFromValue(newList));
599
600 QWidgetList oldZOrder = m_oldParentZOrder;
601 oldZOrder.removeAll(m_widget);
602 m_oldParentWidget->setProperty("_q_zOrder", qVariantFromValue(oldZOrder));
603
604 QWidgetList newZOrder = qVariantValue<QWidgetList>(m_newParentWidget->property("_q_zOrder"));
605 newZOrder.append(m_widget);
606 m_newParentWidget->setProperty("_q_zOrder", qVariantFromValue(newZOrder));
607
608 m_widget->show();
609 core()->objectInspector()->setFormWindow(formWindow());
610}
611
612void ReparentWidgetCommand::undo()
613{
614 m_widget->setParent(m_oldParentWidget);
615 m_widget->move(m_oldPos);
616
617 m_oldParentWidget->setProperty("_q_widgetOrder", qVariantFromValue(m_oldParentList));
618
619 QWidgetList newList = qVariantValue<QWidgetList>(m_newParentWidget->property("_q_widgetOrder"));
620 newList.removeAll(m_widget);
621 m_newParentWidget->setProperty("_q_widgetOrder", qVariantFromValue(newList));
622
623 m_oldParentWidget->setProperty("_q_zOrder", qVariantFromValue(m_oldParentZOrder));
624
625 QWidgetList newZOrder = qVariantValue<QWidgetList>(m_newParentWidget->property("_q_zOrder"));
626 m_newParentWidget->setProperty("_q_zOrder", qVariantFromValue(newZOrder));
627
628 m_widget->show();
629 core()->objectInspector()->setFormWindow(formWindow());
630}
631
632PromoteToCustomWidgetCommand::PromoteToCustomWidgetCommand
633 (QDesignerFormWindowInterface *formWindow)
634 : QDesignerFormWindowCommand(QApplication::translate("Command", "Promote to custom widget"), formWindow)
635{
636}
637
638void PromoteToCustomWidgetCommand::init(const WidgetList &widgets,const QString &customClassName)
639{
640 m_widgets = widgets;
641 m_customClassName = customClassName;
642}
643
644void PromoteToCustomWidgetCommand::redo()
645{
646 foreach (QWidget *w, m_widgets) {
647 if (w)
648 promoteWidget(core(), w, m_customClassName);
649 }
650 updateSelection();
651}
652
653void PromoteToCustomWidgetCommand::updateSelection()
654{
655 // Update class names in ObjectInspector, PropertyEditor
656 QDesignerFormWindowInterface *fw = formWindow();
657 QDesignerFormEditorInterface *core = fw->core();
658 core->objectInspector()->setFormWindow(fw);
659 if (QObject *o = core->propertyEditor()->object())
660 core->propertyEditor()->setObject(o);
661}
662
663void PromoteToCustomWidgetCommand::undo()
664{
665 foreach (QWidget *w, m_widgets) {
666 if (w)
667 demoteWidget(core(), w);
668 }
669 updateSelection();
670}
671
672// ---- DemoteFromCustomWidgetCommand ----
673
674DemoteFromCustomWidgetCommand::DemoteFromCustomWidgetCommand
675 (QDesignerFormWindowInterface *formWindow) :
676 QDesignerFormWindowCommand(QApplication::translate("Command", "Demote from custom widget"), formWindow),
677 m_promote_cmd(formWindow)
678{
679}
680
681void DemoteFromCustomWidgetCommand::init(const WidgetList &promoted)
682{
683 m_promote_cmd.init(promoted, promotedCustomClassName(core(), promoted.front()));
684}
685
686void DemoteFromCustomWidgetCommand::redo()
687{
688 m_promote_cmd.undo();
689}
690
691void DemoteFromCustomWidgetCommand::undo()
692{
693 m_promote_cmd.redo();
694}
695
696// ---------- CursorSelectionState
697CursorSelectionState::CursorSelectionState()
698{
699}
700
701void CursorSelectionState::save(const QDesignerFormWindowInterface *formWindow)
702{
703 const QDesignerFormWindowCursorInterface *cursor = formWindow->cursor();
704 m_selection.clear();
705 m_current = cursor->current();
706 if (cursor->hasSelection()) {
707 const int count = cursor->selectedWidgetCount();
708 for(int i = 0; i < count; i++)
709 m_selection.push_back(cursor->selectedWidget(i));
710 }
711}
712
713void CursorSelectionState::restore(QDesignerFormWindowInterface *formWindow) const
714{
715 if (m_selection.empty()) {
716 formWindow->clearSelection(true);
717 } else {
718 // Select current as last
719 formWindow->clearSelection(false);
720 const WidgetPointerList::const_iterator cend = m_selection.constEnd();
721 for (WidgetPointerList::const_iterator it = m_selection.constBegin(); it != cend; ++it)
722 if (QWidget *w = *it)
723 if (w != m_current)
724 formWindow->selectWidget(*it, true);
725 if (m_current)
726 formWindow->selectWidget(m_current, true);
727 }
728}
729
730// ---- LayoutCommand ----
731
732LayoutCommand::LayoutCommand(QDesignerFormWindowInterface *formWindow) :
733 QDesignerFormWindowCommand(QString(), formWindow),
734 m_setup(false)
735{
736}
737
738LayoutCommand::~LayoutCommand()
739{
740 delete m_layout;
741}
742
743void LayoutCommand::init(QWidget *parentWidget, const QWidgetList &widgets,
744 LayoutInfo::Type layoutType, QWidget *layoutBase,
745 bool reparentLayoutWidget)
746{
747 m_parentWidget = parentWidget;
748 m_widgets = widgets;
749 formWindow()->simplifySelection(&m_widgets);
750 m_layout = Layout::createLayout(widgets, parentWidget, formWindow(), layoutBase, layoutType);
751 m_layout->setReparentLayoutWidget(reparentLayoutWidget);
752
753 switch (layoutType) {
754 case LayoutInfo::Grid:
755 setText(QApplication::translate("Command", "Lay out using grid"));
756 break;
757 case LayoutInfo::VBox:
758 setText(QApplication::translate("Command", "Lay out vertically"));
759 break;
760 case LayoutInfo::HBox:
761 setText(QApplication::translate("Command", "Lay out horizontally"));
762 break;
763 default:
764 break;
765 }
766 // Delayed setup to avoid confusion in case we are chained
767 // with a BreakLayout in a morph layout macro
768 m_setup = false;
769}
770
771void LayoutCommand::redo()
772{
773 if (!m_setup) {
774 m_layout->setup();
775 m_cursorSelectionState.save(formWindow());
776 m_setup = true;
777 }
778 m_layout->doLayout();
779 core()->objectInspector()->setFormWindow(formWindow());
780}
781
782void LayoutCommand::undo()
783{
784 QDesignerFormEditorInterface *core = formWindow()->core();
785
786 QWidget *lb = m_layout->layoutBaseWidget();
787 QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb);
788 m_layout->undoLayout();
789 delete deco; // release the extension
790
791 // ### generalize (put in function)
792 if (!m_layoutBase && lb != 0 && !(qobject_cast<QLayoutWidget*>(lb) || qobject_cast<QSplitter*>(lb))) {
793 core->metaDataBase()->add(lb);
794 lb->show();
795 }
796 m_cursorSelectionState.restore(formWindow());
797 core->objectInspector()->setFormWindow(formWindow());
798}
799
800// ---- BreakLayoutCommand ----
801BreakLayoutCommand::BreakLayoutCommand(QDesignerFormWindowInterface *formWindow) :
802 QDesignerFormWindowCommand(QApplication::translate("Command", "Break layout"), formWindow),
803 m_layoutHelper(0),
804 m_properties(0),
805 m_propertyMask(0)
806{
807}
808
809BreakLayoutCommand::~BreakLayoutCommand()
810{
811 delete m_layoutHelper;
812 delete m_layout;
813 delete m_properties;
814}
815
816const LayoutProperties *BreakLayoutCommand::layoutProperties() const
817{
818 return m_properties;
819}
820
821int BreakLayoutCommand::propertyMask() const
822{
823 return m_propertyMask;
824}
825
826void BreakLayoutCommand::init(const QWidgetList &widgets, QWidget *layoutBase, bool reparentLayoutWidget)
827{
828 enum Type { SplitterLayout, LayoutHasMarginSpacing, LayoutHasState };
829
830 const QDesignerFormEditorInterface *core = formWindow()->core();
831 m_widgets = widgets;
832 m_layoutBase = core->widgetFactory()->containerOfWidget(layoutBase);
833 QLayout *layoutToBeBroken;
834 const LayoutInfo::Type layoutType = LayoutInfo::managedLayoutType(core, m_layoutBase, &layoutToBeBroken);
835 m_layout = Layout::createLayout(widgets, m_layoutBase, formWindow(), layoutBase, layoutType);
836 m_layout->setReparentLayoutWidget(reparentLayoutWidget);
837
838 Type type = LayoutHasState;
839 switch (layoutType) {
840 case LayoutInfo::NoLayout:
841 case LayoutInfo::HSplitter:
842 case LayoutInfo::VSplitter:
843 type = SplitterLayout;
844 break;
845 case LayoutInfo::HBox:
846 case LayoutInfo::VBox: // Margin/spacing need to be saved
847 type = LayoutHasMarginSpacing;
848 break;
849 default: // Margin/spacing need to be saved + has a state (empty rows/columns of a grid)
850 type = LayoutHasState;
851 break;
852 }
853 Q_ASSERT(m_layout != 0);
854 m_layout->sort();
855
856
857 if (type >= LayoutHasMarginSpacing) {
858 m_properties = new LayoutProperties;
859 m_propertyMask = m_properties->fromPropertySheet(core, layoutToBeBroken, LayoutProperties::AllProperties);
860 }
861 if (type >= LayoutHasState)
862 m_layoutHelper = LayoutHelper::createLayoutHelper(layoutType);
863 m_cursorSelectionState.save(formWindow());
864}
865
866void BreakLayoutCommand::redo()
867{
868 if (!m_layout)
869 return;
870
871 QDesignerFormEditorInterface *core = formWindow()->core();
872 QWidget *lb = m_layout->layoutBaseWidget();
873 QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb);
874 formWindow()->clearSelection(false);
875 if (m_layoutHelper)
876 m_layoutHelper->pushState(core, m_layoutBase);
877 m_layout->breakLayout();
878 delete deco; // release the extension
879
880 foreach (QWidget *widget, m_widgets) {
881 widget->resize(widget->size().expandedTo(QSize(16, 16)));
882 }
883 // Update unless we are in an intermediate state of morphing layout
884 // in which a QLayoutWidget will have no layout at all.
885 if (m_layout->reparentLayoutWidget())
886 core->objectInspector()->setFormWindow(formWindow());
887}
888
889void BreakLayoutCommand::undo()
890{
891 if (!m_layout)
892 return;
893
894 formWindow()->clearSelection(false);
895 m_layout->doLayout();
896 if (m_layoutHelper)
897 m_layoutHelper->popState(formWindow()->core(), m_layoutBase);
898
899 QLayout *layoutToRestored = LayoutInfo::managedLayout(formWindow()->core(), m_layoutBase);
900 if (m_properties && m_layoutBase && layoutToRestored)
901 m_properties->toPropertySheet(formWindow()->core(), layoutToRestored, m_propertyMask);
902 m_cursorSelectionState.restore(formWindow());
903 core()->objectInspector()->setFormWindow(formWindow());
904}
905// ---- SimplifyLayoutCommand
906SimplifyLayoutCommand::SimplifyLayoutCommand(QDesignerFormWindowInterface *formWindow) :
907 QDesignerFormWindowCommand(QApplication::translate("Command", "Simplify Grid Layout"), formWindow),
908 m_area(0, 0, 32767, 32767),
909 m_layoutBase(0),
910 m_layoutHelper(0),
911 m_layoutSimplified(false)
912{
913}
914
915SimplifyLayoutCommand::~SimplifyLayoutCommand()
916{
917 delete m_layoutHelper;
918}
919
920bool SimplifyLayoutCommand::canSimplify(QDesignerFormEditorInterface *core, const QWidget *w, int *layoutType)
921{
922 if (!w)
923 return false;
924 QLayout *layout;
925 const LayoutInfo::Type type = LayoutInfo::managedLayoutType(core, w, &layout);
926 if (layoutType)
927 *layoutType = type;
928 if (!layout)
929 return false;
930 switch (type) { // Known negatives
931 case LayoutInfo::NoLayout:
932 case LayoutInfo::UnknownLayout:
933 case LayoutInfo::HSplitter:
934 case LayoutInfo::VSplitter:
935 case LayoutInfo::HBox:
936 case LayoutInfo::VBox:
937 return false;
938 default:
939 break;
940 }
941 switch (type) {
942 case LayoutInfo::Grid:
943 return QLayoutSupport::canSimplifyQuickCheck(qobject_cast<QGridLayout*>(layout));
944 case LayoutInfo::Form:
945 return QLayoutSupport::canSimplifyQuickCheck(qobject_cast<const QFormLayout*>(layout));
946 default:
947 break;
948 }
949 return false;
950}
951
952bool SimplifyLayoutCommand::init(QWidget *layoutBase)
953{
954 QDesignerFormEditorInterface *core = formWindow()->core();
955 m_layoutSimplified = false;
956 int type;
957 if (canSimplify(core, layoutBase, &type)) {
958 m_layoutBase = layoutBase;
959 m_layoutHelper = LayoutHelper::createLayoutHelper(type);
960 m_layoutSimplified = m_layoutHelper->canSimplify(core, layoutBase, m_area);
961 }
962 return m_layoutSimplified;
963}
964
965void SimplifyLayoutCommand::redo()
966{
967 const QDesignerFormEditorInterface *core = formWindow()->core();
968 if (m_layoutSimplified) {
969 m_layoutHelper->pushState(core, m_layoutBase);
970 m_layoutHelper->simplify(core, m_layoutBase, m_area);
971 }
972}
973void SimplifyLayoutCommand::undo()
974{
975 if (m_layoutSimplified)
976 m_layoutHelper->popState(formWindow()->core(), m_layoutBase);
977}
978
979// ---- ToolBoxCommand ----
980ToolBoxCommand::ToolBoxCommand(QDesignerFormWindowInterface *formWindow) :
981 QDesignerFormWindowCommand(QString(), formWindow),
982 m_index(-1)
983{
984}
985
986ToolBoxCommand::~ToolBoxCommand()
987{
988}
989
990void ToolBoxCommand::init(QToolBox *toolBox)
991{
992 m_toolBox = toolBox;
993 m_index = m_toolBox->currentIndex();
994 m_widget = m_toolBox->widget(m_index);
995 m_itemText = m_toolBox->itemText(m_index);
996 m_itemIcon = m_toolBox->itemIcon(m_index);
997}
998
999void ToolBoxCommand::removePage()
1000{
1001 m_toolBox->removeItem(m_index);
1002
1003 m_widget->hide();
1004 m_widget->setParent(formWindow());
1005 formWindow()->clearSelection();
1006 formWindow()->selectWidget(m_toolBox, true);
1007
1008}
1009
1010void ToolBoxCommand::addPage()
1011{
1012 m_widget->setParent(m_toolBox);
1013 m_toolBox->insertItem(m_index, m_widget, m_itemIcon, m_itemText);
1014 m_toolBox->setCurrentIndex(m_index);
1015
1016 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(formWindow()->core()->extensionManager(), m_toolBox);
1017 if (sheet) {
1018 qdesigner_internal::PropertySheetStringValue itemText(m_itemText);
1019 sheet->setProperty(sheet->indexOf(QLatin1String("currentItemText")), qVariantFromValue(itemText));
1020 }
1021
1022 m_widget->show();
1023 formWindow()->clearSelection();
1024 formWindow()->selectWidget(m_toolBox, true);
1025}
1026
1027// ---- MoveToolBoxPageCommand ----
1028MoveToolBoxPageCommand::MoveToolBoxPageCommand(QDesignerFormWindowInterface *formWindow) :
1029 ToolBoxCommand(formWindow),
1030 m_newIndex(-1),
1031 m_oldIndex(-1)
1032{
1033}
1034
1035MoveToolBoxPageCommand::~MoveToolBoxPageCommand()
1036{
1037}
1038
1039void MoveToolBoxPageCommand::init(QToolBox *toolBox, QWidget *page, int newIndex)
1040{
1041 ToolBoxCommand::init(toolBox);
1042 setText(QApplication::translate("Command", "Move Page"));
1043
1044 m_widget = page;
1045 m_oldIndex = m_toolBox->indexOf(m_widget);
1046 m_itemText = m_toolBox->itemText(m_oldIndex);
1047 m_itemIcon = m_toolBox->itemIcon(m_oldIndex);
1048 m_newIndex = newIndex;
1049}
1050
1051void MoveToolBoxPageCommand::redo()
1052{
1053 m_toolBox->removeItem(m_oldIndex);
1054 m_toolBox->insertItem(m_newIndex, m_widget, m_itemIcon, m_itemText);
1055}
1056
1057void MoveToolBoxPageCommand::undo()
1058{
1059 m_toolBox->removeItem(m_newIndex);
1060 m_toolBox->insertItem(m_oldIndex, m_widget, m_itemIcon, m_itemText);
1061}
1062
1063// ---- DeleteToolBoxPageCommand ----
1064DeleteToolBoxPageCommand::DeleteToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
1065 : ToolBoxCommand(formWindow)
1066{
1067}
1068
1069DeleteToolBoxPageCommand::~DeleteToolBoxPageCommand()
1070{
1071}
1072
1073void DeleteToolBoxPageCommand::init(QToolBox *toolBox)
1074{
1075 ToolBoxCommand::init(toolBox);
1076 setText(QApplication::translate("Command", "Delete Page"));
1077}
1078
1079void DeleteToolBoxPageCommand::redo()
1080{
1081 removePage();
1082 cheapUpdate();
1083}
1084
1085void DeleteToolBoxPageCommand::undo()
1086{
1087 addPage();
1088 cheapUpdate();
1089}
1090
1091// ---- AddToolBoxPageCommand ----
1092AddToolBoxPageCommand::AddToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
1093 : ToolBoxCommand(formWindow)
1094{
1095}
1096
1097AddToolBoxPageCommand::~AddToolBoxPageCommand()
1098{
1099}
1100
1101void AddToolBoxPageCommand::init(QToolBox *toolBox)
1102{
1103 init(toolBox, InsertBefore);
1104}
1105
1106void AddToolBoxPageCommand::init(QToolBox *toolBox, InsertionMode mode)
1107{
1108 m_toolBox = toolBox;
1109
1110 m_index = m_toolBox->currentIndex();
1111 if (mode == InsertAfter)
1112 m_index++;
1113 m_widget = new QDesignerWidget(formWindow(), m_toolBox);
1114 m_itemText = QApplication::translate("Command", "Page");
1115 m_itemIcon = QIcon();
1116 m_widget->setObjectName(QLatin1String("page"));
1117 formWindow()->ensureUniqueObjectName(m_widget);
1118
1119 setText(QApplication::translate("Command", "Insert Page"));
1120
1121 QDesignerFormEditorInterface *core = formWindow()->core();
1122 core->metaDataBase()->add(m_widget);
1123}
1124
1125void AddToolBoxPageCommand::redo()
1126{
1127 addPage();
1128 cheapUpdate();
1129}
1130
1131void AddToolBoxPageCommand::undo()
1132{
1133 removePage();
1134 cheapUpdate();
1135}
1136
1137// ---- TabWidgetCommand ----
1138TabWidgetCommand::TabWidgetCommand(QDesignerFormWindowInterface *formWindow) :
1139 QDesignerFormWindowCommand(QString(), formWindow),
1140 m_index(-1)
1141{
1142}
1143
1144TabWidgetCommand::~TabWidgetCommand()
1145{
1146}
1147
1148void TabWidgetCommand::init(QTabWidget *tabWidget)
1149{
1150 m_tabWidget = tabWidget;
1151 m_index = m_tabWidget->currentIndex();
1152 m_widget = m_tabWidget->widget(m_index);
1153 m_itemText = m_tabWidget->tabText(m_index);
1154 m_itemIcon = m_tabWidget->tabIcon(m_index);
1155}
1156
1157void TabWidgetCommand::removePage()
1158{
1159 m_tabWidget->removeTab(m_index);
1160
1161 m_widget->hide();
1162 m_widget->setParent(formWindow());
1163 m_tabWidget->setCurrentIndex(qMin(m_index, m_tabWidget->count()));
1164
1165 formWindow()->clearSelection();
1166 formWindow()->selectWidget(m_tabWidget, true);
1167}
1168
1169void TabWidgetCommand::addPage()
1170{
1171 m_widget->setParent(0);
1172 m_tabWidget->insertTab(m_index, m_widget, m_itemIcon, m_itemText);
1173 m_widget->show();
1174 m_tabWidget->setCurrentIndex(m_index);
1175
1176 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(formWindow()->core()->extensionManager(), m_tabWidget);
1177 if (sheet) {
1178 qdesigner_internal::PropertySheetStringValue itemText(m_itemText);
1179 sheet->setProperty(sheet->indexOf(QLatin1String("currentTabText")), qVariantFromValue(itemText));
1180 }
1181
1182 formWindow()->clearSelection();
1183 formWindow()->selectWidget(m_tabWidget, true);
1184}
1185
1186// ---- DeleteTabPageCommand ----
1187DeleteTabPageCommand::DeleteTabPageCommand(QDesignerFormWindowInterface *formWindow)
1188 : TabWidgetCommand(formWindow)
1189{
1190}
1191
1192DeleteTabPageCommand::~DeleteTabPageCommand()
1193{
1194}
1195
1196void DeleteTabPageCommand::init(QTabWidget *tabWidget)
1197{
1198 TabWidgetCommand::init(tabWidget);
1199 setText(QApplication::translate("Command", "Delete Page"));
1200}
1201
1202void DeleteTabPageCommand::redo()
1203{
1204 removePage();
1205 cheapUpdate();
1206}
1207
1208void DeleteTabPageCommand::undo()
1209{
1210 addPage();
1211 cheapUpdate();
1212}
1213
1214// ---- AddTabPageCommand ----
1215AddTabPageCommand::AddTabPageCommand(QDesignerFormWindowInterface *formWindow)
1216 : TabWidgetCommand(formWindow)
1217{
1218}
1219
1220AddTabPageCommand::~AddTabPageCommand()
1221{
1222}
1223
1224void AddTabPageCommand::init(QTabWidget *tabWidget)
1225{
1226 init(tabWidget, InsertBefore);
1227}
1228
1229void AddTabPageCommand::init(QTabWidget *tabWidget, InsertionMode mode)
1230{
1231 m_tabWidget = tabWidget;
1232
1233 m_index = m_tabWidget->currentIndex();
1234 if (mode == InsertAfter)
1235 m_index++;
1236 m_widget = new QDesignerWidget(formWindow(), m_tabWidget);
1237 m_itemText = QApplication::translate("Command", "Page");
1238 m_itemIcon = QIcon();
1239 m_widget->setObjectName(QLatin1String("tab"));
1240 formWindow()->ensureUniqueObjectName(m_widget);
1241
1242 setText(QApplication::translate("Command", "Insert Page"));
1243
1244 QDesignerFormEditorInterface *core = formWindow()->core();
1245 core->metaDataBase()->add(m_widget);
1246}
1247
1248void AddTabPageCommand::redo()
1249{
1250 addPage();
1251 cheapUpdate();
1252}
1253
1254void AddTabPageCommand::undo()
1255{
1256 removePage();
1257 cheapUpdate();
1258}
1259
1260// ---- MoveTabPageCommand ----
1261MoveTabPageCommand::MoveTabPageCommand(QDesignerFormWindowInterface *formWindow) :
1262 TabWidgetCommand(formWindow),
1263 m_newIndex(-1),
1264 m_oldIndex(-1)
1265{
1266}
1267
1268MoveTabPageCommand::~MoveTabPageCommand()
1269{
1270}
1271
1272void MoveTabPageCommand::init(QTabWidget *tabWidget, QWidget *page,
1273 const QIcon &icon, const QString &label,
1274 int index, int newIndex)
1275{
1276 TabWidgetCommand::init(tabWidget);
1277 setText(QApplication::translate("Command", "Move Page"));
1278
1279 m_page = page;
1280 m_newIndex = newIndex;
1281 m_oldIndex = index;
1282 m_label = label;
1283 m_icon = icon;
1284}
1285
1286void MoveTabPageCommand::redo()
1287{
1288 m_tabWidget->removeTab(m_oldIndex);
1289 m_tabWidget->insertTab(m_newIndex, m_page, m_icon, m_label);
1290 m_tabWidget->setCurrentIndex(m_newIndex);
1291}
1292
1293void MoveTabPageCommand::undo()
1294{
1295 m_tabWidget->removeTab(m_newIndex);
1296 m_tabWidget->insertTab(m_oldIndex, m_page, m_icon, m_label);
1297 m_tabWidget->setCurrentIndex(m_oldIndex);
1298}
1299
1300// ---- StackedWidgetCommand ----
1301StackedWidgetCommand::StackedWidgetCommand(QDesignerFormWindowInterface *formWindow) :
1302 QDesignerFormWindowCommand(QString(), formWindow),
1303 m_index(-1)
1304{
1305}
1306
1307StackedWidgetCommand::~StackedWidgetCommand()
1308{
1309}
1310
1311void StackedWidgetCommand::init(QStackedWidget *stackedWidget)
1312{
1313 m_stackedWidget = stackedWidget;
1314 m_index = m_stackedWidget->currentIndex();
1315 m_widget = m_stackedWidget->widget(m_index);
1316}
1317
1318void StackedWidgetCommand::removePage()
1319{
1320 m_stackedWidget->removeWidget(m_stackedWidget->widget(m_index));
1321
1322 m_widget->hide();
1323 m_widget->setParent(formWindow());
1324
1325 formWindow()->clearSelection();
1326 formWindow()->selectWidget(m_stackedWidget, true);
1327}
1328
1329void StackedWidgetCommand::addPage()
1330{
1331 m_stackedWidget->insertWidget(m_index, m_widget);
1332
1333 m_widget->show();
1334 m_stackedWidget->setCurrentIndex(m_index);
1335
1336 formWindow()->clearSelection();
1337 formWindow()->selectWidget(m_stackedWidget, true);
1338}
1339
1340// ---- MoveStackedWidgetCommand ----
1341MoveStackedWidgetCommand::MoveStackedWidgetCommand(QDesignerFormWindowInterface *formWindow) :
1342 StackedWidgetCommand(formWindow),
1343 m_newIndex(-1),
1344 m_oldIndex(-1)
1345{
1346}
1347
1348MoveStackedWidgetCommand::~MoveStackedWidgetCommand()
1349{
1350}
1351
1352void MoveStackedWidgetCommand::init(QStackedWidget *stackedWidget, QWidget *page, int newIndex)
1353{
1354 StackedWidgetCommand::init(stackedWidget);
1355 setText(QApplication::translate("Command", "Move Page"));
1356
1357 m_widget = page;
1358 m_newIndex = newIndex;
1359 m_oldIndex = m_stackedWidget->indexOf(m_widget);
1360}
1361
1362void MoveStackedWidgetCommand::redo()
1363{
1364 m_stackedWidget->removeWidget(m_widget);
1365 m_stackedWidget->insertWidget(m_newIndex, m_widget);
1366}
1367
1368void MoveStackedWidgetCommand::undo()
1369{
1370 m_stackedWidget->removeWidget(m_widget);
1371 m_stackedWidget->insertWidget(m_oldIndex, m_widget);
1372}
1373
1374// ---- DeleteStackedWidgetPageCommand ----
1375DeleteStackedWidgetPageCommand::DeleteStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
1376 : StackedWidgetCommand(formWindow)
1377{
1378}
1379
1380DeleteStackedWidgetPageCommand::~DeleteStackedWidgetPageCommand()
1381{
1382}
1383
1384void DeleteStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
1385{
1386 StackedWidgetCommand::init(stackedWidget);
1387 setText(QApplication::translate("Command", "Delete Page"));
1388}
1389
1390void DeleteStackedWidgetPageCommand::redo()
1391{
1392 removePage();
1393 cheapUpdate();
1394}
1395
1396void DeleteStackedWidgetPageCommand::undo()
1397{
1398 addPage();
1399 cheapUpdate();
1400}
1401
1402// ---- AddStackedWidgetPageCommand ----
1403AddStackedWidgetPageCommand::AddStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
1404 : StackedWidgetCommand(formWindow)
1405{
1406}
1407
1408AddStackedWidgetPageCommand::~AddStackedWidgetPageCommand()
1409{
1410}
1411
1412void AddStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
1413{
1414 init(stackedWidget, InsertBefore);
1415}
1416
1417void AddStackedWidgetPageCommand::init(QStackedWidget *stackedWidget, InsertionMode mode)
1418{
1419 m_stackedWidget = stackedWidget;
1420
1421 m_index = m_stackedWidget->currentIndex();
1422 if (mode == InsertAfter)
1423 m_index++;
1424 m_widget = new QDesignerWidget(formWindow(), m_stackedWidget);
1425 m_widget->setObjectName(QLatin1String("page"));
1426 formWindow()->ensureUniqueObjectName(m_widget);
1427
1428 setText(QApplication::translate("Command", "Insert Page"));
1429
1430 QDesignerFormEditorInterface *core = formWindow()->core();
1431 core->metaDataBase()->add(m_widget);
1432}
1433
1434void AddStackedWidgetPageCommand::redo()
1435{
1436 addPage();
1437 cheapUpdate();
1438}
1439
1440void AddStackedWidgetPageCommand::undo()
1441{
1442 removePage();
1443 cheapUpdate();
1444}
1445
1446// ---- TabOrderCommand ----
1447TabOrderCommand::TabOrderCommand(QDesignerFormWindowInterface *formWindow)
1448 : QDesignerFormWindowCommand(QApplication::translate("Command", "Change Tab order"), formWindow),
1449 m_widgetItem(0)
1450{
1451}
1452
1453void TabOrderCommand::init(const QList<QWidget*> &newTabOrder)
1454{
1455 QDesignerFormEditorInterface *core = formWindow()->core();
1456 Q_ASSERT(core);
1457
1458 m_widgetItem = core->metaDataBase()->item(formWindow());
1459 Q_ASSERT(m_widgetItem);
1460 m_oldTabOrder = m_widgetItem->tabOrder();
1461 m_newTabOrder = newTabOrder;
1462}
1463
1464void TabOrderCommand::redo()
1465{
1466 m_widgetItem->setTabOrder(m_newTabOrder);
1467}
1468
1469void TabOrderCommand::undo()
1470{
1471 m_widgetItem->setTabOrder(m_oldTabOrder);
1472}
1473
1474// ---- CreateMenuBarCommand ----
1475CreateMenuBarCommand::CreateMenuBarCommand(QDesignerFormWindowInterface *formWindow)
1476 : QDesignerFormWindowCommand(QApplication::translate("Command", "Create Menu Bar"), formWindow)
1477{
1478}
1479
1480void CreateMenuBarCommand::init(QMainWindow *mainWindow)
1481{
1482 m_mainWindow = mainWindow;
1483 QDesignerFormEditorInterface *core = formWindow()->core();
1484 m_menuBar = qobject_cast<QMenuBar*>(core->widgetFactory()->createWidget(QLatin1String("QMenuBar"), m_mainWindow));
1485 core->widgetFactory()->initialize(m_menuBar);
1486}
1487
1488void CreateMenuBarCommand::redo()
1489{
1490 QDesignerFormEditorInterface *core = formWindow()->core();
1491 QDesignerContainerExtension *c;
1492 c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1493 c->addWidget(m_menuBar);
1494
1495 m_menuBar->setObjectName(QLatin1String("menuBar"));
1496 formWindow()->ensureUniqueObjectName(m_menuBar);
1497 core->metaDataBase()->add(m_menuBar);
1498 formWindow()->emitSelectionChanged();
1499 m_menuBar->setFocus();
1500}
1501
1502void CreateMenuBarCommand::undo()
1503{
1504 QDesignerFormEditorInterface *core = formWindow()->core();
1505 QDesignerContainerExtension *c;
1506 c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1507 for (int i = 0; i < c->count(); ++i) {
1508 if (c->widget(i) == m_menuBar) {
1509 c->remove(i);
1510 break;
1511 }
1512 }
1513
1514 core->metaDataBase()->remove(m_menuBar);
1515 formWindow()->emitSelectionChanged();
1516}
1517
1518// ---- DeleteMenuBarCommand ----
1519DeleteMenuBarCommand::DeleteMenuBarCommand(QDesignerFormWindowInterface *formWindow)
1520 : QDesignerFormWindowCommand(QApplication::translate("Command", "Delete Menu Bar"), formWindow)
1521{
1522}
1523
1524void DeleteMenuBarCommand::init(QMenuBar *menuBar)
1525{
1526 m_menuBar = menuBar;
1527 m_mainWindow = qobject_cast<QMainWindow*>(menuBar->parentWidget());
1528}
1529
1530void DeleteMenuBarCommand::redo()
1531{
1532 if (m_mainWindow) {
1533 QDesignerContainerExtension *c;
1534 c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
1535 Q_ASSERT(c != 0);
1536 for (int i=0; i<c->count(); ++i) {
1537 if (c->widget(i) == m_menuBar) {
1538 c->remove(i);
1539 break;
1540 }
1541 }
1542 }
1543
1544 core()->metaDataBase()->remove(m_menuBar);
1545 m_menuBar->hide();
1546 m_menuBar->setParent(formWindow());
1547 formWindow()->emitSelectionChanged();
1548}
1549
1550void DeleteMenuBarCommand::undo()
1551{
1552 if (m_mainWindow) {
1553 m_menuBar->setParent(m_mainWindow);
1554 QDesignerContainerExtension *c;
1555 c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
1556
1557 c->addWidget(m_menuBar);
1558
1559 core()->metaDataBase()->add(m_menuBar);
1560 m_menuBar->show();
1561 formWindow()->emitSelectionChanged();
1562 }
1563}
1564
1565// ---- CreateStatusBarCommand ----
1566CreateStatusBarCommand::CreateStatusBarCommand(QDesignerFormWindowInterface *formWindow)
1567 : QDesignerFormWindowCommand(QApplication::translate("Command", "Create Status Bar"), formWindow)
1568{
1569}
1570
1571void CreateStatusBarCommand::init(QMainWindow *mainWindow)
1572{
1573 m_mainWindow = mainWindow;
1574 QDesignerFormEditorInterface *core = formWindow()->core();
1575 m_statusBar = qobject_cast<QStatusBar*>(core->widgetFactory()->createWidget(QLatin1String("QStatusBar"), m_mainWindow));
1576 core->widgetFactory()->initialize(m_statusBar);
1577}
1578
1579void CreateStatusBarCommand::redo()
1580{
1581 QDesignerFormEditorInterface *core = formWindow()->core();
1582 QDesignerContainerExtension *c;
1583 c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1584 c->addWidget(m_statusBar);
1585
1586 m_statusBar->setObjectName(QLatin1String("statusBar"));
1587 formWindow()->ensureUniqueObjectName(m_statusBar);
1588 core->metaDataBase()->add(m_statusBar);
1589 formWindow()->emitSelectionChanged();
1590}
1591
1592void CreateStatusBarCommand::undo()
1593{
1594 QDesignerFormEditorInterface *core = formWindow()->core();
1595 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1596 for (int i = 0; i < c->count(); ++i) {
1597 if (c->widget(i) == m_statusBar) {
1598 c->remove(i);
1599 break;
1600 }
1601 }
1602
1603 core->metaDataBase()->remove(m_statusBar);
1604 formWindow()->emitSelectionChanged();
1605}
1606
1607// ---- DeleteStatusBarCommand ----
1608DeleteStatusBarCommand::DeleteStatusBarCommand(QDesignerFormWindowInterface *formWindow)
1609 : QDesignerFormWindowCommand(QApplication::translate("Command", "Delete Status Bar"), formWindow)
1610{
1611}
1612
1613void DeleteStatusBarCommand::init(QStatusBar *statusBar)
1614{
1615 m_statusBar = statusBar;
1616 m_mainWindow = qobject_cast<QMainWindow*>(statusBar->parentWidget());
1617}
1618
1619void DeleteStatusBarCommand::redo()
1620{
1621 if (m_mainWindow) {
1622 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
1623 Q_ASSERT(c != 0);
1624 for (int i=0; i<c->count(); ++i) {
1625 if (c->widget(i) == m_statusBar) {
1626 c->remove(i);
1627 break;
1628 }
1629 }
1630 }
1631
1632 core()->metaDataBase()->remove(m_statusBar);
1633 m_statusBar->hide();
1634 m_statusBar->setParent(formWindow());
1635 formWindow()->emitSelectionChanged();
1636}
1637
1638void DeleteStatusBarCommand::undo()
1639{
1640 if (m_mainWindow) {
1641 m_statusBar->setParent(m_mainWindow);
1642 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
1643
1644 c->addWidget(m_statusBar);
1645
1646 core()->metaDataBase()->add(m_statusBar);
1647 m_statusBar->show();
1648 formWindow()->emitSelectionChanged();
1649 }
1650}
1651
1652// ---- AddToolBarCommand ----
1653AddToolBarCommand::AddToolBarCommand(QDesignerFormWindowInterface *formWindow)
1654 : QDesignerFormWindowCommand(QApplication::translate("Command", "Add Tool Bar"), formWindow)
1655{
1656}
1657
1658void AddToolBarCommand::init(QMainWindow *mainWindow)
1659{
1660 m_mainWindow = mainWindow;
1661 QDesignerWidgetFactoryInterface * wf = formWindow()->core()->widgetFactory();
1662 // Pass on 0 parent first to avoid reparenting flicker.
1663 m_toolBar = qobject_cast<QToolBar*>(wf->createWidget(QLatin1String("QToolBar"), 0));
1664 wf->initialize(m_toolBar);
1665 m_toolBar->hide();
1666}
1667
1668void AddToolBarCommand::redo()
1669{
1670 QDesignerFormEditorInterface *core = formWindow()->core();
1671 core->metaDataBase()->add(m_toolBar);
1672
1673 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1674 c->addWidget(m_toolBar);
1675
1676 m_toolBar->setObjectName(QLatin1String("toolBar"));
1677 formWindow()->ensureUniqueObjectName(m_toolBar);
1678 setPropertySheetWindowTitle(core, m_toolBar, m_toolBar->objectName());
1679 formWindow()->emitSelectionChanged();
1680}
1681
1682void AddToolBarCommand::undo()
1683{
1684 QDesignerFormEditorInterface *core = formWindow()->core();
1685 core->metaDataBase()->remove(m_toolBar);
1686 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1687 for (int i = 0; i < c->count(); ++i) {
1688 if (c->widget(i) == m_toolBar) {
1689 c->remove(i);
1690 break;
1691 }
1692 }
1693 formWindow()->emitSelectionChanged();
1694}
1695
1696// ---- DockWidgetCommand:: ----
1697DockWidgetCommand::DockWidgetCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
1698 : QDesignerFormWindowCommand(description, formWindow)
1699{
1700}
1701
1702DockWidgetCommand::~DockWidgetCommand()
1703{
1704}
1705
1706void DockWidgetCommand::init(QDockWidget *dockWidget)
1707{
1708 m_dockWidget = dockWidget;
1709}
1710
1711// ---- AddDockWidgetCommand ----
1712AddDockWidgetCommand::AddDockWidgetCommand(QDesignerFormWindowInterface *formWindow)
1713 : QDesignerFormWindowCommand(QApplication::translate("Command", "Add Dock Window"), formWindow)
1714{
1715}
1716
1717void AddDockWidgetCommand::init(QMainWindow *mainWindow, QDockWidget *dockWidget)
1718{
1719 m_mainWindow = mainWindow;
1720 m_dockWidget = dockWidget;
1721}
1722
1723void AddDockWidgetCommand::init(QMainWindow *mainWindow)
1724{
1725 m_mainWindow = mainWindow;
1726 QDesignerFormEditorInterface *core = formWindow()->core();
1727 m_dockWidget = qobject_cast<QDockWidget*>(core->widgetFactory()->createWidget(QLatin1String("QDockWidget"), m_mainWindow));
1728}
1729
1730void AddDockWidgetCommand::redo()
1731{
1732 QDesignerFormEditorInterface *core = formWindow()->core();
1733 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1734 c->addWidget(m_dockWidget);
1735
1736 m_dockWidget->setObjectName(QLatin1String("dockWidget"));
1737 formWindow()->ensureUniqueObjectName(m_dockWidget);
1738 formWindow()->manageWidget(m_dockWidget);
1739 formWindow()->emitSelectionChanged();
1740}
1741
1742void AddDockWidgetCommand::undo()
1743{
1744 QDesignerFormEditorInterface *core = formWindow()->core();
1745 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), m_mainWindow);
1746 for (int i = 0; i < c->count(); ++i) {
1747 if (c->widget(i) == m_dockWidget) {
1748 c->remove(i);
1749 break;
1750 }
1751 }
1752
1753 formWindow()->unmanageWidget(m_dockWidget);
1754 formWindow()->emitSelectionChanged();
1755}
1756
1757// ---- AdjustWidgetSizeCommand ----
1758AdjustWidgetSizeCommand::AdjustWidgetSizeCommand(QDesignerFormWindowInterface *formWindow)
1759 : QDesignerFormWindowCommand(QString(), formWindow)
1760{
1761}
1762
1763void AdjustWidgetSizeCommand::init(QWidget *widget)
1764{
1765 m_widget = widget;
1766 setText(QApplication::translate("Command", "Adjust Size of '%1'").arg(widget->objectName()));
1767}
1768
1769QWidget *AdjustWidgetSizeCommand::widgetForAdjust() const
1770{
1771 QDesignerFormWindowInterface *fw = formWindow();
1772 // Return the outer, embedding widget if it is the main container
1773 if (Utils::isCentralWidget(fw, m_widget))
1774 return fw->core()->integration()->containerWindow(m_widget);
1775 return m_widget;
1776}
1777
1778void AdjustWidgetSizeCommand::redo()
1779{
1780 QWidget *aw = widgetForAdjust();
1781 m_geometry = aw->geometry();
1782 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
1783 aw->adjustSize();
1784 const bool isMainContainer = aw != m_widget;
1785 if (!isMainContainer) {
1786 /* When doing adjustsize on a selected non-laid out child that has been enlarged
1787 * and pushed partially over the top/left edge[s], it is possible that it "disappears"
1788 * when shrinking. In that case, move it back so that it remains visible. */
1789 if (aw->parentWidget()->layout() == 0) {
1790 const QRect contentsRect = aw->parentWidget()->contentsRect();
1791 const QRect newGeometry = aw->geometry();
1792 QPoint newPos = m_geometry.topLeft();
1793 if (newGeometry.bottom() <= contentsRect.y())
1794 newPos.setY(contentsRect.y());
1795 if (newGeometry.right() <= contentsRect.x())
1796 newPos.setX(contentsRect.x());
1797 if (newPos != m_geometry.topLeft())
1798 aw->move(newPos);
1799 }
1800 }
1801 updatePropertyEditor();
1802}
1803
1804void AdjustWidgetSizeCommand::undo()
1805{
1806 QWidget *aw = widgetForAdjust();
1807 aw->resize(m_geometry.size());
1808 if (m_geometry.topLeft() != aw->geometry().topLeft())
1809 aw->move(m_geometry.topLeft());
1810 updatePropertyEditor();
1811}
1812
1813void AdjustWidgetSizeCommand::updatePropertyEditor() const
1814{
1815 if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
1816 if (propertyEditor->object() == m_widget)
1817 propertyEditor->setPropertyValue(QLatin1String("geometry"), m_widget->geometry(), true);
1818 }
1819}
1820// ------------ ChangeFormLayoutItemRoleCommand
1821
1822ChangeFormLayoutItemRoleCommand::ChangeFormLayoutItemRoleCommand(QDesignerFormWindowInterface *formWindow) :
1823 QDesignerFormWindowCommand(QApplication::translate("Command", "Change Form Layout Item Geometry"), formWindow),
1824 m_operation(SpanningToLabel)
1825{
1826}
1827
1828void ChangeFormLayoutItemRoleCommand::init(QWidget *widget, Operation op)
1829{
1830 m_widget = widget;
1831 m_operation = op;
1832}
1833
1834void ChangeFormLayoutItemRoleCommand::redo()
1835{
1836 doOperation(m_operation);
1837}
1838
1839void ChangeFormLayoutItemRoleCommand::undo()
1840{
1841 doOperation(reverseOperation(m_operation));
1842}
1843
1844ChangeFormLayoutItemRoleCommand::Operation ChangeFormLayoutItemRoleCommand::reverseOperation(Operation op)
1845{
1846 switch (op) {
1847 case SpanningToLabel:
1848 return LabelToSpanning;
1849 case SpanningToField:
1850 return FieldToSpanning;
1851 case LabelToSpanning:
1852 return SpanningToLabel;
1853 case FieldToSpanning:
1854 return SpanningToField;
1855 }
1856 return SpanningToField;
1857}
1858
1859void ChangeFormLayoutItemRoleCommand::doOperation(Operation op)
1860{
1861 QFormLayout *fl = ChangeFormLayoutItemRoleCommand::managedFormLayoutOf(formWindow()->core(), m_widget);
1862 const int index = fl->indexOf(m_widget);
1863 Q_ASSERT(index != -1);
1864 int row;
1865 QFormLayout::ItemRole role;
1866 fl->getItemPosition (index, &row, &role);
1867 Q_ASSERT(index != -1);
1868 QLayoutItem *item = fl->takeAt(index);
1869 const QRect area = QRect(0, row, 2, 1);
1870 switch (op) {
1871 case SpanningToLabel:
1872 fl->setItem(row, QFormLayout::LabelRole, item);
1873 QLayoutSupport::createEmptyCells(fl);
1874 break;
1875 case SpanningToField:
1876 fl->setItem(row, QFormLayout::FieldRole, item);
1877 QLayoutSupport::createEmptyCells(fl);
1878 break;
1879 case LabelToSpanning:
1880 case FieldToSpanning:
1881 QLayoutSupport::removeEmptyCells(fl, area);
1882 fl->setItem(row, QFormLayout::SpanningRole, item);
1883 break;
1884 }
1885}
1886
1887unsigned ChangeFormLayoutItemRoleCommand::possibleOperations(QDesignerFormEditorInterface *core, QWidget *w)
1888{
1889 QFormLayout *fl = managedFormLayoutOf(core, w);
1890 if (!fl)
1891 return 0;
1892 const int index = fl->indexOf(w);
1893 if (index == -1)
1894 return 0;
1895 int row, col, colspan;
1896 getFormLayoutItemPosition(fl, index, &row, &col, 0, &colspan);
1897 // Spanning item?
1898 if (colspan > 1)
1899 return SpanningToLabel|SpanningToField;
1900 // Is the neighbouring column free, that is, can the current item be expanded?
1901 const QFormLayout::ItemRole neighbouringRole = col == 0 ? QFormLayout::FieldRole : QFormLayout::LabelRole;
1902 const bool empty = LayoutInfo::isEmptyItem(fl->itemAt(row, neighbouringRole));
1903 if (empty)
1904 return col == 0 ? LabelToSpanning : FieldToSpanning;
1905 return 0;
1906}
1907
1908QFormLayout *ChangeFormLayoutItemRoleCommand::managedFormLayoutOf(QDesignerFormEditorInterface *core, QWidget *w)
1909{
1910 if (QLayout *layout = LayoutInfo::managedLayout(core, w->parentWidget()))
1911 if (QFormLayout *fl = qobject_cast<QFormLayout *>(layout))
1912 return fl;
1913 return 0;
1914}
1915
1916// ---- ChangeLayoutItemGeometry ----
1917ChangeLayoutItemGeometry::ChangeLayoutItemGeometry(QDesignerFormWindowInterface *formWindow)
1918 : QDesignerFormWindowCommand(QApplication::translate("Command", "Change Layout Item Geometry"), formWindow)
1919{
1920}
1921
1922void ChangeLayoutItemGeometry::init(QWidget *widget, int row, int column, int rowspan, int colspan)
1923{
1924 m_widget = widget;
1925 Q_ASSERT(m_widget->parentWidget() != 0);
1926
1927 QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
1928 Q_ASSERT(layout != 0);
1929
1930 QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
1931 Q_ASSERT(grid != 0);
1932
1933 const int itemIndex = grid->indexOf(m_widget);
1934 Q_ASSERT(itemIndex != -1);
1935
1936 int current_row, current_column, current_rowspan, current_colspan;
1937 grid->getItemPosition(itemIndex, &current_row, &current_column, &current_rowspan, &current_colspan);
1938
1939 m_oldInfo.setRect(current_column, current_row, current_colspan, current_rowspan);
1940 m_newInfo.setRect(column, row, colspan, rowspan);
1941}
1942
1943void ChangeLayoutItemGeometry::changeItemPosition(const QRect &g)
1944{
1945 QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
1946 Q_ASSERT(layout != 0);
1947
1948 QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
1949 Q_ASSERT(grid != 0);
1950
1951 const int itemIndex = grid->indexOf(m_widget);
1952 Q_ASSERT(itemIndex != -1);
1953
1954 QLayoutItem *item = grid->takeAt(itemIndex);
1955 delete item;
1956
1957 if (!QLayoutSupport::removeEmptyCells(grid, g))
1958 qWarning() << "ChangeLayoutItemGeometry::changeItemPosition: Nonempty cell at " << g << '.';
1959
1960 grid->addWidget(m_widget, g.top(), g.left(), g.height(), g.width());
1961
1962 grid->invalidate();
1963 grid->activate();
1964
1965 QLayoutSupport::createEmptyCells(grid);
1966
1967 formWindow()->clearSelection(false);
1968 formWindow()->selectWidget(m_widget, true);
1969}
1970
1971void ChangeLayoutItemGeometry::redo()
1972{
1973 changeItemPosition(m_newInfo);
1974}
1975
1976void ChangeLayoutItemGeometry::undo()
1977{
1978 changeItemPosition(m_oldInfo);
1979}
1980
1981// ---- ContainerWidgetCommand ----
1982ContainerWidgetCommand::ContainerWidgetCommand(QDesignerFormWindowInterface *formWindow) :
1983 QDesignerFormWindowCommand(QString(), formWindow),
1984 m_index(-1)
1985{
1986}
1987
1988ContainerWidgetCommand::~ContainerWidgetCommand()
1989{
1990}
1991
1992QDesignerContainerExtension *ContainerWidgetCommand::containerExtension() const
1993{
1994 QExtensionManager *mgr = core()->extensionManager();
1995 return qt_extension<QDesignerContainerExtension*>(mgr, m_containerWidget);
1996}
1997
1998void ContainerWidgetCommand::init(QWidget *containerWidget)
1999{
2000 m_containerWidget = containerWidget;
2001
2002 if (QDesignerContainerExtension *c = containerExtension()) {
2003 m_index = c->currentIndex();
2004 m_widget = c->widget(m_index);
2005 }
2006}
2007
2008void ContainerWidgetCommand::removePage()
2009{
2010 if (QDesignerContainerExtension *c = containerExtension()) {
2011 if (const int count = c->count()) {
2012 // Undo add after last?
2013 const int deleteIndex = m_index >= 0 ? m_index : count - 1;
2014 c->remove(deleteIndex);
2015 m_widget->hide();
2016 m_widget->setParent(formWindow());
2017 }
2018 }
2019}
2020
2021void ContainerWidgetCommand::addPage()
2022{
2023 if (QDesignerContainerExtension *c = containerExtension()) {
2024 int newCurrentIndex;
2025 if (m_index >= 0) {
2026 c->insertWidget(m_index, m_widget);
2027 newCurrentIndex = m_index;
2028 } else {
2029 c->addWidget(m_widget);
2030 newCurrentIndex = c->count() -1 ;
2031 }
2032 m_widget->show();
2033 c->setCurrentIndex(newCurrentIndex);
2034 }
2035}
2036
2037// ---- DeleteContainerWidgetPageCommand ----
2038DeleteContainerWidgetPageCommand::DeleteContainerWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
2039 : ContainerWidgetCommand(formWindow)
2040{
2041}
2042
2043DeleteContainerWidgetPageCommand::~DeleteContainerWidgetPageCommand()
2044{
2045}
2046
2047void DeleteContainerWidgetPageCommand::init(QWidget *containerWidget, ContainerType ct)
2048{
2049 ContainerWidgetCommand::init(containerWidget);
2050 switch (ct) {
2051 case WizardContainer:
2052 case PageContainer:
2053 setText(QApplication::translate("Command", "Delete Page"));
2054 break;
2055 case MdiContainer:
2056 setText(QApplication::translate("Command", "Delete Subwindow"));
2057 break;
2058 }
2059}
2060
2061void DeleteContainerWidgetPageCommand::redo()
2062{
2063 removePage();
2064 cheapUpdate();
2065}
2066
2067void DeleteContainerWidgetPageCommand::undo()
2068{
2069 addPage();
2070 cheapUpdate();
2071}
2072
2073// ---- AddContainerWidgetPageCommand ----
2074AddContainerWidgetPageCommand::AddContainerWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
2075 : ContainerWidgetCommand(formWindow)
2076{
2077}
2078
2079AddContainerWidgetPageCommand::~AddContainerWidgetPageCommand()
2080{
2081}
2082
2083void AddContainerWidgetPageCommand::init(QWidget *containerWidget, ContainerType ct, InsertionMode mode)
2084{
2085 m_containerWidget = containerWidget;
2086
2087 if (QDesignerContainerExtension *c = containerExtension()) {
2088 m_index = c->currentIndex();
2089 if (m_index >= 0 && mode == InsertAfter)
2090 m_index++;
2091 m_widget = 0;
2092 const QDesignerFormEditorInterface *core = formWindow()->core();
2093 switch (ct) {
2094 case PageContainer:
2095 setText(QApplication::translate("Command", "Insert Page"));
2096 m_widget = new QDesignerWidget(formWindow(), m_containerWidget);
2097 m_widget->setObjectName(QApplication::translate("Command", "page"));
2098 break;
2099 case MdiContainer:
2100 setText(QApplication::translate("Command", "Insert Subwindow"));
2101 m_widget = new QDesignerWidget(formWindow(), m_containerWidget);
2102 m_widget->setObjectName(QApplication::translate("Command", "subwindow"));
2103 setPropertySheetWindowTitle(core, m_widget, QApplication::translate("Command", "Subwindow"));
2104 break;
2105 case WizardContainer: // Apply style, don't manage
2106 m_widget = core->widgetFactory()->createWidget(QLatin1String("QWizardPage"), 0);
2107 break;
2108 }
2109 formWindow()->ensureUniqueObjectName(m_widget);
2110 core->metaDataBase()->add(m_widget);
2111 }
2112}
2113
2114void AddContainerWidgetPageCommand::redo()
2115{
2116 addPage();
2117 cheapUpdate();
2118}
2119
2120void AddContainerWidgetPageCommand::undo()
2121{
2122 removePage();
2123 cheapUpdate();
2124}
2125
2126ChangeCurrentPageCommand::ChangeCurrentPageCommand(QDesignerFormWindowInterface *formWindow)
2127 :
2128 QDesignerFormWindowCommand(QString(), formWindow), m_oldIndex(0), m_newIndex(0)
2129{
2130}
2131
2132ChangeCurrentPageCommand::~ChangeCurrentPageCommand()
2133{
2134}
2135
2136QDesignerContainerExtension *ChangeCurrentPageCommand::containerExtension() const
2137{
2138 QExtensionManager *mgr = core()->extensionManager();
2139 return qt_extension<QDesignerContainerExtension*>(mgr, m_containerWidget);
2140}
2141
2142void ChangeCurrentPageCommand::init(QWidget *containerWidget, int newIndex)
2143{
2144 m_containerWidget = containerWidget;
2145
2146 if (QDesignerContainerExtension *c = containerExtension()) {
2147 m_newIndex = newIndex;
2148 m_oldIndex = c->currentIndex();
2149 m_widget = c->widget(m_oldIndex);
2150 }
2151}
2152
2153void ChangeCurrentPageCommand::redo()
2154{
2155 containerExtension()->setCurrentIndex(m_newIndex);
2156}
2157
2158void ChangeCurrentPageCommand::undo()
2159{
2160 containerExtension()->setCurrentIndex(m_oldIndex);
2161}
2162
2163static int itemRoles[] = {
2164 Qt::DecorationPropertyRole,
2165 Qt::DisplayPropertyRole,
2166 Qt::ToolTipPropertyRole,
2167 Qt::StatusTipPropertyRole,
2168 Qt::WhatsThisPropertyRole,
2169 Qt::FontRole,
2170 Qt::TextAlignmentRole,
2171 Qt::BackgroundRole,
2172 Qt::ForegroundRole,
2173 Qt::CheckStateRole,
2174 -1
2175};
2176
2177template<class T>
2178static void copyRoleFromItem(ItemData *id, int role, const T *item)
2179{
2180 QVariant v = item->data(role);
2181 if (v.isValid())
2182 id->m_properties.insert(role, v);
2183}
2184
2185template<class T>
2186static void copyRolesFromItem(ItemData *id, const T *item, bool editor)
2187{
2188 static const int defaultFlags = T().flags();
2189
2190 for (int i = 0; itemRoles[i] != -1; i++)
2191 copyRoleFromItem<T>(id, itemRoles[i], item);
2192
2193 if (editor)
2194 copyRoleFromItem<T>(id, ItemFlagsShadowRole, item);
2195 else if (item->flags() != defaultFlags)
2196 id->m_properties.insert(ItemFlagsShadowRole, qVariantFromValue((int)item->flags()));
2197}
2198
2199template<class T>
2200static void copyRolesToItem(const ItemData *id, T *item, DesignerIconCache *iconCache, bool editor)
2201{
2202 QHash<int, QVariant>::const_iterator it = id->m_properties.constBegin(),
2203 end = id->m_properties.constEnd();
2204 for (; it != end; ++it)
2205 if (it.value().isValid()) {
2206 if (!editor && it.key() == ItemFlagsShadowRole) {
2207 item->setFlags((Qt::ItemFlags)it.value().toInt());
2208 } else {
2209 item->setData(it.key(), it.value());
2210 switch (it.key()) {
2211 case Qt::DecorationPropertyRole:
2212 if (iconCache)
2213 item->setIcon(iconCache->icon(qVariantValue<PropertySheetIconValue>(it.value())));
2214 break;
2215 case Qt::DisplayPropertyRole:
2216 item->setText(qVariantValue<PropertySheetStringValue>(it.value()).value());
2217 break;
2218 case Qt::ToolTipPropertyRole:
2219 item->setToolTip(qVariantValue<PropertySheetStringValue>(it.value()).value());
2220 break;
2221 case Qt::StatusTipPropertyRole:
2222 item->setStatusTip(qVariantValue<PropertySheetStringValue>(it.value()).value());
2223 break;
2224 case Qt::WhatsThisPropertyRole:
2225 item->setWhatsThis(qVariantValue<PropertySheetStringValue>(it.value()).value());
2226 break;
2227 }
2228 }
2229 }
2230
2231 if (editor)
2232 item->setFlags(item->flags() | Qt::ItemIsEditable);
2233}
2234
2235ItemData::ItemData(const QListWidgetItem *item, bool editor)
2236{
2237 copyRolesFromItem<QListWidgetItem>(this, item, editor);
2238}
2239
2240QListWidgetItem *ItemData::createListItem(DesignerIconCache *iconCache, bool editor) const
2241{
2242 QListWidgetItem *item = new QListWidgetItem();
2243 copyRolesToItem(this, item, iconCache, editor);
2244 return item;
2245}
2246
2247ItemData::ItemData(const QTableWidgetItem *item, bool editor)
2248{
2249 copyRolesFromItem(this, item, editor);
2250}
2251
2252QTableWidgetItem *ItemData::createTableItem(DesignerIconCache *iconCache, bool editor) const
2253{
2254 QTableWidgetItem *item = new QTableWidgetItem;
2255 copyRolesToItem(this, item, iconCache, editor);
2256 return item;
2257}
2258
2259static void copyRoleFromItem(ItemData *id, int role, const QTreeWidgetItem *item, int column)
2260{
2261 QVariant v = item->data(column, role);
2262 if (v.isValid())
2263 id->m_properties.insert(role, v);
2264}
2265
2266ItemData::ItemData(const QTreeWidgetItem *item, int column)
2267{
2268 copyRoleFromItem(this, Qt::EditRole, item, column);
2269 PropertySheetStringValue str(item->text(column));
2270 m_properties.insert(Qt::DisplayPropertyRole, qVariantFromValue(str));
2271
2272 for (int i = 0; itemRoles[i] != -1; i++)
2273 copyRoleFromItem(this, itemRoles[i], item, column);
2274}
2275
2276void ItemData::fillTreeItemColumn(QTreeWidgetItem *item, int column, DesignerIconCache *iconCache) const
2277{
2278 QHash<int, QVariant>::const_iterator it = m_properties.constBegin(), end = m_properties.constEnd();
2279 for (; it != end; ++it)
2280 if (it.value().isValid()) {
2281 item->setData(column, it.key(), it.value());
2282 switch (it.key()) {
2283 case Qt::DecorationPropertyRole:
2284 if (iconCache)
2285 item->setIcon(column, iconCache->icon(qVariantValue<PropertySheetIconValue>(it.value())));
2286 break;
2287 case Qt::DisplayPropertyRole:
2288 item->setText(column, qVariantValue<PropertySheetStringValue>(it.value()).value());
2289 break;
2290 case Qt::ToolTipPropertyRole:
2291 item->setToolTip(column, qVariantValue<PropertySheetStringValue>(it.value()).value());
2292 break;
2293 case Qt::StatusTipPropertyRole:
2294 item->setStatusTip(column, qVariantValue<PropertySheetStringValue>(it.value()).value());
2295 break;
2296 case Qt::WhatsThisPropertyRole:
2297 item->setWhatsThis(column, qVariantValue<PropertySheetStringValue>(it.value()).value());
2298 break;
2299 }
2300 }
2301}
2302
2303ListContents::ListContents(const QTreeWidgetItem *item)
2304{
2305 for (int i = 0; i < item->columnCount(); i++)
2306 m_items.append(ItemData(item, i));
2307}
2308
2309QTreeWidgetItem *ListContents::createTreeItem(DesignerIconCache *iconCache) const
2310{
2311 QTreeWidgetItem *item = new QTreeWidgetItem;
2312 int i = 0;
2313 foreach (const ItemData &id, m_items)
2314 id.fillTreeItemColumn(item, i++, iconCache);
2315 return item;
2316}
2317
2318void ListContents::createFromListWidget(const QListWidget *listWidget, bool editor)
2319{
2320 m_items.clear();
2321
2322 for (int i = 0; i < listWidget->count(); i++)
2323 m_items.append(ItemData(listWidget->item(i), editor));
2324}
2325
2326void ListContents::applyToListWidget(QListWidget *listWidget, DesignerIconCache *iconCache, bool editor) const
2327{
2328 listWidget->clear();
2329
2330 int i = 0;
2331 foreach (const ItemData &entry, m_items) {
2332 if (!entry.isValid())
2333 new QListWidgetItem(TableWidgetContents::defaultHeaderText(i), listWidget);
2334 else
2335 listWidget->addItem(entry.createListItem(iconCache, editor));
2336 i++;
2337 }
2338}
2339
2340void ListContents::createFromComboBox(const QComboBox *comboBox)
2341{
2342 m_items.clear();
2343
2344 const int count = comboBox->count();
2345 for (int i = 0; i < count; i++) {
2346 // We might encounter items added in a custom combo
2347 // constructor. Ignore those.
2348 const QVariant textValue = comboBox->itemData(i, Qt::DisplayPropertyRole);
2349 if (!textValue.isNull()) {
2350 ItemData entry;
2351 entry.m_properties.insert(Qt::DisplayPropertyRole, textValue);
2352 const QVariant iconValue = comboBox->itemData(i, Qt::DecorationPropertyRole);
2353 if (!iconValue.isNull())
2354 entry.m_properties.insert(Qt::DecorationPropertyRole, iconValue);
2355 m_items.append(entry);
2356 }
2357 }
2358}
2359
2360void ListContents::applyToComboBox(QComboBox *comboBox, DesignerIconCache *iconCache) const
2361{
2362 comboBox->clear();
2363
2364 foreach (const ItemData &hash, m_items) {
2365 QIcon icon;
2366 if (iconCache)
2367 icon = iconCache->icon(qVariantValue<PropertySheetIconValue>(
2368 hash.m_properties[Qt::DecorationPropertyRole]));
2369 QVariant var = hash.m_properties[Qt::DisplayPropertyRole];
2370 PropertySheetStringValue str = qVariantValue<PropertySheetStringValue>(var);
2371 comboBox->addItem(icon, str.value());
2372 comboBox->setItemData(comboBox->count() - 1,
2373 var,
2374 Qt::DisplayPropertyRole);
2375 comboBox->setItemData(comboBox->count() - 1,
2376 hash.m_properties[Qt::DecorationPropertyRole],
2377 Qt::DecorationPropertyRole);
2378 }
2379}
2380
2381// --------- TableWidgetContents
2382
2383TableWidgetContents::TableWidgetContents() :
2384 m_columnCount(0),
2385 m_rowCount(0)
2386{
2387}
2388
2389void TableWidgetContents::clear()
2390{
2391 m_horizontalHeader.m_items.clear();
2392 m_verticalHeader.m_items.clear();
2393 m_items.clear();
2394 m_columnCount = 0;
2395 m_rowCount = 0;
2396}
2397
2398QString TableWidgetContents::defaultHeaderText(int i)
2399{
2400 return QString::number(i + 1);
2401}
2402
2403bool TableWidgetContents::nonEmpty(const QTableWidgetItem *item, int headerColumn)
2404{
2405 static int defaultFlags = QTableWidgetItem().flags();
2406
2407 if (item->flags() != defaultFlags)
2408 return true;
2409
2410 QString text = qVariantValue<PropertySheetStringValue>(item->data(Qt::DisplayPropertyRole)).value();
2411 if (!text.isEmpty()) {
2412 if (headerColumn < 0 || text != defaultHeaderText(headerColumn))
2413 return true;
2414 } else {
2415 // FIXME: This doesn't seem to make sense
2416 return true;
2417 }
2418
2419 for (int i = 0; itemRoles[i] != -1; i++)
2420 if (itemRoles[i] != Qt::DisplayPropertyRole && item->data(itemRoles[i]).isValid())
2421 return true;
2422
2423 return false;
2424}
2425
2426void TableWidgetContents::insertHeaderItem(const QTableWidgetItem *item, int i, ListContents *header, bool editor)
2427{
2428 if (nonEmpty(item, i))
2429 header->m_items.append(ItemData(item, editor));
2430 else
2431 header->m_items.append(ItemData());
2432}
2433
2434void TableWidgetContents::fromTableWidget(const QTableWidget *tableWidget, bool editor)
2435{
2436 clear();
2437 m_columnCount = tableWidget->columnCount();
2438 m_rowCount = tableWidget->rowCount();
2439 // horiz header: Legacy behaviour: auto-generate number for empty items
2440 for (int col = 0; col < m_columnCount; col++)
2441 if (const QTableWidgetItem *item = tableWidget->horizontalHeaderItem(col))
2442 insertHeaderItem(item, col, &m_horizontalHeader, editor);
2443 // vertical header: Legacy behaviour: auto-generate number for empty items
2444 for (int row = 0; row < m_rowCount; row++)
2445 if (const QTableWidgetItem *item = tableWidget->verticalHeaderItem(row))
2446 insertHeaderItem(item, row, &m_verticalHeader, editor);
2447 // cell data
2448 for (int col = 0; col < m_columnCount; col++)
2449 for (int row = 0; row < m_rowCount; row++)
2450 if (const QTableWidgetItem *item = tableWidget->item(row, col))
2451 if (nonEmpty(item, -1))
2452 m_items.insert(CellRowColumnAddress(row, col), ItemData(item, editor));
2453}
2454
2455void TableWidgetContents::applyToTableWidget(QTableWidget *tableWidget, DesignerIconCache *iconCache, bool editor) const
2456{
2457 tableWidget->clear();
2458
2459 tableWidget->setColumnCount(m_columnCount);
2460 tableWidget->setRowCount(m_rowCount);
2461
2462 // horiz header
2463 int col = 0;
2464 foreach (const ItemData &id, m_horizontalHeader.m_items) {
2465 if (id.isValid())
2466 tableWidget->setHorizontalHeaderItem(col, id.createTableItem(iconCache, editor));
2467 col++;
2468 }
2469 // vertical header
2470 int row = 0;
2471 foreach (const ItemData &id, m_verticalHeader.m_items) {
2472 if (id.isValid())
2473 tableWidget->setVerticalHeaderItem(row, id.createTableItem(iconCache, editor));
2474 row++;
2475 }
2476 // items
2477 const TableItemMap::const_iterator icend = m_items.constEnd();
2478 for (TableItemMap::const_iterator it = m_items.constBegin(); it != icend; ++ it)
2479 tableWidget->setItem(it.key().first, it.key().second, it.value().createTableItem(iconCache, editor));
2480}
2481
2482bool TableWidgetContents::operator==(const TableWidgetContents &rhs) const
2483{
2484 if (m_columnCount != rhs.m_columnCount || m_rowCount != rhs.m_rowCount)
2485 return false;
2486
2487 return m_horizontalHeader.m_items == rhs.m_horizontalHeader.m_items &&
2488 m_verticalHeader.m_items == rhs.m_verticalHeader.m_items &&
2489 m_items == rhs.m_items;
2490}
2491
2492// ---- ChangeTableContentsCommand ----
2493ChangeTableContentsCommand::ChangeTableContentsCommand(QDesignerFormWindowInterface *formWindow) :
2494 QDesignerFormWindowCommand(QApplication::translate("Command", "Change Table Contents"),
2495 formWindow), m_iconCache(0)
2496{
2497 FormWindowBase *fwb = qobject_cast<FormWindowBase *>(formWindow);
2498 if (fwb)
2499 m_iconCache = fwb->iconCache();
2500}
2501
2502void ChangeTableContentsCommand::init(QTableWidget *tableWidget,
2503 const TableWidgetContents &oldCont, const TableWidgetContents &newCont)
2504{
2505 m_tableWidget = tableWidget;
2506 m_oldContents = oldCont;
2507 m_newContents = newCont;
2508}
2509
2510void ChangeTableContentsCommand::redo()
2511{
2512 m_newContents.applyToTableWidget(m_tableWidget, m_iconCache, false);
2513 QMetaObject::invokeMethod(m_tableWidget, "updateGeometries");
2514}
2515
2516void ChangeTableContentsCommand::undo()
2517{
2518 m_oldContents.applyToTableWidget(m_tableWidget, m_iconCache, false);
2519 QMetaObject::invokeMethod(m_tableWidget, "updateGeometries");
2520}
2521
2522// --------- TreeWidgetContents
2523TreeWidgetContents::ItemContents::ItemContents(const QTreeWidgetItem *item, bool editor) :
2524 ListContents(item)
2525{
2526 static const int defaultFlags = QTreeWidgetItem().flags();
2527
2528 if (editor) {
2529 QVariant v = item->data(0, ItemFlagsShadowRole);
2530 m_itemFlags = v.isValid() ? v.toInt() : -1;
2531 } else {
2532 m_itemFlags = (item->flags() != defaultFlags) ? (int)item->flags() : -1;
2533 }
2534
2535 for (int i = 0; i < item->childCount(); i++)
2536 m_children.append(ItemContents(item->child(i), editor));
2537}
2538
2539QTreeWidgetItem *TreeWidgetContents::ItemContents::createTreeItem(DesignerIconCache *iconCache, bool editor) const
2540{
2541 QTreeWidgetItem *item = ListContents::createTreeItem(iconCache);
2542
2543 if (editor)
2544 item->setFlags(item->flags() | Qt::ItemIsEditable);
2545
2546 if (m_itemFlags != -1) {
2547 if (editor)
2548 item->setData(0, ItemFlagsShadowRole, qVariantFromValue(m_itemFlags));
2549 else
2550 item->setFlags((Qt::ItemFlags)m_itemFlags);
2551 }
2552
2553 foreach (const ItemContents &ic, m_children)
2554 item->addChild(ic.createTreeItem(iconCache, editor));
2555
2556 return item;
2557}
2558
2559bool TreeWidgetContents::ItemContents::operator==(const TreeWidgetContents::ItemContents &rhs) const
2560{
2561 return
2562 m_itemFlags == rhs.m_itemFlags &&
2563 m_items == rhs.m_items &&
2564 m_children == rhs.m_children;
2565}
2566
2567void TreeWidgetContents::clear()
2568{
2569 m_headerItem.m_items.clear();
2570 m_rootItems.clear();
2571}
2572
2573void TreeWidgetContents::fromTreeWidget(const QTreeWidget *treeWidget, bool editor)
2574{
2575 clear();
2576 m_headerItem = ListContents(treeWidget->headerItem());
2577 for (int col = 0; col < treeWidget->topLevelItemCount(); col++)
2578 m_rootItems.append(ItemContents(treeWidget->topLevelItem(col), editor));
2579}
2580
2581void TreeWidgetContents::applyToTreeWidget(QTreeWidget *treeWidget, DesignerIconCache *iconCache, bool editor) const
2582{
2583 treeWidget->clear();
2584
2585 treeWidget->setColumnCount(m_headerItem.m_items.count());
2586 treeWidget->setHeaderItem(m_headerItem.createTreeItem(iconCache));
2587 foreach (const ItemContents &ic, m_rootItems)
2588 treeWidget->addTopLevelItem(ic.createTreeItem(iconCache, editor));
2589 treeWidget->expandAll();
2590}
2591
2592bool TreeWidgetContents::operator==(const TreeWidgetContents &rhs) const
2593{
2594 return
2595 m_headerItem == rhs.m_headerItem &&
2596 m_rootItems == rhs.m_rootItems;
2597}
2598
2599// ---- ChangeTreeContentsCommand ----
2600ChangeTreeContentsCommand::ChangeTreeContentsCommand(QDesignerFormWindowInterface *formWindow)
2601 : QDesignerFormWindowCommand(QApplication::translate("Command", "Change Tree Contents"), formWindow),
2602 m_iconCache(0)
2603{
2604 FormWindowBase *fwb = qobject_cast<FormWindowBase *>(formWindow);
2605 if (fwb)
2606 m_iconCache = fwb->iconCache();
2607}
2608
2609void ChangeTreeContentsCommand::init(QTreeWidget *treeWidget,
2610 const TreeWidgetContents &oldState, const TreeWidgetContents &newState)
2611{
2612 m_treeWidget = treeWidget;
2613 m_oldState = oldState;
2614 m_newState = newState;
2615}
2616
2617void ChangeTreeContentsCommand::redo()
2618{
2619 m_newState.applyToTreeWidget(m_treeWidget, m_iconCache, false);
2620}
2621
2622void ChangeTreeContentsCommand::undo()
2623{
2624 m_oldState.applyToTreeWidget(m_treeWidget, m_iconCache, false);
2625}
2626
2627// ---- ChangeListContentsCommand ----
2628ChangeListContentsCommand::ChangeListContentsCommand(QDesignerFormWindowInterface *formWindow)
2629 : QDesignerFormWindowCommand(QString(), formWindow), m_iconCache(0)
2630{
2631 FormWindowBase *fwb = qobject_cast<FormWindowBase *>(formWindow);
2632 if (fwb)
2633 m_iconCache = fwb->iconCache();
2634}
2635
2636void ChangeListContentsCommand::init(QListWidget *listWidget,
2637 const ListContents &oldItems, const ListContents &items)
2638{
2639 m_listWidget = listWidget;
2640 m_comboBox = 0;
2641
2642 m_newItemsState = items;
2643 m_oldItemsState = oldItems;
2644}
2645
2646void ChangeListContentsCommand::init(QComboBox *comboBox,
2647 const ListContents &oldItems, const ListContents &items)
2648{
2649 m_listWidget = 0;
2650 m_comboBox = comboBox;
2651
2652 m_newItemsState = items;
2653 m_oldItemsState = oldItems;
2654}
2655
2656void ChangeListContentsCommand::redo()
2657{
2658 if (m_listWidget)
2659 m_newItemsState.applyToListWidget(m_listWidget, m_iconCache, false);
2660 else if (m_comboBox)
2661 m_newItemsState.applyToComboBox(m_comboBox, m_iconCache);
2662}
2663
2664void ChangeListContentsCommand::undo()
2665{
2666 if (m_listWidget)
2667 m_oldItemsState.applyToListWidget(m_listWidget, m_iconCache, false);
2668 else if (m_comboBox)
2669 m_oldItemsState.applyToComboBox(m_comboBox, m_iconCache);
2670}
2671
2672// ---- AddActionCommand ----
2673
2674AddActionCommand::AddActionCommand(QDesignerFormWindowInterface *formWindow) :
2675 QDesignerFormWindowCommand(QApplication::translate("Command", "Add action"), formWindow)
2676{
2677 m_action = 0;
2678}
2679
2680void AddActionCommand::init(QAction *action)
2681{
2682 Q_ASSERT(m_action == 0);
2683 m_action = action;
2684}
2685
2686void AddActionCommand::redo()
2687{
2688 core()->actionEditor()->setFormWindow(formWindow());
2689 core()->actionEditor()->manageAction(m_action);
2690}
2691
2692void AddActionCommand::undo()
2693{
2694 core()->actionEditor()->setFormWindow(formWindow());
2695 core()->actionEditor()->unmanageAction(m_action);
2696}
2697
2698// ---- RemoveActionCommand ----
2699
2700RemoveActionCommand::RemoveActionCommand(QDesignerFormWindowInterface *formWindow) :
2701 QDesignerFormWindowCommand(QApplication::translate("Command", "Remove action"), formWindow),
2702 m_action(0)
2703{
2704}
2705
2706static RemoveActionCommand::ActionData findActionIn(QAction *action)
2707{
2708 RemoveActionCommand::ActionData result;
2709 // We only want menus and toolbars, no toolbuttons.
2710 foreach (QWidget *widget, action->associatedWidgets())
2711 if (qobject_cast<const QMenu *>(widget) || qobject_cast<const QToolBar *>(widget)) {
2712 const QList<QAction*> actionList = widget->actions();
2713 const int size = actionList.size();
2714 for (int i = 0; i < size; ++i) {
2715 if (actionList.at(i) == action) {
2716 QAction *before = 0;
2717 if (i + 1 < size)
2718 before = actionList.at(i + 1);
2719 result.append(RemoveActionCommand::ActionDataItem(before, widget));
2720 break;
2721 }
2722 }
2723 }
2724 return result;
2725}
2726
2727void RemoveActionCommand::init(QAction *action)
2728{
2729 Q_ASSERT(m_action == 0);
2730 m_action = action;
2731
2732 m_actionData = findActionIn(action);
2733}
2734
2735void RemoveActionCommand::redo()
2736{
2737 QDesignerFormWindowInterface *fw = formWindow();
2738 foreach (const ActionDataItem &item, m_actionData) {
2739 item.widget->removeAction(m_action);
2740 }
2741 // Notify components (for example, signal slot editor)
2742 if (qdesigner_internal::FormWindowBase *fwb = qobject_cast<qdesigner_internal::FormWindowBase *>(fw))
2743 fwb->emitObjectRemoved(m_action);
2744
2745 core()->actionEditor()->setFormWindow(fw);
2746 core()->actionEditor()->unmanageAction(m_action);
2747 if (!m_actionData.empty())
2748 core()->objectInspector()->setFormWindow(fw);
2749}
2750
2751void RemoveActionCommand::undo()
2752{
2753 core()->actionEditor()->setFormWindow(formWindow());
2754 core()->actionEditor()->manageAction(m_action);
2755 foreach (const ActionDataItem &item, m_actionData) {
2756 item.widget->insertAction(item.before, m_action);
2757 }
2758 if (!m_actionData.empty())
2759 core()->objectInspector()->setFormWindow(formWindow());
2760}
2761
2762// ---- ActionInsertionCommand ----
2763
2764ActionInsertionCommand::ActionInsertionCommand(const QString &text, QDesignerFormWindowInterface *formWindow) :
2765 QDesignerFormWindowCommand(text, formWindow),
2766 m_parentWidget(0),
2767 m_action(0),
2768 m_beforeAction(0),
2769 m_update(false)
2770{
2771}
2772
2773void ActionInsertionCommand::init(QWidget *parentWidget, QAction *action, QAction *beforeAction, bool update)
2774{
2775 Q_ASSERT(m_parentWidget == 0);
2776 Q_ASSERT(m_action == 0);
2777
2778 m_parentWidget = parentWidget;
2779 m_action = action;
2780 m_beforeAction = beforeAction;
2781 m_update = update;
2782}
2783
2784void ActionInsertionCommand::insertAction()
2785{
2786 Q_ASSERT(m_action != 0);
2787 Q_ASSERT(m_parentWidget != 0);
2788
2789 if (m_beforeAction)
2790 m_parentWidget->insertAction(m_beforeAction, m_action);
2791 else
2792 m_parentWidget->addAction(m_action);
2793
2794 if (m_update) {
2795 cheapUpdate();
2796 if (QMenu *menu = m_action->menu())
2797 selectUnmanagedObject(menu);
2798 else
2799 selectUnmanagedObject(m_action);
2800 PropertyHelper::triggerActionChanged(m_action); // Update Used column in action editor.
2801 }
2802}
2803void ActionInsertionCommand::removeAction()
2804{
2805 Q_ASSERT(m_action != 0);
2806 Q_ASSERT(m_parentWidget != 0);
2807
2808 if (QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(m_parentWidget))
2809 menu->hideSubMenu();
2810
2811 m_parentWidget->removeAction(m_action);
2812
2813 if (m_update) {
2814 cheapUpdate();
2815 selectUnmanagedObject(m_parentWidget);
2816 PropertyHelper::triggerActionChanged(m_action); // Update Used column in action editor.
2817 }
2818}
2819
2820InsertActionIntoCommand::InsertActionIntoCommand(QDesignerFormWindowInterface *formWindow) :
2821 ActionInsertionCommand(QApplication::translate("Command", "Add action"), formWindow)
2822{
2823}
2824// ---- RemoveActionFromCommand ----
2825
2826RemoveActionFromCommand::RemoveActionFromCommand(QDesignerFormWindowInterface *formWindow) :
2827 ActionInsertionCommand(QApplication::translate("Command", "Remove action"), formWindow)
2828{
2829}
2830
2831// ---- AddMenuActionCommand ----
2832
2833MenuActionCommand::MenuActionCommand(const QString &text, QDesignerFormWindowInterface *formWindow) :
2834 QDesignerFormWindowCommand(text, formWindow),
2835 m_action(0),
2836 m_actionBefore(0),
2837 m_menuParent(0),
2838 m_associatedWidget(0),
2839 m_objectToSelect(0)
2840{
2841}
2842
2843void MenuActionCommand::init(QAction *action, QAction *actionBefore,
2844 QWidget *associatedWidget, QWidget *objectToSelect)
2845{
2846 QMenu *menu = action->menu();
2847 Q_ASSERT(menu);
2848 m_menuParent = menu->parentWidget();
2849 m_action = action;
2850 m_actionBefore = actionBefore;
2851 m_associatedWidget = associatedWidget;
2852 m_objectToSelect = objectToSelect;
2853}
2854
2855void MenuActionCommand::insertMenu()
2856{
2857 core()->metaDataBase()->add(m_action);
2858 QMenu *menu = m_action->menu();
2859 if (m_menuParent && menu->parentWidget() != m_menuParent)
2860 menu->setParent(m_menuParent);
2861 core()->metaDataBase()->add(menu);
2862 m_associatedWidget->insertAction(m_actionBefore, m_action);
2863 cheapUpdate();
2864 selectUnmanagedObject(menu);
2865}
2866
2867void MenuActionCommand::removeMenu()
2868{
2869 m_action->menu()->setParent(0);
2870 QMenu *menu = m_action->menu();
2871 core()->metaDataBase()->remove(menu);
2872 menu->setParent(0);
2873 core()->metaDataBase()->remove(m_action);
2874 m_associatedWidget->removeAction(m_action);
2875 cheapUpdate();
2876 selectUnmanagedObject(m_objectToSelect);
2877}
2878
2879AddMenuActionCommand::AddMenuActionCommand(QDesignerFormWindowInterface *formWindow) :
2880 MenuActionCommand(QApplication::translate("Command", "Add menu"), formWindow)
2881{
2882}
2883
2884// ---- RemoveMenuActionCommand ----
2885RemoveMenuActionCommand::RemoveMenuActionCommand(QDesignerFormWindowInterface *formWindow) :
2886 MenuActionCommand(QApplication::translate("Command", "Remove menu"), formWindow)
2887{
2888}
2889
2890// ---- CreateSubmenuCommand ----
2891CreateSubmenuCommand::CreateSubmenuCommand(QDesignerFormWindowInterface *formWindow) :
2892 QDesignerFormWindowCommand(QApplication::translate("Command", "Create submenu"), formWindow),
2893 m_action(0),
2894 m_menu(0),
2895 m_objectToSelect(0)
2896{
2897}
2898
2899void CreateSubmenuCommand::init(QDesignerMenu *menu, QAction *action, QObject *objectToSelect)
2900{
2901 m_menu = menu;
2902 m_action = action;
2903 m_objectToSelect = objectToSelect;
2904}
2905
2906void CreateSubmenuCommand::redo()
2907{
2908 m_menu->createRealMenuAction(m_action);
2909 cheapUpdate();
2910 if (m_objectToSelect)
2911 selectUnmanagedObject(m_objectToSelect);
2912}
2913
2914void CreateSubmenuCommand::undo()
2915{
2916 m_menu->removeRealMenu(m_action);
2917 cheapUpdate();
2918 selectUnmanagedObject(m_menu);
2919}
2920
2921// ---- DeleteToolBarCommand ----
2922DeleteToolBarCommand::DeleteToolBarCommand(QDesignerFormWindowInterface *formWindow)
2923 : QDesignerFormWindowCommand(QApplication::translate("Command", "Delete Tool Bar"), formWindow)
2924{
2925}
2926
2927void DeleteToolBarCommand::init(QToolBar *toolBar)
2928{
2929 m_toolBar = toolBar;
2930 m_mainWindow = qobject_cast<QMainWindow*>(toolBar->parentWidget());
2931}
2932
2933void DeleteToolBarCommand::redo()
2934{
2935 if (m_mainWindow) {
2936 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
2937 Q_ASSERT(c != 0);
2938 for (int i=0; i<c->count(); ++i) {
2939 if (c->widget(i) == m_toolBar) {
2940 c->remove(i);
2941 break;
2942 }
2943 }
2944 }
2945
2946 core()->metaDataBase()->remove(m_toolBar);
2947 m_toolBar->hide();
2948 m_toolBar->setParent(formWindow());
2949 formWindow()->emitSelectionChanged();
2950}
2951
2952void DeleteToolBarCommand::undo()
2953{
2954 if (m_mainWindow) {
2955 m_toolBar->setParent(m_mainWindow);
2956 QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), m_mainWindow);
2957
2958 c->addWidget(m_toolBar);
2959
2960 core()->metaDataBase()->add(m_toolBar);
2961 m_toolBar->show();
2962 formWindow()->emitSelectionChanged();
2963 }
2964}
2965
2966} // namespace qdesigner_internal
2967
2968QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.