1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include <QtDesigner/QtDesigner>
31
#include "qdesigner_command_p.h"
33
#include "qlayout_widget_p.h"
34
#include "qdesigner_widget_p.h"
35
#include "qdesigner_promotedwidget_p.h"
37
#include <QtCore/qdebug.h>
39
#include <QtGui/QToolBox>
40
#include <QtGui/QStackedWidget>
41
#include <QtGui/QTabWidget>
42
#include <QtGui/QSplitter>
43
#include <QtGui/QDockWidget>
44
#include <QtGui/QMainWindow>
46
// ---- QDesignerFormEditorCommand ----
47
QDesignerFormEditorCommand::QDesignerFormEditorCommand(const QString &description, QDesignerFormEditorInterface *core)
48
: QtCommand(description),
53
QDesignerFormEditorInterface *QDesignerFormEditorCommand::core() const
58
// ---- QDesignerFormWindowManagerCommand ----
59
QDesignerFormWindowManagerCommand::QDesignerFormWindowManagerCommand(const QString &description, QDesignerFormWindowManagerInterface *formWindowManager)
60
: QtCommand(description),
61
m_formWindowManager(formWindowManager)
65
QDesignerFormWindowManagerInterface *QDesignerFormWindowManagerCommand::formWindowManager() const
67
return m_formWindowManager;
70
// ---- QDesignerFormWindowCommand ----
71
QDesignerFormWindowCommand::QDesignerFormWindowCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
72
: QtCommand(description),
73
m_formWindow(formWindow)
77
QDesignerFormWindowInterface *QDesignerFormWindowCommand::formWindow() const
82
bool QDesignerFormWindowCommand::hasLayout(QWidget *widget) const
84
QDesignerFormEditorInterface *core = formWindow()->core();
85
if (widget && LayoutInfo::layoutType(core, widget) != LayoutInfo::NoLayout) {
86
QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget);
93
void QDesignerFormWindowCommand::checkObjectName(QWidget *widget)
95
if (widget->objectName().isEmpty())
96
qWarning("invalid object name");
98
QDesignerFormEditorInterface *core = formWindow()->core();
99
if (QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget)) {
100
item->setName(widget->objectName());
104
void QDesignerFormWindowCommand::updateBuddies(const QString &old_name,
105
const QString &new_name)
107
QDesignerFormEditorInterface *core = formWindow()->core();
109
QList<QDesignerLabel*> label_list = qFindChildren<QDesignerLabel*>(formWindow());
110
foreach (QDesignerLabel *label, label_list) {
111
QDesignerPropertySheetExtension* propertySheet
112
= qt_extension<QDesignerPropertySheetExtension*>
113
(core->extensionManager(), label);
114
if (propertySheet == 0)
116
int idx = propertySheet->indexOf(QLatin1String("buddy"));
119
if (propertySheet->property(idx).toString() == old_name)
120
propertySheet->setProperty(idx, new_name);
124
void QDesignerFormWindowCommand::checkSelection(QWidget *widget)
129
QDesignerFormEditorInterface *core = formWindow()->core();
131
formWindow()->updateSelection(widget);
133
if (LayoutInfo::layoutType(core, widget) != LayoutInfo::NoLayout)
134
formWindow()->updateChildSelections(widget);
138
void QDesignerFormWindowCommand::checkParent(QWidget *widget, QWidget *parentWidget)
142
if (widget->parentWidget() != parentWidget)
143
widget->setParent(parentWidget);
146
// ---- SetPropertyCommand ----
147
SetPropertyCommand::SetPropertyCommand(QDesignerFormWindowInterface *formWindow)
148
: QDesignerFormWindowCommand(QString(), formWindow),
156
QWidget *SetPropertyCommand::widget() const
161
QWidget *SetPropertyCommand::parentWidget() const
163
return m_parentWidget;
166
void SetPropertyCommand::init(QWidget *widget, const QString &propertyName, const QVariant &newValue)
171
m_parentWidget = widget->parentWidget();
172
m_propertyName = propertyName;
173
m_newValue = newValue;
175
QDesignerFormEditorInterface *core = formWindow()->core();
176
m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget);
177
Q_ASSERT(m_propertySheet);
179
m_index = m_propertySheet->indexOf(m_propertyName);
180
Q_ASSERT(m_index != -1);
182
m_changed = m_propertySheet->isChanged(m_index);
183
m_oldValue = m_propertySheet->property(m_index);
185
setDescription(tr("changed '%1' of '%2'").arg(m_propertyName).arg(m_widget->objectName()));
188
void SetPropertyCommand::redo()
190
Q_ASSERT(m_propertySheet);
191
Q_ASSERT(m_index != -1);
193
m_propertySheet->setProperty(m_index, m_newValue);
194
m_changed = m_propertySheet->isChanged(m_index);
195
m_propertySheet->setChanged(m_index, true);
197
if (m_propertyName == QLatin1String("geometry")) {
198
checkSelection(m_widget);
199
checkParent(m_widget, m_parentWidget);
200
} else if (m_propertyName == QLatin1String("objectName")) {
201
checkObjectName(m_widget);
202
updateBuddies(m_oldValue.toString(), m_newValue.toString());
205
if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
206
if (propertyEditor->object() == widget())
207
propertyEditor->setPropertyValue(propertyName(), m_newValue, true);
211
void SetPropertyCommand::undo()
213
Q_ASSERT(m_propertySheet);
214
Q_ASSERT(m_index != -1);
216
m_propertySheet->setProperty(m_index, m_oldValue);
217
m_propertySheet->setChanged(m_index, m_changed);
219
if (m_propertyName == QLatin1String("geometry")) {
220
checkSelection(m_widget);
221
checkParent(m_widget, m_parentWidget);
222
} else if (m_propertyName == QLatin1String("objectName")) {
223
checkObjectName(m_widget);
224
updateBuddies(m_newValue.toString(), m_oldValue.toString());
227
if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
228
if (propertyEditor->object() == widget())
229
propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed);
233
bool SetPropertyCommand::mergeMeWith(QtCommand *other)
235
if (SetPropertyCommand *cmd = qobject_cast<SetPropertyCommand*>(other)) {
236
if (cmd->propertyName() == propertyName() && cmd->widget() == widget()) {
237
m_newValue = cmd->newValue();
245
// ---- ResetPropertyCommand ----
246
ResetPropertyCommand::ResetPropertyCommand(QDesignerFormWindowInterface *formWindow)
247
: QDesignerFormWindowCommand(QString(), formWindow),
255
QWidget *ResetPropertyCommand::widget() const
260
QWidget *ResetPropertyCommand::parentWidget() const
262
return m_parentWidget;
265
void ResetPropertyCommand::init(QWidget *widget, const QString &propertyName)
270
m_parentWidget = widget->parentWidget();
271
m_propertyName = propertyName;
273
QDesignerFormEditorInterface *core = formWindow()->core();
274
m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget);
275
Q_ASSERT(m_propertySheet);
277
m_index = m_propertySheet->indexOf(m_propertyName);
278
Q_ASSERT(m_index != -1);
280
m_changed = m_propertySheet->isChanged(m_index);
281
m_oldValue = m_propertySheet->property(m_index);
283
setDescription(tr("reset '%1' of '%2'").arg(m_propertyName).arg(m_widget->objectName()));
286
void ResetPropertyCommand::redo()
288
Q_ASSERT(m_propertySheet);
289
Q_ASSERT(m_index != -1);
291
QObject *obj = m_widget;
292
if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(obj))
293
obj = promoted->child();
297
if (m_propertySheet->reset(m_index)) {
298
new_value = m_propertySheet->property(m_index);
300
int item_idx = formWindow()->core()->widgetDataBase()->indexOfObject(obj);
301
if (item_idx == -1) {
302
new_value = m_oldValue; // We simply don't know the value in this case
304
QDesignerWidgetDataBaseItemInterface *item
305
= formWindow()->core()->widgetDataBase()->item(item_idx);
306
QList<QVariant> default_prop_values = item->defaultPropertyValues();
307
if (m_index < default_prop_values.size())
308
new_value = default_prop_values.at(m_index);
310
new_value = m_oldValue; // Again, we just don't know
313
m_propertySheet->setProperty(m_index, new_value);
316
m_propertySheet->setChanged(m_index, false);
318
if (m_propertyName == QLatin1String("geometry")) {
319
checkSelection(m_widget);
320
checkParent(m_widget, m_parentWidget);
321
} else if (m_propertyName == QLatin1String("objectName")) {
322
checkObjectName(m_widget);
325
if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
326
if (propertyEditor->object() == widget())
327
propertyEditor->setPropertyValue(propertyName(), new_value, false);
331
void ResetPropertyCommand::undo()
333
Q_ASSERT(m_propertySheet);
334
Q_ASSERT(m_index != -1);
336
m_propertySheet->setProperty(m_index, m_oldValue);
337
m_propertySheet->setChanged(m_index, m_changed);
339
if (m_propertyName == QLatin1String("geometry")) {
340
checkSelection(m_widget);
341
checkParent(m_widget, m_parentWidget);
342
} else if (m_propertyName == QLatin1String("objectName")) {
343
checkObjectName(m_widget);
346
if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
347
if (propertyEditor->object() == widget())
348
propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed);
352
// ---- InsertWidgetCommand ----
353
InsertWidgetCommand::InsertWidgetCommand(QDesignerFormWindowInterface *formWindow)
354
: QDesignerFormWindowCommand(QString(), formWindow)
358
void InsertWidgetCommand::init(QWidget *widget)
362
setDescription(tr("Insert '%1'").arg(widget->objectName()));
364
QWidget *parentWidget = m_widget->parentWidget();
365
QDesignerFormEditorInterface *core = formWindow()->core();
366
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
368
m_insertMode = deco ? deco->currentInsertMode() : QDesignerLayoutDecorationExtension::InsertWidgetMode;
369
m_cell = deco ? deco->currentCell() : qMakePair(0, 0);
372
void InsertWidgetCommand::redo()
374
checkObjectName(m_widget);
376
QWidget *parentWidget = m_widget->parentWidget();
378
QDesignerFormEditorInterface *core = formWindow()->core();
379
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
382
if (LayoutInfo::layoutType(core, parentWidget) == LayoutInfo::Grid) {
383
switch (m_insertMode) {
384
case QDesignerLayoutDecorationExtension::InsertRowMode: {
385
deco->insertRow(m_cell.first);
388
case QDesignerLayoutDecorationExtension::InsertColumnMode: {
389
deco->insertColumn(m_cell.second);
395
deco->insertWidget(m_widget, m_cell);
398
formWindow()->manageWidget(m_widget);
402
void InsertWidgetCommand::undo()
404
QWidget *parentWidget = m_widget->parentWidget();
406
QDesignerFormEditorInterface *core = formWindow()->core();
407
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
410
deco->removeWidget(m_widget);
414
formWindow()->unmanageWidget(m_widget);
418
// ---- RaiseWidgetCommand ----
419
RaiseWidgetCommand::RaiseWidgetCommand(QDesignerFormWindowInterface *formWindow)
420
: QDesignerFormWindowCommand(QString(), formWindow)
424
void RaiseWidgetCommand::init(QWidget *widget)
427
setDescription(tr("Raise '%1'").arg(widget->objectName()));
430
void RaiseWidgetCommand::redo()
435
void RaiseWidgetCommand::undo()
439
// ---- LowerWidgetCommand ----
440
LowerWidgetCommand::LowerWidgetCommand(QDesignerFormWindowInterface *formWindow)
441
: QDesignerFormWindowCommand(QString(), formWindow)
445
void LowerWidgetCommand::init(QWidget *widget)
448
setDescription(tr("Lower '%1'").arg(widget->objectName()));
451
void LowerWidgetCommand::redo()
456
void LowerWidgetCommand::undo()
460
// ---- DeleteWidgetCommand ----
461
DeleteWidgetCommand::DeleteWidgetCommand(QDesignerFormWindowInterface *formWindow)
462
: QDesignerFormWindowCommand(QString(), formWindow)
466
void DeleteWidgetCommand::init(QWidget *widget)
469
m_parentWidget = widget->parentWidget();
470
m_geometry = widget->geometry();
472
m_layoutType = LayoutInfo::NoLayout;
474
if (hasLayout(m_parentWidget)) {
475
m_layoutType = LayoutInfo::layoutType(formWindow()->core(), m_parentWidget);
477
switch (m_layoutType) {
478
case LayoutInfo::VBox:
479
m_index = static_cast<QVBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);
481
case LayoutInfo::HBox:
482
m_index = static_cast<QHBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);
484
case LayoutInfo::Grid: {
486
while (QLayoutItem *item = m_parentWidget->layout()->itemAt(m_index)) {
487
if (item->widget() == m_widget)
492
static_cast<QGridLayout*>(m_parentWidget->layout())->getItemPosition(m_index, &m_row, &m_col, &m_rowspan, &m_colspan);
500
m_formItem = formWindow()->core()->metaDataBase()->item(formWindow());
501
m_tabOrderIndex = m_formItem->tabOrder().indexOf(widget);
503
setDescription(tr("Delete '%1'").arg(widget->objectName()));
506
void DeleteWidgetCommand::redo()
508
QDesignerFormEditorInterface *core = formWindow()->core();
509
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), m_parentWidget);
512
deco->removeWidget(m_widget);
514
formWindow()->unmanageWidget(m_widget);
515
m_widget->setParent(formWindow());
518
if (m_tabOrderIndex != -1) {
519
QList<QWidget*> tab_order = m_formItem->tabOrder();
520
tab_order.removeAt(m_tabOrderIndex);
521
m_formItem->setTabOrder(tab_order);
524
formWindow()->emitSelectionChanged();
527
void DeleteWidgetCommand::undo()
529
m_widget->setParent(m_parentWidget);
530
m_widget->setGeometry(m_geometry);
531
formWindow()->manageWidget(m_widget);
533
// ### set up alignment
534
switch (m_layoutType) {
535
case LayoutInfo::VBox: {
536
QVBoxLayout *vbox = static_cast<QVBoxLayout*>(m_parentWidget->layout());
537
insert_into_box_layout(vbox, m_index, m_widget);
540
case LayoutInfo::HBox: {
541
QHBoxLayout *hbox = static_cast<QHBoxLayout*>(m_parentWidget->layout());
542
insert_into_box_layout(hbox, m_index, m_widget);
545
case LayoutInfo::Grid: {
546
QGridLayout *grid = static_cast<QGridLayout*>(m_parentWidget->layout());
547
add_to_grid_layout(grid, m_widget, m_row, m_col, m_rowspan, m_colspan);
556
if (m_tabOrderIndex != -1) {
557
QList<QWidget*> tab_order = m_formItem->tabOrder();
558
tab_order.insert(m_tabOrderIndex, m_widget);
559
m_formItem->setTabOrder(tab_order);
562
formWindow()->emitSelectionChanged();
565
// ---- ReparentWidgetCommand ----
566
ReparentWidgetCommand::ReparentWidgetCommand(QDesignerFormWindowInterface *formWindow)
567
: QDesignerFormWindowCommand(QString(), formWindow)
571
void ReparentWidgetCommand::init(QWidget *widget, QWidget *parentWidget)
576
m_oldParentWidget = widget->parentWidget();
577
m_newParentWidget = parentWidget;
579
m_oldPos = m_widget->pos();
580
m_newPos = m_newParentWidget->mapFromGlobal(m_oldParentWidget->mapToGlobal(m_oldPos));
582
setDescription(tr("Reparent '%1'").arg(widget->objectName()));
585
void ReparentWidgetCommand::redo()
587
m_widget->setParent(m_newParentWidget);
588
m_widget->move(m_newPos);
593
void ReparentWidgetCommand::undo()
595
m_widget->setParent(m_oldParentWidget);
596
m_widget->move(m_oldPos);
601
// ---- PromoteToCustomWidgetCommand ----
603
static void replace_widget_item(QDesignerFormWindowInterface *fw, QWidget *wgt, QWidget *promoted)
605
QDesignerFormEditorInterface *core = fw->core();
606
QWidget *parent = wgt->parentWidget();
609
if (QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) {
610
QLayout *layout = LayoutInfo::managedLayout(core, parent);
611
Q_ASSERT(layout != 0);
613
int old_index = layout->indexOf(wgt);
614
Q_ASSERT(old_index != -1);
616
info = deco->itemInfo(old_index);
618
QLayoutItem *item = layout->takeAt(old_index);
623
if (qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) {
624
QLayout *layout = LayoutInfo::managedLayout(core, parent);
625
Q_ASSERT(layout != 0);
627
// ### check if `info' is valid!
629
switch (LayoutInfo::layoutType(core, layout)) {
630
default: Q_ASSERT(0); break;
632
case LayoutInfo::VBox:
633
insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.top(), promoted);
636
case LayoutInfo::HBox:
637
insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.left(), promoted);
640
case LayoutInfo::Grid:
641
add_to_grid_layout(static_cast<QGridLayout*>(layout), promoted, info.top(), info.left(), info.height(), info.width());
647
PromoteToCustomWidgetCommand::PromoteToCustomWidgetCommand
648
(QDesignerFormWindowInterface *formWindow)
649
: QDesignerFormWindowCommand(tr("Promote to custom widget"), formWindow)
655
void PromoteToCustomWidgetCommand::init(QDesignerWidgetDataBaseItemInterface *item,
659
m_promoted = new QDesignerPromotedWidget(item, widget->parentWidget());
662
void PromoteToCustomWidgetCommand::redo()
664
m_promoted->setObjectName(QLatin1String("__qt__promoted_") + m_widget->objectName());
665
m_promoted->setGeometry(m_widget->geometry());
667
replace_widget_item(formWindow(), m_widget, m_promoted);
669
m_promoted->setChildWidget(m_widget);
670
formWindow()->manageWidget(m_promoted);
672
formWindow()->clearSelection();
673
formWindow()->selectWidget(m_promoted);
677
void PromoteToCustomWidgetCommand::undo()
679
m_promoted->setChildWidget(0);
680
m_widget->setParent(m_promoted->parentWidget());
681
m_widget->setGeometry(m_promoted->geometry());
683
replace_widget_item(formWindow(), m_promoted, m_widget);
685
formWindow()->manageWidget(m_widget);
686
formWindow()->unmanageWidget(m_promoted);
691
formWindow()->clearSelection();
692
formWindow()->selectWidget(m_promoted);
695
// ---- DemoteFromCustomWidgetCommand ----
697
DemoteFromCustomWidgetCommand::DemoteFromCustomWidgetCommand
698
(QDesignerFormWindowInterface *formWindow)
699
: QDesignerFormWindowCommand(tr("Demote from custom widget"), formWindow)
701
m_promote_cmd = new PromoteToCustomWidgetCommand(formWindow);
704
void DemoteFromCustomWidgetCommand::init(QDesignerPromotedWidget *promoted)
706
m_promote_cmd->m_widget = promoted->child();
707
m_promote_cmd->m_promoted = promoted;
710
void DemoteFromCustomWidgetCommand::redo()
712
m_promote_cmd->undo();
713
m_promote_cmd->m_widget->show();
716
void DemoteFromCustomWidgetCommand::undo()
718
m_promote_cmd->redo();
721
// ---- LayoutCommand ----
722
LayoutCommand::LayoutCommand(QDesignerFormWindowInterface *formWindow)
723
: QDesignerFormWindowCommand(QString(), formWindow)
727
LayoutCommand::~LayoutCommand()
729
m_layout->deleteLater();
732
void LayoutCommand::init(QWidget *parentWidget, const QList<QWidget*> &widgets, LayoutInfo::Type layoutType,
733
QWidget *layoutBase, bool splitter)
735
m_parentWidget = parentWidget;
737
formWindow()->simplifySelection(&m_widgets);
738
QPoint grid = formWindow()->grid();
739
QSize sz(qMax(5, grid.x()), qMax(5, grid.y()));
741
switch (layoutType) {
742
case LayoutInfo::Grid:
743
m_layout = new GridLayout(widgets, m_parentWidget, formWindow(), layoutBase, sz);
744
setDescription(tr("Lay out using grid"));
747
case LayoutInfo::VBox:
748
m_layout = new VerticalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter);
749
setDescription(tr("Lay out vertically"));
752
case LayoutInfo::HBox:
753
m_layout = new HorizontalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter);
754
setDescription(tr("Lay out horizontally"));
763
void LayoutCommand::redo()
765
m_layout->doLayout();
766
checkSelection(m_parentWidget);
769
void LayoutCommand::undo()
771
QDesignerFormEditorInterface *core = formWindow()->core();
773
QWidget *lb = m_layout->layoutBaseWidget();
774
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb);
776
QWidget *p = m_layout->parentWidget();
777
if (!deco && hasLayout(p)) {
778
deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), p);
781
delete deco; // release the extension
783
m_layout->undoLayout();
785
if (!m_layoutBase && lb != 0 && !qobject_cast<QLayoutWidget*>(lb)) {
786
core->metaDataBase()->add(lb);
790
checkSelection(m_parentWidget);
793
// ---- BreakLayoutCommand ----
794
BreakLayoutCommand::BreakLayoutCommand(QDesignerFormWindowInterface *formWindow)
795
: QDesignerFormWindowCommand(tr("Break layout"), formWindow)
799
BreakLayoutCommand::~BreakLayoutCommand()
803
void BreakLayoutCommand::init(const QList<QWidget*> &widgets, QWidget *layoutBase)
805
QDesignerFormEditorInterface *core = formWindow()->core();
808
m_layoutBase = core->widgetFactory()->containerOfWidget(layoutBase);
811
QPoint grid = formWindow()->grid();
813
LayoutInfo::Type lay = LayoutInfo::layoutType(core, m_layoutBase);
814
if (lay == LayoutInfo::HBox)
815
m_layout = new HorizontalLayout(widgets, m_layoutBase, formWindow(), m_layoutBase, qobject_cast<QSplitter*>(m_layoutBase) != 0);
816
else if (lay == LayoutInfo::VBox)
817
m_layout = new VerticalLayout(widgets, m_layoutBase, formWindow(), m_layoutBase, qobject_cast<QSplitter*>(m_layoutBase) != 0);
818
else if (lay == LayoutInfo::Grid)
819
m_layout = new GridLayout(widgets, m_layoutBase, formWindow(), m_layoutBase, QSize(qMax(5, grid.x()), qMax(5, grid.y())));
822
Q_ASSERT(m_layout != 0);
826
m_margin = m_layout->margin();
827
m_spacing = m_layout->spacing();
830
void BreakLayoutCommand::redo()
835
QDesignerFormEditorInterface *core = formWindow()->core();
836
QWidget *lb = m_layout->layoutBaseWidget();
837
QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb);
838
QWidget *p = m_layout->parentWidget();
839
if (!deco && hasLayout(p))
840
deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), p);
842
delete deco; // release the extension
844
formWindow()->clearSelection(false);
845
m_layout->breakLayout();
847
core->metaDataBase()->add(lb);
849
foreach (QWidget *widget, m_widgets) {
850
widget->resize(widget->size().expandedTo(QSize(16, 16)));
854
void BreakLayoutCommand::undo()
859
formWindow()->clearSelection(false);
860
m_layout->doLayout();
862
if (m_layoutBase && m_layoutBase->layout()) {
863
m_layoutBase->layout()->setSpacing(m_spacing);
864
m_layoutBase->layout()->setMargin(m_margin);
868
// ---- ToolBoxCommand ----
869
ToolBoxCommand::ToolBoxCommand(QDesignerFormWindowInterface *formWindow)
870
: QDesignerFormWindowCommand(QString(), formWindow)
874
ToolBoxCommand::~ToolBoxCommand()
878
void ToolBoxCommand::init(QToolBox *toolBox)
881
m_index = m_toolBox->currentIndex();
882
m_widget = m_toolBox->widget(m_index);
883
m_itemText = m_toolBox->itemText(m_index);
884
m_itemIcon = m_toolBox->itemIcon(m_index);
887
void ToolBoxCommand::removePage()
889
m_toolBox->removeItem(m_index);
892
m_widget->setParent(formWindow());
895
void ToolBoxCommand::addPage()
897
m_widget->setParent(m_toolBox);
898
m_toolBox->insertItem(m_index, m_widget, m_itemIcon, m_itemText);
903
// ---- DeleteToolBoxPageCommand ----
904
DeleteToolBoxPageCommand::DeleteToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
905
: ToolBoxCommand(formWindow)
909
DeleteToolBoxPageCommand::~DeleteToolBoxPageCommand()
913
void DeleteToolBoxPageCommand::init(QToolBox *toolBox)
915
ToolBoxCommand::init(toolBox);
916
setDescription(tr("Delete Page"));
919
void DeleteToolBoxPageCommand::redo()
924
void DeleteToolBoxPageCommand::undo()
929
// ---- AddToolBoxPageCommand ----
930
AddToolBoxPageCommand::AddToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
931
: ToolBoxCommand(formWindow)
935
AddToolBoxPageCommand::~AddToolBoxPageCommand()
939
void AddToolBoxPageCommand::init(QToolBox *toolBox)
943
m_index = m_toolBox->currentIndex();
944
m_widget = new QDesignerWidget(formWindow(), m_toolBox);
945
m_itemText = tr("Page");
946
m_itemIcon = QIcon();
947
m_widget->setObjectName(tr("page"));
948
formWindow()->ensureUniqueObjectName(m_widget);
950
setDescription(tr("Add Page"));
952
QDesignerFormEditorInterface *core = formWindow()->core();
953
core->metaDataBase()->add(m_widget);
956
void AddToolBoxPageCommand::redo()
961
void AddToolBoxPageCommand::undo()
966
// ---- TabWidgetCommand ----
967
TabWidgetCommand::TabWidgetCommand(QDesignerFormWindowInterface *formWindow)
968
: QDesignerFormWindowCommand(QString(), formWindow)
972
TabWidgetCommand::~TabWidgetCommand()
976
void TabWidgetCommand::init(QTabWidget *tabWidget)
978
m_tabWidget = tabWidget;
979
m_index = m_tabWidget->currentIndex();
980
m_widget = m_tabWidget->widget(m_index);
981
m_itemText = m_tabWidget->tabText(m_index);
982
m_itemIcon = m_tabWidget->tabIcon(m_index);
985
void TabWidgetCommand::removePage()
987
m_tabWidget->removeTab(m_index);
990
m_widget->setParent(formWindow());
991
m_tabWidget->setCurrentIndex(qMin(m_index, m_tabWidget->count()));
994
void TabWidgetCommand::addPage()
996
m_widget->setParent(0);
997
m_tabWidget->insertTab(m_index, m_widget, m_itemIcon, m_itemText);
999
m_tabWidget->setCurrentIndex(m_index);
1002
// ---- DeleteTabPageCommand ----
1003
DeleteTabPageCommand::DeleteTabPageCommand(QDesignerFormWindowInterface *formWindow)
1004
: TabWidgetCommand(formWindow)
1008
DeleteTabPageCommand::~DeleteTabPageCommand()
1012
void DeleteTabPageCommand::init(QTabWidget *tabWidget)
1014
TabWidgetCommand::init(tabWidget);
1015
setDescription(tr("Delete Page"));
1018
void DeleteTabPageCommand::redo()
1023
void DeleteTabPageCommand::undo()
1028
// ---- AddTabPageCommand ----
1029
AddTabPageCommand::AddTabPageCommand(QDesignerFormWindowInterface *formWindow)
1030
: TabWidgetCommand(formWindow)
1034
AddTabPageCommand::~AddTabPageCommand()
1038
void AddTabPageCommand::init(QTabWidget *tabWidget)
1040
m_tabWidget = tabWidget;
1042
m_index = m_tabWidget->currentIndex();
1043
m_widget = new QDesignerWidget(formWindow(), m_tabWidget);
1044
m_itemText = tr("Page");
1045
m_itemIcon = QIcon();
1046
m_widget->setObjectName(tr("tab"));
1047
formWindow()->ensureUniqueObjectName(m_widget);
1049
setDescription(tr("Add Page"));
1051
QDesignerFormEditorInterface *core = formWindow()->core();
1052
core->metaDataBase()->add(m_widget);
1055
void AddTabPageCommand::redo()
1060
void AddTabPageCommand::undo()
1065
// ---- MoveTabPageCommand ----
1066
MoveTabPageCommand::MoveTabPageCommand(QDesignerFormWindowInterface *formWindow)
1067
: TabWidgetCommand(formWindow)
1071
MoveTabPageCommand::~MoveTabPageCommand()
1075
void MoveTabPageCommand::init(QTabWidget *tabWidget, QWidget *page,
1076
const QIcon &icon, const QString &label,
1077
int index, int newIndex)
1079
TabWidgetCommand::init(tabWidget);
1080
setDescription(tr("Move Page"));
1083
m_newIndex = newIndex;
1089
void MoveTabPageCommand::redo()
1091
m_tabWidget->removeTab(m_oldIndex);
1092
m_tabWidget->insertTab(m_newIndex, m_page, m_icon, m_label);
1093
m_tabWidget->setCurrentIndex(m_newIndex);
1096
void MoveTabPageCommand::undo()
1098
m_tabWidget->removeTab(m_newIndex);
1099
m_tabWidget->insertTab(m_oldIndex, m_page, m_icon, m_label);
1100
m_tabWidget->setCurrentIndex(m_oldIndex);
1103
// ---- StackedWidgetCommand ----
1104
StackedWidgetCommand::StackedWidgetCommand(QDesignerFormWindowInterface *formWindow)
1105
: QDesignerFormWindowCommand(QString(), formWindow)
1109
StackedWidgetCommand::~StackedWidgetCommand()
1113
void StackedWidgetCommand::init(QStackedWidget *stackedWidget)
1115
m_stackedWidget = stackedWidget;
1116
m_index = m_stackedWidget->currentIndex();
1117
m_widget = m_stackedWidget->widget(m_index);
1120
void StackedWidgetCommand::removePage()
1122
m_stackedWidget->removeWidget(m_stackedWidget->widget(m_index));
1125
m_widget->setParent(formWindow());
1128
void StackedWidgetCommand::addPage()
1130
m_stackedWidget->insertWidget(m_index, m_widget);
1135
// ---- DeleteStackedWidgetPageCommand ----
1136
DeleteStackedWidgetPageCommand::DeleteStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
1137
: StackedWidgetCommand(formWindow)
1141
DeleteStackedWidgetPageCommand::~DeleteStackedWidgetPageCommand()
1145
void DeleteStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
1147
StackedWidgetCommand::init(stackedWidget);
1148
setDescription(tr("Delete Page"));
1151
void DeleteStackedWidgetPageCommand::redo()
1156
void DeleteStackedWidgetPageCommand::undo()
1161
// ---- AddStackedWidgetPageCommand ----
1162
AddStackedWidgetPageCommand::AddStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
1163
: StackedWidgetCommand(formWindow)
1167
AddStackedWidgetPageCommand::~AddStackedWidgetPageCommand()
1171
void AddStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
1173
m_stackedWidget = stackedWidget;
1175
m_index = m_stackedWidget->currentIndex();
1176
m_widget = new QDesignerWidget(formWindow(), m_stackedWidget);
1177
m_widget->setObjectName(tr("page"));
1178
formWindow()->ensureUniqueObjectName(m_widget);
1180
setDescription(tr("Add Page"));
1182
QDesignerFormEditorInterface *core = formWindow()->core();
1183
core->metaDataBase()->add(m_widget);
1186
void AddStackedWidgetPageCommand::redo()
1191
void AddStackedWidgetPageCommand::undo()
1196
// ---- TabOrderCommand ----
1197
TabOrderCommand::TabOrderCommand(QDesignerFormWindowInterface *formWindow)
1198
: QDesignerFormWindowCommand(tr("Change Tab order"), formWindow),
1203
void TabOrderCommand::init(const QList<QWidget*> &newTabOrder)
1205
QDesignerFormEditorInterface *core = formWindow()->core();
1208
m_widgetItem = core->metaDataBase()->item(formWindow());
1209
Q_ASSERT(m_widgetItem);
1210
m_oldTabOrder = m_widgetItem->tabOrder();
1211
m_newTabOrder = newTabOrder;
1214
void TabOrderCommand::redo()
1216
m_widgetItem->setTabOrder(m_newTabOrder);
1219
void TabOrderCommand::undo()
1221
m_widgetItem->setTabOrder(m_oldTabOrder);
1224
// ---- DockWidgetCommand:: ----
1225
DockWidgetCommand::DockWidgetCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
1226
: QDesignerFormWindowCommand(description, formWindow)
1230
DockWidgetCommand::~DockWidgetCommand()
1234
void DockWidgetCommand::init(QDockWidget *dockWidget)
1236
m_dockWidget = dockWidget;
1239
// ---- SetDockWidgetCommand ----
1240
SetDockWidgetCommand::SetDockWidgetCommand(QDesignerFormWindowInterface *formWindow)
1241
: DockWidgetCommand(tr("Set Dock Window Widget"), formWindow)
1245
void SetDockWidgetCommand::init(QDockWidget *dockWidget, QWidget *widget)
1247
DockWidgetCommand::init(dockWidget);
1249
m_oldWidget = dockWidget->widget();
1252
void SetDockWidgetCommand::undo()
1254
m_dockWidget->setWidget(m_oldWidget);
1257
void SetDockWidgetCommand::redo()
1259
formWindow()->unmanageWidget(m_widget);
1260
formWindow()->core()->metaDataBase()->add(m_widget);
1261
m_dockWidget->setWidget(m_widget);
1264
// ---- AddDockWidgetCommand ----
1265
AddDockWidgetCommand::AddDockWidgetCommand(QDesignerFormWindowInterface *formWindow)
1266
: QDesignerFormWindowCommand(tr("Add Dock Window"), formWindow)
1270
void AddDockWidgetCommand::init(QMainWindow *mainWindow, QDockWidget *dockWidget)
1272
m_mainWindow = mainWindow;
1273
m_dockWidget = dockWidget;
1276
void AddDockWidgetCommand::redo()
1278
m_mainWindow->addDockWidget(Qt::LeftDockWidgetArea, m_dockWidget);
1281
void AddDockWidgetCommand::undo()
1283
m_mainWindow->removeDockWidget(m_dockWidget);
1286
// ---- AdjustWidgetSizeCommand ----
1287
AdjustWidgetSizeCommand::AdjustWidgetSizeCommand(QDesignerFormWindowInterface *formWindow)
1288
: QDesignerFormWindowCommand(QString(), formWindow)
1292
void AdjustWidgetSizeCommand::init(QWidget *widget)
1295
setDescription(tr("Adjust Size of '%1'").arg(widget->objectName()));
1298
void AdjustWidgetSizeCommand::redo()
1300
if (formWindow()->mainContainer() == m_widget && formWindow()->parentWidget()) {
1301
formWindow()->parentWidget()->resize(m_widget->sizeHint());
1303
m_widget->adjustSize();
1306
if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
1307
if (propertyEditor->object() == m_widget)
1308
propertyEditor->setPropertyValue(QLatin1String("geometry"), m_widget->geometry(), true);
1312
void AdjustWidgetSizeCommand::undo()
1316
// ---- ChangeLayoutItemGeometry ----
1317
ChangeLayoutItemGeometry::ChangeLayoutItemGeometry(QDesignerFormWindowInterface *formWindow)
1318
: QDesignerFormWindowCommand(tr("Change Layout Item Geometry"), formWindow)
1322
void ChangeLayoutItemGeometry::init(QWidget *widget, int row, int column, int rowspan, int colspan)
1325
Q_ASSERT(m_widget->parentWidget() != 0);
1327
QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
1328
Q_ASSERT(layout != 0);
1330
QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
1331
Q_ASSERT(grid != 0);
1333
int itemIndex = grid->indexOf(m_widget);
1334
Q_ASSERT(itemIndex != -1);
1336
int current_row, current_column, current_rowspan, current_colspan;
1337
grid->getItemPosition(itemIndex, ¤t_row, ¤t_column, ¤t_rowspan, ¤t_colspan);
1339
m_oldInfo.setRect(current_column, current_row, current_colspan, current_rowspan);
1340
m_newInfo.setRect(column, row, colspan, rowspan);
1343
void ChangeLayoutItemGeometry::changeItemPosition(const QRect &g)
1345
QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
1346
Q_ASSERT(layout != 0);
1348
QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
1349
Q_ASSERT(grid != 0);
1351
int itemIndex = grid->indexOf(m_widget);
1352
Q_ASSERT(itemIndex != -1);
1354
QLayoutItem *item = grid->takeAt(itemIndex);
1357
add_to_grid_layout(grid, m_widget, g.top(), g.left(), g.height(), g.width());
1362
QLayoutSupport::createEmptyCells(grid);
1364
formWindow()->clearSelection(false);
1365
formWindow()->selectWidget(m_widget, true);
1368
void ChangeLayoutItemGeometry::redo()
1370
changeItemPosition(m_newInfo);
1373
void ChangeLayoutItemGeometry::undo()
1375
changeItemPosition(m_oldInfo);
1378
// ---- InsertRowCommand ----
1379
InsertRowCommand::InsertRowCommand(QDesignerFormWindowInterface *formWindow)
1380
: QDesignerFormWindowCommand(tr("Insert Row"), formWindow)
1384
void InsertRowCommand::init(QWidget *widget, int row)
1390
void InsertRowCommand::redo()
1394
void InsertRowCommand::undo()