[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.
This commit is contained in:
Adriaan de Groot 2020-03-12 03:45:14 +01:00
parent 1c9a2e8c5f
commit 0fd7fec25e
4 changed files with 36 additions and 28 deletions

View File

@ -28,6 +28,7 @@
#include "progresstree/ProgressTreeView.h" #include "progresstree/ProgressTreeView.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Qml.h"
#include "utils/Retranslator.h" #include "utils/Retranslator.h"
#include <QApplication> #include <QApplication>
@ -133,6 +134,7 @@ CalamaresWindow::getWidgetSidebar( int desiredWidth )
QWidget* QWidget*
CalamaresWindow::getQmlSidebar( int desiredWidth ) CalamaresWindow::getQmlSidebar( int desiredWidth )
{ {
CalamaresUtils::registerCalamaresModels();
QQuickWidget* w = new QQuickWidget( this ); QQuickWidget* w = new QQuickWidget( this );
w->setSource( QUrl( ":/sidebar.qml" ) ); w->setSource( QUrl( ":/sidebar.qml" ) );
return w; return w;

View File

@ -19,6 +19,7 @@
#include "Qml.h" #include "Qml.h"
#include "Branding.h" #include "Branding.h"
#include "ViewManager.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QByteArray> #include <QByteArray>
@ -113,5 +114,25 @@ qmlSearchNames()
return names; 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 } // namespace CalamaresUtils

View File

@ -28,6 +28,12 @@ class QQuickItem;
namespace CalamaresUtils 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 /** @brief Calls the QML method @p method on @p qmlObject
* *
@ -53,17 +59,17 @@ enum class QmlSearch
Both 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 /** @brief Find a suitable QML file, given the search method and name hints
* *
* Returns QString() if nothing is found (which would mean the module * Returns QString() if nothing is found (which would mean the module
* is badly configured). * is badly configured).
*/ */
QString searchQmlFile( QmlSearch method, UIDLLEXPORT QString searchQmlFile( QmlSearch method,
const QString& configuredName, const QString& configuredName,
const Calamares::ModuleSystem::InstanceKey& i = Calamares::ModuleSystem::InstanceKey() ); const Calamares::ModuleSystem::InstanceKey& i );
///@brief Names for the search terms (in config files)
const NamedEnumTable< QmlSearch >& qmlSearchNames();
} // namespace CalamaresUtils } // namespace CalamaresUtils

View File

@ -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 namespace Calamares
{ {
@ -97,7 +76,7 @@ QmlViewStep::QmlViewStep( QObject* parent )
, m_spinner( new WaitingWidget( tr( "Loading ..." ) ) ) , m_spinner( new WaitingWidget( tr( "Loading ..." ) ) )
, m_qmlWidget( new QQuickWidget ) , m_qmlWidget( new QQuickWidget )
{ {
registerCalamaresModels(); CalamaresUtils::registerCalamaresModels();
QVBoxLayout* layout = new QVBoxLayout( m_widget ); QVBoxLayout* layout = new QVBoxLayout( m_widget );
layout->addWidget( m_spinner ); layout->addWidget( m_spinner );