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