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-02-18 17:40:15 +01:00
|
|
|
* Copyright 2017, 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 "PackageTreeItem.h"
|
|
|
|
|
2017-11-24 13:52:52 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2020-03-20 19:48:29 +01:00
|
|
|
PackageTreeItem::PackageTreeItem( const QString& packageName, PackageTreeItem* parent )
|
2020-02-18 11:02:53 +01:00
|
|
|
: m_parentItem( parent )
|
2017-01-25 09:34:18 +01:00
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
m_packageName = packageName;
|
2017-01-25 09:34:18 +01:00
|
|
|
if ( parent != nullptr )
|
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
|
|
|
|
m_selected = 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 19:48:29 +01:00
|
|
|
m_selected = 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-02-18 11:02:53 +01:00
|
|
|
PackageTreeItem::PackageTreeItem::PackageTreeItem()
|
|
|
|
: PackageTreeItem( QString(), nullptr )
|
2017-11-24 13:52:52 +01:00
|
|
|
{
|
2020-03-20 19:48:29 +01:00
|
|
|
m_selected = Qt::Checked;
|
|
|
|
m_name = QLatin1String( "<root>" );
|
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-03-10 04:46:59 +01:00
|
|
|
if ( !packageName().isEmpty() ) // packages have a packagename, groups don't
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-03-10 04:46:59 +01:00
|
|
|
switch ( column )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2020-03-10 04:46:59 +01:00
|
|
|
case 0:
|
2017-01-23 13:42:40 +01:00
|
|
|
return QVariant( packageName() );
|
2020-03-10 04:46:59 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-01-23 13:42:40 +01:00
|
|
|
}
|
2020-03-10 04:46:59 +01:00
|
|
|
else
|
2017-01-23 13:42:40 +01:00
|
|
|
{
|
2020-03-10 04:46:59 +01:00
|
|
|
switch ( column ) // group
|
|
|
|
{
|
|
|
|
case 0:
|
2020-03-20 19:48:29 +01:00
|
|
|
return QVariant( name() );
|
2020-03-10 04:46:59 +01:00
|
|
|
case 1:
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
return m_selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !m_selected )
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Has no non-hiddent parents */
|
2020-03-20 19:48:29 +01:00
|
|
|
return m_selected;
|
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
|
|
|
// 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
|
|
|
|
|
|
|
// Figure out checked-state based on the children
|
|
|
|
int childrenSelected = 0;
|
|
|
|
int childrenPartiallySelected = 0;
|
|
|
|
for ( int i = 0; i < currentItem->childCount(); i++ )
|
|
|
|
{
|
|
|
|
if ( currentItem->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
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
if ( currentItem->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 )
|
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
currentItem->setSelected( Qt::Unchecked );
|
2020-02-18 11:02:53 +01:00
|
|
|
}
|
2017-11-24 13:52:52 +01:00
|
|
|
else if ( childrenSelected == currentItem->childCount() )
|
2020-02-18 11:02:53 +01:00
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
currentItem->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
|
|
|
{
|
2017-11-24 13:52:52 +01:00
|
|
|
currentItem->setSelected( Qt::PartiallyChecked );
|
2020-02-18 11:02:53 +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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|