Skip to content

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
 

‎src/app/qgsmaptooladdring.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
25+
QgsMapToolAddRing::QgsMapToolAddRing(QgsMapCanvas* canvas): QgsMapToolCapture(canvas, QgsMapToolCapture::CapturePolygon)
26+
{
27+
28+
}
29+
30+
QgsMapToolAddRing::~QgsMapToolAddRing()
31+
{
32+
33+
}
34+
35+
void QgsMapToolAddRing::canvasReleaseEvent(QMouseEvent * e)
36+
{
37+
//check if we operate on a vector layer
38+
QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>(mCanvas->currentLayer());
39+
40+
if (!vlayer)
41+
{
42+
QMessageBox::information(0, QObject::tr("Not a vector layer"), \
43+
QObject::tr("The current layer is not a vector layer"));
44+
return;
45+
}
46+
47+
if (!vlayer->isEditable())
48+
{
49+
QMessageBox::information(0, QObject::tr("Layer not editable"),
50+
QObject::tr("Cannot edit the vector layer. To make it editable, go to the file item "
51+
"of the layer, right click and check 'Allow Editing'."));
52+
return;
53+
}
54+
55+
//add point to list and to rubber band
56+
int error = addVertex(e->pos());
57+
if(error == 1)
58+
{
59+
//current layer is not a vector layer
60+
return;
61+
}
62+
else if (error == 2)
63+
{
64+
//problem with coordinate transformation
65+
QMessageBox::information(0, QObject::tr("Coordinate transform error"), \
66+
QObject::tr("Cannot transform the point to the layers coordinate system"));
67+
return;
68+
}
69+
70+
if (e->button() == Qt::LeftButton)
71+
{
72+
mCapturing = TRUE;
73+
}
74+
else if (e->button() == Qt::RightButton)
75+
{
76+
mCapturing = FALSE;
77+
delete mRubberBand;
78+
mRubberBand = 0;
79+
80+
//close polygon
81+
mCaptureList.push_back(*mCaptureList.begin());
82+
83+
int addRingReturnCode = vlayer->addRing(mCaptureList);
84+
if(addRingReturnCode != 0)
85+
{
86+
QString errorMessage;
87+
//todo: open message box to communicate errors
88+
if(addRingReturnCode == 1)
89+
{
90+
errorMessage = QObject::tr("A problem with geometry type occured");
91+
}
92+
else if(addRingReturnCode == 2)
93+
{
94+
errorMessage = QObject::tr("The inserted Ring is not closed");
95+
}
96+
else if(addRingReturnCode == 3)
97+
{
98+
errorMessage = QObject::tr("The inserted Ring is not a valid geometry");
99+
}
100+
else if(addRingReturnCode == 4)
101+
{
102+
errorMessage = QObject::tr("The inserted Ring crosses existing rings");
103+
}
104+
else if(addRingReturnCode == 5)
105+
{
106+
errorMessage = QObject::tr("The inserted Ring is not contained in a feature");
107+
}
108+
else
109+
{
110+
errorMessage = QObject::tr("An unknown error occured");
111+
}
112+
QMessageBox::critical(0, QObject::tr("Error, could not add ring"), errorMessage);
113+
}
114+
mCaptureList.clear();
115+
mCanvas->refresh();
116+
}
117+
}

‎src/app/qgsmaptooladdring.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***************************************************************************
2+
qgsmaptooladdring.h - 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 "qgsmaptoolcapture.h"
18+
19+
/**A tool to cut holes into polygons and multipolygon features*/
20+
class QgsMapToolAddRing: public QgsMapToolCapture
21+
{
22+
public:
23+
QgsMapToolAddRing(QgsMapCanvas* canvas);
24+
~QgsMapToolAddRing();
25+
void canvasReleaseEvent(QMouseEvent * e);
26+
};

0 commit comments

Comments
 (0)
Please sign in to comment.