| 171 | } |
| 172 | |
| 173 | static QStringList BrowseForFileInternal(QString context, |
| 174 | QString caption, |
| 175 | QString filter, |
| 176 | QString defaultPath, |
| 177 | QWidget* parentWidget, |
| 178 | bool single) |
| 179 | { |
| 180 | static QMap<QString, QString> savedPaths; |
| 181 | |
| 182 | QFileDialog w(parentWidget, caption); |
| 183 | QSet<QString> locations; |
| 184 | auto f = [&locations](QStandardPaths::StandardLocation l) { |
| 185 | QString location = QStandardPaths::writableLocation(l); |
| 186 | QFileInfo finfo(location); |
| 187 | if (!finfo.exists()) { |
| 188 | return; |
| 189 | } |
| 190 | locations.insert(location); |
| 191 | }; |
| 192 | f(QStandardPaths::DesktopLocation); |
| 193 | f(QStandardPaths::DocumentsLocation); |
| 194 | f(QStandardPaths::DownloadLocation); |
| 195 | f(QStandardPaths::HomeLocation); |
| 196 | QList<QUrl> urls; |
| 197 | for (auto location : locations) { |
| 198 | urls.append(QUrl::fromLocalFile(location)); |
| 199 | } |
| 200 | urls.append(QUrl::fromLocalFile(defaultPath)); |
| 201 | |
| 202 | w.setFileMode(single ? QFileDialog::ExistingFile : QFileDialog::ExistingFiles); |
| 203 | w.setAcceptMode(QFileDialog::AcceptOpen); |
| 204 | w.setNameFilter(filter); |
| 205 | |
| 206 | QString pathToOpen; |
| 207 | if (savedPaths.contains(context)) { |
| 208 | pathToOpen = savedPaths[context]; |
| 209 | } else { |
| 210 | pathToOpen = defaultPath; |
| 211 | } |
| 212 | if (!pathToOpen.isEmpty()) { |
| 213 | QFileInfo finfo(pathToOpen); |
| 214 | if (finfo.exists() && finfo.isDir()) { |
| 215 | w.setDirectory(finfo.absoluteFilePath()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | w.setSidebarUrls(urls); |
| 220 | |
| 221 | if (w.exec()) { |
| 222 | savedPaths[context] = w.directory().absolutePath(); |
| 223 | return w.selectedFiles(); |
| 224 | } |
| 225 | savedPaths[context] = w.directory().absolutePath(); |
| 226 | return {}; |
| 227 | } |
| 228 | |
| 229 | QString GuiUtil::BrowseForFile(QString context, QString caption, QString filter, QString defaultPath, QWidget* parentWidget) |
| 230 | { |