labelme.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. import sys
  4. from PyQt4.QtGui import *
  5. from PyQt4.QtCore import *
  6. __appname__ = 'labelme'
  7. ### Utility functions and classes.
  8. def action(parent, text, slot=None, shortcut=None, icon=None,
  9. tip=None, checkable=False):
  10. """Create a new action and assign callbacks, shortcuts, etc."""
  11. a = QAction(text, parent)
  12. if icon is not None:
  13. a.setIcon(QIcon(u':/%s' % icon))
  14. if shortcut is not None:
  15. a.setShortcut(shortcut)
  16. if tip is not None:
  17. a.setToolTip(tip)
  18. a.setStatusTip(tip)
  19. if slot is not None:
  20. a.triggered.connect(slot)
  21. if checkable:
  22. a.setCheckable(True)
  23. return a
  24. def add_actions(widget, actions):
  25. for action in actions:
  26. if action is None:
  27. widget.addSeparator()
  28. else:
  29. widget.addAction(action)
  30. class WindowMixin(object):
  31. def menu(self, title, actions=None):
  32. menu = self.menuBar().addMenu(title)
  33. if actions:
  34. add_actions(menu, actions)
  35. return menu
  36. def toolbar(self, title, actions=None):
  37. toolbar = QToolBar(title)
  38. toolbar.setObjectName(u'%sToolBar' % title)
  39. #toolbar.setOrientation(Qt.Vertical)
  40. toolbar.setContentsMargins(0,0,0,0)
  41. toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
  42. toolbar.layout().setContentsMargins(0,0,0,0)
  43. if actions:
  44. add_actions(toolbar, actions)
  45. self.addToolBar(Qt.LeftToolBarArea, toolbar)
  46. return toolbar
  47. class MainWindow(QMainWindow, WindowMixin):
  48. def __init__(self):
  49. super(MainWindow, self).__init__()
  50. self.setWindowTitle(__appname__)
  51. self.setContentsMargins(0, 0, 0, 0)
  52. # Main widgets.
  53. self.label = QLineEdit(u'Hello world, مرحبا ، العالم, Γεια σου κόσμε!')
  54. self.dock = QDockWidget(u'Label', parent=self)
  55. self.dock.setWidget(self.label)
  56. self.addDockWidget(Qt.BottomDockWidgetArea, self.dock)
  57. self.setCentralWidget(QWidget())
  58. # Actions
  59. quit = action(self, '&Quit', self.close, 'Ctrl+Q', u'Exit application')
  60. labl = self.dock.toggleViewAction()
  61. labl.setShortcut('Ctrl+L')
  62. add_actions(self.menu('&File'), (labl, None, quit))
  63. add_actions(self.toolbar('Tools'), (labl, None, quit,))
  64. self.statusBar().showMessage('%s started.' % __appname__)
  65. self.statusBar().show()
  66. def main(argv):
  67. """Standard boilerplate Qt application code."""
  68. app = QApplication(argv)
  69. app.setApplicationName(__appname__)
  70. win = MainWindow()
  71. win.show()
  72. return app.exec_()
  73. if __name__ == '__main__':
  74. sys.exit(main(sys.argv))