[Kst] branches/work/kst/portto4/kst/src/libkstapp

Mike Fenton mike at staikos.net
Mon Oct 15 17:25:28 CEST 2007


SVN commit 725504 by fenton:

Implementation of Save/Restore for SvgItem.


 M  +2 -0      builtingraphics.cpp  
 M  +90 -1     svgitem.cpp  
 M  +14 -1     svgitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/builtingraphics.cpp #725503:725504
@@ -15,6 +15,7 @@
 #include "ellipseitem.h"
 #include "labelitem.h"
 #include "pictureitem.h"
+#include "svgitem.h"
 
 namespace Kst {
   namespace Builtins {
@@ -24,6 +25,7 @@
       new EllipseItemFactory;
       new LabelItemFactory;
       new PictureItemFactory;
+      new SvgItemFactory;
     }
   }
 }
--- branches/work/kst/portto4/kst/src/libkstapp/svgitem.cpp #725503:725504
@@ -10,16 +10,30 @@
  ***************************************************************************/
 
 #include "svgitem.h"
+#include "debug.h"
 
 #include <QDebug>
 #include <QFileDialog>
 #include <QGraphicsScene>
 #include <QSvgRenderer>
 
+
 namespace Kst {
 
 SvgItem::SvgItem(View *parent, const QString &file)
-  : ViewItem(parent), _svg(new QSvgRenderer(file)) {
+  : ViewItem(parent) {
+
+  if (!file.isNull()) {
+    _svg = new QSvgRenderer(file);
+    QFile svgfile(file);
+    if (svgfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
+      while (!svgfile.atEnd()) {
+          _svgData.append(svgfile.readLine());
+      }
+    }
+  } else {
+    _svg = new QSvgRenderer();
+  }
   //FIXME need to set the element id??
   setName("Svg");
   setLockAspectRatio(true);
@@ -38,6 +52,21 @@
 }
 
 
+void SvgItem::save(QXmlStreamWriter &xml) {
+  xml.writeStartElement("svg");
+  ViewItem::save(xml);
+  xml.writeStartElement("data");
+  xml.writeCharacters(qCompress(_svgData).toBase64());
+  xml.writeEndElement();
+  xml.writeEndElement();
+}
+
+
+void SvgItem::setSvgData(const QByteArray &svgData) {
+  _svg->load(svgData);
+  _svgData = svgData;
+}
+
 void CreateSvgCommand::createItem() {
   QString file = QFileDialog::getOpenFileName(_view, tr("Kst: Open Svg Image"));
   if (file.isEmpty())
@@ -49,6 +78,66 @@
   CreateCommand::createItem();
 }
 
+
+SvgItemFactory::SvgItemFactory()
+: GraphicsFactory() {
+  registerFactory("svg", this);
 }
 
+
+SvgItemFactory::~SvgItemFactory() {
+}
+
+
+ViewItem* SvgItemFactory::generateGraphics(QXmlStreamReader& xml, View *view, ViewItem *parent) {
+  SvgItem *rc = 0;
+  while (!xml.atEnd()) {
+    bool validTag = true;
+    if (xml.isStartElement()) {
+      if (xml.name().toString() == "svg") {
+        Q_ASSERT(!rc);
+        rc = new SvgItem(view);
+        if (parent) {
+          rc->setParentItem(parent);
+        }
+        // TODO add any specialized SvgItem Properties here.
+      } else if (xml.name().toString() == "data") {
+        Q_ASSERT(rc);
+        xml.readNext();
+        QByteArray qbca = QByteArray::fromBase64(xml.text().toString().toLatin1());
+        rc->setSvgData(qUncompress(qbca));
+        xml.readNext();
+        if (!xml.isEndElement() || (xml.name().toString() != "data")) {
+          validTag = false;
+        }
+        xml.readNext();
+      } else {
+        Q_ASSERT(rc);
+        if (!rc->parse(xml, validTag) && validTag) {
+          ViewItem *i = GraphicsFactory::parse(xml, view, rc);
+          if (!i) {
+          }
+        }
+      }
+    } else if (xml.isEndElement()) {
+      if (xml.name().toString() == "svg") {
+        break;
+      } else {
+        validTag = false;
+      }
+    }
+    if (!validTag) {
+      qDebug("invalid Tag\n");
+      Debug::self()->log(QObject::tr("Error creating svg object from Kst file."), Debug::Warning);
+      delete rc;
+      return 0;
+    }
+    xml.readNext();
+  }
+  return rc;
+}
+
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/svgitem.h #725503:725504
@@ -13,6 +13,7 @@
 #define SVGITEM_H
 
 #include "viewitem.h"
+#include "graphicsfactory.h"
 
 class QSvgRenderer;
 
@@ -22,13 +23,17 @@
 {
   Q_OBJECT
   public:
-    SvgItem(View *parent, const QString &file);
+    SvgItem(View *parent, const QString &file = QString());
     ~SvgItem();
 
+    virtual void save(QXmlStreamWriter &xml);
     virtual void paint(QPainter *painter);
 
+    void setSvgData(const QByteArray &svgData);
+
   private:
     QSvgRenderer *_svg;
+    QByteArray _svgData;
 };
 
 
@@ -41,6 +46,14 @@
     void createItem();
 };
 
+
+class SvgItemFactory : public GraphicsFactory {
+  public:
+    SvgItemFactory();
+    ~SvgItemFactory();
+    ViewItem* generateGraphics(QXmlStreamReader& stream, View *view, ViewItem *parent = 0);
+};
+
 }
 
 #endif


More information about the Kst mailing list