OpenFile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD 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 General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/File/NewFile/NewFile.js");
  20. if (exists("scripts/File/AutoSave/AutoSave.js")) {
  21. include("scripts/File/AutoSave/AutoSave.js");
  22. }
  23. /**
  24. * \class OpenFile
  25. * \brief Handles all user interaction to open documents.
  26. * \ingroup ecma_file
  27. */
  28. function OpenFile(guiAction) {
  29. NewFile.call(this, guiAction);
  30. if (!isNull(guiAction)) {
  31. this.args = guiAction.getArguments();
  32. }
  33. }
  34. OpenFile.prototype = new NewFile();
  35. OpenFile.prototype.beginEvent = function () {
  36. File.prototype.beginEvent.call(this);
  37. try {
  38. var filterStrings = RFileImporterRegistry.getFilterStrings();
  39. filterStrings = translateFilterStrings(filterStrings);
  40. if (filterStrings.length === 0) {
  41. EAction.handleUserWarning(qsTr("No import filters have been found. Aborting..."));
  42. return;
  43. }
  44. var fileName;
  45. if (!isNull(this.args) && this.args.length >= 1) {
  46. fileName = this.args[this.args.length - 1];
  47. if (!new QFileInfo(fileName).isAbsolute()) {
  48. fileName = RSettings.getLaunchPath() + QDir.separator + fileName;
  49. }
  50. if (!isNull(fileName)) {
  51. NewFile.createMdiChild(fileName);
  52. return;
  53. }
  54. }
  55. if (!isNull(this.guiAction)) {
  56. fileName = this.guiAction.data();
  57. if (!isNull(fileName)) {
  58. NewFile.createMdiChild(fileName);
  59. return;
  60. }
  61. }
  62. var lastOpenFileDir = RSettings.getStringValue("OpenFile/Path", RSettings.getDocumentsLocation());
  63. var appWin = EAction.getMainWindow();
  64. var fileDialog = new QFileDialog(appWin, qsTr("Open Drawing"), lastOpenFileDir, "");
  65. var allFilter = filterStrings[0];
  66. filterStrings = new Array(qsTr("All Files") + " (*)").concat(filterStrings);
  67. fileDialog.setNameFilters(filterStrings);
  68. fileDialog.selectNameFilter(allFilter);
  69. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  70. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  71. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  72. }
  73. fileDialog.fileMode = QFileDialog.ExistingFiles;
  74. // global function qcadInitFileDialog may be registered to perform
  75. // additional initialization
  76. if (typeof qcadInitFileDialog != "undefined" && isFunction(qcadInitFileDialog)) {
  77. qcadInitFileDialog(fileDialog);
  78. }
  79. if (!fileDialog.exec()) {
  80. destr(fileDialog);
  81. EAction.activateMainWindow();
  82. return;
  83. }
  84. EAction.activateMainWindow();
  85. RSettings.setValue("OpenFile/Path", fileDialog.directory().absolutePath());
  86. var fileNames = fileDialog.selectedFiles();
  87. var nameFilter = fileDialog.selectedNameFilter();
  88. for (var i = 0; i < fileNames.length; ++i) {
  89. NewFile.createMdiChild(fileNames[i], nameFilter);
  90. }
  91. destr(fileDialog);
  92. EAction.activateMainWindow();
  93. } catch (error) {
  94. EAction.handleUserWarning("OpenFile.js error:" + error.toString());
  95. }
  96. };