summaryrefslogtreecommitdiff
path: root/src/main-view/PacksTreeModel.cpp
blob: 55b8c341b2727dae0f1fa09f063cd954f0611ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "PacksTreeModel.h"
#include "AppModel.h"
#include "../dictionary/TreeItem.h"

PacksTreeModel::PacksTreeModel( AppModel* aAppModel, QObject* aParent ):
    QAbstractItemModel( aParent ), m_appModel( aAppModel )
    {
    }

QVariant PacksTreeModel::data(const QModelIndex &index, int role) const
    {
    if( !index.isValid() )
        return QVariant();
    if( role != Qt::DisplayRole )
        return QVariant();
    TreeItem* item = static_cast<TreeItem*>( index.internalPointer() );
    return item->data( index.column() );
    }

Qt::ItemFlags PacksTreeModel::flags(const QModelIndex &index) const
    {
    if( !index.isValid() )
        return 0;
    if( !index.parent().isValid() ) // First level item = dictionary
        return Qt::ItemIsEnabled;
    else    // pack
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }

QVariant PacksTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
        switch( section )
            {
            case 0: return tr("Card pack"); break;
            case 1: return tr("Sched"); break;
            case 2: return tr("New"); break;
            }
    return QVariant();
    }

QModelIndex PacksTreeModel::index(int row, int column, const QModelIndex &parent) const
    {
    if( !hasIndex(row, column, parent) )
        return QModelIndex();
    if( !parent.isValid() ) // First level item = dictionary
        {
        Dictionary* dic = m_appModel->dictionary( row );
        if( dic )
            return createIndex( row, column, dic );
        else
            return QModelIndex();
        }
    TreeItem* parentItem = static_cast<TreeItem*>( parent.internalPointer() );
    const TreeItem* childItem = parentItem->child( row );
    if( childItem )
        return createIndex( row, column, (void*)childItem );
    else
        return QModelIndex();
    }

QModelIndex PacksTreeModel::parent(const QModelIndex &index) const
    {
    if( !index.isValid() )
        return QModelIndex();
    TreeItem* childItem = static_cast<TreeItem*>( index.internalPointer() );
    if( !childItem )
        return QModelIndex();
    const TreeItem* parentItem = childItem->parent();

    if( parentItem )
        return createIndex( parentItem->row(), 0, (void*)parentItem );
    else
        return QModelIndex();   // The root item
    }

int PacksTreeModel::rowCount(const QModelIndex &parent) const
    {
    if( parent.column() > 0 )   // Only the first column may have children
        return 0;
    if( !parent.isValid() )    // Root item
        return m_appModel->dictionariesNum();
    TreeItem* parentItem = static_cast<TreeItem*>( parent.internalPointer() );
    return parentItem->childCount();
    }

void PacksTreeModel::updateData()    // TODO: Suspicious method, just reveals protected methods
    {
    beginResetModel();
    endResetModel();
    }