| 111 | } |
| 112 | |
| 113 | static QStringList BrowseForFileInternal(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget, bool single) |
| 114 | { |
| 115 | static QMap<QString, QString> savedPaths; |
| 116 | |
| 117 | QFileDialog w(parentWidget, caption); |
| 118 | QSet<QString> locations; |
| 119 | auto f = [&](QStandardPaths::StandardLocation l) |
| 120 | { |
| 121 | QString location = QStandardPaths::writableLocation(l); |
| 122 | QFileInfo finfo(location); |
| 123 | if (!finfo.exists()) { |
| 124 | return; |
| 125 | } |
| 126 | locations.insert(location); |
| 127 | }; |
| 128 | f(QStandardPaths::DesktopLocation); |
| 129 | f(QStandardPaths::DocumentsLocation); |
| 130 | f(QStandardPaths::DownloadLocation); |
| 131 | f(QStandardPaths::HomeLocation); |
| 132 | QList<QUrl> urls; |
| 133 | for (auto location : locations) |
| 134 | { |
| 135 | urls.append(QUrl::fromLocalFile(location)); |
| 136 | } |
| 137 | urls.append(QUrl::fromLocalFile(defaultPath)); |
| 138 | |
| 139 | w.setFileMode(single ? QFileDialog::ExistingFile : QFileDialog::ExistingFiles); |
| 140 | w.setAcceptMode(QFileDialog::AcceptOpen); |
| 141 | w.setNameFilter(filter); |
| 142 | |
| 143 | QString pathToOpen; |
| 144 | if(savedPaths.contains(context)) |
| 145 | { |
| 146 | pathToOpen = savedPaths[context]; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | pathToOpen = defaultPath; |
| 151 | } |
| 152 | if(!pathToOpen.isEmpty()) |
| 153 | { |
| 154 | QFileInfo finfo(pathToOpen); |
| 155 | if(finfo.exists() && finfo.isDir()) |
| 156 | { |
| 157 | w.setDirectory(finfo.absoluteFilePath()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | w.setSidebarUrls(urls); |
| 162 | |
| 163 | if (w.exec()) |
| 164 | { |
| 165 | savedPaths[context] = w.directory().absolutePath(); |
| 166 | return w.selectedFiles(); |
| 167 | } |
| 168 | savedPaths[context] = w.directory().absolutePath(); |
| 169 | return {}; |
| 170 | } |