-
Notifications
You must be signed in to change notification settings - Fork 4
/
itemimage.cpp
290 lines (251 loc) · 8.09 KB
/
itemimage.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <QPainter>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragLeaveEvent>
#include <QDropEvent>
#include <QApplication>
#include "itemimage.h"
#include "tibiahandler.h"
ItemImage::ItemImage( QWidget *parent ) : QFrame( parent )
{
m_shownumbers = false;
m_droptype = QString();
m_image = NULL;
m_acceptFiles = false;
setAcceptDrops( true );
setImage( QImage() );
}
ItemImage::ItemImage( QWidget *parent, const QImage& img ) : QFrame( parent )
{
m_shownumbers = false;
m_droptype = QString();
m_image = NULL;
m_acceptFiles = false;
setAcceptDrops( true );
setImage( img );
}
ItemImage::ItemImage( const QImage& img ) : QFrame( NULL )
{
m_shownumbers = false;
m_droptype = QString();
m_image = NULL;
m_acceptFiles = false;
setAcceptDrops( true );
setImage( img );
}
ItemImage::~ItemImage()
{
if( m_image ) {
delete m_image;
m_image = NULL;
}
}
void ItemImage::setMimeDropType( const QString& type )
{
QMutexLocker locker( &mutex );
m_droptype = type;
}
const QString& ItemImage::getMimeDropType( void ) const
{
QMutexLocker locker( &mutex );
return m_droptype;
}
void ItemImage::setColor( const QColor& color )
{
QMutexLocker locker( &mutex );
m_color = color;
update();
}
void ItemImage::showNumbers( bool show )
{
QMutexLocker locker( &mutex );
m_shownumbers = show;
update();
}
void ItemImage::setAcceptFiles( bool accept )
{
m_acceptFiles = accept;
update();
}
void ItemImage::clearContents( void )
{
clearRectangles();
if( m_image ) {
delete m_image;
m_image = NULL;
}
update();
}
void ItemImage::clearRectangles( void )
{
m_highlighted = QRect();
for( QMap<QPoint *, QRect>::iterator it = m_pieces.begin(); it != m_pieces.end(); it++ )
delete it.key();
m_pieces.clear();
m_indexes.clear();
}
void ItemImage::setImage( const QImage& img )
{
QMutexLocker locker( &mutex );
clearContents();
m_image = new QImage( img );
if( m_image->width() < 32 && m_image->height() < 32 ) {
setMinimumSize( QSize( 32, 32 ) );
setMaximumSize( QSize( 32, 32 ) );
} else if( m_image->width() < 32 && m_image->height() >= 32 ) {
setMinimumSize( QSize( 32, m_image->size().height() ) );
setMaximumSize( QSize( 32, m_image->size().height() ) );
} else if( m_image->width() >= 32 && m_image->height() < 32 ) {
setMinimumSize( QSize( m_image->size().width(), 32 ) );
setMaximumSize( QSize( m_image->size().width(), 32 ) );
} else {
setMinimumSize( m_image->size() );
setMaximumSize( m_image->size() );
}
emit imageChanged( img );
updatePieces();
update();
}
const QImage ItemImage::getImage( void )
{
if( m_image )
return *m_image;
return QImage();
}
void ItemImage::clear( void )
{
clearContents();
setMinimumSize( QSize( 32, 32 ) );
setMaximumSize( QSize( 32, 32 ) );
}
void ItemImage::paintEvent( QPaintEvent *event )
{
QFrame::paintEvent( event );
QPainter painter( this );
painter.setRenderHint( QPainter::HighQualityAntialiasing );
if( m_image && !m_shownumbers )
painter.drawImage( this->rect(), *m_image );
if( m_shownumbers && !m_pieces.isEmpty() ) {
QFont font( ItemImage::font() );
font.setBold( true );
font.setPointSize( 10 );
painter.setFont( font );
for( QVector<QRect>::const_iterator it = m_indexes.begin(); it != m_indexes.end(); it++ )
painter.drawText( *it, Qt::AlignCenter, QString::number( m_indexes.indexOf( *it ) ) );
}
if( m_highlighted.isValid() ) {
if( m_color.isValid() )
drawGradientRectangle( painter, m_highlighted, m_color );
else
drawGradientRectangle( painter, m_highlighted, QApplication::palette().color( QPalette::Highlight ) );
}
}
void ItemImage::drawGradientRectangle( QPainter& painter, QRect& rect, QColor baseColor )
{
QColor baseLighter = baseColor.lighter( 175 );
QColor gradientStart = QColor( baseLighter.red(), baseLighter.green(), baseLighter.blue(), 150 );
QColor gradientEnd = QColor( baseColor.red(), baseColor.green(), baseColor.blue(), 150 );
QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, 1 ) );
linearGrad.setColorAt( 0, gradientStart );
linearGrad.setColorAt( 1, gradientEnd );
linearGrad.setCoordinateMode( QGradient::ObjectBoundingMode );
painter.setBrush( linearGrad );
painter.setPen( QPen( baseColor ) );
painter.drawRoundedRect( rect, 2.7, 2.7 );
/*painter.drawRect( rect );
painter.setBrush( QColor( 0x00, 0x00, 0xFF, 100 ) );
painter.setPen( QPen( QColor(0x00, 0x00, 0xFF ) ) );
painter.drawRect( rect );*/
}
void ItemImage::updatePieces( void )
{
clearRectangles();
if( m_image ) {
quint8 width = m_image->rect().width() / 32;
quint8 height = m_image->rect().height() / 32;
if( width > 0 && height > 0 ) {
qint8 real_x = 0;
qint8 real_y = 0;
for( qint8 offset_y = height - 1; offset_y >= 0; offset_y-- ) { // Reverse image chunking to produce proper points
real_x = 0;
for( qint8 offset_x = width - 1; offset_x >= 0; offset_x-- ) {
QPoint *coordinate = new QPoint( real_x, real_y );
m_pieces.insert( coordinate, QRect( m_image->rect().topLeft().x() + offset_x * 32, m_image->rect().topLeft().y() + offset_y * 32, 32, 32 ).adjusted( 0, 0, -1, -1 ) );
real_x++;
}
real_y++;
}
for( qint8 fy = 0; fy < height; fy++ ) {
for( qint8 fx = 0; fx < width; fx++ ) { // Forward image chunking to update numbers
QRect rect( m_image->rect().topLeft().x() + fx * 32, m_image->rect().topLeft().y() + fy * 32, 32, 32 );
m_indexes.push_back( rect );
}
}
}
}
}
void ItemImage::dragEnterEvent( QDragEnterEvent *event )
{
if ( !m_droptype.isEmpty() && event->mimeData()->hasFormat( m_droptype ) )
event->accept();
else if ( event->mimeData()->hasFormat( "text/uri-list" ) && m_acceptFiles )
event->accept();
else
event->ignore();
}
void ItemImage::dragLeaveEvent( QDragLeaveEvent *event )
{
m_highlighted = QRect();
update();
event->accept();
}
void ItemImage::dragMoveEvent( QDragMoveEvent *event )
{
if ( !m_droptype.isEmpty() && event->mimeData()->hasFormat( m_droptype ) ) {
bool inRect = false;
QRect rect;
foreach( rect, m_pieces ) {
if( inRect = rect.contains( event->pos() ) )
break;
}
if( inRect ) {
m_highlighted = rect;
event->setDropAction( Qt::CopyAction );
event->accept();
} else {
m_highlighted = QRect();
event->ignore();
}
update( m_image->rect() );
}
if( m_acceptFiles && event->mimeData()->hasFormat( "text/uri-list" ) ) {
if( this->rect().contains( event->pos() ) ) {
m_highlighted = this->rect().adjusted( 0, 0, -1, -1 );
event->setDropAction( Qt::CopyAction );
event->accept();
update( m_image->rect() );
}
}
}
void ItemImage::dropEvent( QDropEvent *event )
{
if ( !m_droptype.isEmpty() && event->mimeData()->hasFormat( m_droptype ) ) {
if( m_highlighted.isValid() ) {
QByteArray pieceData = event->mimeData()->data( m_droptype );
QDataStream stream( &pieceData, QIODevice::ReadOnly );
qint32 size;
stream >> size;
if( size > 0 ) {
SharedResource resource;
stream >> resource;
QPoint *point = m_pieces.key( m_highlighted );
emit droppedSprite( resource, point->x(), point->y() );
}
}
}
if( m_acceptFiles && event->mimeData()->hasUrls() ) {
emit droppedFile( event->mimeData()->urls().last().toLocalFile() );
}
m_highlighted = QRect();
update( m_image->rect() );
}