Advertisement
Guest User

Untitled

a guest
Aug 26th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import pathlib
  2. from PySide2 import QtWidgets, QtCore
  3.  
  4. class ComboBoxWithRC(QtWidgets.QComboBox):
  5.     RightClicked = QtCore.Signal() #emit this to edit multiple selections via RC
  6.    
  7.     def mousePressEvent(self, event):
  8.         if event.button() == QtCore.Qt.MouseButton.RightButton:
  9.             self.RightClicked.emit()
  10.         else:
  11.             super(ComboBoxWithRC, self).mousePressEvent(event)
  12.  
  13. class TypeDelegate(QtWidgets.QStyledItemDelegate):
  14.     EditNow = QtCore.Signal() #emit this to edit multiple selections via RC
  15.    
  16.     def __init__(self, parent=None):
  17.         super(TypeDelegate, self).__init__(parent)
  18.         self.types = ["A", "B", "C", "D", "E"]
  19.  
  20.     def createEditor(self, parent, option, index):
  21.         w = ComboBoxWithRC(parent)
  22.         w.RightClicked.connect(self.EditNow.emit)
  23.         w.addItems(self.types)
  24.         return w
  25.  
  26.     def setModelData(self, editor, model, index):
  27.         model.setData(index, editor.currentText())
  28.  
  29. class PublishTable(QtWidgets.QTableWidget):
  30.     HEADERS = ["File Path", "Type"]
  31.    
  32.     def __init__(self, parent=None):
  33.         super(PublishTable, self).__init__(parent)
  34.         self.setColumnCount(len(PublishTable.HEADERS))
  35.         self.setHorizontalHeaderLabels(PublishTable.HEADERS)
  36.         self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
  37.         self.type_delegate = TypeDelegate()
  38.         self.type_delegate.EditNow.connect(self.edit_selection)
  39.         self.setItemDelegateForColumn(PublishTable.HEADERS.index("Type"), self.type_delegate)
  40.  
  41.     def load_data(self, path_list):
  42.         prev_row_count = self.rowCount()
  43.         # add the required rows:
  44.         self.setRowCount(prev_row_count + len(path_list))      
  45.  
  46.         data_list = []
  47.         for path in path_list:
  48.             data = (path, None, None)
  49.             data_list.append(data)
  50.         # add the new data
  51.         for row, row_data in enumerate(data_list):
  52.             new_row_index = row + prev_row_count
  53.             for column, value in enumerate(row_data):
  54.                 item = QtWidgets.QTableWidgetItem(value)
  55.                 self.setItem(new_row_index, column, item)
  56.                 self.openPersistentEditor(self.model().index(new_row_index, PublishTable.HEADERS.index("Type")))
  57.  
  58.     def edit_selection(self):
  59.         print("editing multipled rows")
  60.         print(self.selectedIndexes())
  61.  
  62. if __name__ == "__main__":
  63.     import sys
  64.     args = sys.argv
  65.     app = QtWidgets.QApplication(args)
  66.     w = PublishTable()
  67.     w.load_data(["/path/1", "/path/2", "/path/3"])
  68.     w.show()
  69.     sys.exit(app.exec_())
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement