summaryrefslogtreecommitdiff
path: root/src/dic-options/PackFieldsListModel.cpp
blob: eb6eb6f812d72f06b17b5c24ce7b1bf4a61d19e3 (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
94
95
96
97
98
99
100
101
#include "PackFieldsListModel.h"
#include "../dictionary/CardPack.h"
#include "../dictionary/Field.h"

#include <QMimeData>

int PackFieldsListModel::rowCount( const QModelIndex& /*parent*/ ) const
    {
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    if( !pack )
        return 0;
    return pack->getFields().size();
    }

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

    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    if( !pack )
        return QVariant();
    const Field* field = pack->getFields().value(index.row());
    switch( role )
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
        return field->name();
    case Qt::FontRole:
        if( index.row() == 0 )
            {
            QFont font;
            font.setBold(true);
            return font;
            }
        else
            return QFont();
    default:
         return QVariant();
    }
    }

void PackFieldsListModel::changeParentRow( const QModelIndex& aIndex )
    {
    m_parentRow = aIndex.row();
    emit layoutChanged();
    }

bool PackFieldsListModel::setData(const QModelIndex& index, const QVariant& aValue, int role)
    {
    if( !index.isValid() || role != Qt::EditRole )
        return false;
    const Field* field = m_parent->m_dict.field( aValue.toString() );
    if( !field )
        return false;
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    pack->setField( index.row(), field );
    emit dataChanged(index, index);
    return true;
    }

bool PackFieldsListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/)
    {
    beginInsertRows(QModelIndex(), position, position+rows-1);
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    for (int row = 0; row < rows; ++row)
        pack->insertField( position, m_parent->m_emptyField );
    endInsertRows();
    return true;
    }

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

const void* PackFieldsListModel::dataPtr( const QModelIndex& aIndex ) const
    {
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    if( !pack )
        return NULL;
    const Field* field = pack->getFields().value( aIndex.row() );
    return static_cast<const void*>( field );
    }

void PackFieldsListModel::insertPointer( int aPos, void* aData )
    {
    beginInsertRows(QModelIndex(), aPos, aPos );
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    if( !pack )
        return;
    pack->insertField( aPos, static_cast<Field*>( aData ) );
    endInsertRows();
    }