Cursor for DrawMapScene. Feel free to ajust its look.
authorunc0rr
Fri, 27 Apr 2012 00:43:48 +0400
changeset 6934 14a230552c2e
parent 6933 78d194a30520
child 6935 472ed92c4173
Cursor for DrawMapScene. Feel free to ajust its look.
QTfrontend/drawmapscene.cpp
QTfrontend/drawmapscene.h
QTfrontend/ui/widget/drawmapwidget.cpp
QTfrontend/ui/widget/drawmapwidget.h
--- a/QTfrontend/drawmapscene.cpp	Thu Apr 26 09:48:52 2012 +0200
+++ b/QTfrontend/drawmapscene.cpp	Fri Apr 27 00:43:48 2012 +0400
@@ -31,7 +31,8 @@
 DrawMapScene::DrawMapScene(QObject *parent) :
     QGraphicsScene(parent),
     m_pen(Qt::yellow),
-    m_brush(Qt::yellow)
+    m_brush(Qt::yellow),
+    m_cursor(new QGraphicsEllipseItem(-0.5, -0.5, 1, 1))
 {
     setSceneRect(0, 0, 4096, 2048);
 
@@ -47,6 +48,11 @@
     m_pen.setJoinStyle(Qt::RoundJoin);
     m_pen.setCapStyle(Qt::RoundCap);
     m_currPath = 0;
+
+    m_isCursorShown = false;
+    m_cursor->setPen(QPen(Qt::green));
+    m_cursor->setZValue(1);
+    m_cursor->setScale(m_pen.width());
 }
 
 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
@@ -71,6 +77,10 @@
 
         emit pathChanged();
     }
+
+    if(!m_isCursorShown)
+        showCursor();
+    m_cursor->setPos(mouseEvent->scenePos());
 }
 
 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
@@ -115,6 +125,8 @@
     else if(wheelEvent->delta() < 0 && m_pen.width() >= 16)
         m_pen.setWidth(m_pen.width() - 10);
 
+    m_cursor->setScale(m_pen.width());
+
     if(m_currPath)
     {
         m_currPath->setPen(m_pen);
@@ -122,8 +134,30 @@
     }
 }
 
+void DrawMapScene::showCursor()
+{
+    qDebug() << "show cursor";
+    if(!m_isCursorShown)
+        addItem(m_cursor);
+
+    m_isCursorShown = true;
+}
+
+void DrawMapScene::hideCursor()
+{
+    qDebug() << "hide cursor";
+    if(m_isCursorShown)
+        removeItem(m_cursor);
+
+    m_isCursorShown = false;
+}
+
 void DrawMapScene::undo()
 {
+    // cursor is a part of items()
+    if(m_isCursorShown)
+        return;
+
     if(items().size())
     {
         removeItem(items().first());
@@ -143,6 +177,10 @@
 
 void DrawMapScene::clearMap()
 {
+    // cursor is a part of items()
+    if(m_isCursorShown)
+        return;
+
     // don't clear if already cleared
     if(!items().size())
         return;
@@ -280,7 +318,7 @@
 
     // redraw path
     {
-        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
+        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]);
         pathItem->setPath(pointsToPath(paths[0].points));
     }
 
--- a/QTfrontend/drawmapscene.h	Thu Apr 26 09:48:52 2012 +0200
+++ b/QTfrontend/drawmapscene.h	Fri Apr 27 00:43:48 2012 +0400
@@ -21,6 +21,7 @@
 
 #include <QGraphicsScene>
 #include <QPainterPath>
+#include <QGraphicsEllipseItem>
 
 class QGraphicsPathItem;
 
@@ -50,6 +51,8 @@
         void clearMap();
         void simplifyLast();
         void setErasing(bool erasing);
+        void showCursor();
+        void hideCursor();
 
     private:
         QPen m_pen;
@@ -60,6 +63,8 @@
         Paths oldPaths;
         bool m_isErasing;
         QList<QGraphicsItem *> oldItems;
+        QGraphicsEllipseItem * m_cursor;
+        bool m_isCursorShown;
 
         virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
         virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
--- a/QTfrontend/ui/widget/drawmapwidget.cpp	Thu Apr 26 09:48:52 2012 +0200
+++ b/QTfrontend/ui/widget/drawmapwidget.cpp	Fri Apr 27 00:43:48 2012 +0400
@@ -19,6 +19,7 @@
 #include <QFile>
 #include <QMessageBox>
 #include <QEvent>
+#include <QDebug>
 
 #include "drawmapwidget.h"
 
@@ -51,8 +52,9 @@
 
 void DrawMapWidget::setScene(DrawMapScene * scene)
 {
+    m_scene = scene;
+
     ui->graphicsView->setScene(scene);
-    m_scene = scene;
 }
 
 void DrawMapWidget::resizeEvent(QResizeEvent * event)
@@ -110,3 +112,48 @@
             m_scene->decode(qUncompress(QByteArray::fromBase64(f.readAll())));
     }
 }
+
+
+
+DrawMapView::DrawMapView(QWidget *parent) :
+    QGraphicsView(parent)
+{
+   setMouseTracking(true);
+
+    m_scene = 0;
+}
+
+
+DrawMapView::~DrawMapView()
+{
+
+}
+
+void DrawMapView::setScene(DrawMapScene *scene)
+{
+    m_scene = scene;
+
+    QGraphicsView::setScene(scene);
+}
+
+// Why don't I ever recieve this event?
+void DrawMapView::enterEvent(QEvent *event)
+{
+    if(m_scene)
+        m_scene->showCursor();
+
+    QGraphicsView::enterEvent(event);
+}
+
+void DrawMapView::leaveEvent(QEvent *event)
+{
+    if(m_scene)
+        m_scene->hideCursor();
+
+    QGraphicsView::leaveEvent(event);
+}
+
+bool DrawMapView::viewportEvent(QEvent *event)
+{
+    return QGraphicsView::viewportEvent(event);
+}
--- a/QTfrontend/ui/widget/drawmapwidget.h	Thu Apr 26 09:48:52 2012 +0200
+++ b/QTfrontend/ui/widget/drawmapwidget.h	Fri Apr 27 00:43:48 2012 +0400
@@ -27,19 +27,39 @@
 #include "qaspectratiolayout.h"
 #include "drawmapscene.h"
 
+
+class DrawMapView : public QGraphicsView
+{
+    Q_OBJECT
+
+public:
+    explicit DrawMapView(QWidget *parent = 0);
+    ~DrawMapView();
+
+    void setScene(DrawMapScene *scene);
+
+protected:
+    void enterEvent(QEvent * event);
+    void leaveEvent(QEvent * event);
+    bool viewportEvent(QEvent * event);
+
+private:
+    DrawMapScene * m_scene;
+};
+
 namespace Ui
 {
     class Ui_DrawMapWidget
     {
         public:
-            QGraphicsView *graphicsView;
+            DrawMapView *graphicsView;
 
             void setupUi(QWidget *drawMapWidget)
             {
                 QAspectRatioLayout * arLayout = new QAspectRatioLayout(drawMapWidget);
                 arLayout->setMargin(0);
 
-                graphicsView = new QGraphicsView(drawMapWidget);
+                graphicsView = new DrawMapView(drawMapWidget);
                 arLayout->addWidget(graphicsView);
 
                 retranslateUi(drawMapWidget);