multiselectwidget.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import sys
  2. from PyQt4.QtCore import *
  3. from PyQt4.QtGui import *
  4. from ui_multiselectwidget import *
  5. class MultiSelectWidget(Ui_MultiSelectWidget, QWidget):
  6. __pyqtSignals__ = ("selected_changed()",)
  7. __pyqtSignals__ = ("items_added(PyQt_PyObject)",)
  8. __pyqtSignals__ = ("items_removed(PyQt_PyObject)",)
  9. def __init__(self, parent=None):
  10. super(MultiSelectWidget, self).__init__()
  11. self.setupUi(self)
  12. self.add_selected_button.clicked.connect(self.add_selected)
  13. self.add_all_button.clicked.connect(self.add_all)
  14. self.remove_selected_button.clicked.connect(self.remove_selected)
  15. self.remove_all_button.clicked.connect(self.remove_all)
  16. self.available_list.setSelectionMode(QAbstractItemView.ExtendedSelection)
  17. self.selected_list.setSelectionMode(QAbstractItemView.ExtendedSelection)
  18. def switch_selected(self, src, dst):
  19. selected = src.selectedItems()
  20. items = []
  21. for s in selected:
  22. row = src.indexFromItem(s).row()
  23. item = src.takeItem(row)
  24. dst.addItem(item)
  25. items.append(item)
  26. dst.sortItems()
  27. self.emit(SIGNAL("selected_changed()"))
  28. if src is self.selected_list:
  29. self.emit(SIGNAL("items_removed(PyQt_PyObject)"), items)
  30. else:
  31. self.emit(SIGNAL("items_added(PyQt_PyObject)"), items)
  32. def add_selected(self):
  33. self.switch_selected(self.available_list, self.selected_list)
  34. def remove_selected(self):
  35. self.switch_selected(self.selected_list, self.available_list)
  36. def move_all(self, src, dst):
  37. items = []
  38. while src.count() > 0:
  39. item = src.takeItem(0)
  40. dst.addItem(item)
  41. items.append(item)
  42. dst.sortItems()
  43. self.emit(SIGNAL("selected_changed()"))
  44. if src is self.selected_list:
  45. self.emit(SIGNAL("items_removed(PyQt_PyObject)"), items)
  46. else:
  47. self.emit(SIGNAL("items_added(PyQt_PyObject)"), items)
  48. def add_all(self):
  49. self.move_all(self.available_list, self.selected_list)
  50. def remove_all(self):
  51. self.move_all(self.selected_list, self.available_list)
  52. def clear(self):
  53. self.available_list.clear()
  54. self.selected_list.clear()