-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileinput.cpp
73 lines (60 loc) · 1.71 KB
/
fileinput.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
#include "fileinput.h"
#include <QHBoxLayout>
#include <QFileDialog>
#include <QFileInfo>
#include <QDebug>
FileInput::FileInput(FileInputMode mode, QWidget *parent) :
QWidget(parent)
{
_mode = mode;
_text = new QLineEdit();
_browse = new QPushButton(trUtf8("Browse"));
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->addWidget(_text);
layout->addWidget(_browse);
connect(_text, SIGNAL(textChanged(QString)), this, SIGNAL(valueChanged()));
connect(_browse, SIGNAL(clicked()), this, SLOT(browse()));
}
QString FileInput::value() {
return _text->text();
}
void FileInput::setValue(QString text) {
_text->setText(text);
}
void FileInput::browse() {
QString path;
switch(_mode) {
case DirectoryMode:
path = QFileDialog::getExistingDirectory(this, dialog_caption, dialog_dir);
break;
case FileOpenMode:
path = QFileDialog::getOpenFileName(this, dialog_caption, dialog_dir, dialog_filter);
break;
case FileSaveMode:
path = QFileDialog::getSaveFileName(this, dialog_caption, dialog_dir, dialog_filter);
break;
}
if(path.isEmpty()) {
return;
}
_text->setText(path);
}
bool FileInput::isEmpty() {
return _text->text().isEmpty();
}
bool FileInput::isReadableDir() {
if(isEmpty()) return false;
QFileInfo info(_text->text());
return info.isDir() && info.isReadable();
}
bool FileInput::isWritableFile() {
if(isEmpty()) return false;
QFileInfo info(_text->text());
if(info.exists()) {
return info.isWritable();
} else {
// we check parent directory of selected file
return QFileInfo(info.dir().absolutePath()).isWritable();
}
}