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

Mike Fenton mike at staikos.net
Fri Oct 12 22:47:30 CEST 2007


SVN commit 724619 by fenton:

Implementation of Save/Restore for PictureItem


 M  +2 -0      builtingraphics.cpp  
 M  +68 -0     pictureitem.cpp  
 M  +10 -0     pictureitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/builtingraphics.cpp #724618:724619
@@ -14,6 +14,7 @@
 #include "lineitem.h"
 #include "ellipseitem.h"
 #include "labelitem.h"
+#include "pictureitem.h"
 
 namespace Kst {
   namespace Builtins {
@@ -22,6 +23,7 @@
       new LineItemFactory;
       new EllipseItemFactory;
       new LabelItemFactory;
+      new PictureItemFactory;
     }
   }
 }
--- branches/work/kst/portto4/kst/src/libkstapp/pictureitem.cpp #724618:724619
@@ -11,10 +11,13 @@
 
 #include "pictureitem.h"
 
+#include "debug.h"
+
 #include <QDebug>
 #include <QFileDialog>
 #include <QGraphicsItem>
 #include <QGraphicsScene>
+#include <QBuffer>
 
 namespace Kst {
 
@@ -29,6 +32,18 @@
 }
 
 
+void PictureItem::save(QXmlStreamWriter &xml) {
+  xml.writeStartElement("picture");
+  QByteArray qba;
+  QBuffer buffer(&qba);
+  buffer.open(QIODevice::WriteOnly);
+  QImage(_image).save(&buffer, "PNG"); // writes image into ba in PNG format
+  xml.writeAttribute("data", qCompress(qba).toBase64());
+  ViewItem::save(xml);
+  xml.writeEndElement();
+}
+
+
 void PictureItem::paint(QPainter *painter) {
   // We can do better here.  Cache the scaled pixmap also.
   if (!_image.isNull() && rect().isValid()) {
@@ -49,6 +64,59 @@
   CreateCommand::createItem();
 }
 
+
+PictureItemFactory::PictureItemFactory()
+: GraphicsFactory() {
+  registerFactory("picture", this);
 }
 
+
+PictureItemFactory::~PictureItemFactory() {
+}
+
+
+ViewItem* PictureItemFactory::generateGraphics(QXmlStreamReader& xml, View *view, ViewItem *parent) {
+  PictureItem *rc = 0;
+  while (!xml.atEnd()) {
+    bool validTag = true;
+    if (xml.isStartElement()) {
+      if (xml.name().toString() == "picture") {
+        Q_ASSERT(!rc);
+        QXmlStreamAttributes attrs = xml.attributes();
+        QImage loadedImage;
+        QByteArray qbca = QByteArray::fromBase64(attrs.value("data").toString().toLatin1());
+        loadedImage.loadFromData(qUncompress(qbca));
+        rc = new PictureItem(view, loadedImage);
+        if (parent) {
+          rc->setParentItem(parent);
+        }
+        // TODO add any specialized PictureItem Properties here.
+      } 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() == "picture") {
+        break;
+      } else {
+        validTag = false;
+      }
+    }
+    if (!validTag) {
+      qDebug("invalid Tag\n");
+      Debug::self()->log(QObject::tr("Error creating picture 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/pictureitem.h #724618:724619
@@ -13,6 +13,7 @@
 #define PICTUREITEM_H
 
 #include "viewitem.h"
+#include "graphicsfactory.h"
 
 namespace Kst {
 
@@ -23,6 +24,7 @@
     PictureItem(View *parent, const QImage &image);
     ~PictureItem();
 
+    virtual void save(QXmlStreamWriter &xml);
     virtual void paint(QPainter *painter);
 
   private:
@@ -39,6 +41,14 @@
     void createItem();
 };
 
+
+class PictureItemFactory : public GraphicsFactory {
+  public:
+    PictureItemFactory();
+    ~PictureItemFactory();
+    ViewItem* generateGraphics(QXmlStreamReader& stream, View *view, ViewItem *parent = 0);
+};
+
 }
 
 #endif


More information about the Kst mailing list