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>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2017-2018, 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"
|
|
|
|
|
2019-04-29 12:04:55 +02:00
|
|
|
#include "utils/Yaml.h"
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
PackageModel::PackageModel( const YAML::Node& data, QObject* parent )
|
|
|
|
: QAbstractItemModel( parent )
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2017-01-25 09:34:18 +01:00
|
|
|
m_rootItem = new PackageTreeItem();
|
2017-01-23 13:42:40 +01:00
|
|
|
setupModelData( data, m_rootItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageModel::~PackageModel()
|
|
|
|
{
|
|
|
|
delete m_rootItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PackageModel::index( int row, int column, const QModelIndex& parent ) const
|
|
|
|
{
|
2017-01-23 14:04:07 +01:00
|
|
|
if ( !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
|
|
|
|
{
|
|
|
|
if ( !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
|
|
|
|
{
|
|
|
|
if ( 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
|
|
|
|
{
|
|
|
|
if ( !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() );
|
2017-01-23 13:42:40 +01:00
|
|
|
if ( index.column() == 0 && role == Qt::CheckStateRole )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return item->isSelected();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( item->isHidden() && role == Qt::DisplayRole ) // Hidden group
|
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
return QVariant();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2017-01-25 09:34:18 +01:00
|
|
|
if ( role == Qt::DisplayRole )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 14:04:07 +01:00
|
|
|
return item->data( index.column() );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-25 09:34:18 +01:00
|
|
|
return QVariant();
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PackageModel::setData( const QModelIndex& index, const QVariant& value, int role )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if ( !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
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
if ( index.column() == 0 )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
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 04:01:39 +01:00
|
|
|
return ( section == 0 ) ? tr( "Name" ) : tr( "Description" );
|
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
|
|
|
QList< PackageTreeItem::ItemData >
|
2017-01-25 09:34:18 +01:00
|
|
|
PackageModel::getPackages() const
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
QList< PackageTreeItem* > items = getItemPackages( m_rootItem );
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( auto package : m_hiddenItems )
|
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
|
|
|
}
|
|
|
|
QList< PackageTreeItem::ItemData > packages;
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( auto item : items )
|
|
|
|
{
|
2017-01-25 09:34:18 +01:00
|
|
|
PackageTreeItem::ItemData itemData;
|
2020-02-18 11:02:53 +01:00
|
|
|
itemData.preScript = item->parentItem()->preScript(); // Only groups have hooks
|
|
|
|
itemData.packageName = item->packageName(); // this seg faults
|
|
|
|
itemData.postScript = item->parentItem()->postScript(); // Only groups have hooks
|
|
|
|
itemData.isCritical = item->parentItem()->isCritical(); // Only groups are critical
|
2017-01-25 09:34:18 +01:00
|
|
|
packages.append( itemData );
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
return packages;
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
QList< PackageTreeItem* >
|
2017-01-25 09:34:18 +01:00
|
|
|
PackageModel::getItemPackages( PackageTreeItem* item ) const
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
QList< PackageTreeItem* > selectedPackages;
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( int i = 0; i < item->childCount(); i++ )
|
|
|
|
{
|
2017-01-25 09:34:18 +01:00
|
|
|
if ( item->child( i )->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-02-18 11:02:53 +01:00
|
|
|
if ( !item->child( i )->childCount() ) // package
|
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
selectedPackages.append( item->child( i ) );
|
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-25 09:34:18 +01:00
|
|
|
selectedPackages.append( getItemPackages( item->child( i ) ) );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
return selectedPackages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PackageModel::setupModelData( const YAML::Node& data, PackageTreeItem* parent )
|
|
|
|
{
|
|
|
|
for ( YAML::const_iterator it = data.begin(); it != data.end(); ++it )
|
|
|
|
{
|
|
|
|
const YAML::Node itemDefinition = *it;
|
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
QString name( tr( CalamaresUtils::yamlToVariant( itemDefinition[ "name" ] ).toByteArray() ) );
|
|
|
|
QString description( tr( CalamaresUtils::yamlToVariant( itemDefinition[ "description" ] ).toByteArray() ) );
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
PackageTreeItem::ItemData itemData;
|
|
|
|
itemData.name = name;
|
|
|
|
itemData.description = description;
|
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "pre-install" ] )
|
2020-02-18 11:39:54 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
itemData.preScript = CalamaresUtils::yamlToVariant( itemDefinition[ "pre-install" ] ).toString();
|
2020-02-18 11:39:54 +01:00
|
|
|
}
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "post-install" ] )
|
2020-02-18 11:39:54 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
itemData.postScript = CalamaresUtils::yamlToVariant( itemDefinition[ "post-install" ] ).toString();
|
2020-02-18 11:39:54 +01:00
|
|
|
}
|
2017-01-23 14:04:07 +01:00
|
|
|
PackageTreeItem* item = new PackageTreeItem( itemData, parent );
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "selected" ] )
|
|
|
|
item->setSelected( CalamaresUtils::yamlToVariant( itemDefinition[ "selected" ] ).toBool() ? Qt::Checked
|
|
|
|
: Qt::Unchecked );
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
|
|
|
item->setSelected( parent->isSelected() ); // Inherit from it's parent
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "hidden" ] )
|
2020-02-18 11:39:54 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
item->setHidden( CalamaresUtils::yamlToVariant( itemDefinition[ "hidden" ] ).toBool() );
|
2020-02-18 11:39:54 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "critical" ] )
|
2020-02-18 11:39:54 +01:00
|
|
|
{
|
2020-02-18 11:02:53 +01:00
|
|
|
item->setCritical( CalamaresUtils::yamlToVariant( itemDefinition[ "critical" ] ).toBool() );
|
2020-02-18 11:39:54 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "packages" ] )
|
|
|
|
for ( YAML::const_iterator packageIt = itemDefinition[ "packages" ].begin();
|
|
|
|
packageIt != itemDefinition[ "packages" ].end();
|
|
|
|
++packageIt )
|
2017-02-04 12:36:20 +01:00
|
|
|
item->appendChild(
|
|
|
|
new PackageTreeItem( CalamaresUtils::yamlToVariant( *packageIt ).toString(), item ) );
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( itemDefinition[ "subgroups" ] )
|
|
|
|
{
|
|
|
|
setupModelData( itemDefinition[ "subgroups" ], item );
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
if ( item->isHidden() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-01-23 13:42:40 +01:00
|
|
|
m_hiddenItems.append( item );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
item->setCheckable( true );
|
|
|
|
parent->appendChild( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|