QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsmaptooleditblanksegments.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptooleditblanksegments.h
3 ---------------------
4 begin : 2025/08/19
5 copyright : (C) 2025 by Julien Cabieces
6 email : julien dot cabieces at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSMAPTOOLEDITBLANKSEGMENTS_H
17#define QGSMAPTOOLEDITBLANKSEGMENTS_H
18
19#define SIP_NO_FILE
20
21#include "qgsfeatureid.h"
22#include "qgslinesymbollayer.h"
23#include "qgsmapcanvasitem.h"
24#include "qgsmaptool.h"
25#include "qgsrubberband.h"
26#include "qgssymbol.h"
27#include "qobjectuniqueptr.h"
28
29class QgsMapToolBlankSegmentRubberBand;
30class QgsVectorLayer;
31class QgsSymbol;
32class QgsSymbolLayer;
33
52{
53 Q_OBJECT
54
55 public:
63 QgsMapToolEditBlankSegmentsBase( QgsMapCanvas *canvas, QgsVectorLayer *layer, QgsLineSymbolLayer *symbolLayer, int blankSegmentFieldIndex );
64
69
70 void canvasMoveEvent( QgsMapMouseEvent *e ) override;
71 void canvasPressEvent( QgsMapMouseEvent *e ) override;
72 void keyPressEvent( QKeyEvent *e ) override;
73
74 void activate() override;
75
76 protected:
77 typedef QList<QList<QPolygonF>> FeaturePoints;
79
80 private:
84 QPair<double, double> startEndDistance() const;
85
89 void updateAttribute();
90
94 void loadFeaturePoints();
95
101 virtual QgsTemplatedLineSymbolLayerBase *createRenderedPointsSymbolLayer( const QgsTemplatedLineSymbolLayerBase *original ) = 0;
102
108 int closestBlankSegmentIndex( const QPointF &point, double &distance ) const;
109
118 QPointF closestPoint( const QPointF &point, double &distance, int &partIndex, int &ringIndex, int &pointIndex ) const;
119
124 void updateStartEndRubberBand();
125
129 void updateHoveredBlankSegment( const QPoint &pos );
130
134 void setCurrentBlankSegment( int currentBlankSegmentIndex );
135
142
146 enum State
147 {
148 SelectFeature,
149 FeatureSelected,
150 BlankSegmentSelected,
151 BlankSegmentModificationStarted,
152 BlankSegmentCreationStarted
153 };
154
155 std::vector<QObjectUniquePtr<QgsBlankSegmentRubberBand>> mBlankSegments;
156 QgsVectorLayer *mLayer = nullptr;
157 std::unique_ptr<QgsSymbol> mSymbol;
158 const QString mSymbolLayerId;
159 QgsTemplatedLineSymbolLayerBase *mSymbolLayer = nullptr;
160
161 int mBlankSegmentsFieldIndex = -1;
162 QgsFeatureId mCurrentFeatureId = FID_NULL;
163 QgsRectangle mExtent;
164 State mState = State::SelectFeature;
165 int mCurrentBlankSegmentIndex = -1;
166 int mHoveredBlankSegmentIndex = -1;
167
168 // currently edited blank segment, start point is the fixed point and end point is the currently
169 // modified one
171 QObjectUniquePtr<QgsRubberBand> mStartRubberBand;
172 QObjectUniquePtr<QgsRubberBand> mEndRubberBand;
173
175};
176
183template<class T>
185{
186 public:
195 : QgsMapToolEditBlankSegmentsBase( canvas, layer, symbolLayer, blankSegmentFieldIndex )
196 {
197 }
198
200 {
201 const T *sl = dynamic_cast<const T *>( originalSl );
202 return sl ? new QgsRenderedPointsSymbolLayer( sl, mPoints ) : nullptr;
203 }
204
205 private:
210 class QgsRenderedPointsSymbolLayer : public T
211 {
212 public:
217 QgsRenderedPointsSymbolLayer( const T *original, FeaturePoints &points )
218 : T( original->rotateSymbols(), original->interval() )
219 , mPoints( points )
220 {
221 original->copyTemplateSymbolProperties( this );
222 }
223
224 void renderPolyline( const QPolygonF &points, QgsSymbolRenderContext &context ) override
225 {
226 const int iPart = context.geometryPartNum() - 1;
227 if ( iPart < 0 || QgsRenderedPointsSymbolLayer::mRingIndex < 0 )
228 return;
229
230 if ( iPart >= mPoints.count() )
231 mPoints.resize( iPart + 1 );
232
233 QVector<QPolygonF> &rings = mPoints[iPart];
234 if ( QgsRenderedPointsSymbolLayer::mRingIndex >= rings.count() )
235 rings.resize( QgsRenderedPointsSymbolLayer::mRingIndex + 1 );
236
237 rings[QgsRenderedPointsSymbolLayer::mRingIndex] = points;
238 }
239
240 private:
241 FeaturePoints &mPoints;
242 };
243};
244
245
246#endif
Keeps a pointer to a QObject and deletes it whenever this object is deleted.
Abstract base class for line symbol layers.
A mouse event which is the result of a user interaction with a QgsMapCanvas.
Rubber band used to draw blank segments on edition.
QgsMapToolEditBlankSegmentsBase(QgsMapCanvas *canvas, QgsVectorLayer *layer, QgsLineSymbolLayer *symbolLayer, int blankSegmentFieldIndex)
Constructor.
~QgsMapToolEditBlankSegmentsBase() override
Destructor.
QList< QList< QPolygonF > > FeaturePoints
QgsMapToolEditBlankSegments(QgsMapCanvas *canvas, QgsVectorLayer *layer, QgsLineSymbolLayer *symbolLayer, int blankSegmentFieldIndex)
Constructor.
QgsTemplatedLineSymbolLayerBase * createRenderedPointsSymbolLayer(const QgsTemplatedLineSymbolLayerBase *originalSl) override
Create symbol layer used to retrieve rendered points from symbol layer The feature will be rendered u...
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
friend class QgsMapCanvas
Definition qgsmaptool.h:380
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
virtual void activate()
called when set as currently active map tool
A rectangle specified with double values.
Abstract base class for symbol layers.
int geometryPartNum() const
Part number of current geometry.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Base class for templated line symbols, e.g.
Represents a vector layer which manages a vector based dataset.
#define FID_NULL
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features