Skip to content

Commit

Permalink
[processing] update extent selection widget
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Nov 17, 2014
1 parent 24a3e22 commit c3d230c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 44 deletions.
84 changes: 42 additions & 42 deletions python/plugins/processing/gui/ExtentSelectionPanel.py
Expand Up @@ -25,43 +25,39 @@

__revision__ = '$Format:%H$'

from qgis.core import *
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.utils import iface

from processing.gui.RectangleMapTool import RectangleMapTool
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterMultipleInput
from processing.tools import dataobjects
from qgis.utils import iface

class ExtentSelectionPanel(QtGui.QWidget):
from processing.ui.ui_widgetExtentSelector import Ui_Form

class ExtentSelectionPanel(QWidget, Ui_Form):

def __init__(self, dialog, alg, default):
super(ExtentSelectionPanel, self).__init__(None)
QWidget.__init__(self)
self.setupUi(self)

self.dialog = dialog
self.params = alg.parameters
self.horizontalLayout = QtGui.QHBoxLayout(self)
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.text = QtGui.QLineEdit()
self.text.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
if self.canUseAutoExtent():
if hasattr(self.text, 'setPlaceholderText'):
self.text.setPlaceholderText(
if hasattr(self.leExtent, 'setPlaceholderText'):
self.leExtent.setPlaceholderText(
self.tr('[Leave blank to use min covering extent]'))
self.horizontalLayout.addWidget(self.text)
self.pushButton = QtGui.QPushButton()
self.pushButton.setText('...')
self.pushButton.clicked.connect(self.buttonPushed)
self.horizontalLayout.addWidget(self.pushButton)
self.setLayout(self.horizontalLayout)

self.btnSelect.clicked.connect(self.selectExtent)

canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
self.connect(self.tool, SIGNAL('rectangleCreated()'), self.fillCoords)
self.tool.rectangleCreated.connect(self.updateExtent)

def canUseAutoExtent(self):
for param in self.params:
Expand All @@ -72,28 +68,31 @@ def canUseAutoExtent(self):

return False

def buttonPushed(self):
def selectExtent(self):
popupmenu = QMenu()
useLayerExtentAction = QtGui.QAction(self.tr('Use layer/canvas extent'),
self.pushButton)
useLayerExtentAction.triggered.connect(self.useLayerExtent)
useLayerExtentAction = QAction(
self.tr('Use layer/canvas extent'), self.btnSelect)
selectOnCanvasAction = QAction(
self.tr('Select extent on canvas'), self.btnSelect)

popupmenu.addAction(useLayerExtentAction)
selectOnCanvasAction = QtGui.QAction(self.tr('Select extent on canvas'),
self.pushButton)
selectOnCanvasAction.triggered.connect(self.selectOnCanvas)
popupmenu.addAction(selectOnCanvasAction)

selectOnCanvasAction.triggered.connect(self.selectOnCanvas)
useLayerExtentAction.triggered.connect(self.useLayerExtent)

if self.canUseAutoExtent():
useMincoveringExtentAction = \
QtGui.QAction(self.tr('Use min covering extent from input layers'),
self.pushButton)
useMincoveringExtentAction = QAction(
self.tr('Use min covering extent from input layers'),
self.btnSelect)
useMincoveringExtentAction.triggered.connect(
self.useMinCoveringExtent)
popupmenu.addAction(useMincoveringExtentAction)

popupmenu.exec_(QtGui.QCursor.pos())
popupmenu.exec_(QCursor.pos())

def useMinCoveringExtent(self):
self.text.setText('')
self.leExtent.setText('')

def getMinCoveringExtent(self):
first = True
Expand All @@ -119,8 +118,8 @@ def getMinCoveringExtent(self):
self.addToRegion(layer, first)
first = False
if found:
return str(self.xmin) + ',' + str(self.xmax) + ',' \
+ str(self.ymin) + ',' + str(self.ymax)
return '{},{},{},{}'.format(
self.xmin, self.xmax, self.ymin, self.ymax)
else:
return None

Expand Down Expand Up @@ -148,8 +147,8 @@ def useLayerExtent(self):
for layer in layers:
extents.append(layer.name())
extentsDict[layer.name()] = layer.extent()
(item, ok) = QtGui.QInputDialog.getItem(self, 'Select extent',
'Use extent from', extents, False)
(item, ok) = QInputDialog.getItem(self, self.tr('Select extent'),
self.tr('Use extent from'), extents, False)
if ok:
self.setValueFromRect(extentsDict[item])

Expand All @@ -158,14 +157,15 @@ def selectOnCanvas(self):
canvas.setMapTool(self.tool)
self.dialog.showMinimized()

def fillCoords(self):
def updateExtent(self):
r = self.tool.rectangle()
self.setValueFromRect(r)

def setValueFromRect(self, r):
s = str(r.xMinimum()) + ',' + str(r.xMaximum()) + ',' \
+ str(r.yMinimum()) + ',' + str(r.yMaximum())
self.text.setText(s)
s = '{},{},{},{}'.format(
r.xMinimum(), r.xMaximum(), r.yMinimum(), r.yMaximum())

self.leExtent.setText(s)
self.tool.reset()
canvas = iface.mapCanvas()
canvas.setMapTool(self.prevMapTool)
Expand All @@ -174,7 +174,7 @@ def setValueFromRect(self, r):
self.dialog.activateWindow()

def getValue(self):
if str(self.text.text()).strip() != '':
return str(self.text.text())
if str(self.leExtent.text()).strip() != '':
return str(self.leExtent.text())
else:
return self.getMinCoveringExtent()
7 changes: 5 additions & 2 deletions python/plugins/processing/gui/RectangleMapTool.py
Expand Up @@ -33,6 +33,9 @@

class RectangleMapTool(QgsMapToolEmitPoint):

rectangleCreated = pyqtSignal()
deactovated = pyqtSignal()

def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
Expand All @@ -58,7 +61,7 @@ def canvasPressEvent(self, e):
def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
if self.rectangle() is not None:
self.emit(SIGNAL('rectangleCreated()'))
self.rectangleCreated.emit()

def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
Expand Down Expand Up @@ -107,4 +110,4 @@ def setRectangle(self, rect):

def deactivate(self):
QgsMapTool.deactivate(self)
self.emit(SIGNAL('deactivated()'))
self.deactivated.emit()
37 changes: 37 additions & 0 deletions python/plugins/processing/ui/widgetExtentSelector.ui
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="leExtent"/>
</item>
<item>
<widget class="QToolButton" name="btnSelect">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit c3d230c

Please sign in to comment.