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
qextensionmanager.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
5
7
8/*!
9 \class QExtensionManager
10
11 \brief The QExtensionManager class provides extension management
12 facilities for \QD.
13
14 \inmodule QtDesigner
15
16 In \QD the extensions are not created until they are required. For
17 that reason, when implementing an extension, you must also create
18 a QExtensionFactory, i.e a class that is able to make an instance
19 of your extension, and register it using \QD's extension manager.
20
21 The registration of an extension factory is typically made in the
22 QDesignerCustomWidgetInterface::initialize() function:
23
24 \snippet lib/tools_designer_src_lib_extension_qextensionmanager.cpp 0
25
26 The QExtensionManager is not intended to be instantiated
27 directly. You can retrieve an interface to \QD's extension manager
28 using the QDesignerFormEditorInterface::extensionManager()
29 function. A pointer to \QD's current QDesignerFormEditorInterface
30 object (\c formEditor in the example above) is provided by the
31 QDesignerCustomWidgetInterface::initialize() function's
32 parameter. When implementing a custom widget plugin, you must
33 subclass the QDesignerCustomWidgetInterface to expose your plugin
34 to \QD.
35
36 Then, when an extension is required, \QD's extension manager will
37 run through all its registered factories calling
38 QExtensionFactory::createExtension() for each until the first one
39 that is able to create the requested extension for the selected
40 object, is found. This factory will then make an instance of the
41 extension.
42
43 There are four available types of extensions in \QD:
44 QDesignerContainerExtension , QDesignerMemberSheetExtension,
45 QDesignerPropertySheetExtension and
46 QDesignerTaskMenuExtension. \QD's behavior is the same whether the
47 requested extension is associated with a container, a member
48 sheet, a property sheet or a task menu.
49
50 For a complete example using the QExtensionManager class, see the
51 \l {taskmenuextension}{Task Menu Extension example}. The
52 example shows how to create a custom widget plugin for Qt
53 Designer, and how to use the QDesignerTaskMenuExtension class
54 to add custom items to \QD's task menu.
55
56 \sa QExtensionFactory, QAbstractExtensionManager
57*/
58
59/*!
60 Constructs an extension manager with the given \a parent.
61*/
62QExtensionManager::QExtensionManager(QObject *parent)
63 : QObject(parent)
64{
65}
66
67
68/*!
69 Destroys the extension manager
70*/
71QExtensionManager::~QExtensionManager() = default;
72
73/*!
74 Register the extension specified by the given \a factory and
75 extension identifier \a iid.
76*/
77void QExtensionManager::registerExtensions(QAbstractExtensionFactory *factory, const QString &iid)
78{
79 if (iid.isEmpty()) {
80 m_globalExtension.prepend(factory);
81 return;
82 }
83
84 auto it = m_extensions.find(iid);
85 if (it == m_extensions.end())
86 it = m_extensions.insert(iid, FactoryList());
87
88 it.value().prepend(factory);
89}
90
91/*!
92 Unregister the extension specified by the given \a factory and
93 extension identifier \a iid.
94*/
95void QExtensionManager::unregisterExtensions(QAbstractExtensionFactory *factory, const QString &iid)
96{
97 if (iid.isEmpty()) {
98 m_globalExtension.removeAll(factory);
99 return;
100 }
101
102 const auto it = m_extensions.find(iid);
103 if (it == m_extensions.end())
104 return;
105
106 FactoryList &factories = it.value();
107 factories.removeAll(factory);
108
109 if (factories.isEmpty())
110 m_extensions.erase(it);
111}
112
113/*!
114 Returns the extension specified by \a iid, for the given \a
115 object.
116*/
117QObject *QExtensionManager::extension(QObject *object, const QString &iid) const
118{
119 const auto it = m_extensions.constFind(iid);
120 if (it != m_extensions.constEnd()) {
121 for (const auto &f : it.value()) {
122 if (QObject *ext = f->extension(object, iid))
123 return ext;
124 }
125 }
126
127 for (const auto &gf : m_globalExtension) {
128 if (QObject *ext = gf->extension(object, iid))
129 return ext;
130 }
131
132 return nullptr;
133}
134
135QT_END_NAMESPACE
Combined button and popup list for selecting options.