Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdesigner_components.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include <QtDesigner/QDesignerComponents>
6
7#include <actioneditor_p.h>
8#include <pluginmanager_p.h>
9#include <widgetdatabase_p.h>
10#include <widgetfactory_p.h>
11
12#include <formeditor/formeditor.h>
13#include <widgetbox/widgetbox.h>
14#include <propertyeditor/propertyeditor.h>
15#include <objectinspector/objectinspector.h>
16#include <taskmenu/taskmenu_component.h>
18#include <signalsloteditor/signalsloteditorwindow.h>
19
20#include <buddyeditor/buddyeditor_plugin.h>
21#include <signalsloteditor/signalsloteditor_plugin.h>
22#include <tabordereditor/tabordereditor_plugin.h>
23
24#include <QtDesigner/abstractlanguage.h>
25#include <QtDesigner/qextensionmanager.h>
26#include <QtDesigner/abstractintegration.h>
27#include <QtDesigner/abstractresourcebrowser.h>
28
29#include <QtCore/qplugin.h>
30#include <QtCore/qdir.h>
31#include <QtCore/qtextstream.h>
32#include <QtCore/qdebug.h>
33#include <QtCore/qfile.h>
34#include <QtCore/qfileinfo.h>
35
36Q_IMPORT_PLUGIN(SignalSlotEditorPlugin)
37Q_IMPORT_PLUGIN(BuddyEditorPlugin)
38Q_IMPORT_PLUGIN(TabOrderEditorPlugin)
39
40static void initResources()
41{
42 // Q_INIT_RESOURCE only usable in functions in global namespace
43 Q_INIT_RESOURCE(formeditor);
44 Q_INIT_RESOURCE(widgetbox);
45 Q_INIT_RESOURCE(propertyeditor);
46}
47
48QT_BEGIN_NAMESPACE
49
50using namespace Qt::StringLiterals;
51
52/*!
53 \class QDesignerComponents
54 \brief The QDesignerComponents class provides a central resource for the various components
55 used in the \QD user interface.
56 \inmodule QtDesigner
57 \internal
58
59 The QDesignerComponents class is a factory for each of the standard components present
60 in the \QD user interface. It is mostly useful for developers who want to implement
61 a standalone form editing environment using \QD's components, or who need to integrate
62 \QD's components into an existing integrated development environment (IDE).
63
64 \sa QDesignerFormEditorInterface, QDesignerObjectInspectorInterface,
65 QDesignerPropertyEditorInterface, QDesignerWidgetBoxInterface
66*/
67
68/*!
69 Initializes the resources used by the components.*/
70void QDesignerComponents::initializeResources()
71{
72 initResources();
73}
74
75/*!
76 Initializes the plugins used by the components.*/
77void QDesignerComponents::initializePlugins(QDesignerFormEditorInterface *core)
78{
79 QDesignerIntegration::initializePlugins(core);
80}
81
82// ### fixme Qt 7 createFormEditorWithPluginPaths->createFormEditor
83
84/*!
85 Constructs a form editor interface with the given \a parent.*/
86QDesignerFormEditorInterface *QDesignerComponents::createFormEditor(QObject *parent)
87{
88 return createFormEditorWithPluginPaths({}, parent);
89}
90
91/*!
92 Constructs a form editor interface with the given \a pluginPaths and the \a parent.
93 \since 6.7
94*/
95QDesignerFormEditorInterface *
96 QDesignerComponents::createFormEditorWithPluginPaths(const QStringList &pluginPaths,
97 QObject *parent)
98{
99 return new qdesigner_internal::FormEditor(pluginPaths, parent);
100}
101
102/*!
103 Returns a new task menu with the given \a parent for the \a core interface.*/
104QObject *QDesignerComponents::createTaskMenu(QDesignerFormEditorInterface *core, QObject *parent)
105{
106 return new qdesigner_internal::TaskMenuComponent(core, parent);
107}
108
109static inline int qtMajorVersion(int qtVersion) { return qtVersion >> 16; }
110static inline int qtMinorVersion(int qtVersion) { return (qtVersion >> 8) & 0xFF; }
111static inline void setMinorVersion(int minorVersion, int *qtVersion)
112{
113 *qtVersion &= ~0xFF00;
114 *qtVersion |= minorVersion << 8;
115}
116
117// Build the version-dependent name of the user widget box file, '$HOME.designer/widgetbox4.4.xml'
118static inline QString widgetBoxFileName(int qtVersion, const QDesignerLanguageExtension *lang = nullptr)
119{
120 QString rc; {
121 QTextStream str(&rc);
122 str << QDir::homePath() << QDir::separator() << ".designer" << QDir::separator()
123 << "widgetbox";
124 // The naming convention using the version was introduced with 4.4
125 const int major = qtMajorVersion(qtVersion);
126 const int minor = qtMinorVersion(qtVersion);
127 if (major >= 4 && minor >= 4)
128 str << major << '.' << minor;
129 if (lang)
130 str << '.' << lang->uiExtension();
131 str << ".xml";
132 }
133 return rc;
134}
135
136/*!
137 Returns a new widget box interface with the given \a parent for the \a core interface.*/
138QDesignerWidgetBoxInterface *QDesignerComponents::createWidgetBox(QDesignerFormEditorInterface *core, QWidget *parent)
139{
140 qdesigner_internal::WidgetBox *widgetBox = new qdesigner_internal::WidgetBox(core, parent);
141
142 const QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core);
143
144 do {
145 if (lang) {
146 const QString languageWidgetBox = lang->widgetBoxContents();
147 if (!languageWidgetBox.isEmpty()) {
148 widgetBox->loadContents(lang->widgetBoxContents());
149 break;
150 }
151 }
152
153 widgetBox->setFileName(u":/qt-project.org/widgetbox/widgetbox.xml"_s);
154 widgetBox->load();
155 } while (false);
156
157 const QString userWidgetBoxFile = widgetBoxFileName(QT_VERSION, lang);
158
159 widgetBox->setFileName(userWidgetBoxFile);
160 if (!QFileInfo::exists(userWidgetBoxFile)) {
161 // check previous version, that is, are we running the new version for the first time
162 // If so, try to copy the old widget box file
163 if (const int minv = qtMinorVersion(QT_VERSION)) {
164 int oldVersion = QT_VERSION;
165 setMinorVersion(minv - 1, &oldVersion);
166 const QString oldWidgetBoxFile = widgetBoxFileName(oldVersion, lang);
167 if (QFileInfo::exists(oldWidgetBoxFile))
168 QFile::copy(oldWidgetBoxFile, userWidgetBoxFile);
169 }
170 }
171 widgetBox->load();
172
173 return widgetBox;
174}
175
176/*!
177 Returns a new property editor interface with the given \a parent for the \a core interface.*/
178QDesignerPropertyEditorInterface *QDesignerComponents::createPropertyEditor(QDesignerFormEditorInterface *core, QWidget *parent)
179{
180 return new qdesigner_internal::PropertyEditor(core, parent);
181}
182
183/*!
184 Returns a new object inspector interface with the given \a parent for the \a core interface.*/
185QDesignerObjectInspectorInterface *QDesignerComponents::createObjectInspector(QDesignerFormEditorInterface *core, QWidget *parent)
186{
187 return new qdesigner_internal::ObjectInspector(core, parent);
188}
189
190/*!
191 Returns a new action editor interface with the given \a parent for the \a core interface.*/
192QDesignerActionEditorInterface *QDesignerComponents::createActionEditor(QDesignerFormEditorInterface *core, QWidget *parent)
193{
194 return new qdesigner_internal::ActionEditor(core, parent);
195}
196
197/*!
198 Returns a new resource editor with the given \a parent for the \a core interface.*/
199QWidget *QDesignerComponents::createResourceEditor(QDesignerFormEditorInterface *core, QWidget *parent)
200{
201 if (QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core)) {
202 QWidget *w = lang->createResourceBrowser(parent);
203 if (w)
204 return w;
205 }
206 QtResourceView *resourceView = new QtResourceView(core, parent);
207 resourceView->setResourceModel(core->resourceModel());
208 resourceView->setSettingsKey(u"ResourceBrowser"_s);
209 // Note for integrators: make sure you call createResourceEditor() after you instantiated your subclass of designer integration
210 // (designer doesn't do that since by default editing resources is enabled)
211 const QDesignerIntegrationInterface *integration = core->integration();
212 if (integration && !integration->hasFeature(QDesignerIntegrationInterface::ResourceEditorFeature))
213 resourceView->setResourceEditingEnabled(false);
214 return resourceView;
215}
216
217/*!
218 Returns a new signal-slot editor with the given \a parent for the \a core interface.*/
219QWidget *QDesignerComponents::createSignalSlotEditor(QDesignerFormEditorInterface *core, QWidget *parent)
220{
221 return new qdesigner_internal::SignalSlotEditorWindow(core, parent);
222}
223
224/*!
225 Returns the default plugin paths of Qt Widgets Designer's plugin manager.
226
227 \return Plugin paths
228 \since 6.7
229*/
230QStringList QDesignerComponents::defaultPluginPaths()
231{
232 return QDesignerPluginManager::defaultPluginPaths();
233}
234
235QT_END_NAMESPACE
static int qtMajorVersion(int qtVersion)
static QString widgetBoxFileName(int qtVersion, const QDesignerLanguageExtension *lang=nullptr)
static void setMinorVersion(int minorVersion, int *qtVersion)
static int qtMinorVersion(int qtVersion)