summaryrefslogtreecommitdiff
path: root/src/dic-options/PacksListModel.cpp
blob: c2dfc3105361faa26d4f65b11c657075e58e1890 (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
#include "PacksListModel.h"
#include "../dictionary/CardPack.h"

QVariant PacksListModel::data( const QModelIndex &index, int role ) const
    {
    if (!index.isValid())
         return QVariant();
    if (index.row() >= rowCount())
         return QVariant();

    switch( role )
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
        {
        const CardPack* pack =  m_parent->m_dict.cardPack( index.row() );
        if( !pack )
            return QVariant();
        return pack->id();
        }
    default:
         return QVariant();
    }
    }

bool PacksListModel::setData(const QModelIndex& index, const QVariant& aValue, int role)
    {
    if( !index.isValid() || role != Qt::EditRole )
        return false;
    m_parent->m_dict.cardPack( index.row() )->setName( aValue.toString() );
    emit dataChanged(index, index);
    return true;
    }

Qt::ItemFlags PacksListModel::flags(const QModelIndex &index) const
    {
    Qt::ItemFlags defaultFlags = DraggableListModel::flags(index);
        return defaultFlags;
    }

bool PacksListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/)
    {
    beginInsertRows(QModelIndex(), position, position+rows-1);
    for (int i = 0; i < rows; i++)
        {
        CardPack* newPack = new CardPack( &(m_parent->m_dict) );
        newPack->addField( m_parent->m_dict.field(0) );
        m_parent->m_dict.insertPack( position + i, newPack );
        }
    endInsertRows();
    return true;
    }

bool PacksListModel::removeRows(int position, int rows, const QModelIndex &/*parent*/)
    {
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    for (int row = 0; row < rows; ++row)
        m_parent->m_dict.removePack( position );
    endRemoveRows();
    return true;
    }

const void* PacksListModel::dataPtr( const QModelIndex& aIndex ) const
    {
    CardPack* pack = m_parent->m_dict.cardPack( aIndex.row() );
    return static_cast<const void*>( pack );
    }

void PacksListModel::insertPointer( int aPos, void* aData )
    {
    beginInsertRows(QModelIndex(), aPos, aPos );
    m_parent->m_dict.insertPack( aPos, static_cast<CardPack*>( aData ) );
    endInsertRows();
    }

void PacksListModel::removePack( int aPos )
    {
    beginRemoveRows(QModelIndex(), aPos, aPos);
    m_parent->m_dict.destroyPack( aPos );
    endRemoveRows();
    }