source: calamares/trunk/fuentes/src/modules/netinstall/PackageTreeItem.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.7 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright (c) 2017, Kyle Robbertze <kyle@aims.ac.za>
4 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "PackageTreeItem.h"
21
22#include "utils/Logger.h"
23
24PackageTreeItem::PackageTreeItem( const ItemData& data, PackageTreeItem* parent )
25    : m_parentItem( parent )
26    , m_data( data )
27{ }
28
29PackageTreeItem::PackageTreeItem( const QString packageName, PackageTreeItem* parent ) :
30    m_parentItem( parent )
31{
32    m_data.packageName = packageName;
33    if ( parent != nullptr )
34        m_data.selected = parent->isSelected();
35    else
36        m_data.selected = Qt::Unchecked;
37}
38
39PackageTreeItem::PackageTreeItem( PackageTreeItem* parent ) :
40    m_parentItem( parent )
41{
42}
43
44PackageTreeItem::PackageTreeItem::PackageTreeItem() :
45    PackageTreeItem( QString(), nullptr )
46{
47    m_data.selected = Qt::Checked;
48    m_data.name = QLatin1Literal( "<root>" );
49}
50
51PackageTreeItem::~PackageTreeItem()
52{
53    qDeleteAll( m_childItems );
54}
55
56void
57PackageTreeItem::appendChild( PackageTreeItem* child )
58{
59    m_childItems.append( child );
60}
61
62PackageTreeItem*
63PackageTreeItem::child( int row )
64{
65    return m_childItems.value( row );
66}
67
68int
69PackageTreeItem::childCount() const
70{
71    return m_childItems.count();
72}
73
74int
75PackageTreeItem::row() const
76{
77    if ( m_parentItem )
78        return m_parentItem->m_childItems.indexOf( const_cast<PackageTreeItem*>( this ) );
79    return 0;
80}
81
82int
83PackageTreeItem::columnCount() const
84{
85    return m_columns;
86}
87
88QVariant
89PackageTreeItem::data( int column ) const
90{
91    if ( packageName() != nullptr ) // package
92    {
93        if ( !column )
94            return QVariant( packageName() );
95        return QVariant();
96    }
97    switch ( column ) // group
98    {
99    case 0:
100        return QVariant( prettyName() );
101    case 1:
102        return QVariant( description() );
103    default:
104        return QVariant();
105    }
106}
107
108PackageTreeItem*
109PackageTreeItem::parentItem()
110{
111    return m_parentItem;
112}
113
114const PackageTreeItem*
115PackageTreeItem::parentItem() const
116{
117    return m_parentItem;
118}
119
120
121QString
122PackageTreeItem::prettyName() const
123{
124    return m_data.name;
125}
126
127QString
128PackageTreeItem::description() const
129{
130    return m_data.description;
131}
132
133QString
134PackageTreeItem::preScript() const
135{
136    return m_data.preScript;
137}
138
139QString
140PackageTreeItem::packageName() const
141{
142    return m_data.packageName;
143}
144
145QString
146PackageTreeItem::postScript() const
147{
148    return m_data.postScript;
149}
150
151bool
152PackageTreeItem::isHidden() const
153{
154    return m_data.isHidden;
155}
156
157void
158PackageTreeItem::setHidden( bool isHidden )
159{
160    m_data.isHidden = isHidden;
161}
162
163bool
164PackageTreeItem::hiddenSelected() const
165{
166    Q_ASSERT( m_data.isHidden );
167    if (! m_data.selected )
168        return false;
169
170    const PackageTreeItem* currentItem = parentItem();
171    while ( currentItem != nullptr )
172    {
173        if ( !currentItem->isHidden() )
174            return currentItem->isSelected() != Qt::Unchecked;
175        currentItem = currentItem->parentItem();
176    }
177
178    /* Has no non-hiddent parents */
179    return m_data.selected;
180}
181
182
183bool
184PackageTreeItem::isCritical() const
185{
186    return m_data.isCritical;
187}
188
189void
190PackageTreeItem::setCritical( bool isCritical )
191{
192    m_data.isCritical = isCritical;
193}
194
195Qt::CheckState
196PackageTreeItem::isSelected() const
197{
198    return m_data.selected;
199}
200
201void
202PackageTreeItem::setSelected( Qt::CheckState isSelected )
203{
204    if ( parentItem() == nullptr )
205        // This is the root, it is always checked so don't change state
206        return;
207
208    m_data.selected = isSelected;
209    setChildrenSelected( isSelected );
210
211    // Look for suitable parent item which may change checked-state
212    // when one of its children changes.
213    PackageTreeItem* currentItem = parentItem();
214    while ( ( currentItem != nullptr ) && ( currentItem->childCount() == 0 ) )
215    {
216        currentItem = currentItem->parentItem();
217    }
218    if ( currentItem == nullptr )
219        // Reached the root .. don't bother
220        return;
221
222    // Figure out checked-state based on the children
223    int childrenSelected = 0;
224    int childrenPartiallySelected = 0;
225    for ( int i = 0; i < currentItem->childCount(); i++ )
226    {
227        if ( currentItem->child( i )->isSelected() == Qt::Checked )
228            childrenSelected++;
229        if ( currentItem->child( i )->isSelected() == Qt::PartiallyChecked )
230            childrenPartiallySelected++;
231    }
232    if ( !childrenSelected  && !childrenPartiallySelected)
233        currentItem->setSelected( Qt::Unchecked );
234    else if ( childrenSelected == currentItem->childCount() )
235        currentItem->setSelected( Qt::Checked );
236    else
237        currentItem->setSelected( Qt::PartiallyChecked );
238}
239
240void
241PackageTreeItem::setChildrenSelected( Qt::CheckState isSelected )
242{
243    if ( isSelected != Qt::PartiallyChecked )
244        // Children are never root; don't need to use setSelected on them.
245        for ( auto child : m_childItems )
246        {
247            child->m_data.selected = isSelected;
248            child->setChildrenSelected( isSelected );
249        }
250}
251
252int
253PackageTreeItem::type() const
254{
255    return QStandardItem::UserType;
256}
Note: See TracBrowser for help on using the repository browser.