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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#include "FieldsListModel.h"
#include "../dictionary/Field.h"
#include <QMimeData>
QVariant FieldsListModel::data( const QModelIndex &index, int role ) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= rowCount())
return QVariant();
const Field* field = m_parent->m_dict.field( index.row() );
switch( role )
{
case Qt::DisplayRole:
switch( index.column() )
{
case 0:
return field->name();
case 1:
return field->style();
default:
return QVariant();
}
case Qt::EditRole:
switch( index.column() )
{
case 0:
return field->name();
case 1:
return field->style();
default:
return QVariant();
}
case EStyleRole:
return field->style();
default:
return QVariant();
}
}
QVariant FieldsListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if( role != Qt::DisplayRole )
return QVariant();
if( orientation == Qt::Horizontal )
switch( section )
{
case 0:
return tr("Field");
case 1:
return tr("Style");
default:
return QVariant();
}
else
return QVariant();
}
Qt::ItemFlags FieldsListModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
if (index.isValid())
return Qt::ItemIsEditable | Qt::ItemIsDragEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
bool FieldsListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if( !index.isValid() || role!=Qt::EditRole )
return false;
switch( index.column() )
{
case 0:
m_parent->m_dict.setFieldName(index.row(), value.toString());
emit dataChanged(index, index);
return true;
case 1:
m_parent->m_dict.setFieldStyle(index.row(), value.toString());
emit dataChanged(index, index);
return true;
default:
return true;
}
}
bool FieldsListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/)
{
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row = 0; row < rows; ++row)
m_parent->m_dict.insertField( position, tr("new field") );
endInsertRows();
return true;
}
bool FieldsListModel::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.removeField( position );
endRemoveRows();
return true;
}
void FieldsListModel::removeField(int aPos )
{
beginRemoveRows(QModelIndex(), aPos, aPos);
m_parent->m_dict.destroyField( aPos );
endRemoveRows();
}
void FieldsListModel::insertField(int aPos, Field* aField )
{
beginInsertRows(QModelIndex(), aPos, aPos);
m_parent->m_dict.insertField( aPos, aField );
endInsertRows();
}
Qt::DropActions FieldsListModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QStringList FieldsListModel::mimeTypes() const
{
QStringList types;
types << "application/octet-stream";
return types;
}
QMimeData *FieldsListModel::mimeData(const QModelIndexList &indexes) const
{
QStringList list;
QModelIndexList validIndexes;
foreach (QModelIndex index, indexes)
if (index.isValid() && index.column() == 0)
validIndexes << index;
qSort(validIndexes);
int num = validIndexes.size();
QByteArray encodedData;
QDataStream stream( &encodedData, QIODevice::WriteOnly );
const Field** fieldPtrs = new const Field*[num];
int i=0;
foreach (QModelIndex index, validIndexes)
fieldPtrs[i++] = m_parent->m_dict.field( index.row() );
stream.writeBytes( (char*)fieldPtrs, num*sizeof(Field*) );
delete fieldPtrs;
QMimeData *mimeData = new QMimeData();
mimeData->setData( "application/octet-stream", encodedData );
return mimeData;
}
bool FieldsListModel::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;
if (column > 0)
column = 0;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
QByteArray encodedData = data->data("application/octet-stream");
QDataStream stream( &encodedData, QIODevice::ReadOnly );
Field** fieldPtrs = new Field*[ m_parent->m_dict.fieldsNum() ];
uint num;
stream.readBytes( (char*&)fieldPtrs, num ); //TODO: Avoid converting pointer to other types
num /= sizeof(Field*);
QList<QPersistentModelIndex> movedIndexes;
for(uint i=0; i<num; i++)
{
insertField( beginRow + i, fieldPtrs[i] );
movedIndexes << QPersistentModelIndex( index(beginRow + i, 0, QModelIndex()) );
}
delete fieldPtrs;
emit indexesDropped( movedIndexes );
return true;
}
void FieldsListModel::moveIndexesUpDown( QModelIndexList aIndexes, int aDirection )
{
QList<QPersistentModelIndex> pSrcIndexes;
foreach (QModelIndex idx, aIndexes)
if( idx.isValid() && idx.column() == 0 )
pSrcIndexes << QPersistentModelIndex( idx );
int rowsNum = pSrcIndexes.size();
if( rowsNum == 0 )
return;
qSort( pSrcIndexes );
int beginRow;
if( aDirection < 0 )
beginRow = pSrcIndexes[0].row()-1;
else
beginRow = pSrcIndexes[rowsNum-1].row()+2;
if( rowsNum == 1 && (beginRow < 0 || beginRow > rowCount()) )
return;
if( beginRow < 0 )
beginRow = 0;
if( beginRow > rowCount() )
beginRow = rowCount();
QMimeData* mime = mimeData( aIndexes );
dropMimeData( mime, Qt::MoveAction, beginRow, 0, QModelIndex() );
foreach (QModelIndex index, pSrcIndexes)
removeRow( index.row() );
}
|