| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the Qt Designer of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qextensionmanager.h"
|
|---|
| 43 |
|
|---|
| 44 | QT_BEGIN_NAMESPACE
|
|---|
| 45 |
|
|---|
| 46 | /*!
|
|---|
| 47 | \class QExtensionManager
|
|---|
| 48 |
|
|---|
| 49 | \brief The QExtensionManager class provides extension management
|
|---|
| 50 | facilities for Qt Designer.
|
|---|
| 51 |
|
|---|
| 52 | \inmodule QtDesigner
|
|---|
| 53 |
|
|---|
| 54 | In \QD the extensions are not created until they are required. For
|
|---|
| 55 | that reason, when implementing an extension, you must also create
|
|---|
| 56 | a QExtensionFactory, i.e a class that is able to make an instance
|
|---|
| 57 | of your extension, and register it using \QD's extension manager.
|
|---|
| 58 |
|
|---|
| 59 | The registration of an extension factory is typically made in the
|
|---|
| 60 | QDesignerCustomWidgetInterface::initialize() function:
|
|---|
| 61 |
|
|---|
| 62 | \snippet doc/src/snippets/code/tools_designer_src_lib_extension_qextensionmanager.cpp 0
|
|---|
| 63 |
|
|---|
| 64 | The QExtensionManager is not intended to be instantiated
|
|---|
| 65 | directly. You can retrieve an interface to \QD's extension manager
|
|---|
| 66 | using the QDesignerFormEditorInterface::extensionManager()
|
|---|
| 67 | function. A pointer to \QD's current QDesignerFormEditorInterface
|
|---|
| 68 | object (\c formEditor in the example above) is provided by the
|
|---|
| 69 | QDesignerCustomWidgetInterface::initialize() function's
|
|---|
| 70 | parameter. When implementing a custom widget plugin, you must
|
|---|
| 71 | subclass the QDesignerCustomWidgetInterface to expose your plugin
|
|---|
| 72 | to \QD.
|
|---|
| 73 |
|
|---|
| 74 | Then, when an extension is required, \QD's extension manager will
|
|---|
| 75 | run through all its registered factories calling
|
|---|
| 76 | QExtensionFactory::createExtension() for each until the first one
|
|---|
| 77 | that is able to create the requested extension for the selected
|
|---|
| 78 | object, is found. This factory will then make an instance of the
|
|---|
| 79 | extension.
|
|---|
| 80 |
|
|---|
| 81 | There are four available types of extensions in \QD:
|
|---|
| 82 | QDesignerContainerExtension , QDesignerMemberSheetExtension,
|
|---|
| 83 | QDesignerPropertySheetExtension and
|
|---|
| 84 | QDesignerTaskMenuExtension. \QD's behavior is the same whether the
|
|---|
| 85 | requested extension is associated with a container, a member
|
|---|
| 86 | sheet, a property sheet or a task menu.
|
|---|
| 87 |
|
|---|
| 88 | For a complete example using the QExtensionManager class, see the
|
|---|
| 89 | \l {designer/taskmenuextension}{Task Menu Extension example}. The
|
|---|
| 90 | example shows how to create a custom widget plugin for Qt
|
|---|
| 91 | Designer, and how to to use the QDesignerTaskMenuExtension class
|
|---|
| 92 | to add custom items to \QD's task menu.
|
|---|
| 93 |
|
|---|
| 94 | \sa QExtensionFactory, QAbstractExtensionManager
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | /*!
|
|---|
| 98 | Constructs an extension manager with the given \a parent.
|
|---|
| 99 | */
|
|---|
| 100 | QExtensionManager::QExtensionManager(QObject *parent)
|
|---|
| 101 | : QObject(parent)
|
|---|
| 102 | {
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /*!
|
|---|
| 107 | Destroys the extension manager
|
|---|
| 108 | */
|
|---|
| 109 | QExtensionManager::~QExtensionManager()
|
|---|
| 110 | {
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /*!
|
|---|
| 114 | Register the extension specified by the given \a factory and
|
|---|
| 115 | extension identifier \a iid.
|
|---|
| 116 | */
|
|---|
| 117 | void QExtensionManager::registerExtensions(QAbstractExtensionFactory *factory, const QString &iid)
|
|---|
| 118 | {
|
|---|
| 119 | if (iid.isEmpty()) {
|
|---|
| 120 | m_globalExtension.prepend(factory);
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | FactoryMap::iterator it = m_extensions.find(iid);
|
|---|
| 125 | if (it == m_extensions.end())
|
|---|
| 126 | it = m_extensions.insert(iid, FactoryList());
|
|---|
| 127 |
|
|---|
| 128 | it.value().prepend(factory);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*!
|
|---|
| 132 | Unregister the extension specified by the given \a factory and
|
|---|
| 133 | extension identifier \a iid.
|
|---|
| 134 | */
|
|---|
| 135 | void QExtensionManager::unregisterExtensions(QAbstractExtensionFactory *factory, const QString &iid)
|
|---|
| 136 | {
|
|---|
| 137 | if (iid.isEmpty()) {
|
|---|
| 138 | m_globalExtension.removeAll(factory);
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | const FactoryMap::iterator it = m_extensions.find(iid);
|
|---|
| 143 | if (it == m_extensions.end())
|
|---|
| 144 | return;
|
|---|
| 145 |
|
|---|
| 146 | FactoryList &factories = it.value();
|
|---|
| 147 | factories.removeAll(factory);
|
|---|
| 148 |
|
|---|
| 149 | if (factories.isEmpty())
|
|---|
| 150 | m_extensions.erase(it);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /*!
|
|---|
| 154 | Returns the extension specified by \a iid, for the given \a
|
|---|
| 155 | object.
|
|---|
| 156 | */
|
|---|
| 157 | QObject *QExtensionManager::extension(QObject *object, const QString &iid) const
|
|---|
| 158 | {
|
|---|
| 159 | const FactoryMap::const_iterator it = m_extensions.constFind(iid);
|
|---|
| 160 | if (it != m_extensions.constEnd()) {
|
|---|
| 161 | const FactoryList::const_iterator fcend = it.value().constEnd();
|
|---|
| 162 | for (FactoryList::const_iterator fit = it.value().constBegin(); fit != fcend; ++fit)
|
|---|
| 163 | if (QObject *ext = (*fit)->extension(object, iid))
|
|---|
| 164 | return ext;
|
|---|
| 165 | }
|
|---|
| 166 | const FactoryList::const_iterator gfcend = m_globalExtension.constEnd();
|
|---|
| 167 | for (FactoryList::const_iterator git = m_globalExtension.constBegin(); git != gfcend; ++git)
|
|---|
| 168 | if (QObject *ext = (*git)->extension(object, iid))
|
|---|
| 169 | return ext;
|
|---|
| 170 |
|
|---|
| 171 | return 0;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | QT_END_NAMESPACE
|
|---|