ExRegisterFileImporter.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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("../IOExamples.js");
  20. /**
  21. * File importer factory.
  22. *
  23. * Responsible for filter strings, checking if this importer is suitable
  24. * to import files of a given type and for instantiating the file importer
  25. * on request.
  26. */
  27. function ExFileImporterFactory() {
  28. RFileImporterFactoryAdapter.call(this);
  29. }
  30. ExFileImporterFactory.prototype = new RFileImporterFactoryAdapter();
  31. ExFileImporterFactory.prototype.getFilterStrings = function() {
  32. return [ qsTr("Example Files %1").arg("(*.example)") ];
  33. };
  34. ExFileImporterFactory.prototype.canImport = function(fileName, nameFilter) {
  35. var fi = new QFileInfo(fileName);
  36. if (fi.suffix().toLowerCase()==="example") {
  37. return 1;
  38. }
  39. return -1;
  40. };
  41. ExFileImporterFactory.prototype.instantiate = function(document, messageHandler, progressHandler) {
  42. return new ExFileImporter(document, messageHandler, progressHandler);
  43. };
  44. /**
  45. * \ingroup ecma_misc_examples_ioexamples
  46. * \class ExFileImporter
  47. * File importer implementation.
  48. * This class is instantiated if a file of the registered type is imported.
  49. */
  50. function ExFileImporter(document, messageHandler, progressHandler) {
  51. RFileImporterAdapter.call(this, document, messageHandler, progressHandler);
  52. this.messageHandler = messageHandler;
  53. };
  54. ExFileImporter.prototype = new RFileImporterAdapter();
  55. ExFileImporter.prototype.importFile = function(fileName, nameFilter) {
  56. var doc = this.getDocument();
  57. // the given file 'fileName' should be imported into doc here...
  58. var fi = new QFileInfo(fileName);
  59. var file = new QFile(fi.absoluteFilePath());
  60. var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
  61. if (!file.open(flags)) {
  62. return false;
  63. }
  64. this.startImport();
  65. var ts = new QTextStream(file);
  66. setUtf8Codec(ts);
  67. var line;
  68. do {
  69. line = ts.readLine();
  70. if (!isNull(this.messageHandler)) {
  71. this.messageHandler.handleUserMessage(qsTr("Read from file: \"%1\"").arg(line));
  72. }
  73. // use importObject to import an object read from the file:
  74. //this.importObject(new RLineEntity(doc, new RLineData(new RVector(0,0), new RVector(100,100))));
  75. } while (!ts.atEnd());
  76. file.close();
  77. this.endImport();
  78. return true;
  79. };
  80. /**
  81. * This action registers a file importer for files of type '.example'.
  82. */
  83. function ExRegisterFileImporter(guiAction) {
  84. IOExamples.call(this, guiAction);
  85. }
  86. ExRegisterFileImporter.prototype = new IOExamples();
  87. ExRegisterFileImporter.prototype.beginEvent = function() {
  88. // this can also be done in an init function to register the file importer
  89. // during the application initialization:
  90. RFileImporterRegistry.registerFileImporter(new ExFileImporterFactory());
  91. var appWin = EAction.getMainWindow();
  92. QMessageBox.information(
  93. appWin,
  94. qsTr("Info"),
  95. qsTr("Registered a file importer for file type \".example\". Use File > Open to see the filter in action.")
  96. );
  97. };
  98. /**
  99. * Adds a menu for this action.
  100. */
  101. ExRegisterFileImporter.init = function(basePath) {
  102. var action = new RGuiAction(qsTr("&Register File Importer"), RMainWindowQt.getMainWindow());
  103. action.setRequiresDocument(false);
  104. action.setScriptFile(basePath + "/ExRegisterFileImporter.js");
  105. action.setGroupSortOrder(71110);
  106. action.setSortOrder(200);
  107. action.setWidgetNames(["IOExamplesMenu"]);
  108. };