- Implement decode() function.
authorunc0rr
Wed, 01 Dec 2010 22:06:23 +0300
changeset 4442 f8424e1bc936
parent 4441 bb156179f56e
child 4443 d393b9ccd328
child 4444 92fc23595de8
- Implement decode() function. - Implement saving and loading to/from file
tools/drawMapTest/drawmapscene.cpp
tools/drawMapTest/drawmapscene.h
tools/drawMapTest/mainwindow.cpp
tools/drawMapTest/mainwindow.h
tools/drawMapTest/mainwindow.ui
--- a/tools/drawMapTest/drawmapscene.cpp	Wed Dec 01 20:45:46 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.cpp	Wed Dec 01 22:06:23 2010 +0300
@@ -93,7 +93,7 @@
             qint16 px = qToBigEndian((qint16)point.x());
             qint16 py = qToBigEndian((qint16)point.y());
             quint8 flags = 2;
-            if(cnt) flags |= 0x80;
+            if(!cnt) flags |= 0x80;
             b.append((const char *)&flags, 1);
             b.append((const char *)&px, 2);
             b.append((const char *)&py, 2);
@@ -106,6 +106,39 @@
     return b;
 }
 
+void DrawMapScene::decode(QByteArray data)
+{
+    clear();
+    paths.clear();
+
+    QList<QPoint> points;
+
+    while(data.size() >= 5)
+    {
+        quint8 flags = *(quint8 *)data.data();
+        data.remove(0, 1);
+        qint16 px = qFromBigEndian(*(qint16 *)data.data());
+        data.remove(0, 2);
+        qint16 py = qFromBigEndian(*(qint16 *)data.data());
+        data.remove(0, 2);
+
+        //last chunk or first point
+        if((data.size() < 5) || (flags & 0x80))
+        {
+            if(points.size())
+            {
+                qDebug() << points;
+                addPath(pointsToPath(points), m_pen);
+                paths.prepend(points);
+
+                points.clear();
+            }
+        }
+
+        points.append(QPoint(px, py));
+    }
+}
+
 void DrawMapScene::simplifyLast()
 {
     QList<QPoint> points = paths[0];
@@ -128,19 +161,25 @@
 
     // redraw path
     {
-
-        QPainterPath path;
-        QPointF p = paths[0][0] + QPointF(0.01, 0.01);
-        path.moveTo(p);
-
-        foreach(QPoint p, paths[0])
-            path.lineTo(p);
-
         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
-        pathItem->setPath(path);
-
-        ++i;
+        pathItem->setPath(pointsToPath(paths[0]));
     }
 
     emit pathChanged();
 }
+
+QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
+{
+    QPainterPath path;
+
+    if(points.size())
+    {
+        QPointF p = points[0] + QPointF(0.01, 0.01);
+        path.moveTo(p);
+
+        foreach(QPoint p, points)
+            path.lineTo(p);
+    }
+
+    return path;
+}
--- a/tools/drawMapTest/drawmapscene.h	Wed Dec 01 20:45:46 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.h	Wed Dec 01 22:06:23 2010 +0300
@@ -2,6 +2,7 @@
 #define DRAWMAPSCENE_H
 
 #include <QGraphicsScene>
+#include <QPainterPath>
 
 class QGraphicsPathItem;
 
@@ -14,6 +15,7 @@
     explicit DrawMapScene(QObject *parent = 0);
 
     QByteArray encode();
+    void decode(QByteArray data);
 
 signals:
     void pathChanged();
@@ -31,6 +33,8 @@
     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
     virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
+
+    QPainterPath pointsToPath(const QList<QPoint> points);
 };
 
 #endif // DRAWMAPSCENE_H
--- a/tools/drawMapTest/mainwindow.cpp	Wed Dec 01 20:45:46 2010 +0300
+++ b/tools/drawMapTest/mainwindow.cpp	Wed Dec 01 22:06:23 2010 +0300
@@ -1,3 +1,5 @@
+#include <QFileDialog>
+
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 #include "drawmapscene.h"
@@ -51,3 +53,30 @@
 {
     scene->simplifyLast();
 }
+
+void MainWindow::on_pbSave_clicked()
+{
+    QString fileName = QFileDialog::getSaveFileName(this, tr("Save map"), ".");
+
+    if(!fileName.isEmpty())
+    {
+        QFile f(fileName);
+
+        f.open(QIODevice::WriteOnly);
+        f.write(qCompress(scene->encode()).toBase64());
+    }
+}
+
+void MainWindow::on_pbLoad_clicked()
+{
+    QString fileName = QFileDialog::getOpenFileName(this, tr("Open map file"), ".");
+
+    if(!fileName.isEmpty())
+    {
+        QFile f(fileName);
+
+        f.open(QIODevice::ReadOnly);
+        QByteArray data = qUncompress(QByteArray::fromBase64(f.readAll()));
+        scene->decode(data);
+    }
+}
--- a/tools/drawMapTest/mainwindow.h	Wed Dec 01 20:45:46 2010 +0300
+++ b/tools/drawMapTest/mainwindow.h	Wed Dec 01 22:06:23 2010 +0300
@@ -25,6 +25,8 @@
     virtual void resizeEvent(QResizeEvent * event);
 
 private slots:
+    void on_pbLoad_clicked();
+    void on_pbSave_clicked();
     void on_pbSimplify_clicked();
     void scene_pathChanged();
 };
--- a/tools/drawMapTest/mainwindow.ui	Wed Dec 01 20:45:46 2010 +0300
+++ b/tools/drawMapTest/mainwindow.ui	Wed Dec 01 22:06:23 2010 +0300
@@ -44,6 +44,20 @@
        </spacer>
       </item>
       <item>
+       <widget class="QPushButton" name="pbSave">
+        <property name="text">
+         <string>Save</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="pbLoad">
+        <property name="text">
+         <string>Load</string>
+        </property>
+       </widget>
+      </item>
+      <item>
        <widget class="QSpinBox" name="sbBytes">
         <property name="readOnly">
          <bool>true</bool>