label_qlist_widget.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from qtpy import QtWidgets
  2. class LabelQListWidget(QtWidgets.QListWidget):
  3. def __init__(self, *args, **kwargs):
  4. super(LabelQListWidget, self).__init__(*args, **kwargs)
  5. self.canvas = None
  6. self.itemsToShapes = []
  7. def get_shape_from_item(self, item):
  8. for index, (item_, shape) in enumerate(self.itemsToShapes):
  9. if item_ is item:
  10. return shape
  11. def get_item_from_shape(self, shape):
  12. for index, (item, shape_) in enumerate(self.itemsToShapes):
  13. if shape_ is shape:
  14. return item
  15. def clear(self):
  16. super(LabelQListWidget, self).clear()
  17. self.itemsToShapes = []
  18. def setParent(self, parent):
  19. self.parent = parent
  20. def dropEvent(self, event):
  21. shapes = self.shapes
  22. super(LabelQListWidget, self).dropEvent(event)
  23. if self.shapes == shapes:
  24. return
  25. if self.canvas is None:
  26. raise RuntimeError('self.canvas must be set beforehand.')
  27. self.parent.setDirty()
  28. self.canvas.loadShapes(self.shapes)
  29. @property
  30. def shapes(self):
  31. shapes = []
  32. for i in range(self.count()):
  33. item = self.item(i)
  34. shape = self.get_shape_from_item(item)
  35. shapes.append(shape)
  36. return shapes