MultipleInputPanel.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. ***************************************************************************
  3. MultipleInputPanel.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.core import QgsProcessing
  23. from qgis.PyQt import uic
  24. from qgis.PyQt.QtCore import pyqtSignal
  25. ''
  26. from processing.gui.MultipleInputDialog import MultipleInputDialog
  27. from processing.gui.MultipleFileInputDialog import MultipleFileInputDialog
  28. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  29. with warnings.catch_warnings():
  30. warnings.filterwarnings("ignore", category=DeprecationWarning)
  31. WIDGET, BASE = uic.loadUiType(
  32. os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
  33. class MultipleInputPanel(BASE, WIDGET):
  34. selectionChanged = pyqtSignal()
  35. def __init__(self, options=None, datatype=None):
  36. super().__init__(None)
  37. self.setupUi(self)
  38. self.leText.setEnabled(False)
  39. self.leText.setText(self.tr('0 elements selected'))
  40. self.btnSelect.clicked.connect(self.showSelectionDialog)
  41. self.options = options
  42. self.datatype = datatype
  43. self.selectedoptions = []
  44. def setSelectedItems(self, selected):
  45. # No checking is performed!
  46. self.selectedoptions = selected
  47. self.leText.setText(
  48. self.tr('{0} elements selected').format(len(self.selectedoptions)))
  49. def showSelectionDialog(self):
  50. if self.datatype == QgsProcessing.TypeFile:
  51. dlg = MultipleFileInputDialog(self.selectedoptions)
  52. else:
  53. dlg = MultipleInputDialog(self.options, self.selectedoptions, datatype=self.datatype)
  54. dlg.exec_()
  55. if dlg.selectedoptions is not None:
  56. self.selectedoptions = dlg.selectedoptions
  57. self.leText.setText(
  58. self.tr('{0} elements selected').format(len(self.selectedoptions)))
  59. self.selectionChanged.emit()
  60. def updateForOptions(self, options):
  61. selectedoptions = []
  62. selected = [self.options[i] if isinstance(i, int) else i for i in self.selectedoptions]
  63. for sel in selected:
  64. if not isinstance(sel, int):
  65. try:
  66. idx = options.index(sel)
  67. selectedoptions.append(idx)
  68. except ValueError:
  69. pass
  70. else:
  71. selectedoptions.append(sel)
  72. self.options = options
  73. self.setSelectedItems(selectedoptions)