PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/xml/save_xml.cpp

https://code.google.com/p/activityedit/
C++ | 191 lines | 164 code | 20 blank | 7 comment | 4 complexity | dea2ce08c6209dea0fbb588789b1bf2c MD5 | raw file
  1. #include "save_xml.h"
  2. #include <QHash>
  3. #include "../shape/action.h"
  4. #include "../flow/flow.h"
  5. #include "../flow/noteflow.h"
  6. #include "../flow/arrowflow.h"
  7. #include "../flow/flowtext.h"
  8. #include "../shape/decision.h"
  9. #include "../shape/note.h"
  10. #include "../shape/initialnode.h"
  11. #include "../shape/finalnode.h"
  12. #include "../flow/tempshape.h"
  13. #include <typeinfo>
  14. #include <QXmlStreamWriter>
  15. #include <QFile>
  16. #include <QDebug>
  17. typedef int ID;
  18. typedef void (*SAVE_SHAPE_FUNC) (QXmlStreamWriter &x, Shape *shape, QHash<Shape *, ID> &ids);
  19. void save_TempShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  20. {
  21. TempShape *s = dynamic_cast<TempShape *>(sh);
  22. x.writeStartElement("TempShape");
  23. x.writeAttribute("ID", QString::number(ids[s]));
  24. x.writeAttribute("x", QString::number(s->pos().x()));
  25. x.writeAttribute("y", QString::number(s->pos().y()));
  26. x.writeEndElement();
  27. }
  28. void saveFlowKnots (Flow *flow, QXmlStreamWriter &x)
  29. {
  30. QPolygonF poly = flow->createFlowPolygon();
  31. x.writeAttribute("KnotsCount", QString::number(poly.count()-2));
  32. for(int i=1; i<poly.count()-1; ++i)
  33. {
  34. x.writeAttribute("KnotX_" + QString::number(i-1), QString::number(poly[i].x()));
  35. x.writeAttribute("KnotY_" + QString::number(i-1), QString::number(poly[i].y()));
  36. }
  37. }
  38. void save_NoteFlow (QXmlStreamWriter &x, Shape *s, QHash<Shape*, ID> &ids)
  39. {
  40. NoteFlow *flow = dynamic_cast<NoteFlow *>(s);
  41. x.writeStartElement("NoteFlow");
  42. x.writeAttribute("ID", QString::number(ids[flow]));
  43. x.writeAttribute("fromID", QString::number(ids[flow->startShape()]));
  44. x.writeAttribute("toID", QString::number(ids[flow->endShape()]));
  45. saveFlowKnots(flow, x);
  46. x.writeEndElement();
  47. }
  48. void save_ActionShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  49. {
  50. ActionShape *s = dynamic_cast<ActionShape *>(sh);
  51. x.writeStartElement("ActionShape");
  52. x.writeAttribute("ID", QString::number(ids[s]));
  53. x.writeAttribute("x", QString::number(s->pos().x()));
  54. x.writeAttribute("y", QString::number(s->pos().y()));
  55. x.writeAttribute("width", QString::number(s->size().width()));
  56. x.writeAttribute("height", QString::number(s->size().height()));
  57. x.writeAttribute("title", s->title());
  58. x.writeAttribute("description", s->description());
  59. x.writeEndElement();
  60. }
  61. void save_NoteShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  62. {
  63. NoteShape *s = dynamic_cast<NoteShape *>(sh);
  64. x.writeStartElement("NoteShape");
  65. x.writeAttribute("ID", QString::number(ids[s]));
  66. x.writeAttribute("x", QString::number(s->pos().x()));
  67. x.writeAttribute("y", QString::number(s->pos().y()));
  68. x.writeAttribute("width", QString::number(s->size().width()));
  69. x.writeAttribute("height", QString::number(s->size().height()));
  70. x.writeAttribute("title", s->title());
  71. x.writeAttribute("description", s->description());
  72. x.writeEndElement();
  73. }
  74. void save_DecisionShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  75. {
  76. DecisionShape *s = dynamic_cast<DecisionShape *>(sh);
  77. x.writeStartElement("DecisionShape");
  78. x.writeAttribute("ID", QString::number(ids[s]));
  79. x.writeAttribute("x", QString::number(s->pos().x()));
  80. x.writeAttribute("y", QString::number(s->pos().y()));
  81. x.writeAttribute("width", QString::number(s->size().width()));
  82. x.writeAttribute("height", QString::number(s->size().height()));
  83. x.writeAttribute("corners", s->connectToCorners()?"true":"false");
  84. x.writeEndElement();
  85. }
  86. void save_ArrowFlow (QXmlStreamWriter &x, Shape *s, QHash<Shape*, ID> &ids)
  87. {
  88. ArrowFlow *flow = dynamic_cast<ArrowFlow *>(s);
  89. x.writeStartElement("ArrowFlow");
  90. x.writeAttribute("ID", QString::number(ids[flow]));
  91. x.writeAttribute("fromID", QString::number(ids[flow->startShape()]));
  92. x.writeAttribute("toID", QString::number(ids[flow->endShape()]));
  93. x.writeAttribute("offsetX", QString::number(flow->textShape()->offsetX()));
  94. x.writeAttribute("offsetY", QString::number(flow->textShape()->offsetY()));
  95. x.writeAttribute("text", flow->text());
  96. saveFlowKnots(flow, x);
  97. x.writeEndElement();
  98. }
  99. void save_InitialNodeShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  100. {
  101. InitialNodeShape *s = dynamic_cast<InitialNodeShape *>(sh);
  102. x.writeStartElement("InitialNodeShape");
  103. x.writeAttribute("ID", QString::number(ids[s]));
  104. x.writeAttribute("x", QString::number(s->pos().x()));
  105. x.writeAttribute("y", QString::number(s->pos().y()));
  106. x.writeEndElement();
  107. }
  108. void save_FinalNodeShape (QXmlStreamWriter &x, Shape *sh, QHash<Shape*, ID> &ids)
  109. {
  110. FinalNodeShape *s = dynamic_cast<FinalNodeShape *>(sh);
  111. x.writeStartElement("FinalNodeShape");
  112. x.writeAttribute("ID", QString::number(ids[s]));
  113. x.writeAttribute("x", QString::number(s->pos().x()));
  114. x.writeAttribute("y", QString::number(s->pos().y()));
  115. x.writeEndElement();
  116. }
  117. void file_save_xml (const QString &filename, QGraphicsView *view, bool isdraft)
  118. {
  119. QFile file(filename);
  120. if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  121. throw FileErrorException(filename);
  122. QTextStream stream(&file);
  123. stream << create_scene_xml(view->scene(), isdraft, view);
  124. file.close();
  125. }
  126. QString create_scene_xml (QGraphicsScene *view, bool isdraft, QGraphicsView *v)
  127. {
  128. QHash<Shape*, ID> shapeids;
  129. QHash<QString, SAVE_SHAPE_FUNC> savefunc;
  130. QString str;
  131. QXmlStreamWriter writer(&str);
  132. writer.setAutoFormatting(true);
  133. writer.writeStartDocument();
  134. writer.writeStartElement("activityedit_xml");
  135. writer.writeAttribute("draft", (isdraft? "1" : "0"));
  136. if (v)
  137. {
  138. QSize sz = v->viewport()->size();
  139. QPointF pt = v->mapToScene(sz.width()/2, sz.height()/2);
  140. writer.writeAttribute("viewx", QString::number(pt.x()));
  141. writer.writeAttribute("viewy", QString::number(pt.y()));
  142. }
  143. /* ??????? ??????? ID */
  144. CustomGraphicsScene *scene = dynamic_cast<CustomGraphicsScene *>(view);
  145. QList<Shape *> items = scene->shapes();
  146. int curid = 0;
  147. foreach(Shape *item, items)
  148. shapeids[item] = curid++;
  149. /*
  150. * ???????? ????????????????? ?? ????????
  151. * ????? ????? ?? ?????????
  152. * ...
  153. * ? Shape ? ??? ?????? ?? ????, ?? ????? ????? ???
  154. */
  155. savefunc[typeid(ActionShape).name()] = save_ActionShape;
  156. savefunc[typeid(NoteShape).name()] = save_NoteShape;
  157. savefunc[typeid(DecisionShape).name()] = save_DecisionShape;
  158. savefunc[typeid(InitialNodeShape).name()] = save_InitialNodeShape;
  159. savefunc[typeid(FinalNodeShape).name()] = save_FinalNodeShape;
  160. savefunc[typeid(ArrowFlow).name()] = save_ArrowFlow;
  161. savefunc[typeid(NoteFlow).name()] = save_NoteFlow;
  162. savefunc[typeid(TempShape).name()] = save_TempShape;
  163. foreach(Shape *item, items)
  164. if (savefunc[typeid(*item).name()])
  165. savefunc[typeid(*item).name()](writer, item, shapeids);
  166. else
  167. qDebug() << "unhandled shape: " << typeid(*item).name();
  168. writer.writeEndElement();
  169. writer.writeEndDocument();
  170. return str;
  171. }