-
Notifications
You must be signed in to change notification settings - Fork 4
/
itemspritemodel.cpp
123 lines (101 loc) · 3.36 KB
/
itemspritemodel.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
#include <QMimeData>
#include "itemspritemodel.h"
#include "resourcehandler.h"
#include "tibiahandler.h"
extern ResourceHandler g_resourceHandler;
extern TibiaHandler g_tibiaHandler;
ItemSpriteModel::ItemSpriteModel( QObject *parent, ItemData *itemData ) : QAbstractListModel( parent ),
m_itemData( itemData ),
m_dropActions( Qt::CopyAction ),
m_itemFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled )
{
}
void ItemSpriteModel::setDragMimeFormat( const QString& mimeType )
{
m_dragMimeType = mimeType;
}
void ItemSpriteModel::setDropMimeFormats( const QStringList& mimeTypes )
{
m_dropMimeTypes = mimeTypes;
}
void ItemSpriteModel::setSupportedDropActions( Qt::DropActions actions )
{
m_dropActions = actions;
}
void ItemSpriteModel::setItemFlags( Qt::ItemFlags flags )
{
m_itemFlags = flags;
}
QVariant ItemSpriteModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
return QVariant();
if( role == Qt::DecorationRole || role == Qt::DisplayRole || role == Qt::UserRole ) {
SharedResource resource = g_tibiaHandler.getSpriteResource( index.row(), *m_itemData );
if( resource ) {
if( role == Qt::DecorationRole ) {
TibiaSprite sprite = resource->getTibiaSprite();
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 )
return QVariant( g_resourceHandler.getDisplayIdentifier( resource ) );
} else if( role == Qt::UserRole ) {
return QVariant::fromValue( resource );
}
} else {
if( role == Qt::DecorationRole ) {
return QVariant( dummy.image );
} else if( role == Qt::DisplayRole ) {
return QVariant( 0 );
} else if( role == Qt::UserRole ) {
return QVariant::fromValue( SharedResource() );
}
}
}
return QVariant();
}
void ItemSpriteModel::invalidate( void )
{
reset();
}
int ItemSpriteModel::rowCount( const QModelIndex& parent ) const
{
if( parent.isValid() )
return 0;
return m_itemData->getSpriteCount();
}
QMimeData *ItemSpriteModel::mimeData( const QModelIndexList& indexes ) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream( &encodedData, QIODevice::WriteOnly );
foreach( QModelIndex index, indexes ) {
if ( index.isValid() ) {
stream << data( index, Qt::UserRole ).value<SharedResource>();
}
}
mimeData->setData( m_dragMimeType, encodedData );
return mimeData;
}
// Incoming information
bool ItemSpriteModel::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 ItemSpriteModel::mimeTypes( void ) const
{
return m_dropMimeTypes;
}
Qt::DropActions ItemSpriteModel::supportedDropActions( void ) const
{
return m_dropActions;
}
Qt::ItemFlags ItemSpriteModel::flags(const QModelIndex&) const
{
return m_itemFlags;
}