Improve map editor a bit
authorunc0rr
Sun, 28 Nov 2010 17:23:51 +0300
changeset 4426 969e411c72aa
parent 4425 2314bb0c433d
child 4427 c5193713055f
Improve map editor a bit
tools/drawMapTest/drawmapscene.cpp
tools/drawMapTest/drawmapscene.h
tools/drawMapTest/mainwindow.cpp
tools/drawMapTest/mainwindow.ui
--- a/tools/drawMapTest/drawmapscene.cpp	Sat Nov 27 23:57:31 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.cpp	Sun Nov 28 17:23:51 2010 +0300
@@ -1,21 +1,25 @@
 #include <QDebug>
 #include <QGraphicsSceneMouseEvent>
+#include <QGraphicsPathItem>
 
 #include "drawmapscene.h"
 
 DrawMapScene::DrawMapScene(QObject *parent) :
     QGraphicsScene(parent),
-    m_pen(Qt::black),
-    m_brush(Qt::black)
+    m_pen(Qt::yellow),
+    m_brush(Qt::yellow)
 {
     setSceneRect(0, 0, 4096, 2048);
 
     QLinearGradient gradient(0, 0, 0, 2048);
-    gradient.setColorAt(0, QColor(160, 160, 255));
-    gradient.setColorAt(1, QColor(255, 255, 160));
+    gradient.setColorAt(0, QColor(60, 60, 155));
+    gradient.setColorAt(1, QColor(155, 155, 60));
     setBackgroundBrush(QBrush(gradient));
 
-    m_halfWidth = 67;
+    m_pen.setWidth(67);
+    m_pen.setJoinStyle(Qt::RoundJoin);
+    m_pen.setCapStyle(Qt::RoundCap);
+    m_currPath = 0;
 }
 
 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
@@ -23,30 +27,40 @@
 
     qDebug() << "move" << mouseEvent->scenePos();
 
-    if(mouseEvent->buttons() && Qt::LeftButton)
-        drawFigure(mouseEvent->scenePos());
+    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
+    {
+        QPainterPath path = m_currPath->path();
+        path.lineTo(mouseEvent->scenePos());
+        m_currPath->setPath(path);
+        //drawFigure(mouseEvent->scenePos());
+    }
 }
 
 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
 {
     qDebug() << "press" << mouseEvent->scenePos();
 
-    drawFigure(mouseEvent->scenePos());
+    m_currPath = addPath(QPainterPath(), m_pen);
+
+    QPainterPath path = m_currPath->path();
+    QPointF p = mouseEvent->scenePos();
+    p += QPointF(0.01, 0.01);
+    path.moveTo(p);
+    path.lineTo(mouseEvent->scenePos());
+    m_currPath->setPath(path);
+
+    //drawFigure(mouseEvent->scenePos());
 }
 
 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
 {
     qDebug() << "release" << mouseEvent->scenePos();
+
+    m_currPath = 0;
 }
 
-void DrawMapScene::drawFigure(const QPointF & point)
+void DrawMapScene::undo()
 {
-    addEllipse(
-            point.x() - m_halfWidth,
-            point.y() - m_halfWidth,
-            m_halfWidth * 2,
-            m_halfWidth * 2,
-            m_pen,
-            m_brush
-        );
+    if(items().size())
+        removeItem(items().first());
 }
--- a/tools/drawMapTest/drawmapscene.h	Sat Nov 27 23:57:31 2010 +0300
+++ b/tools/drawMapTest/drawmapscene.h	Sun Nov 28 17:23:51 2010 +0300
@@ -3,6 +3,8 @@
 
 #include <QGraphicsScene>
 
+class QGraphicsPathItem;
+
 class DrawMapScene : public QGraphicsScene
 {
 Q_OBJECT
@@ -12,17 +14,16 @@
 signals:
 
 public slots:
+    void undo();
 
 private:
-    qreal m_halfWidth;
     QPen m_pen;
     QBrush m_brush;
+    QGraphicsPathItem  * m_currPath;
 
     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
     virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
-
-    void drawFigure(const QPointF & point);
 };
 
 #endif // DRAWMAPSCENE_H
--- a/tools/drawMapTest/mainwindow.cpp	Sat Nov 27 23:57:31 2010 +0300
+++ b/tools/drawMapTest/mainwindow.cpp	Sun Nov 28 17:23:51 2010 +0300
@@ -10,6 +10,8 @@
 
     DrawMapScene * scene = new DrawMapScene(this);
     ui->graphicsView->setScene(scene);
+
+    connect(ui->pbUndo, SIGNAL(clicked()), scene, SLOT(undo()));
 }
 
 MainWindow::~MainWindow()
--- a/tools/drawMapTest/mainwindow.ui	Sat Nov 27 23:57:31 2010 +0300
+++ b/tools/drawMapTest/mainwindow.ui	Sun Nov 28 17:23:51 2010 +0300
@@ -14,11 +14,11 @@
    <string>MainWindow</string>
   </property>
   <widget class="QWidget" name="centralWidget">
-   <layout class="QVBoxLayout" name="verticalLayout">
-    <item>
+   <layout class="QGridLayout" name="gridLayout">
+    <item row="0" column="2">
      <widget class="QGraphicsView" name="graphicsView"/>
     </item>
-    <item>
+    <item row="1" column="2">
      <widget class="QPlainTextEdit" name="plainTextEdit">
       <property name="maximumSize">
        <size>
@@ -28,6 +28,30 @@
       </property>
      </widget>
     </item>
+    <item row="0" column="0" rowspan="2">
+     <layout class="QVBoxLayout" name="verticalLayout">
+      <item>
+       <widget class="QPushButton" name="pbUndo">
+        <property name="text">
+         <string>Undo</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <spacer name="verticalSpacer">
+        <property name="orientation">
+         <enum>Qt::Vertical</enum>
+        </property>
+        <property name="sizeHint" stdset="0">
+         <size>
+          <width>20</width>
+          <height>40</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+     </layout>
+    </item>
    </layout>
   </widget>
  </widget>