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

163 lines
6.0 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
ExtentSelectionPanel.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. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import str
2012-10-05 23:28:47 +02:00
__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$'
2015-05-18 21:04:20 +03:00
import os
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtWidgets import QMenu, QAction, QInputDialog
from qgis.PyQt.QtGui import QCursor
from qgis.gui import QgsMessageBar
from qgis.core import QgsRasterLayer, QgsVectorLayer
from qgis.utils import iface
2013-08-12 20:44:27 +02:00
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.core.ProcessingConfig import ProcessingConfig
from processing.tools import dataobjects
2012-09-15 18:25:25 +03:00
2015-05-18 21:04:20 +03:00
pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
2015-05-18 21:04:20 +03:00
class ExtentSelectionPanel(BASE, WIDGET):
2012-09-15 18:25:25 +03:00
def __init__(self, dialog, param):
2015-05-18 21:04:20 +03:00
super(ExtentSelectionPanel, self).__init__(None)
self.setupUi(self)
2012-09-15 18:25:25 +03:00
self.dialog = dialog
self.param = param
if self.param.optional:
if hasattr(self.leText, 'setPlaceholderText'):
self.leText.setPlaceholderText(
self.tr('[Use "auto" to use min covering extent]'))
self.btnSelect.clicked.connect(self.selectExtent)
canvas = iface.mapCanvas()
2012-09-15 18:25:25 +03:00
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
self.tool.rectangleCreated.connect(self.updateExtent)
2012-09-15 18:25:25 +03:00
if param.default:
tokens = param.default.split(',')
2015-12-23 16:05:39 +01:00
if len(tokens) == 4:
try:
float(tokens[0])
float(tokens[1])
float(tokens[2])
float(tokens[3])
self.leText.setText(param.default)
2015-12-23 16:05:39 +01:00
except:
pass
def selectExtent(self):
2012-09-15 18:25:25 +03:00
popupmenu = QMenu()
useLayerExtentAction = QAction(
self.tr('Use layer/canvas extent'), self.btnSelect)
selectOnCanvasAction = QAction(
self.tr('Select extent on canvas'), self.btnSelect)
2012-09-15 18:25:25 +03:00
popupmenu.addAction(useLayerExtentAction)
popupmenu.addAction(selectOnCanvasAction)
selectOnCanvasAction.triggered.connect(self.selectOnCanvas)
useLayerExtentAction.triggered.connect(self.useLayerExtent)
if self.param.optional:
useMincoveringExtentAction = QAction(
self.tr('Use min covering extent from input layers'),
self.btnSelect)
useMincoveringExtentAction.triggered.connect(
self.useMinCoveringExtent)
2012-09-15 18:25:25 +03:00
popupmenu.addAction(useMincoveringExtentAction)
popupmenu.exec_(QCursor.pos())
2012-09-15 18:25:25 +03:00
def useMinCoveringExtent(self):
self.leText.setText('auto')
2012-09-15 18:25:25 +03:00
def useLayerExtent(self):
CANVAS_KEY = 'Use canvas extent'
2012-09-15 18:25:25 +03:00
extentsDict = {}
extentsDict[CANVAS_KEY] = {"extent": iface.mapCanvas().extent(),
2016-01-20 16:39:13 +02:00
"authid": iface.mapCanvas().mapSettings().destinationCrs().authid()}
2012-09-15 18:25:25 +03:00
extents = [CANVAS_KEY]
layers = dataobjects.getAllLayers()
2012-09-15 18:25:25 +03:00
for layer in layers:
authid = layer.crs().authid()
if ProcessingConfig.getSetting(ProcessingConfig.SHOW_CRS_DEF) \
and authid is not None:
layerName = u'{} [{}]'.format(layer.name(), authid)
else:
layerName = layer.name()
extents.append(layerName)
extentsDict[layerName] = {"extent": layer.extent(), "authid": authid}
(item, ok) = QInputDialog.getItem(self, self.tr('Select extent'),
self.tr('Use extent from'), extents, False)
2012-09-15 18:25:25 +03:00
if ok:
self.setValueFromRect(extentsDict[item]["extent"])
2016-01-20 16:39:13 +02:00
if extentsDict[item]["authid"] != iface.mapCanvas().mapSettings().destinationCrs().authid():
iface.messageBar().pushMessage(self.tr("Warning"),
2015-11-27 23:25:25 +01:00
self.tr("The projection of the chosen layer is not the same as canvas projection! The selected extent might not be what was intended."),
QgsMessageBar.WARNING, 8)
2012-09-15 18:25:25 +03:00
def selectOnCanvas(self):
canvas = iface.mapCanvas()
2012-09-15 18:25:25 +03:00
canvas.setMapTool(self.tool)
self.dialog.showMinimized()
def updateExtent(self):
2012-09-15 18:25:25 +03:00
r = self.tool.rectangle()
self.setValueFromRect(r)
def setValueFromRect(self, r):
s = '{},{},{},{}'.format(
r.xMinimum(), r.xMaximum(), r.yMinimum(), r.yMaximum())
self.leText.setText(s)
2012-09-15 18:25:25 +03:00
self.tool.reset()
canvas = iface.mapCanvas()
2012-09-15 18:25:25 +03:00
canvas.setMapTool(self.prevMapTool)
self.dialog.showNormal()
self.dialog.raise_()
self.dialog.activateWindow()
def getValue(self):
if str(self.leText.text()).strip() != '':
return str(self.leText.text())
else:
2016-09-04 17:58:17 +02:00
return None
def setExtentFromString(self, s):
self.leText.setText(s)