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

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/qdesigner_taskmenu.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 "qdesigner_taskmenu_p.h"
 
30
#include "qdesigner_command_p.h"
 
31
#include "qdesigner_promotedwidget_p.h"
 
32
#include "qtundo_p.h"
 
33
#include "richtexteditor_p.h"
 
34
#include "promotetocustomwidgetdialog_p.h"
 
35
#include "widgetfactory_p.h"
 
36
#include "widgetdatabase_p.h"
 
37
#include "qlayout_widget_p.h"
 
38
#include "spacer_widget_p.h"
 
39
#include "layout_p.h"
 
40
 
 
41
#include <QtDesigner/QtDesigner>
 
42
#include <QtDesigner/QExtensionManager>
 
43
#include <QtDesigner/QDesignerLayoutDecorationExtension>
 
44
 
 
45
#include <QtGui/QAction>
 
46
#include <QtGui/QWidget>
 
47
#include <QtGui/QInputDialog>
 
48
#include <QtGui/QMainWindow>
 
49
#include <QtGui/QDockWidget>
 
50
#include <QtCore/QVariant>
 
51
 
 
52
#include <QtCore/qdebug.h>
 
53
 
 
54
QDesignerTaskMenu::QDesignerTaskMenu(QWidget *widget, QObject *parent)
 
55
    : QObject(parent),
 
56
      m_widget(widget)
 
57
{
 
58
    m_separator = new QAction(this);
 
59
    m_separator->setSeparator(true);
 
60
 
 
61
    m_changeObjectNameAction = new QAction(tr("Change objectName"), this);
 
62
    connect(m_changeObjectNameAction, SIGNAL(triggered()), this, SLOT(changeObjectName()));
 
63
 
 
64
    m_changeStatusTip = new QAction(tr("Change statusTip"), this);
 
65
    connect(m_changeStatusTip, SIGNAL(triggered()), this, SLOT(changeStatusTip()));
 
66
 
 
67
    m_changeToolTip = new QAction(tr("Change toolTip"), this);
 
68
    connect(m_changeToolTip, SIGNAL(triggered()), this, SLOT(changeToolTip()));
 
69
 
 
70
    m_changeWhatsThis = new QAction(tr("Change whatsThis"), this);
 
71
    connect(m_changeWhatsThis, SIGNAL(triggered()), this, SLOT(changeWhatsThis()));
 
72
 
 
73
    m_createDockWidgetAction = new QAction(tr("Create Dock Window"), this);
 
74
    connect(m_createDockWidgetAction, SIGNAL(triggered()), this, SLOT(createDockWidget()));
 
75
 
 
76
    m_promoteToCustomWidgetAction = new QAction(tr("Promote to Custom Widget"), this);
 
77
    connect(m_promoteToCustomWidgetAction, SIGNAL(triggered()), this, SLOT(promoteToCustomWidget()));
 
78
 
 
79
    QString demote_string = tr("Demote from Custom Widget");
 
80
    if (const QDesignerPromotedWidget *promoted = qobject_cast<const QDesignerPromotedWidget*>(widget))
 
81
        demote_string = tr("Demote to ") + promoted->item()->extends();
 
82
 
 
83
    m_demoteFromCustomWidgetAction = new QAction(demote_string, this);
 
84
    connect(m_demoteFromCustomWidgetAction, SIGNAL(triggered()), this, SLOT(demoteFromCustomWidget()));
 
85
}
 
86
 
 
87
QDesignerTaskMenu::~QDesignerTaskMenu()
 
88
{
 
89
}
 
90
 
 
91
QWidget *QDesignerTaskMenu::widget() const
 
92
{
 
93
    return m_widget;
 
94
}
 
95
 
 
96
QDesignerFormWindowInterface *QDesignerTaskMenu::formWindow() const
 
97
{
 
98
    QDesignerFormWindowInterface *result = QDesignerFormWindowInterface::findFormWindow(widget());
 
99
    Q_ASSERT(result != 0);
 
100
    return result;
 
101
}
 
102
 
 
103
QList<QAction*> QDesignerTaskMenu::taskActions() const
 
104
{
 
105
    QDesignerFormWindowInterface *formWindow = QDesignerFormWindowInterface::findFormWindow(widget());
 
106
    Q_ASSERT(formWindow);
 
107
 
 
108
    QList<QAction*> actions;
 
109
 
 
110
    actions.append(m_changeObjectNameAction);
 
111
    actions.append(m_separator);
 
112
    actions.append(m_changeToolTip);
 
113
    actions.append(m_changeWhatsThis);
 
114
 
 
115
#if 0
 
116
    if (qobject_cast<const QMainWindow*>(formWindow->mainContainer()) != 0 && qobject_cast<QDockWidget*>(widget()) == 0)
 
117
        actions.append(m_createDockWidgetAction);
 
118
#endif
 
119
 
 
120
    if (static_cast<void*>(m_widget) != static_cast<void*>(formWindow)) {
 
121
        actions.append(m_separator);
 
122
        if (qobject_cast<const QDesignerPromotedWidget*>(m_widget) == 0)
 
123
            actions.append(m_promoteToCustomWidgetAction);
 
124
        else
 
125
            actions.append(m_demoteFromCustomWidgetAction);
 
126
    }
 
127
 
 
128
    return actions;
 
129
}
 
130
 
 
131
void QDesignerTaskMenu::changeObjectName()
 
132
{
 
133
    QDesignerFormWindowInterface *fw = formWindow();
 
134
    Q_ASSERT(fw != 0);
 
135
 
 
136
    QDesignerFormEditorInterface *core = fw->core();
 
137
    QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget());
 
138
    Q_ASSERT(sheet != 0);
 
139
 
 
140
    QString newObjectName = QInputDialog::getText(widget(), tr("Change Object Name"),
 
141
            tr("Object Name"), QLineEdit::Normal, sheet->property(sheet->indexOf(QLatin1String("objectName"))).toString());;
 
142
 
 
143
    if (!newObjectName.isEmpty()) {
 
144
        fw->cursor()->setProperty(QLatin1String("objectName"), newObjectName);
 
145
    }
 
146
}
 
147
 
 
148
void QDesignerTaskMenu::createDockWidget()
 
149
{
 
150
    QDesignerFormWindowInterface *formWindow = QDesignerFormWindowInterface::findFormWindow(widget());
 
151
    Q_ASSERT(formWindow != 0);
 
152
 
 
153
    QMainWindow *mainWindow = qobject_cast<QMainWindow*>(formWindow->mainContainer());
 
154
    Q_ASSERT(mainWindow != 0);
 
155
 
 
156
    formWindow->beginCommand(tr("Create Dock Window"));
 
157
 
 
158
    QDesignerWidgetFactoryInterface *widgetFactory = formWindow->core()->widgetFactory();
 
159
    QDockWidget *dockWidget = (QDockWidget *) widgetFactory->createWidget(QLatin1String("QDockWidget"), formWindow->mainContainer());
 
160
    Q_ASSERT(dockWidget);
 
161
 
 
162
    InsertWidgetCommand *cmd = new InsertWidgetCommand(formWindow);
 
163
    cmd->init(dockWidget);
 
164
    formWindow->commandHistory()->push(cmd);
 
165
 
 
166
    ReparentWidgetCommand *reparentCmd = new ReparentWidgetCommand(formWindow);
 
167
    reparentCmd->init(widget(), dockWidget);
 
168
    formWindow->commandHistory()->push(reparentCmd);
 
169
 
 
170
    SetDockWidgetCommand *setDockWidgetCmd = new SetDockWidgetCommand(formWindow);
 
171
    setDockWidgetCmd->init(dockWidget, m_widget);
 
172
    formWindow->commandHistory()->push(setDockWidgetCmd);
 
173
 
 
174
    AddDockWidgetCommand *addDockWidgetCmd = new AddDockWidgetCommand(formWindow);
 
175
    addDockWidgetCmd->init(mainWindow, dockWidget);
 
176
    formWindow->commandHistory()->push(addDockWidgetCmd);
 
177
 
 
178
    formWindow->endCommand();
 
179
}
 
180
 
 
181
QDesignerTaskMenuFactory::QDesignerTaskMenuFactory(QExtensionManager *extensionManager)
 
182
    : QExtensionFactory(extensionManager)
 
183
{
 
184
}
 
185
 
 
186
QObject *QDesignerTaskMenuFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
 
187
{
 
188
    if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
 
189
        return 0;
 
190
 
 
191
    QWidget *widget = qobject_cast<QWidget*>(object);
 
192
    if (widget == 0)
 
193
        return 0;
 
194
 
 
195
    // check if is an internal widget (### generalize)
 
196
    if (qobject_cast<QLayoutWidget*>(widget) || qobject_cast<Spacer*>(widget))
 
197
        return 0;
 
198
 
 
199
    return new QDesignerTaskMenu(widget, parent);
 
200
}
 
201
 
 
202
void QDesignerTaskMenu::promoteToCustomWidget()
 
203
{
 
204
    QDesignerFormWindowInterface *fw = formWindow();
 
205
    QDesignerFormEditorInterface *core = fw->core();
 
206
    QWidget *wgt = widget();
 
207
    QDesignerWidgetDataBaseInterface *db = core->widgetDataBase();
 
208
    WidgetFactory *factory = qobject_cast<WidgetFactory*>(core->widgetFactory());
 
209
 
 
210
    Q_ASSERT(qobject_cast<QDesignerPromotedWidget*>(wgt) == 0);
 
211
 
 
212
    QString base_class_name = QLatin1String(factory->classNameOf(wgt));
 
213
 
 
214
    PromoteToCustomWidgetDialog dialog(db, base_class_name);
 
215
    if (!dialog.exec())
 
216
        return;
 
217
 
 
218
    QString custom_class_name = dialog.customClassName();
 
219
    QString include_file = dialog.includeFile();
 
220
 
 
221
    QDesignerWidgetDataBaseItemInterface *item = 0;
 
222
    int idx = db->indexOfClassName(custom_class_name);
 
223
    if (idx == -1) {
 
224
        item = new WidgetDataBaseItem(custom_class_name, tr("Promoted Widgets"));
 
225
        item->setCustom(true);
 
226
        item->setPromoted(true);
 
227
        item->setExtends(base_class_name);
 
228
        db->append(item);
 
229
    } else {
 
230
        item = db->item(idx);
 
231
    }
 
232
    item->setIncludeFile(include_file);
 
233
 
 
234
    // ### use the undo stack
 
235
    // fw->beginCommand(tr("Promote to custom widget"));
 
236
 
 
237
    PromoteToCustomWidgetCommand *cmd = new PromoteToCustomWidgetCommand(fw);
 
238
    cmd->init(item, wgt);
 
239
    fw->commandHistory()->push(cmd);
 
240
}
 
241
 
 
242
void QDesignerTaskMenu::demoteFromCustomWidget()
 
243
{
 
244
    QDesignerFormWindowInterface *fw = formWindow();
 
245
    QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(widget());
 
246
    Q_ASSERT(promoted != 0);
 
247
 
 
248
    // ### use the undo stack
 
249
    //fw->beginCommand(tr("Demote to ") + promoted->item()->extends());
 
250
 
 
251
    DemoteFromCustomWidgetCommand *cmd = new DemoteFromCustomWidgetCommand(fw);
 
252
    cmd->init(promoted);
 
253
    fw->commandHistory()->push(cmd);
 
254
}
 
255
 
 
256
void QDesignerTaskMenu::changeRichTextProperty(const QString &propertyName)
 
257
{
 
258
    if (QDesignerFormWindowInterface *fw = formWindow()) {
 
259
        RichTextEditorDialog *dlg = new RichTextEditorDialog(fw);
 
260
        Q_ASSERT(m_widget->parentWidget() != 0);
 
261
        RichTextEditor *editor = dlg->editor();
 
262
 
 
263
        QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(fw->core()->extensionManager(), m_widget);
 
264
        Q_ASSERT(sheet != 0);
 
265
 
 
266
        editor->setDefaultFont(m_widget->font());
 
267
        editor->setText(sheet->property(sheet->indexOf(propertyName)).toString());
 
268
        editor->selectAll();
 
269
 
 
270
        if (dlg->exec()) {
 
271
            QString text = editor->text(Qt::RichText);
 
272
            fw->cursor()->setWidgetProperty(m_widget, propertyName, QVariant(text));
 
273
        }
 
274
 
 
275
        delete dlg;
 
276
    }
 
277
}
 
278
 
 
279
void QDesignerTaskMenu::changeToolTip()
 
280
{
 
281
    changeRichTextProperty(QLatin1String("toolTip"));
 
282
}
 
283
 
 
284
void QDesignerTaskMenu::changeStatusTip()
 
285
{
 
286
    changeRichTextProperty(QLatin1String("statusTip"));
 
287
}
 
288
 
 
289
void QDesignerTaskMenu::changeWhatsThis()
 
290
{
 
291
    changeRichTextProperty(QLatin1String("whatsThis"));
 
292
}