-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileChooser.cpp
More file actions
135 lines (117 loc) · 2.64 KB
/
FileChooser.cpp
File metadata and controls
135 lines (117 loc) · 2.64 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
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
#include "FileChooser.h"
#include "Settings.h"
#include "GUIHelper.h"
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMenu>
#include <QDesktopServices>
#include <QBoxLayout>
#include <QMimeData>
FileChooser::FileChooser(FileChooser::Type type, QWidget *parent)
: QWidget(parent)
, type_(type)
{
setAcceptDrops(true);
//create layout
QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(3);
setLayout(layout);
//add widgets
text_ = new QLineEdit();
layout->addWidget(text_);
button_ = new QPushButton("browse");
layout->addWidget(button_);
connect(button_, SIGNAL(clicked()), this, SLOT(browseFile()));
}
QString FileChooser::file()
{
return text_->text();
}
void FileChooser::setFile(QString file)
{
text_->setText(file);
}
void FileChooser::browseFile()
{
QString file = "";
if (type_==LOAD)
{
file = QFileDialog::getOpenFileName(this, "Select input file.", Settings::path("open_data_folder", true), "*.*");
if (file!="")
{
Settings::setPath("open_data_folder", file);
}
}
else
{
file = QFileDialog::getSaveFileName(this, "Select output file.", Settings::path("store_data_folder", true), "*.*");
if (file!="")
{
Settings::setPath("store_data_folder", file);
}
}
text_->setText(file);
text_->setToolTip(file);
}
void FileChooser::dragEnterEvent(QDragEnterEvent* e)
{
if (e->mimeData()->hasFormat("text/uri-list") && e->mimeData()->urls().count()==1)
{
e->acceptProposedAction();
}
}
void FileChooser::dropEvent(QDropEvent* e)
{
QString filename = e->mimeData()->urls().first().toLocalFile();
if (filename.isEmpty()) return;
text_->setText(filename);
}
void FileChooser::contextMenuEvent(QContextMenuEvent* e)
{
//create menu
QMenu* menu = new QMenu();
menu->addAction("Copy");
menu->addAction("Paste");
menu->addAction("Delete");
menu->addSeparator();
if (text_->text()!="")
{
menu->addAction("Open");
}
//execute
QAction* selected = menu->exec(e->globalPos());
//evaluate
if (selected!=0)
{
if(selected->text()=="Copy")
{
text_->selectAll();
text_->copy();
text_->deselect();
}
else if(selected->text()=="Paste")
{
QString previous = text_->text();
//paste
text_->selectAll();
text_->paste();
text_->deselect();
//check file exists
if (!QFile::exists(text_->text()))
{
GUIHelper::showMessage("Error", "File '" + text_->text() + "' does not exist!");
text_->setText(previous);
}
}
else if(selected->text()=="Delete")
{
text_->clear();
}
else if(selected->text()=="Open")
{
QDesktopServices::openUrl("file:///" + text_->text());
}
}
}