[libcalamares] Start a 'presets' configuration datastructure

This commit is contained in:
Adriaan de Groot 2021-03-12 13:16:36 +01:00
parent 63fc1ecca3
commit d9f2f5e988
3 changed files with 107 additions and 0 deletions

View File

@ -51,6 +51,7 @@ set( libSources
modulesystem/Descriptor.cpp
modulesystem/InstanceKey.cpp
modulesystem/Module.cpp
modulesystem/Preset.cpp
modulesystem/RequirementsChecker.cpp
modulesystem/RequirementsModel.cpp

View File

@ -0,0 +1,49 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#include "Preset.h"
#include "utils/Logger.h"
#include "utils/Variant.h"
static void
loadPresets( Calamares::ModuleSystem::Presets& preset,
const QVariantMap& configurationMap,
std::function< bool( const QString& ) > pred )
{
cDebug() << "Creating presets" << preset.capacity();
for ( auto it = configurationMap.cbegin(); it != configurationMap.cend(); ++it )
{
if ( !it.key().isEmpty() && pred( it.key() ) )
{
QVariantMap m = it.value().toMap();
QString value = CalamaresUtils::getString( m, "value" );
bool editable = CalamaresUtils::getBool( m, "editable", true );
preset.append( Calamares::ModuleSystem::PresetField { it.key(), value, editable } );
cDebug() << Logger::SubEntry << "Preset for" << it.key() << "applied editable?" << editable;
}
}
}
Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap )
{
reserve( configurationMap.count() );
loadPresets( *this, configurationMap, []( const QString& ) { return true; } );
}
Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys )
{
reserve( recognizedKeys.size() );
loadPresets(
*this, configurationMap, [&recognizedKeys]( const QString& s ) { return recognizedKeys.contains( s ); } );
}

View File

@ -0,0 +1,57 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#ifndef CALAMARES_MODULESYSTEM_PRESET_H
#define CALAMARES_MODULESYSTEM_PRESET_H
#include <QString>
#include <QVariantMap>
#include <QVector>
namespace Calamares
{
namespace ModuleSystem
{
/** @brief The settings for a single field
*
* The settings apply to a single field; **often** this will
* correspond to a single value or property of a Config
* object, but there is no guarantee of a correspondence
* between names here and names in the code.
*
* The value is stored as a string; consumers (e.g. the UI)
* will need to translate the value to whatever is actually
* used (e.g. in the case of an integer field).
*
* By default, presets are still editable. Set that to @c false
* to make the field unchangeable (again, the UI is responsible
* for setting that up).
*/
struct PresetField
{
QString fieldName;
QString value;
bool editable = true;
};
/** @brief All the presets for one UI entity
*
* This is a collection of presets read from a module
* configuration file, one setting per field.
*/
class Presets : public QVector< PresetField >
{
public:
explicit Presets( const QVariantMap& configurationMap );
Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys );
};
} // namespace ModuleSystem
} // namespace Calamares
#endif