labelDialog.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from PyQt4.QtGui import *
  20. from PyQt4.QtCore import *
  21. from lib import newIcon, labelValidator
  22. # TODO:
  23. # - Calculate optimal position so as not to go out of screen area.
  24. BB = QDialogButtonBox
  25. class LabelDialog(QDialog):
  26. def __init__(self, text="Enter object label", parent=None):
  27. super(LabelDialog, self).__init__(parent)
  28. self.edit = QLineEdit()
  29. self.edit.setText(text)
  30. self.edit.setValidator(labelValidator())
  31. self.edit.editingFinished.connect(self.postProcess)
  32. layout = QVBoxLayout()
  33. layout.addWidget(self.edit)
  34. self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
  35. bb.button(BB.Ok).setIcon(newIcon('done'))
  36. bb.button(BB.Cancel).setIcon(newIcon('undo'))
  37. bb.accepted.connect(self.validate)
  38. bb.rejected.connect(self.reject)
  39. layout.addWidget(bb)
  40. self.setLayout(layout)
  41. def validate(self):
  42. if self.edit.text().trimmed():
  43. self.accept()
  44. def postProcess(self):
  45. self.edit.setText(self.edit.text().trimmed())
  46. def popUp(self, text='', move=True):
  47. self.edit.setText(text)
  48. self.edit.setSelection(0, len(text))
  49. self.edit.setFocus(Qt.PopupFocusReason)
  50. if move:
  51. self.move(QCursor.pos())
  52. return self.edit.text() if self.exec_() else None