From dc403237f29fcbb457719759764d2f5ec21fc948 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sun, 22 Mar 2020 15:09:43 +0100 Subject: [PATCH] [netinstall] Build model from QVariantList - As an alternative to the YAML-wranging, build the model from a QVariantList instead. - Expose this as a constructor, too. --- src/modules/netinstall/PackageModel.cpp | 57 ++++++++++++++++++++++--- src/modules/netinstall/PackageModel.h | 2 + 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/modules/netinstall/PackageModel.cpp b/src/modules/netinstall/PackageModel.cpp index 673e15f5e..0ac8116b3 100644 --- a/src/modules/netinstall/PackageModel.cpp +++ b/src/modules/netinstall/PackageModel.cpp @@ -19,6 +19,7 @@ #include "PackageModel.h" +#include "utils/Variant.h" #include "utils/Yaml.h" PackageModel::PackageModel( const YAML::Node& data, QObject* parent ) @@ -28,6 +29,13 @@ PackageModel::PackageModel( const YAML::Node& data, QObject* parent ) setupModelData( data, m_rootItem ); } +PackageModel::PackageModel( const QVariantList& data, QObject* parent ) + : QAbstractItemModel( parent ) +{ + m_rootItem = new PackageTreeItem(); + setupModelData( data, m_rootItem ); +} + PackageModel::~PackageModel() { delete m_rootItem; @@ -206,18 +214,55 @@ PackageModel::getItemPackages( PackageTreeItem* item ) const return selectedPackages; } -static QString -getString( const YAML::Node& itemDefinition, const char* key ) -{ - return itemDefinition[ key ] ? CalamaresUtils::yamlToVariant( itemDefinition[ key ] ).toString() : QString(); -} - static bool getBool( const YAML::Node& itemDefinition, const char* key ) { return itemDefinition[ key ] ? CalamaresUtils::yamlToVariant( itemDefinition[ key ] ).toBool() : false; } +void +PackageModel::setupModelData( const QVariantList& groupList, PackageTreeItem* parent ) +{ + for ( const auto& group : groupList ) + { + QVariantMap groupMap = group.toMap(); + if ( groupMap.isEmpty() ) + { + continue; + } + + PackageTreeItem* item = new PackageTreeItem( groupMap, parent ); + if ( groupMap.contains( "selected" ) ) + { + item->setSelected( CalamaresUtils::getBool( groupMap, "selected", false ) ? Qt::Checked : Qt::Unchecked ); + } + if ( groupMap.contains( "packages" ) ) + { + for ( const auto& packageName : groupMap.value( "packages" ).toStringList() ) + { + item->appendChild( new PackageTreeItem( packageName, item ) ); + } + } + if ( groupMap.contains( "subgroups" ) ) + { + QVariantList subgroups = groupMap.value( "subgroups" ).toList(); + if ( !subgroups.isEmpty() ) + { + setupModelData( subgroups, item ); + } + } + if ( item->isHidden() ) + { + m_hiddenItems.append( item ); + } + else + { + item->setCheckable( true ); + parent->appendChild( item ); + } + } +} + void PackageModel::setupModelData( const YAML::Node& data, PackageTreeItem* parent ) { diff --git a/src/modules/netinstall/PackageModel.h b/src/modules/netinstall/PackageModel.h index 09701ef7d..db41c4197 100644 --- a/src/modules/netinstall/PackageModel.h +++ b/src/modules/netinstall/PackageModel.h @@ -48,6 +48,7 @@ public: static constexpr const int MetaExpandRole = Qt::UserRole + 1; explicit PackageModel( const YAML::Node& data, QObject* parent = nullptr ); + explicit PackageModel( const QVariantList& data, QObject* parent = nullptr ); ~PackageModel() override; QVariant data( const QModelIndex& index, int role ) const override; @@ -66,6 +67,7 @@ public: private: void setupModelData( const YAML::Node& data, PackageTreeItem* parent ); + void setupModelData( const QVariantList& l, PackageTreeItem* parent ); PackageTreeItem* m_rootItem; PackageTreeItem::List m_hiddenItems;