SvgImport.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/File.js");
  20. include("scripts/ImportExport/SvgImporter/SvgImporter.js");
  21. function SvgImport(guiAction) {
  22. File.call(this, guiAction);
  23. }
  24. SvgImport.prototype = new File();
  25. SvgImport.getPreferencesCategory = function() {
  26. return [ qsTr("File"), qsTr("SVG Import")];
  27. };
  28. SvgImport.includeBasePath = includeBasePath;
  29. SvgImport.prototype.beginEvent = function() {
  30. File.prototype.beginEvent.call(this);
  31. var fileName = undefined;
  32. if (!isNull(this.guiAction)) {
  33. var args = this.guiAction.getArguments();
  34. this.guiAction.clearArguments();
  35. if (args.length>0) {
  36. fileName = args[0];
  37. var fi = new QFileInfo(fileName);
  38. if (!fi.isAbsolute()) {
  39. fileName = RSettings.getLaunchPath() + QDir.separator + fileName;
  40. fi = new QFileInfo(fileName);
  41. }
  42. if (!fi.exists()) {
  43. qWarning("file does not exist: ", fileName);
  44. this.terminate();
  45. return;
  46. }
  47. }
  48. }
  49. var appWin = EAction.getMainWindow();
  50. if (isNull(fileName)) {
  51. var lastDir = RSettings.getStringValue( "SvgImport/Path", RSettings.getDocumentsLocation());
  52. fileName = QFileDialog.getOpenFileName(
  53. appWin, qsTr("Import SVG"), lastDir,
  54. qsTr("SVG Files") + " (*.svg);;" + qsTr("All Files") + " (*)");
  55. if (fileName.length===0) {
  56. this.terminate();
  57. return;
  58. }
  59. RSettings.setValue("SvgImport/Path", new QFileInfo(fileName).absolutePath());
  60. }
  61. var dialog = WidgetFactory.createDialog(SvgImport.includeBasePath, "SvgImportDialog.ui", appWin);
  62. var resolutionCombo = dialog.findChild("Resolution");
  63. if (!dialog.exec()) {
  64. destr(dialog);
  65. EAction.activateMainWindow();
  66. this.terminate();
  67. return;
  68. }
  69. var resolution = parseFloat(resolutionCombo.currentText);
  70. if (isNaN(resolution) || resolution<=0) {
  71. resolution = 90;
  72. }
  73. var svgImporter = new SvgImporter(this.getDocument(), resolution);
  74. svgImporter.importFile(fileName, "");
  75. var di = this.getDocumentInterface();
  76. if (!isNull(di)) {
  77. di.regenerateScenes();
  78. di.autoZoom();
  79. }
  80. if (!isNull(appWin)) {
  81. appWin.notifyListeners();
  82. }
  83. destr(dialog);
  84. EAction.activateMainWindow();
  85. this.terminate();
  86. };