Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Generate a proper qml module plugin in qt5

Halla Rempt7f7dbc5ce222
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c18f4b4e4e..83c7ab1aff 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -426,7 +426,11 @@ include(ECMGenerateHeaders)
include(GenerateExportHeader)
include(ECMMarkAsTest)
include(ECMInstallIcons)
+include(ECMFindQmlModule)
include(ECMQmlModule)
+if (QT_MAJOR_VERSION STREQUAL "5")
+ include(ECMGenerateQmlTypes)
+endif()
include(CMakePackageConfigHelpers)
include(WriteBasicConfigVersionFile)
diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cpp
index d17502720a..bb3d688f48 100644
--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp
+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp
@@ -121,6 +121,8 @@ TextPropertiesDock::TextPropertiesDock()
setWidget(m_quickWidget);
setEnabled(true);
+ qmlRegisterModule("org.krita.qml", 1, 0);
+
m_quickWidget->engine()->rootContext()->setContextProperty("mainWindow", this);
m_quickWidget->engine()->rootContext()->setContextObject(new KLocalizedContext(this));
diff --git a/plugins/dockers/textproperties/qml/DoubleSpinBox.qml b/plugins/dockers/textproperties/qml/DoubleSpinBox.qml
deleted file mode 100644
index c1708a9660..0000000000
--- a/plugins/dockers/textproperties/qml/DoubleSpinBox.qml
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-import QtQuick 2.15
-import QtQuick.Controls 2.15
-
-
-// Simple double spinbox based on Qt example implementation, should proly be switched to spinbox slider at some point.
-SpinBox {
- editable: true;
- wheelEnabled: true;
-
- property int decimals: 2;
- property int multiplier: Math.pow(10, decimals);
-
- validator: DoubleValidator {
- bottom: Math.min(from, to)
- top: Math.max(from, to)
- }
-
- textFromValue: function(value, locale) {
- return Number(value / multiplier).toLocaleString(locale, 'f', decimals)
- }
-
- valueFromText: function(text, locale) {
- return Number.fromLocaleString(locale, text) * multiplier;
- }
-}
diff --git a/plugins/dockers/textproperties/qml/FontSizeAdjust.qml b/plugins/dockers/textproperties/qml/FontSizeAdjust.qml
index e5a621eacb..09c18c3ec7 100644
--- a/plugins/dockers/textproperties/qml/FontSizeAdjust.qml
+++ b/plugins/dockers/textproperties/qml/FontSizeAdjust.qml
@@ -7,6 +7,7 @@ import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.12
import org.krita.flake.text 1.0
+import org.krita.qml 1.0
TextPropertyBase {
propertyName: i18nc("@label:spinbox", "Font Size Adjust");
diff --git a/qmlmodules/CMakeLists.txt b/qmlmodules/CMakeLists.txt
index 6bb1046cdd..5f8a4c990a 100644
--- a/qmlmodules/CMakeLists.txt
+++ b/qmlmodules/CMakeLists.txt
@@ -1,5 +1 @@
-#add_subdirectory(types)
-#add_subdirectory(utilities)
-#add_subdirectory(resources)
-#add_subdirectory(widgets)
add_subdirectory(libqml)
diff --git a/qmlmodules/libqml/CMakeLists.txt b/qmlmodules/libqml/CMakeLists.txt
index 2043955c7a..3cc0a87cd5 100644
--- a/qmlmodules/libqml/CMakeLists.txt
+++ b/qmlmodules/libqml/CMakeLists.txt
@@ -1,27 +1,49 @@
-ecm_add_qml_module(kritaqml
- URI org.krita.qml
- VERSION 1.0
- SHARED
- GENERATE_PLUGIN_SOURCE
-)
+if (QT_MAJOR_VERSION STREQUAL "6")
+
+ ecm_add_qml_module(KritaQmlPlugin
+ URI org.krita.qml
+ VERSION 1.0
+ SHARED
+ GENERATE_PLUGIN_SOURCE
+ )
-target_sources(kritaqml
- PUBLIC
+else ()
+ ecm_add_qml_module(KritaQmlPlugin
+ URI "org.krita.qml"
+ VERSION 1.0
+ CLASSNAME KritaQmlPlugin
+ )
+endif ()
+
+target_sources(KritaQmlPlugin
+ PRIVATE
+ KritaQmlPlugin.cpp
+ KritaQmlPlugin.h
SvgTextLabel.cpp
+ SvgTextLabel.h
)
-target_link_libraries(kritaqml
- PUBLIC
- kritaui
+ecm_target_qml_sources(KritaQmlPlugin SOURCES
+ qml/DoubleSpinBox.qml
+)
+
+target_link_libraries(KritaQmlPlugin
PRIVATE
+ kritaui
+ Qt::Qml
Qt::Quick
+ Qt::QuickControls2
)
-ecm_target_qml_sources(kritaqml SOURCES
- qml/DoubleSpinBox.qml
-)
+ecm_finalize_qml_module(KritaQmlPlugin DESTINATION ${KDE_INSTALL_QMLDIR})
+
+
+if (QT_MAJOR_VERSION STREQUAL "5")
+ ecm_generate_qmltypes(org.krita.qml 1.0 DESTINATION ${KDE_INSTALL_QMLDIR}/org/krita/qml)
+endif ()
+
+
+install(TARGETS KritaQmlPlugin ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
-ecm_finalize_qml_module(kritaqml DESTINATION ${KDE_INSTALL_QMLDIR})
-install(TARGETS kritaqml ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
diff --git a/qmlmodules/libqml/KritaQmlPlugin.cpp b/qmlmodules/libqml/KritaQmlPlugin.cpp
new file mode 100644
index 0000000000..5cae9da04b
--- /dev/null
+++ b/qmlmodules/libqml/KritaQmlPlugin.cpp
@@ -0,0 +1,28 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Halla Rempt <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+
+#include "KritaQmlPlugin.h"
+
+#include <QObject>
+
+KritaQmlPlugin::KritaQmlPlugin(QObject *parent)
+ : QQmlExtensionPlugin(parent)
+{
+}
+
+void KritaQmlPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
+{
+ Q_UNUSED(engine);
+ Q_UNUSED(uri);
+}
+
+void KritaQmlPlugin::registerTypes(const char *uri)
+{
+ Q_UNUSED(uri);
+}
+
+#include "KritaQmlPlugin.moc"
+#include "moc_KritaQmlPlugin.cpp"
diff --git a/qmlmodules/libqml/KritaQmlPlugin.h b/qmlmodules/libqml/KritaQmlPlugin.h
new file mode 100644
index 0000000000..76fe643817
--- /dev/null
+++ b/qmlmodules/libqml/KritaQmlPlugin.h
@@ -0,0 +1,24 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Halla Rempt <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-or-later
+ */
+#ifndef KRITAQMLPLUGIN_H
+#define KRITAQMLPLUGIN_H
+
+#include <QUrl>
+#include <QQmlEngine>
+#include <QQmlExtensionPlugin>
+
+class KritaQmlPlugin : public QQmlExtensionPlugin
+{
+ Q_OBJECT
+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
+
+public:
+ KritaQmlPlugin(QObject *parent = nullptr);
+ void initializeEngine(QQmlEngine *engine, const char *uri) override;
+ void registerTypes(const char *uri) override;
+};
+
+#endif
diff --git a/qmlmodules/libqml/qml/DoubleSpinBox.qml b/qmlmodules/libqml/qml/DoubleSpinBox.qml
index c1708a9660..137c27fd98 100644
--- a/qmlmodules/libqml/qml/DoubleSpinBox.qml
+++ b/qmlmodules/libqml/qml/DoubleSpinBox.qml
@@ -7,7 +7,8 @@ import QtQuick 2.15
import QtQuick.Controls 2.15
-// Simple double spinbox based on Qt example implementation, should proly be switched to spinbox slider at some point.
+// Simple double spinbox based on Qt example implementation, should
+// probably be switched to spinbox slider at some point.
SpinBox {
editable: true;
wheelEnabled: true;