ImportFile.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/Edit/Paste/Paste.js");
  20. /**
  21. * \class ImportFile
  22. * \brief Inserts a file into the drawing.
  23. * The target point is specified by the user.
  24. * \ingroup ecma_edit
  25. */
  26. function ImportFile(guiAction, fileName) {
  27. Paste.call(this, guiAction);
  28. this.sourceDocument = new RDocument(new RMemoryStorage(), createSpatialIndex());
  29. this.sourceDi = new RDocumentInterface(this.sourceDocument);
  30. this.sourceDi.setNotifyListeners(false);
  31. // unit tests pass file name directly to action:
  32. this.fileName = fileName;
  33. this.blockName = undefined;
  34. this.asBlock = false;
  35. this.setUiOptions(ImportFile.includeBasePath + "/ImportFile.ui");
  36. }
  37. ImportFile.prototype = new Paste();
  38. ImportFile.includeBasePath = includeBasePath;
  39. ImportFile.prototype.beginEvent = function() {
  40. Paste.prototype.beginEvent.call(this);
  41. var fileDialogInfo;
  42. if (!isNull(this.fileName)) {
  43. fileDialogInfo = [ this.fileName, "" ];
  44. }
  45. else {
  46. fileDialogInfo = this.getFileName();
  47. }
  48. // workaround for Qt keyboard focus bug:
  49. var appWin = RMainWindowQt.getMainWindow();
  50. if (!isNull(appWin)) {
  51. appWin.activateWindow();
  52. appWin.raise();
  53. appWin.setFocus(Qt.OtherFocusReason);
  54. }
  55. if (isNull(fileDialogInfo)) {
  56. destr(this.sourceDi);
  57. this.sourceDi = undefined;
  58. this.terminate();
  59. return;
  60. }
  61. if (this.sourceDi.importFile(fileDialogInfo[0], fileDialogInfo[1], false)!==RDocumentInterface.IoErrorNoError) {
  62. EAction.handleUserWarning(qsTr("Error while importing file %1").arg(fileDialogInfo[0]));
  63. if (!isNull(this.sourceDi)) {
  64. destr(this.sourceDi);
  65. this.sourceDi = undefined;
  66. }
  67. this.terminate();
  68. return;
  69. }
  70. };
  71. ImportFile.prototype.finishEvent = function() {
  72. if (!isNull(this.sourceDi)) {
  73. destr(this.sourceDi);
  74. }
  75. Paste.prototype.finishEvent.call(this);
  76. };
  77. ImportFile.prototype.initOperation = function(op) {
  78. if (this.asBlock && !isNull(this.blockName) && this.blockName.length>0) {
  79. op.setBlockName(this.blockName);
  80. }
  81. // op.setCopyAllLayers(true);
  82. // op.setCopyEmptyBlocks(true);
  83. };
  84. ImportFile.prototype.getFileName = function() {
  85. var lastOpenFileDir = RSettings.getStringValue("ImportFile/Path", RSettings.getDocumentsLocation());
  86. var filterStrings = RFileImporterRegistry.getFilterStrings();
  87. filterStrings = translateFilterStrings(filterStrings);
  88. if (filterStrings.length===0) {
  89. EAction.handleUserWarning(qsTr("No import filters have been found. Aborting..."));
  90. return undefined;
  91. }
  92. filterStrings = new Array(qsTr("All Files") + " (*)").concat(filterStrings);
  93. var appWin = EAction.getMainWindow();
  94. var fileDialog = new QFileDialog(appWin, qsTr("Import Drawing"), lastOpenFileDir, "");
  95. var allFilter = filterStrings[0];
  96. fileDialog.setNameFilters(filterStrings);
  97. fileDialog.selectNameFilter(allFilter);
  98. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  99. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  100. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  101. }
  102. fileDialog.fileMode = QFileDialog.ExistingFiles;
  103. fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:"));
  104. if (!fileDialog.exec()) {
  105. destr(fileDialog);
  106. EAction.activateMainWindow();
  107. return undefined;
  108. }
  109. var fileNames = fileDialog.selectedFiles();
  110. if (fileNames.length===0) {
  111. destr(fileDialog);
  112. EAction.activateMainWindow();
  113. return undefined;
  114. }
  115. var fileName = fileNames[0];
  116. var suffix = new QFileInfo(fileName).suffix();
  117. suffix = suffix.toUpperCase();
  118. var imageFormats = QImageReader.supportedImageFormats();
  119. for (var i=0; i<imageFormats.length; i++) {
  120. var format = imageFormats[i];
  121. if (suffix===format.toString().toUpperCase()) {
  122. // delegate import to image action:
  123. include("scripts/Draw/Image/Image.js");
  124. var imageAction = new Image();
  125. imageAction.fileName = fileName;
  126. this.getDocumentInterface().queueAction(imageAction);
  127. this.terminate();
  128. return;
  129. }
  130. }
  131. RSettings.setValue("ImportFile/Path", fileDialog.directory().absolutePath());
  132. var nameFilter = fileDialog.selectedNameFilter();
  133. destr(fileDialog);
  134. EAction.activateMainWindow();
  135. //this.blockName = new QFileInfo(fileNames[0]).baseName();
  136. //RSettings.setValue("ImportFile/BlockName", new QFileInfo(fileNames[0]).baseName());
  137. var optionsToolBar = EAction.getOptionsToolBar();
  138. if (!isNull(optionsToolBar)) {
  139. var leBlockName = optionsToolBar.findChild("BlockName");
  140. if (!isNull(leBlockName)) {
  141. var blockName = new QFileInfo(fileNames[0]).completeBaseName();
  142. blockName = RDxfServices.getSafeBlockName(blockName);
  143. leBlockName.text = blockName;
  144. }
  145. }
  146. return [ fileNames[0], nameFilter ];
  147. };
  148. ImportFile.prototype.slotAsBlockChanged = function(value) {
  149. this.asBlock = value;
  150. this.updatePreview(true);
  151. };
  152. ImportFile.prototype.slotBlockNameChanged = function(value) {
  153. this.blockName = value;
  154. this.updatePreview(true);
  155. };