2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2017-01-23 13:42:40 +01:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <kyle@aims.ac.za>
|
|
|
|
* SPDX-FileCopyrightText: 2017 2020, Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2017-01-23 13:42:40 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2017-01-23 13:42:40 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PackageTreeItem.h"
|
|
|
|
|
2017-11-24 13:52:52 +01:00
|
|
|
#include "utils/Logger.h"
|
2020-03-20 23:03:47 +01:00
|
|
|
#include "utils/Variant.h"
|
2017-11-24 13:52:52 +01:00
|
|
|
|
2020-03-27 20:51:52 +01:00
|
|
|
/** @brief Should a package be selected, given its parent's state? */
|
2020-03-20 23:03:47 +01:00
|
|
|
static Qt::CheckState
|
|
|
|
parentCheckState( PackageTreeItem* parent )
|
2017-01-25 09:34:18 +01:00
|
|
|
{
|
2020-03-20 23:03:47 +01:00
|
|
|
if ( parent )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
// Avoid partially-checked .. a package can't be partial
|
2020-03-20 23:03:47 +01:00
|
|
|
return parent->isSelected() == Qt::Unchecked ? Qt::Unchecked : Qt::Checked;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-25 09:34:18 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-20 23:03:47 +01:00
|
|
|
return Qt::Unchecked;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-25 09:34:18 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
2020-03-27 20:51:52 +01:00
|
|
|
/** @brief Should a subgroup be marked critical?
|
|
|
|
*
|
|
|
|
* If set explicitly, then use that, otherwise use the parent's critical-ness.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
parentCriticality( const QVariantMap& groupData, PackageTreeItem* parent )
|
|
|
|
{
|
|
|
|
if ( groupData.contains( "critical" ) )
|
|
|
|
{
|
|
|
|
return CalamaresUtils::getBool( groupData, "critical", false );
|
|
|
|
}
|
|
|
|
return parent ? parent->isCritical() : false;
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:03:47 +01:00
|
|
|
PackageTreeItem::PackageTreeItem( const QString& packageName, PackageTreeItem* parent )
|
|
|
|
: m_parentItem( parent )
|
|
|
|
, m_packageName( packageName )
|
|
|
|
, m_selected( parentCheckState( parent ) )
|
2020-03-23 09:59:03 +01:00
|
|
|
, m_isGroup( false )
|
2020-03-27 20:45:04 +01:00
|
|
|
, m_isCritical( parent ? parent->isCritical() : false )
|
2020-03-27 16:47:33 +01:00
|
|
|
, m_showReadOnly( parent ? parent->isImmutable() : false )
|
2020-03-20 23:03:47 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:33:59 +02:00
|
|
|
PackageTreeItem::PackageTreeItem( const QVariantMap& groupData, PackageTag&& parent )
|
|
|
|
: m_parentItem( parent.parent )
|
|
|
|
, m_packageName( CalamaresUtils::getString( groupData, "name" ) )
|
|
|
|
, m_selected( parentCheckState( parent.parent ) )
|
|
|
|
, m_description( CalamaresUtils::getString( groupData, "description" ) )
|
|
|
|
, m_isGroup( false )
|
|
|
|
, m_isCritical( parent.parent ? parent.parent->isCritical() : false )
|
|
|
|
, m_showReadOnly( parent.parent ? parent.parent->isImmutable() : false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageTreeItem::PackageTreeItem( const QVariantMap& groupData, GroupTag&& parent )
|
|
|
|
: m_parentItem( parent.parent )
|
2020-03-20 23:03:47 +01:00
|
|
|
, m_name( CalamaresUtils::getString( groupData, "name" ) )
|
2020-04-16 22:33:59 +02:00
|
|
|
, m_selected( parentCheckState( parent.parent ) )
|
2020-03-20 23:03:47 +01:00
|
|
|
, m_description( CalamaresUtils::getString( groupData, "description" ) )
|
|
|
|
, m_preScript( CalamaresUtils::getString( groupData, "pre-install" ) )
|
|
|
|
, m_postScript( CalamaresUtils::getString( groupData, "post-install" ) )
|
2022-01-23 20:58:10 +01:00
|
|
|
, m_source( CalamaresUtils::getString( groupData, "source" ) )
|
2020-03-23 09:59:03 +01:00
|
|
|
, m_isGroup( true )
|
2020-04-16 22:33:59 +02:00
|
|
|
, m_isCritical( parentCriticality( groupData, parent.parent ) )
|
2020-03-20 23:03:47 +01:00
|
|
|
, m_isHidden( CalamaresUtils::getBool( groupData, "hidden", false ) )
|
2020-03-22 13:21:39 +01:00
|
|
|
, m_showReadOnly( CalamaresUtils::getBool( groupData, "immutable", false ) )
|
2020-03-20 23:03:47 +01:00
|
|
|
, m_startExpanded( CalamaresUtils::getBool( groupData, "expanded", false ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:02:53 +01:00
|
|
|
PackageTreeItem::PackageTreeItem::PackageTreeItem()
|
2020-03-23 09:59:03 +01:00
|
|
|
: m_parentItem( nullptr )
|
|
|
|
, m_name( QStringLiteral( "<root>" ) )
|
|
|
|
, m_selected( Qt::Checked )
|
|
|
|
, m_isGroup( true )
|
2017-11-24 13:52:52 +01:00
|
|
|
{
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
|
|
|
|
PackageTreeItem::~PackageTreeItem()
|
|
|
|
{
|
|
|
|
qDeleteAll( m_childItems );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PackageTreeItem::appendChild( PackageTreeItem* child )
|
|
|
|
{
|
|
|
|
m_childItems.append( child );
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageTreeItem*
|
|
|
|
PackageTreeItem::child( int row )
|
|
|
|
{
|
|
|
|
return m_childItems.value( row );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PackageTreeItem::childCount() const
|
|
|
|
{
|
|
|
|
return m_childItems.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PackageTreeItem::row() const
|
|
|
|
{
|
|
|
|
if ( m_parentItem )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
|
|
|
return m_parentItem->m_childItems.indexOf( const_cast< PackageTreeItem* >( this ) );
|
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
PackageTreeItem::data( int column ) const
|
|
|
|
{
|
2020-04-16 22:41:28 +02:00
|
|
|
switch ( column )
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-04-16 22:41:28 +02:00
|
|
|
case 0:
|
|
|
|
// packages have a packagename, groups don't
|
|
|
|
return QVariant( isPackage() ? packageName() : name() );
|
|
|
|
case 1:
|
|
|
|
// packages often have a blank description
|
|
|
|
return QVariant( description() );
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageTreeItem*
|
|
|
|
PackageTreeItem::parentItem()
|
|
|
|
{
|
|
|
|
return m_parentItem;
|
|
|
|
}
|
|
|
|
|
2017-11-21 12:13:42 +01:00
|
|
|
const PackageTreeItem*
|
|
|
|
PackageTreeItem::parentItem() const
|
|
|
|
{
|
|
|
|
return m_parentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
PackageTreeItem::hiddenSelected() const
|
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
if ( !m_isHidden )
|
|
|
|
{
|
2020-03-23 10:49:16 +01:00
|
|
|
return m_selected != Qt::Unchecked;
|
2020-03-20 19:48:29 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 10:49:16 +01:00
|
|
|
if ( m_selected == Qt::Unchecked )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-21 12:13:42 +01:00
|
|
|
return false;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-21 12:13:42 +01:00
|
|
|
|
|
|
|
const PackageTreeItem* currentItem = parentItem();
|
|
|
|
while ( currentItem != nullptr )
|
|
|
|
{
|
|
|
|
if ( !currentItem->isHidden() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-21 12:13:42 +01:00
|
|
|
return currentItem->isSelected() != Qt::Unchecked;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-21 12:13:42 +01:00
|
|
|
currentItem = currentItem->parentItem();
|
|
|
|
}
|
|
|
|
|
2020-03-23 10:49:16 +01:00
|
|
|
/* Has no non-hidden parents */
|
|
|
|
return m_selected != Qt::Unchecked;
|
2017-11-21 12:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-23 13:42:40 +01:00
|
|
|
void
|
|
|
|
PackageTreeItem::setSelected( Qt::CheckState isSelected )
|
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
if ( parentItem() == nullptr )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
// This is the root, it is always checked so don't change state
|
2017-11-24 13:52:52 +01:00
|
|
|
return;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
|
2020-03-20 19:48:29 +01:00
|
|
|
m_selected = isSelected;
|
2017-01-23 13:42:40 +01:00
|
|
|
setChildrenSelected( isSelected );
|
2017-11-24 13:52:52 +01:00
|
|
|
|
|
|
|
// Look for suitable parent item which may change checked-state
|
|
|
|
// when one of its children changes.
|
2017-01-23 13:42:40 +01:00
|
|
|
PackageTreeItem* currentItem = parentItem();
|
2017-11-24 13:52:52 +01:00
|
|
|
while ( ( currentItem != nullptr ) && ( currentItem->childCount() == 0 ) )
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
|
|
|
currentItem = currentItem->parentItem();
|
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
if ( currentItem == nullptr )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-23 10:49:16 +01:00
|
|
|
// Reached the root .. don't bother
|
2017-11-24 13:52:52 +01:00
|
|
|
return;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
|
2020-03-27 17:28:32 +01:00
|
|
|
currentItem->updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PackageTreeItem::updateSelected()
|
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
// Figure out checked-state based on the children
|
|
|
|
int childrenSelected = 0;
|
|
|
|
int childrenPartiallySelected = 0;
|
2020-03-27 17:28:32 +01:00
|
|
|
for ( int i = 0; i < childCount(); i++ )
|
2017-11-24 13:52:52 +01:00
|
|
|
{
|
2020-03-27 17:28:32 +01:00
|
|
|
if ( child( i )->isSelected() == Qt::Checked )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
childrenSelected++;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2020-03-27 17:28:32 +01:00
|
|
|
if ( child( i )->isSelected() == Qt::PartiallyChecked )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
childrenPartiallySelected++;
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
}
|
2020-02-18 11:02:53 +01:00
|
|
|
if ( !childrenSelected && !childrenPartiallySelected )
|
|
|
|
{
|
2020-03-27 17:28:32 +01:00
|
|
|
setSelected( Qt::Unchecked );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2020-03-27 17:28:32 +01:00
|
|
|
else if ( childrenSelected == childCount() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-27 17:28:32 +01:00
|
|
|
setSelected( Qt::Checked );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
else
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-27 17:28:32 +01:00
|
|
|
setSelected( Qt::PartiallyChecked );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:28:32 +01:00
|
|
|
|
2017-01-23 13:42:40 +01:00
|
|
|
void
|
|
|
|
PackageTreeItem::setChildrenSelected( Qt::CheckState isSelected )
|
|
|
|
{
|
|
|
|
if ( isSelected != Qt::PartiallyChecked )
|
2017-11-24 13:52:52 +01:00
|
|
|
// Children are never root; don't need to use setSelected on them.
|
2017-01-23 13:42:40 +01:00
|
|
|
for ( auto child : m_childItems )
|
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
child->m_selected = isSelected;
|
2017-01-23 13:42:40 +01:00
|
|
|
child->setChildrenSelected( isSelected );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:58:10 +01:00
|
|
|
void
|
|
|
|
PackageTreeItem::removeChild( int row )
|
|
|
|
{
|
2022-01-31 12:59:03 +01:00
|
|
|
if ( 0 <= row && row < m_childItems.count() )
|
2022-01-23 20:58:10 +01:00
|
|
|
{
|
|
|
|
m_childItems.removeAt( row );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-24 22:39:14 +01:00
|
|
|
cWarning() << "Attempt to remove invalid child in removeChild() at row " << row;
|
2022-01-23 20:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:42:40 +01:00
|
|
|
int
|
|
|
|
PackageTreeItem::type() const
|
|
|
|
{
|
|
|
|
return QStandardItem::UserType;
|
|
|
|
}
|
2020-03-20 19:48:29 +01:00
|
|
|
|
|
|
|
QVariant
|
|
|
|
PackageTreeItem::toOperation() const
|
|
|
|
{
|
|
|
|
// If it's a package with a pre- or post-script, replace
|
|
|
|
// with the more complicated datastructure.
|
|
|
|
if ( !m_preScript.isEmpty() || !m_postScript.isEmpty() )
|
|
|
|
{
|
|
|
|
QMap< QString, QVariant > sdetails;
|
|
|
|
sdetails.insert( "pre-script", m_preScript );
|
|
|
|
sdetails.insert( "package", m_packageName );
|
|
|
|
sdetails.insert( "post-script", m_postScript );
|
|
|
|
return sdetails;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_packageName;
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 10:16:54 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
PackageTreeItem::operator==( const PackageTreeItem& rhs ) const
|
|
|
|
{
|
|
|
|
if ( isGroup() != rhs.isGroup() )
|
|
|
|
{
|
|
|
|
// Different kinds
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isGroup() )
|
|
|
|
{
|
|
|
|
return name() == rhs.name() && description() == rhs.description() && preScript() == rhs.preScript()
|
|
|
|
&& postScript() == rhs.postScript() && isCritical() == rhs.isCritical() && isHidden() == rhs.isHidden()
|
|
|
|
&& m_showReadOnly == rhs.m_showReadOnly && expandOnStart() == rhs.expandOnStart();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return packageName() == rhs.packageName();
|
|
|
|
}
|
|
|
|
}
|