Description: Support building as QML module.
Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
Forwarded: pending

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ded7bc..6a31c5e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,8 +6,10 @@
 cmake_minimum_required(VERSION 3.0.0)
 project(quickflux VERSION 1.1.3)
 
-option(quickflux_INSTALL "Enable the installation of targets." ON)
+set(CMAKE_AUTOMOC TRUE)
 
+option(quickflux_INSTALL "Enable the installation of targets." ON)
+option(BUILD_QML_MODULE "Build QML module" OFF)
 if(MSVC)
   set_property (GLOBAL PROPERTY USE_FOLDERS ON)
 endif()
@@ -97,6 +99,10 @@ add_library(quickflux STATIC
   )
 add_library(QuickFlux::quickflux ALIAS quickflux)
 
+target_compile_definitions(quickflux PUBLIC
+    QUICK_FLUX_DISABLE_AUTO_QML_REGISTER
+)
+
 target_link_libraries(quickflux
   PUBLIC
   Qt5::Qml
@@ -168,4 +174,23 @@ if(quickflux_INSTALL)
       DESTINATION ${CONFIG_PACKAGE_LOCATION}
   )
 
+  if(BUILD_QML_MODULE)
+    # Qt5's cmake does not export QT_IMPORTS_DIR, lets query qmake on our own for now
+    get_target_property(QMAKE_EXECUTABLE Qt5::qmake LOCATION)
+    function(QUERY_QMAKE VAR RESULT)
+      execute_process(
+        COMMAND ${QMAKE_EXECUTABLE} -query "${VAR}"
+        RESULT_VARIABLE return_code
+        OUTPUT_VARIABLE output
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+      )
+      if(NOT return_code)
+        file(TO_CMAKE_PATH "${output}" output)
+        set(${RESULT} ${output} PARENT_SCOPE)
+      endif(NOT return_code)
+    endfunction(QUERY_QMAKE)
+
+    add_subdirectory(qml)
+  endif()
+
 endif()
diff --git a/qml/CMakeLists.txt b/qml/CMakeLists.txt
new file mode 100644
index 0000000..e27fb64
--- /dev/null
+++ b/qml/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(QuickFlux)
diff --git a/qml/QuickFlux/CMakeLists.txt b/qml/QuickFlux/CMakeLists.txt
new file mode 100644
index 0000000..aadb319
--- /dev/null
+++ b/qml/QuickFlux/CMakeLists.txt
@@ -0,0 +1,19 @@
+query_qmake(QT_INSTALL_QML QT_IMPORTS_DIR)
+
+set(PLUGIN_DIR ${QT_IMPORTS_DIR}/QuickFlux)
+
+add_library(quickflux_plugin
+    MODULE
+    quickflux_plugin.cpp
+)
+
+target_link_libraries(quickflux_plugin
+    PRIVATE
+    QuickFlux::quickflux
+)
+
+install(TARGETS quickflux_plugin
+    LIBRARY DESTINATION "${PLUGIN_DIR}"
+)
+
+install(FILES qmldir DESTINATION "${PLUGIN_DIR}")
diff --git a/qml/QuickFlux/qmldir b/qml/QuickFlux/qmldir
new file mode 100644
index 0000000..7f52543
--- /dev/null
+++ b/qml/QuickFlux/qmldir
@@ -0,0 +1,2 @@
+module QuickFlux
+plugin quickflux_plugin
diff --git a/qml/QuickFlux/quickflux_plugin.cpp b/qml/QuickFlux/quickflux_plugin.cpp
new file mode 100644
index 0000000..5b7b2d7
--- /dev/null
+++ b/qml/QuickFlux/quickflux_plugin.cpp
@@ -0,0 +1,27 @@
+#include "quickflux_plugin.h"
+
+#include <QtQml/QtQml>
+#include <QtQml/QQmlContext>
+
+#include "QuickFlux"
+#include "QFAppDispatcher"
+
+static QObject *appDispatcherProvider(QQmlEngine *engine, QJSEngine *scriptEngine) {
+    Q_UNUSED(engine);
+    Q_UNUSED(scriptEngine);
+
+    QFAppDispatcher* object = new QFAppDispatcher();
+    object->setEngine(engine);
+
+    return object;
+}
+
+void QuickFluxPlugin::registerTypes(const char *uri)
+{
+    registerQuickFluxQmlTypes();
+}
+
+void QuickFluxPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
+{
+    QQmlExtensionPlugin::initializeEngine(engine, uri);
+}
diff --git a/qml/QuickFlux/quickflux_plugin.h b/qml/QuickFlux/quickflux_plugin.h
new file mode 100644
index 0000000..42cc45f
--- /dev/null
+++ b/qml/QuickFlux/quickflux_plugin.h
@@ -0,0 +1,17 @@
+#ifndef QUICKFLUX_PLUGIN_H
+#define QUICKFLUX_PLUGIN_H
+
+#include <QtQml/QQmlEngine>
+#include <QtQml/QQmlExtensionPlugin>
+
+class QuickFluxPlugin : public QQmlExtensionPlugin
+{
+    Q_OBJECT
+    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
+
+public:
+    void registerTypes(const char *uri);
+    void initializeEngine(QQmlEngine *engine, const char *uri);
+};
+
+#endif // QUICKFLUX_PLUGIN_H
