Basic encoding of drawn map
authorunc0rr
Sun, 28 Nov 2010 19:03:28 +0300
changeset 4427 c5193713055f
parent 4426 969e411c72aa
child 4428 2bc3d3475edf
Basic encoding of drawn map
tools/drawMapTest/drawmapscene.cpp
tools/drawMapTest/drawmapscene.h
tools/drawMapTest/mainwindow.cpp
tools/drawMapTest/mainwindow.h
--- a/tools/drawMapTest/drawmapscene.cpp	Sun Nov 28 17:23:51 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.cpp	Sun Nov 28 19:03:28 2010 +0300
@@ -1,6 +1,7 @@
 #include <QDebug>
 #include <QGraphicsSceneMouseEvent>
 #include <QGraphicsPathItem>
+#include <QtEndian>
 
 #include "drawmapscene.h"
 
@@ -31,8 +32,10 @@
     {
         QPainterPath path = m_currPath->path();
         path.lineTo(mouseEvent->scenePos());
+        paths.last().append(mouseEvent->scenePos().toPoint());
         m_currPath->setPath(path);
-        //drawFigure(mouseEvent->scenePos());
+
+        emit pathChanged();
     }
 }
 
@@ -47,9 +50,10 @@
     p += QPointF(0.01, 0.01);
     path.moveTo(p);
     path.lineTo(mouseEvent->scenePos());
+    paths.append(QList<QPoint>() << mouseEvent->scenePos().toPoint());
     m_currPath->setPath(path);
 
-    //drawFigure(mouseEvent->scenePos());
+    emit pathChanged();
 }
 
 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
@@ -62,5 +66,35 @@
 void DrawMapScene::undo()
 {
     if(items().size())
+    {
         removeItem(items().first());
+        paths.removeLast();
+
+        emit pathChanged();
+    }
 }
+
+QByteArray DrawMapScene::encode()
+{
+    QByteArray b;
+
+    foreach(QList<QPoint> points, paths)
+    {
+        int cnt = 0;
+        foreach(QPoint point, points)
+        {
+            qint16 px = qToBigEndian((qint16)point.x());
+            qint16 py = qToBigEndian((qint16)point.y());
+            quint8 flags = 2;
+            if(cnt) flags |= 0x80;
+            b.append((const char *)&flags, 1);
+            b.append((const char *)&px, 2);
+            b.append((const char *)&py, 2);
+
+            ++cnt;
+        }
+
+    }
+
+    return b;
+}
--- a/tools/drawMapTest/drawmapscene.h	Sun Nov 28 17:23:51 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.h	Sun Nov 28 19:03:28 2010 +0300
@@ -5,13 +5,18 @@
 
 class QGraphicsPathItem;
 
+typedef QList<QList<QPoint> > Paths;
+
 class DrawMapScene : public QGraphicsScene
 {
 Q_OBJECT
 public:
     explicit DrawMapScene(QObject *parent = 0);
 
+    QByteArray encode();
+
 signals:
+    void pathChanged();
 
 public slots:
     void undo();
@@ -20,6 +25,7 @@
     QPen m_pen;
     QBrush m_brush;
     QGraphicsPathItem  * m_currPath;
+    Paths paths;
 
     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
     virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
--- a/tools/drawMapTest/mainwindow.cpp	Sun Nov 28 17:23:51 2010 +0300
+++ b/tools/drawMapTest/mainwindow.cpp	Sun Nov 28 19:03:28 2010 +0300
@@ -8,10 +8,11 @@
 {
     ui->setupUi(this);
 
-    DrawMapScene * scene = new DrawMapScene(this);
+    scene = new DrawMapScene(this);
     ui->graphicsView->setScene(scene);
 
     connect(ui->pbUndo, SIGNAL(clicked()), scene, SLOT(undo()));
+    connect(scene, SIGNAL(pathChanged()), this, SLOT(scene_pathChanged()));
 }
 
 MainWindow::~MainWindow()
@@ -38,3 +39,8 @@
     if(ui->graphicsView)
         ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
 }
+
+void MainWindow::scene_pathChanged()
+{
+    ui->plainTextEdit->setPlainText(scene->encode().toBase64());
+}
--- a/tools/drawMapTest/mainwindow.h	Sun Nov 28 17:23:51 2010 +0300
+++ b/tools/drawMapTest/mainwindow.h	Sun Nov 28 19:03:28 2010 +0300
@@ -7,6 +7,8 @@
     class MainWindow;
 }
 
+class DrawMapScene;
+
 class MainWindow : public QMainWindow {
     Q_OBJECT
 public:
@@ -18,8 +20,12 @@
 
 private:
     Ui::MainWindow *ui;
+    DrawMapScene * scene;
 
     virtual void resizeEvent(QResizeEvent * event);
+
+private slots:
+    void scene_pathChanged();
 };
 
 #endif // MAINWINDOW_H