calamares/src/modules/netinstall/PackageTreeItem.cpp

240 lines
5.2 KiB
C++
Raw Normal View History

2017-01-23 13:42:40 +01:00
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright (c) 2017, Kyle Robbertze <kyle@aims.ac.za>
* Copyright 2017, 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-09-13 20:07:09 +02:00
PackageTreeItem::PackageTreeItem( const ItemData& data, PackageTreeItem* parent )
: m_parentItem( parent )
, m_data( data )
2017-01-23 13:42:40 +01:00
{ }
PackageTreeItem::PackageTreeItem( const QString packageName, PackageTreeItem* parent ) :
2017-01-25 09:34:18 +01:00
m_parentItem( parent )
{
m_data.packageName = packageName;
if ( parent != nullptr )
m_data.selected = parent->isSelected();
else
m_data.selected = Qt::Unchecked;
}
2017-01-23 13:42:40 +01:00
PackageTreeItem::PackageTreeItem( PackageTreeItem* parent ) :
m_parentItem( parent )
{ }
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 )
return m_parentItem->m_childItems.indexOf( const_cast<PackageTreeItem*>( this ) );
return 0;
}
int
PackageTreeItem::columnCount() const
{
return m_columns;
}
QVariant
PackageTreeItem::data( int column ) const
{
2017-01-25 09:34:18 +01:00
if ( packageName() != nullptr ) // package
2017-01-23 13:42:40 +01:00
{
2017-01-25 09:34:18 +01:00
if ( !column )
2017-01-23 13:42:40 +01:00
return QVariant( packageName() );
return QVariant();
}
2017-01-23 14:04:07 +01:00
switch ( column ) // group
2017-01-23 13:42:40 +01:00
{
2017-01-23 14:04:07 +01:00
case 0:
return QVariant( prettyName() );
case 1:
return QVariant( description() );
default:
return QVariant();
2017-01-23 13:42:40 +01:00
}
}
PackageTreeItem*
PackageTreeItem::parentItem()
{
return m_parentItem;
}
const PackageTreeItem*
PackageTreeItem::parentItem() const
{
return m_parentItem;
}
2017-01-23 13:42:40 +01:00
QString
PackageTreeItem::prettyName() const
{
return m_data.name;
}
QString
PackageTreeItem::description() const
{
return m_data.description;
}
QString
PackageTreeItem::preScript() const
{
return m_data.preScript;
}
QString
PackageTreeItem::packageName() const
{
2017-01-25 09:34:18 +01:00
return m_data.packageName;
2017-01-23 13:42:40 +01:00
}
QString
PackageTreeItem::postScript() const
{
return m_data.postScript;
}
bool
PackageTreeItem::isHidden() const
{
2017-01-25 09:34:18 +01:00
return m_data.isHidden;
2017-01-23 13:42:40 +01:00
}
void
PackageTreeItem::setHidden( bool isHidden )
{
2017-01-25 09:34:18 +01:00
m_data.isHidden = isHidden;
2017-01-23 13:42:40 +01:00
}
bool
PackageTreeItem::hiddenSelected() const
{
Q_ASSERT( m_data.isHidden );
if (! m_data.selected )
return false;
const PackageTreeItem* currentItem = parentItem();
while ( currentItem != nullptr )
{
if ( !currentItem->isHidden() )
return currentItem->isSelected() != Qt::Unchecked;
currentItem = currentItem->parentItem();
}
/* Has no non-hiddent parents */
return m_data.selected;
}
2017-01-23 13:42:40 +01:00
bool
PackageTreeItem::isCritical() const
{
2017-01-25 09:34:18 +01:00
return m_data.isCritical;
2017-01-23 13:42:40 +01:00
}
void
PackageTreeItem::setCritical( bool isCritical )
{
2017-01-25 09:34:18 +01:00
m_data.isCritical = isCritical;
2017-01-23 13:42:40 +01:00
}
Qt::CheckState
PackageTreeItem::isSelected() const
{
2017-01-25 09:34:18 +01:00
return m_data.selected;
2017-01-23 13:42:40 +01:00
}
void
PackageTreeItem::setSelected( Qt::CheckState isSelected )
{
2017-01-25 09:34:18 +01:00
m_data.selected = isSelected;
2017-01-23 13:42:40 +01:00
setChildrenSelected( isSelected );
PackageTreeItem* currentItem = parentItem();
while ( currentItem != nullptr )
{
if ( currentItem->childCount() == 0)
{
currentItem = currentItem->parentItem();
continue;
}
2017-01-23 13:42:40 +01:00
int childrenSelected = 0;
int childrenPartiallySelected = 0;
2017-01-23 13:42:40 +01:00
for ( int i = 0; i < currentItem->childCount(); i++ )
{
if ( currentItem->child( i )->isSelected() == Qt::Checked )
childrenSelected++;
if ( currentItem->child( i )->isSelected() == Qt::PartiallyChecked )
childrenPartiallySelected++;
2017-01-23 13:42:40 +01:00
}
if ( !childrenSelected && !childrenPartiallySelected)
2017-01-25 09:34:18 +01:00
currentItem->m_data.selected = Qt::Unchecked;
2017-01-23 13:42:40 +01:00
else if ( childrenSelected == currentItem->childCount() )
2017-01-25 09:34:18 +01:00
currentItem->m_data.selected = Qt::Checked;
2017-01-23 13:42:40 +01:00
else
2017-01-25 09:34:18 +01:00
currentItem->m_data.selected = Qt::PartiallyChecked;
2017-01-23 13:42:40 +01:00
currentItem = currentItem->parentItem();
}
}
void
PackageTreeItem::setChildrenSelected( Qt::CheckState isSelected )
{
if ( isSelected != Qt::PartiallyChecked )
for ( auto child : m_childItems )
{
2017-01-25 09:34:18 +01:00
child->m_data.selected = isSelected;
2017-01-23 13:42:40 +01:00
child->setChildrenSelected( isSelected );
}
}
int
PackageTreeItem::type() const
{
return QStandardItem::UserType;
}