~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/app/qgsmaptooladdring.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
    qgsmaptooladdring.cpp  - map tool to cut rings in polygon and multipolygon features
 
3
    ---------------------
 
4
    begin                : April 2007
 
5
    copyright            : (C) 2007 by Marco Hugentobler
 
6
    email                : marco dot hugentobler at karto dot baug dot ethz dot ch
 
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 "qgsmaptooladdring.h"
 
18
#include "qgsgeometry.h"
 
19
#include "qgsmapcanvas.h"
 
20
#include "qgsproject.h"
 
21
#include "qgsrubberband.h"
 
22
#include "qgsvectorlayer.h"
 
23
#include <QMessageBox>
 
24
#include <QMouseEvent>
 
25
 
 
26
QgsMapToolAddRing::QgsMapToolAddRing( QgsMapCanvas* canvas ): QgsMapToolCapture( canvas, QgsMapToolCapture::CapturePolygon )
 
27
{
 
28
 
 
29
}
 
30
 
 
31
QgsMapToolAddRing::~QgsMapToolAddRing()
 
32
{
 
33
 
 
34
}
 
35
 
 
36
void QgsMapToolAddRing::canvasReleaseEvent( QMouseEvent * e )
 
37
{
 
38
  //check if we operate on a vector layer
 
39
  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
 
40
 
 
41
  if ( !vlayer )
 
42
  {
 
43
    QMessageBox::information( 0, tr( "Not a vector layer" ),
 
44
                              tr( "The current layer is not a vector layer" ) );
 
45
    return;
 
46
  }
 
47
 
 
48
  if ( !vlayer->isEditable() )
 
49
  {
 
50
    QMessageBox::information( 0, tr( "Layer not editable" ),
 
51
                              tr( "Cannot edit the vector layer. To make it editable, go to the file item "
 
52
                                  "of the layer, right click and check 'Allow Editing'." ) );
 
53
    return;
 
54
  }
 
55
 
 
56
  //add point to list and to rubber band
 
57
  int error = addVertex( e->pos() );
 
58
  if ( error == 1 )
 
59
  {
 
60
    //current layer is not a vector layer
 
61
    return;
 
62
  }
 
63
  else if ( error == 2 )
 
64
  {
 
65
    //problem with coordinate transformation
 
66
    QMessageBox::information( 0, tr( "Coordinate transform error" ),
 
67
                              tr( "Cannot transform the point to the layers coordinate system" ) );
 
68
    return;
 
69
  }
 
70
 
 
71
  if ( e->button() == Qt::LeftButton )
 
72
  {
 
73
    mCapturing = TRUE;
 
74
  }
 
75
  else if ( e->button() == Qt::RightButton )
 
76
  {
 
77
    mCapturing = FALSE;
 
78
    delete mRubberBand;
 
79
    mRubberBand = 0;
 
80
 
 
81
    //close polygon
 
82
    mCaptureList.push_back( *mCaptureList.begin() );
 
83
 
 
84
    vlayer->beginEditCommand( tr( "Ring added" ) );
 
85
    int addRingReturnCode = vlayer->addRing( mCaptureList );
 
86
    if ( addRingReturnCode != 0 )
 
87
    {
 
88
      QString errorMessage;
 
89
      //todo: open message box to communicate errors
 
90
      if ( addRingReturnCode == 1 )
 
91
      {
 
92
        errorMessage = tr( "A problem with geometry type occured" );
 
93
      }
 
94
      else if ( addRingReturnCode == 2 )
 
95
      {
 
96
        errorMessage = tr( "The inserted Ring is not closed" );
 
97
      }
 
98
      else if ( addRingReturnCode == 3 )
 
99
      {
 
100
        errorMessage = tr( "The inserted Ring is not a valid geometry" );
 
101
      }
 
102
      else if ( addRingReturnCode == 4 )
 
103
      {
 
104
        errorMessage = tr( "The inserted Ring crosses existing rings" );
 
105
      }
 
106
      else if ( addRingReturnCode == 5 )
 
107
      {
 
108
        errorMessage = tr( "The inserted Ring is not contained in a feature" );
 
109
      }
 
110
      else
 
111
      {
 
112
        errorMessage = tr( "An unknown error occured" );
 
113
      }
 
114
      QMessageBox::critical( 0, tr( "Error, could not add ring" ), errorMessage );
 
115
      vlayer->destroyEditCommand();
 
116
    }
 
117
    else
 
118
    {
 
119
      vlayer->endEditCommand();
 
120
    }
 
121
    mCaptureList.clear();
 
122
    mCanvas->refresh();
 
123
  }
 
124
}