ExXmlExport.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/EAction.js");
  20. /**
  21. * \ingroup ecma_misc_examples_ioexamples
  22. * \class ExXmlExport
  23. * This action writes an XML file.
  24. * This can be used as starting point for XML exporters.
  25. */
  26. function ExXmlExport(guiAction) {
  27. EAction.call(this, guiAction);
  28. }
  29. ExXmlExport.prototype = new EAction();
  30. ExXmlExport.prototype.beginEvent = function() {
  31. EAction.prototype.beginEvent.call(this);
  32. var appWin = EAction.getMainWindow();
  33. var initialPath = QDir.homePath();
  34. var filterAll = qsTr("All Files") + " (*)";
  35. var fileDialog = new QFileDialog(appWin, qsTr("Export XML"), initialPath, "");
  36. fileDialog.setNameFilters([ filterAll ]);
  37. fileDialog.selectNameFilter(filterAll);
  38. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  39. fileDialog.setOption(QFileDialog.DontConfirmOverwrite, true);
  40. fileDialog.fileMode = QFileDialog.AnyFile;
  41. fileDialog.acceptMode = QFileDialog.AcceptSave;
  42. if (!fileDialog.exec()) {
  43. destr(fileDialog);
  44. EAction.activateMainWindow();
  45. this.terminate();
  46. return;
  47. }
  48. var files = fileDialog.selectedFiles();
  49. if (files.length===0) {
  50. destr(fileDialog);
  51. EAction.activateMainWindow();
  52. this.terminate();
  53. return;
  54. }
  55. var fileName = files[0];
  56. var file = new QFile(fileName);
  57. var flags = makeQIODeviceOpenMode(QIODevice.WriteOnly, QIODevice.Text);
  58. if (!file.open(flags)) {
  59. EAction.handleUserWarning("cannot open file: " + fileName);
  60. this.terminate();
  61. return;
  62. }
  63. EAction.handleUserMessage("exporting: " + fileName);
  64. var stream = new QXmlStreamWriter(file);
  65. stream.setAutoFormatting(true);
  66. stream.writeStartDocument();
  67. stream.writeStartElement("QCAD");
  68. stream.writeComment("QCAD XML export example");
  69. stream.writeStartElement("tag");
  70. stream.writeEndElement();
  71. stream.writeEndElement();
  72. stream.writeEndDocument();
  73. destr(stream);
  74. file.close();
  75. EAction.handleUserMessage("export successful");
  76. this.terminate();
  77. };