summaryrefslogtreecommitdiff
path: root/src/dic-options/UnusedFieldsListModel.cpp
blob: 33112851851f1f2442b737346873db01dbbfd499 (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
102
103
104
105
106
107
108
109
110
111
#include "UnusedFieldsListModel.h"
#include "../dictionary/CardPack.h"
#include "../dictionary/Dictionary.h"

#include <QMimeData>

UnusedFieldsListModel::UnusedFieldsListModel( DictionaryOptionsDialog* aParent ):
    DraggableListModel( aParent ), m_parentRow( 0 )
    {
    updateUnusedFields();
    }


int UnusedFieldsListModel::rowCount(const QModelIndex& /*parent*/) const
    {
    return m_unusedFields.size();
    }

QVariant UnusedFieldsListModel::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:
        return m_unusedFields.value(index.row())->name();
    default:
         return QVariant();
    }
    }

void UnusedFieldsListModel::updateUnusedFields()
    {
    beginResetModel();
    m_unusedFields.clear();
    CardPack* pack = m_parent->m_dict.cardPack( m_parentRow );
    if( !pack )
        return;
    QList<const Field*> packFields = pack->getFields();
    for( int i=0; i < m_parent->m_dict.fieldsNum(); i++ )
        {
        const Field* dictField = m_parent->m_dict.field( i );
        if( !packFields.contains( dictField ) )
            m_unusedFields << dictField;
        }
    endResetModel();
    }

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

bool UnusedFieldsListModel::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;
    m_unusedFields[index.row()] = field;
    emit dataChanged(index, index);
    return true;
    }

bool UnusedFieldsListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/)
    {
    beginInsertRows(QModelIndex(), position, position+rows-1);
    for (int row = 0; row < rows; ++row)
        m_unusedFields.insert( position, m_parent->m_emptyField );
    endInsertRows();
    updateUnusedFields();
    return true;
    }

bool UnusedFieldsListModel::removeRows(int position, int rows, const QModelIndex &/*parent*/)
    {
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    for (int row = 0; row < rows; ++row)
        m_unusedFields.removeAt( position );
    endRemoveRows();
    updateUnusedFields();
    return true;
    }

const void* UnusedFieldsListModel::dataPtr( const QModelIndex& aIndex ) const
    {
    const Field* field = m_unusedFields.value( aIndex.row() );
    return static_cast<const void*>( field );
    }

void UnusedFieldsListModel::insertPointer( int aPos, void* aData )
    {
    m_unusedFields.insert( aPos, static_cast<Field*>( aData ) );
    }

bool UnusedFieldsListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
    int /*row*/, int /*column*/, const QModelIndex &/*parent*/)
    {
    if (action == Qt::IgnoreAction)
        return true;
    if (!data->hasFormat("application/octet-stream"))
        return false;
    return true;
    }