From 0fd7fec25ea6aeac54d83dac8c90ba6cf949a2fa Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 12 Mar 2020 03:45:14 +0100 Subject: [PATCH] [libcalamaresui] Move registration into Qml-service - Registration of QML modules may need to be done for more parts of Calamares. Move into the library, out of the model. - Register for QML when using the QML sidebar. --- src/calamares/CalamaresWindow.cpp | 2 ++ src/libcalamaresui/utils/Qml.cpp | 21 ++++++++++++++++++ src/libcalamaresui/utils/Qml.h | 18 ++++++++++----- src/libcalamaresui/viewpages/QmlViewStep.cpp | 23 +------------------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/calamares/CalamaresWindow.cpp b/src/calamares/CalamaresWindow.cpp index f65e0acdf..33bf66373 100644 --- a/src/calamares/CalamaresWindow.cpp +++ b/src/calamares/CalamaresWindow.cpp @@ -28,6 +28,7 @@ #include "progresstree/ProgressTreeView.h" #include "utils/CalamaresUtilsGui.h" #include "utils/Logger.h" +#include "utils/Qml.h" #include "utils/Retranslator.h" #include @@ -133,6 +134,7 @@ CalamaresWindow::getWidgetSidebar( int desiredWidth ) QWidget* CalamaresWindow::getQmlSidebar( int desiredWidth ) { + CalamaresUtils::registerCalamaresModels(); QQuickWidget* w = new QQuickWidget( this ); w->setSource( QUrl( ":/sidebar.qml" ) ); return w; diff --git a/src/libcalamaresui/utils/Qml.cpp b/src/libcalamaresui/utils/Qml.cpp index c64578661..5dca2fba1 100644 --- a/src/libcalamaresui/utils/Qml.cpp +++ b/src/libcalamaresui/utils/Qml.cpp @@ -19,6 +19,7 @@ #include "Qml.h" #include "Branding.h" +#include "ViewManager.h" #include "utils/Logger.h" #include @@ -113,5 +114,25 @@ qmlSearchNames() return names; } +void +registerCalamaresModels() +{ + static bool done = false; + if ( !done ) + { + done = true; + // Because branding and viewmanager have a parent (CalamaresApplication + // and CalamaresWindow), they will not be deleted by QmlEngine. + // https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership + qmlRegisterSingletonType< Calamares::Branding >( + "io.calamares.ui", 1, 0, "Branding", []( QQmlEngine*, QJSEngine* ) -> QObject* { + return Calamares::Branding::instance(); + } ); + qmlRegisterSingletonType< Calamares::Branding >( + "io.calamares.core", 1, 0, "ViewManager", []( QQmlEngine*, QJSEngine* ) -> QObject* { + return Calamares::ViewManager::instance(); + } ); + } +} } // namespace CalamaresUtils diff --git a/src/libcalamaresui/utils/Qml.h b/src/libcalamaresui/utils/Qml.h index 149dc79ef..f859add21 100644 --- a/src/libcalamaresui/utils/Qml.h +++ b/src/libcalamaresui/utils/Qml.h @@ -28,6 +28,12 @@ class QQuickItem; namespace CalamaresUtils { +/** @brief Sets up global Calamares models for QML + * + * This needs to be called at least once to make the global Calamares + * models (Branding, ViewManager, ...) available to QML. + */ +UIDLLEXPORT void registerCalamaresModels(); /** @brief Calls the QML method @p method on @p qmlObject * @@ -53,17 +59,17 @@ enum class QmlSearch Both }; +///@brief Names for the search terms (in config files) +UIDLLEXPORT const NamedEnumTable< QmlSearch >& qmlSearchNames(); + /** @brief Find a suitable QML file, given the search method and name hints * * Returns QString() if nothing is found (which would mean the module * is badly configured). */ -QString searchQmlFile( QmlSearch method, - const QString& configuredName, - const Calamares::ModuleSystem::InstanceKey& i = Calamares::ModuleSystem::InstanceKey() ); - -///@brief Names for the search terms (in config files) -const NamedEnumTable< QmlSearch >& qmlSearchNames(); +UIDLLEXPORT QString searchQmlFile( QmlSearch method, + const QString& configuredName, + const Calamares::ModuleSystem::InstanceKey& i ); } // namespace CalamaresUtils diff --git a/src/libcalamaresui/viewpages/QmlViewStep.cpp b/src/libcalamaresui/viewpages/QmlViewStep.cpp index a437c2aeb..468ede962 100644 --- a/src/libcalamaresui/viewpages/QmlViewStep.cpp +++ b/src/libcalamaresui/viewpages/QmlViewStep.cpp @@ -67,27 +67,6 @@ changeQMLState( QMLAction action, QQuickItem* item ) } } -static void -registerCalamaresModels() -{ - static bool done = false; - if ( !done ) - { - done = true; - // Because branding and viewmanager have a parent (CalamaresApplication - // and CalamaresWindow), they will not be deleted by QmlEngine. - // https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership - qmlRegisterSingletonType< Calamares::Branding >( - "io.calamares.ui", 1, 0, "Branding", []( QQmlEngine*, QJSEngine* ) -> QObject* { - return Calamares::Branding::instance(); - } ); - qmlRegisterSingletonType< Calamares::Branding >( - "io.calamares.core", 1, 0, "ViewManager", []( QQmlEngine*, QJSEngine* ) -> QObject* { - return Calamares::ViewManager::instance(); - } ); - } -} - namespace Calamares { @@ -97,7 +76,7 @@ QmlViewStep::QmlViewStep( QObject* parent ) , m_spinner( new WaitingWidget( tr( "Loading ..." ) ) ) , m_qmlWidget( new QQuickWidget ) { - registerCalamaresModels(); + CalamaresUtils::registerCalamaresModels(); QVBoxLayout* layout = new QVBoxLayout( m_widget ); layout->addWidget( m_spinner );