Swiftray  1.0
document-serializer.h
Go to the documentation of this file.
1 #include <QDebug>
2 #include <document.h>
3 
19 public:
20  DocumentSerializer(QDataStream &stream) :
21  out(stream),
22  in(stream) {}
23 
25  out << QString("NINJAV1.1");
26  out << QSize(doc.width(), doc.height());
27  out << doc.layers().size();
28  for (auto &layer : doc.layers()) {
29  serializeLayer(layer);
30  }
31  }
32 
34  QString doc_version;
35  in >> doc_version;
36  qInfo() << "Doc Version" << doc_version;
37  Document *doc = new Document;
38  QSize doc_size;
39  in >> doc_size;
40  doc->setWidth(doc_size.width());
41  doc->setHeight(doc_size.height());
42 
43  int layers_size;
44  in >> layers_size;
45  doc->layers_.clear();
46  for (int i = 0; i < layers_size; i++) {
47  LayerPtr layer = deserializeLayer();
48  doc->addLayer(layer);
49  }
50  return doc;
51  }
52 
53  void serializeLayer(const LayerPtr &layer) {
54  out << layer->name_;
55  out << layer->type_;
56  out << layer->color_;
57 
58  out << layer->use_diode_;
59  out << layer->is_visible_;
60  out << layer->target_height_;
61  out << layer->step_height_;
62  out << layer->repeat_;
63  out << layer->speed_;
64 
65  out << layer->children().size();
66  for (auto &shape: layer->children()) {
67  // TODO (Use string instead of enum int for future compatibility)
68  // TODO (Add operator for shape and datastream)
69  serializeShape(shape);
70  }
71  }
72 
74  LayerPtr layer = make_shared<Layer>();
75  in >> layer->name_;
76  in >> layer->type_;
77  in >> layer->color_;
78 
79  in >> layer->use_diode_;
80  in >> layer->is_visible_;
81  in >> layer->target_height_;
82  in >> layer->step_height_;
83  in >> layer->repeat_;
84  in >> layer->speed_;
85 
86  int shape_size;
87  in >> shape_size;
88  qInfo() << "Parsing layer" << layer->name_ << "children" << shape_size;
89  for (int i = 0; i < shape_size; i++) {
90  auto shape = deserializeShape();
91  shape->flushCache();
92  layer->addShape(shape);
93  }
94  return layer;
95  }
96 
97  void serializeShape(const ShapePtr &shape) {
98  out << shape->type();
99  //blabla
100  switch (shape->type()) {
101  case Shape::Type::Path:
102  serializePathShape((PathShape *) shape.get());
103  break;
104  case Shape::Type::Text:
105  serializeTextShape((TextShape *) shape.get());
106  break;
107  case Shape::Type::Bitmap:
108  serializeBitmapShape((BitmapShape *) shape.get());
109  break;
110  case Shape::Type::Group:
111  serializeGroupShape((GroupShape *) shape.get());
112  break;
113  default:
114  break;
115  }
116  }
117 
119  Shape::Type type;
120  in >> type;
121  qInfo() << "Parsing shape" << (int) type;
122  switch (type) {
123  case Shape::Type::Path:
124  return ShapePtr(deserializePathShape());
125  case Shape::Type::Text:
126  return ShapePtr(deserializeTextShape());
127  case Shape::Type::Bitmap:
129  case Shape::Type::Group:
131  default:
132  break;
133  }
134  Q_ASSERT_X(false, "Deserialize", "Failed to parse shape type");
135  }
136 
137  void serializeShapeProp(Shape *shape) {
138  out << shape->rotation_;
139  out << shape->transform_;
140  out << shape->selected_;
141  }
142 
144  in >> shape->rotation_;
145  in >> shape->transform_;
146  in >> shape->selected_;
147  }
148 
150  serializeShapeProp(shape);
151  out << *shape->bitmap_.get();
152  }
153 
155  BitmapShape *shape = new BitmapShape();
156  deserializeShapeProp(shape);
157  QPixmap pixmap;
158  in >> pixmap;
159  shape->bitmap_ = make_unique<QPixmap>(pixmap);
160  return shape;
161  }
162 
163 
165  serializeShapeProp(shape);
166  out << shape->filled_;
167  out << shape->path_;
168  }
169 
171  auto *shape = new PathShape();
172  deserializeShapeProp(shape);
173  in >> shape->filled_;
174  in >> shape->path_;
175  return shape;
176  }
177 
178 
180  serializeShapeProp(shape);
181  out << shape->children_.size();
182  for (auto &shape: shape->children_) {
183  serializeShape(shape);
184  }
185  }
186 
188  auto *group = new GroupShape();
189  deserializeShapeProp(group);
190  int children_size;
191  in >> children_size;
192  for (int i = 0; i < children_size; i++) {
193  auto shape = deserializeShape();
194  shape->setParent(group);
195  shape->flushCache();
196  group->children_ << shape;
197  }
198  return group;
199  }
200 
202  serializeShapeProp(shape);
203  out << shape->filled_;
204  out << shape->path_;
205  out << shape->line_height_;
206  out << shape->lines_;
207  out << shape->font_;
208  }
209 
211  auto *shape = new TextShape();
212  deserializeShapeProp(shape);
213  in >> shape->filled_;
214  in >> shape->path_;
215  in >> shape->line_height_;
216  in >> shape->lines_;
217  in >> shape->font_;
218  return shape;
219  }
220 
221  QDataStream &out;
222  QDataStream &in;
223 };
Definition: bitmap-shape.h:7
Document state store for layers, shapes, document specfic settings and current view state.
Definition: document.h:19
const QList< LayerPtr > & layers() const
Definition: document.cpp:182
void addLayer(LayerPtr &layer)
Definition: document.cpp:106
void setWidth(qreal width)
Definition: document.cpp:130
qreal height() const
Definition: document.cpp:128
qreal width() const
Definition: document.cpp:126
void setHeight(qreal height)
Definition: document.cpp:132
Save and load documents as binary format.
Definition: document-serializer.h:18
void serializeLayer(const LayerPtr &layer)
Definition: document-serializer.h:53
void deserializeShapeProp(Shape *shape)
Definition: document-serializer.h:143
void serializeShape(const ShapePtr &shape)
Definition: document-serializer.h:97
QDataStream & out
Definition: document-serializer.h:221
TextShape * deserializeTextShape()
Definition: document-serializer.h:210
void serializePathShape(PathShape *shape)
Definition: document-serializer.h:164
void serializeGroupShape(GroupShape *shape)
Definition: document-serializer.h:179
DocumentSerializer(QDataStream &stream)
Definition: document-serializer.h:20
QDataStream & in
Definition: document-serializer.h:222
Document * deserializeDocument()
Definition: document-serializer.h:33
BitmapShape * deserializeBitmapShape()
Definition: document-serializer.h:154
ShapePtr deserializeShape()
Definition: document-serializer.h:118
void serializeShapeProp(Shape *shape)
Definition: document-serializer.h:137
void serializeDocument(Document &doc)
Definition: document-serializer.h:24
void serializeBitmapShape(BitmapShape *shape)
Definition: document-serializer.h:149
LayerPtr deserializeLayer()
Definition: document-serializer.h:73
void serializeTextShape(TextShape *shape)
Definition: document-serializer.h:201
GroupShape * deserializeGroupShape()
Definition: document-serializer.h:187
PathShape * deserializePathShape()
Definition: document-serializer.h:170
Definition: group-shape.h:8
Definition: path-shape.h:7
QPainterPath path_
Definition: path-shape.h:52
A base class for shape objects that contains transform and parent information.
Definition: shape.h:17
Type
Definition: shape.h:19
bool selected_
Definition: shape.h:134
QTransform transform_
Definition: shape.h:128
Definition: text-shape.h:6
shared_ptr< Layer > LayerPtr
Definition: layer.h:127
shared_ptr< Shape > ShapePtr
Definition: shape.h:137