Skip to content

Commit a1286f4

Browse files
author
jef
committedNov 29, 2007
- fixes #839
- (unused) QgsGeometryVertexIndex::empty() added - debug output now using QgsDebugMsg and indentation updates git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7678 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

3 files changed

+132
-180
lines changed

3 files changed

+132
-180
lines changed
 

‎src/app/qgsmaptoolvertexedit.cpp

Lines changed: 119 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
***************************************************************************/
1515
/* $Id$ */
1616

17+
#include "qgslogger.h"
1718
#include "qgsmaptoolvertexedit.h"
1819
#include "qgsmapcanvas.h"
1920
#include "qgsmaprender.h"
@@ -49,11 +50,11 @@ void QgsMapToolVertexEdit::canvasMoveEvent(QMouseEvent * e)
4950
{
5051
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
5152
if(!vlayer)
52-
{
53-
return;
54-
}
53+
{
54+
return;
55+
}
5556

56-
if (e->buttons() == Qt::LeftButton && (mTool == AddVertex || mTool == MoveVertex))
57+
if (e->buttons() == Qt::LeftButton && (mTool == AddVertex || mTool == MoveVertex) && mRubberBand)
5758
{
5859
//int index = (mStartPointValid ? 1 : 0);
5960
int index;
@@ -89,75 +90,60 @@ void QgsMapToolVertexEdit::canvasMoveEvent(QMouseEvent * e)
8990
//transform snapped point back into map coordinates for the rubberband
9091
QgsPoint mapPoint = toMapCoords(vlayer, layerPoint);
9192

92-
if (mRubberBand)
93-
{
94-
mRubberBand->movePoint(index, mapPoint);
95-
}
96-
else
97-
{
98-
#ifdef QGISDEBUG
99-
std::cout << "QgsMapToolVertexEdit::canvasMoveEvent: mRubberBand is empty when it ought not to be!" << std::endl;
100-
#endif
101-
}
102-
93+
mRubberBand->movePoint(index, mapPoint);
10394
}
104-
10595
}
10696

10797

10898
void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
10999
{
110100
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
111101
if(!vlayer)
112-
{
113-
return;
114-
}
102+
{
103+
return;
104+
}
115105

116106
QgsPoint layerPoint = toLayerCoords(vlayer, e->pos());
117-
107+
118108
QgsGeometryVertexIndex index, rb1Index, rb2Index; //rb1Index/rb2Index is for rubberbanding
119-
109+
120110
if (mTool == AddVertex)
121111
{
122112
// Find the closest line segment to the mouse position
123113
// Then set up the rubber band to its endpoints
124114

125-
#ifdef QGISDEBUG
126-
std::cout << "QgsMapCanvas::mousePressEvent: QGis::AddVertex." << std::endl;
127-
#endif
128-
129-
//Find nearest segment of the selected line, move that node to the mouse location
130-
if (!snapSegmentWithContext(layerPoint))
131-
{
132-
displaySnapToleranceWarning();
133-
return;
134-
}
135-
136-
index = mSnappedBeforeVertex;
137-
// Get the endpoint of the snapped-to segment
138-
QgsPoint layerPnt2 = mSnappedAtGeometry.vertexAt(index);
139-
140-
// Get the startpoint of the snapped-to segment
141-
index.decrement_back();
142-
QgsPoint layerPnt1 = mSnappedAtGeometry.vertexAt(index);
143-
144-
createRubberBand();
145-
146-
if (layerPnt1 != QgsPoint(0,0))
147-
{
148-
QgsPoint mapPnt1 = toMapCoords(vlayer, layerPnt1);
149-
mRubberBand->addPoint(mapPnt1);
150-
}
151-
QgsPoint mapPoint = toMapCoords(vlayer, layerPoint);
152-
mRubberBand->addPoint(mapPoint);
153-
QgsPoint mapPnt2 = toMapCoords(vlayer, layerPnt2);
154-
mRubberBand->addPoint(mapPnt2);
115+
QgsDebugMsg("QGis::AddVertex.");
116+
117+
//Find nearest segment of the selected line, move that node to the mouse location
118+
if ( !snapSegmentWithContext(layerPoint) )
119+
{
120+
displaySnapToleranceWarning();
121+
return;
122+
}
123+
124+
index = mSnappedBeforeVertex;
125+
// Get the endpoint of the snapped-to segment
126+
QgsPoint layerPnt2 = mSnappedAtGeometry.vertexAt(index);
127+
128+
// Get the startpoint of the snapped-to segment
129+
index.decrement_back();
130+
QgsPoint layerPnt1 = mSnappedAtGeometry.vertexAt(index);
131+
132+
createRubberBand();
133+
134+
if (layerPnt1 != QgsPoint(0,0))
135+
{
136+
QgsPoint mapPnt1 = toMapCoords(vlayer, layerPnt1);
137+
mRubberBand->addPoint(mapPnt1);
138+
}
139+
QgsPoint mapPoint = toMapCoords(vlayer, layerPoint);
140+
mRubberBand->addPoint(mapPoint);
141+
QgsPoint mapPnt2 = toMapCoords(vlayer, layerPnt2);
142+
mRubberBand->addPoint(mapPnt2);
155143
}
156144
else if (mTool == MoveVertex)
157145
{
158-
#ifdef QGISDEBUG
159-
std::cout << "QgsMapCanvas::mousePressEvent: QGis::MoveVertex." << std::endl;
160-
#endif
146+
QgsDebugMsg("QGis::MoveVertex.");
161147

162148
// Find the closest line segment to the mouse position
163149
// Then find the closest vertex on that line segment
@@ -167,88 +153,82 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
167153

168154
snapPoint = layerPoint;
169155
if(vlayer->vectorType() == QGis::Point)//snap to point for point/multipoint layers
156+
{
157+
if(!snapVertexWithContext(snapPoint))
170158
{
171-
if(!snapVertexWithContext(snapPoint))
172-
{
173-
displaySnapToleranceWarning();
174-
return;
175-
}
159+
displaySnapToleranceWarning();
160+
return;
176161
}
162+
}
177163
else //snap to segment and take the closest vertex in case of line/multiline/polygon/multipolygon layers
164+
{
165+
if (!snapSegmentWithContext(snapPoint))
166+
{
167+
displaySnapToleranceWarning();
168+
return;
169+
}
170+
171+
if (!snapVertexOfSnappedSegment(snapPoint))
172+
{
173+
QMessageBox::warning(0, QObject::tr("Error"),
174+
QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"));
175+
return;
176+
}
177+
178+
QgsDebugMsg("Creating rubber band for moveVertex");
179+
180+
index = mSnappedAtVertex;
181+
createRubberBand();
182+
if(mRubberBandIndex1 != -1)
178183
{
179-
if (!snapSegmentWithContext(snapPoint))
180-
{
181-
displaySnapToleranceWarning();
182-
return;
183-
}
184-
185-
if (!snapVertexOfSnappedSegment(snapPoint))
186-
{
187-
QMessageBox::warning(0, QObject::tr("Error"),
188-
QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"));
189-
return;
190-
}
191-
192-
#ifdef QGISDEBUG
193-
qWarning("Creating rubber band for moveVertex");
194-
#endif
195-
196-
index = mSnappedAtVertex;
197-
createRubberBand();
198-
if(mRubberBandIndex1 != -1)
199-
{
200-
rb1Index.push_back(mRubberBandIndex1);
201-
QgsPoint layerPnt1 = mSnappedAtGeometry.vertexAt(rb1Index);
202-
QgsPoint mapPnt1 = toMapCoords(vlayer, layerPnt1);
203-
mRubberBand->addPoint(mapPnt1);
204-
mStartPointValid = true;
205-
}
206-
else
207-
{
208-
mStartPointValid = false;
209-
}
210-
if(mRubberBandIndex1 != -1 && mRubberBandIndex2 != -1)
211-
{
212-
QgsPoint mapPoint = toMapCoords(vlayer, layerPoint);
213-
mRubberBand->addPoint(mapPoint);
214-
}
215-
if(mRubberBandIndex2 != -1)
216-
{
217-
rb2Index.push_back(mRubberBandIndex2);
218-
QgsPoint layerPnt2 = mSnappedAtGeometry.vertexAt(rb2Index);
219-
QgsPoint mapPnt2 = toMapCoords(vlayer, layerPnt2);
220-
mRubberBand->addPoint(mapPnt2);
221-
}
222-
#ifdef QGISDEBUG
223-
qWarning("Creating rubber band for moveVertex");
224-
#endif
184+
rb1Index.push_back(mRubberBandIndex1);
185+
QgsPoint layerPnt1 = mSnappedAtGeometry.vertexAt(rb1Index);
186+
QgsPoint mapPnt1 = toMapCoords(vlayer, layerPnt1);
187+
mRubberBand->addPoint(mapPnt1);
188+
mStartPointValid = true;
225189
}
190+
else
191+
{
192+
mStartPointValid = false;
193+
}
194+
if(mRubberBandIndex1 != -1 && mRubberBandIndex2 != -1)
195+
{
196+
QgsPoint mapPoint = toMapCoords(vlayer, layerPoint);
197+
mRubberBand->addPoint(mapPoint);
198+
}
199+
if(mRubberBandIndex2 != -1)
200+
{
201+
rb2Index.push_back(mRubberBandIndex2);
202+
QgsPoint layerPnt2 = mSnappedAtGeometry.vertexAt(rb2Index);
203+
QgsPoint mapPnt2 = toMapCoords(vlayer, layerPnt2);
204+
mRubberBand->addPoint(mapPnt2);
205+
}
206+
QgsDebugMsg("Creating rubber band for moveVertex");
207+
}
226208
}
227209
else if (mTool == DeleteVertex)
228210
{
229-
#ifdef QGISDEBUG
230-
std::cout << "QgsMapCanvas::mousePressEvent: QGis::DeleteVertex." << std::endl;
231-
#endif
211+
QgsDebugMsg("QGis::DeleteVertex.");
232212

233213
// TODO: Find nearest node of the selected line, show a big X symbol
234-
214+
235215
// TODO: Find nearest segment of the selected line, move that node to the mouse location
236216
if (!snapVertexWithContext(layerPoint))
237-
{
238-
QMessageBox::warning(0, QObject::tr("Error"),
239-
QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"));
240-
return;
241-
}
242-
217+
{
218+
QMessageBox::warning(0, QObject::tr("Error"),
219+
QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"));
220+
return;
221+
}
222+
243223
// Get the point of the snapped-to vertex
244224
QgsPoint layerPnt = mSnappedAtGeometry.vertexAt(mSnappedAtVertex);
245225
QgsPoint mapPnt = toMapCoords(vlayer, layerPnt);
246-
226+
247227
mCross = new QgsVertexMarker(mCanvas);
248228
mCross->setIconType(QgsVertexMarker::ICON_X);
249229
mCross->setCenter(mapPnt);
250230
}
251-
231+
252232
}
253233

254234
double QgsMapToolVertexEdit::tolerance()
@@ -299,9 +279,7 @@ bool QgsMapToolVertexEdit::snapSegmentWithContext(QgsPoint& point)
299279
}
300280
else
301281
{
302-
#ifdef QGISDEBUG
303-
std::cout << "QgsMapToolVertexEdit::snapSegmentWithContext: Snapped to segment fid " << atFeatureId << "." << std::endl;
304-
#endif
282+
QgsDebugMsg("Snapped to segment fid " + QString::number(atFeatureId) + ".");
305283

306284
// Save where we snapped to
307285
mSnappedBeforeVertex = beforeVertex;
@@ -333,9 +311,7 @@ bool QgsMapToolVertexEdit::snapVertexWithContext(QgsPoint& point)
333311
}
334312
else
335313
{
336-
#ifdef QGISDEBUG
337-
std::cout << "QgsMapToolVertexEdit: Snapped to segment fid " << atFeatureId << "." << std::endl;
338-
#endif
314+
QgsDebugMsg("Snapped to segment fid " + QString::number(atFeatureId) + ".");
339315

340316
// Save where we snapped to
341317
mSnappedAtVertex = atVertex;
@@ -354,22 +330,14 @@ bool QgsMapToolVertexEdit::snapVertexOfSnappedSegment(QgsPoint& point)
354330
QgsGeometryVertexIndex snappedTwoBeforeVertex(mSnappedBeforeVertex);
355331
snappedTwoBeforeVertex.decrement_back();
356332

357-
#ifdef QGISDEBUG
358-
std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Choice of "
359-
<< snappedTwoBeforeVertex.toString().toLocal8Bit().data() << " or "
360-
<< mSnappedBeforeVertex.toString().toLocal8Bit().data() << "." << std::endl;
361-
#endif
362-
333+
QgsDebugMsg( "Choice of " + snappedTwoBeforeVertex.toString()
334+
+ " or " + mSnappedBeforeVertex.toString() + ".");
363335

364336
twoBeforeVertexSqrDist = mSnappedAtGeometry.sqrDistToVertexAt(point, snappedTwoBeforeVertex);
365337
beforeVertexSqrDist = mSnappedAtGeometry.sqrDistToVertexAt(point, mSnappedBeforeVertex);
366338

367-
#ifdef QGISDEBUG
368-
std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Choice of "
369-
<< twoBeforeVertexSqrDist << " or "
370-
<< beforeVertexSqrDist << "." << std::endl;
371-
#endif
372-
339+
QgsDebugMsg( "Choice of " + QString::number(twoBeforeVertexSqrDist)
340+
+ " or " + QString::number(beforeVertexSqrDist) + ".");
373341

374342
// See which of the two verticies is closer (i.e. smaller squared distance)
375343
if (twoBeforeVertexSqrDist < beforeVertexSqrDist)
@@ -381,10 +349,7 @@ bool QgsMapToolVertexEdit::snapVertexOfSnappedSegment(QgsPoint& point)
381349
mSnappedAtVertex = mSnappedBeforeVertex;
382350
}
383351

384-
#ifdef QGISDEBUG
385-
std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Chose "
386-
<< mSnappedAtVertex.toString().toLocal8Bit().data() << "." << std::endl;
387-
#endif
352+
QgsDebugMsg("Chose " + mSnappedAtVertex.toString() + ".");
388353

389354
// Now determine the rubber band verticies to use with this snapped vertex
390355
mSnappedAtGeometry.adjacentVerticies(mSnappedAtVertex, mRubberBandIndex1, mRubberBandIndex2);
@@ -396,19 +361,19 @@ void QgsMapToolVertexEdit::snapVertex(QgsPoint& point, int exclFeatureId, int ex
396361
{
397362
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
398363
if(vlayer)
364+
{
365+
QgsGeometryVertexIndex vIndex;
366+
int snappedFeatureId;
367+
int rbPoint1, rbPoint2;
368+
QgsGeometry snappedGeometry;
369+
QgsPoint cpyPoint = point;
370+
vlayer->snapVertexWithContext(cpyPoint, vIndex, rbPoint1, rbPoint2, snappedFeatureId, snappedGeometry, tolerance());
371+
if(snappedFeatureId != exclFeatureId || vIndex.back() != exclVertexNr)
399372
{
400-
QgsGeometryVertexIndex vIndex;
401-
int snappedFeatureId;
402-
int rbPoint1, rbPoint2;
403-
QgsGeometry snappedGeometry;
404-
QgsPoint cpyPoint = point;
405-
vlayer->snapVertexWithContext(cpyPoint, vIndex, rbPoint1, rbPoint2, snappedFeatureId, snappedGeometry, tolerance());
406-
if(snappedFeatureId != exclFeatureId || vIndex.back() != exclVertexNr)
407-
{
408-
//success, assign snapped coordinates to point
409-
point = cpyPoint;
410-
}
373+
//success, assign snapped coordinates to point
374+
point = cpyPoint;
411375
}
376+
}
412377
}
413378

414379

@@ -450,9 +415,7 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
450415
//snap to nearest vertex of vectorlayer
451416
snapVertex(layerPoint, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
452417

453-
#ifdef QGISDEBUG
454-
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: AddVertex." << std::endl;
455-
#endif
418+
QgsDebugMsg("AddVertex.");
456419

457420
deleteRubberBand();
458421

@@ -464,9 +427,8 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
464427
{
465428
//snap to nearest vertex of vectorlayer
466429
snapVertex(layerPoint, mSnappedAtFeatureId, mSnappedAtVertex.back());
467-
#ifdef QGISDEBUG
468-
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: MoveVertex." << std::endl;
469-
#endif
430+
431+
QgsDebugMsg("MoveVertex.");
470432

471433
delete mRubberBand;
472434
mRubberBand = 0;
@@ -475,9 +437,7 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
475437
}
476438
else if (mTool == DeleteVertex)
477439
{
478-
#ifdef QGISDEBUG
479-
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: DeleteVertex." << std::endl;
480-
#endif
440+
QgsDebugMsg("DeleteVertex.");
481441

482442
delete mCross;
483443
mCross = 0;

‎src/core/qgsgeometryvertexindex.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ QgsGeometryVertexIndex & QgsGeometryVertexIndex::operator=( QgsGeometryVertexInd
4141
QgsGeometryVertexIndex::~QgsGeometryVertexIndex()
4242
{
4343
// NOOP
44-
}
44+
}
45+
46+
bool QgsGeometryVertexIndex::empty() const
47+
{
48+
return mIndex.empty();
49+
}
4550

4651
void QgsGeometryVertexIndex::push_back(int& i)
4752
{
@@ -50,30 +55,14 @@ void QgsGeometryVertexIndex::push_back(int& i)
5055

5156
int QgsGeometryVertexIndex::back() const
5257
{
53-
#ifdef QGISDEBUG
54-
QString str;
55-
for(std::vector<int>::const_iterator it = mIndex.begin(); it != mIndex.end(); ++it)
56-
{
57-
str += " ";
58-
str += QString::number(*it);
59-
}
60-
QgsDebugMsg("mIndex contains:" + str + ".");
61-
#endif
58+
QgsDebugMsg("mIndex contains: " + toString() );
6259
return mIndex.back();
6360
}
6461

6562

6663
int QgsGeometryVertexIndex::get_at(int i) const
6764
{
68-
#ifdef QGISDEBUG
69-
QString str;
70-
for(std::vector<int>::const_iterator it = mIndex.begin(); it != mIndex.end(); ++it)
71-
{
72-
str += " ";
73-
str += QString::number(*it);
74-
}
75-
QgsDebugMsg("mIndex contains:" + str + ".");
76-
#endif
65+
QgsDebugMsg("mIndex contains: " + toString() );
7766
return mIndex[i];
7867
}
7968

@@ -101,7 +90,7 @@ void QgsGeometryVertexIndex::assign_back(int& i)
10190
n = i;
10291
}
10392

104-
QString QgsGeometryVertexIndex::toString()
93+
QString QgsGeometryVertexIndex::toString() const
10594
{
10695
QString s;
10796

‎src/core/qgsgeometryvertexindex.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class CORE_EXPORT QgsGeometryVertexIndex {
5959
*/
6060
int get_at(int i) const;
6161

62+
/** no elements */
63+
bool empty() const;
64+
6265
/** Resets the index */
6366
void clear();
6467

@@ -72,7 +75,7 @@ class CORE_EXPORT QgsGeometryVertexIndex {
7275
void assign_back(int& i);
7376

7477
/** Returns this index as a string - useful for "printf debugging" */
75-
QString toString();
78+
QString toString() const;
7679

7780

7881
private:

0 commit comments

Comments
 (0)
Please sign in to comment.