123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- # Copyright (c) <2015-Present> Tzutalin
- # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
- # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
- # associated documentation files (the "Software"), to deal in the Software without restriction, including without
- # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
- # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
- # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
- # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
- # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
- # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- # THE SOFTWARE.
- try:
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- except ImportError:
- from PyQt4.QtGui import *
- from PyQt4.QtCore import *
- from libs.utils import newIcon, labelValidator
- BB = QDialogButtonBox
- class LabelDialog(QDialog):
- def __init__(self, text="Enter object label", parent=None, listItem=None):
- super(LabelDialog, self).__init__(parent)
- self.edit = QLineEdit() # OLD
- # self.edit = QTextEdit()
- self.edit.setText(text)
- # self.edit.setValidator(labelValidator()) # 验证有效性
- self.edit.editingFinished.connect(self.postProcess)
- model = QStringListModel()
- model.setStringList(listItem)
- completer = QCompleter()
- completer.setModel(model)
- self.edit.setCompleter(completer)
- layout = QVBoxLayout()
- layout.addWidget(self.edit)
- self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
- bb.button(BB.Ok).setIcon(newIcon('done'))
- bb.button(BB.Cancel).setIcon(newIcon('undo'))
- bb.accepted.connect(self.validate)
- bb.rejected.connect(self.reject)
- layout.addWidget(bb)
- # if listItem is not None and len(listItem) > 0:
- # self.listWidget = QListWidget(self)
- # for item in listItem:
- # self.listWidget.addItem(item)
- # self.listWidget.itemClicked.connect(self.listItemClick)
- # self.listWidget.itemDoubleClicked.connect(self.listItemDoubleClick)
- # layout.addWidget(self.listWidget)
- self.setLayout(layout)
- def validate(self):
- try:
- if self.edit.text().trimmed():
- self.accept()
- except AttributeError:
- # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
- if self.edit.text().strip():
- self.accept()
- def postProcess(self):
- try:
- self.edit.setText(self.edit.text().trimmed())
- # print(self.edit.text())
- except AttributeError:
- # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
- self.edit.setText(self.edit.text())
- print(self.edit.text())
- def popUp(self, text='', move=True):
- self.edit.setText(text)
- self.edit.setSelection(0, len(text))
- self.edit.setFocus(Qt.PopupFocusReason)
- if move:
- cursor_pos = QCursor.pos()
- parent_bottomRight = self.parentWidget().geometry()
- max_x = parent_bottomRight.x() + parent_bottomRight.width() - self.sizeHint().width()
- max_y = parent_bottomRight.y() + parent_bottomRight.height() - self.sizeHint().height()
- max_global = self.parentWidget().mapToGlobal(QPoint(max_x, max_y))
- if cursor_pos.x() > max_global.x():
- cursor_pos.setX(max_global.x())
- if cursor_pos.y() > max_global.y():
- cursor_pos.setY(max_global.y())
- self.move(cursor_pos)
- return self.edit.text() if self.exec_() else None
- def listItemClick(self, tQListWidgetItem):
- try:
- text = tQListWidgetItem.text().trimmed()
- except AttributeError:
- # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
- text = tQListWidgetItem.text().strip()
- self.edit.setText(text)
- def listItemDoubleClick(self, tQListWidgetItem):
- self.listItemClick(tQListWidgetItem)
- self.validate()
|