You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.7 KiB

  1. from PyQt5 import QtWidgets, QtGui
  2. from PyQt5.QtCore import pyqtSignal, Qt, QModelIndex
  3. class MItemModel(QtGui.QStandardItemModel):
  4. """
  5. Special item model to ensure whole row is moved.
  6. """
  7. itemsDropped = pyqtSignal()
  8. def __init__(self):
  9. super().__init__()
  10. self.pendingRemoveRows = False
  11. def dropMimeData(self, data, action, row, col, parent):
  12. """
  13. Always move the entire row, and don't allow column "shifting"
  14. """
  15. ret = super().dropMimeData(data, Qt.MoveAction, row, 0, parent)
  16. if ret:
  17. self.pendingRemoveRows = True
  18. return ret
  19. def removeRows(self, row, count, index=QModelIndex()):
  20. """
  21. Emit a signal after rows have been moved
  22. """
  23. ret = super().removeRows(row, count, index)
  24. if self.pendingRemoveRows:
  25. self.itemsDropped.emit()
  26. self.pendingRemoveRows = False
  27. return ret
  28. class MProxyStyle(QtWidgets.QProxyStyle):
  29. """
  30. Proxy style to change the appearance of the TableView.
  31. """
  32. def drawPrimitive(self, element, option, painter, widget=None):
  33. """
  34. Draw a line across the entire row rather than just the column
  35. we're hovering over.
  36. """
  37. if element == self.PE_IndicatorItemViewItemDrop and not option.rect.isNull():
  38. option_new = QtWidgets.QStyleOption(option)
  39. option_new.rect.setLeft(0)
  40. if widget:
  41. option_new.rect.setRight(widget.width())
  42. option = option_new
  43. super().drawPrimitive(element, option, painter, widget)
  44. class MTableView(QtWidgets.QTableView):
  45. """
  46. Subclass the built in TableView to customise it.
  47. """
  48. def __init__(self, parent):
  49. super().__init__(parent)
  50. self.model = MItemModel()
  51. self.setModel(self.model)
  52. self.verticalHeader().hide()
  53. self.horizontalHeader().show()
  54. self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Interactive)
  55. self.horizontalHeader().setStretchLastSection(True)
  56. self.setShowGrid(False)
  57. # self.setDragDropMode(self.InternalMove)
  58. self.setDragDropOverwriteMode(False)
  59. # Set our custom style - this draws the drop indicator across the whole row
  60. self.setStyle(MProxyStyle())
  61. def clear(self):
  62. self.model.removeRows(0, self.model.rowCount())
  63. class ChordTableView(MTableView):
  64. """
  65. Subclass MTableView to add properties just for the chord table.
  66. """
  67. def __init__(self, parent):
  68. super().__init__(parent)
  69. self.model.setHorizontalHeaderLabels(['Chord', 'Guitar voicing', 'Piano voicing'])
  70. def populate(self, cList):
  71. """
  72. Fill the table from a list of Chord objects.
  73. """
  74. self.model.removeRows(0, self.model.rowCount())
  75. for c in cList:
  76. rowList = [QtGui.QStandardItem(c.name), QtGui.QStandardItem(
  77. ",".join(c.voicings['guitar'] if 'guitar' in c.voicings.keys() else "")),
  78. QtGui.QStandardItem(
  79. ",".join(c.voicings['piano'] if 'piano' in c.voicings.keys() else ""))]
  80. for item in rowList:
  81. item.setEditable(False)
  82. item.setDropEnabled(False)
  83. self.model.appendRow(rowList)
  84. class SectionTableView(MTableView):
  85. """
  86. Subclass MTableView to add properties just for the section table.
  87. """
  88. def __init__(self, parent):
  89. super().__init__(parent)
  90. self.model.setHorizontalHeaderLabels(['Name'])
  91. def populate(self, sList):
  92. """
  93. Fill the table from a list of Section objects.
  94. """
  95. self.model.removeRows(0, self.model.rowCount())
  96. for s in sList:
  97. rowList = [QtGui.QStandardItem(s.name)]
  98. for item in rowList:
  99. item.setEditable(False)
  100. item.setDropEnabled(False)
  101. self.model.appendRow(rowList)
  102. class BlockTableView(MTableView):
  103. """
  104. Subclass MTableView to add properties just for the block table.
  105. """
  106. def __init__(self, parent):
  107. super().__init__(parent)
  108. self.model.setHorizontalHeaderLabels(['Chord', 'Length', 'Notes'])
  109. def populate(self, bList):
  110. """
  111. Fill the table from a list of Block objects.
  112. """
  113. self.model.removeRows(0, self.model.rowCount())
  114. for b in bList:
  115. rowList = [QtGui.QStandardItem((b.chord.name if b.chord else "")), QtGui.QStandardItem(
  116. str(b.length)), QtGui.QStandardItem(b.notes)]
  117. for item in rowList:
  118. item.setEditable(False)
  119. item.setDropEnabled(False)
  120. self.model.appendRow(rowList)