Swiftray  1.0
image-context.h
Go to the documentation of this file.
1 #include <QImage>
2 #include <QByteArray>
3 #include <parser/svgpp-common.h>
4 #include <shape/bitmap-shape.h>
6 
7 #pragma once
8 
9 namespace Parser {
10 
11 class ImageContext : public BaseContext {
12 public:
13  ImageContext(BaseContext const &parent) : BaseContext(parent) {
14  qInfo() << "Enter image";
15  width_ = 0;
16  height_ = 0;
17  bitmap_ = nullptr;
18  }
19 
20  boost::optional<double> const &width() const { return width_; }
21 
22  boost::optional<double> const &height() const { return height_; }
23 
24  using BaseContext::set;
25 
26  template<class IRI>
27  void set(tag::attribute::xlink::href, tag::iri_fragment,
28  IRI const &fragment) {
29  qInfo() << "xlink::href" << fragment;
30  }
31 
32  void set(tag::attribute::xlink::href, RangedChar fragment) {
33  auto str = std::string(fragment.begin(), fragment.end());
34  auto substr = std::string(fragment.begin() + 22, fragment.end());
35  qInfo() << "xlink::href (from string)" << substr.size();
36  QImage img = QImage::fromData(QByteArray::fromBase64(QString::fromStdString(substr).toUtf8()));
37  qInfo() << "image size" << img.size();
38  bitmap_ = make_shared<BitmapShape>(img);
39  }
40 
41  void set(tag::attribute::x, double val) { x_ = val; }
42 
43  void set(tag::attribute::y, double val) { y_ = val; }
44 
45  void set(tag::attribute::width, double val) { width_ = val; }
46 
47  void set(tag::attribute::height, double val) { height_ = val; }
48 
49  void on_exit_element() {
50  QString bitmap_layer_name("Bitmap");
51  BitmapShape *new_shape = (BitmapShape *) bitmap_.get();
52  if (width_ == 0) width_ = new_shape->pixmap()->width();
53  if (height_ == 0) height_ = new_shape->pixmap()->height();
54  new_shape->setTransform(
55  qtransform() * QTransform().translate(x_, y_).scale(
56  width_ / new_shape->pixmap()->width(),
57  height_ / new_shape->pixmap()->height())
58 
59  );
60  svgpp_add_shape(bitmap_, bitmap_layer_name);
61  }
62 
63  string type() {
64  return "image";
65  }
66 
67 private:
68  std::string fragment_id_;
69  double x_, y_;
70  double width_, height_;
71  shared_ptr<Shape> bitmap_;
72 };
73 
74 }
Definition: bitmap-shape.h:7
const QPixmap * pixmap() const
Definition: bitmap-shape.cpp:94
Definition: base-context.h:10
void set(tag::attribute::width, double val)
Definition: object-context.h:9
Definition: image-context.h:11
void on_exit_element()
Definition: image-context.h:49
string type()
Definition: image-context.h:63
void set(tag::attribute::width, double val)
Definition: image-context.h:45
ImageContext(BaseContext const &parent)
Definition: image-context.h:13
void set(tag::attribute::xlink::href, RangedChar fragment)
Definition: image-context.h:32
void set(tag::attribute::x, double val)
Definition: image-context.h:41
void set(tag::attribute::height, double val)
Definition: image-context.h:47
boost::optional< double > const & height() const
Definition: image-context.h:22
void set(tag::attribute::xlink::href, tag::iri_fragment, IRI const &fragment)
Definition: image-context.h:27
void set(tag::attribute::y, double val)
Definition: image-context.h:43
boost::optional< double > const & width() const
Definition: image-context.h:20
QTransform qtransform()
Definition: transformable-context.h:28
void setTransform(const QTransform &transform)
Definition: shape.cpp:60
Definition: base-context.h:8
boost::iterator_range< const char * > RangedChar
Definition: svgpp-defs.h:13
void svgpp_add_shape(ShapePtr &shape, QString &layer_name)
Definition: svgpp-impl.cpp:268