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

« back to all changes in this revision

Viewing changes to tools/designer/src/components/formeditor/formwindowmanager.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 "formwindowmanager.h"
 
30
#include "formwindow_dnditem.h"
 
31
#include "widgetdatabase_p.h"
 
32
#include "iconloader_p.h"
 
33
#include "widgetselection.h"
 
34
#include "qdesigner_resource.h"
 
35
#include "connectionedit_p.h"
 
36
 
 
37
#include <QtDesigner/QtDesigner>
 
38
#include <qdesigner_promotedwidget_p.h>
 
39
#include <qdesigner_command_p.h>
 
40
#include <layoutinfo_p.h>
 
41
 
 
42
#include <QtGui/QAction>
 
43
#include <QtGui/QLayout>
 
44
#include <QtGui/QMouseEvent>
 
45
#include <QtGui/QApplication>
 
46
#include <QtGui/QIcon>
 
47
#include <QtGui/QBitmap>
 
48
#include <QtGui/QPainter>
 
49
#include <QtGui/QSizeGrip>
 
50
#include <QtGui/QAbstractButton>
 
51
#include <QtGui/QToolBox>
 
52
#include <QtGui/QMainWindow>
 
53
#include <QtGui/QMenuBar>
 
54
#include <QtGui/QClipboard>
 
55
#include <QtGui/QWorkspace>
 
56
#include <QtGui/QDesktopWidget>
 
57
 
 
58
#include <QtCore/qdebug.h>
 
59
 
 
60
using namespace qdesigner_internal;
 
61
 
 
62
static QString whatsThisFrom(const QString &str)
 
63
{
 
64
    Q_UNUSED(str); /// ### implement me!
 
65
    return str;
 
66
}
 
67
 
 
68
FormWindowManager::FormWindowManager(QDesignerFormEditorInterface *core, QObject *parent)
 
69
    : QDesignerFormWindowManagerInterface(parent),
 
70
      m_core(core),
 
71
      m_activeFormWindow(0)
 
72
{
 
73
    m_layoutChilds = false;
 
74
 
 
75
    setupActions();
 
76
    qApp->installEventFilter(this);
 
77
 
 
78
    // DnD stuff
 
79
    m_last_widget_under_mouse = 0;
 
80
    m_last_form_under_mouse = 0;
 
81
    m_widget_box_under_mouse = 0;
 
82
}
 
83
 
 
84
FormWindowManager::~FormWindowManager()
 
85
{
 
86
    qDeleteAll(m_formWindows);
 
87
}
 
88
 
 
89
QDesignerFormEditorInterface *FormWindowManager::core() const
 
90
{
 
91
    return m_core;
 
92
}
 
93
 
 
94
QDesignerFormWindowInterface *FormWindowManager::activeFormWindow() const
 
95
{
 
96
    return m_activeFormWindow;
 
97
}
 
98
 
 
99
int FormWindowManager::formWindowCount() const
 
100
{
 
101
    return m_formWindows.size();
 
102
}
 
103
 
 
104
QDesignerFormWindowInterface *FormWindowManager::formWindow(int index) const
 
105
{
 
106
    return m_formWindows.at(index);
 
107
}
 
108
 
 
109
static bool isMouseMoveOrRelease(QEvent *e)
 
110
{
 
111
    return e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseMove;
 
112
}
 
113
 
 
114
bool FormWindowManager::eventFilter(QObject *o, QEvent *e)
 
115
{
 
116
    if (
 
117
#ifndef Q_OS_WIN
 
118
        o == m_core->topLevel() &&
 
119
#endif
 
120
        !m_drag_item_list.isEmpty() && isMouseMoveOrRelease(e)) {
 
121
        // We're dragging
 
122
        QMouseEvent *me = static_cast<QMouseEvent*>(e);
 
123
        me->accept();
 
124
 
 
125
        if (me->type() == QEvent::MouseButtonRelease)
 
126
            endDrag(me->globalPos());
 
127
        else
 
128
            setItemsPos(me->globalPos());
 
129
        return true;
 
130
    }
 
131
 
 
132
    if (!o->isWidgetType())
 
133
        return false;
 
134
 
 
135
    QWidget *widget = static_cast<QWidget*>(o);
 
136
 
 
137
    if (qobject_cast<WidgetHandle*>(widget)) { // ### remove me
 
138
        return false;
 
139
    }
 
140
 
 
141
    FormWindow *fw = FormWindow::findFormWindow(widget);
 
142
    if (fw == 0) {
 
143
        return false;
 
144
    }
 
145
 
 
146
    if (QWidget *managedWidget = findManagedWidget(fw, widget)) {
 
147
       switch (e->type()) {
 
148
        case QEvent::Hide: {
 
149
            if (widget == managedWidget && fw->isWidgetSelected(managedWidget))
 
150
                fw->hideSelection(widget);
 
151
        } break;
 
152
 
 
153
        case QEvent::WindowActivate: {
 
154
            if (fw->isMainContainer(managedWidget)) {
 
155
                core()->formWindowManager()->setActiveFormWindow(fw);
 
156
            }
 
157
        } break;
 
158
 
 
159
        case QEvent::WindowDeactivate: {
 
160
            fw->repaintSelection();
 
161
        } break;
 
162
 
 
163
        default: {
 
164
            if (fw->handleEvent(widget, managedWidget, e)) {
 
165
                return true;
 
166
            }
 
167
        } break;
 
168
 
 
169
        } // end switch
 
170
    }
 
171
 
 
172
    return false;
 
173
}
 
174
 
 
175
void FormWindowManager::addFormWindow(QDesignerFormWindowInterface *w)
 
176
{
 
177
    FormWindow *formWindow = qobject_cast<FormWindow*>(w);
 
178
    if (!formWindow || m_formWindows.contains(formWindow))
 
179
        return;
 
180
 
 
181
    connect(formWindow, SIGNAL(selectionChanged()), this, SLOT(slotUpdateActions()));
 
182
    connect(formWindow->commandHistory(), SIGNAL(commandExecuted()), this, SLOT(slotUpdateActions()));
 
183
    connect(formWindow, SIGNAL(toolChanged(int)), this, SLOT(slotUpdateActions()));
 
184
 
 
185
    m_formWindows.append(formWindow);
 
186
    emit formWindowAdded(formWindow);
 
187
}
 
188
 
 
189
void FormWindowManager::removeFormWindow(QDesignerFormWindowInterface *w)
 
190
{
 
191
    FormWindow *formWindow = qobject_cast<FormWindow*>(w);
 
192
 
 
193
    int idx = m_formWindows.indexOf(formWindow);
 
194
    if (!formWindow || idx == -1)
 
195
        return;
 
196
 
 
197
    formWindow->disconnect(this);
 
198
    m_formWindows.removeAt(idx);
 
199
    emit formWindowRemoved(formWindow);
 
200
 
 
201
    if (formWindow == m_activeFormWindow)
 
202
        setActiveFormWindow(0);
 
203
}
 
204
 
 
205
void FormWindowManager::setActiveFormWindow(QDesignerFormWindowInterface *w)
 
206
{
 
207
    FormWindow *formWindow = qobject_cast<FormWindow*>(w);
 
208
 
 
209
    if (formWindow == m_activeFormWindow)
 
210
        return;
 
211
 
 
212
    FormWindow *old = m_activeFormWindow;
 
213
 
 
214
    m_activeFormWindow = formWindow;
 
215
 
 
216
    slotUpdateActions();
 
217
 
 
218
    if (m_activeFormWindow) {
 
219
        m_activeFormWindow->repaintSelection();
 
220
        if (old)
 
221
            old->repaintSelection();
 
222
    }
 
223
 
 
224
    emit activeFormWindowChanged(m_activeFormWindow);
 
225
 
 
226
    if (m_activeFormWindow) {
 
227
        m_activeFormWindow->emitSelectionChanged();
 
228
        m_activeFormWindow->commandHistory()->setCurrent();
 
229
 
 
230
        QWidget *parent = m_activeFormWindow->parentWidget();
 
231
        QWorkspace *workspace = 0;
 
232
        while (parent != 0) {
 
233
            if ((workspace = qobject_cast<QWorkspace*>(parent)))
 
234
                break;
 
235
            parent = parent->parentWidget();
 
236
        }
 
237
        if (workspace != 0)
 
238
            workspace->setActiveWindow(m_activeFormWindow->parentWidget());
 
239
    }
 
240
}
 
241
 
 
242
QWidget *FormWindowManager::findManagedWidget(FormWindow *fw, QWidget *w)
 
243
{
 
244
    while (w && w != fw) {
 
245
        if (fw->isManaged(w))
 
246
            break;
 
247
        w = w->parentWidget();
 
248
    }
 
249
 
 
250
    QWidget *parent = w->parentWidget();
 
251
    if (parent != 0 && qobject_cast<QDesignerPromotedWidget*>(parent) != 0)
 
252
        w = parent;
 
253
 
 
254
    return w;
 
255
}
 
256
 
 
257
void FormWindowManager::setupActions()
 
258
{
 
259
    m_actionCut = new QAction(createIconSet(QLatin1String("editcut.png")), tr("Cu&t"), this);
 
260
    m_actionCut->setShortcut(Qt::CTRL + Qt::Key_X);
 
261
    m_actionCut->setStatusTip(tr("Cuts the selected widgets and puts them on the clipboard"));
 
262
    m_actionCut->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Cut")));
 
263
    connect(m_actionCut, SIGNAL(triggered()), this, SLOT(slotActionCutActivated()));
 
264
    m_actionCut->setEnabled(false);
 
265
 
 
266
    m_actionCopy = new QAction(createIconSet(QLatin1String("editcopy.png")), tr("&Copy"), this);
 
267
    m_actionCopy->setShortcut(Qt::CTRL + Qt::Key_C);
 
268
    m_actionCopy->setStatusTip(tr("Copies the selected widgets to the clipboard"));
 
269
    m_actionCopy->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Copy")));
 
270
    connect(m_actionCopy, SIGNAL(triggered()), this, SLOT(slotActionCopyActivated()));
 
271
    m_actionCopy->setEnabled(false);
 
272
 
 
273
    m_actionPaste = new QAction(createIconSet(QLatin1String("editpaste.png")), tr("&Paste"), this);
 
274
    m_actionPaste->setShortcut(Qt::CTRL + Qt::Key_V);
 
275
    m_actionPaste->setStatusTip(tr("Pastes the clipboard's contents"));
 
276
    m_actionPaste->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Paste")));
 
277
    connect(m_actionPaste, SIGNAL(triggered()), this, SLOT(slotActionPasteActivated()));
 
278
    m_actionPaste->setEnabled(false);
 
279
 
 
280
    m_actionDelete = new QAction(tr("&Delete"), this);
 
281
    m_actionDelete->setStatusTip(tr("Deletes the selected widgets"));
 
282
    m_actionDelete->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Delete")));
 
283
    connect(m_actionDelete, SIGNAL(triggered()), this, SLOT(slotActionDeleteActivated()));
 
284
    m_actionDelete->setEnabled(false);
 
285
 
 
286
    m_actionSelectAll = new QAction(tr("Select &All"), this);
 
287
    m_actionSelectAll->setShortcut(Qt::CTRL + Qt::Key_A);
 
288
    m_actionSelectAll->setStatusTip(tr("Selects all widgets"));
 
289
    m_actionSelectAll->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Select All")));
 
290
    connect(m_actionSelectAll, SIGNAL(triggered()), this, SLOT(slotActionSelectAllActivated()));
 
291
    m_actionSelectAll->setEnabled(false);
 
292
 
 
293
    m_actionRaise = new QAction(createIconSet(QLatin1String("editraise.png")), tr("Bring to &Front"), this);
 
294
    m_actionRaise->setStatusTip(tr("Raises the selected widgets"));
 
295
    m_actionRaise->setWhatsThis(tr("Raises the selected widgets"));
 
296
    connect(m_actionRaise, SIGNAL(triggered()), this, SLOT(slotActionRaiseActivated()));
 
297
    m_actionRaise->setEnabled(false);
 
298
 
 
299
    m_actionLower = new QAction(createIconSet(QLatin1String("editlower.png")), tr("Send to &Back"), this);
 
300
    m_actionLower->setStatusTip(tr("Lowers the selected widgets"));
 
301
    m_actionLower->setWhatsThis(tr("Lowers the selected widgets"));
 
302
    connect(m_actionLower, SIGNAL(triggered()), this, SLOT(slotActionLowerActivated()));
 
303
    m_actionLower->setEnabled(false);
 
304
 
 
305
 
 
306
    m_actionAdjustSize = new QAction(createIconSet(QLatin1String("adjustsize.png")), tr("Adjust &Size"), this);
 
307
    m_actionAdjustSize->setShortcut(Qt::CTRL + Qt::Key_J);
 
308
    m_actionAdjustSize->setStatusTip(tr("Adjusts the size of the selected widget"));
 
309
    m_actionAdjustSize->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Adjust Size")));
 
310
    connect(m_actionAdjustSize, SIGNAL(triggered()), this, SLOT(slotActionAdjustSizeActivated()));
 
311
    m_actionAdjustSize->setEnabled(false);
 
312
 
 
313
    m_actionHorizontalLayout = new QAction(createIconSet(QLatin1String("edithlayout.png")), tr("Lay Out &Horizontally"), this);
 
314
    m_actionHorizontalLayout->setShortcut(Qt::CTRL + Qt::Key_H);
 
315
    m_actionHorizontalLayout->setStatusTip(tr("Lays out the selected widgets horizontally"));
 
316
    m_actionHorizontalLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Horizontally")));
 
317
    connect(m_actionHorizontalLayout, SIGNAL(triggered()), this, SLOT(slotActionHorizontalLayoutActivated()));
 
318
    m_actionHorizontalLayout->setEnabled(false);
 
319
 
 
320
    m_actionVerticalLayout = new QAction(createIconSet(QLatin1String("editvlayout.png")), tr("Lay Out &Vertically"), this);
 
321
    m_actionVerticalLayout->setShortcut(Qt::CTRL + Qt::Key_L);
 
322
    m_actionVerticalLayout->setStatusTip(tr("Lays out the selected widgets vertically"));
 
323
    m_actionVerticalLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Vertically")));
 
324
    connect(m_actionVerticalLayout, SIGNAL(triggered()), this, SLOT(slotActionVerticalLayoutActivated()));
 
325
    m_actionVerticalLayout->setEnabled(false);
 
326
 
 
327
    m_actionGridLayout = new QAction(createIconSet(QLatin1String("editgrid.png")), tr("Lay Out in a &Grid"), this);
 
328
    m_actionGridLayout->setShortcut(Qt::CTRL + Qt::Key_G);
 
329
    m_actionGridLayout->setStatusTip(tr("Lays out the selected widgets in a grid"));
 
330
    m_actionGridLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out in a Grid")));
 
331
    connect(m_actionGridLayout, SIGNAL(triggered()), this, SLOT(slotActionGridLayoutActivated()));
 
332
    m_actionGridLayout->setEnabled(false);
 
333
 
 
334
    m_actionSplitHorizontal = new QAction(createIconSet(QLatin1String("editvlayoutsplit.png")),
 
335
                                          tr("Lay Out Horizontally in S&plitter"), this);
 
336
    m_actionSplitHorizontal->setStatusTip(tr("Lays out the selected widgets horizontally in a splitter"));
 
337
    m_actionSplitHorizontal->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Horizontally in Splitter")));
 
338
    connect(m_actionSplitHorizontal, SIGNAL(triggered()), this, SLOT(slotActionSplitHorizontalActivated()));
 
339
    m_actionSplitHorizontal->setEnabled(false);
 
340
 
 
341
    m_actionSplitVertical = new QAction(createIconSet(QLatin1String("edithlayoutsplit.png")),
 
342
                                        tr("Lay Out Vertically in Sp&litter"), this);
 
343
    m_actionSplitVertical->setStatusTip(tr("Lays out the selected widgets vertically in a splitter"));
 
344
    m_actionSplitVertical->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Vertically in Splitter")));
 
345
    connect(m_actionSplitVertical, SIGNAL(triggered()), this, SLOT(slotActionSplitVerticalActivated()));
 
346
    m_actionSplitVertical->setEnabled(false);
 
347
 
 
348
    m_actionBreakLayout = new QAction(createIconSet(QLatin1String("editbreaklayout.png")), tr("&Break Layout"), this);
 
349
    m_actionBreakLayout->setShortcut(Qt::CTRL + Qt::Key_B);
 
350
    m_actionBreakLayout->setStatusTip(tr("Breaks the selected layout"));
 
351
    m_actionBreakLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Break Layout")));
 
352
    connect(m_actionBreakLayout, SIGNAL(triggered()), this, SLOT(slotActionBreakLayoutActivated()));
 
353
    m_actionBreakLayout->setEnabled(false);
 
354
 
 
355
    m_actionUndo = new QAction(tr("Undo"), this);
 
356
    m_actionUndo->setShortcut(Qt::CTRL + Qt::Key_Z);
 
357
    m_actionUndo->setEnabled(false);
 
358
    m_actionRedo = new QAction(tr("Redo"), this);
 
359
    m_actionRedo->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Z);
 
360
    m_actionRedo->setEnabled(false);
 
361
}
 
362
 
 
363
void FormWindowManager::slotActionCutActivated()
 
364
{
 
365
    m_activeFormWindow->cut();
 
366
}
 
367
 
 
368
void FormWindowManager::slotActionCopyActivated()
 
369
{
 
370
    m_activeFormWindow->copy();
 
371
    slotUpdateActions();
 
372
}
 
373
 
 
374
void FormWindowManager::slotActionPasteActivated()
 
375
{
 
376
    m_activeFormWindow->paste();
 
377
}
 
378
 
 
379
void FormWindowManager::slotActionDeleteActivated()
 
380
{
 
381
    m_activeFormWindow->deleteWidgets();
 
382
}
 
383
 
 
384
void FormWindowManager::slotActionLowerActivated()
 
385
{
 
386
    m_activeFormWindow->lowerWidgets();
 
387
}
 
388
 
 
389
void FormWindowManager::slotActionRaiseActivated()
 
390
{
 
391
    m_activeFormWindow->lowerWidgets();
 
392
}
 
393
 
 
394
void FormWindowManager::slotActionHorizontalLayoutActivated()
 
395
{
 
396
    if (m_layoutChilds)
 
397
        layoutContainerHorizontal();
 
398
    else
 
399
        m_activeFormWindow->layoutHorizontal();
 
400
}
 
401
 
 
402
void FormWindowManager::slotActionVerticalLayoutActivated()
 
403
{
 
404
    if (m_layoutChilds)
 
405
        layoutContainerVertical();
 
406
    else
 
407
        m_activeFormWindow->layoutVertical();
 
408
}
 
409
 
 
410
void FormWindowManager::slotActionGridLayoutActivated()
 
411
{
 
412
    if (m_layoutChilds)
 
413
        layoutContainerGrid();
 
414
    else
 
415
        m_activeFormWindow->layoutGrid();
 
416
}
 
417
 
 
418
void FormWindowManager::slotActionSplitHorizontalActivated()
 
419
{
 
420
    if (m_layoutChilds)
 
421
        ; // no way to do that
 
422
    else
 
423
        m_activeFormWindow->layoutHorizontalSplit();
 
424
}
 
425
 
 
426
void FormWindowManager::slotActionSplitVerticalActivated()
 
427
{
 
428
    if (m_layoutChilds)
 
429
        ; // no way to do that
 
430
    else
 
431
        m_activeFormWindow->layoutVerticalSplit();
 
432
}
 
433
 
 
434
void FormWindowManager::slotActionBreakLayoutActivated()
 
435
{
 
436
    QList<QWidget*> widgets = m_activeFormWindow->selectedWidgets();
 
437
 
 
438
    if (widgets.isEmpty())
 
439
        widgets.append(m_activeFormWindow->mainContainer());
 
440
 
 
441
    m_activeFormWindow->simplifySelection(&widgets);
 
442
 
 
443
    QList<QWidget*> layoutBaseList;
 
444
 
 
445
    foreach (QWidget *widget, widgets) {
 
446
        QWidget *currentWidget = core()->widgetFactory()->containerOfWidget(widget);
 
447
 
 
448
        while (currentWidget && currentWidget != m_activeFormWindow) {
 
449
            if (QLayout *layout = LayoutInfo::managedLayout(core(), currentWidget)) {
 
450
                if (!layoutBaseList.contains(layout->parentWidget())) {
 
451
                    layoutBaseList.prepend(layout->parentWidget());
 
452
                }
 
453
            }
 
454
            currentWidget = currentWidget->parentWidget();
 
455
        }
 
456
    }
 
457
 
 
458
    if (layoutBaseList.isEmpty()) {
 
459
        // nothing to do
 
460
        return;
 
461
    }
 
462
 
 
463
    m_activeFormWindow->beginCommand(tr("Break Layout"));
 
464
    foreach (QWidget *layoutBase, layoutBaseList) {
 
465
        m_activeFormWindow->breakLayout(layoutBase);
 
466
    }
 
467
    m_activeFormWindow->endCommand();
 
468
}
 
469
 
 
470
void FormWindowManager::slotActionAdjustSizeActivated()
 
471
{
 
472
    Q_ASSERT(m_activeFormWindow != 0);
 
473
 
 
474
    m_activeFormWindow->beginCommand(tr("Adjust Size"));
 
475
 
 
476
    QList<QWidget*> selectedWidgets = m_activeFormWindow->selectedWidgets();
 
477
    m_activeFormWindow->simplifySelection(&selectedWidgets);
 
478
 
 
479
    if (selectedWidgets.isEmpty()) {
 
480
        Q_ASSERT(m_activeFormWindow->mainContainer() != 0);
 
481
        selectedWidgets.append(m_activeFormWindow->mainContainer());
 
482
    }
 
483
 
 
484
    foreach (QWidget *widget, selectedWidgets) {
 
485
        bool unlaidout = LayoutInfo::layoutType(core(), widget->parentWidget()) == LayoutInfo::NoLayout;
 
486
        bool isMainContainer = m_activeFormWindow->isMainContainer(widget);
 
487
 
 
488
        if (unlaidout || isMainContainer) {
 
489
            AdjustWidgetSizeCommand *cmd = new AdjustWidgetSizeCommand(m_activeFormWindow);
 
490
            cmd->init(widget);
 
491
            m_activeFormWindow->commandHistory()->push(cmd);
 
492
        }
 
493
    }
 
494
 
 
495
    m_activeFormWindow->endCommand();
 
496
}
 
497
 
 
498
void FormWindowManager::slotActionSelectAllActivated()
 
499
{
 
500
    m_activeFormWindow->selectAll();
 
501
}
 
502
 
 
503
void FormWindowManager::slotUpdateActions()
 
504
{
 
505
    m_layoutChilds = false;
 
506
 
 
507
    int selectedWidgetCount = 0;
 
508
    int laidoutWidgetCount = 0;
 
509
    int unlaidoutWidgetCount = 0;
 
510
    bool pasteAvailable = false;
 
511
    bool layoutAvailable = false;
 
512
    bool breakAvailable = false;
 
513
    bool layoutContainer = false;
 
514
 
 
515
    if (m_activeFormWindow != 0 && m_activeFormWindow->currentTool() == 0) {
 
516
        QList<QWidget*> simplifiedSelection = m_activeFormWindow->selectedWidgets();
 
517
        selectedWidgetCount = simplifiedSelection.count();
 
518
        pasteAvailable = qApp->clipboard()->mimeData() && qApp->clipboard()->mimeData()->hasText();
 
519
 
 
520
        m_activeFormWindow->simplifySelection(&simplifiedSelection);
 
521
        if (simplifiedSelection.isEmpty())
 
522
            simplifiedSelection.append(m_activeFormWindow->mainContainer());
 
523
 
 
524
        foreach (QWidget *widget, simplifiedSelection) {
 
525
            if (LayoutInfo::isWidgetLaidout(m_core, widget))
 
526
                ++laidoutWidgetCount;
 
527
            else
 
528
                ++unlaidoutWidgetCount;
 
529
        }
 
530
 
 
531
        if (simplifiedSelection.count() == 1) {
 
532
            m_layoutChilds = false;
 
533
 
 
534
            QWidget *widget = core()->widgetFactory()->containerOfWidget(simplifiedSelection.first());
 
535
            QDesignerWidgetDataBaseInterface *db = m_core->widgetDataBase();
 
536
            if (QDesignerWidgetDataBaseItemInterface *item = db->item(db->indexOfObject(widget))) {
 
537
                QLayout *layout = LayoutInfo::managedLayout(m_core, widget);
 
538
                layoutContainer = (item->isContainer() || m_activeFormWindow->isMainContainer(widget));
 
539
 
 
540
                layoutAvailable = layoutContainer
 
541
                                    && m_activeFormWindow->hasInsertedChildren(widget)
 
542
                                    && layout == 0;
 
543
 
 
544
                m_layoutChilds = layoutAvailable;
 
545
                breakAvailable = layout != 0 || LayoutInfo::isWidgetLaidout(m_core, widget);
 
546
            }
 
547
        } else {
 
548
            layoutAvailable = unlaidoutWidgetCount > 1;
 
549
            breakAvailable = false;
 
550
        }
 
551
    }
 
552
 
 
553
    m_actionCut->setEnabled(selectedWidgetCount > 0);
 
554
    m_actionCopy->setEnabled(selectedWidgetCount > 0);
 
555
    m_actionDelete->setEnabled(selectedWidgetCount > 0);
 
556
    m_actionLower->setEnabled(selectedWidgetCount > 0);
 
557
    m_actionRaise->setEnabled(selectedWidgetCount > 0);
 
558
 
 
559
    m_actionPaste->setEnabled(pasteAvailable);
 
560
 
 
561
    m_actionSelectAll->setEnabled(m_activeFormWindow != 0);
 
562
 
 
563
    m_actionAdjustSize->setEnabled(unlaidoutWidgetCount > 0);
 
564
 
 
565
    m_actionHorizontalLayout->setEnabled(layoutAvailable);
 
566
    m_actionVerticalLayout->setEnabled(layoutAvailable);
 
567
    m_actionSplitHorizontal->setEnabled(layoutAvailable && !layoutContainer);
 
568
    m_actionSplitVertical->setEnabled(layoutAvailable && !layoutContainer);
 
569
    m_actionGridLayout->setEnabled(layoutAvailable);
 
570
 
 
571
    m_actionBreakLayout->setEnabled(breakAvailable);
 
572
}
 
573
 
 
574
void FormWindowManager::layoutContainerHorizontal()
 
575
{
 
576
    QWidget *w = m_activeFormWindow->mainContainer();
 
577
    QList<QWidget*> l(m_activeFormWindow->selectedWidgets());
 
578
    if (l.count() == 1)
 
579
        w = l.first();
 
580
 
 
581
    if (w != 0)
 
582
        m_activeFormWindow->layoutHorizontalContainer(w);
 
583
}
 
584
 
 
585
void FormWindowManager::layoutContainerVertical()
 
586
{
 
587
    QWidget *w = m_activeFormWindow->mainContainer();
 
588
    QList<QWidget*> l(m_activeFormWindow->selectedWidgets());
 
589
    if (l.count() == 1)
 
590
        w = l.first();
 
591
 
 
592
    if (w)
 
593
        m_activeFormWindow->layoutVerticalContainer(w);
 
594
}
 
595
 
 
596
void FormWindowManager::layoutContainerGrid()
 
597
{
 
598
    QWidget *w = m_activeFormWindow->mainContainer();
 
599
    QList<QWidget*> l(m_activeFormWindow->selectedWidgets());
 
600
    if (l.count() == 1)
 
601
        w = l.first();
 
602
 
 
603
    if (w)
 
604
        m_activeFormWindow->layoutGridContainer(w);
 
605
}
 
606
 
 
607
QDesignerFormWindowInterface *FormWindowManager::createFormWindow(QWidget *parentWidget, Qt::WindowFlags flags)
 
608
{
 
609
    FormWindow *formWindow = new FormWindow(qobject_cast<FormEditor*>(core()), parentWidget, flags);
 
610
    addFormWindow(formWindow);
 
611
    return formWindow;
 
612
}
 
613
 
 
614
QAction *FormWindowManager::actionUndo() const
 
615
{
 
616
    return m_actionUndo;
 
617
}
 
618
 
 
619
QAction *FormWindowManager::actionRedo() const
 
620
{
 
621
    return m_actionRedo;
 
622
}
 
623
 
 
624
// DnD stuff
 
625
 
 
626
void FormWindowManager::dragItems(const QList<QDesignerDnDItemInterface*> &item_list)
 
627
{
 
628
    if (!m_drag_item_list.isEmpty()) {
 
629
        qWarning("FormWindowManager::dragItem(): called while already dragging");
 
630
        return;
 
631
    }
 
632
 
 
633
    beginDrag(item_list, QCursor::pos());
 
634
}
 
635
 
 
636
void FormWindowManager::beginDrag(const QList<QDesignerDnDItemInterface*> &item_list, const QPoint &globalPos)
 
637
{
 
638
    Q_ASSERT(m_drag_item_list.isEmpty());
 
639
 
 
640
    m_drag_item_list = item_list;
 
641
 
 
642
    setItemsPos(globalPos);
 
643
 
 
644
    foreach(QDesignerDnDItemInterface *item, m_drag_item_list) {
 
645
        QWidget *deco = item->decoration();
 
646
        QBitmap bitmap(deco->size());
 
647
        QPainter p(&bitmap);
 
648
        p.fillRect(bitmap.rect(), Qt::color1);
 
649
        p.setPen(Qt::color0);
 
650
        p.drawPoint(deco->mapFromGlobal(globalPos));
 
651
        p.end();
 
652
        deco->setMask(bitmap);
 
653
        QPoint pos = deco->pos();
 
654
        QRect ag = qApp->desktop()->availableGeometry(deco);
 
655
        deco->move(qMin(qMax(pos.x(), ag.left()), ag.right()), qMin(qMax(pos.y(), ag.top()), ag.bottom()));
 
656
        deco->show();
 
657
        deco->move(pos);
 
658
        deco->setWindowOpacity(0.8);
 
659
    }
 
660
 
 
661
#ifndef Q_OS_WIN
 
662
    m_core->topLevel()->grabMouse();
 
663
#endif
 
664
}
 
665
 
 
666
static QDesignerWidgetBoxInterface *widgetBoxAt(const QPoint &global_pos)
 
667
{
 
668
    QWidget *w = qApp->widgetAt(global_pos);
 
669
    while (w != 0) {
 
670
        QDesignerWidgetBoxInterface *wb = qobject_cast<QDesignerWidgetBoxInterface*>(w);
 
671
        if (wb != 0)
 
672
            return wb;
 
673
        w = w->parentWidget();
 
674
    }
 
675
    return 0;
 
676
}
 
677
 
 
678
void FormWindowManager::setItemsPos(const QPoint &globalPos)
 
679
{
 
680
    foreach(QDesignerDnDItemInterface *item, m_drag_item_list)
 
681
        item->decoration()->move(globalPos - item->hotSpot());
 
682
 
 
683
    QWidget *widget_under_mouse = qApp->widgetAt(globalPos);
 
684
    int max_try = 3;
 
685
    while (max_try && widget_under_mouse && isDecoration(widget_under_mouse)) {
 
686
        --max_try;
 
687
        widget_under_mouse = qApp->widgetAt(widget_under_mouse->pos() - QPoint(1,1));
 
688
        Q_ASSERT(!qobject_cast<ConnectionEdit*>(widget_under_mouse));
 
689
    }
 
690
 
 
691
    FormWindow *form_under_mouse
 
692
            = qobject_cast<FormWindow*>(QDesignerFormWindowInterface::findFormWindow(widget_under_mouse));
 
693
    if (form_under_mouse != 0 && !form_under_mouse->hasFeature(QDesignerFormWindowInterface::EditFeature))
 
694
        form_under_mouse = 0;
 
695
    if (form_under_mouse != 0) {
 
696
        // widget_under_mouse might be some temporary thing like the dropLine. We need
 
697
        // the actual widget that's part of the edited GUI.
 
698
        widget_under_mouse
 
699
            = form_under_mouse->widgetAt(form_under_mouse->mapFromGlobal(globalPos));
 
700
 
 
701
        Q_ASSERT(!qobject_cast<ConnectionEdit*>(widget_under_mouse));
 
702
    }
 
703
 
 
704
    if (m_last_form_under_mouse != 0 && widget_under_mouse != m_last_widget_under_mouse) {
 
705
        m_last_form_under_mouse->highlightWidget(m_last_widget_under_mouse,
 
706
                                    m_last_widget_under_mouse->mapFromGlobal(globalPos),
 
707
                                    FormWindow::Restore);
 
708
    }
 
709
 
 
710
    FormWindow *source_form = qobject_cast<FormWindow*>(m_drag_item_list.first()->source());
 
711
    if (form_under_mouse != 0
 
712
        && (source_form == 0 || widget_under_mouse != source_form->mainContainer())) {
 
713
 
 
714
        form_under_mouse->highlightWidget(widget_under_mouse,
 
715
                                    widget_under_mouse->mapFromGlobal(globalPos),
 
716
                                    FormWindow::Highlight);
 
717
    }
 
718
 
 
719
    m_last_widget_under_mouse = widget_under_mouse;
 
720
    m_last_form_under_mouse = form_under_mouse;
 
721
    if (m_last_form_under_mouse == 0)
 
722
        m_widget_box_under_mouse = widgetBoxAt(globalPos);
 
723
    else
 
724
        m_widget_box_under_mouse = 0;
 
725
}
 
726
 
 
727
void FormWindowManager::endDrag(const QPoint &pos)
 
728
{
 
729
#ifndef Q_OS_WIN
 
730
    m_core->topLevel()->releaseMouse();
 
731
#endif
 
732
 
 
733
    Q_ASSERT(!m_drag_item_list.isEmpty());
 
734
 
 
735
    foreach (QDesignerDnDItemInterface *item, m_drag_item_list)
 
736
        item->decoration()->hide();
 
737
 
 
738
    // ugly, but you can't qobject_cast from interfaces
 
739
    if (m_last_form_under_mouse != 0 &&
 
740
            m_last_form_under_mouse->hasFeature(QDesignerFormWindowInterface::EditFeature)) {
 
741
        m_last_form_under_mouse->dropWidgets(m_drag_item_list, m_last_widget_under_mouse, pos);
 
742
    } else if (m_widget_box_under_mouse != 0) {
 
743
        m_widget_box_under_mouse->dropWidgets(m_drag_item_list, pos);
 
744
        foreach (QDesignerDnDItemInterface *item, m_drag_item_list) {
 
745
            if (item->type() == QDesignerDnDItemInterface::CopyDrop)
 
746
                continue;
 
747
            FormWindow *source = qobject_cast<FormWindow*>(item->source());
 
748
            if (source == 0)
 
749
                continue;
 
750
            QWidget *widget = item->widget();
 
751
            if (widget == 0)
 
752
                continue;
 
753
            source->deleteWidgets(QList<QWidget*>() << widget);
 
754
        }
 
755
    } else {
 
756
        foreach (QDesignerDnDItemInterface *item, m_drag_item_list) {
 
757
            if (item->widget() != 0)
 
758
                item->widget()->show();
 
759
        }
 
760
    }
 
761
 
 
762
    foreach (QDesignerDnDItemInterface *item, m_drag_item_list)
 
763
        delete item;
 
764
 
 
765
    m_drag_item_list.clear();
 
766
    m_last_widget_under_mouse = 0;
 
767
    m_last_form_under_mouse = 0;
 
768
    m_widget_box_under_mouse = 0;
 
769
}
 
770
 
 
771
bool FormWindowManager::isDecoration(QWidget *widget) const
 
772
{
 
773
    foreach (QDesignerDnDItemInterface *item, m_drag_item_list) {
 
774
        if (item->decoration() == widget)
 
775
            return true;
 
776
    }
 
777
 
 
778
    return false;
 
779
}
 
780