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