labelDialog.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #
  2. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  3. #
  4. # This file is part of Labelme.
  5. #
  6. # Labelme is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Labelme is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. try:
  20. from PyQt5.QtGui import *
  21. from PyQt5.QtCore import *
  22. from PyQt5.QtWidgets import *
  23. PYQT5 = True
  24. except ImportError:
  25. from PyQt4.QtGui import *
  26. from PyQt4.QtCore import *
  27. PYQT5 = False
  28. from .lib import newIcon, labelValidator
  29. # TODO:
  30. # - Calculate optimal position so as not to go out of screen area.
  31. BB = QDialogButtonBox
  32. class LabelDialog(QDialog):
  33. def __init__(self, text="Enter object label", parent=None, labels=None):
  34. super(LabelDialog, self).__init__(parent)
  35. self.edit = QLineEdit()
  36. self.edit.setPlaceholderText(text)
  37. self.edit.setValidator(labelValidator())
  38. self.edit.editingFinished.connect(self.postProcess)
  39. layout = QVBoxLayout()
  40. layout.addWidget(self.edit)
  41. # buttons
  42. self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
  43. bb.button(BB.Ok).setIcon(newIcon('done'))
  44. bb.button(BB.Cancel).setIcon(newIcon('undo'))
  45. bb.accepted.connect(self.validate)
  46. bb.rejected.connect(self.reject)
  47. layout.addWidget(bb)
  48. # label_list
  49. self.labelList = QListWidget()
  50. if labels:
  51. self.labelList.addItems(labels)
  52. self.labelList.currentItemChanged.connect(self.labelSelected)
  53. layout.addWidget(self.labelList)
  54. self.setLayout(layout)
  55. def addLabelHistory(self, label):
  56. if self.labelList.findItems(label, Qt.MatchExactly):
  57. return
  58. self.labelList.addItem(label)
  59. self.labelList.sortItems()
  60. def labelSelected(self, item):
  61. self.edit.setText(item.text())
  62. def validate(self):
  63. if PYQT5:
  64. if self.edit.text().strip():
  65. self.accept()
  66. else:
  67. if self.edit.text().trimmed():
  68. self.accept()
  69. def postProcess(self):
  70. if PYQT5:
  71. self.edit.setText(self.edit.text().strip())
  72. else:
  73. self.edit.setText(self.edit.text().trimmed())
  74. def popUp(self, text='', move=True):
  75. self.edit.setText(text)
  76. self.edit.setSelection(0, len(text))
  77. self.edit.setFocus(Qt.PopupFocusReason)
  78. if move:
  79. self.move(QCursor.pos())
  80. return self.edit.text() if self.exec_() else None