keyDialog.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import re
  2. from PyQt5 import QtCore
  3. from PyQt5 import QtGui
  4. from PyQt5 import QtWidgets
  5. from PyQt5.Qt import QT_VERSION_STR
  6. from libs.utils import newIcon, labelValidator
  7. QT5 = QT_VERSION_STR[0] == '5'
  8. # TODO(unknown):
  9. # - Calculate optimal position so as not to go out of screen area.
  10. class KeyQLineEdit(QtWidgets.QLineEdit):
  11. def setListWidget(self, list_widget):
  12. self.list_widget = list_widget
  13. def keyPressEvent(self, e):
  14. if e.key() in [QtCore.Qt.Key_Up, QtCore.Qt.Key_Down]:
  15. self.list_widget.keyPressEvent(e)
  16. else:
  17. super(KeyQLineEdit, self).keyPressEvent(e)
  18. class KeyDialog(QtWidgets.QDialog):
  19. def __init__(
  20. self,
  21. text="Enter object label",
  22. parent=None,
  23. labels=None,
  24. sort_labels=True,
  25. show_text_field=True,
  26. completion="startswith",
  27. fit_to_content=None,
  28. flags=None,
  29. ):
  30. if fit_to_content is None:
  31. fit_to_content = {"row": False, "column": True}
  32. self._fit_to_content = fit_to_content
  33. super(KeyDialog, self).__init__(parent)
  34. self.edit = KeyQLineEdit()
  35. self.edit.setPlaceholderText(text)
  36. self.edit.setValidator(labelValidator())
  37. self.edit.editingFinished.connect(self.postProcess)
  38. if flags:
  39. self.edit.textChanged.connect(self.updateFlags)
  40. layout = QtWidgets.QVBoxLayout()
  41. if show_text_field:
  42. layout_edit = QtWidgets.QHBoxLayout()
  43. layout_edit.addWidget(self.edit, 6)
  44. layout.addLayout(layout_edit)
  45. # buttons
  46. self.buttonBox = bb = QtWidgets.QDialogButtonBox(
  47. QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel,
  48. QtCore.Qt.Horizontal,
  49. self,
  50. )
  51. bb.button(bb.Ok).setIcon(newIcon("done"))
  52. bb.button(bb.Cancel).setIcon(newIcon("undo"))
  53. bb.accepted.connect(self.validate)
  54. bb.rejected.connect(self.reject)
  55. layout.addWidget(bb)
  56. # label_list
  57. self.labelList = QtWidgets.QListWidget()
  58. if self._fit_to_content["row"]:
  59. self.labelList.setHorizontalScrollBarPolicy(
  60. QtCore.Qt.ScrollBarAlwaysOff
  61. )
  62. if self._fit_to_content["column"]:
  63. self.labelList.setVerticalScrollBarPolicy(
  64. QtCore.Qt.ScrollBarAlwaysOff
  65. )
  66. self._sort_labels = sort_labels
  67. if labels:
  68. self.labelList.addItems(labels)
  69. if self._sort_labels:
  70. self.labelList.sortItems()
  71. else:
  72. self.labelList.setDragDropMode(
  73. QtWidgets.QAbstractItemView.InternalMove
  74. )
  75. self.labelList.currentItemChanged.connect(self.labelSelected)
  76. self.labelList.itemDoubleClicked.connect(self.labelDoubleClicked)
  77. self.edit.setListWidget(self.labelList)
  78. layout.addWidget(self.labelList)
  79. # label_flags
  80. if flags is None:
  81. flags = {}
  82. self._flags = flags
  83. self.flagsLayout = QtWidgets.QVBoxLayout()
  84. self.resetFlags()
  85. layout.addItem(self.flagsLayout)
  86. self.edit.textChanged.connect(self.updateFlags)
  87. self.setLayout(layout)
  88. # completion
  89. completer = QtWidgets.QCompleter()
  90. if not QT5 and completion != "startswith":
  91. completion = "startswith"
  92. if completion == "startswith":
  93. completer.setCompletionMode(QtWidgets.QCompleter.InlineCompletion)
  94. # Default settings.
  95. # completer.setFilterMode(QtCore.Qt.MatchStartsWith)
  96. elif completion == "contains":
  97. completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
  98. completer.setFilterMode(QtCore.Qt.MatchContains)
  99. else:
  100. raise ValueError("Unsupported completion: {}".format(completion))
  101. completer.setModel(self.labelList.model())
  102. self.edit.setCompleter(completer)
  103. def addLabelHistory(self, label):
  104. if self.labelList.findItems(label, QtCore.Qt.MatchExactly):
  105. return
  106. self.labelList.addItem(label)
  107. if self._sort_labels:
  108. self.labelList.sortItems()
  109. def labelSelected(self, item):
  110. self.edit.setText(item.text())
  111. def validate(self):
  112. text = self.edit.text()
  113. if hasattr(text, "strip"):
  114. text = text.strip()
  115. else:
  116. text = text.trimmed()
  117. if text:
  118. self.accept()
  119. def labelDoubleClicked(self, item):
  120. self.validate()
  121. def postProcess(self):
  122. text = self.edit.text()
  123. if hasattr(text, "strip"):
  124. text = text.strip()
  125. else:
  126. text = text.trimmed()
  127. self.edit.setText(text)
  128. def updateFlags(self, label_new):
  129. # keep state of shared flags
  130. flags_old = self.getFlags()
  131. flags_new = {}
  132. for pattern, keys in self._flags.items():
  133. if re.match(pattern, label_new):
  134. for key in keys:
  135. flags_new[key] = flags_old.get(key, False)
  136. self.setFlags(flags_new)
  137. def deleteFlags(self):
  138. for i in reversed(range(self.flagsLayout.count())):
  139. item = self.flagsLayout.itemAt(i).widget()
  140. self.flagsLayout.removeWidget(item)
  141. item.setParent(None)
  142. def resetFlags(self, label=""):
  143. flags = {}
  144. for pattern, keys in self._flags.items():
  145. if re.match(pattern, label):
  146. for key in keys:
  147. flags[key] = False
  148. self.setFlags(flags)
  149. def setFlags(self, flags):
  150. self.deleteFlags()
  151. for key in flags:
  152. item = QtWidgets.QCheckBox(key, self)
  153. item.setChecked(flags[key])
  154. self.flagsLayout.addWidget(item)
  155. item.show()
  156. def getFlags(self):
  157. flags = {}
  158. for i in range(self.flagsLayout.count()):
  159. item = self.flagsLayout.itemAt(i).widget()
  160. flags[item.text()] = item.isChecked()
  161. return flags
  162. def popUp(self, text=None, move=True, flags=None):
  163. if self._fit_to_content["row"]:
  164. self.labelList.setMinimumHeight(
  165. self.labelList.sizeHintForRow(0) * self.labelList.count() + 2
  166. )
  167. if self._fit_to_content["column"]:
  168. self.labelList.setMinimumWidth(
  169. self.labelList.sizeHintForColumn(0) + 2
  170. )
  171. # if text is None, the previous label in self.edit is kept
  172. if text is None:
  173. text = self.edit.text()
  174. if flags:
  175. self.setFlags(flags)
  176. else:
  177. self.resetFlags(text)
  178. self.edit.setText(text)
  179. self.edit.setSelection(0, len(text))
  180. items = self.labelList.findItems(text, QtCore.Qt.MatchFixedString)
  181. if items:
  182. if len(items) != 1:
  183. self.labelList.setCurrentItem(items[0])
  184. row = self.labelList.row(items[0])
  185. self.edit.completer().setCurrentRow(row)
  186. self.edit.setFocus(QtCore.Qt.PopupFocusReason)
  187. if move:
  188. self.move(QtGui.QCursor.pos())
  189. if self.exec_():
  190. return self.edit.text(), self.getFlags()
  191. else:
  192. return None, None