Drawn map menu: Add spin box to change brush size by hand
authorWuzzy <Wuzzy2@mail.ru>
Wed, 14 Mar 2018 22:59:06 +0100 (2018-03-14)
changeset 13209 f5d36be88c61
parent 13207 d948d39e5da8
child 13210 25ebc1ac0967
Drawn map menu: Add spin box to change brush size by hand
QTfrontend/drawmapscene.cpp
QTfrontend/drawmapscene.h
QTfrontend/ui/page/pagedrawmap.cpp
QTfrontend/ui/page/pagedrawmap.h
QTfrontend/ui/widget/drawmapwidget.cpp
QTfrontend/ui/widget/drawmapwidget.h
--- a/QTfrontend/drawmapscene.cpp	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/drawmapscene.cpp	Wed Mar 14 22:59:06 2018 +0100
@@ -52,7 +52,7 @@
 
     m_pathType = Polyline;
 
-    m_pen.setWidth(76);
+    m_pen.setWidth(DRAWN_MAP_BRUSH_SIZE_START);
     m_pen.setJoinStyle(Qt::RoundJoin);
     m_pen.setCapStyle(Qt::RoundCap);
     m_currPath = 0;
@@ -61,7 +61,7 @@
     QPen cursorPen = QPen(DRAWN_MAP_COLOR_CURSOR_PEN);
     cursorPen.setJoinStyle(Qt::RoundJoin);
     cursorPen.setCapStyle(Qt::RoundCap);
-    cursorPen.setWidth(m_pen.width());
+    cursorPen.setWidth(brushSize());
     m_cursor->setPen(cursorPen);
     m_cursor->setZValue(1);
 }
@@ -131,7 +131,7 @@
     path.lineTo(mouseEvent->scenePos());
 
     PathParams params;
-    params.width = serializePenWidth(m_pen.width());
+    params.width = serializePenWidth(brushSize());
     params.erasing = m_isErasing;
     params.initialPoint = mouseEvent->scenePos().toPoint();
     params.points = QList<QPoint>() << params.initialPoint;
@@ -185,22 +185,38 @@
     }
 }
 
-void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
+void DrawMapScene::setBrushSize(int newBrushSize)
 {
-    if(wheelEvent->delta() > 0 && m_pen.width() < 516)
-        m_pen.setWidth(m_pen.width() + 10);
-    else if(wheelEvent->delta() < 0 && m_pen.width() >= 16)
-        m_pen.setWidth(m_pen.width() - 10);
+    if(newBrushSize > DRAWN_MAP_BRUSH_SIZE_MAX)
+        newBrushSize = DRAWN_MAP_BRUSH_SIZE_MAX;
+    if(newBrushSize < DRAWN_MAP_BRUSH_SIZE_MIN)
+        newBrushSize = DRAWN_MAP_BRUSH_SIZE_MIN;
 
+    m_pen.setWidth(newBrushSize);
     QPen cursorPen = m_cursor->pen();
     cursorPen.setWidth(m_pen.width());
     m_cursor->setPen(cursorPen);
-
     if(m_currPath)
     {
         m_currPath->setPen(m_pen);
         paths.first().width = serializePenWidth(m_pen.width());
     }
+
+    emit brushSizeChanged(newBrushSize);
+}
+
+int DrawMapScene::brushSize()
+{
+    return m_pen.width();
+}
+
+void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
+{
+    int b = brushSize();
+    if(wheelEvent->delta() > 0)
+        setBrushSize(b + DRAWN_MAP_BRUSH_SIZE_STEP);
+    else if(wheelEvent->delta() < 0 && b >= DRAWN_MAP_BRUSH_SIZE_MIN)
+        setBrushSize(b - DRAWN_MAP_BRUSH_SIZE_STEP);
 }
 
 void DrawMapScene::showCursor()
--- a/QTfrontend/drawmapscene.h	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/drawmapscene.h	Wed Mar 14 22:59:06 2018 +0100
@@ -23,6 +23,11 @@
 #include <QPainterPath>
 #include <QGraphicsEllipseItem>
 
+#define DRAWN_MAP_BRUSH_SIZE_STEP (10)
+#define DRAWN_MAP_BRUSH_SIZE_MAX (516)
+#define DRAWN_MAP_BRUSH_SIZE_MIN (16)
+#define DRAWN_MAP_BRUSH_SIZE_START (76)
+
 class QGraphicsPathItem;
 
 struct PathParams
@@ -50,9 +55,11 @@
         QByteArray encode();
         void decode(QByteArray data);
         int pointsCount();
+        int brushSize();
 
     signals:
         void pathChanged();
+        void brushSizeChanged(int brushSize);
 
     public slots:
         void undo();
@@ -63,6 +70,7 @@
         void showCursor();
         void hideCursor();
         void setPathType(PathType pathType);
+        void setBrushSize(int brushSize);
 
     private:
         QPen m_pen;
--- a/QTfrontend/ui/page/pagedrawmap.cpp	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/ui/page/pagedrawmap.cpp	Wed Mar 14 22:59:06 2018 +0100
@@ -21,6 +21,7 @@
 #include <QFileDialog>
 #include <QCheckBox>
 #include <QRadioButton>
+#include <QSpinBox>
 
 #include "pagedrawmap.h"
 #include "drawmapwidget.h"
@@ -42,10 +43,17 @@
 
     rbPolyline->setChecked(true);
 
-    pbUndo = addButton(tr("Undo"), pageLayout, 4, 0);
-    pbClear = addButton(tr("Clear"), pageLayout, 5, 0);
+    sbBrushSize = new QSpinBox(this);
+    sbBrushSize->setWhatsThis(tr("Brush size"));
+    sbBrushSize->setRange(DRAWN_MAP_BRUSH_SIZE_MIN, DRAWN_MAP_BRUSH_SIZE_MAX);
+    sbBrushSize->setValue(DRAWN_MAP_BRUSH_SIZE_START);
+    sbBrushSize->setSingleStep(DRAWN_MAP_BRUSH_SIZE_STEP);
+    pageLayout->addWidget(sbBrushSize, 4, 0);
 
-    pbOptimize = addButton(tr("Optimize"), pageLayout, 6, 0);
+    pbUndo = addButton(tr("Undo"), pageLayout, 5, 0);
+    pbClear = addButton(tr("Clear"), pageLayout, 6, 0);
+
+    pbOptimize = addButton(tr("Optimize"), pageLayout, 7, 0);
     // The optimize button is quite buggy, so we disable it for now.
     // TODO: Re-enable optimize button when it's finished.
     pbOptimize->setVisible(false);
@@ -79,6 +87,10 @@
     connect(pbUndo, SIGNAL(clicked()), drawMapWidget, SLOT(undo()));
     connect(pbClear, SIGNAL(clicked()), drawMapWidget, SLOT(clear()));
     connect(pbOptimize, SIGNAL(clicked()), drawMapWidget, SLOT(optimize()));
+    connect(sbBrushSize, SIGNAL(valueChanged(int)), drawMapWidget, SLOT(setBrushSize(int)));
+
+    connect(drawMapWidget, SIGNAL(brushSizeChanged(int)), this, SLOT(brushSizeChanged(int)));
+
     connect(pbLoad, SIGNAL(clicked()), this, SLOT(load()));
     connect(pbSave, SIGNAL(clicked()), this, SLOT(save()));
 
@@ -117,3 +129,8 @@
         else if(rbEllipse->isChecked()) drawMapWidget->setPathType(DrawMapScene::Ellipse);
     }
 }
+
+void PageDrawMap::brushSizeChanged(int brushSize)
+{
+    sbBrushSize->setValue(brushSize);
+}
--- a/QTfrontend/ui/page/pagedrawmap.h	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/ui/page/pagedrawmap.h	Wed Mar 14 22:59:06 2018 +0100
@@ -48,6 +48,10 @@
         QRadioButton * rbPolyline;
         QRadioButton * rbRectangle;
         QRadioButton * rbEllipse;
+        QSpinBox * sbBrushSize;
+
+    public slots:
+        void brushSizeChanged(int brushSize);
 
     private slots:
         void load();
--- a/QTfrontend/ui/widget/drawmapwidget.cpp	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/ui/widget/drawmapwidget.cpp	Wed Mar 14 22:59:06 2018 +0100
@@ -56,6 +56,7 @@
 
     ui->graphicsView->setScene(scene);
     connect(scene, SIGNAL(pathChanged()), this, SLOT(pathChanged()));
+    connect(scene, SIGNAL(brushSizeChanged(int)), this, SLOT(brushSizeChanged_slot(int)));
 }
 
 void DrawMapWidget::resizeEvent(QResizeEvent * event)
@@ -133,6 +134,11 @@
     if(m_scene) m_scene->setPathType(pathType);
 }
 
+void DrawMapWidget::setBrushSize(int brushSize)
+{
+    if(m_scene) m_scene->setBrushSize(brushSize);
+}
+
 void DrawMapWidget::save(const QString & fileName)
 {
     if(m_scene)
@@ -179,7 +185,10 @@
     ui->lblPoints->setNum(m_scene->pointsCount());
 }
 
-
+void DrawMapWidget::brushSizeChanged_slot(int brushSize)
+{
+    emit brushSizeChanged(brushSize);
+}
 
 DrawMapView::DrawMapView(QWidget *parent) :
     QGraphicsView(parent)
--- a/QTfrontend/ui/widget/drawmapwidget.h	Wed Mar 14 21:46:07 2018 +0100
+++ b/QTfrontend/ui/widget/drawmapwidget.h	Wed Mar 14 22:59:06 2018 +0100
@@ -108,6 +108,10 @@
         void save(const QString & fileName);
         void load(const QString & fileName);
         void setPathType(DrawMapScene::PathType pathType);
+        void setBrushSize(int brushSize);
+
+    signals:
+        void brushSizeChanged(int brushSize);
 
     protected:
         void changeEvent(QEvent *e);
@@ -121,6 +125,7 @@
 
     private slots:
         void pathChanged();
+        void brushSizeChanged_slot(int brushSize);
 };
 
 #endif // DRAWMAPWIDGET_H