| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | #!/usr/bin/env python# -*- coding: utf8 -*-import os.pathimport reimport sysfrom functools import partialfrom collections import defaultdictfrom PyQt4.QtGui import *from PyQt4.QtCore import *import resourcesfrom shape import Shapefrom canvas import Canvasfrom zoomwidget import ZoomWidget__appname__ = 'labelme'# TODO:# - Zoom is too "steppy".### 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.zoom_widget = ZoomWidget()        #self.dock.setFeatures(QDockWidget.DockWidgetMovable|QDockWidget.DockWidgetFloatable)        self.canvas = Canvas()        #self.canvas.setAlignment(Qt.AlignCenter)        self.canvas.setContextMenuPolicy(Qt.ActionsContextMenu)        self.canvas.zoomRequest.connect(self.zoomRequest)        scroll = QScrollArea()        scroll.setWidget(self.canvas)        scroll.setWidgetResizable(True)        self.scrollBars = {            Qt.Vertical: scroll.verticalScrollBar(),            Qt.Horizontal: scroll.horizontalScrollBar()            }        self.canvas.scrollRequest.connect(self.scrollRequest)        self.setCentralWidget(scroll)        self.addDockWidget(Qt.BottomDockWidgetArea, self.dock)        # Actions        quit = action(self, '&Quit', self.close,                'Ctrl+Q', 'quit', u'Exit application')        open = action(self, '&Open', self.openFile,                'Ctrl+O', 'open', u'Open file')        color = action(self, '&Color', self.chooseColor,                'Ctrl+C', 'color', u'Choose line color')        label = action(self,'&New Label', self.newLabel,                'Ctrl+N', 'new', u'Add new label')        delete = action(self,'&delete', self.deleteSelectedShape,                'Ctrl+D', 'delete', u'Delete')        labl = self.dock.toggleViewAction()        labl.setShortcut('Ctrl+L')        zoom = QWidgetAction(self)        zoom.setDefaultWidget(self.zoom_widget)        fit_window = action(self, '&Fit Window', self.setFitWindow,                'Ctrl+F', 'fit',  u'Fit image to window', checkable=True)        self.menus = struct(                file=self.menu('&File'),                edit=self.menu('&Image'),                view=self.menu('&View'))        add_actions(self.menus.file, (open, quit))        add_actions(self.menus.edit, (label, color, fit_window))        add_actions(self.menus.view, (labl,))        self.tools = self.toolbar('Tools')        add_actions(self.tools, (open, color, None, label, delete, None,            zoom, fit_window, 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        self.zoom_level = 100        self.fit_window = False        # 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))        # Callbacks:        self.zoom_widget.editingFinished.connect(self.paintCanvas)    ## Callback functions:    def scrollRequest(self, delta, orientation):        units = - delta / (8 * 15)        bar = self.scrollBars[orientation]        bar.setValue(bar.value() + bar.singleStep() * units)    def zoomRequest(self, delta):        if not self.fit_window:            units = delta / (8 * 15)            scale = 10            self.zoom_widget.setValue(self.zoom_widget.value() + scale * units)            self.zoom_widget.editingFinished.emit()    def setFitWindow(self, value=True):        self.zoom_widget.setEnabled(not value)        self.fit_window = value        self.paintCanvas()    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.loadPixmap()            self.statusBar().showMessage(message)    def resizeEvent(self, event):        if self.fit_window and self.canvas and not self.image.isNull():            self.paintCanvas()        super(MainWindow, self).resizeEvent(event)    def loadPixmap(self):        assert not self.image.isNull(), "cannot load null image"        self.canvas.pixmap = QPixmap.fromImage(self.image)    def paintCanvas(self):        assert not self.image.isNull(), "cannot paint null image"        self.canvas.scale = self.fitSize() if self.fit_window\                            else 0.01 * self.zoom_widget.value()        self.canvas.adjustSize()        self.canvas.repaint()    def fitSize(self):        """Figure out the size of the pixmap in order to fit the main widget."""        e = 2.0 # So that no scrollbars are generated.        w1 = self.centralWidget().width() - e        h1 = self.centralWidget().height() - e        a1 = w1/ h1        # Calculate a new scale value based on the pixmap's aspect ratio.        w2 = self.canvas.pixmap.width() - 0.0        h2 = self.canvas.pixmap.height() - 0.0        a2 = w2 / h2        return w1 / w2 if a2 >= a1 else h1 / h2    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)        # Change the color for all shape lines:        Shape.line_color = self.color        self.canvas.repaint()    def newLabel(self):        self.canvas.deSelectShape()        self.canvas.startLabeling=True    def deleteSelectedShape(self):        self.canvas.deleteSelected()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 valueclass struct(object):    def __init__(self, **kwargs):        self.__dict__.update(kwargs)def 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))
 |