| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 | #!/usr/bin/env python# -*- coding: utf8 -*-import os.pathimport reimport sysfrom functools import partialfrom collections import defaultdictfrom PyQt4.QtGui import *from PyQt4.QtCore import *from canvas import Canvas__appname__ = 'labelme'### Utility functions and classes.def action(parent, text, slot=None, shortcut=None, icon=None,           tip=None, checkable=False):    """Create a new action and assign callbacks, shortcuts, etc."""    a = QAction(text, parent)    if icon is not None:        a.setIcon(QIcon(u':/%s' % icon))    if shortcut is not None:        a.setShortcut(shortcut)    if tip is not None:        a.setToolTip(tip)        a.setStatusTip(tip)    if slot is not None:        a.triggered.connect(slot)    if checkable:        a.setCheckable(True)    return adef add_actions(widget, actions):    for action in actions:        if action is None:            widget.addSeparator()        else:            widget.addAction(action)class WindowMixin(object):    def menu(self, title, actions=None):        menu = self.menuBar().addMenu(title)        if actions:            add_actions(menu, actions)        return menu    def toolbar(self, title, actions=None):        toolbar = QToolBar(title)        toolbar.setObjectName(u'%sToolBar' % title)        #toolbar.setOrientation(Qt.Vertical)        toolbar.setContentsMargins(0,0,0,0)        toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)        toolbar.layout().setContentsMargins(0,0,0,0)        if actions:            add_actions(toolbar, actions)        self.addToolBar(Qt.LeftToolBarArea, toolbar)        return toolbarclass MainWindow(QMainWindow, WindowMixin):    def __init__(self, filename=None):        super(MainWindow, self).__init__()        self.setWindowTitle(__appname__)        self.setContentsMargins(0, 0, 0, 0)        # Main widgets.        self.label = QLineEdit(u'Hello world, مرحبا ، العالم, Γεια σου κόσμε!')        self.dock = QDockWidget(u'Label', parent=self)        self.dock.setObjectName(u'Label')        self.dock.setWidget(self.label)        #self.dock.setFeatures(QDockWidget.DockWidgetMovable|QDockWidget.DockWidgetFloatable)        self.imageWidget = Canvas()        self.imageWidget.setAlignment(Qt.AlignCenter)        self.imageWidget.setContextMenuPolicy(Qt.ActionsContextMenu)        self.addDockWidget(Qt.BottomDockWidgetArea, self.dock)        self.setCentralWidget(self.imageWidget)        # Actions        quit = action(self, '&Quit', self.close, 'Ctrl+Q', u'Exit application')        open = action(self, '&Open', self.openFile, 'Ctrl+O', u'Open file')        color = action(self, '&Color', self.chooseColor, 'Ctrl+C', u'Choose line color')        labl = self.dock.toggleViewAction()        labl.setShortcut('Ctrl+L')        add_actions(self.menu('&File'), (open, color, None, labl, None, quit))        add_actions(self.toolbar('Tools'), (open, color, None, labl, None, quit))        self.statusBar().showMessage('%s started.' % __appname__)        self.statusBar().show()        # Application state.        self.image = QImage()        self.filename = filename        self.recent_files = []        self.color = None        # TODO: Could be completely declarative.        # Restore application settings.        types = {            'filename': QString,            'recent-files': QStringList,            'window/size': QSize,            'window/position': QPoint,            'window/geometry': QByteArray,            # Docks and toolbars:            'window/state': QByteArray,        }        self.settings = settings = Settings(types)        self.recent_files = settings['recent-files']        size = settings.get('window/size', QSize(600, 500))        position = settings.get('window/position', QPoint(0, 0))        self.resize(size)        self.move(position)        # or simply:        #self.restoreGeometry(settings['window/geometry']        self.restoreState(settings['window/state'])        self.color = QColor(settings.get('line/color', QColor(0, 255, 0, 128)))        # The file menu has default dynamically generated entries.        self.updateFileMenu()        # Since loading the file may take some time, make sure it runs in the background.        self.queueEvent(partial(self.loadFile, self.filename))    def queueEvent(self, function):        QTimer.singleShot(0, function)    def loadFile(self, filename=None):        """Load the specified file, or the last opened file if None."""        if filename is None:            filename = self.settings['filename']        # FIXME: Load the actual file here.        if QFile.exists(filename):            # Load image            image = QImage(filename)            if image.isNull():                message = "Failed to read %s" % filename            else:                message = "Loaded %s" % os.path.basename(unicode(filename))                self.image = image                self.filename = filename                self.showImage()            self.statusBar().showMessage(message)    def showImage(self):        if self.image.isNull():            return        self.imageWidget.setPixmap(self.scaled(QPixmap.fromImage(self.image)))        self.imageWidget.show()    def resizeEvent(self, event):        if self.imageWidget and self.imageWidget.pixmap():            self.imageWidget.setPixmap(self.scaled(self.imageWidget.pixmap()))        super(MainWindow, self).resizeEvent(event)    def scaled(self, pixmap):        width = self.centralWidget().width()        height = self.centralWidget().height()        return pixmap.scaled(width, height,          Qt.KeepAspectRatio, Qt.SmoothTransformation)    def closeEvent(self, event):        # TODO: Make sure changes are saved.        s = self.settings        s['filename'] = self.filename if self.filename else QString()        s['window/size'] = self.size()        s['window/position'] = self.pos()        s['window/state'] = self.saveState()        s['line/color'] = self.color        #s['window/geometry'] = self.saveGeometry()    def updateFileMenu(self):        """Populate menu with recent files."""    ## Dialogs.    def openFile(self):        if not self.check():            return        path = os.path.dirname(unicode(self.filename))\                if self.filename else '.'        formats = ['*.%s' % unicode(fmt).lower()\                for fmt in QImageReader.supportedImageFormats()]        filename = unicode(QFileDialog.getOpenFileName(self,            '%s - Choose Image', path, 'Image files (%s)' % ' '.join(formats)))        if filename:            self.loadFile(filename)    def check(self):        # TODO: Prompt user to save labels etc.        return True    def chooseColor(self):        self.color = QColorDialog.getColor(self.color, self,                u'Choose line color', QColorDialog.ShowAlphaChannel)class Settings(object):    """Convenience dict-like wrapper around QSettings."""    def __init__(self, types=None):        self.data = QSettings()        self.types = defaultdict(lambda: QVariant, types if types else {})    def __setitem__(self, key, value):        t = self.types[key]        self.data.setValue(key,                t(value) if not isinstance(value, t) else value)    def __getitem__(self, key):        return self._cast(key, self.data.value(key))    def get(self, key, default=None):        return self._cast(key, self.data.value(key, default))    def _cast(self, key, value):        # XXX: Very nasty way of converting types to QVariant methods :P        t = self.types[key]        if t != QVariant:            method = getattr(QVariant, re.sub('^Q', 'to', t.__name__, count=1))            return method(value)        return valuedef main(argv):    """Standard boilerplate Qt application code."""    app = QApplication(argv)    app.setApplicationName(__appname__)    win = MainWindow(argv[1] if len(argv) == 2 else None)    win.show()    return app.exec_()if __name__ == '__main__':    sys.exit(main(sys.argv))
 |