source: calamares/trunk/fuentes/src/calamares/progresstree/ProgressTreeModel.cpp @ 7538

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

sync with github

File size: 4.7 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
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 "ProgressTreeModel.h"
21
22#include "progresstree/ViewStepItem.h"
23#include "ViewManager.h"
24
25ProgressTreeModel::ProgressTreeModel( QObject* parent )
26    : QAbstractItemModel( parent )
27    , m_rootItem( nullptr )
28{
29    setupModelData();
30}
31
32
33ProgressTreeModel::~ProgressTreeModel()
34{
35    delete m_rootItem;
36}
37
38
39Qt::ItemFlags
40ProgressTreeModel::flags( const QModelIndex& index ) const
41{
42    if ( !index.isValid() )
43        return Qt::ItemFlags();
44
45    return Qt::ItemIsEnabled;
46}
47
48
49QModelIndex
50ProgressTreeModel::index( int row, int column, const QModelIndex& parent ) const
51{
52    if ( !hasIndex( row, column, parent ) )
53        return QModelIndex();
54
55    ProgressTreeItem* parentItem;
56
57    if ( !parent.isValid() )
58        parentItem = m_rootItem;
59    else
60        parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() );
61
62    ProgressTreeItem* childItem = parentItem->child( row );
63    if ( childItem )
64        return createIndex( row, column, childItem );
65    else
66        return QModelIndex();
67}
68
69
70QModelIndex
71ProgressTreeModel::parent( const QModelIndex& index ) const
72{
73    if ( !index.isValid() )
74        return QModelIndex();
75
76    ProgressTreeItem* childItem = static_cast< ProgressTreeItem* >( index.internalPointer() );
77    ProgressTreeItem* parentItem = childItem->parent();
78
79    if ( parentItem == m_rootItem )
80        return QModelIndex();
81
82    return createIndex( parentItem->row(), 0, parentItem );
83}
84
85
86QVariant
87ProgressTreeModel::data( const QModelIndex& index, int role ) const
88{
89    if ( !index.isValid() )
90        return QVariant();
91
92    ProgressTreeItem* item = static_cast< ProgressTreeItem* >( index.internalPointer() );
93
94    return item->data( role );
95}
96
97
98QVariant
99ProgressTreeModel::headerData( int section, Qt::Orientation orientation, int role ) const
100{
101    Q_UNUSED( section );
102    Q_UNUSED( orientation );
103    Q_UNUSED( role );
104
105    return QVariant();
106}
107
108
109int
110ProgressTreeModel::rowCount( const QModelIndex& parent ) const
111{
112    ProgressTreeItem* parentItem;
113    if ( parent.column() > 0 )
114        return 0;
115
116    if ( !parent.isValid() )
117        parentItem = m_rootItem;
118    else
119        parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() );
120
121    return parentItem->childCount();
122}
123
124
125int
126ProgressTreeModel::columnCount( const QModelIndex& parent ) const
127{
128    if ( parent.isValid() )
129        return static_cast< ProgressTreeItem* >( parent.internalPointer() )->columnCount();
130    else
131        return m_rootItem->columnCount();
132}
133
134
135void
136ProgressTreeModel::setupModelData()
137{
138    delete m_rootItem;
139
140    m_rootItem = new ProgressTreeRoot();
141    const Calamares::ViewManager* vm = Calamares::ViewManager::instance();
142
143    const auto steps = vm->viewSteps();
144    for ( const Calamares::ViewStep* step : steps )
145    {
146        m_rootItem->appendChild( new ViewStepItem( step, m_rootItem ) );
147    }
148}
149
150
151QModelIndex
152ProgressTreeModel::indexFromItem( ProgressTreeItem* item )
153{
154    if ( !item || !item->parent() )
155        return QModelIndex();
156
157    // Reconstructs a QModelIndex from a ProgressTreeItem that is somewhere in the tree.
158    // Traverses the item to the root node, then rebuilds the qmodelindices from there
159    // back down; each int is the row of that item in the parent.
160    /**
161     * In this diagram, if the item is G, childIndexList will contain [0, 2, 0]
162     *
163     *    A
164     *      D
165     *      E
166     *      F
167     *        G
168     *        H
169     *    B
170     *    C
171     *
172     **/
173    QList< int > childIndexList;
174    ProgressTreeItem* curItem = item;
175    while ( curItem != m_rootItem ) {
176        int row  = curItem->row(); //relative to its parent
177        if ( row < 0 ) // something went wrong, bail
178            return QModelIndex();
179
180        childIndexList << row;
181
182        curItem = curItem->parent();
183    }
184
185    // Now we rebuild the QModelIndex we need
186    QModelIndex idx;
187    for ( int i = childIndexList.size() - 1; i >= 0 ; i-- )
188    {
189        idx = index( childIndexList[ i ], 0, idx );
190    }
191
192    return idx;
193}
Note: See TracBrowser for help on using the repository browser.