1 /**********************************************************************************************
2     Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3     Copyright (C) 2018 Norbert Truchsess <norbert.truchsess@t-online.de>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 **********************************************************************************************/
19 
20 #ifndef IMOUSEEDITLINE_H
21 #define IMOUSEEDITLINE_H
22 
23 #include "gis/IGisItem.h"
24 #include "gis/IGisLine.h"
25 #include "gis/rte/router/CRouterOptimization.h"
26 #include "mouse/IMouse.h"
27 #include <QDebug>
28 #include <QPointer>
29 #include <QPolygonF>
30 
31 class CGisDraw;
32 class CCanvas;
33 class IGisLine;
34 class CScrOptEditLine;
35 class ILineOp;
36 
37 class IMouseEditLine : public IMouse
38 {
39     Q_OBJECT
40 public:
41     enum features_e
42     {
43         eFeatureSnapToLines = 0x01
44         , eFeatureRouting   = 0x02
45     };
46 
47     /**
48        @brief Start to create a new track with given point as first track point
49        @param point     the starting point
50        @param gis       the draw context to use
51        @param parent    the canvas to use
52      */
53     IMouseEditLine(const IGisItem::key_t& key, const QPointF& point, bool enableStatus, const QString& type, CGisDraw* gis, CCanvas* canvas, CMouseAdapter* mouse);
54     /**
55        @brief Edit an existing track
56        @param trk       the track to edit
57        @param gis       the draw context to use
58        @param parent    the canvas to use
59      */
60     IMouseEditLine(const IGisItem::key_t& key, IGisLine& src, bool enableStatus, const QString& type, CGisDraw* gis, CCanvas* canvas, CMouseAdapter* mouse);
61     virtual ~IMouseEditLine();
62 
63     void draw(QPainter& p, CCanvas::redraw_e needsRedraw, const QRect& rect) override;
64 
65     void leftClicked(const QPoint& pos) override;
66     void mouseMoved(const QPoint& pos) override;
67     void leftButtonDown(const QPoint& pos) override;
68     void rightButtonDown(const QPoint& pos) override;
69 
70     void scaleChanged() override;
71 
72     void abortStep() override;
73 
74     bool useAutoRouting() const;
75     bool useVectorRouting() const;
76     bool useTrackRouting() const;
77 
78     void storeToHistory(const SGisLine& line);
79     void restoreFromHistory(SGisLine& line);
80 
81     virtual void updateStatus();
82 
83 protected slots:
84     /**
85        @brief Delete the selected point
86      */
87     void slotDeletePoint();
88     /**
89        @brief Start to select a range of points
90      */
91     void slotSelectRange();
92     /**
93        @brief Move selected point
94      */
95     void slotMovePoint();
96     /**
97        @brief Add points in direction start of track (eStateAddPointBwd)
98      */
99     void slotAddPoint();
100 
101     void slotNoRouting();
102     void slotAutoRouting();
103     void slotVectorRouting();
104     void slotTrackRouting();
105 
106     void slotOptimize();
107 
108     virtual void slotAbort() = 0;
109     void slotAbortEx(bool showMB);
110     virtual void slotCopyToOrig();
111     virtual void slotCopyToNew() = 0;
112 
113     void slotUndo();
114     void slotRedo();
115 
116 protected:
117     virtual void drawLine(const QPolygonF& l, const QColor color, int width, QPainter& p);
118     /**
119        @brief Get access to the IGisLine object a subclass of IMouseEditLine is handling.
120        @return A valid pointer or 0.
121      */
122     virtual IGisLine* getGisLine() const = 0;
123 
124     virtual void startNewLine(const QPointF& point);
125 
126     /// shadow cursor needed to restore cursor after some actions providing their own cursor.
127     QCursor cursor1;
128 
129     /// the abstract line object to edit
130     SGisLine points;
131 
132     /// undo/redo history
133     QList<SGisLine> history;
134     qint32 idxHistory = NOIDX;
135 
136     /// the on screen buttons
137     CScrOptEditLine* scrOptEditLine;
138 
139     /// the key of the GIS item to edit
140     IGisItem::key_t key;
141 
142     QString docPanning = tr("<br/><b>Move the map</b><br/>If you keep the left mouse button pressed and move the mouse, you will move the map.<br/><br/>");
143 
144 private:
145     void commonSetup();
146     void changeCursor();
147 
148     QPolygonF pixelLine;
149     QPolygonF pixelPts;
150     QPolygonF pixelSubs;
151 
152     /// the current active line operation (move, add, delete...)
153     ILineOp* lineOp = nullptr;
154 
155     bool enableStatus;
156 
157     QString type;
158 
159     CRouterOptimization optimizer;
160 };
161 
162 #endif //IMOUSEEDITLINE_H
163 
164 
165