~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/qdesigner_command.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtDesigner/QtDesigner>
 
30
 
 
31
#include "qdesigner_command_p.h"
 
32
#include "layout_p.h"
 
33
#include "qlayout_widget_p.h"
 
34
#include "qdesigner_widget_p.h"
 
35
#include "qdesigner_promotedwidget_p.h"
 
36
 
 
37
#include <QtCore/qdebug.h>
 
38
 
 
39
#include <QtGui/QToolBox>
 
40
#include <QtGui/QStackedWidget>
 
41
#include <QtGui/QTabWidget>
 
42
#include <QtGui/QSplitter>
 
43
#include <QtGui/QDockWidget>
 
44
#include <QtGui/QMainWindow>
 
45
 
 
46
// ---- QDesignerFormEditorCommand ----
 
47
QDesignerFormEditorCommand::QDesignerFormEditorCommand(const QString &description, QDesignerFormEditorInterface *core)
 
48
    : QtCommand(description),
 
49
      m_core(core)
 
50
{
 
51
}
 
52
 
 
53
QDesignerFormEditorInterface *QDesignerFormEditorCommand::core() const
 
54
{
 
55
    return m_core;
 
56
}
 
57
 
 
58
// ---- QDesignerFormWindowManagerCommand ----
 
59
QDesignerFormWindowManagerCommand::QDesignerFormWindowManagerCommand(const QString &description, QDesignerFormWindowManagerInterface *formWindowManager)
 
60
    : QtCommand(description),
 
61
      m_formWindowManager(formWindowManager)
 
62
{
 
63
}
 
64
 
 
65
QDesignerFormWindowManagerInterface *QDesignerFormWindowManagerCommand::formWindowManager() const
 
66
{
 
67
    return m_formWindowManager;
 
68
}
 
69
 
 
70
// ---- QDesignerFormWindowCommand ----
 
71
QDesignerFormWindowCommand::QDesignerFormWindowCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
 
72
    : QtCommand(description),
 
73
      m_formWindow(formWindow)
 
74
{
 
75
}
 
76
 
 
77
QDesignerFormWindowInterface *QDesignerFormWindowCommand::formWindow() const
 
78
{
 
79
    return m_formWindow;
 
80
}
 
81
 
 
82
bool QDesignerFormWindowCommand::hasLayout(QWidget *widget) const
 
83
{
 
84
    QDesignerFormEditorInterface *core = formWindow()->core();
 
85
    if (widget && LayoutInfo::layoutType(core, widget) != LayoutInfo::NoLayout) {
 
86
        QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget);
 
87
        return item != 0;
 
88
    }
 
89
 
 
90
    return false;
 
91
}
 
92
 
 
93
void QDesignerFormWindowCommand::checkObjectName(QWidget *widget)
 
94
{
 
95
    if (widget->objectName().isEmpty())
 
96
        qWarning("invalid object name");
 
97
 
 
98
    QDesignerFormEditorInterface *core = formWindow()->core();
 
99
    if (QDesignerMetaDataBaseItemInterface *item = core->metaDataBase()->item(widget)) {
 
100
        item->setName(widget->objectName());
 
101
    }
 
102
}
 
103
 
 
104
void QDesignerFormWindowCommand::updateBuddies(const QString &old_name,
 
105
                                                const QString &new_name)
 
106
{
 
107
    QDesignerFormEditorInterface *core = formWindow()->core();
 
108
 
 
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)
 
115
            continue;
 
116
        int idx = propertySheet->indexOf(QLatin1String("buddy"));
 
117
        if (idx == -1)
 
118
            continue;
 
119
        if (propertySheet->property(idx).toString() == old_name)
 
120
            propertySheet->setProperty(idx, new_name);
 
121
    }
 
122
}
 
123
 
 
124
void QDesignerFormWindowCommand::checkSelection(QWidget *widget)
 
125
{
 
126
    Q_UNUSED(widget);
 
127
 
 
128
#if 0 // ### port me
 
129
    QDesignerFormEditorInterface *core = formWindow()->core();
 
130
 
 
131
    formWindow()->updateSelection(widget);
 
132
 
 
133
    if (LayoutInfo::layoutType(core, widget) != LayoutInfo::NoLayout)
 
134
        formWindow()->updateChildSelections(widget);
 
135
#endif
 
136
}
 
137
 
 
138
void QDesignerFormWindowCommand::checkParent(QWidget *widget, QWidget *parentWidget)
 
139
{
 
140
    Q_ASSERT(widget);
 
141
 
 
142
    if (widget->parentWidget() != parentWidget)
 
143
        widget->setParent(parentWidget);
 
144
}
 
145
 
 
146
// ---- SetPropertyCommand ----
 
147
SetPropertyCommand::SetPropertyCommand(QDesignerFormWindowInterface *formWindow)
 
148
    : QDesignerFormWindowCommand(QString(), formWindow),
 
149
      m_index(-1),
 
150
      m_propertySheet(0),
 
151
      m_changed(false)
 
152
{
 
153
    setCanMerge(true);
 
154
}
 
155
 
 
156
QWidget *SetPropertyCommand::widget() const
 
157
{
 
158
    return m_widget;
 
159
}
 
160
 
 
161
QWidget *SetPropertyCommand::parentWidget() const
 
162
{
 
163
    return m_parentWidget;
 
164
}
 
165
 
 
166
void SetPropertyCommand::init(QWidget *widget, const QString &propertyName, const QVariant &newValue)
 
167
{
 
168
    Q_ASSERT(widget);
 
169
 
 
170
    m_widget = widget;
 
171
    m_parentWidget = widget->parentWidget();
 
172
    m_propertyName = propertyName;
 
173
    m_newValue = newValue;
 
174
 
 
175
    QDesignerFormEditorInterface *core = formWindow()->core();
 
176
    m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget);
 
177
    Q_ASSERT(m_propertySheet);
 
178
 
 
179
    m_index = m_propertySheet->indexOf(m_propertyName);
 
180
    Q_ASSERT(m_index != -1);
 
181
 
 
182
    m_changed = m_propertySheet->isChanged(m_index);
 
183
    m_oldValue = m_propertySheet->property(m_index);
 
184
 
 
185
    setDescription(tr("changed '%1' of '%2'").arg(m_propertyName).arg(m_widget->objectName()));
 
186
}
 
187
 
 
188
void SetPropertyCommand::redo()
 
189
{
 
190
    Q_ASSERT(m_propertySheet);
 
191
    Q_ASSERT(m_index != -1);
 
192
 
 
193
    m_propertySheet->setProperty(m_index, m_newValue);
 
194
    m_changed = m_propertySheet->isChanged(m_index);
 
195
    m_propertySheet->setChanged(m_index, true);
 
196
 
 
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());
 
203
    }
 
204
 
 
205
    if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
 
206
        if (propertyEditor->object() == widget())
 
207
            propertyEditor->setPropertyValue(propertyName(), m_newValue, true);
 
208
    }
 
209
}
 
210
 
 
211
void SetPropertyCommand::undo()
 
212
{
 
213
    Q_ASSERT(m_propertySheet);
 
214
    Q_ASSERT(m_index != -1);
 
215
 
 
216
    m_propertySheet->setProperty(m_index, m_oldValue);
 
217
    m_propertySheet->setChanged(m_index, m_changed);
 
218
 
 
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());
 
225
    }
 
226
 
 
227
    if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
 
228
        if (propertyEditor->object() == widget())
 
229
            propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed);
 
230
    }
 
231
}
 
232
 
 
233
bool SetPropertyCommand::mergeMeWith(QtCommand *other)
 
234
{
 
235
    if (SetPropertyCommand *cmd = qobject_cast<SetPropertyCommand*>(other)) {
 
236
        if (cmd->propertyName() == propertyName() && cmd->widget() == widget()) {
 
237
            m_newValue = cmd->newValue();
 
238
            return true;
 
239
        }
 
240
    }
 
241
 
 
242
    return false;
 
243
}
 
244
 
 
245
// ---- ResetPropertyCommand ----
 
246
ResetPropertyCommand::ResetPropertyCommand(QDesignerFormWindowInterface *formWindow)
 
247
    : QDesignerFormWindowCommand(QString(), formWindow),
 
248
      m_index(-1),
 
249
      m_propertySheet(0),
 
250
      m_changed(false)
 
251
{
 
252
    setCanMerge(true);
 
253
}
 
254
 
 
255
QWidget *ResetPropertyCommand::widget() const
 
256
{
 
257
    return m_widget;
 
258
}
 
259
 
 
260
QWidget *ResetPropertyCommand::parentWidget() const
 
261
{
 
262
    return m_parentWidget;
 
263
}
 
264
 
 
265
void ResetPropertyCommand::init(QWidget *widget, const QString &propertyName)
 
266
{
 
267
    Q_ASSERT(widget);
 
268
 
 
269
    m_widget = widget;
 
270
    m_parentWidget = widget->parentWidget();
 
271
    m_propertyName = propertyName;
 
272
 
 
273
    QDesignerFormEditorInterface *core = formWindow()->core();
 
274
    m_propertySheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget);
 
275
    Q_ASSERT(m_propertySheet);
 
276
 
 
277
    m_index = m_propertySheet->indexOf(m_propertyName);
 
278
    Q_ASSERT(m_index != -1);
 
279
 
 
280
    m_changed = m_propertySheet->isChanged(m_index);
 
281
    m_oldValue = m_propertySheet->property(m_index);
 
282
 
 
283
    setDescription(tr("reset '%1' of '%2'").arg(m_propertyName).arg(m_widget->objectName()));
 
284
}
 
285
 
 
286
void ResetPropertyCommand::redo()
 
287
{
 
288
    Q_ASSERT(m_propertySheet);
 
289
    Q_ASSERT(m_index != -1);
 
290
 
 
291
    QObject *obj = m_widget;
 
292
    if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(obj))
 
293
        obj = promoted->child();
 
294
 
 
295
    QVariant new_value;
 
296
 
 
297
    if (m_propertySheet->reset(m_index)) {
 
298
        new_value = m_propertySheet->property(m_index);
 
299
    } else {
 
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
 
303
        } else {
 
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);
 
309
            else
 
310
                new_value = m_oldValue; // Again, we just don't know
 
311
        }
 
312
 
 
313
        m_propertySheet->setProperty(m_index, new_value);
 
314
    }
 
315
 
 
316
    m_propertySheet->setChanged(m_index, false);
 
317
 
 
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);
 
323
    }
 
324
 
 
325
    if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
 
326
        if (propertyEditor->object() == widget())
 
327
            propertyEditor->setPropertyValue(propertyName(), new_value, false);
 
328
    }
 
329
}
 
330
 
 
331
void ResetPropertyCommand::undo()
 
332
{
 
333
    Q_ASSERT(m_propertySheet);
 
334
    Q_ASSERT(m_index != -1);
 
335
 
 
336
    m_propertySheet->setProperty(m_index, m_oldValue);
 
337
    m_propertySheet->setChanged(m_index, m_changed);
 
338
 
 
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);
 
344
    }
 
345
 
 
346
    if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
 
347
        if (propertyEditor->object() == widget())
 
348
            propertyEditor->setPropertyValue(propertyName(), m_oldValue, m_changed);
 
349
    }
 
350
}
 
351
 
 
352
// ---- InsertWidgetCommand ----
 
353
InsertWidgetCommand::InsertWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
354
    : QDesignerFormWindowCommand(QString(), formWindow)
 
355
{
 
356
}
 
357
 
 
358
void InsertWidgetCommand::init(QWidget *widget)
 
359
{
 
360
    m_widget = widget;
 
361
 
 
362
    setDescription(tr("Insert '%1'").arg(widget->objectName()));
 
363
 
 
364
    QWidget *parentWidget = m_widget->parentWidget();
 
365
    QDesignerFormEditorInterface *core = formWindow()->core();
 
366
    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
 
367
 
 
368
    m_insertMode = deco ? deco->currentInsertMode() : QDesignerLayoutDecorationExtension::InsertWidgetMode;
 
369
    m_cell = deco ? deco->currentCell() : qMakePair(0, 0);
 
370
}
 
371
 
 
372
void InsertWidgetCommand::redo()
 
373
{
 
374
    checkObjectName(m_widget);
 
375
 
 
376
    QWidget *parentWidget = m_widget->parentWidget();
 
377
 
 
378
    QDesignerFormEditorInterface *core = formWindow()->core();
 
379
    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
 
380
 
 
381
    if (deco != 0) {
 
382
        if (LayoutInfo::layoutType(core, parentWidget) == LayoutInfo::Grid) {
 
383
            switch (m_insertMode) {
 
384
                case QDesignerLayoutDecorationExtension::InsertRowMode: {
 
385
                    deco->insertRow(m_cell.first);
 
386
                } break;
 
387
 
 
388
                case QDesignerLayoutDecorationExtension::InsertColumnMode: {
 
389
                    deco->insertColumn(m_cell.second);
 
390
                } break;
 
391
 
 
392
                default: break;
 
393
            } // end switch
 
394
        }
 
395
        deco->insertWidget(m_widget, m_cell);
 
396
    }
 
397
 
 
398
    formWindow()->manageWidget(m_widget);
 
399
    m_widget->show();
 
400
}
 
401
 
 
402
void InsertWidgetCommand::undo()
 
403
{
 
404
    QWidget *parentWidget = m_widget->parentWidget();
 
405
 
 
406
    QDesignerFormEditorInterface *core = formWindow()->core();
 
407
    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parentWidget);
 
408
 
 
409
    if (deco) {
 
410
        deco->removeWidget(m_widget);
 
411
        deco->simplify();
 
412
    }
 
413
 
 
414
    formWindow()->unmanageWidget(m_widget);
 
415
    m_widget->hide();
 
416
}
 
417
 
 
418
// ---- RaiseWidgetCommand ----
 
419
RaiseWidgetCommand::RaiseWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
420
    : QDesignerFormWindowCommand(QString(), formWindow)
 
421
{
 
422
}
 
423
 
 
424
void RaiseWidgetCommand::init(QWidget *widget)
 
425
{
 
426
    m_widget = widget;
 
427
    setDescription(tr("Raise '%1'").arg(widget->objectName()));
 
428
}
 
429
 
 
430
void RaiseWidgetCommand::redo()
 
431
{
 
432
    m_widget->raise();
 
433
}
 
434
 
 
435
void RaiseWidgetCommand::undo()
 
436
{
 
437
}
 
438
 
 
439
// ---- LowerWidgetCommand ----
 
440
LowerWidgetCommand::LowerWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
441
    : QDesignerFormWindowCommand(QString(), formWindow)
 
442
{
 
443
}
 
444
 
 
445
void LowerWidgetCommand::init(QWidget *widget)
 
446
{
 
447
    m_widget = widget;
 
448
    setDescription(tr("Lower '%1'").arg(widget->objectName()));
 
449
}
 
450
 
 
451
void LowerWidgetCommand::redo()
 
452
{
 
453
    m_widget->raise();
 
454
}
 
455
 
 
456
void LowerWidgetCommand::undo()
 
457
{
 
458
}
 
459
 
 
460
// ---- DeleteWidgetCommand ----
 
461
DeleteWidgetCommand::DeleteWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
462
    : QDesignerFormWindowCommand(QString(), formWindow)
 
463
{
 
464
}
 
465
 
 
466
void DeleteWidgetCommand::init(QWidget *widget)
 
467
{
 
468
    m_widget = widget;
 
469
    m_parentWidget = widget->parentWidget();
 
470
    m_geometry = widget->geometry();
 
471
 
 
472
    m_layoutType = LayoutInfo::NoLayout;
 
473
    m_index = -1;
 
474
    if (hasLayout(m_parentWidget)) {
 
475
        m_layoutType = LayoutInfo::layoutType(formWindow()->core(), m_parentWidget);
 
476
 
 
477
        switch (m_layoutType) {
 
478
            case LayoutInfo::VBox:
 
479
                m_index = static_cast<QVBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);
 
480
                break;
 
481
            case LayoutInfo::HBox:
 
482
                m_index = static_cast<QHBoxLayout*>(m_parentWidget->layout())->indexOf(m_widget);
 
483
                break;
 
484
            case LayoutInfo::Grid: {
 
485
                m_index = 0;
 
486
                while (QLayoutItem *item = m_parentWidget->layout()->itemAt(m_index)) {
 
487
                    if (item->widget() == m_widget)
 
488
                        break;
 
489
                    ++m_index;
 
490
                }
 
491
 
 
492
                static_cast<QGridLayout*>(m_parentWidget->layout())->getItemPosition(m_index, &m_row, &m_col, &m_rowspan, &m_colspan);
 
493
            } break;
 
494
 
 
495
            default:
 
496
                break;
 
497
        } // end switch
 
498
    }
 
499
 
 
500
    m_formItem = formWindow()->core()->metaDataBase()->item(formWindow());
 
501
    m_tabOrderIndex = m_formItem->tabOrder().indexOf(widget);
 
502
 
 
503
    setDescription(tr("Delete '%1'").arg(widget->objectName()));
 
504
}
 
505
 
 
506
void DeleteWidgetCommand::redo()
 
507
{
 
508
    QDesignerFormEditorInterface *core = formWindow()->core();
 
509
    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), m_parentWidget);
 
510
 
 
511
    if (deco)
 
512
        deco->removeWidget(m_widget);
 
513
 
 
514
    formWindow()->unmanageWidget(m_widget);
 
515
    m_widget->setParent(formWindow());
 
516
    m_widget->hide();
 
517
 
 
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);
 
522
    }
 
523
 
 
524
    formWindow()->emitSelectionChanged();
 
525
}
 
526
 
 
527
void DeleteWidgetCommand::undo()
 
528
{
 
529
    m_widget->setParent(m_parentWidget);
 
530
    m_widget->setGeometry(m_geometry);
 
531
    formWindow()->manageWidget(m_widget);
 
532
 
 
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);
 
538
        } break;
 
539
 
 
540
        case LayoutInfo::HBox: {
 
541
            QHBoxLayout *hbox = static_cast<QHBoxLayout*>(m_parentWidget->layout());
 
542
            insert_into_box_layout(hbox, m_index, m_widget);
 
543
        } break;
 
544
 
 
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);
 
548
        } break;
 
549
 
 
550
        default:
 
551
            break;
 
552
    } // end switch
 
553
 
 
554
    m_widget->show();
 
555
 
 
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);
 
560
    }
 
561
 
 
562
    formWindow()->emitSelectionChanged();
 
563
}
 
564
 
 
565
// ---- ReparentWidgetCommand ----
 
566
ReparentWidgetCommand::ReparentWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
567
    : QDesignerFormWindowCommand(QString(), formWindow)
 
568
{
 
569
}
 
570
 
 
571
void ReparentWidgetCommand::init(QWidget *widget, QWidget *parentWidget)
 
572
{
 
573
    Q_ASSERT(widget);
 
574
 
 
575
    m_widget = widget;
 
576
    m_oldParentWidget = widget->parentWidget();
 
577
    m_newParentWidget = parentWidget;
 
578
 
 
579
    m_oldPos = m_widget->pos();
 
580
    m_newPos = m_newParentWidget->mapFromGlobal(m_oldParentWidget->mapToGlobal(m_oldPos));
 
581
 
 
582
    setDescription(tr("Reparent '%1'").arg(widget->objectName()));
 
583
}
 
584
 
 
585
void ReparentWidgetCommand::redo()
 
586
{
 
587
    m_widget->setParent(m_newParentWidget);
 
588
    m_widget->move(m_newPos);
 
589
 
 
590
    m_widget->show();
 
591
}
 
592
 
 
593
void ReparentWidgetCommand::undo()
 
594
{
 
595
    m_widget->setParent(m_oldParentWidget);
 
596
    m_widget->move(m_oldPos);
 
597
 
 
598
    m_widget->show();
 
599
}
 
600
 
 
601
// ---- PromoteToCustomWidgetCommand ----
 
602
 
 
603
static void replace_widget_item(QDesignerFormWindowInterface *fw, QWidget *wgt, QWidget *promoted)
 
604
{
 
605
    QDesignerFormEditorInterface *core = fw->core();
 
606
    QWidget *parent = wgt->parentWidget();
 
607
 
 
608
    QRect info;
 
609
    if (QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) {
 
610
        QLayout *layout = LayoutInfo::managedLayout(core, parent);
 
611
        Q_ASSERT(layout != 0);
 
612
 
 
613
        int old_index = layout->indexOf(wgt);
 
614
        Q_ASSERT(old_index != -1);
 
615
 
 
616
        info = deco->itemInfo(old_index);
 
617
 
 
618
        QLayoutItem *item = layout->takeAt(old_index);
 
619
        delete item;
 
620
        layout->activate();
 
621
    }
 
622
 
 
623
    if (qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), parent)) {
 
624
        QLayout *layout = LayoutInfo::managedLayout(core, parent);
 
625
        Q_ASSERT(layout != 0);
 
626
 
 
627
        // ### check if `info' is valid!
 
628
 
 
629
        switch (LayoutInfo::layoutType(core, layout)) {
 
630
            default: Q_ASSERT(0); break;
 
631
 
 
632
            case LayoutInfo::VBox:
 
633
                insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.top(), promoted);
 
634
                break;
 
635
 
 
636
            case LayoutInfo::HBox:
 
637
                insert_into_box_layout(static_cast<QBoxLayout*>(layout), info.left(), promoted);
 
638
                break;
 
639
 
 
640
            case LayoutInfo::Grid:
 
641
                add_to_grid_layout(static_cast<QGridLayout*>(layout), promoted, info.top(), info.left(), info.height(), info.width());
 
642
                break;
 
643
        }
 
644
    }
 
645
}
 
646
 
 
647
PromoteToCustomWidgetCommand::PromoteToCustomWidgetCommand
 
648
                                (QDesignerFormWindowInterface *formWindow)
 
649
    : QDesignerFormWindowCommand(tr("Promote to custom widget"), formWindow)
 
650
{
 
651
    m_widget = 0;
 
652
    m_promoted = 0;
 
653
}
 
654
 
 
655
void PromoteToCustomWidgetCommand::init(QDesignerWidgetDataBaseItemInterface *item,
 
656
                                        QWidget *widget)
 
657
{
 
658
    m_widget = widget;
 
659
    m_promoted = new QDesignerPromotedWidget(item, widget->parentWidget());
 
660
}
 
661
 
 
662
void PromoteToCustomWidgetCommand::redo()
 
663
{
 
664
    m_promoted->setObjectName(QLatin1String("__qt__promoted_") + m_widget->objectName());
 
665
    m_promoted->setGeometry(m_widget->geometry());
 
666
 
 
667
    replace_widget_item(formWindow(), m_widget, m_promoted);
 
668
 
 
669
    m_promoted->setChildWidget(m_widget);
 
670
    formWindow()->manageWidget(m_promoted);
 
671
 
 
672
    formWindow()->clearSelection();
 
673
    formWindow()->selectWidget(m_promoted);
 
674
    m_promoted->show();
 
675
}
 
676
 
 
677
void PromoteToCustomWidgetCommand::undo()
 
678
{
 
679
    m_promoted->setChildWidget(0);
 
680
    m_widget->setParent(m_promoted->parentWidget());
 
681
    m_widget->setGeometry(m_promoted->geometry());
 
682
 
 
683
    replace_widget_item(formWindow(), m_promoted, m_widget);
 
684
 
 
685
    formWindow()->manageWidget(m_widget);
 
686
    formWindow()->unmanageWidget(m_promoted);
 
687
 
 
688
    m_promoted->hide();
 
689
    m_widget->show();
 
690
 
 
691
    formWindow()->clearSelection();
 
692
    formWindow()->selectWidget(m_promoted);
 
693
}
 
694
 
 
695
// ---- DemoteFromCustomWidgetCommand ----
 
696
 
 
697
DemoteFromCustomWidgetCommand::DemoteFromCustomWidgetCommand
 
698
                                    (QDesignerFormWindowInterface *formWindow)
 
699
    : QDesignerFormWindowCommand(tr("Demote from custom widget"), formWindow)
 
700
{
 
701
    m_promote_cmd = new PromoteToCustomWidgetCommand(formWindow);
 
702
}
 
703
 
 
704
void DemoteFromCustomWidgetCommand::init(QDesignerPromotedWidget *promoted)
 
705
{
 
706
    m_promote_cmd->m_widget = promoted->child();
 
707
    m_promote_cmd->m_promoted = promoted;
 
708
}
 
709
 
 
710
void DemoteFromCustomWidgetCommand::redo()
 
711
{
 
712
    m_promote_cmd->undo();
 
713
    m_promote_cmd->m_widget->show();
 
714
}
 
715
 
 
716
void DemoteFromCustomWidgetCommand::undo()
 
717
{
 
718
    m_promote_cmd->redo();
 
719
}
 
720
 
 
721
// ---- LayoutCommand ----
 
722
LayoutCommand::LayoutCommand(QDesignerFormWindowInterface *formWindow)
 
723
    : QDesignerFormWindowCommand(QString(), formWindow)
 
724
{
 
725
}
 
726
 
 
727
LayoutCommand::~LayoutCommand()
 
728
{
 
729
    m_layout->deleteLater();
 
730
}
 
731
 
 
732
void LayoutCommand::init(QWidget *parentWidget, const QList<QWidget*> &widgets, LayoutInfo::Type layoutType,
 
733
        QWidget *layoutBase, bool splitter)
 
734
{
 
735
    m_parentWidget = parentWidget;
 
736
    m_widgets = widgets;
 
737
    formWindow()->simplifySelection(&m_widgets);
 
738
    QPoint grid = formWindow()->grid();
 
739
    QSize sz(qMax(5, grid.x()), qMax(5, grid.y()));
 
740
 
 
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"));
 
745
            break;
 
746
 
 
747
        case LayoutInfo::VBox:
 
748
            m_layout = new VerticalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter);
 
749
            setDescription(tr("Lay out vertically"));
 
750
            break;
 
751
 
 
752
        case LayoutInfo::HBox:
 
753
            m_layout = new HorizontalLayout(widgets, m_parentWidget, formWindow(), layoutBase, splitter);
 
754
            setDescription(tr("Lay out horizontally"));
 
755
            break;
 
756
        default:
 
757
            Q_ASSERT(0);
 
758
    }
 
759
 
 
760
    m_layout->setup();
 
761
}
 
762
 
 
763
void LayoutCommand::redo()
 
764
{
 
765
    m_layout->doLayout();
 
766
    checkSelection(m_parentWidget);
 
767
}
 
768
 
 
769
void LayoutCommand::undo()
 
770
{
 
771
    QDesignerFormEditorInterface *core = formWindow()->core();
 
772
 
 
773
    QWidget *lb = m_layout->layoutBaseWidget();
 
774
    QDesignerLayoutDecorationExtension *deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), lb);
 
775
 
 
776
    QWidget *p = m_layout->parentWidget();
 
777
    if (!deco && hasLayout(p)) {
 
778
        deco = qt_extension<QDesignerLayoutDecorationExtension*>(core->extensionManager(), p);
 
779
    }
 
780
 
 
781
    delete deco; // release the extension
 
782
 
 
783
    m_layout->undoLayout();
 
784
 
 
785
    if (!m_layoutBase && lb != 0 && !qobject_cast<QLayoutWidget*>(lb)) {
 
786
        core->metaDataBase()->add(lb);
 
787
        lb->show();
 
788
    }
 
789
 
 
790
    checkSelection(m_parentWidget);
 
791
}
 
792
 
 
793
// ---- BreakLayoutCommand ----
 
794
BreakLayoutCommand::BreakLayoutCommand(QDesignerFormWindowInterface *formWindow)
 
795
    : QDesignerFormWindowCommand(tr("Break layout"), formWindow)
 
796
{
 
797
}
 
798
 
 
799
BreakLayoutCommand::~BreakLayoutCommand()
 
800
{
 
801
}
 
802
 
 
803
void BreakLayoutCommand::init(const QList<QWidget*> &widgets, QWidget *layoutBase)
 
804
{
 
805
    QDesignerFormEditorInterface *core = formWindow()->core();
 
806
 
 
807
    m_widgets = widgets;
 
808
    m_layoutBase = core->widgetFactory()->containerOfWidget(layoutBase);
 
809
    m_layout = 0;
 
810
 
 
811
    QPoint grid = formWindow()->grid();
 
812
 
 
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())));
 
820
    // ### StackedLayout
 
821
 
 
822
    Q_ASSERT(m_layout != 0);
 
823
 
 
824
    m_layout->sort();
 
825
 
 
826
    m_margin = m_layout->margin();
 
827
    m_spacing = m_layout->spacing();
 
828
}
 
829
 
 
830
void BreakLayoutCommand::redo()
 
831
{
 
832
    if (!m_layout)
 
833
        return;
 
834
 
 
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);
 
841
 
 
842
    delete deco; // release the extension
 
843
 
 
844
    formWindow()->clearSelection(false);
 
845
    m_layout->breakLayout();
 
846
 
 
847
    core->metaDataBase()->add(lb);
 
848
 
 
849
    foreach (QWidget *widget, m_widgets) {
 
850
        widget->resize(widget->size().expandedTo(QSize(16, 16)));
 
851
    }
 
852
}
 
853
 
 
854
void BreakLayoutCommand::undo()
 
855
{
 
856
    if (!m_layout)
 
857
        return;
 
858
 
 
859
    formWindow()->clearSelection(false);
 
860
    m_layout->doLayout();
 
861
 
 
862
    if (m_layoutBase && m_layoutBase->layout()) {
 
863
        m_layoutBase->layout()->setSpacing(m_spacing);
 
864
        m_layoutBase->layout()->setMargin(m_margin);
 
865
    }
 
866
}
 
867
 
 
868
// ---- ToolBoxCommand ----
 
869
ToolBoxCommand::ToolBoxCommand(QDesignerFormWindowInterface *formWindow)
 
870
    : QDesignerFormWindowCommand(QString(), formWindow)
 
871
{
 
872
}
 
873
 
 
874
ToolBoxCommand::~ToolBoxCommand()
 
875
{
 
876
}
 
877
 
 
878
void ToolBoxCommand::init(QToolBox *toolBox)
 
879
{
 
880
    m_toolBox = 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);
 
885
}
 
886
 
 
887
void ToolBoxCommand::removePage()
 
888
{
 
889
    m_toolBox->removeItem(m_index);
 
890
 
 
891
    m_widget->hide();
 
892
    m_widget->setParent(formWindow());
 
893
}
 
894
 
 
895
void ToolBoxCommand::addPage()
 
896
{
 
897
    m_widget->setParent(m_toolBox);
 
898
    m_toolBox->insertItem(m_index, m_widget, m_itemIcon, m_itemText);
 
899
 
 
900
    m_widget->show();
 
901
}
 
902
 
 
903
// ---- DeleteToolBoxPageCommand ----
 
904
DeleteToolBoxPageCommand::DeleteToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
 
905
    : ToolBoxCommand(formWindow)
 
906
{
 
907
}
 
908
 
 
909
DeleteToolBoxPageCommand::~DeleteToolBoxPageCommand()
 
910
{
 
911
}
 
912
 
 
913
void DeleteToolBoxPageCommand::init(QToolBox *toolBox)
 
914
{
 
915
    ToolBoxCommand::init(toolBox);
 
916
    setDescription(tr("Delete Page"));
 
917
}
 
918
 
 
919
void DeleteToolBoxPageCommand::redo()
 
920
{
 
921
    removePage();
 
922
}
 
923
 
 
924
void DeleteToolBoxPageCommand::undo()
 
925
{
 
926
    addPage();
 
927
}
 
928
 
 
929
// ---- AddToolBoxPageCommand ----
 
930
AddToolBoxPageCommand::AddToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
 
931
    : ToolBoxCommand(formWindow)
 
932
{
 
933
}
 
934
 
 
935
AddToolBoxPageCommand::~AddToolBoxPageCommand()
 
936
{
 
937
}
 
938
 
 
939
void AddToolBoxPageCommand::init(QToolBox *toolBox)
 
940
{
 
941
    m_toolBox = toolBox;
 
942
 
 
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);
 
949
 
 
950
    setDescription(tr("Add Page"));
 
951
 
 
952
    QDesignerFormEditorInterface *core = formWindow()->core();
 
953
    core->metaDataBase()->add(m_widget);
 
954
}
 
955
 
 
956
void AddToolBoxPageCommand::redo()
 
957
{
 
958
    addPage();
 
959
}
 
960
 
 
961
void AddToolBoxPageCommand::undo()
 
962
{
 
963
    removePage();
 
964
}
 
965
 
 
966
// ---- TabWidgetCommand ----
 
967
TabWidgetCommand::TabWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
968
    : QDesignerFormWindowCommand(QString(), formWindow)
 
969
{
 
970
}
 
971
 
 
972
TabWidgetCommand::~TabWidgetCommand()
 
973
{
 
974
}
 
975
 
 
976
void TabWidgetCommand::init(QTabWidget *tabWidget)
 
977
{
 
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);
 
983
}
 
984
 
 
985
void TabWidgetCommand::removePage()
 
986
{
 
987
    m_tabWidget->removeTab(m_index);
 
988
 
 
989
    m_widget->hide();
 
990
    m_widget->setParent(formWindow());
 
991
    m_tabWidget->setCurrentIndex(qMin(m_index, m_tabWidget->count()));
 
992
}
 
993
 
 
994
void TabWidgetCommand::addPage()
 
995
{
 
996
    m_widget->setParent(0);
 
997
    m_tabWidget->insertTab(m_index, m_widget, m_itemIcon, m_itemText);
 
998
    m_widget->show();
 
999
    m_tabWidget->setCurrentIndex(m_index);
 
1000
}
 
1001
 
 
1002
// ---- DeleteTabPageCommand ----
 
1003
DeleteTabPageCommand::DeleteTabPageCommand(QDesignerFormWindowInterface *formWindow)
 
1004
    : TabWidgetCommand(formWindow)
 
1005
{
 
1006
}
 
1007
 
 
1008
DeleteTabPageCommand::~DeleteTabPageCommand()
 
1009
{
 
1010
}
 
1011
 
 
1012
void DeleteTabPageCommand::init(QTabWidget *tabWidget)
 
1013
{
 
1014
    TabWidgetCommand::init(tabWidget);
 
1015
    setDescription(tr("Delete Page"));
 
1016
}
 
1017
 
 
1018
void DeleteTabPageCommand::redo()
 
1019
{
 
1020
    removePage();
 
1021
}
 
1022
 
 
1023
void DeleteTabPageCommand::undo()
 
1024
{
 
1025
    addPage();
 
1026
}
 
1027
 
 
1028
// ---- AddTabPageCommand ----
 
1029
AddTabPageCommand::AddTabPageCommand(QDesignerFormWindowInterface *formWindow)
 
1030
    : TabWidgetCommand(formWindow)
 
1031
{
 
1032
}
 
1033
 
 
1034
AddTabPageCommand::~AddTabPageCommand()
 
1035
{
 
1036
}
 
1037
 
 
1038
void AddTabPageCommand::init(QTabWidget *tabWidget)
 
1039
{
 
1040
    m_tabWidget = tabWidget;
 
1041
 
 
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);
 
1048
 
 
1049
    setDescription(tr("Add Page"));
 
1050
 
 
1051
    QDesignerFormEditorInterface *core = formWindow()->core();
 
1052
    core->metaDataBase()->add(m_widget);
 
1053
}
 
1054
 
 
1055
void AddTabPageCommand::redo()
 
1056
{
 
1057
    addPage();
 
1058
}
 
1059
 
 
1060
void AddTabPageCommand::undo()
 
1061
{
 
1062
    removePage();
 
1063
}
 
1064
 
 
1065
// ---- MoveTabPageCommand ----
 
1066
MoveTabPageCommand::MoveTabPageCommand(QDesignerFormWindowInterface *formWindow)
 
1067
    : TabWidgetCommand(formWindow)
 
1068
{
 
1069
}
 
1070
 
 
1071
MoveTabPageCommand::~MoveTabPageCommand()
 
1072
{
 
1073
}
 
1074
 
 
1075
void MoveTabPageCommand::init(QTabWidget *tabWidget, QWidget *page,
 
1076
                      const QIcon &icon, const QString &label,
 
1077
                      int index, int newIndex)
 
1078
{
 
1079
    TabWidgetCommand::init(tabWidget);
 
1080
    setDescription(tr("Move Page"));
 
1081
 
 
1082
    m_page = page;
 
1083
    m_newIndex = newIndex;
 
1084
    m_oldIndex = index;
 
1085
    m_label = label;
 
1086
    m_icon = icon;
 
1087
}
 
1088
 
 
1089
void MoveTabPageCommand::redo()
 
1090
{
 
1091
    m_tabWidget->removeTab(m_oldIndex);
 
1092
    m_tabWidget->insertTab(m_newIndex, m_page, m_icon, m_label);
 
1093
    m_tabWidget->setCurrentIndex(m_newIndex);
 
1094
}
 
1095
 
 
1096
void MoveTabPageCommand::undo()
 
1097
{
 
1098
    m_tabWidget->removeTab(m_newIndex);
 
1099
    m_tabWidget->insertTab(m_oldIndex, m_page, m_icon, m_label);
 
1100
    m_tabWidget->setCurrentIndex(m_oldIndex);
 
1101
}
 
1102
 
 
1103
// ---- StackedWidgetCommand ----
 
1104
StackedWidgetCommand::StackedWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
1105
    : QDesignerFormWindowCommand(QString(), formWindow)
 
1106
{
 
1107
}
 
1108
 
 
1109
StackedWidgetCommand::~StackedWidgetCommand()
 
1110
{
 
1111
}
 
1112
 
 
1113
void StackedWidgetCommand::init(QStackedWidget *stackedWidget)
 
1114
{
 
1115
    m_stackedWidget = stackedWidget;
 
1116
    m_index = m_stackedWidget->currentIndex();
 
1117
    m_widget = m_stackedWidget->widget(m_index);
 
1118
}
 
1119
 
 
1120
void StackedWidgetCommand::removePage()
 
1121
{
 
1122
    m_stackedWidget->removeWidget(m_stackedWidget->widget(m_index));
 
1123
 
 
1124
    m_widget->hide();
 
1125
    m_widget->setParent(formWindow());
 
1126
}
 
1127
 
 
1128
void StackedWidgetCommand::addPage()
 
1129
{
 
1130
    m_stackedWidget->insertWidget(m_index, m_widget);
 
1131
 
 
1132
    m_widget->show();
 
1133
}
 
1134
 
 
1135
// ---- DeleteStackedWidgetPageCommand ----
 
1136
DeleteStackedWidgetPageCommand::DeleteStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
 
1137
    : StackedWidgetCommand(formWindow)
 
1138
{
 
1139
}
 
1140
 
 
1141
DeleteStackedWidgetPageCommand::~DeleteStackedWidgetPageCommand()
 
1142
{
 
1143
}
 
1144
 
 
1145
void DeleteStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
 
1146
{
 
1147
    StackedWidgetCommand::init(stackedWidget);
 
1148
    setDescription(tr("Delete Page"));
 
1149
}
 
1150
 
 
1151
void DeleteStackedWidgetPageCommand::redo()
 
1152
{
 
1153
    removePage();
 
1154
}
 
1155
 
 
1156
void DeleteStackedWidgetPageCommand::undo()
 
1157
{
 
1158
    addPage();
 
1159
}
 
1160
 
 
1161
// ---- AddStackedWidgetPageCommand ----
 
1162
AddStackedWidgetPageCommand::AddStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
 
1163
    : StackedWidgetCommand(formWindow)
 
1164
{
 
1165
}
 
1166
 
 
1167
AddStackedWidgetPageCommand::~AddStackedWidgetPageCommand()
 
1168
{
 
1169
}
 
1170
 
 
1171
void AddStackedWidgetPageCommand::init(QStackedWidget *stackedWidget)
 
1172
{
 
1173
    m_stackedWidget = stackedWidget;
 
1174
 
 
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);
 
1179
 
 
1180
    setDescription(tr("Add Page"));
 
1181
 
 
1182
    QDesignerFormEditorInterface *core = formWindow()->core();
 
1183
    core->metaDataBase()->add(m_widget);
 
1184
}
 
1185
 
 
1186
void AddStackedWidgetPageCommand::redo()
 
1187
{
 
1188
    addPage();
 
1189
}
 
1190
 
 
1191
void AddStackedWidgetPageCommand::undo()
 
1192
{
 
1193
    removePage();
 
1194
}
 
1195
 
 
1196
// ---- TabOrderCommand ----
 
1197
TabOrderCommand::TabOrderCommand(QDesignerFormWindowInterface *formWindow)
 
1198
    : QDesignerFormWindowCommand(tr("Change Tab order"), formWindow),
 
1199
      m_widgetItem(0)
 
1200
{
 
1201
}
 
1202
 
 
1203
void TabOrderCommand::init(const QList<QWidget*> &newTabOrder)
 
1204
{
 
1205
    QDesignerFormEditorInterface *core = formWindow()->core();
 
1206
    Q_ASSERT(core);
 
1207
 
 
1208
    m_widgetItem = core->metaDataBase()->item(formWindow());
 
1209
    Q_ASSERT(m_widgetItem);
 
1210
    m_oldTabOrder = m_widgetItem->tabOrder();
 
1211
    m_newTabOrder = newTabOrder;
 
1212
}
 
1213
 
 
1214
void TabOrderCommand::redo()
 
1215
{
 
1216
    m_widgetItem->setTabOrder(m_newTabOrder);
 
1217
}
 
1218
 
 
1219
void TabOrderCommand::undo()
 
1220
{
 
1221
    m_widgetItem->setTabOrder(m_oldTabOrder);
 
1222
}
 
1223
 
 
1224
// ---- DockWidgetCommand:: ----
 
1225
DockWidgetCommand::DockWidgetCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
 
1226
    : QDesignerFormWindowCommand(description, formWindow)
 
1227
{
 
1228
}
 
1229
 
 
1230
DockWidgetCommand::~DockWidgetCommand()
 
1231
{
 
1232
}
 
1233
 
 
1234
void DockWidgetCommand::init(QDockWidget *dockWidget)
 
1235
{
 
1236
    m_dockWidget = dockWidget;
 
1237
}
 
1238
 
 
1239
// ---- SetDockWidgetCommand ----
 
1240
SetDockWidgetCommand::SetDockWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
1241
    : DockWidgetCommand(tr("Set Dock Window Widget"), formWindow)
 
1242
{
 
1243
}
 
1244
 
 
1245
void SetDockWidgetCommand::init(QDockWidget *dockWidget, QWidget *widget)
 
1246
{
 
1247
    DockWidgetCommand::init(dockWidget);
 
1248
    m_widget = widget;
 
1249
    m_oldWidget = dockWidget->widget();
 
1250
}
 
1251
 
 
1252
void SetDockWidgetCommand::undo()
 
1253
{
 
1254
    m_dockWidget->setWidget(m_oldWidget);
 
1255
}
 
1256
 
 
1257
void SetDockWidgetCommand::redo()
 
1258
{
 
1259
    formWindow()->unmanageWidget(m_widget);
 
1260
    formWindow()->core()->metaDataBase()->add(m_widget);
 
1261
    m_dockWidget->setWidget(m_widget);
 
1262
}
 
1263
 
 
1264
// ---- AddDockWidgetCommand ----
 
1265
AddDockWidgetCommand::AddDockWidgetCommand(QDesignerFormWindowInterface *formWindow)
 
1266
    : QDesignerFormWindowCommand(tr("Add Dock Window"), formWindow)
 
1267
{
 
1268
}
 
1269
 
 
1270
void AddDockWidgetCommand::init(QMainWindow *mainWindow, QDockWidget *dockWidget)
 
1271
{
 
1272
    m_mainWindow = mainWindow;
 
1273
    m_dockWidget = dockWidget;
 
1274
}
 
1275
 
 
1276
void AddDockWidgetCommand::redo()
 
1277
{
 
1278
    m_mainWindow->addDockWidget(Qt::LeftDockWidgetArea, m_dockWidget);
 
1279
}
 
1280
 
 
1281
void AddDockWidgetCommand::undo()
 
1282
{
 
1283
    m_mainWindow->removeDockWidget(m_dockWidget);
 
1284
}
 
1285
 
 
1286
// ---- AdjustWidgetSizeCommand ----
 
1287
AdjustWidgetSizeCommand::AdjustWidgetSizeCommand(QDesignerFormWindowInterface *formWindow)
 
1288
    : QDesignerFormWindowCommand(QString(), formWindow)
 
1289
{
 
1290
}
 
1291
 
 
1292
void AdjustWidgetSizeCommand::init(QWidget *widget)
 
1293
{
 
1294
    m_widget = widget;
 
1295
    setDescription(tr("Adjust Size of '%1'").arg(widget->objectName()));
 
1296
}
 
1297
 
 
1298
void AdjustWidgetSizeCommand::redo()
 
1299
{
 
1300
    if (formWindow()->mainContainer() == m_widget && formWindow()->parentWidget()) {
 
1301
        formWindow()->parentWidget()->resize(m_widget->sizeHint());
 
1302
    } else {
 
1303
        m_widget->adjustSize();
 
1304
    }
 
1305
 
 
1306
    if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {
 
1307
        if (propertyEditor->object() == m_widget)
 
1308
            propertyEditor->setPropertyValue(QLatin1String("geometry"), m_widget->geometry(), true);
 
1309
    }
 
1310
}
 
1311
 
 
1312
void AdjustWidgetSizeCommand::undo()
 
1313
{
 
1314
}
 
1315
 
 
1316
// ---- ChangeLayoutItemGeometry ----
 
1317
ChangeLayoutItemGeometry::ChangeLayoutItemGeometry(QDesignerFormWindowInterface *formWindow)
 
1318
    : QDesignerFormWindowCommand(tr("Change Layout Item Geometry"), formWindow)
 
1319
{
 
1320
}
 
1321
 
 
1322
void ChangeLayoutItemGeometry::init(QWidget *widget, int row, int column, int rowspan, int colspan)
 
1323
{
 
1324
    m_widget = widget;
 
1325
    Q_ASSERT(m_widget->parentWidget() != 0);
 
1326
 
 
1327
    QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
 
1328
    Q_ASSERT(layout != 0);
 
1329
 
 
1330
    QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
 
1331
    Q_ASSERT(grid != 0);
 
1332
 
 
1333
    int itemIndex = grid->indexOf(m_widget);
 
1334
    Q_ASSERT(itemIndex != -1);
 
1335
 
 
1336
    int current_row, current_column, current_rowspan, current_colspan;
 
1337
    grid->getItemPosition(itemIndex, &current_row, &current_column, &current_rowspan, &current_colspan);
 
1338
 
 
1339
    m_oldInfo.setRect(current_column, current_row, current_colspan, current_rowspan);
 
1340
    m_newInfo.setRect(column, row, colspan, rowspan);
 
1341
}
 
1342
 
 
1343
void ChangeLayoutItemGeometry::changeItemPosition(const QRect &g)
 
1344
{
 
1345
    QLayout *layout = LayoutInfo::managedLayout(formWindow()->core(), m_widget->parentWidget());
 
1346
    Q_ASSERT(layout != 0);
 
1347
 
 
1348
    QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
 
1349
    Q_ASSERT(grid != 0);
 
1350
 
 
1351
    int itemIndex = grid->indexOf(m_widget);
 
1352
    Q_ASSERT(itemIndex != -1);
 
1353
 
 
1354
    QLayoutItem *item = grid->takeAt(itemIndex);
 
1355
    delete item;
 
1356
 
 
1357
    add_to_grid_layout(grid, m_widget, g.top(), g.left(), g.height(), g.width());
 
1358
 
 
1359
    grid->invalidate();
 
1360
    grid->activate();
 
1361
 
 
1362
    QLayoutSupport::createEmptyCells(grid);
 
1363
 
 
1364
    formWindow()->clearSelection(false);
 
1365
    formWindow()->selectWidget(m_widget, true);
 
1366
}
 
1367
 
 
1368
void ChangeLayoutItemGeometry::redo()
 
1369
{
 
1370
    changeItemPosition(m_newInfo);
 
1371
}
 
1372
 
 
1373
void ChangeLayoutItemGeometry::undo()
 
1374
{
 
1375
    changeItemPosition(m_oldInfo);
 
1376
}
 
1377
 
 
1378
// ---- InsertRowCommand ----
 
1379
InsertRowCommand::InsertRowCommand(QDesignerFormWindowInterface *formWindow)
 
1380
    : QDesignerFormWindowCommand(tr("Insert Row"), formWindow)
 
1381
{
 
1382
}
 
1383
 
 
1384
void InsertRowCommand::init(QWidget *widget, int row)
 
1385
{
 
1386
    m_widget = widget;
 
1387
    m_row = row;
 
1388
}
 
1389
 
 
1390
void InsertRowCommand::redo()
 
1391
{
 
1392
}
 
1393
 
 
1394
void InsertRowCommand::undo()
 
1395
{
 
1396
}