-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfilepicker.cpp
More file actions
155 lines (141 loc) · 6.26 KB
/
filepicker.cpp
File metadata and controls
155 lines (141 loc) · 6.26 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "filepicker.h"
FilePicker::FilePicker(QWidget *parent) : QWidget(parent)
{
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom);
this->setLayout(layout);
titleLabel = new QLabel;
titleLabel->setText("Select a file");
QFont titleFont = titleLabel->font();
titleFont.setPointSize(15);
titleLabel->setFont(titleFont);
layout->addWidget(titleLabel);
pathLabel = new QLabel;
layout->addWidget(pathLabel);
fileList = new QListWidget;
fileList->setVerticalScrollMode(QListWidget::ScrollPerPixel);
connect(fileList, &QListWidget::itemActivated, [=](QListWidgetItem* item) {
QString filename = item->data(Qt::UserRole).toString();
if (QDir(filename).exists()) {
currentDir = QDir(filename);
reloadFiles();
}
});
connect(fileList, &QListWidget::itemSelectionChanged, [=]() {
if (currentType == singleFolder) {
okButton->setEnabled(true);
} else {
if (fileList->selectedItems().count() == 0) {
okButton->setEnabled(false);
} else {
okButton->setEnabled(true);
}
}
});
layout->addWidget(fileList);
QBoxLayout* buttonLayout = new QBoxLayout(QBoxLayout::LeftToRight);
QPushButton* upButton = new QPushButton;
upButton->setText("Go Up");
upButton->setIcon(QIcon::fromTheme("go-parent-folder"));
connect(upButton, &QPushButton::clicked, [=]() {
currentDir.cdUp();
reloadFiles();
});
buttonLayout->addWidget(upButton);
buttonLayout->addStretch();
okButton = new QPushButton;
okButton->setText("OK");
okButton->setIcon(QIcon::fromTheme("dialog-ok"));
connect(okButton, &QPushButton::clicked, [=]() {
std::vector<CefString> selectedFiles;
if (currentType == single) {
selectedFiles.push_back(fileList->selectedItems().first()->data(Qt::UserRole).toString().toStdString());
} else if (currentType == multiple) {
for (QListWidgetItem* item : fileList->selectedItems()) {
selectedFiles.push_back(item->data(Qt::UserRole).toString().toStdString());
}
} else if (currentType == singleFolder) {
selectedFiles.push_back(currentDir.path().toStdString());
}
callback.get()->Continue(0, selectedFiles);
emit fileDone();
});
buttonLayout->addWidget(okButton);
QPushButton* cancelButton = new QPushButton;
cancelButton->setText("Cancel");
cancelButton->setIcon(QIcon::fromTheme("dialog-cancel"));
connect(cancelButton, &QPushButton::clicked, [=]() {
callback.get()->Cancel();
emit fileDone();
});
connect(cancelButton, SIGNAL(clicked(bool)), this, SIGNAL(fileDone()));
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void FilePicker::startSelectFile(CefRefPtr<CefFileDialogCallback> callback, selectionType type) {
if (type == multiple) {
titleLabel->setText("Select some files");
fileList->setSelectionMode(QListWidget::ExtendedSelection);
okButton->setText("OK");
} else if (type == singleFolder) {
titleLabel->setText("Select a folder");
fileList->setSelectionMode(QListWidget::SingleSelection);
okButton->setText("Select this folder");
} else if (type == single) {
titleLabel->setText("Select a file");
fileList->setSelectionMode(QListWidget::SingleSelection);
okButton->setText("OK");
}
currentType = type;
this->callback = callback;
currentDir.setPath(QDir::homePath());
reloadFiles();
}
void FilePicker::reloadFiles() {
fileList->clear();
pathLabel->setText(currentDir.path());
if (currentDir.exists()) {
//TODO: Replace "false" with a "Show Hidden Files" checkbox
QFileInfoList info = currentDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | (false ? QDir::Hidden : (QDir::Filters) 0), QDir::Name | QDir::DirsFirst | QDir::IgnoreCase);
if (info.count() == 0) {
//showInfoMessage("There's nothing in this folder.");
} else {
for (int i = 0; i < info.count(); i++) {
QListWidgetItem* fileItem = new QListWidgetItem();
if (info[i].fileName().startsWith(".")) {
fileItem->setText(info[i].fileName());
} else {
fileItem->setText(info[i].baseName());
}
fileItem->setData(Qt::UserRole, info[i].filePath());
fileItem->setData(Qt::UserRole + 1, info[i].fileName());
if (info[i].isDir()) {
if (info[i].path() == QDir::homePath() && info[i].fileName() == "Documents") {
fileItem->setIcon(QIcon::fromTheme("folder-documents"));
} else if (info[i].path() == QDir::homePath() && info[i].fileName() == "Downloads") {
fileItem->setIcon(QIcon::fromTheme("folder-downloads"));
} else if (info[i].path() == QDir::homePath() && info[i].fileName() == "Music") {
fileItem->setIcon(QIcon::fromTheme("folder-music"));
} else if (info[i].path() == QDir::homePath() && info[i].fileName() == "Videos") {
fileItem->setIcon(QIcon::fromTheme("folder-videos"));
} else if (info[i].path() == QDir::homePath() && info[i].fileName() == "Pictures") {
fileItem->setIcon(QIcon::fromTheme("folder-pictures"));
} else {
fileItem->setIcon(QIcon::fromTheme("folder"));
}
} else {
if (currentType == singleFolder) {
delete fileItem;
continue;
} else {
fileItem->setIcon(QIcon::fromTheme(mimeDatabase.mimeTypeForFile(info[i].filePath()).iconName()));
}
}
if (info[i].isHidden()) {
QBrush disabledColor = fileList->palette().brush(QPalette::Disabled, QPalette::Foreground);
fileItem->setForeground(disabledColor);
}
fileList->addItem(fileItem);
}
}
}
}