-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageseqmodel.cpp
145 lines (107 loc) · 3.27 KB
/
imageseqmodel.cpp
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
#include "imageseqmodel.h"
#include "imageinsequence.h"
#include "QMimeData"
#include "QStringList"
#include "iostream"
ImageSeqModel::ImageSeqModel(QObject *parent, QList<ImageInSequence> *li) :
QAbstractListModel(parent)
{
this->li = *li;
}
int ImageSeqModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return li.count();
}
QVariant ImageSeqModel::data(const QModelIndex &index, int role) const {
int row = index.row();
if (role != Qt::DisplayRole){
return (QVariant::Invalid);
}
if ((index.column() == 0) && (row >= 0) && (row < rowCount()))
{
ImageInSequence is = li.at(index.row());
return is.toVariantMap();
}
return (QVariant::Invalid);
}
Qt::DropActions ImageSeqModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt::DropActions ImageSeqModel::supportedDragActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt::ItemFlags ImageSeqModel::flags(const QModelIndex &index) const{
Qt::ItemFlags defaultFlags = 0;
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
bool ImageSeqModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
return true;
if (column > 0)
return false;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
QByteArray encodedData = data->data("application/x-myowncustomdata");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QVariant isVariant;
stream >> isVariant;
QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, isVariant);
return true;
}
QStringList ImageSeqModel::mimeTypes() const{
QStringList types;
types << "application/x-myowncustomdata";
return types;
}
QMimeData * ImageSeqModel::mimeData ( const QModelIndexList & indexes ) const{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
stream << data(indexes.first(), Qt::DisplayRole);
mimeData->setData("application/x-myowncustomdata", encodedData);
return mimeData;
}
bool ImageSeqModel::setData ( const QModelIndex & index, const QVariant & value, int role ){
if (!index.isValid() )
{
return false;
}
int row = index.row();
if(row < 0){
return false;
}
ImageInSequence is;
is.fromVariant(value.toMap());
//Si l'objet n'est pas contenu on l'insert sinon on le déplace
if(li.contains(is)){
li.removeOne(is);
li.insert(row,is);
}else{
li.insert(row,is);
}
emit dataChanged(index,index);
return true;
}
bool ImageSeqModel::removeRows(int row, int count, const QModelIndex &parent){
if(row > li.count() || row < 0){
return false;
}
li.removeAt(row);
QModelIndex startIndex = index(0,0);
QModelIndex endIndex = index(li.count(),0);
emit dataChanged(startIndex,endIndex);
return true;
}