-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdroplabel.cpp
More file actions
48 lines (38 loc) · 1.45 KB
/
Copy pathdroplabel.cpp
File metadata and controls
48 lines (38 loc) · 1.45 KB
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
#include "droplabel.h"
DropLabel::DropLabel(QWidget *parent, const QStringList &extensions) //drag and drop box
:QLabel(parent), extensions(extensions)
{
setAcceptDrops(true);
setAlignment(Qt::AlignCenter);
setText("Drop a file here");
setStyleSheet("border: 2px dashed gray;");
}
void DropLabel::dragEnterEvent(QDragEnterEvent *event){
//user puts some files above the box, if it doesn't have any file expected reject
auto storage = event->mimeData()->urls();
for (const auto &file: storage){
for (const auto &extension : extensions){
if(file.toLocalFile().toLower().endsWith(extension.toLower())){event->acceptProposedAction();return;}}
}
event->ignore();
}
void DropLabel::dropEvent(QDropEvent *event){
//user drops the files, filter and use only the expected files
auto storage = event->mimeData()->urls();
QStringList list_;
for (const auto &file: storage){
for (const auto &extension : extensions){
if(file.toLocalFile().toLower().endsWith(extension.toLower())){
list_.append(file.toLocalFile());
}
}
}
emit fileDropped(list_);
}
void DropLabel::mousePressEvent(QMouseEvent *){
//user clics on the box, call explorer and allow it to chose only the expected files
QStringList path = QFileDialog::getOpenFileNames(this, "Chose files", "", "All files (*)");
if(path.size() != 0){
emit fileDropped(path);
}
}