1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qdesigner_taskmenu_p.h"
30
#include "qdesigner_command_p.h"
31
#include "qdesigner_promotedwidget_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"
41
#include <QtDesigner/QtDesigner>
42
#include <QtDesigner/QExtensionManager>
43
#include <QtDesigner/QDesignerLayoutDecorationExtension>
45
#include <QtGui/QAction>
46
#include <QtGui/QWidget>
47
#include <QtGui/QInputDialog>
48
#include <QtGui/QMainWindow>
49
#include <QtGui/QDockWidget>
50
#include <QtCore/QVariant>
52
#include <QtCore/qdebug.h>
54
QDesignerTaskMenu::QDesignerTaskMenu(QWidget *widget, QObject *parent)
58
m_separator = new QAction(this);
59
m_separator->setSeparator(true);
61
m_changeObjectNameAction = new QAction(tr("Change objectName"), this);
62
connect(m_changeObjectNameAction, SIGNAL(triggered()), this, SLOT(changeObjectName()));
64
m_changeStatusTip = new QAction(tr("Change statusTip"), this);
65
connect(m_changeStatusTip, SIGNAL(triggered()), this, SLOT(changeStatusTip()));
67
m_changeToolTip = new QAction(tr("Change toolTip"), this);
68
connect(m_changeToolTip, SIGNAL(triggered()), this, SLOT(changeToolTip()));
70
m_changeWhatsThis = new QAction(tr("Change whatsThis"), this);
71
connect(m_changeWhatsThis, SIGNAL(triggered()), this, SLOT(changeWhatsThis()));
73
m_createDockWidgetAction = new QAction(tr("Create Dock Window"), this);
74
connect(m_createDockWidgetAction, SIGNAL(triggered()), this, SLOT(createDockWidget()));
76
m_promoteToCustomWidgetAction = new QAction(tr("Promote to Custom Widget"), this);
77
connect(m_promoteToCustomWidgetAction, SIGNAL(triggered()), this, SLOT(promoteToCustomWidget()));
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();
83
m_demoteFromCustomWidgetAction = new QAction(demote_string, this);
84
connect(m_demoteFromCustomWidgetAction, SIGNAL(triggered()), this, SLOT(demoteFromCustomWidget()));
87
QDesignerTaskMenu::~QDesignerTaskMenu()
91
QWidget *QDesignerTaskMenu::widget() const
96
QDesignerFormWindowInterface *QDesignerTaskMenu::formWindow() const
98
QDesignerFormWindowInterface *result = QDesignerFormWindowInterface::findFormWindow(widget());
99
Q_ASSERT(result != 0);
103
QList<QAction*> QDesignerTaskMenu::taskActions() const
105
QDesignerFormWindowInterface *formWindow = QDesignerFormWindowInterface::findFormWindow(widget());
106
Q_ASSERT(formWindow);
108
QList<QAction*> actions;
110
actions.append(m_changeObjectNameAction);
111
actions.append(m_separator);
112
actions.append(m_changeToolTip);
113
actions.append(m_changeWhatsThis);
116
if (qobject_cast<const QMainWindow*>(formWindow->mainContainer()) != 0 && qobject_cast<QDockWidget*>(widget()) == 0)
117
actions.append(m_createDockWidgetAction);
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);
125
actions.append(m_demoteFromCustomWidgetAction);
131
void QDesignerTaskMenu::changeObjectName()
133
QDesignerFormWindowInterface *fw = formWindow();
136
QDesignerFormEditorInterface *core = fw->core();
137
QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(), widget());
138
Q_ASSERT(sheet != 0);
140
QString newObjectName = QInputDialog::getText(widget(), tr("Change Object Name"),
141
tr("Object Name"), QLineEdit::Normal, sheet->property(sheet->indexOf(QLatin1String("objectName"))).toString());;
143
if (!newObjectName.isEmpty()) {
144
fw->cursor()->setProperty(QLatin1String("objectName"), newObjectName);
148
void QDesignerTaskMenu::createDockWidget()
150
QDesignerFormWindowInterface *formWindow = QDesignerFormWindowInterface::findFormWindow(widget());
151
Q_ASSERT(formWindow != 0);
153
QMainWindow *mainWindow = qobject_cast<QMainWindow*>(formWindow->mainContainer());
154
Q_ASSERT(mainWindow != 0);
156
formWindow->beginCommand(tr("Create Dock Window"));
158
QDesignerWidgetFactoryInterface *widgetFactory = formWindow->core()->widgetFactory();
159
QDockWidget *dockWidget = (QDockWidget *) widgetFactory->createWidget(QLatin1String("QDockWidget"), formWindow->mainContainer());
160
Q_ASSERT(dockWidget);
162
InsertWidgetCommand *cmd = new InsertWidgetCommand(formWindow);
163
cmd->init(dockWidget);
164
formWindow->commandHistory()->push(cmd);
166
ReparentWidgetCommand *reparentCmd = new ReparentWidgetCommand(formWindow);
167
reparentCmd->init(widget(), dockWidget);
168
formWindow->commandHistory()->push(reparentCmd);
170
SetDockWidgetCommand *setDockWidgetCmd = new SetDockWidgetCommand(formWindow);
171
setDockWidgetCmd->init(dockWidget, m_widget);
172
formWindow->commandHistory()->push(setDockWidgetCmd);
174
AddDockWidgetCommand *addDockWidgetCmd = new AddDockWidgetCommand(formWindow);
175
addDockWidgetCmd->init(mainWindow, dockWidget);
176
formWindow->commandHistory()->push(addDockWidgetCmd);
178
formWindow->endCommand();
181
QDesignerTaskMenuFactory::QDesignerTaskMenuFactory(QExtensionManager *extensionManager)
182
: QExtensionFactory(extensionManager)
186
QObject *QDesignerTaskMenuFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
188
if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
191
QWidget *widget = qobject_cast<QWidget*>(object);
195
// check if is an internal widget (### generalize)
196
if (qobject_cast<QLayoutWidget*>(widget) || qobject_cast<Spacer*>(widget))
199
return new QDesignerTaskMenu(widget, parent);
202
void QDesignerTaskMenu::promoteToCustomWidget()
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());
210
Q_ASSERT(qobject_cast<QDesignerPromotedWidget*>(wgt) == 0);
212
QString base_class_name = QLatin1String(factory->classNameOf(wgt));
214
PromoteToCustomWidgetDialog dialog(db, base_class_name);
218
QString custom_class_name = dialog.customClassName();
219
QString include_file = dialog.includeFile();
221
QDesignerWidgetDataBaseItemInterface *item = 0;
222
int idx = db->indexOfClassName(custom_class_name);
224
item = new WidgetDataBaseItem(custom_class_name, tr("Promoted Widgets"));
225
item->setCustom(true);
226
item->setPromoted(true);
227
item->setExtends(base_class_name);
230
item = db->item(idx);
232
item->setIncludeFile(include_file);
234
// ### use the undo stack
235
// fw->beginCommand(tr("Promote to custom widget"));
237
PromoteToCustomWidgetCommand *cmd = new PromoteToCustomWidgetCommand(fw);
238
cmd->init(item, wgt);
239
fw->commandHistory()->push(cmd);
242
void QDesignerTaskMenu::demoteFromCustomWidget()
244
QDesignerFormWindowInterface *fw = formWindow();
245
QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(widget());
246
Q_ASSERT(promoted != 0);
248
// ### use the undo stack
249
//fw->beginCommand(tr("Demote to ") + promoted->item()->extends());
251
DemoteFromCustomWidgetCommand *cmd = new DemoteFromCustomWidgetCommand(fw);
253
fw->commandHistory()->push(cmd);
256
void QDesignerTaskMenu::changeRichTextProperty(const QString &propertyName)
258
if (QDesignerFormWindowInterface *fw = formWindow()) {
259
RichTextEditorDialog *dlg = new RichTextEditorDialog(fw);
260
Q_ASSERT(m_widget->parentWidget() != 0);
261
RichTextEditor *editor = dlg->editor();
263
QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(fw->core()->extensionManager(), m_widget);
264
Q_ASSERT(sheet != 0);
266
editor->setDefaultFont(m_widget->font());
267
editor->setText(sheet->property(sheet->indexOf(propertyName)).toString());
271
QString text = editor->text(Qt::RichText);
272
fw->cursor()->setWidgetProperty(m_widget, propertyName, QVariant(text));
279
void QDesignerTaskMenu::changeToolTip()
281
changeRichTextProperty(QLatin1String("toolTip"));
284
void QDesignerTaskMenu::changeStatusTip()
286
changeRichTextProperty(QLatin1String("statusTip"));
289
void QDesignerTaskMenu::changeWhatsThis()
291
changeRichTextProperty(QLatin1String("whatsThis"));