FixedTableDialog.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """
  2. ***************************************************************************
  3. FixedTableDialog.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.gui import QgsGui
  23. from qgis.PyQt import uic
  24. from qgis.PyQt.QtWidgets import QDialog, QPushButton, QAbstractItemView, QDialogButtonBox
  25. from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
  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', 'DlgFixedTable.ui'))
  31. class FixedTableDialog(BASE, WIDGET):
  32. def __init__(self, param, table):
  33. """
  34. Constructor for FixedTableDialog
  35. :param param: linked processing parameter
  36. :param table: initial table contents - squashed to 1-dimensional!
  37. """
  38. super().__init__(None)
  39. self.setupUi(self)
  40. QgsGui.instance().enableAutoGeometryRestore(self)
  41. self.tblView.setSelectionBehavior(QAbstractItemView.SelectRows)
  42. self.tblView.setSelectionMode(QAbstractItemView.ExtendedSelection)
  43. self.param = param
  44. self.rettable = None
  45. # Additional buttons
  46. self.btnAdd = QPushButton(self.tr('Add row'))
  47. self.buttonBox.addButton(self.btnAdd,
  48. QDialogButtonBox.ActionRole)
  49. self.btnRemove = QPushButton(self.tr('Remove row(s)'))
  50. self.buttonBox.addButton(self.btnRemove,
  51. QDialogButtonBox.ActionRole)
  52. self.btnRemoveAll = QPushButton(self.tr('Remove all'))
  53. self.buttonBox.addButton(self.btnRemoveAll,
  54. QDialogButtonBox.ActionRole)
  55. self.btnAdd.clicked.connect(self.addRow)
  56. self.btnRemove.clicked.connect(lambda: self.removeRows())
  57. self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))
  58. if self.param.hasFixedNumberRows():
  59. self.btnAdd.setEnabled(False)
  60. self.btnRemove.setEnabled(False)
  61. self.btnRemoveAll.setEnabled(False)
  62. self.populateTable(table)
  63. def populateTable(self, table):
  64. cols = len(self.param.headers())
  65. rows = len(table) // cols
  66. model = QStandardItemModel(rows, cols)
  67. # Set headers
  68. model.setHorizontalHeaderLabels(self.param.headers())
  69. # Populate table
  70. for row in range(rows):
  71. for col in range(cols):
  72. item = QStandardItem(str(table[row * cols + col]))
  73. model.setItem(row, col, item)
  74. self.tblView.setModel(model)
  75. def accept(self):
  76. cols = self.tblView.model().columnCount()
  77. rows = self.tblView.model().rowCount()
  78. # Table MUST BE 1-dimensional to match core QgsProcessingParameterMatrix expectations
  79. self.rettable = []
  80. for row in range(rows):
  81. for col in range(cols):
  82. self.rettable.append(str(self.tblView.model().item(row, col).text()))
  83. QDialog.accept(self)
  84. def reject(self):
  85. QDialog.reject(self)
  86. def removeRows(self, removeAll=False):
  87. if removeAll:
  88. self.tblView.model().clear()
  89. self.tblView.model().setHorizontalHeaderLabels(self.param.headers())
  90. else:
  91. indexes = sorted(self.tblView.selectionModel().selectedRows())
  92. self.tblView.setUpdatesEnabled(False)
  93. for i in reversed(indexes):
  94. self.tblView.model().removeRows(i.row(), 1)
  95. self.tblView.setUpdatesEnabled(True)
  96. def addRow(self):
  97. items = [QStandardItem('0') for i in range(self.tblView.model().columnCount())]
  98. self.tblView.model().appendRow(items)