QGIS/python/plugins/processing/gui/RectangleMapTool.py

114 lines
3.8 KiB
Python
Raw Normal View History

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'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
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
class RectangleMapTool(QgsMapToolEmitPoint):
2012-09-15 18:25:25 +03:00
rectangleCreated = pyqtSignal()
2015-06-19 20:42:00 +02:00
deactivated = pyqtSignal()
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
2012-09-15 18:25:25 +03:00
self.rubberBand = QgsRubberBand(self.canvas, QGis.Polygon)
self.rubberBand.setColor(QColor(255, 0, 0, 100))
self.rubberBand.setWidth(2)
2012-09-15 18:25:25 +03:00
self.reset()
2012-09-15 18:25:25 +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
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
self.showRect(self.startPoint, self.endPoint)
2012-09-15 18:25:25 +03:00
def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
if self.rectangle() is not None:
self.rectangleCreated.emit()
2012-09-15 18:25:25 +03:00
def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
return
2012-09-15 18:25:25 +03:00
self.endPoint = self.toMapCoordinates(e.pos())
2012-09-15 18:25:25 +03:00
self.showRect(self.startPoint, self.endPoint)
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)
self.deactivated.emit()