~ubuntu-branches/ubuntu/utopic/qgis/utopic

« back to all changes in this revision

Viewing changes to src/gui/qgsmaptoolvertexedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
    qgsmaptoolvertexedit.cpp  - tool for adding, moving, deleting vertices
3
 
    ---------------------
4
 
    begin                : January 2006
5
 
    copyright            : (C) 2006 by Martin Dobias
6
 
    email                : wonder.sk at gmail 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
 
/* $Id$ */
16
 
 
17
 
#include "qgsmaptoolvertexedit.h"
18
 
#include "qgsmapcanvas.h"
19
 
#include "qgsvertexmarker.h"
20
 
#include "qgsrubberband.h"
21
 
#include "qgsvectorlayer.h"
22
 
#include "qgsvectordataprovider.h"
23
 
#include "qgsmaptopixel.h"
24
 
#include "qgsproject.h"
25
 
#include "qgscursors.h"
26
 
#include <QMessageBox>
27
 
#include <QPixmap>
28
 
#include <QCursor>
29
 
 
30
 
 
31
 
QgsMapToolVertexEdit::QgsMapToolVertexEdit(QgsMapCanvas* canvas, enum Tool tool)
32
 
  : QgsMapTool(canvas), mTool(tool), mRubberBandIndex1(-1), mRubberBandIndex2(-1), mRubberBand(0)
33
 
{
34
 
  // TODO - select a real cursor
35
 
  QPixmap mySelectQPixmap = QPixmap((const char **) capture_point_cursor);
36
 
  mCursor = QCursor(mySelectQPixmap, 8, 8);
37
 
}
38
 
 
39
 
QgsMapToolVertexEdit::~QgsMapToolVertexEdit()
40
 
{
41
 
  delete mRubberBand;
42
 
  mRubberBand = 0;
43
 
}
44
 
 
45
 
 
46
 
void QgsMapToolVertexEdit::canvasMoveEvent(QMouseEvent * e)
47
 
{
48
 
  if (e->buttons() == Qt::LeftButton && (mTool == AddVertex || mTool == MoveVertex))
49
 
  {
50
 
    //int index = (mStartPointValid ? 1 : 0);
51
 
    int index;
52
 
 
53
 
    if(mTool == MoveVertex)
54
 
    {
55
 
      if(mRubberBandIndex1 == -1)
56
 
      {
57
 
        index = 0;
58
 
      }
59
 
      else
60
 
      {
61
 
        index = 1;
62
 
      }
63
 
    }
64
 
    else
65
 
    {
66
 
      index = 1;
67
 
    }
68
 
 
69
 
    QgsPoint rbpoint = toMapCoords(e->pos());
70
 
 
71
 
    //snap to nearest vertex of vectorlayer
72
 
    if (mTool == AddVertex)
73
 
    {
74
 
      snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
75
 
    }
76
 
    else if (mTool == MoveVertex)
77
 
    {
78
 
      snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedAtVertex.back());
79
 
    }
80
 
 
81
 
    if (mRubberBand)
82
 
    {
83
 
      mRubberBand->movePoint(index, rbpoint);
84
 
    }
85
 
    else
86
 
    {
87
 
#ifdef QGISDEBUG
88
 
        std::cout << "QgsMapToolVertexEdit::canvasMoveEvent: mRubberBand is empty when it ought not to be!" << std::endl;
89
 
#endif
90
 
    }
91
 
 
92
 
  }
93
 
 
94
 
}
95
 
 
96
 
 
97
 
void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
98
 
{
99
 
  QgsPoint point = toMapCoords(e->pos());
100
 
  
101
 
  double x1, y1;
102
 
  double x2, y2;
103
 
  QgsGeometryVertexIndex index, rb1Index, rb2Index; //rb1Index/rb2Index is for rubberbanding
104
 
  
105
 
  if (mTool == AddVertex)
106
 
  {
107
 
    // Find the closest line segment to the mouse position
108
 
    // Then set up the rubber band to its endpoints
109
 
 
110
 
#ifdef QGISDEBUG
111
 
        std::cout << "QgsMapCanvas::mousePressEvent: QGis::AddVertex." << std::endl;
112
 
#endif
113
 
        
114
 
        //Find nearest segment of the selected line, move that node to the mouse location
115
 
        if (!snapSegmentWithContext(point))
116
 
          {
117
 
            QMessageBox::warning(0, QObject::tr("Error"), 
118
 
                                 QObject::tr("Could not snap segment. Have you set the tolerance in Settings > Project Properties > General?"),
119
 
                                 QMessageBox::Ok, Qt::NoButton);
120
 
            return;
121
 
          }
122
 
        
123
 
        index = mSnappedBeforeVertex;
124
 
        // Get the endpoint of the snapped-to segment
125
 
        mSnappedAtGeometry.vertexAt(x2, y2, index);
126
 
        
127
 
        // Get the startpoint of the snapped-to segment
128
 
        index.decrement_back();
129
 
        mStartPointValid = mSnappedAtGeometry.vertexAt(x1, y1, index);
130
 
        
131
 
        createRubberBand();
132
 
        
133
 
        if (mStartPointValid)
134
 
          {
135
 
            mRubberBand->addPoint(QgsPoint(x1,y1));
136
 
          }
137
 
        mRubberBand->addPoint(toMapCoords(e->pos()));
138
 
        mRubberBand->addPoint(QgsPoint(x2,y2));
139
 
  }
140
 
  else if (mTool == MoveVertex)
141
 
  {
142
 
#ifdef QGISDEBUG
143
 
    std::cout << "QgsMapCanvas::mousePressEvent: QGis::MoveVertex." << std::endl;
144
 
#endif
145
 
 
146
 
    // Find the closest line segment to the mouse position
147
 
    // Then find the closest vertex on that line segment
148
 
    // Then set up the rubber band to its adjoining vertexes
149
 
 
150
 
    QgsPoint snapPoint;
151
 
 
152
 
    snapPoint = point;
153
 
    QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
154
 
    if(!vlayer)
155
 
    {
156
 
            return;
157
 
    }
158
 
    if(vlayer->vectorType() == QGis::Point)//snap to point for point/multipoint layers
159
 
      {
160
 
        if(!snapVertexWithContext(snapPoint))
161
 
          {
162
 
            QMessageBox::warning(0, QObject::tr("Error"), 
163
 
                                 QObject::tr("Could not snap segment. Have you set the tolerance in Settings > Project Properties > General?"),
164
 
                                 QMessageBox::Ok, Qt::NoButton);
165
 
            return;
166
 
          }
167
 
      }
168
 
    else //snap to segment and take the closest vertex in case of line/multiline/polygon/multipolygon layers
169
 
      {
170
 
        if (!snapSegmentWithContext(snapPoint))
171
 
          {
172
 
            QMessageBox::warning(0, QObject::tr("Error"), 
173
 
                                 QObject::tr("Could not snap segment. Have you set the tolerance in Settings > Project Properties > General?"),
174
 
                                 QMessageBox::Ok, Qt::NoButton);
175
 
            return;
176
 
          }
177
 
        
178
 
        snapPoint = point;
179
 
        if (!snapVertexOfSnappedSegment(snapPoint))
180
 
          {
181
 
            QMessageBox::warning(0, QObject::tr("Error"), 
182
 
                                 QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"),
183
 
                                 QMessageBox::Ok, Qt::NoButton);
184
 
            return;
185
 
          }
186
 
        
187
 
#ifdef QGISDEBUG
188
 
        qWarning("Creating rubber band for moveVertex");
189
 
#endif
190
 
        
191
 
        index = mSnappedAtVertex;
192
 
        createRubberBand();
193
 
        if(mRubberBandIndex1 != -1)
194
 
          {
195
 
            rb1Index.push_back(mRubberBandIndex1);
196
 
            mSnappedAtGeometry.vertexAt(x1, y1, rb1Index);
197
 
            mRubberBand->addPoint(QgsPoint(x1,y1));
198
 
            mStartPointValid = true;
199
 
          }
200
 
        else
201
 
          {
202
 
            mStartPointValid = false;
203
 
          }
204
 
        if(mRubberBandIndex1 != -1 && mRubberBandIndex2 != -1)
205
 
          {
206
 
            mRubberBand->addPoint(toMapCoords(e->pos()));
207
 
          }
208
 
        if(mRubberBandIndex2 != -1)
209
 
          {
210
 
            rb2Index.push_back(mRubberBandIndex2);
211
 
            mSnappedAtGeometry.vertexAt(x2, y2, rb2Index);
212
 
            mRubberBand->addPoint(QgsPoint(x2,y2));
213
 
          }
214
 
#ifdef QGISDEBUG
215
 
    qWarning("Creating rubber band for moveVertex");
216
 
#endif    
217
 
      }
218
 
  }
219
 
  else if (mTool == DeleteVertex)
220
 
  {
221
 
#ifdef QGISDEBUG
222
 
    std::cout << "QgsMapCanvas::mousePressEvent: QGis::DeleteVertex." << std::endl;
223
 
#endif
224
 
 
225
 
    // TODO: Find nearest node of the selected line, show a big X symbol
226
 
  
227
 
    // TODO: Find nearest segment of the selected line, move that node to the mouse location
228
 
    if (!snapVertexWithContext(point))
229
 
      {
230
 
        QMessageBox::warning(0, QObject::tr("Error"), 
231
 
          QObject::tr("Could not snap vertex. Have you set the tolerance in Settings > Project Properties > General?"),
232
 
          QMessageBox::Ok, Qt::NoButton);
233
 
        return;
234
 
      }
235
 
      
236
 
    // Get the point of the snapped-to vertex
237
 
    mSnappedAtGeometry.vertexAt(x1, y1, mSnappedAtVertex);
238
 
    
239
 
    mCross = new QgsVertexMarker(mCanvas);
240
 
    mCross->setIconType(QgsVertexMarker::ICON_X);
241
 
    mCross->setCenter(QgsPoint(x1,y1));
242
 
  }
243
 
  
244
 
}
245
 
 
246
 
double QgsMapToolVertexEdit::tolerance()
247
 
{
248
 
  // TODO: tolerance should be in screen pixels
249
 
  // here it can be computed for map coordinates depending on zoom
250
 
  return QgsProject::instance()->readDoubleEntry("Digitizing","/Tolerance",0);
251
 
}
252
 
 
253
 
 
254
 
bool QgsMapToolVertexEdit::snapSegmentWithContext(QgsPoint& point)
255
 
{
256
 
  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
257
 
          
258
 
  QgsGeometryVertexIndex beforeVertex;
259
 
  int atFeatureId;
260
 
  QgsGeometry atGeometry;
261
 
  
262
 
  if (!vlayer)
263
 
    return FALSE;
264
 
  
265
 
  // TODO: Find nearest segment of the selected line, move that node to the mouse location
266
 
  if (!vlayer->snapSegmentWithContext(point, beforeVertex, atFeatureId, atGeometry, tolerance()))
267
 
  {
268
 
    mSnappedAtFeatureId = -1;
269
 
    return FALSE;
270
 
  }
271
 
  else
272
 
  {
273
 
#ifdef QGISDEBUG
274
 
      std::cout << "QgsMapToolVertexEdit::snapSegmentWithContext: Snapped to segment fid " << atFeatureId << "." << std::endl;
275
 
#endif
276
 
    
277
 
    // Save where we snapped to
278
 
    mSnappedBeforeVertex = beforeVertex;
279
 
    mSnappedAtFeatureId  = atFeatureId;
280
 
    mSnappedAtGeometry   = atGeometry;
281
 
    return TRUE;
282
 
  }
283
 
 
284
 
}
285
 
 
286
 
 
287
 
bool QgsMapToolVertexEdit::snapVertexWithContext(QgsPoint& point)
288
 
{
289
 
  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
290
 
  
291
 
  QgsGeometryVertexIndex atVertex;
292
 
  int atFeatureId;
293
 
  QgsGeometry atGeometry;
294
 
  
295
 
  if (!vlayer)
296
 
    return FALSE;
297
 
  
298
 
  if (!vlayer->snapVertexWithContext(point, atVertex, mRubberBandIndex1, mRubberBandIndex2, atFeatureId, atGeometry, tolerance()))
299
 
  {
300
 
    mSnappedAtFeatureId = -1;
301
 
    return FALSE;
302
 
  }
303
 
  else
304
 
  {
305
 
#ifdef QGISDEBUG
306
 
      std::cout << "QgsMapToolVertexEdit: Snapped to segment fid " << atFeatureId << "." << std::endl;
307
 
#endif
308
 
    
309
 
    // Save where we snapped to
310
 
    mSnappedAtVertex     = atVertex;
311
 
    mSnappedAtFeatureId  = atFeatureId;
312
 
    mSnappedAtGeometry   = atGeometry;
313
 
    return TRUE;
314
 
  }
315
 
}
316
 
 
317
 
bool QgsMapToolVertexEdit::snapVertexOfSnappedSegment(QgsPoint& point)
318
 
{
319
 
  double twoBeforeVertexSqrDist;
320
 
  double    beforeVertexSqrDist;
321
 
 
322
 
  // Set up the "other side" of the snapped-to segment
323
 
  QgsGeometryVertexIndex snappedTwoBeforeVertex(mSnappedBeforeVertex);
324
 
  snappedTwoBeforeVertex.decrement_back();
325
 
 
326
 
#ifdef QGISDEBUG
327
 
  std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Choice of "
328
 
            << snappedTwoBeforeVertex.toString().toLocal8Bit().data() << " or " 
329
 
            << mSnappedBeforeVertex.toString().toLocal8Bit().data() << "." << std::endl;
330
 
#endif
331
 
 
332
 
 
333
 
  twoBeforeVertexSqrDist = mSnappedAtGeometry.sqrDistToVertexAt(point, snappedTwoBeforeVertex);
334
 
  beforeVertexSqrDist    = mSnappedAtGeometry.sqrDistToVertexAt(point, mSnappedBeforeVertex);
335
 
 
336
 
#ifdef QGISDEBUG
337
 
  std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Choice of "
338
 
            << twoBeforeVertexSqrDist << " or " 
339
 
            << beforeVertexSqrDist << "." << std::endl;
340
 
#endif
341
 
 
342
 
 
343
 
  // See which of the two verticies is closer (i.e. smaller squared distance)
344
 
  if (twoBeforeVertexSqrDist < beforeVertexSqrDist)
345
 
  {
346
 
    mSnappedAtVertex = snappedTwoBeforeVertex;
347
 
  }
348
 
  else
349
 
  {
350
 
    mSnappedAtVertex = mSnappedBeforeVertex;
351
 
  }
352
 
 
353
 
#ifdef QGISDEBUG
354
 
  std::cout << "QgsMapToolVertexEdit::snapVertexOfSnappedSegment: Chose "
355
 
            << mSnappedAtVertex.toString().toLocal8Bit().data() << "." << std::endl;
356
 
#endif
357
 
 
358
 
  // Now determine the rubber band verticies to use with this snapped vertex
359
 
  mSnappedAtGeometry.adjacentVerticies(mSnappedAtVertex, mRubberBandIndex1, mRubberBandIndex2);
360
 
 
361
 
  return TRUE;
362
 
}
363
 
 
364
 
bool QgsMapToolVertexEdit::snapVertex(QgsPoint& point, int exclFeatureId, int exclVertexNr)
365
 
{
366
 
  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
367
 
  if(vlayer)
368
 
    {
369
 
      QgsGeometryVertexIndex vIndex;
370
 
      int snappedFeatureId;
371
 
      int rbPoint1, rbPoint2;
372
 
      QgsGeometry snappedGeometry;
373
 
      //do the snapping to a copy point first
374
 
      QgsPoint cpyPoint = point;
375
 
      vlayer->snapVertexWithContext(cpyPoint, vIndex, rbPoint1, rbPoint2, snappedFeatureId, snappedGeometry, tolerance());
376
 
      if(snappedFeatureId != exclFeatureId || vIndex.back() != exclVertexNr)
377
 
        {
378
 
          point = cpyPoint;
379
 
        }
380
 
    }
381
 
  // TODO: return type is bool but no logic exists to determine the
382
 
  // appropriate return value. We return true to make it compile on Windows...
383
 
  return true;
384
 
}
385
 
 
386
 
 
387
 
void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
388
 
{
389
 
  if (mSnappedAtFeatureId == -1)
390
 
    {
391
 
      return;
392
 
    }
393
 
  
394
 
  QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
395
 
  
396
 
  if (!vlayer)
397
 
  {
398
 
    QMessageBox::information(0, QObject::tr("Not a vector layer"), QObject::tr("The current layer is not a vector layer"), QMessageBox::Ok);
399
 
    return;
400
 
  }
401
 
  
402
 
  if (!(vlayer->getDataProvider()->capabilities() & QgsVectorDataProvider::ChangeGeometries))
403
 
  {
404
 
    QMessageBox::information(0, QObject::tr("Change geometry"),
405
 
                             QObject::tr("Data provider of the current layer doesn't allow changing geometries"),
406
 
                             QMessageBox::Ok);
407
 
    return;
408
 
  }
409
 
  
410
 
  if (!vlayer->isEditable())
411
 
  {
412
 
    QMessageBox::information(0, QObject::tr("Layer not editable"),
413
 
                              QObject::tr("Cannot edit the vector layer. Use 'Start editing' in the legend item menu"),
414
 
                              QMessageBox::Ok);
415
 
    return;
416
 
  }
417
 
  
418
 
  QgsPoint point = toMapCoords(e->pos());
419
 
  
420
 
  if (mTool == AddVertex)
421
 
  {
422
 
 
423
 
    //snap to nearest vertex of vectorlayer
424
 
    snapVertex(point, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
425
 
 
426
 
#ifdef QGISDEBUG
427
 
    std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: AddVertex." << std::endl;
428
 
#endif
429
 
 
430
 
    deleteRubberBand();
431
 
 
432
 
    // Add the new vertex
433
 
    vlayer->insertVertexBefore(point.x(), point.y(), mSnappedAtFeatureId, mSnappedBeforeVertex);
434
 
    mCanvas->refresh();
435
 
  }
436
 
  else if (mTool == MoveVertex)
437
 
  {
438
 
    //snap to nearest vertex of vectorlayer
439
 
    snapVertex(point, mSnappedAtFeatureId, mSnappedAtVertex.back());
440
 
#ifdef QGISDEBUG
441
 
    std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: MoveVertex." << std::endl;
442
 
#endif
443
 
 
444
 
    delete mRubberBand;
445
 
    mRubberBand = 0;
446
 
    vlayer->moveVertexAt(point.x(), point.y(), mSnappedAtFeatureId, mSnappedAtVertex);
447
 
    mCanvas->refresh();
448
 
  }
449
 
  else if (mTool == DeleteVertex)
450
 
  {
451
 
#ifdef QGISDEBUG
452
 
    std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: DeleteVertex." << std::endl;
453
 
#endif
454
 
 
455
 
    delete mCross;
456
 
    mCross = 0;
457
 
 
458
 
    // delete vertex
459
 
    vlayer->deleteVertexAt(mSnappedAtFeatureId, mSnappedAtVertex);
460
 
    mCanvas->refresh();
461
 
  }
462
 
}
463
 
 
464
 
void QgsMapToolVertexEdit::createRubberBand()
465
 
{
466
 
  mRubberBand = new QgsRubberBand(mCanvas, FALSE);
467
 
  QgsProject* project = QgsProject::instance();
468
 
  QColor color( project->readNumEntry("Digitizing", "/LineColorRedPart", 255),
469
 
                project->readNumEntry("Digitizing", "/LineColorGreenPart", 0),
470
 
                project->readNumEntry("Digitizing", "/LineColorBluePart", 0));
471
 
  mRubberBand->setColor(color);
472
 
  mRubberBand->setWidth(project->readNumEntry("Digitizing", "/LineWidth", 1));
473
 
}
474
 
 
475
 
void QgsMapToolVertexEdit::deleteRubberBand()
476
 
{
477
 
  if (mRubberBand)
478
 
  {
479
 
    delete mRubberBand;
480
 
    mRubberBand = 0;
481
 
 
482
 
    // also remove reference to the indicies to the ends of the rubber band
483
 
    mRubberBandIndex1 = -1;
484
 
    mRubberBandIndex2 = -1;
485
 
  }
486
 
}
487
 
 
488
 
void QgsMapToolVertexEdit::deactivate()
489
 
{
490
 
  deleteRubberBand();
491
 
}
492
 
 
493
 
QgsPoint QgsMapToolVertexEdit::maybeInversePoint(QgsPoint point, const char whenmsg[])
494
 
{
495
 
  QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>(mCanvas->currentLayer());
496
 
  QgsPoint transformedPoint;
497
 
 
498
 
  if( mCanvas->projectionsEnabled() )
499
 
  {
500
 
    // Do reverse transformation before saving. If possible!
501
 
    try
502
 
    {
503
 
      transformedPoint = vlayer->coordinateTransform()->transform(point, QgsCoordinateTransform::INVERSE);
504
 
    }
505
 
    catch(QgsCsException &cse)
506
 
    {
507
 
      //#ifdef QGISDEBUG
508
 
      std::cout << "Caught transform error when " << whenmsg <<"." 
509
 
          << "Setting untransformed values." << std::endl;
510
 
      //#endif  
511
 
      // Transformation failed,. Bail out with original rectangle.
512
 
      return point;
513
 
    }
514
 
    return transformedPoint;
515
 
  }
516
 
  return point;
517
 
}