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

208 lines
7.4 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. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2015-05-18 21:04:20 +03:00
import os
import warnings
2015-05-18 21:04:20 +03:00
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import (
QMenu,
QAction,
QDialog,
QVBoxLayout,
QDialogButtonBox,
QLabel
)
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QCursor
from qgis.PyQt.QtCore import QCoreApplication, pyqtSignal
from qgis.gui import QgsMapLayerComboBox
from qgis.utils import iface
from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingParameters,
QgsProject,
QgsReferencedRectangle,
QgsMapLayerProxyModel)
2013-08-12 20:44:27 +02:00
from processing.gui.RectangleMapTool import RectangleMapTool
from processing.core.ProcessingConfig import ProcessingConfig
2017-06-09 15:31:25 +10:00
from processing.tools.dataobjects import createContext
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]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
2015-05-18 21:04:20 +03:00
class LayerSelectionDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle(self.tr('Select Extent'))
vl = QVBoxLayout()
vl.addWidget(QLabel(self.tr('Use extent from')))
self.combo = QgsMapLayerComboBox()
self.combo.setFilters(
QgsMapLayerProxyModel.HasGeometry | QgsMapLayerProxyModel.RasterLayer | QgsMapLayerProxyModel.MeshLayer)
self.combo.setShowCrs(ProcessingConfig.getSetting(ProcessingConfig.SHOW_CRS_DEF))
vl.addWidget(self.combo)
self.button_box = QDialogButtonBox()
self.button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
vl.addWidget(self.button_box)
self.setLayout(vl)
def selected_layer(self):
return self.combo.currentLayer()
2015-05-18 21:04:20 +03:00
class ExtentSelectionPanel(BASE, WIDGET):
2012-09-15 18:25:25 +03:00
hasChanged = pyqtSignal()
def __init__(self, dialog, param):
2015-05-18 21:04:20 +03:00
super(ExtentSelectionPanel, self).__init__(None)
self.setupUi(self)
self.leText.textChanged.connect(lambda: self.hasChanged.emit())
2012-09-15 18:25:25 +03:00
self.dialog = dialog
self.param = param
self.crs = QgsProject.instance().crs()
2017-05-16 16:36:00 +10:00
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
if hasattr(self.leText, 'setPlaceholderText'):
self.leText.setPlaceholderText(
self.tr('[Leave blank to use min covering extent]'))
self.btnSelect.clicked.connect(self.selectExtent)
if iface is not None:
canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
self.tool.rectangleCreated.connect(self.updateExtent)
else:
self.prevMapTool = None
self.tool = None
2012-09-15 18:25:25 +03:00
2017-06-09 15:31:25 +10:00
if param.defaultValue() is not None:
context = createContext()
rect = QgsProcessingParameters.parameterAsExtent(param, {param.name(): param.defaultValue()}, context)
crs = QgsProcessingParameters.parameterAsExtentCrs(param, {param.name(): param.defaultValue()}, context)
2017-06-09 15:31:25 +10:00
if not rect.isNull():
2015-12-23 16:05:39 +01:00
try:
2017-06-09 15:31:25 +10:00
s = '{},{},{},{}'.format(
rect.xMinimum(), rect.xMaximum(), rect.yMinimum(), rect.yMaximum())
if crs.isValid():
s += ' [' + crs.authid() + ']'
self.crs = crs
2017-06-09 15:31:25 +10:00
self.leText.setText(s)
2015-12-23 16:05:39 +01:00
except:
pass
def selectExtent(self):
2012-09-15 18:25:25 +03:00
popupmenu = QMenu()
useCanvasExtentAction = QAction(
QCoreApplication.translate("ExtentSelectionPanel", 'Use Canvas Extent'),
self.btnSelect)
useLayerExtentAction = QAction(
QCoreApplication.translate("ExtentSelectionPanel", 'Use Layer Extent…'),
2018-09-21 15:44:19 +02:00
self.btnSelect)
selectOnCanvasAction = QAction(
self.tr('Select Extent on Canvas'), self.btnSelect)
popupmenu.addAction(useCanvasExtentAction)
2012-09-15 18:25:25 +03:00
popupmenu.addAction(selectOnCanvasAction)
popupmenu.addSeparator()
popupmenu.addAction(useLayerExtentAction)
selectOnCanvasAction.triggered.connect(self.selectOnCanvas)
useLayerExtentAction.triggered.connect(self.useLayerExtent)
useCanvasExtentAction.triggered.connect(self.useCanvasExtent)
2017-05-16 16:36:00 +10:00
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
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('')
2012-09-15 18:25:25 +03:00
def useLayerExtent(self):
dlg = LayerSelectionDialog(self)
if dlg.exec_():
layer = dlg.selected_layer()
self.setValueFromRect(QgsReferencedRectangle(layer.extent(), layer.crs()))
2012-09-15 18:25:25 +03:00
def useCanvasExtent(self):
self.setValueFromRect(QgsReferencedRectangle(iface.mapCanvas().extent(),
iface.mapCanvas().mapSettings().destinationCrs()))
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())
try:
self.crs = r.crs()
except:
self.crs = QgsProject.instance().crs()
if self.crs.isValid():
s += ' [' + self.crs.authid() + ']'
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)