[libcalamaresui] Move QML-searching
- This is utility code, so it can be in the QML "service" from Calamares, rather than in the QmlViewStep itself. That makes it usable for other QML bits as well.
This commit is contained in:
parent
7d99ad3177
commit
7a8eb09cdb
@ -18,11 +18,13 @@
|
||||
|
||||
#include "Qml.h"
|
||||
|
||||
#include "Branding.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QObject>
|
||||
#include <QQuickItem>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
|
||||
namespace CalamaresUtils
|
||||
@ -49,4 +51,67 @@ callQMLFunction( QQuickItem* qmlObject, const char* method )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
searchQmlFile( QmlSearch method, const QString& configuredName, const Calamares::ModuleSystem::InstanceKey& i )
|
||||
{
|
||||
QString bPath( QStringLiteral( "%1/%2.qml" ) );
|
||||
QString qrPath( QStringLiteral( ":/%1.qml" ) );
|
||||
|
||||
cDebug() << "Looking for QML for" << i.toString();
|
||||
QStringList candidates;
|
||||
if ( configuredName.startsWith( '/' ) )
|
||||
{
|
||||
candidates << configuredName;
|
||||
}
|
||||
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::BrandingOnly ) )
|
||||
{
|
||||
QString brandDir = Calamares::Branding::instance()->componentDirectory();
|
||||
candidates << ( configuredName.isEmpty() ? QString() : bPath.arg( brandDir, configuredName ) )
|
||||
<< bPath.arg( brandDir, i.toString() ) << bPath.arg( brandDir, i.module() );
|
||||
}
|
||||
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::QrcOnly ) )
|
||||
{
|
||||
candidates << ( configuredName.isEmpty() ? QString() : qrPath.arg( configuredName ) )
|
||||
<< qrPath.arg( i.toString() ) << qrPath.arg( i.module() );
|
||||
}
|
||||
for ( const QString& candidate : candidates )
|
||||
{
|
||||
if ( candidate.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cDebug() << Logger::SubEntry << "Looking at QML file" << candidate;
|
||||
if ( QFile::exists( candidate ) )
|
||||
{
|
||||
if ( candidate.startsWith( ':' ) )
|
||||
{
|
||||
// Inconsistency: QFile only sees the file with :,
|
||||
// but QML needs an explicit scheme (of qrc:)
|
||||
return QStringLiteral( "qrc" ) + candidate;
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
cDebug() << Logger::SubEntry << "None found.";
|
||||
return QString();
|
||||
}
|
||||
|
||||
const NamedEnumTable< QmlSearch >&
|
||||
qmlSearchNames()
|
||||
{
|
||||
// *INDENT-OFF*
|
||||
// clang-format off
|
||||
static NamedEnumTable< QmlSearch > names {
|
||||
{ QStringLiteral( "both" ), QmlSearch::Both },
|
||||
{ QStringLiteral( "qrc" ), QmlSearch::QrcOnly },
|
||||
{ QStringLiteral( "branding" ), QmlSearch::BrandingOnly }
|
||||
};
|
||||
// *INDENT-ON*
|
||||
// clang-format on
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -21,6 +21,9 @@
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include "modulesystem/InstanceKey.h"
|
||||
#include "utils/NamedEnum.h"
|
||||
|
||||
class QQuickItem;
|
||||
|
||||
namespace CalamaresUtils
|
||||
@ -34,8 +37,33 @@ namespace CalamaresUtils
|
||||
*
|
||||
* If there is a return value from the QML method, it is logged (but not otherwise used).
|
||||
*/
|
||||
UIDLLEXPORT void
|
||||
callQMLFunction( QQuickItem* qmlObject, const char* method );
|
||||
UIDLLEXPORT void callQMLFunction( QQuickItem* qmlObject, const char* method );
|
||||
|
||||
/** @brief Search modes for loading Qml files.
|
||||
*
|
||||
* A QML file could be compiled into QRC, or it could live
|
||||
* in the branding directory (and, in debug-runs, in
|
||||
* the current-directory). Modules have some control
|
||||
* over where the search is done.
|
||||
*/
|
||||
enum class QmlSearch
|
||||
{
|
||||
QrcOnly,
|
||||
BrandingOnly,
|
||||
Both
|
||||
};
|
||||
|
||||
/** @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();
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
|
@ -37,23 +37,6 @@
|
||||
#include <QWidget>
|
||||
|
||||
|
||||
static const NamedEnumTable< Calamares::QmlViewStep::QmlSearch >&
|
||||
searchNames()
|
||||
{
|
||||
using Search = Calamares::QmlViewStep::QmlSearch;
|
||||
// *INDENT-OFF*
|
||||
// clang-format off
|
||||
static NamedEnumTable< Search > names {
|
||||
{ QStringLiteral( "both" ), Search::Both },
|
||||
{ QStringLiteral( "qrc" ), Search::QrcOnly },
|
||||
{ QStringLiteral( "branding" ), Search::BrandingOnly }
|
||||
};
|
||||
// *INDENT-ON*
|
||||
// clang-format on
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
/// @brief State-change of the QML, for changeQMLState()
|
||||
enum class QMLAction
|
||||
{
|
||||
@ -244,66 +227,12 @@ QmlViewStep::showQml()
|
||||
}
|
||||
|
||||
|
||||
/** @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( QmlViewStep::QmlSearch method, const QString& configuredName, const Calamares::ModuleSystem::InstanceKey& i )
|
||||
{
|
||||
using QmlSearch = Calamares::QmlViewStep::QmlSearch;
|
||||
|
||||
QString bPath( QStringLiteral( "%1/%2.qml" ) );
|
||||
QString qrPath( QStringLiteral( ":/%1.qml" ) );
|
||||
|
||||
cDebug() << "Looking for QML for" << i.toString();
|
||||
QStringList candidates;
|
||||
if ( configuredName.startsWith( '/' ) )
|
||||
{
|
||||
candidates << configuredName;
|
||||
}
|
||||
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::BrandingOnly ) )
|
||||
{
|
||||
QString brandDir = Calamares::Branding::instance()->componentDirectory();
|
||||
candidates << ( configuredName.isEmpty() ? QString()
|
||||
: bPath.arg( brandDir, configuredName ) )
|
||||
<< bPath.arg( brandDir, i.toString() )
|
||||
<< bPath.arg( brandDir, i.module() );
|
||||
}
|
||||
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::QrcOnly ) )
|
||||
{
|
||||
candidates << ( configuredName.isEmpty() ? QString() : qrPath.arg( configuredName ) )
|
||||
<< qrPath.arg( i.toString() )
|
||||
<< qrPath.arg( i.module() );
|
||||
}
|
||||
for ( const QString& candidate : candidates )
|
||||
{
|
||||
if ( candidate.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cDebug() << Logger::SubEntry << "Looking at QML file" << candidate;
|
||||
if ( QFile::exists( candidate ) )
|
||||
{
|
||||
if ( candidate.startsWith( ':' ) )
|
||||
{
|
||||
// Inconsistency: QFile only sees the file with :,
|
||||
// but QML needs an explicit scheme (of qrc:)
|
||||
return QStringLiteral( "qrc" ) + candidate;
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
cDebug() << Logger::SubEntry << "None found.";
|
||||
return QString();
|
||||
}
|
||||
|
||||
void
|
||||
QmlViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
||||
{
|
||||
bool ok = false;
|
||||
m_searchMethod = searchNames().find( CalamaresUtils::getString( configurationMap, "qmlSearch" ), ok );
|
||||
m_searchMethod
|
||||
= CalamaresUtils::qmlSearchNames().find( CalamaresUtils::getString( configurationMap, "qmlSearch" ), ok );
|
||||
if ( !ok )
|
||||
{
|
||||
cDebug() << "Bad QML search mode.";
|
||||
|
@ -19,6 +19,7 @@
|
||||
#ifndef QMLVIEWSTEP_H
|
||||
#define QMLVIEWSTEP_H
|
||||
|
||||
#include "utils/Qml.h"
|
||||
#include "viewpages/ViewStep.h"
|
||||
|
||||
class QQmlComponent;
|
||||
@ -38,15 +39,7 @@ namespace Calamares
|
||||
class QmlViewStep : public Calamares::ViewStep
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum class QmlSearch
|
||||
{
|
||||
QrcOnly,
|
||||
BrandingOnly,
|
||||
Both
|
||||
};
|
||||
|
||||
/** @brief Creates a QML view step
|
||||
*
|
||||
* The search behavior for the actial QML depends on a QmlSearch value.
|
||||
@ -98,7 +91,7 @@ private:
|
||||
void showFailedQml();
|
||||
|
||||
/// @brief Controls where m_name is searched
|
||||
QmlSearch m_searchMethod;
|
||||
CalamaresUtils::QmlSearch m_searchMethod;
|
||||
|
||||
QString m_name;
|
||||
QString m_qmlFileName;
|
||||
|
Loading…
Reference in New Issue
Block a user