ExRegisterFileExporter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 exporter factory.
  22. *
  23. * Responsible for filter strings, checking if this exporter is suitable
  24. * to export files of a given type and for instantiating the file exporter
  25. * on request.
  26. */
  27. function ExFileExporterFactory() {
  28. RFileExporterFactoryAdapter.call(this);
  29. }
  30. ExFileExporterFactory.prototype = new RFileExporterFactoryAdapter();
  31. ExFileExporterFactory.prototype.getFilterStrings = function() {
  32. return [ qsTr("Example Files %1").arg("(*.example)") ];
  33. };
  34. ExFileExporterFactory.prototype.canExport = function(fileName, nameFilter) {
  35. var fi = new QFileInfo(fileName);
  36. if (fi.suffix().toLowerCase()==="example") {
  37. return 1;
  38. }
  39. return -1;
  40. };
  41. ExFileExporterFactory.prototype.instantiate = function(document, messageHandler, progressHandler) {
  42. return new ExFileExporter(document, messageHandler, progressHandler);
  43. };
  44. /**
  45. * \ingroup ecma_misc_examples_ioexamples
  46. * \class ExFileExporter
  47. * File exporter implementation.
  48. * This class is instantiated if a file of the registered type is exported.
  49. */
  50. function ExFileExporter(document, messageHandler, progressHandler) {
  51. RFileExporterAdapter.call(this, document, messageHandler, progressHandler);
  52. };
  53. ExFileExporter.prototype = new RFileExporterAdapter();
  54. ExFileExporter.prototype.getCorrectedFileName = function(fileName, nameFilter) {
  55. var ret = fileName;
  56. var ext = new QFileInfo(ret).suffix().toLowerCase();
  57. if (ext!=="example") {
  58. ret += ".example";
  59. }
  60. return ret;
  61. };
  62. ExFileExporter.prototype.exportFile = function(fileName, nameFilter, setFileName) {
  63. var doc = this.getDocument();
  64. // the document 'doc' should be exported to file 'fileName' here...
  65. var file = new QFile(fileName);
  66. var flags = makeQIODeviceOpenMode(QIODevice.WriteOnly, QIODevice.Text);
  67. if (!file.open(flags)) {
  68. return false;
  69. }
  70. if (setFileName) {
  71. doc.setFileName(fileName);
  72. }
  73. var ts = new QTextStream(file);
  74. setUtf8Codec(ts);
  75. ts.writeString("Example");
  76. file.close();
  77. return true;
  78. };
  79. /**
  80. * This action registers a file exporter for files of type '.example'.
  81. */
  82. function ExRegisterFileExporter(guiAction) {
  83. IOExamples.call(this, guiAction);
  84. }
  85. ExRegisterFileExporter.prototype = new IOExamples();
  86. ExRegisterFileExporter.prototype.beginEvent = function() {
  87. // this can also be done in an init function to register the file exporter
  88. // during the application initialization:
  89. RFileExporterRegistry.registerFileExporter(new ExFileExporterFactory());
  90. var appWin = EAction.getMainWindow();
  91. QMessageBox.information(
  92. appWin,
  93. qsTr("Info"),
  94. qsTr("Registered a file exporter for file type \".example\". Use File > Save As to see the filter in action.")
  95. );
  96. };
  97. /**
  98. * Adds a menu for this action.
  99. */
  100. ExRegisterFileExporter.init = function(basePath) {
  101. var action = new RGuiAction(qsTr("&Register File Exporter"), RMainWindowQt.getMainWindow());
  102. action.setRequiresDocument(false);
  103. action.setScriptFile(basePath + "/ExRegisterFileExporter.js");
  104. action.setGroupSortOrder(71110);
  105. action.setSortOrder(200);
  106. action.setWidgetNames(["IOExamplesMenu"]);
  107. };