From ea0d68f7d46c800319d3926360603c67652f4a8d Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@digia.com>
Date: Tue, 12 Feb 2013 14:15:31 +0100
Subject: [PATCH] Qt Designer custom widget wizard: Write plugins compatible
 with Qt 4 / 5.

Generate plugin metadata for Qt 5 or plugin export for Qt 4.

Change-Id: I9888ebb22204c96755f82efc0dd2bceb51a19b0c
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
---
 .../customwidgetwizard/tpl_collection.h       |  1 +
 .../customwidgetwizard/tpl_plugin.pro         |  8 ++++-
 .../customwidgetwizard/tpl_single.h           |  1 +
 .../customwidgetwizard/plugingenerator.cpp    | 32 ++++++++++++-------
 4 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_collection.h b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_collection.h
index 2d1ef9e099f..2ce5cb46e5c 100644
--- a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_collection.h
+++ b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_collection.h
@@ -8,6 +8,7 @@ class @COLLECTION_PLUGIN_CLASS@ : public QObject, public QDesignerCustomWidgetCo
 {
     Q_OBJECT
     Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
+    @COLLECTION_PLUGIN_METADATA@
 
 public:
     explicit @COLLECTION_PLUGIN_CLASS@(QObject *parent = 0);
diff --git a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_plugin.pro b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_plugin.pro
index 885e37c5394..53d12b7397f 100644
--- a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_plugin.pro
+++ b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_plugin.pro
@@ -1,4 +1,4 @@
-CONFIG      += designer plugin debug_and_release
+CONFIG      += plugin debug_and_release
 TARGET      = $$qtLibraryTarget(@PLUGIN_NAME@)
 TEMPLATE    = lib
 
@@ -7,6 +7,12 @@ SOURCES     =@PLUGIN_SOURCES@
 RESOURCES   = @PLUGIN_RESOURCES@
 LIBS        += -L. @WIDGET_LIBS@
 
+greaterThan(QT_MAJOR_VERSION, 4) {
+    QT += designer
+} else {
+    CONFIG += designer
+}
+
 target.path = $$[QT_INSTALL_PLUGINS]/designer
 INSTALLS    += target
 
diff --git a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_single.h b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_single.h
index 45a2781e2f2..09415544742 100644
--- a/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_single.h
+++ b/share/qtcreator/templates/qt4project/customwidgetwizard/tpl_single.h
@@ -7,6 +7,7 @@ class @PLUGIN_CLASS@ : public QObject, public QDesignerCustomWidgetInterface
 {
     Q_OBJECT
     Q_INTERFACES(QDesignerCustomWidgetInterface)
+    @SINGLE_PLUGIN_METADATA@
 
 public:
     @PLUGIN_CLASS@(QObject *parent = 0);
diff --git a/src/plugins/qt4projectmanager/customwidgetwizard/plugingenerator.cpp b/src/plugins/qt4projectmanager/customwidgetwizard/plugingenerator.cpp
index 78b922f2089..dcfe0cd245a 100644
--- a/src/plugins/qt4projectmanager/customwidgetwizard/plugingenerator.cpp
+++ b/src/plugins/qt4projectmanager/customwidgetwizard/plugingenerator.cpp
@@ -68,6 +68,19 @@ static inline Core::GeneratedFile  generateIconFile(const QString &source, const
     return rc;
 }
 
+static QString qt4PluginExport(const QString &pluginName, const QString &pluginClassName)
+{
+    return QLatin1String("#if QT_VERSION < 0x050000\nQ_EXPORT_PLUGIN2(")
+        + pluginName + QLatin1String(", ") + pluginClassName
+        + QLatin1String(")\n#endif // QT_VERSION < 0x050000");
+}
+
+static QString qt5PluginMetaData(const QString &interfaceName)
+{
+    return QLatin1String("#if QT_VERSION >= 0x050000\n    Q_PLUGIN_METADATA(IID \"org.qt-project.Qt.")
+        + interfaceName + QLatin1String("\")\n#endif // QT_VERSION >= 0x050000");
+}
+
 QList<Core::GeneratedFile>  PluginGenerator::generatePlugin(const GenerationParameters& p, const PluginOptions &options,
                                                             QString *errorMessage)
 {
@@ -100,6 +113,9 @@ QList<Core::GeneratedFile>  PluginGenerator::generatePlugin(const GenerationPara
         sm.clear();
         sm.insert(QLatin1String("SINGLE_INCLUDE_GUARD"), headerGuard(wo.pluginHeaderFile));
         sm.insert(QLatin1String("PLUGIN_CLASS"), wo.pluginClassName);
+        sm.insert(QLatin1String("SINGLE_PLUGIN_METADATA"),
+            options.widgetOptions.count() == 1 ?
+                qt5PluginMetaData(QLatin1String("QDesignerCustomWidgetInterface")) : QString());
         const QString pluginHeaderContents = processTemplate(p.templatePath + QLatin1String("/tpl_single.h"), sm, errorMessage);
         if (pluginHeaderContents.isEmpty())
             return QList<Core::GeneratedFile>();
@@ -126,12 +142,8 @@ QList<Core::GeneratedFile>  PluginGenerator::generatePlugin(const GenerationPara
         sm.insert(QLatin1String("WIDGET_DOMXML"), cStringQuote(wo.domXml));
         sm.insert(QLatin1String("SINGLE_PLUGIN_EXPORT"),
             options.widgetOptions.count() == 1 ?
-                QLatin1String("\nQ_EXPORT_PLUGIN2(") +
-                    options.pluginName +
-                    QLatin1String(", ") +
-                    wo.pluginClassName +
-                    QLatin1Char(')') :
-                QString());
+                qt4PluginExport(options.pluginName, wo.pluginClassName) : QString());
+
         const QString pluginSourceContents = processTemplate(p.templatePath + QLatin1String("/tpl_single.cpp"), sm, errorMessage);
         if (pluginSourceContents.isEmpty())
             return QList<Core::GeneratedFile>();
@@ -222,6 +234,7 @@ QList<Core::GeneratedFile>  PluginGenerator::generatePlugin(const GenerationPara
         sm.clear();
         sm.insert(QLatin1String("COLLECTION_INCLUDE_GUARD"), headerGuard(options.collectionHeaderFile));
         sm.insert(QLatin1String("COLLECTION_PLUGIN_CLASS"), options.collectionClassName);
+        sm.insert(QLatin1String("COLLECTION_PLUGIN_METADATA"), qt5PluginMetaData(QLatin1String("QDesignerCustomWidgetCollectionInterface")));
         const QString collectionHeaderContents = processTemplate(p.templatePath + QLatin1String("/tpl_collection.h"), sm, errorMessage);
         if (collectionHeaderContents.isEmpty())
             return QList<Core::GeneratedFile>();
@@ -237,12 +250,7 @@ QList<Core::GeneratedFile>  PluginGenerator::generatePlugin(const GenerationPara
                 options.collectionHeaderFile +
                 QLatin1String("\""));
         sm.insert(QLatin1String("PLUGIN_ADDITIONS"), pluginAdditions);
-        sm.insert(QLatin1String("COLLECTION_PLUGIN_EXPORT"),
-            QLatin1String("Q_EXPORT_PLUGIN2(") +
-                options.pluginName +
-                QLatin1String(", ") +
-                options.collectionClassName +
-                QLatin1Char(')'));
+        sm.insert(QLatin1String("COLLECTION_PLUGIN_EXPORT"), qt4PluginExport(options.pluginName, options.collectionClassName));
         const QString collectionSourceFileContents = processTemplate(p.templatePath + QLatin1String("/tpl_collection.cpp"), sm, errorMessage);
         if (collectionSourceFileContents.isEmpty())
             return QList<Core::GeneratedFile>();
-- 
GitLab