-
Notifications
You must be signed in to change notification settings - Fork 0
/
droparea.cpp
109 lines (94 loc) · 2.52 KB
/
droparea.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
#include "droparea.h"
#include <QDragEnterEvent>
#include <QMimeData>
#include <QStringList>
DropArea::DropArea(QQuickItem *parent)
: QQuickItem(parent)
{
setFlag(QQuickItem::ItemAcceptsDrops,true);
}
void DropArea::dragEnterEvent(QDragEnterEvent *event)
{
if(!this->isValidImages(event->mimeData())){
event->ignore();
emit entered(false,1);
return;
}
if(!this->isNotExceedingLimit(event->mimeData())){
event->ignore();
emit entered(false,2);
return;
}
event->acceptProposedAction();
emit entered(true);
}
//! [dragEnterEvent() function]
//! [dragMoveEvent() function]
void DropArea::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}
//! [dragMoveEvent() function]
//! [dropEvent() function part1]
void DropArea::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData = event->mimeData();
QList<QUrl> urlList = mimeData->urls();
this->urls.append(urlList);
event->acceptProposedAction();
emit dropped(this->stringifyUrls(urlList));
emit urlsChanged();
}
bool DropArea::isValidImages(const QMimeData *mimeData)
{
QList<QUrl> urlList = mimeData->urls();
for (int i = 0; i < urlList.size() && i < 32; ++i) {
if(!urlList.at(i).isValid())return false;
QString url = urlList.at(i).path();
if(!url.contains(".jpg", Qt::CaseInsensitive)&&
!url.contains(".jpeg", Qt::CaseInsensitive)&&
!url.contains(".png", Qt::CaseInsensitive)
)return false;
}
return true;
}
bool DropArea::isNotExceedingLimit(const QMimeData *mimeData)
{
QList<QUrl> urlList = mimeData->urls();
return ((getCount()+urlList.length())<=this->imageMaxLimit);
}
QStringList DropArea::getUrls()
{
return stringifyUrls(this->urls);
}
QStringList DropArea::stringifyUrls(QList<QUrl> _urls)
{
QList<QString> urls;
for (int i = 0; i < _urls.size(); ++i) {
urls.append(_urls.at(i).toString());
}
return urls;
}
//! [dropEvent() function part3]
//! [dragLeaveEvent() function]
void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
{
event->accept();
emit left();
}
//! [dragLeaveEvent() function]
int DropArea::getCount()
{
return this->urls.length();
}
void DropArea::clear()
{
QList<QUrl> urlList;
this->urls = urlList;
emit urlsChanged();
}
void DropArea::removeUrl(int index)
{
this->urls.removeAt(index);
emit urlsChanged();
}