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
default_extensionfactory.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
7#include <qpointer.h>
8#include <QtCore/qdebug.h>
9
11
12/*!
13 \class QExtensionFactory
14
15 \brief The QExtensionFactory class allows you to create a factory
16 that is able to make instances of custom extensions in Qt
17 Designer.
18
19 \inmodule QtDesigner
20
21 In \QD the extensions are not created until they are required. For
22 that reason, when implementing a custom extension, you must also
23 create a QExtensionFactory, i.e. a class that is able to make an
24 instance of your extension, and register it using \QD's \l
25 {QExtensionManager}{extension manager}.
26
27 The QExtensionManager class provides extension management
28 facilities for \QD. When an extension is required, Qt
29 Designer's \l {QExtensionManager}{extension manager} will run
30 through all its registered factories calling
31 QExtensionFactory::createExtension() for each until the first one
32 that is able to create a requested extension for the selected
33 object, is found. This factory will then make an instance of the
34 extension.
35
36 There are four available types of extensions in \QD:
37 QDesignerContainerExtension , QDesignerMemberSheetExtension,
38 QDesignerPropertySheetExtension and QDesignerTaskMenuExtension. Qt
39 Designer's behavior is the same whether the requested extension is
40 associated with a multi page container, a member sheet, a property
41 sheet or a task menu.
42
43 You can either create a new QExtensionFactory and reimplement the
44 QExtensionFactory::createExtension() function. For example:
45
46 \snippet lib/tools_designer_src_lib_extension_default_extensionfactory.cpp 0
47
48 Or you can use an existing factory, expanding the
49 QExtensionFactory::createExtension() function to make the factory
50 able to create your extension as well. For example:
51
52 \snippet lib/tools_designer_src_lib_extension_default_extensionfactory.cpp 1
53
54 For a complete example using the QExtensionFactory class, see the
55 \l {taskmenuextension}{Task Menu Extension example}. The
56 example shows how to create a custom widget plugin for Qt
57 Designer, and how to use the QDesignerTaskMenuExtension class
58 to add custom items to \QD's task menu.
59
60 \sa QExtensionManager, QAbstractExtensionFactory
61*/
62
63/*!
64 Constructs an extension factory with the given \a parent.
65*/
66QExtensionFactory::QExtensionFactory(QExtensionManager *parent)
67 : QObject(parent)
68{
69}
70
71/*!
72 Returns the extension specified by \a iid for the given \a object.
73
74 \sa createExtension()
75*/
76
77QObject *QExtensionFactory::extension(QObject *object, const QString &iid) const
78{
79 if (!object)
80 return nullptr;
81 const auto key = std::make_pair(iid, object);
82
83 auto it = m_extensions.find(key);
84 if (it == m_extensions.end()) {
85 if (QObject *ext = createExtension(object, iid, const_cast<QExtensionFactory*>(this))) {
86 connect(ext, &QObject::destroyed, this, &QExtensionFactory::objectDestroyed);
87 it = m_extensions.insert(key, ext);
88 }
89 }
90
91 if (!m_extended.contains(object)) {
92 connect(object, &QObject::destroyed, this, &QExtensionFactory::objectDestroyed);
93 m_extended.insert(object, true);
94 }
95
96 if (it == m_extensions.end())
97 return nullptr;
98
99 return it.value();
100}
101
102void QExtensionFactory::objectDestroyed(QObject *object)
103{
104 for (auto it = m_extensions.begin(); it != m_extensions.end(); ) {
105 if (it.key().second == object || object == it.value())
106 it = m_extensions.erase(it);
107 else
108 ++it;
109 }
110
111 m_extended.remove(object);
112}
113
114/*!
115 Creates an extension specified by \a iid for the given \a object.
116 The extension object is created as a child of the specified \a
117 parent.
118
119 \sa extension()
120*/
121QObject *QExtensionFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
122{
123 Q_UNUSED(object);
124 Q_UNUSED(iid);
125 Q_UNUSED(parent);
126
127 return nullptr;
128}
129
130/*!
131 Returns the extension manager for the extension factory.
132*/
133QExtensionManager *QExtensionFactory::extensionManager() const
134{
135 return static_cast<QExtensionManager *>(parent());
136}
137
138QT_END_NAMESPACE
Combined button and popup list for selecting options.