forked from pbek/QOwnNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiledialog.cpp
More file actions
77 lines (62 loc) · 2.23 KB
/
filedialog.cpp
File metadata and controls
77 lines (62 loc) · 2.23 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
#include "filedialog.h"
#include <QDebug>
#include <QSettings>
FileDialog::FileDialog(const QString& name) {
if (!name.isEmpty()) {
setObjectName(QStringLiteral("FileDialog-") + name);
_generalSettingsKey = QStringLiteral("FileDialog/LastPath");
_settingsKey = _generalSettingsKey + "-" + name;
QSettings settings;
QString path = settings.value(_settingsKey).toString();
QFileInfo fileInfo(path);
// if there are problems with the directory path use path from any
// FileDialog
if (!fileInfo.isDir() || !fileInfo.isReadable()) {
path = settings.value(_generalSettingsKey).toString();
}
fileInfo = QFileInfo(path);
// qDebug() << __func__ << " - 'path': " << path;
// if there are still problems with the directory path use the home
// directory
if (!fileInfo.isDir() || !fileInfo.isReadable()) {
path = QDir::homePath();
}
setDirectory(path);
// store the directory for the next time the dialog opens
connect(this, SIGNAL(fileSelected(QString)), this,
SLOT(storeDirectory(QString)));
connect(this, SIGNAL(filesSelected(QStringList)), this,
SLOT(storeDirectory(QStringList)));
}
}
/**
* Stores the directory for the next time the dialog opens
*
* @param path
*/
void FileDialog::storeDirectory(QString path) {
QSettings settings;
QFileInfo fileInfo(path);
// get the directory path from the path if it is not a directory
// we don't use "isFile()" because that will fail if the file doesn't
// exist yet
if (!fileInfo.isDir()) {
path = fileInfo.dir().path();
}
// this is the path for just this dialog
settings.setValue(_settingsKey, path);
// this is the path for all FileDialog
settings.setValue(_generalSettingsKey, path);
}
void FileDialog::storeDirectory(const QStringList& files) {
if (files.count() > 0) {
storeDirectory(files.at(0));
}
}
/**
* Returns the selected file or an empty string if none was selected
*/
QString FileDialog::selectedFile() {
QStringList fileNames = selectedFiles();
return fileNames.count() > 0 ? fileNames.at(0) : QString();
}