2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RectangleMapTool.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from PyQt4.QtCore import pyqtSignal
|
|
|
|
from PyQt4.QtGui import QColor
|
|
|
|
from qgis.core import QGis, QgsPoint, QgsRectangle
|
|
|
|
from qgis.gui import QgsMapTool, QgsMapToolEmitPoint, QgsRubberBand
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
class RectangleMapTool(QgsMapToolEmitPoint):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-10-04 19:47:46 +03:00
|
|
|
rectangleCreated = pyqtSignal()
|
2015-06-19 20:42:00 +02:00
|
|
|
deactivated = pyqtSignal()
|
2014-10-04 19:47:46 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def __init__(self, canvas):
|
|
|
|
self.canvas = canvas
|
|
|
|
QgsMapToolEmitPoint.__init__(self, self.canvas)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
self.rubberBand = QgsRubberBand(self.canvas, QGis.Polygon)
|
2013-11-03 12:29:05 +01:00
|
|
|
self.rubberBand.setColor(QColor(255, 0, 0, 100))
|
|
|
|
self.rubberBand.setWidth(2)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
self.reset()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def reset(self):
|
|
|
|
self.startPoint = self.endPoint = None
|
|
|
|
self.isEmittingPoint = False
|
|
|
|
self.rubberBand.reset(QGis.Polygon)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def canvasPressEvent(self, e):
|
|
|
|
self.startPoint = self.toMapCoordinates(e.pos())
|
|
|
|
self.endPoint = self.startPoint
|
|
|
|
self.isEmittingPoint = True
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
self.showRect(self.startPoint, self.endPoint)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def canvasReleaseEvent(self, e):
|
|
|
|
self.isEmittingPoint = False
|
|
|
|
if self.rectangle() is not None:
|
2014-10-04 19:47:46 +03:00
|
|
|
self.rectangleCreated.emit()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def canvasMoveEvent(self, e):
|
|
|
|
if not self.isEmittingPoint:
|
|
|
|
return
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
self.endPoint = self.toMapCoordinates(e.pos())
|
2012-09-15 18:25:25 +03:00
|
|
|
self.showRect(self.startPoint, self.endPoint)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def showRect(self, startPoint, endPoint):
|
|
|
|
self.rubberBand.reset(QGis.Polygon)
|
|
|
|
if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
|
|
|
|
return
|
|
|
|
|
|
|
|
point1 = QgsPoint(startPoint.x(), startPoint.y())
|
|
|
|
point2 = QgsPoint(startPoint.x(), endPoint.y())
|
|
|
|
point3 = QgsPoint(endPoint.x(), endPoint.y())
|
|
|
|
point4 = QgsPoint(endPoint.x(), startPoint.y())
|
|
|
|
|
|
|
|
self.rubberBand.addPoint(point1, False)
|
|
|
|
self.rubberBand.addPoint(point2, False)
|
|
|
|
self.rubberBand.addPoint(point3, False)
|
|
|
|
# True to update canvas
|
|
|
|
self.rubberBand.addPoint(point4, True)
|
|
|
|
self.rubberBand.show()
|
|
|
|
|
|
|
|
def rectangle(self):
|
|
|
|
if self.startPoint is None or self.endPoint is None:
|
|
|
|
return None
|
|
|
|
elif self.startPoint.x() == self.endPoint.x() or \
|
|
|
|
self.startPoint.y() == self.endPoint.y():
|
|
|
|
return None
|
|
|
|
|
|
|
|
return QgsRectangle(self.startPoint, self.endPoint)
|
|
|
|
|
|
|
|
def setRectangle(self, rect):
|
|
|
|
if rect == self.rectangle():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if rect is None:
|
|
|
|
self.reset()
|
|
|
|
else:
|
|
|
|
self.startPoint = QgsPoint(rect.xMaximum(), rect.yMaximum())
|
|
|
|
self.endPoint = QgsPoint(rect.xMinimum(), rect.yMinimum())
|
|
|
|
self.showRect(self.startPoint, self.endPoint)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def deactivate(self):
|
|
|
|
QgsMapTool.deactivate(self)
|
2014-10-04 19:47:46 +03:00
|
|
|
self.deactivated.emit()
|