[packagechooser,netinstall] Proper implementation of source field

This commit is contained in:
dalto 2022-01-24 17:01:16 -06:00
parent 1db217931b
commit 22c9d888b4
4 changed files with 48 additions and 21 deletions

View File

@ -76,6 +76,6 @@ NetInstallPage::onActivate()
const QVariantList groups = gs->value( "NetinstallAdd" ).toList(); const QVariantList groups = gs->value( "NetinstallAdd" ).toList();
if ( !groups.isEmpty() ) if ( !groups.isEmpty() )
{ {
m_config->model()->appendModelData( groups, "packageChooser" ); m_config->model()->appendModelData( groups );
} }
} }

View File

@ -14,6 +14,17 @@
#include "utils/Variant.h" #include "utils/Variant.h"
#include "utils/Yaml.h" #include "utils/Yaml.h"
/** @brief Appends groups to the tree
*
* Uses the data from @p groupList to add elements to the
* existing tree that m_rootItem points to. If m_rootItem
* is not valid, it does nothing
*
* Before adding anything to the model, it ensures that there
* is no existing data from the same source. If there is, that
* data is pruned first
*
*/
static void static void
setSelections2( const QStringList& selectNames, PackageTreeItem* item ) setSelections2( const QStringList& selectNames, PackageTreeItem* item )
{ {
@ -28,6 +39,28 @@ setSelections2( const QStringList& selectNames, PackageTreeItem* item )
} }
} }
/** @brief Collects all the "source" values from @p groupList
*
* Iterates over @p groupList and returns all nonempty "source"
* values from the maps.
*
*/
static QStringList
collectSources( const QVariantList& groupList )
{
QStringList sources;
for ( const QVariant& group : groupList )
{
QVariantMap groupMap = group.toMap();
if ( !groupMap[ "source" ].toString().isEmpty() )
{
sources.append( groupMap[ "source" ].toString() );
}
}
return sources;
}
PackageModel::PackageModel( QObject* parent ) PackageModel::PackageModel( QObject* parent )
: QAbstractItemModel( parent ) : QAbstractItemModel( parent )
{ {
@ -334,21 +367,26 @@ PackageModel::setupModelData( const QVariantList& l )
} }
void void
PackageModel::appendModelData( const QVariantList& groupList, const QString& source ) PackageModel::appendModelData( const QVariantList& groupList )
{ {
if ( m_rootItem ) if ( m_rootItem )
{ {
Q_EMIT beginResetModel(); Q_EMIT beginResetModel();
const QStringList sources = collectSources( groupList );
if ( !sources.isEmpty() )
{
// Prune any existing data from the same source // Prune any existing data from the same source
for ( int i = 0; i < m_rootItem->childCount(); i++ ) for ( int i = 0; i < m_rootItem->childCount(); i++ )
{ {
PackageTreeItem* child = m_rootItem->child( i ); PackageTreeItem* child = m_rootItem->child( i );
if ( child->source() == source ) if ( sources.contains( child->source() ) )
{ {
m_rootItem->removeChild( i ); m_rootItem->removeChild( i );
} }
} }
}
// Add the new data to the model // Add the new data to the model
setupModelData( groupList, m_rootItem ); setupModelData( groupList, m_rootItem );

View File

@ -68,18 +68,7 @@ public:
PackageTreeItem::List getPackages() const; PackageTreeItem::List getPackages() const;
PackageTreeItem::List getItemPackages( PackageTreeItem* item ) const; PackageTreeItem::List getItemPackages( PackageTreeItem* item ) const;
/** @brief Appends groups to the tree void appendModelData(const QVariantList& groupList);
*
* Uses the data from @p groupList to add elements to the
* existing tree that m_rootItem points to. If m_rootItem
* is not valid, it does nothing
*
* Before adding anything to the model, it ensures that there
* is no existing data from the same source. If there is, that
* data is pruned first
*
*/
void appendModelData(const QVariantList& groupList, const QString &source );
private: private:
friend class ItemTests; friend class ItemTests;

View File

@ -147,7 +147,7 @@ PackageListModel::getNetinstallDataForNames( const QStringList& ids ) const
if ( !p.netinstallData.isEmpty() ) if ( !p.netinstallData.isEmpty() )
{ {
QVariantMap newData = p.netinstallData; QVariantMap newData = p.netinstallData;
newData["source"] = "packageChooser"; newData[ "source" ] = QStringLiteral( "packageChooser" );
l.append( newData ); l.append( newData );
} }
} }