RangePanel.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. ***************************************************************************
  3. RangePanel.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. import warnings
  22. from qgis.PyQt import uic
  23. from qgis.PyQt.QtCore import pyqtSignal
  24. from qgis.PyQt.QtWidgets import QDialog
  25. from qgis.core import QgsProcessingParameterNumber
  26. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  27. with warnings.catch_warnings():
  28. warnings.filterwarnings("ignore", category=DeprecationWarning)
  29. WIDGET, BASE = uic.loadUiType(
  30. os.path.join(pluginPath, 'ui', 'widgetRangeSelector.ui'))
  31. class RangePanel(BASE, WIDGET):
  32. hasChanged = pyqtSignal()
  33. def __init__(self, param):
  34. super().__init__(None)
  35. self.setupUi(self)
  36. self.param = param
  37. # Integer or Double range
  38. if self.param.dataType() == QgsProcessingParameterNumber.Integer:
  39. self.spnMin.setDecimals(0)
  40. self.spnMax.setDecimals(0)
  41. if param.defaultValue() is not None:
  42. self.setValue(param.defaultValue())
  43. values = self.getValues()
  44. # Spin range logic
  45. self.spnMin.valueChanged.connect(lambda: self.setMinMax())
  46. self.spnMax.valueChanged.connect(lambda: self.setMaxMin())
  47. def setMinMax(self):
  48. values = self.getValues()
  49. if values[0] >= values[1]:
  50. self.spnMax.setValue(values[0])
  51. self.hasChanged.emit()
  52. def setMaxMin(self):
  53. values = self.getValues()
  54. if values[0] >= values[1]:
  55. self.spnMin.setValue(values[1])
  56. self.hasChanged.emit()
  57. def getValue(self):
  58. return f'{self.spnMin.value()},{self.spnMax.value()}'
  59. def getValues(self):
  60. value = self.getValue()
  61. if value:
  62. return [float(a) for a in value.split(',')]
  63. def setValue(self, value):
  64. try:
  65. values = value.split(',')
  66. minVal = float(values[0])
  67. maxVal = float(values[1])
  68. self.spnMin.setValue(float(minVal))
  69. self.spnMax.setValue(float(maxVal))
  70. except:
  71. return