OpenFile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. var filterStrings = RFileImporterRegistry.getFilterStrings();
  38. filterStrings = translateFilterStrings(filterStrings);
  39. if (filterStrings.length===0) {
  40. EAction.handleUserWarning(qsTr("No import filters have been found. Aborting..."));
  41. return;
  42. }
  43. var fileName;
  44. if (!isNull(this.args) && this.args.length>=1) {
  45. fileName = this.args[this.args.length-1];
  46. if (!new QFileInfo(fileName).isAbsolute()) {
  47. fileName = RSettings.getLaunchPath() + QDir.separator + fileName;
  48. }
  49. if (!isNull(fileName)) {
  50. NewFile.createMdiChild(fileName);
  51. return;
  52. }
  53. }
  54. if (!isNull(this.guiAction)) {
  55. fileName = this.guiAction.data();
  56. if (!isNull(fileName)) {
  57. NewFile.createMdiChild(fileName);
  58. return;
  59. }
  60. }
  61. var lastOpenFileDir = RSettings.getStringValue(
  62. "OpenFile/Path",
  63. RSettings.getDocumentsLocation());
  64. var appWin = EAction.getMainWindow();
  65. var fileDialog = new QFileDialog(appWin, qsTr("Open Drawing"), lastOpenFileDir, "");
  66. var allFilter = filterStrings[0];
  67. filterStrings = new Array(qsTr("All Files") + " (*)").concat(filterStrings);
  68. fileDialog.setNameFilters(filterStrings);
  69. fileDialog.selectNameFilter(allFilter);
  70. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  71. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  72. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  73. }
  74. fileDialog.fileMode = QFileDialog.ExistingFiles;
  75. // global function qcadInitFileDialog may be registered to perform
  76. // additional initialization
  77. if (typeof qcadInitFileDialog!="undefined" && isFunction(qcadInitFileDialog)) {
  78. qcadInitFileDialog(fileDialog);
  79. }
  80. if (!fileDialog.exec()) {
  81. destr(fileDialog);
  82. EAction.activateMainWindow();
  83. return;
  84. }
  85. EAction.activateMainWindow();
  86. RSettings.setValue("OpenFile/Path", fileDialog.directory().absolutePath());
  87. var fileNames = fileDialog.selectedFiles();
  88. var nameFilter = fileDialog.selectedNameFilter();
  89. for ( var i = 0; i < fileNames.length; ++i) {
  90. NewFile.createMdiChild(fileNames[i], nameFilter);
  91. }
  92. destr(fileDialog);
  93. EAction.activateMainWindow();
  94. };