-
Notifications
You must be signed in to change notification settings - Fork 4
/
spritemodel.cpp
215 lines (172 loc) · 5.64 KB
/
spritemodel.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
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
#include <QMimeData>
#include <QPixmapCache>
#include "spritemodel.h"
#include "tibiafile.h"
#include "resourcehandler.h"
#include "tibiahandler.h"
#include "resourcefile.h"
extern ResourceHandler g_resourceHandler;
extern TibiaHandler g_tibiaHandler;
SpriteModel::SpriteModel( QObject *parent, quint8 resourceType, TibiaFile *tibiaFile ) : QAbstractListModel( parent ),
m_resourceType( resourceType ),
m_tibiaFile( tibiaFile ),
m_dropActions( Qt::CopyAction ),
m_itemFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled )
{
}
SpriteModel::~SpriteModel( void )
{
}
void SpriteModel::setDragMimeFormat( const QString& mimeType )
{
m_dragMimeType = mimeType;
}
void SpriteModel::setDropMimeFormats( const QStringList& mimeTypes )
{
m_dropMimeTypes = mimeTypes;
}
void SpriteModel::setSupportedDropActions( Qt::DropActions actions )
{
m_dropActions = actions;
}
void SpriteModel::setItemFlags( Qt::ItemFlags flags )
{
m_itemFlags = flags;
}
qint32 SpriteModel::getIndexByResource( SharedResource& obj ) const
{
return m_resourceList.indexOf( obj );
}
void SpriteModel::addSprites( quint8 resourceType, ResourceList& resources )
{
if( resourceType != m_resourceType )
return;
emit addedSprites( resourceType, resources.size() );
beginInsertRows( QModelIndex(), m_resourceList.size(), m_resourceList.size() + resources.size() );
m_resourceList += resources;
endInsertRows();
}
void SpriteModel::removeSprites( quint8 resourceType, quint32 amount )
{
if( resourceType != m_resourceType )
return;
emit removedSprites( resourceType, amount );
int row = m_resourceList.size() - amount;
int count = amount;
int beginRow = qMax( 0, row );
int endRow = qMin( row + count - 1, m_resourceList.size() - 1 );
beginRemoveRows( QModelIndex(), beginRow, endRow );
for( qint32 i = beginRow; i <= endRow; i++ )
m_resourceList.removeLast();
endRemoveRows();
}
void SpriteModel::addSprite( quint8 resourceType, SharedResource& obj, qint32 index )
{
if( resourceType != m_resourceType )
return;
if( index == -1 )
index = m_resourceList.size();
emit addedSprites( resourceType, 1 );
beginInsertRows( QModelIndex(), index, index );
m_resourceList.insert( index, obj );
endInsertRows();
}
bool SpriteModel::removeSprite( quint8 resourceType, qint32 index )
{
if( resourceType != m_resourceType )
return false;
if( index == -1 )
return false;
emit removedSprites( resourceType, 1 );
beginRemoveRows( QModelIndex(), index, index );
m_resourceList.removeAt( index );
endRemoveRows();
return true;
}
void SpriteModel::setSprite( quint8 resourceType, qint32 index, SharedResource& obj )
{
if( resourceType != m_resourceType )
return;
QModelIndex modelIndex = QAbstractListModel::index( index );
if( modelIndex.isValid() ) {
m_resourceList.replace( index, obj );
emit dataChanged( modelIndex, modelIndex );
}
}
QVariant SpriteModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
return QVariant();
if( role == Qt::DecorationRole || role == Qt::DisplayRole || role == Qt::UserRole ) {
SharedResource resource = m_resourceList.value( index.row() );
if( role == Qt::DecorationRole ) {
TibiaSprite sprite = g_resourceHandler.getSpriteByResource( resource );
if( sprite.image.height() > 128 || sprite.image.width() > 128 )
return QVariant( sprite.image.scaled( 128, 128, Qt::KeepAspectRatio ) );
return QVariant( sprite.image );
} else if( role == Qt::DisplayRole ) {
if( resource ) {
quint32 realId = g_resourceHandler.getDisplayIdentifier( resource );
if( realId == 0xFFFF ) {
if( ResourceFile *resourceFile = dynamic_cast<ResourceFile *>( resource->getFile() ) )
return QVariant( resourceFile->getSource() );
}
return QVariant( realId );
}
} else if( role == Qt::UserRole ) {
return QVariant::fromValue( resource );
}
}
return QVariant();
}
// Item is visible if QRect QTreeView::visualRect ( const QModelIndex & index ) const is valid
int SpriteModel::rowCount( const QModelIndex& parent ) const
{
if( parent.isValid() )
return 0;
return m_resourceList.size();
}
void SpriteModel::clear( void )
{
m_resourceList.clear();
reset();
}
void SpriteModel::setResourceList( const ResourceList& sprites )
{
m_resourceList = sprites;
reset();
}
QMimeData *SpriteModel::mimeData( const QModelIndexList& indexes ) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream( &encodedData, QIODevice::WriteOnly );
stream << ( qint32 )indexes.size();
foreach( QModelIndex index, indexes ) {
if ( index.isValid() ) {
stream << data( index, Qt::UserRole ).value<SharedResource>();
}
}
mimeData->setData( m_dragMimeType, encodedData );
return mimeData;
}
// Incoming information
bool SpriteModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex& parent )
{
Q_UNUSED( action );
Q_UNUSED( column );
emit decodeMimeDrop( data, row, parent );
return true;
}
QStringList SpriteModel::mimeTypes( void ) const
{
return m_dropMimeTypes;
}
Qt::DropActions SpriteModel::supportedDropActions( void ) const
{
return m_dropActions;
}
Qt::ItemFlags SpriteModel::flags( const QModelIndex& ) const
{
return m_itemFlags;
}