labelDialog.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. try:
  14. from PyQt5.QtGui import *
  15. from PyQt5.QtCore import *
  16. from PyQt5.QtWidgets import *
  17. except ImportError:
  18. from PyQt4.QtGui import *
  19. from PyQt4.QtCore import *
  20. from libs.utils import newIcon, labelValidator
  21. BB = QDialogButtonBox
  22. class LabelDialog(QDialog):
  23. def __init__(self, text="Enter object label", parent=None, listItem=None):
  24. super(LabelDialog, self).__init__(parent)
  25. self.edit = QLineEdit() # OLD
  26. # self.edit = QTextEdit()
  27. self.edit.setText(text)
  28. # self.edit.setValidator(labelValidator()) # 验证有效性
  29. self.edit.editingFinished.connect(self.postProcess)
  30. model = QStringListModel()
  31. model.setStringList(listItem)
  32. completer = QCompleter()
  33. completer.setModel(model)
  34. self.edit.setCompleter(completer)
  35. layout = QVBoxLayout()
  36. layout.addWidget(self.edit)
  37. self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
  38. bb.button(BB.Ok).setIcon(newIcon('done'))
  39. bb.button(BB.Cancel).setIcon(newIcon('undo'))
  40. bb.accepted.connect(self.validate)
  41. bb.rejected.connect(self.reject)
  42. layout.addWidget(bb)
  43. # if listItem is not None and len(listItem) > 0:
  44. # self.listWidget = QListWidget(self)
  45. # for item in listItem:
  46. # self.listWidget.addItem(item)
  47. # self.listWidget.itemClicked.connect(self.listItemClick)
  48. # self.listWidget.itemDoubleClicked.connect(self.listItemDoubleClick)
  49. # layout.addWidget(self.listWidget)
  50. self.setLayout(layout)
  51. def validate(self):
  52. try:
  53. if self.edit.text().trimmed():
  54. self.accept()
  55. except AttributeError:
  56. # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
  57. if self.edit.text().strip():
  58. self.accept()
  59. def postProcess(self):
  60. try:
  61. self.edit.setText(self.edit.text().trimmed())
  62. # print(self.edit.text())
  63. except AttributeError:
  64. # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
  65. self.edit.setText(self.edit.text())
  66. print(self.edit.text())
  67. def popUp(self, text='', move=True):
  68. self.edit.setText(text)
  69. self.edit.setSelection(0, len(text))
  70. self.edit.setFocus(Qt.PopupFocusReason)
  71. if move:
  72. cursor_pos = QCursor.pos()
  73. parent_bottomRight = self.parentWidget().geometry()
  74. max_x = parent_bottomRight.x() + parent_bottomRight.width() - self.sizeHint().width()
  75. max_y = parent_bottomRight.y() + parent_bottomRight.height() - self.sizeHint().height()
  76. max_global = self.parentWidget().mapToGlobal(QPoint(max_x, max_y))
  77. if cursor_pos.x() > max_global.x():
  78. cursor_pos.setX(max_global.x())
  79. if cursor_pos.y() > max_global.y():
  80. cursor_pos.setY(max_global.y())
  81. self.move(cursor_pos)
  82. return self.edit.text() if self.exec_() else None
  83. def listItemClick(self, tQListWidgetItem):
  84. try:
  85. text = tQListWidgetItem.text().trimmed()
  86. except AttributeError:
  87. # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
  88. text = tQListWidgetItem.text().strip()
  89. self.edit.setText(text)
  90. def listItemDoubleClick(self, tQListWidgetItem):
  91. self.listItemClick(tQListWidgetItem)
  92. self.validate()