// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // // W A R N I N G // ------------- // // This file is not part of the Qt API. It exists for the convenience // of Qt Designer. This header // file may change from version to version without notice, or even be removed. // // We mean it. // #ifndef SHARED_EXTENSIONFACTORY_H #define SHARED_EXTENSIONFACTORY_H #include #include QT_BEGIN_NAMESPACE namespace qdesigner_internal { // Extension factory for registering an extension for an object type. template class ExtensionFactory: public QExtensionFactory { public: explicit ExtensionFactory(const QString &iid, QExtensionManager *parent = nullptr); // Convenience for registering the extension. Do not use for derived classes. static void registerExtension(QExtensionManager *mgr, const QString &iid); protected: QObject *createExtension(QObject *qObject, const QString &iid, QObject *parent) const override; private: // Can be overwritten to perform checks on the object. // Default does a qobject_cast to the desired class. virtual Object *checkObject(QObject *qObject) const; const QString m_iid; }; template ExtensionFactory::ExtensionFactory(const QString &iid, QExtensionManager *parent) : QExtensionFactory(parent), m_iid(iid) { } template Object *ExtensionFactory::checkObject(QObject *qObject) const { return qobject_cast(qObject); } template QObject *ExtensionFactory::createExtension(QObject *qObject, const QString &iid, QObject *parent) const { if (iid != m_iid) return nullptr; Object *object = checkObject(qObject); if (!object) return nullptr; return new Extension(object, parent); } template void ExtensionFactory::registerExtension(QExtensionManager *mgr, const QString &iid) { ExtensionFactory *factory = new ExtensionFactory(iid, mgr); mgr->registerExtensions(factory, iid); } } // namespace qdesigner_internal QT_END_NAMESPACE #endif // SHARED_EXTENSIONFACTORY_H