| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 51 Franklin Street, * |
| 19 |
* Fifth Floor, Boston, MA 02110-1301, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QButtonGroup> |
| 26 |
#include <QCompleter> |
| 27 |
#include <QCryptographicHash> |
| 28 |
#include <QDialogButtonBox> |
| 29 |
#include <QDir> |
| 30 |
#include <QGridLayout> |
| 31 |
#include <QGroupBox> |
| 32 |
#include <QLineEdit> |
| 33 |
#include <QMenuBar> |
| 34 |
#include <QPushButton> |
| 35 |
#include <QRadioButton> |
| 36 |
#include <QRegularExpression> |
| 37 |
#include <QRegularExpressionMatch> |
| 38 |
#include <QResizeEvent> |
| 39 |
#include <QStandardPaths> |
| 40 |
#include <QStyle> |
| 41 |
#include <QUrl> |
| 42 |
|
| 43 |
|
| 44 |
#include <Base/Parameter.h> |
| 45 |
#include <App/Application.h> |
| 46 |
|
| 47 |
#include "FileDialog.h" |
| 48 |
#include "MainWindow.h" |
| 49 |
#include "Tools.h" |
| 50 |
|
| 51 |
|
| 52 |
using namespace Gui; |
| 53 |
|
| 54 |
// An raii-helper struct to disable actions while dialogs are open |
| 55 |
// At least on macos, shortcuts for enabled actions will still trigger while dialogs are open |
| 56 |
struct ActionDisabler |
| 57 |
{ |
| 58 |
ActionDisabler() |
| 59 |
{ |
| 60 |
auto mainWin = Gui::getMainWindow(); |
| 61 |
if (!mainWin) { |
| 62 |
return; |
| 63 |
} |
| 64 |
QMenuBar* menuBar = mainWin->menuBar(); |
| 65 |
if (!menuBar) { |
| 66 |
return; |
| 67 |
} |
| 68 |
auto actions = menuBar->actions(); |
| 69 |
actionsToReenable.reserve(actions.size()); |
| 70 |
for (auto action : actions) { |
| 71 |
if (action->isEnabled()) { |
| 72 |
action->setEnabled(false); |
| 73 |
actionsToReenable.push_back(action); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
~ActionDisabler() |
| 79 |
{ |
| 80 |
for (auto action : actionsToReenable) { |
| 81 |
if (action) { |
| 82 |
action->setEnabled(true); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
FC_DISABLE_COPY(ActionDisabler) |
| 88 |
|
| 89 |
std::vector<QAction*> actionsToReenable {}; |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
bool DialogOptions::dontUseNativeFileDialog() |
| 94 |
{ |
| 95 |
#if defined(USE_QT_DIALOGS) |
| 96 |
constexpr bool notNativeDialog = true; |
| 97 |
#else |
| 98 |
constexpr bool notNativeDialog = false; |
| 99 |
#endif |
| 100 |
|
| 101 |
ParameterGrp::handle group = App::GetApplication() |
| 102 |
.GetUserParameter() |
| 103 |
.GetGroup("BaseApp") |
| 104 |
->GetGroup("Preferences") |
| 105 |
->GetGroup("Dialog"); |
| 106 |
return group->GetBool("DontUseNativeDialog", notNativeDialog); |
| 107 |
} |
| 108 |
|
| 109 |
bool DialogOptions::dontUseNativeColorDialog() |
| 110 |
{ |
| 111 |
#if defined(USE_QT_DIALOGS) |
| 112 |
constexpr bool notNativeDialog = true; |
| 113 |
#else |
| 114 |
constexpr bool notNativeDialog = false; |
| 115 |
#endif |
| 116 |
|
| 117 |
ParameterGrp::handle group = App::GetApplication() |
| 118 |
.GetUserParameter() |
| 119 |
.GetGroup("BaseApp") |
| 120 |
->GetGroup("Preferences") |
| 121 |
->GetGroup("Dialog"); |
| 122 |
return group->GetBool("DontUseNativeColorDialog", notNativeDialog); |
| 123 |
} |
| 124 |
|
| 125 |
/* TRANSLATOR Gui::FileDialog */ |
| 126 |
|
| 127 |
FileDialog::FileDialog(QWidget* parent) |
| 128 |
: QFileDialog(parent) |
| 129 |
{ |
| 130 |
connect(this, &QFileDialog::filterSelected, this, &FileDialog::onSelectedFilter); |
| 131 |
} |
| 132 |
|
| 133 |
FileDialog::~FileDialog() = default; |
| 134 |
|
| 135 |
void FileDialog::onSelectedFilter(const QString& /*filter*/) |
| 136 |
{ |
| 137 |
QRegularExpression rx(QLatin1String(R"(\(\*.(\w+))")); |
| 138 |
QString suf = selectedNameFilter(); |
| 139 |
auto match = rx.match(suf); |
| 140 |
if (match.hasMatch()) { |
| 141 |
suf = match.captured(1); |
| 142 |
setDefaultSuffix(suf); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
QList<QUrl> FileDialog::fetchSidebarUrls() |
| 147 |
{ |
| 148 |
QStringList list; |
| 149 |
list << QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); |
| 150 |
list << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); |
| 151 |
list << QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); |
| 152 |
list << QStandardPaths::writableLocation(QStandardPaths::HomeLocation); |
| 153 |
list << QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); |
| 154 |
list << getWorkingDirectory(); |
| 155 |
list << restoreLocation(); |
| 156 |
list << QDir::currentPath(); |
| 157 |
|
| 158 |
QList<QUrl> urls; |
| 159 |
for (const auto& it : list) { |
| 160 |
if (QFileInfo::exists(it)) { |
| 161 |
urls << QUrl::fromLocalFile(it); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return urls; |
| 166 |
} |
| 167 |
|
| 168 |
bool FileDialog::hasSuffix(const QString& ext) const |
| 169 |
{ |
| 170 |
QRegularExpression rx( |
| 171 |
QStringLiteral("\\*.(%1)\\W").arg(ext), |
| 172 |
QRegularExpression::CaseInsensitiveOption |
| 173 |
); |
| 174 |
QStringList filters = nameFilters(); |
| 175 |
for (const auto& str : filters) { |
| 176 |
if (rx.match(str).hasMatch()) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
void FileDialog::accept() |
| 185 |
{ |
| 186 |
// When saving to a file make sure that the entered filename ends with the selected |
| 187 |
// file filter |
| 188 |
if (acceptMode() == QFileDialog::AcceptSave) { |
| 189 |
QStringList files = selectedFiles(); |
| 190 |
if (!files.isEmpty()) { |
| 191 |
QString ext = this->defaultSuffix(); |
| 192 |
QString file = files.front(); |
| 193 |
QString suffix = QFileInfo(file).suffix(); |
| 194 |
// #0001928: do not add a suffix if a file with suffix is entered |
| 195 |
// #0002209: make sure that the entered suffix is part of one of the filters |
| 196 |
if (!ext.isEmpty() && (suffix.isEmpty() || !hasSuffix(suffix))) { |
| 197 |
file = QStringLiteral("%1.%2").arg(file, ext); |
| 198 |
// That's the built-in line edit |
| 199 |
auto fileNameEdit = this->findChild<QLineEdit*>(QStringLiteral("fileNameEdit")); |
| 200 |
if (fileNameEdit) { |
| 201 |
fileNameEdit->setText(file); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
QFileDialog::accept(); |
| 207 |
} |
| 208 |
|
| 209 |
void FileDialog::getSuffixesDescription(QStringList& suffixes, const QString* suffixDescriptions) |
| 210 |
{ |
| 211 |
QRegularExpression rx; |
| 212 |
// start the raw string with a ( |
| 213 |
// match a *, a . and at least one word character (a-z, A-Z, 0-9, _) with \*\.\w+ |
| 214 |
// end the raw string with a ) |
| 215 |
rx.setPattern(QLatin1String(R"(\*\.\w+)")); |
| 216 |
|
| 217 |
QRegularExpressionMatchIterator i = rx.globalMatch(*suffixDescriptions); |
| 218 |
while (i.hasNext()) { |
| 219 |
QRegularExpressionMatch match = i.next(); |
| 220 |
QString suffix = match.captured(0); |
| 221 |
suffixes << suffix; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* This is a convenience static function that will return a file name selected by the user. The file |
| 227 |
* does not have to exist. |
| 228 |
*/ |
| 229 |
QString FileDialog::getSaveFileName( |
| 230 |
QWidget* parent, |
| 231 |
const QString& caption, |
| 232 |
const QString& dir, |
| 233 |
const QString& filter, |
| 234 |
QString* selectedFilter, |
| 235 |
Options options |
| 236 |
) |
| 237 |
{ |
| 238 |
ActionDisabler actionDisabler {}; |
| 239 |
|
| 240 |
QString dirName = dir; |
| 241 |
bool hasFilename = false; |
| 242 |
if (dirName.isEmpty()) { |
| 243 |
dirName = getWorkingDirectory(); |
| 244 |
} |
| 245 |
else { |
| 246 |
QFileInfo fi(dir); |
| 247 |
if (fi.isRelative()) { |
| 248 |
dirName = getWorkingDirectory(); |
| 249 |
dirName += QLatin1String("/"); |
| 250 |
dirName += fi.fileName(); |
| 251 |
} |
| 252 |
if (!fi.fileName().isEmpty()) { |
| 253 |
hasFilename = true; |
| 254 |
} |
| 255 |
|
| 256 |
// get the suffix for the filter: use the selected filter if there is one, |
| 257 |
// otherwise find the first valid suffix in the complete list of filters |
| 258 |
const QString* filterToSearch; |
| 259 |
if (selectedFilter && !selectedFilter->isEmpty()) { |
| 260 |
filterToSearch = selectedFilter; |
| 261 |
} |
| 262 |
else { |
| 263 |
filterToSearch = &filter; |
| 264 |
} |
| 265 |
|
| 266 |
QStringList filterSuffixes; |
| 267 |
getSuffixesDescription(filterSuffixes, filterToSearch); |
| 268 |
const QString fiSuffix = fi.suffix(); |
| 269 |
const QString dotSuffix = QLatin1String("*.") + fiSuffix; // To match with filterSuffixes |
| 270 |
if (fiSuffix.isEmpty() || !filterSuffixes.contains(dotSuffix)) { |
| 271 |
// there is no suffix or not a suffix that matches the filter, so |
| 272 |
// default to the first suffix of the filter |
| 273 |
if (!filterSuffixes.isEmpty()) { |
| 274 |
dirName += filterSuffixes[0].mid(1); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
QString windowTitle = caption; |
| 280 |
if (windowTitle.isEmpty()) { |
| 281 |
windowTitle = FileDialog::tr("Save As"); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
// NOTE: We must not change the specified file name afterwards as we may return the name of an |
| 286 |
// already existing file. Hence we must extract the first matching suffix from the filter list |
| 287 |
// and append it before showing the file dialog. |
| 288 |
QString file; |
| 289 |
if (DialogOptions::dontUseNativeFileDialog()) { |
| 290 |
QList<QUrl> urls = fetchSidebarUrls(); |
| 291 |
|
| 292 |
options |= QFileDialog::DontUseNativeDialog; |
| 293 |
|
| 294 |
FileDialog dlg(parent); |
| 295 |
dlg.setOptions(options); |
| 296 |
dlg.setWindowTitle(windowTitle); |
| 297 |
dlg.setSidebarUrls(urls); |
| 298 |
auto iconprov = std::make_unique<FileIconProvider>(); |
| 299 |
dlg.setIconProvider(iconprov.get()); |
| 300 |
dlg.setFileMode(QFileDialog::AnyFile); |
| 301 |
dlg.setAcceptMode(QFileDialog::AcceptSave); |
| 302 |
dlg.setDirectory(dirName); |
| 303 |
if (hasFilename) { |
| 304 |
dlg.selectFile(dirName); |
| 305 |
} |
| 306 |
dlg.setNameFilters(filter.split(QLatin1String(";;"))); |
| 307 |
if (selectedFilter && !selectedFilter->isEmpty()) { |
| 308 |
dlg.selectNameFilter(*selectedFilter); |
| 309 |
} |
| 310 |
dlg.onSelectedFilter(dlg.selectedNameFilter()); |
| 311 |
dlg.setOption(QFileDialog::HideNameFilterDetails, false); |
| 312 |
dlg.setOption(QFileDialog::DontConfirmOverwrite, false); |
| 313 |
if (dlg.exec() == QDialog::Accepted) { |
| 314 |
if (selectedFilter) { |
| 315 |
*selectedFilter = dlg.selectedNameFilter(); |
| 316 |
} |
| 317 |
file = dlg.selectedFiles().constFirst(); |
| 318 |
} |
| 319 |
} |
| 320 |
else { |
| 321 |
file = QFileDialog::getSaveFileName(parent, windowTitle, dirName, filter, selectedFilter, options); |
| 322 |
file = QDir::fromNativeSeparators(file); |
| 323 |
} |
| 324 |
|
| 325 |
if (!file.isEmpty()) { |
| 326 |
setWorkingDirectory(file); |
| 327 |
return file; |
| 328 |
} |
| 329 |
else { |
| 330 |
return {}; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* This is a convenience static function that will return an existing directory selected by the user. |
| 336 |
*/ |
| 337 |
QString FileDialog::getExistingDirectory( |
| 338 |
QWidget* parent, |
| 339 |
const QString& caption, |
| 340 |
const QString& dir, |
| 341 |
Options options |
| 342 |
) |
| 343 |
{ |
| 344 |
ActionDisabler actionDisabler {}; |
| 345 |
QString path = QFileDialog::getExistingDirectory(parent, caption, dir, options); |
| 346 |
// valid path was selected |
| 347 |
if (!path.isEmpty()) { |
| 348 |
QDir d(path); |
| 349 |
path = d.path(); // get path in Qt manner |
| 350 |
} |
| 351 |
|
| 352 |
return path; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* This is a convenience static function that returns an existing file selected by the user. |
| 357 |
* If the user pressed Cancel, it returns a null string. |
| 358 |
*/ |
| 359 |
QString FileDialog::getOpenFileName( |
| 360 |
QWidget* parent, |
| 361 |
const QString& caption, |
| 362 |
const QString& dir, |
| 363 |
const QString& filter, |
| 364 |
QString* selectedFilter, |
| 365 |
Options options |
| 366 |
) |
| 367 |
{ |
| 368 |
ActionDisabler actionDisabler {}; |
| 369 |
QString dirName = dir; |
| 370 |
if (dirName.isEmpty()) { |
| 371 |
dirName = getWorkingDirectory(); |
| 372 |
} |
| 373 |
|
| 374 |
QString windowTitle = caption; |
| 375 |
if (windowTitle.isEmpty()) { |
| 376 |
windowTitle = FileDialog::tr("Open"); |
| 377 |
} |
| 378 |
|
| 379 |
QString file; |
| 380 |
if (DialogOptions::dontUseNativeFileDialog()) { |
| 381 |
QList<QUrl> urls = fetchSidebarUrls(); |
| 382 |
|
| 383 |
options |= QFileDialog::DontUseNativeDialog; |
| 384 |
|
| 385 |
FileDialog dlg(parent); |
| 386 |
dlg.setOptions(options); |
| 387 |
dlg.setWindowTitle(windowTitle); |
| 388 |
dlg.setSidebarUrls(urls); |
| 389 |
auto iconprov = std::make_unique<FileIconProvider>(); |
| 390 |
dlg.setIconProvider(iconprov.get()); |
| 391 |
dlg.setFileMode(QFileDialog::ExistingFile); |
| 392 |
dlg.setAcceptMode(QFileDialog::AcceptOpen); |
| 393 |
dlg.setDirectory(dirName); |
| 394 |
dlg.setNameFilters(filter.split(QLatin1String(";;"))); |
| 395 |
dlg.setOption(QFileDialog::HideNameFilterDetails, false); |
| 396 |
if (selectedFilter && !selectedFilter->isEmpty()) { |
| 397 |
dlg.selectNameFilter(*selectedFilter); |
| 398 |
} |
| 399 |
if (dlg.exec() == QDialog::Accepted) { |
| 400 |
if (selectedFilter) { |
| 401 |
*selectedFilter = dlg.selectedNameFilter(); |
| 402 |
} |
| 403 |
file = dlg.selectedFiles().constFirst(); |
| 404 |
} |
| 405 |
} |
| 406 |
else { |
| 407 |
file = QFileDialog::getOpenFileName(parent, windowTitle, dirName, filter, selectedFilter, options); |
| 408 |
file = QDir::fromNativeSeparators(file); |
| 409 |
} |
| 410 |
|
| 411 |
if (!file.isEmpty()) { |
| 412 |
setWorkingDirectory(file); |
| 413 |
return file; |
| 414 |
} |
| 415 |
else { |
| 416 |
return {}; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* This is a convenience static function that will return one or more existing files selected by the |
| 422 |
* user. |
| 423 |
*/ |
| 424 |
QStringList FileDialog::getOpenFileNames( |
| 425 |
QWidget* parent, |
| 426 |
const QString& caption, |
| 427 |
const QString& dir, |
| 428 |
const QString& filter, |
| 429 |
QString* selectedFilter, |
| 430 |
Options options |
| 431 |
) |
| 432 |
{ |
| 433 |
ActionDisabler actionDisabler {}; |
| 434 |
QString dirName = dir; |
| 435 |
if (dirName.isEmpty()) { |
| 436 |
dirName = getWorkingDirectory(); |
| 437 |
} |
| 438 |
|
| 439 |
QString windowTitle = caption; |
| 440 |
if (windowTitle.isEmpty()) { |
| 441 |
windowTitle = FileDialog::tr("Open"); |
| 442 |
} |
| 443 |
|
| 444 |
QStringList files; |
| 445 |
if (DialogOptions::dontUseNativeFileDialog()) { |
| 446 |
QList<QUrl> urls = fetchSidebarUrls(); |
| 447 |
|
| 448 |
options |= QFileDialog::DontUseNativeDialog; |
| 449 |
|
| 450 |
FileDialog dlg(parent); |
| 451 |
dlg.setOptions(options); |
| 452 |
dlg.setWindowTitle(windowTitle); |
| 453 |
dlg.setSidebarUrls(urls); |
| 454 |
auto iconprov = std::make_unique<FileIconProvider>(); |
| 455 |
dlg.setIconProvider(iconprov.get()); |
| 456 |
dlg.setFileMode(QFileDialog::ExistingFiles); |
| 457 |
dlg.setAcceptMode(QFileDialog::AcceptOpen); |
| 458 |
dlg.setDirectory(dirName); |
| 459 |
dlg.setNameFilters(filter.split(QLatin1String(";;"))); |
| 460 |
dlg.setOption(QFileDialog::HideNameFilterDetails, false); |
| 461 |
if (selectedFilter && !selectedFilter->isEmpty()) { |
| 462 |
dlg.selectNameFilter(*selectedFilter); |
| 463 |
} |
| 464 |
if (dlg.exec() == QDialog::Accepted) { |
| 465 |
if (selectedFilter) { |
| 466 |
*selectedFilter = dlg.selectedNameFilter(); |
| 467 |
} |
| 468 |
files = dlg.selectedFiles(); |
| 469 |
} |
| 470 |
} |
| 471 |
else { |
| 472 |
files = QFileDialog::getOpenFileNames(parent, windowTitle, dirName, filter, selectedFilter, options); |
| 473 |
for (auto& file : files) { |
| 474 |
file = QDir::fromNativeSeparators(file); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if (!files.isEmpty()) { |
| 479 |
setWorkingDirectory(files.front()); |
| 480 |
} |
| 481 |
|
| 482 |
return files; |
| 483 |
} |
| 484 |
|
| 485 |
QString FileDialog::workingDirectory; |
| 486 |
|
| 487 |
/** |
| 488 |
* Returns the working directory for the file dialog. This path can be used in |
| 489 |
* combination with getSaveFileName(), getOpenFileName(), getOpenFileNames() or |
| 490 |
* getExistingDirectory() to open the dialog in this path. |
| 491 |
*/ |
| 492 |
QString FileDialog::getWorkingDirectory() |
| 493 |
{ |
| 494 |
return workingDirectory; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Sets the working directory to \a dir for the file dialog. |
| 499 |
* If \a dir is a file then the path only is taken. |
| 500 |
* getWorkingDirectory() returns the working directory. |
| 501 |
*/ |
| 502 |
void FileDialog::setWorkingDirectory(const QString& dir) |
| 503 |
{ |
| 504 |
QString dirName = dir; |
| 505 |
if (!dir.isEmpty()) { |
| 506 |
QFileInfo info(dir); |
| 507 |
if (!info.exists() || info.isFile()) { |
| 508 |
dirName = info.absolutePath(); |
| 509 |
} |
| 510 |
else { |
| 511 |
dirName = info.absoluteFilePath(); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
workingDirectory = dirName; |
| 516 |
saveLocation(dirName); |
| 517 |
} |
| 518 |
|
| 519 |
/*! |
| 520 |
* \brief Return the last location where a file save or load dialog was used. |
| 521 |
* \return QString |
| 522 |
*/ |
| 523 |
QString FileDialog::restoreLocation() |
| 524 |
{ |
| 525 |
std::string path = App::GetApplication().Config()["UserHomePath"]; |
| 526 |
Base::Reference<ParameterGrp> hPath = App::GetApplication() |
| 527 |
.GetUserParameter() |
| 528 |
.GetGroup("BaseApp") |
| 529 |
->GetGroup("Preferences") |
| 530 |
->GetGroup("General"); |
| 531 |
std::string dir = hPath->GetASCII("FileOpenSavePath", path.c_str()); |
| 532 |
QFileInfo fi(QString::fromUtf8(dir.c_str())); |
| 533 |
if (!fi.exists()) { |
| 534 |
dir = path; |
| 535 |
} |
| 536 |
return QString::fromUtf8(dir.c_str()); |
| 537 |
} |
| 538 |
|
| 539 |
/*! |
| 540 |
* \brief Save the last location where a file save or load dialog was used. |
| 541 |
* \param dirName |
| 542 |
*/ |
| 543 |
void FileDialog::saveLocation(const QString& dirName) |
| 544 |
{ |
| 545 |
Base::Reference<ParameterGrp> hPath = App::GetApplication() |
| 546 |
.GetUserParameter() |
| 547 |
.GetGroup("BaseApp") |
| 548 |
->GetGroup("Preferences") |
| 549 |
->GetGroup("General"); |
| 550 |
hPath->SetASCII("FileOpenSavePath", dirName.toUtf8()); |
| 551 |
} |
| 552 |
|
| 553 |
// ====================================================================== |
| 554 |
|
| 555 |
/* TRANSLATOR Gui::FileOptionsDialog */ |
| 556 |
|
| 557 |
FileOptionsDialog::FileOptionsDialog(QWidget* parent, Qt::WindowFlags fl) |
| 558 |
: QFileDialog(parent, fl) |
| 559 |
, extensionPos(ExtensionRight) |
| 560 |
{ |
| 561 |
extensionButton = new QPushButton(this); |
| 562 |
extensionButton->setText(tr("Extended")); |
| 563 |
|
| 564 |
setOption(QFileDialog::DontUseNativeDialog); |
| 565 |
|
| 566 |
// search for the grid layout and add the new button |
| 567 |
auto grid = this->findChild<QGridLayout*>(); |
| 568 |
grid->addWidget(extensionButton, 4, 2, Qt::AlignLeft); |
| 569 |
|
| 570 |
connect(extensionButton, &QPushButton::clicked, this, &FileOptionsDialog::toggleExtension); |
| 571 |
} |
| 572 |
|
| 573 |
FileOptionsDialog::~FileOptionsDialog() = default; |
| 574 |
|
| 575 |
void FileOptionsDialog::accept() |
| 576 |
{ |
| 577 |
// Fixes a bug of the default implementation when entering an asterisk |
| 578 |
auto filename = this->findChild<QLineEdit*>(); |
| 579 |
QString fn = filename->text(); |
| 580 |
if (fn.startsWith(QLatin1String("*"))) { |
| 581 |
QFileInfo fi(fn); |
| 582 |
QString ext = fi.suffix(); |
| 583 |
ext.prepend(QLatin1String("*.")); |
| 584 |
QStringList filters = this->nameFilters(); |
| 585 |
bool ok = false; |
| 586 |
// Compare the given suffix with the suffixes of all filters |
| 587 |
QString filter; |
| 588 |
for (const auto& it : filters) { |
| 589 |
if (it.contains(ext)) { |
| 590 |
filter = it; |
| 591 |
ok = true; |
| 592 |
break; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
// if no appropriate filter was found the add the 'All files' filter |
| 597 |
if (!ok) { |
| 598 |
filter = tr("All files (*.*)"); |
| 599 |
filters << filter; |
| 600 |
setNameFilters(filters); |
| 601 |
} |
| 602 |
|
| 603 |
// empty the line edit |
| 604 |
filename->blockSignals(true); |
| 605 |
filename->clear(); |
| 606 |
filename->blockSignals(false); |
| 607 |
selectNameFilter(filter); |
| 608 |
|
| 609 |
return; |
| 610 |
} |
| 611 |
else if (!fn.isEmpty()) { |
| 612 |
QFileInfo fi(fn); |
| 613 |
QString ext = fi.completeSuffix(); |
| 614 |
QRegularExpression rx(QLatin1String(R"(\(\*.(\w+))")); |
| 615 |
QString suf = selectedNameFilter(); |
| 616 |
auto match = rx.match(suf); |
| 617 |
if (match.hasMatch()) { |
| 618 |
suf = match.captured(1); |
| 619 |
} |
| 620 |
if (ext.isEmpty()) { |
| 621 |
setDefaultSuffix(suf); |
| 622 |
} |
| 623 |
else if (ext.toLower() != suf.toLower()) { |
| 624 |
fn = QStringLiteral("%1.%2").arg(fn, suf); |
| 625 |
selectFile(fn); |
| 626 |
// That's the built-in line edit (fixes Debian bug #811200) |
| 627 |
auto fileNameEdit = this->findChild<QLineEdit*>(QStringLiteral("fileNameEdit")); |
| 628 |
if (fileNameEdit) { |
| 629 |
fileNameEdit->setText(fn); |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
QFileDialog::accept(); |
| 635 |
} |
| 636 |
|
| 637 |
void FileOptionsDialog::toggleExtension() |
| 638 |
{ |
| 639 |
if (extensionWidget) { |
| 640 |
bool showIt = !extensionWidget->isVisible(); |
| 641 |
if (showIt) { |
| 642 |
oldSize = size(); |
| 643 |
QSize s(extensionWidget->sizeHint() |
| 644 |
.expandedTo(extensionWidget->minimumSize()) |
| 645 |
.boundedTo(extensionWidget->maximumSize())); |
| 646 |
if (extensionPos == ExtensionRight) { |
| 647 |
setFixedSize(width() + s.width(), height()); |
| 648 |
} |
| 649 |
else { |
| 650 |
setFixedSize(width(), height() + s.height()); |
| 651 |
} |
| 652 |
|
| 653 |
extensionWidget->show(); |
| 654 |
} |
| 655 |
else { |
| 656 |
extensionWidget->hide(); |
| 657 |
setFixedSize(oldSize); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
void FileOptionsDialog::setOptionsWidget(FileOptionsDialog::ExtensionPosition pos, QWidget* w, bool show) |
| 663 |
{ |
| 664 |
extensionPos = pos; |
| 665 |
extensionWidget = w; |
| 666 |
if (extensionWidget->parentWidget() != this) { |
| 667 |
extensionWidget->setParent(this); |
| 668 |
} |
| 669 |
|
| 670 |
auto grid = this->findChild<QGridLayout*>(); |
| 671 |
|
| 672 |
if (extensionPos == ExtensionRight) { |
| 673 |
int cols = grid->columnCount(); |
| 674 |
grid->addWidget(extensionWidget, 0, cols, -1, -1); |
| 675 |
setMinimumHeight(extensionWidget->height()); |
| 676 |
} |
| 677 |
else if (extensionPos == ExtensionBottom) { |
| 678 |
int rows = grid->rowCount(); |
| 679 |
grid->addWidget(extensionWidget, rows, 0, -1, -1); |
| 680 |
setMinimumWidth(extensionWidget->width()); |
| 681 |
} |
| 682 |
|
| 683 |
oldSize = size(); |
| 684 |
w->hide(); |
| 685 |
if (show) { |
| 686 |
toggleExtension(); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
QWidget* FileOptionsDialog::getOptionsWidget() const |
| 691 |
{ |
| 692 |
return extensionWidget; |
| 693 |
} |
| 694 |
|
| 695 |
// ====================================================================== |
| 696 |
|
| 697 |
/** |
| 698 |
* Constructs an empty file icon provider called \a name, with the parent \a parent. |
| 699 |
*/ |
| 700 |
FileIconProvider::FileIconProvider() = default; |
| 701 |
|
| 702 |
FileIconProvider::~FileIconProvider() = default; |
| 703 |
|
| 704 |
QIcon FileIconProvider::icon(IconType type) const |
| 705 |
{ |
| 706 |
return QFileIconProvider::icon(type); |
| 707 |
} |
| 708 |
|
| 709 |
QIcon FileIconProvider::icon(const QFileInfo& info) const |
| 710 |
{ |
| 711 |
auto toUrl = [](const QFileInfo& info) { |
| 712 |
QFileInfo fi(info); |
| 713 |
fi.makeAbsolute(); |
| 714 |
QString fileName = fi.absoluteFilePath(); |
| 715 |
if (fi.isSymLink()) { |
| 716 |
fileName = fi.symLinkTarget(); |
| 717 |
} |
| 718 |
|
| 719 |
return QUrl::fromLocalFile(fileName).toString(); |
| 720 |
}; |
| 721 |
|
| 722 |
auto urlToThumbnail = [](const QString& filename) { |
| 723 |
QString hash = QString::fromLatin1( |
| 724 |
QCryptographicHash::hash(filename.toUtf8(), QCryptographicHash::Md5).toHex() |
| 725 |
); |
| 726 |
QString cache = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation); |
| 727 |
return QStringLiteral("%1/thumbnails/normal/%2.png").arg(cache, hash); |
| 728 |
}; |
| 729 |
|
| 730 |
auto iconFromFile = [](const QString& filename) { |
| 731 |
if (QFile::exists(filename)) { |
| 732 |
QIcon icon(filename); |
| 733 |
if (!icon.isNull()) { |
| 734 |
return icon; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
return QIcon(QStringLiteral(":/icons/freecad-doc.png")); |
| 739 |
}; |
| 740 |
|
| 741 |
if (info.suffix().toLower() == QLatin1String("fcstd")) { |
| 742 |
// Check if a thumbnail is available |
| 743 |
QString fileName = toUrl(info); |
| 744 |
QString thumb = urlToThumbnail(fileName); |
| 745 |
return iconFromFile(thumb); |
| 746 |
} |
| 747 |
else if (info.suffix().toLower().startsWith(QLatin1String("fcstd"))) { |
| 748 |
QIcon icon(QStringLiteral(":/icons/freecad-doc.png")); |
| 749 |
QIcon darkIcon; |
| 750 |
int w = QApplication::style()->pixelMetric(QStyle::PM_ListViewIconSize); |
| 751 |
darkIcon.addPixmap(icon.pixmap(w, w, QIcon::Disabled, QIcon::Off), QIcon::Normal, QIcon::Off); |
| 752 |
darkIcon.addPixmap(icon.pixmap(w, w, QIcon::Disabled, QIcon::On), QIcon::Normal, QIcon::On); |
| 753 |
return darkIcon; |
| 754 |
} |
| 755 |
return QFileIconProvider::icon(info); |
| 756 |
} |
| 757 |
|
| 758 |
QString FileIconProvider::type(const QFileInfo& info) const |
| 759 |
{ |
| 760 |
return QFileIconProvider::type(info); |
| 761 |
} |
| 762 |
|
| 763 |
// -------------------------------------------------------------------- |
| 764 |
|
| 765 |
/* TRANSLATOR Gui::FileChooser */ |
| 766 |
|
| 767 |
/** |
| 768 |
* Constructs a file chooser called \a name with the parent \a parent. |
| 769 |
*/ |
| 770 |
FileChooser::FileChooser(QWidget* parent) |
| 771 |
: QWidget(parent) |
| 772 |
, md(File) |
| 773 |
, accMode(AcceptOpen) |
| 774 |
, _filter(QString()) |
| 775 |
{ |
| 776 |
auto layout = new QHBoxLayout(this); |
| 777 |
layout->setContentsMargins(0, 0, 0, 0); |
| 778 |
layout->setSpacing(2); |
| 779 |
|
| 780 |
lineEdit = new QLineEdit(this); |
| 781 |
completer = new QCompleter(this); |
| 782 |
completer->setMaxVisibleItems(12); |
| 783 |
fs_model = new QFileSystemModel(completer); |
| 784 |
fs_model->setRootPath(QStringLiteral("")); |
| 785 |
completer->setModel(fs_model); |
| 786 |
lineEdit->setCompleter(completer); |
| 787 |
|
| 788 |
|
| 789 |
connect(lineEdit, &QLineEdit::textChanged, this, &FileChooser::fileNameChanged); |
| 790 |
connect(lineEdit, &QLineEdit::editingFinished, this, &FileChooser::editingFinished); |
| 791 |
|
| 792 |
button = new QPushButton(QStringLiteral("…"), this); |
| 793 |
|
| 794 |
#if defined(Q_OS_MACOS) |
| 795 |
button->setAttribute(Qt::WA_LayoutUsesWidgetRect); // layout size from QMacStyle was not correct |
| 796 |
#endif |
| 797 |
|
| 798 |
layout->addWidget(lineEdit, 1); |
| 799 |
layout->addWidget(button, -1); |
| 800 |
|
| 801 |
connect(button, &QPushButton::clicked, this, &FileChooser::chooseFile); |
| 802 |
|
| 803 |
setFocusProxy(lineEdit); |
| 804 |
} |
| 805 |
|
| 806 |
FileChooser::~FileChooser() = default; |
| 807 |
|
| 808 |
void FileChooser::resizeEvent(QResizeEvent* e) |
| 809 |
{ |
| 810 |
button->setFixedHeight(e->size().height()); |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* \property FileChooser::fileName |
| 815 |
* |
| 816 |
* This property holds the file name. |
| 817 |
* Set this property's value with setFileName() and get this property's value with fileName(). |
| 818 |
* |
| 819 |
* \sa fileName(), setFileName(). |
| 820 |
*/ |
| 821 |
QString FileChooser::fileName() const |
| 822 |
{ |
| 823 |
return lineEdit->text(); |
| 824 |
} |
| 825 |
|
| 826 |
void FileChooser::editingFinished() |
| 827 |
{ |
| 828 |
QString le_converted = QDir::fromNativeSeparators(lineEdit->text()); |
| 829 |
lineEdit->setText(le_converted); |
| 830 |
FileDialog::setWorkingDirectory(le_converted); |
| 831 |
Q_EMIT fileNameSelected(le_converted); |
| 832 |
} |
| 833 |
|
| 834 |
/** |
| 835 |
* Sets the file name \a fn. |
| 836 |
*/ |
| 837 |
void FileChooser::setFileName(const QString& fn) |
| 838 |
{ |
| 839 |
lineEdit->setText(fn); |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* Opens a FileDialog to choose either a file or a directory in dependency of the |
| 844 |
* value of the Mode property. |
| 845 |
*/ |
| 846 |
void FileChooser::chooseFile() |
| 847 |
{ |
| 848 |
ActionDisabler actionDisabler {}; |
| 849 |
QString prechosenDirectory = lineEdit->text(); |
| 850 |
if (prechosenDirectory.isEmpty()) { |
| 851 |
prechosenDirectory = FileDialog::getWorkingDirectory(); |
| 852 |
} |
| 853 |
|
| 854 |
QFileDialog::Options dlgOpt; |
| 855 |
if (DialogOptions::dontUseNativeFileDialog()) { |
| 856 |
dlgOpt = QFileDialog::DontUseNativeDialog; |
| 857 |
} |
| 858 |
|
| 859 |
QString fn; |
| 860 |
if (mode() == File) { |
| 861 |
if (acceptMode() == AcceptOpen) { |
| 862 |
fn = QFileDialog::getOpenFileName( |
| 863 |
this, |
| 864 |
tr("Select a File"), |
| 865 |
prechosenDirectory, |
| 866 |
_filter, |
| 867 |
nullptr, |
| 868 |
dlgOpt |
| 869 |
); |
| 870 |
} |
| 871 |
else { |
| 872 |
fn = QFileDialog::getSaveFileName( |
| 873 |
this, |
| 874 |
tr("Select a File"), |
| 875 |
prechosenDirectory, |
| 876 |
_filter, |
| 877 |
nullptr, |
| 878 |
dlgOpt |
| 879 |
); |
| 880 |
} |
| 881 |
} |
| 882 |
else { |
| 883 |
QFileDialog::Options option = QFileDialog::ShowDirsOnly | dlgOpt; |
| 884 |
fn = QFileDialog::getExistingDirectory(this, tr("Select a Directory"), prechosenDirectory, option); |
| 885 |
} |
| 886 |
|
| 887 |
if (!fn.isEmpty()) { |
| 888 |
fn = QDir::fromNativeSeparators(fn); |
| 889 |
lineEdit->setText(fn); |
| 890 |
FileDialog::setWorkingDirectory(fn); |
| 891 |
Q_EMIT fileNameSelected(fn); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
/** |
| 896 |
* Sets the accept mode. |
| 897 |
*/ |
| 898 |
void FileChooser::setAcceptMode(FileChooser::AcceptMode mode) |
| 899 |
{ |
| 900 |
accMode = mode; |
| 901 |
Q_EMIT acceptModeChanged(accMode); |
| 902 |
} |
| 903 |
|
| 904 |
/** |
| 905 |
* \property FileChooser::mode |
| 906 |
* |
| 907 |
* This property holds whether the widgets selects either a file or a directory. |
| 908 |
* The default value of chooseFile is set to File. |
| 909 |
* |
| 910 |
* \sa chooseFile(), mode(), setMode(). |
| 911 |
*/ |
| 912 |
FileChooser::Mode FileChooser::mode() const |
| 913 |
{ |
| 914 |
return md; |
| 915 |
} |
| 916 |
|
| 917 |
/** |
| 918 |
* If \a m is File the widget is set to choose a file, otherwise it is set to |
| 919 |
* choose a directory. |
| 920 |
*/ |
| 921 |
void FileChooser::setMode(FileChooser::Mode m) |
| 922 |
{ |
| 923 |
md = m; |
| 924 |
Q_EMIT modeChanged(md); |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* \property FileChooser::filter |
| 929 |
* |
| 930 |
* This property holds the set filter to choose a file. This property is used only if |
| 931 |
* FileChooser::Mode is set to File. |
| 932 |
* |
| 933 |
* \sa chooseFile(), filter(), setFilter(). |
| 934 |
*/ |
| 935 |
QString FileChooser::filter() const |
| 936 |
{ |
| 937 |
return _filter; |
| 938 |
} |
| 939 |
|
| 940 |
/** |
| 941 |
* Sets the filter for choosing a file. |
| 942 |
*/ |
| 943 |
void FileChooser::setFilter(const QString& filter) |
| 944 |
{ |
| 945 |
_filter = filter; |
| 946 |
Q_EMIT filterChanged(_filter); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Sets the browse button's text to \a txt. |
| 951 |
*/ |
| 952 |
void FileChooser::setButtonText(const QString& txt) |
| 953 |
{ |
| 954 |
button->setText(txt); |
| 955 |
int w1 = 2 * QtTools::horizontalAdvance(button->fontMetrics(), txt); |
| 956 |
int w2 = 2 * QtTools::horizontalAdvance(button->fontMetrics(), QStringLiteral(" … ")); |
| 957 |
button->setMinimumWidth(std::max(w1, w2)); |
| 958 |
Q_EMIT buttonTextChanged(txt); |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* Returns the browse button's text. |
| 963 |
*/ |
| 964 |
QString FileChooser::buttonText() const |
| 965 |
{ |
| 966 |
return button->text(); |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
// ---------------------------------------------------------------------- |
| 971 |
|
| 972 |
/* TRANSLATOR Gui::SelectModule */ |
| 973 |
|
| 974 |
SelectModule::SelectModule(const QString& type, const SelectModule::Dict& types, QWidget* parent) |
| 975 |
: QDialog(parent, Qt::WindowTitleHint) |
| 976 |
{ |
| 977 |
setWindowTitle(tr("Select Module")); |
| 978 |
groupBox = new QGroupBox(this); |
| 979 |
groupBox->setTitle(tr("Open %1 as").arg(type)); |
| 980 |
|
| 981 |
group = new QButtonGroup(this); |
| 982 |
gridLayout = new QGridLayout(this); |
| 983 |
gridLayout->setSpacing(6); |
| 984 |
gridLayout->setContentsMargins(9, 9, 9, 9); |
| 985 |
|
| 986 |
gridLayout1 = new QGridLayout(groupBox); |
| 987 |
gridLayout1->setSpacing(6); |
| 988 |
gridLayout1->setContentsMargins(9, 9, 9, 9); |
| 989 |
|
| 990 |
int index = 0; |
| 991 |
for (SelectModule::Dict::const_iterator it = types.begin(); it != types.end(); ++it) { |
| 992 |
auto button = new QRadioButton(groupBox); |
| 993 |
|
| 994 |
QRegularExpression rx; |
| 995 |
QString filter = it.key(); |
| 996 |
QString module = it.value(); |
| 997 |
|
| 998 |
// ignore file types in (...) |
| 999 |
rx.setPattern(QLatin1String(R"(\s+\([\w\*\s\.]+\)$)")); |
| 1000 |
auto match = rx.match(filter); |
| 1001 |
if (match.hasMatch()) { |
| 1002 |
filter = filter.left(match.capturedStart()); |
| 1003 |
} |
| 1004 |
|
| 1005 |
// ignore Gui suffix in module name |
| 1006 |
rx.setPattern(QLatin1String("Gui$")); |
| 1007 |
match = rx.match(module); |
| 1008 |
if (match.hasMatch()) { |
| 1009 |
module = module.left(match.capturedStart()); |
| 1010 |
} |
| 1011 |
|
| 1012 |
button->setText(QStringLiteral("%1 (%2)").arg(filter, module)); |
| 1013 |
button->setObjectName(it.value()); |
| 1014 |
gridLayout1->addWidget(button, index, 0, 1, 1); |
| 1015 |
group->addButton(button, index); |
| 1016 |
index++; |
| 1017 |
} |
| 1018 |
|
| 1019 |
gridLayout->addWidget(groupBox, 0, 0, 1, 1); |
| 1020 |
spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 1021 |
gridLayout->addItem(spacerItem, 1, 0, 1, 1); |
| 1022 |
|
| 1023 |
hboxLayout = new QHBoxLayout(); |
| 1024 |
hboxLayout->setSpacing(6); |
| 1025 |
hboxLayout->setContentsMargins(0, 0, 0, 0); |
| 1026 |
spacerItem1 = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum); |
| 1027 |
hboxLayout->addItem(spacerItem1); |
| 1028 |
|
| 1029 |
buttonBox = new QDialogButtonBox(this); |
| 1030 |
buttonBox->setObjectName(QStringLiteral("buttonBox")); |
| 1031 |
buttonBox->setStandardButtons(QDialogButtonBox::Open | QDialogButtonBox::Cancel); |
| 1032 |
buttonBox->button(QDialogButtonBox::Open)->setEnabled(false); |
| 1033 |
|
| 1034 |
hboxLayout->addWidget(buttonBox); |
| 1035 |
gridLayout->addLayout(hboxLayout, 2, 0, 1, 1); |
| 1036 |
|
| 1037 |
// connections |
| 1038 |
connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectModule::accept); |
| 1039 |
connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectModule::reject); |
| 1040 |
connect(group, &QButtonGroup::idClicked, this, &SelectModule::onButtonClicked); |
| 1041 |
} |
| 1042 |
|
| 1043 |
SelectModule::~SelectModule() = default; |
| 1044 |
|
| 1045 |
void SelectModule::accept() |
| 1046 |
{ |
| 1047 |
if (group->checkedButton()) { |
| 1048 |
QDialog::accept(); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
void SelectModule::reject() |
| 1053 |
{ |
| 1054 |
QDialog::reject(); |
| 1055 |
} |
| 1056 |
|
| 1057 |
void SelectModule::onButtonClicked() |
| 1058 |
{ |
| 1059 |
QWidget* button = buttonBox->button(QDialogButtonBox::Open); |
| 1060 |
button->setEnabled(group->checkedButton() != nullptr); |
| 1061 |
} |
| 1062 |
|
| 1063 |
QString SelectModule::getModule() const |
| 1064 |
{ |
| 1065 |
QAbstractButton* button = group->checkedButton(); |
| 1066 |
return (button ? button->objectName() : QString()); |
| 1067 |
} |
| 1068 |
|
| 1069 |
SelectModule::Dict SelectModule::exportHandler(const QString& fileName, const QString& filter) |
| 1070 |
{ |
| 1071 |
return exportHandler(QStringList() << fileName, filter); |
| 1072 |
} |
| 1073 |
|
| 1074 |
SelectModule::Dict SelectModule::exportHandler(const QStringList& fileNames, const QString& filter) |
| 1075 |
{ |
| 1076 |
// first check if there is a certain filter selected |
| 1077 |
SelectModule::Dict dict; |
| 1078 |
if (!filter.isEmpty()) { |
| 1079 |
// If an export filter is specified search directly for the module |
| 1080 |
std::map<std::string, std::string> filterList = App::GetApplication().getExportFilters(); |
| 1081 |
std::map<std::string, std::string>::const_iterator it; |
| 1082 |
it = filterList.find((const char*)filter.toUtf8()); |
| 1083 |
if (it != filterList.end()) { |
| 1084 |
QString module = QString::fromLatin1(it->second.c_str()); |
| 1085 |
for (const auto& fileName : fileNames) { |
| 1086 |
dict[fileName] = module; |
| 1087 |
} |
| 1088 |
return dict; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
// the global filter (or no filter) was selected. We now try to sort filetypes that are |
| 1093 |
// handled by more than one module and ask to the user to select one. |
| 1094 |
QMap<QString, SelectModule::Dict> filetypeHandler; |
| 1095 |
QMap<QString, QStringList> fileExtension; |
| 1096 |
for (const auto& fileName : fileNames) { |
| 1097 |
QFileInfo fi(fileName); |
| 1098 |
QString ext = fi.completeSuffix().toLower(); |
| 1099 |
std::map<std::string, std::string> filters = App::GetApplication().getExportFilters( |
| 1100 |
ext.toStdString() |
| 1101 |
); |
| 1102 |
|
| 1103 |
if (filters.empty()) { |
| 1104 |
ext = fi.suffix().toLower(); |
| 1105 |
filters = App::GetApplication().getExportFilters(ext.toStdString()); |
| 1106 |
} |
| 1107 |
|
| 1108 |
fileExtension[ext].push_back(fileName); |
| 1109 |
for (const auto& filter : filters) { |
| 1110 |
filetypeHandler[ext][QString::fromUtf8(filter.first.c_str())] = QString::fromLatin1( |
| 1111 |
filter.second.c_str() |
| 1112 |
); |
| 1113 |
} |
| 1114 |
// set the default module handler |
| 1115 |
if (!filters.empty()) { |
| 1116 |
dict[fileName] = QString::fromLatin1(filters.begin()->second.c_str()); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
for (QMap<QString, SelectModule::Dict>::const_iterator it = filetypeHandler.cbegin(); |
| 1121 |
it != filetypeHandler.cend(); |
| 1122 |
++it) { |
| 1123 |
if (it.value().size() > 1) { |
| 1124 |
SelectModule dlg(it.key(), it.value(), getMainWindow()); |
| 1125 |
QApplication::beep(); |
| 1126 |
if (dlg.exec()) { |
| 1127 |
QString mod = dlg.getModule(); |
| 1128 |
const QStringList& files = fileExtension[it.key()]; |
| 1129 |
for (const auto& file : files) { |
| 1130 |
dict[file] = mod; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
return dict; |
| 1137 |
} |
| 1138 |
|
| 1139 |
SelectModule::Dict SelectModule::importHandler(const QString& fileName, const QString& filter) |
| 1140 |
{ |
| 1141 |
return importHandler(QStringList() << fileName, filter); |
| 1142 |
} |
| 1143 |
|
| 1144 |
SelectModule::Dict SelectModule::importHandler(const QStringList& fileNames, const QString& filter) |
| 1145 |
{ |
| 1146 |
// first check if there is a certain filter selected |
| 1147 |
SelectModule::Dict dict; |
| 1148 |
if (!filter.isEmpty()) { |
| 1149 |
// If an import filter is specified search directly for the module |
| 1150 |
std::map<std::string, std::string> filterList = App::GetApplication().getImportFilters(); |
| 1151 |
std::map<std::string, std::string>::const_iterator it; |
| 1152 |
it = filterList.find((const char*)filter.toUtf8()); |
| 1153 |
if (it != filterList.end()) { |
| 1154 |
QString module = QString::fromLatin1(it->second.c_str()); |
| 1155 |
for (const auto& fileName : fileNames) { |
| 1156 |
dict[fileName] = module; |
| 1157 |
} |
| 1158 |
return dict; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
// the global filter (or no filter) was selected. We now try to sort filetypes that are |
| 1163 |
// handled by more than one module and ask to the user to select one. |
| 1164 |
QMap<QString, SelectModule::Dict> filetypeHandler; |
| 1165 |
QMap<QString, QStringList> fileExtension; |
| 1166 |
for (const auto& fileName : fileNames) { |
| 1167 |
QFileInfo fi(fileName); |
| 1168 |
QString ext = fi.completeSuffix().toLower(); |
| 1169 |
std::map<std::string, std::string> filters = App::GetApplication().getImportFilters( |
| 1170 |
ext.toStdString() |
| 1171 |
); |
| 1172 |
|
| 1173 |
if (filters.empty()) { |
| 1174 |
ext = fi.suffix().toLower(); |
| 1175 |
filters = App::GetApplication().getImportFilters(ext.toStdString()); |
| 1176 |
} |
| 1177 |
|
| 1178 |
fileExtension[ext].push_back(fileName); |
| 1179 |
for (const auto& filter : filters) { |
| 1180 |
filetypeHandler[ext][QString::fromUtf8(filter.first.c_str())] = QString::fromLatin1( |
| 1181 |
filter.second.c_str() |
| 1182 |
); |
| 1183 |
} |
| 1184 |
// set the default module handler |
| 1185 |
if (!filters.empty()) { |
| 1186 |
dict[fileName] = QString::fromLatin1(filters.begin()->second.c_str()); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
for (QMap<QString, SelectModule::Dict>::const_iterator it = filetypeHandler.cbegin(); |
| 1191 |
it != filetypeHandler.cend(); |
| 1192 |
++it) { |
| 1193 |
if (it.value().size() > 1) { |
| 1194 |
SelectModule dlg(it.key(), it.value(), getMainWindow()); |
| 1195 |
QApplication::beep(); |
| 1196 |
if (dlg.exec()) { |
| 1197 |
QString mod = dlg.getModule(); |
| 1198 |
const QStringList& files = fileExtension[it.key()]; |
| 1199 |
for (const auto& file : files) { |
| 1200 |
dict[file] = mod; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
else { |
| 1204 |
// Cancelled |
| 1205 |
return {}; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
return dict; |
| 1211 |
} |
| 1212 |
|
| 1213 |
|
| 1214 |
#include "moc_FileDialog.cpp" |