2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2017-01-23 13:42:40 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2017, Kyle Robbertze <kyle@aims.ac.za>
|
2020-03-24 12:02:16 +01:00
|
|
|
* Copyright 2017-2018, 2020, Adriaan de Groot <groot@kde.org>
|
2017-01-23 13:42:40 +01:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PackageModel.h"
|
|
|
|
|
2020-03-22 15:09:43 +01:00
|
|
|
#include "utils/Variant.h"
|
2019-04-29 12:04:55 +02:00
|
|
|
#include "utils/Yaml.h"
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-03-24 12:36:31 +01:00
|
|
|
PackageModel::PackageModel( QObject* parent )
|
2020-02-18 11:02:53 +01:00
|
|
|
: QAbstractItemModel( parent )
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-03-22 15:09:43 +01:00
|
|
|
}
|
|
|
|
|
2017-01-23 13:42:40 +01:00
|
|
|
PackageModel::~PackageModel()
|
|
|
|
{
|
|
|
|
delete m_rootItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PackageModel::index( int row, int column, const QModelIndex& parent ) const
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem || !hasIndex( row, column, parent ) )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QModelIndex();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
PackageTreeItem* parentItem;
|
|
|
|
|
|
|
|
if ( !parent.isValid() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
parentItem = m_rootItem;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
|
|
|
parentItem = static_cast< PackageTreeItem* >( parent.internalPointer() );
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
PackageTreeItem* childItem = parentItem->child( row );
|
|
|
|
if ( childItem )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return createIndex( row, column, childItem );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QModelIndex();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PackageModel::parent( const QModelIndex& index ) const
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem || !index.isValid() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QModelIndex();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
PackageTreeItem* child = static_cast< PackageTreeItem* >( index.internalPointer() );
|
2017-01-23 13:42:40 +01:00
|
|
|
PackageTreeItem* parent = child->parentItem();
|
|
|
|
|
|
|
|
if ( parent == m_rootItem )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QModelIndex();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
return createIndex( parent->row(), 0, parent );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PackageModel::rowCount( const QModelIndex& parent ) const
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem || ( parent.column() > 0 ) )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return 0;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
PackageTreeItem* parentItem;
|
|
|
|
if ( !parent.isValid() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
parentItem = m_rootItem;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
|
|
|
parentItem = static_cast< PackageTreeItem* >( parent.internalPointer() );
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
return parentItem->childCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-27 23:19:55 +01:00
|
|
|
PackageModel::columnCount( const QModelIndex& ) const
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-02-19 01:24:24 +01:00
|
|
|
return 2;
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
PackageModel::data( const QModelIndex& index, int role ) const
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem || !index.isValid() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QVariant();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-25 09:34:18 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
PackageTreeItem* item = static_cast< PackageTreeItem* >( index.internalPointer() );
|
2020-03-10 18:46:49 +01:00
|
|
|
switch ( role )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-10 18:46:49 +01:00
|
|
|
case Qt::CheckStateRole:
|
2020-03-27 16:47:33 +01:00
|
|
|
return index.column() == NameColumn ? ( item->isImmutable() ? QVariant() : item->isSelected() ) : QVariant();
|
2020-03-10 18:46:49 +01:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
return item->isHidden() ? QVariant() : item->data( index.column() );
|
2020-03-10 18:59:41 +01:00
|
|
|
case MetaExpandRole:
|
|
|
|
return item->isHidden() ? false : item->expandOnStart();
|
2020-03-10 18:46:49 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PackageModel::setData( const QModelIndex& index, const QVariant& value, int role )
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:42:40 +01:00
|
|
|
if ( role == Qt::CheckStateRole && index.isValid() )
|
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
PackageTreeItem* item = static_cast< PackageTreeItem* >( index.internalPointer() );
|
|
|
|
item->setSelected( static_cast< Qt::CheckState >( value.toInt() ) );
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
emit dataChanged( this->index( 0, 0 ),
|
|
|
|
index.sibling( index.column(), index.row() + 1 ),
|
|
|
|
QVector< int >( Qt::CheckStateRole ) );
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags
|
|
|
|
PackageModel::flags( const QModelIndex& index ) const
|
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem || !index.isValid() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2018-01-13 21:19:08 +01:00
|
|
|
return Qt::ItemFlags();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2020-03-10 18:22:56 +01:00
|
|
|
if ( index.column() == NameColumn )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-27 16:47:33 +01:00
|
|
|
PackageTreeItem* item = static_cast< PackageTreeItem* >( index.internalPointer() );
|
|
|
|
if ( item->isImmutable() )
|
|
|
|
{
|
|
|
|
return QAbstractItemModel::flags( index ); //Qt::NoItemFlags;
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
return Qt::ItemIsUserCheckable | QAbstractItemModel::flags( index );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
return QAbstractItemModel::flags( index );
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
PackageModel::headerData( int section, Qt::Orientation orientation, int role ) const
|
|
|
|
{
|
|
|
|
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-10 18:22:56 +01:00
|
|
|
return ( section == NameColumn ) ? tr( "Name" ) : tr( "Description" );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:03:47 +01:00
|
|
|
PackageTreeItem::List
|
2017-01-25 09:34:18 +01:00
|
|
|
PackageModel::getPackages() const
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
if ( !m_rootItem )
|
|
|
|
{
|
|
|
|
return PackageTreeItem::List();
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:03:47 +01:00
|
|
|
auto items = getItemPackages( m_rootItem );
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( auto package : m_hiddenItems )
|
2020-03-20 23:03:47 +01:00
|
|
|
{
|
2017-11-21 12:13:42 +01:00
|
|
|
if ( package->hiddenSelected() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-21 12:13:42 +01:00
|
|
|
items.append( getItemPackages( package ) );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
2020-03-20 23:03:47 +01:00
|
|
|
return items;
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-20 23:03:47 +01:00
|
|
|
PackageTreeItem::List
|
2017-01-25 09:34:18 +01:00
|
|
|
PackageModel::getItemPackages( PackageTreeItem* item ) const
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-03-24 12:36:31 +01:00
|
|
|
PackageTreeItem::List selectedPackages;
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( int i = 0; i < item->childCount(); i++ )
|
|
|
|
{
|
2020-03-27 20:36:34 +01:00
|
|
|
auto* child = item->child( i );
|
|
|
|
if ( child->isSelected() == Qt::Unchecked )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
continue;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-03-27 20:36:34 +01:00
|
|
|
if ( child->isPackage() ) // package
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-27 20:36:34 +01:00
|
|
|
selectedPackages.append( child );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-27 20:36:34 +01:00
|
|
|
selectedPackages.append( getItemPackages( child ) );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
return selectedPackages;
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:09:43 +01:00
|
|
|
void
|
|
|
|
PackageModel::setupModelData( const QVariantList& groupList, PackageTreeItem* parent )
|
|
|
|
{
|
|
|
|
for ( const auto& group : groupList )
|
|
|
|
{
|
|
|
|
QVariantMap groupMap = group.toMap();
|
|
|
|
if ( groupMap.isEmpty() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:33:59 +02:00
|
|
|
PackageTreeItem* item = new PackageTreeItem( groupMap, PackageTreeItem::GroupTag { parent } );
|
2020-03-22 15:09:43 +01:00
|
|
|
if ( groupMap.contains( "selected" ) )
|
|
|
|
{
|
|
|
|
item->setSelected( CalamaresUtils::getBool( groupMap, "selected", false ) ? Qt::Checked : Qt::Unchecked );
|
|
|
|
}
|
|
|
|
if ( groupMap.contains( "packages" ) )
|
|
|
|
{
|
2020-04-16 22:33:59 +02:00
|
|
|
for ( const auto& packageName : groupMap.value( "packages" ).toList() )
|
2020-03-22 15:09:43 +01:00
|
|
|
{
|
2020-04-16 22:33:59 +02:00
|
|
|
if ( packageName.type() == QVariant::String )
|
|
|
|
{
|
|
|
|
item->appendChild( new PackageTreeItem( packageName.toString(), item ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QVariantMap m = packageName.toMap();
|
|
|
|
if ( !m.isEmpty() )
|
|
|
|
{
|
|
|
|
item->appendChild( new PackageTreeItem( m, PackageTreeItem::PackageTag { item } ) );
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 15:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( groupMap.contains( "subgroups" ) )
|
|
|
|
{
|
|
|
|
QVariantList subgroups = groupMap.value( "subgroups" ).toList();
|
|
|
|
if ( !subgroups.isEmpty() )
|
|
|
|
{
|
|
|
|
setupModelData( subgroups, item );
|
2020-03-27 17:28:32 +01:00
|
|
|
// The children might be checked while the parent isn't (yet).
|
|
|
|
// Children are added to their parent (below) without affecting
|
|
|
|
// the checked-state -- do it manually.
|
|
|
|
item->updateSelected();
|
2020-03-22 15:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( item->isHidden() )
|
|
|
|
{
|
|
|
|
m_hiddenItems.append( item );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->setCheckable( true );
|
|
|
|
parent->appendChild( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 12:36:31 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
PackageModel::setupModelData( const QVariantList& l )
|
|
|
|
{
|
|
|
|
emit beginResetModel();
|
|
|
|
delete m_rootItem;
|
|
|
|
m_rootItem = new PackageTreeItem();
|
|
|
|
setupModelData( l, m_rootItem );
|
|
|
|
emit endResetModel();
|
|
|
|
}
|