Ejemplo n.º 1
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        super(MainWin, self).__init__(parent, flags)

        self.mousemanager = MouseManager(self)

        from gsdview.mousemanager import RubberBandMode
        rubberbandmode = RubberBandMode()
        self.mousemanager.addMode(rubberbandmode)

        def callback(rect):
            print 'rect', rect

        rubberbandmode.rubberBandSeclection.connect(callback)
        #lambda r: sys.stdout.write(str(r)+'\n'))

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager.register(self.graphicsview)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('File', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(700, 500)
Ejemplo n.º 2
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        super(MainWin, self).__init__(parent, flags)

        self.mousemanager = MouseManager(self)

        from gsdview.mousemanager import RubberBandMode
        rubberbandmode = RubberBandMode()
        self.mousemanager.addMode(rubberbandmode)

        def callback(rect):
            print 'rect', rect

        rubberbandmode.rubberBandSeclection.connect(callback)
                                    #lambda r: sys.stdout.write(str(r)+'\n'))

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager.register(self.graphicsview)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('File', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(700, 500)
Ejemplo n.º 3
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtWidgets.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.model = QtGui.QStandardItemModel(self)
        self.model.setColumnCount(2)

        self.treeview = QtWidgets.QTreeView()
        self.treeview.setModel(self.model)
        self.treeview.setHeaderHidden(True)
        self.treeview.setIndentation(0)
        self.treeview.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection)
        self.treeview.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.treeview.header().setStretchLastSection(True)

        self.treedock = QtWidgets.QDockWidget(self.tr('Layers View'), self)
        self.treedock.setWidget(self.treeview)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treedock)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsview = QtWidgets.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.mode = 'hand'

        self.layermanager = LayerManager(self.treeview, self)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtWidgets.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtWidgets.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtWidgets.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtWidgets.QMenu('View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Layer Actions
        layeractions = self.layermanager.actions

        menu = QtWidgets.QMenu('Layer', self)
        menu.addActions(layeractions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Layer toolbar')
        toolbar.addActions(layeractions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtWidgets.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(900, 500)
        self.reset()
        self.statusBar().showMessage('Ready')
Ejemplo n.º 4
0
class VectorGraphicsApp(QtWidgets.QMainWindow):
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtWidgets.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.model = QtGui.QStandardItemModel(self)
        self.model.setColumnCount(2)

        self.treeview = QtWidgets.QTreeView()
        self.treeview.setModel(self.model)
        self.treeview.setHeaderHidden(True)
        self.treeview.setIndentation(0)
        self.treeview.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection)
        self.treeview.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.treeview.header().setStretchLastSection(True)

        self.treedock = QtWidgets.QDockWidget(self.tr('Layers View'), self)
        self.treedock.setWidget(self.treeview)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treedock)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsview = QtWidgets.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.mode = 'hand'

        self.layermanager = LayerManager(self.treeview, self)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtWidgets.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtWidgets.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtWidgets.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtWidgets.QMenu('View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Layer Actions
        layeractions = self.layermanager.actions

        menu = QtWidgets.QMenu('Layer', self)
        menu.addActions(layeractions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Layer toolbar')
        toolbar.addActions(layeractions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtWidgets.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(900, 500)
        self.reset()
        self.statusBar().showMessage('Ready')

    def _setupFileActions(self):
        style = self.style()

        actions = QtWidgets.QActionGroup(self)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogOpenButton)
        QtWidgets.QAction(icon,
                          self.tr('Open Vector'),
                          actions,
                          objectName='openVectorAction',
                          statusTip=self.tr('Open Vector'),
                          triggered=self.onOpenVector)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogResetButton)
        QtWidgets.QAction(icon,
                          self.tr('Close All'),
                          actions,
                          objectName='claseAllAction',
                          statusTip=self.tr('Close All'),
                          triggered=self.reset)

        QtWidgets.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogCancelButton)
        QtWidgets.QAction(icon,
                          self.tr('Exit'),
                          actions,
                          objectName='exitAction',
                          statusTip=self.tr('Exit'),
                          triggered=self.close)

        return actions

    def _setupViewActions(self):
        actions = QtWidgets.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-in-32.png')
        QtWidgets.QAction(icon,
                          self.tr('Zoom In'),
                          actions,
                          objectName='zoomInAction',
                          statusTip=self.tr('Zoom In'),
                          shortcut=self.tr('Ctrl++'),
                          triggered=lambda: self.graphicsview.scale(1.2, 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-out-32.png')
        QtWidgets.QAction(
            icon,
            self.tr('Zoom Out'),
            actions,
            objectName='zoomOutAction',
            statusTip=self.tr('Zoom Out'),
            shortcut=self.tr('Ctrl+-'),
            triggered=lambda: self.graphicsview.scale(1 / 1.2, 1 / 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/page-setup-24.png')
        QtWidgets.QAction(icon,
                          self.tr('Zoom 1:1'),
                          actions,
                          objectName='zoomResetAction',
                          statusTip=self.tr('Zoom 1:1'),
                          triggered=lambda: self.graphicsview.setTransform(
                              QtGui.QTransform(1, 0, 0, -1, 0, 0)))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/fit-page-32.png')
        QtWidgets.QAction(
            icon,
            self.tr('Zoom Fit'),
            actions,
            objectName='zoomFitAction',
            statusTip=self.tr('Zoom Fit'),
            #checkable=True,
            triggered=lambda: self.graphicsview.fitInView(
                self.graphicsview.sceneRect(), QtCore.Qt.KeepAspectRatio))

        return actions

    def _setupHelpActions(self):
        actions = QtWidgets.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtWidgets.QAction(icon,
                          self.tr('About'),
                          actions,
                          objectName='aboutAction',
                          statusTip=self.tr('About'),
                          triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtWidgets.QAction(icon,
                          self.tr('About Qt'),
                          actions,
                          objectName='aboutQtAction',
                          statusTip=self.tr('About Qt'),
                          triggered=QtWidgets.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def about(self):
        title = self.tr('MouseManager Example')
        text = [
            '<h1>Mouse Manager</h1>'
            '<p>Example program for the OGR proxy components.</p>',
            '<p>Copyright (C): 2009-2015 '
            '<a href="mailto:[email protected]">Antonio Valentino<a>.'
            '</p>'
        ]
        text = self.tr('\n'.join(text))
        QtWidgets.QMessageBox.about(self, title, text)

    @QtCore.Slot()
    def reset(self):
        self.scene.clear()
        self.graphicsview.resetTransform()
        self.graphicsview.scale(1., -1.)
        self.model.clear()

    @QtCore.Slot()
    def onOpenVector(self):
        ogrFilters = [
            'All files (*)',
            'ESRI Shapefiles (*.shp)',
            'KML (*.kml, *.kmz)',
        ]
        filenames, filter_ = QtWidgets.QFileDialog.getOpenFileNamesAndFilter(
            self, self.tr('Open Vector'),
            QtCore.QDir.home().absolutePath(), ';;'.join(ogrFilters),
            ogrFilters[1])

        for filename in filenames:
            filename = str(filename)  # unicode --> str
            # @TODO: check if it is already open
            self.openvector(filename)

    def openvector(self, filename):
        # @TODO: check limits
        MAX_LAYER_COUNT = 10

        srs = None
        transform = None
        affine_transform = None

        # @TODO: remove
        #srs = osr.SpatialReference()
        #srs.SetLCC(20, 20, 20, 0, 0, 0)
        ##srs.SetUTM(33)

        # @TODO: remove
        #~ from math import sin, cos, radians
        #~ a = radians(-45)
        #~ qtransform = QtGui.QTransform(
        #~ cos(a), -sin(a), sin(a), cos(a), 0, 0)
        #~ #affine_transform = qtransform
        #~ transform = lambda x, y, z: qtransform.map(x, y)

        ds = ogr.Open(filename)
        if ds is None:
            raise ValueError('unable to open "$s"' % filename)

        if ds.GetLayerCount() > MAX_LAYER_COUNT:
            raise RuntimeError('too many layers: %d' % ds.GetLayerCount())

        for index, layer in enumerate(ds):
            qlayer = ogrqt.layerToGraphicsItem(layer, srs, transform)
            #qlayer.datasource = ds.GetName()
            #qlayer.index = index
            qlayer.setData(ogrqt.DATAKEY['datasource'], ds.GetName())
            qlayer.setData(ogrqt.DATAKEY['index'], index)

            if affine_transform:
                qlayer.setTransform(affine_transform)

            if qlayer.childItems():
                self.scene.addItem(qlayer)

                item = QtGui.QStandardItem(layer.GetName())
                item.setCheckable(True)
                item.setCheckState(QtCore.Qt.Checked)
                item.setData(qlayer)
                item.setToolTip(
                    self.tr('Layer "%s": %d features.' %
                            (layer.GetName(), len(qlayer.childItems()))))

                self.model.appendRow(item)

        self.treeview.resizeColumnToContents(0)

    def _autocolor(self):
        COLORS = (
            #QtCore.Qt.white,
            QtCore.Qt.black,
            QtCore.Qt.red,
            QtCore.Qt.darkRed,
            QtCore.Qt.green,
            QtCore.Qt.darkGreen,
            QtCore.Qt.blue,
            QtCore.Qt.darkBlue,
            QtCore.Qt.cyan,
            QtCore.Qt.darkCyan,
            QtCore.Qt.magenta,
            QtCore.Qt.darkMagenta,
            QtCore.Qt.yellow,
            QtCore.Qt.darkYellow,
            QtCore.Qt.gray,
            QtCore.Qt.darkGray,
            QtCore.Qt.lightGray,
            #QtCore.Qt.transparent,
            #QtCore.Qt.color0,
            #QtCore.Qt.color1,
        )

        ncolors = len(COLORS)
        nrows = self.model.rowCount()
        for row in range(nrows):
            color = COLORS[row % ncolors]

            qlayer = self.model.item(row).data()

            #~ pen = qlayer.pen()
            #~ pen.setColor(color)
            #~ #pen.setWidth(1)
            #~ qlayer.setPen(pen)

            brush = qlayer.brush()
            brush.setColor(color)
            brush.setStyle(QtCore.Qt.SolidPattern)
            qlayer.setBrush(brush)
Ejemplo n.º 5
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        #~ self.graphicsview.setMouseTracking(True)    # @TODO: check
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.addMode(RubberBandMode)
        self.mousemanager.addMode(qt4draw.DrawPointMode)
        self.mousemanager.addMode(qt4draw.DrawLineMode)
        #self.mousemanager.addMode(qt4draw.DrawPolygonMode)
        self.mousemanager.addMode(qt4draw.DrawRectMode)
        self.mousemanager.addMode(qt4draw.DrawEllipseMode)
        self.mousemanager.mode = 'hand'

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('&File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('&View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('&Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.scene.setSceneRect(0, 0, 800, 600)
        self.statusBar().showMessage('Ready')
Ejemplo n.º 6
0
class GraphicsDrawApp(QtGui.QMainWindow):
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        #~ self.graphicsview.setMouseTracking(True)    # @TODO: check
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.addMode(RubberBandMode)
        self.mousemanager.addMode(qt4draw.DrawPointMode)
        self.mousemanager.addMode(qt4draw.DrawLineMode)
        #self.mousemanager.addMode(qt4draw.DrawPolygonMode)
        self.mousemanager.addMode(qt4draw.DrawRectMode)
        self.mousemanager.addMode(qt4draw.DrawEllipseMode)
        self.mousemanager.mode = 'hand'

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('&File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('&View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('&Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.scene.setSceneRect(0, 0, 800, 600)
        self.statusBar().showMessage('Ready')

    def _setupFileActions(self):
        style = self.style()

        actions = QtGui.QActionGroup(self)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogOpenButton)
        QtGui.QAction(icon, self.tr('&Open'), actions,
                      objectName='openAction',
                      shortcut=self.tr('Ctrl+O'),
                      statusTip=self.tr('Open'),
                      triggered=self.onOpen)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogSaveButton)
        QtGui.QAction(icon, self.tr('&Save As'), actions,
                      objectName='saveAsAction',
                      shortcut=self.tr('Ctrl+S'),
                      statusTip=self.tr('Save as'),
                      triggered=self.onSave)

        icon = QtGui.QIcon(
                ':/trolltech/dialogs/qprintpreviewdialog/images/print-32.png')
        QtGui.QAction(icon, self.tr('&Print'), actions,
                      objectName='printAction',
                      shortcut=self.tr('Ctrl+P'),
                      statusTip=self.tr('Print'),
                      triggered=self.onPrint)

        QtGui.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogCancelButton)
        QtGui.QAction(icon, self.tr('&Quit'), actions,
                      objectName='exitAction',
                      shortcut=self.tr('Ctrl+Q'),
                      statusTip=self.tr('Quit'),
                      triggered=self.close)

        return actions

    def _setupViewActions(self):
        style = self.style()
        actions = QtGui.QActionGroup(self)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogResetButton)
        QtGui.QAction(icon, self.tr('Reset'), actions,
                      objectName='resetAction',
                      statusTip=self.tr('Reset'),
                      triggered=self.onReset)

        QtGui.QAction(actions).setSeparator(True)

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-in-32.png')
        QtGui.QAction(icon, self.tr('Zoom In'), actions,
                      objectName='zoomInAction',
                      statusTip=self.tr('Zoom In'),
                      shortcut=self.tr('Ctrl++'),
                      triggered=lambda: self.graphicsview.scale(1.2, 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-out-32.png')
        QtGui.QAction(icon, self.tr('Zoom Out'), actions,
                      objectName='zoomOutAction',
                      statusTip=self.tr('Zoom Out'),
                      shortcut=self.tr('Ctrl+-'),
                      triggered=lambda: self.graphicsview.scale(1 / 1.2,
                                                                1 / 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/page-setup-24.png')
        QtGui.QAction(icon, self.tr('Zoom 1:1'), actions,
                      objectName='zoomResetAction',
                      statusTip=self.tr('Zoom 1:1'),
                      triggered=lambda: self.graphicsview.setMatrix(
                                            QtGui.QMatrix(1, 0, 0, -1, 0, 0)))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/fit-page-32.png')
        QtGui.QAction(icon, self.tr('Zoom Fit'), actions,
                      objectName='zoomFitAction',
                      statusTip=self.tr('Zoom Fit'),
                      #checkable=True,
                      triggered=lambda: self.graphicsview.fitInView(
                                                self.graphicsview.sceneRect(),
                                                QtCore.Qt.KeepAspectRatio))

        return actions

    def _setupHelpActions(self):
        actions = QtGui.QActionGroup(self)

        icon = QtGui.QIcon(
                    ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtGui.QAction(icon, self.tr('About'), actions,
                      objectName='aboutAction',
                      statusTip=self.tr('About'),
                      triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtGui.QAction(icon, self.tr('About Qt'), actions,
                      objectName='aboutQtAction',
                      statusTip=self.tr('About Qt'),
                      triggered=QtGui.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def about(self):
        title = self.tr('Qt4 Graphics Draw Example')
        text = ['<h1>Graphics Draw</h1>'
                '<p>Example program for the basic Qt4 graphics drawing.</p>',
                '<p>Copyright (C): 2010-2012 '
                '<a href="mailto:[email protected]">'
                    'Antonio Valentino<a>.</p>']
        text = self.tr('\n'.join(text))
        QtGui.QMessageBox.about(self, title, text)

    @QtCore.Slot()
    def reset(self):
        self.scene.clear()
        self.scene.setSceneRect(0, 0, 800, 600)
        self.graphicsview.setTransform(QtGui.QTransform())

    @QtCore.Slot()
    def onReset(self):
        ret = QtGui.QMessageBox.question(self,
                self.tr('Reset'),
                self.tr('Are you sure you want to reset the document?\n'
                        'All changes will be lost.'))
        if ret == QtGui.QMessageBox.Ok:
            self.reset()

    @QtCore.Slot()
    def onOpen(self):
        filters = [
            self.tr('All files (*)'),
        ]
        filters.extend('%s file (*.%s)' % (str(f).upper(), str(f))
                        for f in QtGui.QImageReader.supportedImageFormats())

        filename, filter_ = QtGui.QFileDialog.getOpenFileNameAndFilter(
                                        self,
                                        self.tr('Load picture'),
                                        QtCore.QDir.home().absolutePath(),
                                        ';;'.join(filters))  # , filters[1])
        if filename:
            if '.svg' in filename:
                item = QtSvg.QGraphicsSvgItem(filename)
            else:
                image = QtGui.QImage(filename)
                item = QtGui.QGraphicsPixmapItem(image)

            item.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
            item.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)

            self.scene.addItem(item)

    @QtCore.Slot()
    def onSave(self):
        qt4support.imgexport(self.scene, self)

    @QtCore.Slot()
    def onPrint(self):
        qt4support.printObject(self.scene, parent=self)
Ejemplo n.º 7
0
class MainWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        super(MainWin, self).__init__(parent, flags)

        self.mousemanager = MouseManager(self)

        from gsdview.mousemanager import RubberBandMode
        rubberbandmode = RubberBandMode()
        self.mousemanager.addMode(rubberbandmode)

        def callback(rect):
            print('rect', rect)

        rubberbandmode.rubberBandSeclection.connect(callback)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsview = QtWidgets.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager.register(self.graphicsview)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtWidgets.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('File', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtWidgets.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Mouse')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtWidgets.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Help', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(700, 500)

    def _setupFileActions(self):
        style = self.style()

        actions = QtWidgets.QActionGroup(self)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogOpenButton)
        QtWidgets.QAction(icon, 'Open', actions, triggered=self.openfile)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogCloseButton)
        QtWidgets.QAction(icon, 'Close', actions, triggered=self.scene.clear)

        QtWidgets.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogCancelButton)
        QtWidgets.QAction(icon, 'Exit', actions, triggered=self.close)

        return actions

    def _setupHelpActions(self):
        actions = QtWidgets.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtWidgets.QAction(icon, 'About', actions, triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtWidgets.QAction(icon,
                          'About Qt',
                          actions,
                          triggered=QtWidgets.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def openfile(self):
        self.scene.clear()
        self.graphicsview.setTransform(QtGui.QTransform())
        filename = QtWidgets.QFileDialog.getOpenFileName()
        if filename:
            pixmap = QtGui.QPixmap(filename)
            item = self.scene.addPixmap(pixmap)
            self.scene.setSceneRect(item.boundingRect())

    @QtCore.Slot()
    def about(self):
        title = self.tr('MouseManager Example')
        text = [
            '<h1>Mouse Manager</h1>'
            '<p>Example program for the Mouse manager component.</p>',
            '<p>Copyright (C): 2009-2015 '
            '<a href="mailto:[email protected]">'
            'Antonio Valentino'
            '<a>.</p>'
        ]
        text = self.tr('\n'.join(text))
        QtWidgets.QMessageBox.about(self, title, text)
Ejemplo n.º 8
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.model = QtGui.QStandardItemModel(self)
        self.model.setColumnCount(2)

        self.treeview = QtGui.QTreeView()
        self.treeview.setModel(self.model)
        self.treeview.setHeaderHidden(True)
        self.treeview.setIndentation(0)
        self.treeview.setSelectionMode(
            QtGui.QAbstractItemView.ExtendedSelection)
        self.treeview.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.treeview.header().setStretchLastSection(True)

        self.treedock = QtGui.QDockWidget(self.tr('Layers View'), self)
        self.treedock.setWidget(self.treeview)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treedock)

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.mode = 'hand'

        self.layermanager = LayerManager(self.treeview, self)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Layer Actions
        layeractions = self.layermanager.actions

        menu = QtGui.QMenu('Layer', self)
        menu.addActions(layeractions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Layer toolbar')
        toolbar.addActions(layeractions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(900, 500)
        self.reset()
        self.statusBar().showMessage('Ready')
Ejemplo n.º 9
0
class VectorGraphicsApp(QtGui.QMainWindow):
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.model = QtGui.QStandardItemModel(self)
        self.model.setColumnCount(2)

        self.treeview = QtGui.QTreeView()
        self.treeview.setModel(self.model)
        self.treeview.setHeaderHidden(True)
        self.treeview.setIndentation(0)
        self.treeview.setSelectionMode(
            QtGui.QAbstractItemView.ExtendedSelection)
        self.treeview.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.treeview.header().setStretchLastSection(True)

        self.treedock = QtGui.QDockWidget(self.tr('Layers View'), self)
        self.treedock.setWidget(self.treeview)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treedock)

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.mode = 'hand'

        self.layermanager = LayerManager(self.treeview, self)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Layer Actions
        layeractions = self.layermanager.actions

        menu = QtGui.QMenu('Layer', self)
        menu.addActions(layeractions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Layer toolbar')
        toolbar.addActions(layeractions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(900, 500)
        self.reset()
        self.statusBar().showMessage('Ready')

    def _setupFileActions(self):
        style = self.style()

        actions = QtGui.QActionGroup(self)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogOpenButton)
        QtGui.QAction(icon, self.tr('Open Vector'), actions,
                      objectName='openVectorAction',
                      statusTip=self.tr('Open Vector'),
                      triggered=self.onOpenVector)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogResetButton)
        QtGui.QAction(icon, self.tr('Close All'), actions,
                      objectName='claseAllAction',
                      statusTip=self.tr('Close All'),
                      triggered=self.reset)

        QtGui.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogCancelButton)
        QtGui.QAction(icon, self.tr('Exit'), actions,
                      objectName='exitAction',
                      statusTip=self.tr('Exit'),
                      triggered=self.close)

        return actions

    def _setupViewActions(self):
        actions = QtGui.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-in-32.png')
        QtGui.QAction(icon, self.tr('Zoom In'), actions,
                      objectName='zoomInAction',
                      statusTip=self.tr('Zoom In'),
                      shortcut=self.tr('Ctrl++'),
                      triggered=lambda: self.graphicsview.scale(1.2, 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-out-32.png')
        QtGui.QAction(icon, self.tr('Zoom Out'), actions,
                      objectName='zoomOutAction',
                      statusTip=self.tr('Zoom Out'),
                      shortcut=self.tr('Ctrl+-'),
                      triggered=lambda: self.graphicsview.scale(1 / 1.2,
                                                                1 / 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/page-setup-24.png')
        QtGui.QAction(icon, self.tr('Zoom 1:1'), actions,
                      objectName='zoomResetAction',
                      statusTip=self.tr('Zoom 1:1'),
                      triggered=lambda: self.graphicsview.setMatrix(
                                            QtGui.QMatrix(1, 0, 0, -1, 0, 0)))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/fit-page-32.png')
        QtGui.QAction(icon, self.tr('Zoom Fit'), actions,
                      objectName='zoomFitAction',
                      statusTip=self.tr('Zoom Fit'),
                      #checkable=True,
                      triggered=lambda: self.graphicsview.fitInView(
                                                self.graphicsview.sceneRect(),
                                                QtCore.Qt.KeepAspectRatio))

        return actions

    def _setupHelpActions(self):
        actions = QtGui.QActionGroup(self)

        icon = QtGui.QIcon(
                    ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtGui.QAction(icon, self.tr('About'), actions,
                      objectName='aboutAction',
                      statusTip=self.tr('About'),
                      triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtGui.QAction(icon, self.tr('About Qt'), actions,
                      objectName='aboutQtAction',
                      statusTip=self.tr('About Qt'),
                      triggered=QtGui.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def about(self):
        title = self.tr('MouseManager Example')
        text = ['<h1>Mouse Manager</h1>'
                '<p>Example program for the OGR proxy components.</p>',
                '<p>Copyright (C): 2009-2011 '
                '<a href="mailto:[email protected]">'
                    'Antonio Valentino'
                '<a>.</p>']
        text = self.tr('\n'.join(text))
        QtGui.QMessageBox.about(self, title, text)

    @QtCore.Slot()
    def reset(self):
        self.scene.clear()
        self.graphicsview.resetTransform()
        self.graphicsview.scale(1., -1.)
        self.model.clear()

    @QtCore.Slot()
    def onOpenVector(self):
        ogrFilters = [
            'All files (*)',
            'ESRI Shapefiles (*.shp)',
            'KML (*.kml, *.kmz)',
        ]
        filenames, filter_ = QtGui.QFileDialog.getOpenFileNamesAndFilter(
                                        self,
                                        self.tr('Open Vector'),
                                        QtCore.QDir.home().absolutePath(),
                                        ';;'.join(ogrFilters),
                                        ogrFilters[1])

        for filename in filenames:
            filename = str(filename)    # unicode --> str
            # @TODO: check if it is already open
            self.openvector(filename)

    def openvector(self, filename):
        # @TODO: check limits
        MAX_LAYER_COUNT = 10

        srs = None
        transform = None
        affine_transform = None

        # @TODO: remove
        #srs = osr.SpatialReference()
        #srs.SetLCC(20, 20, 20, 0, 0, 0)
        ##srs.SetUTM(33)

        # @TODO: remove
        #~ from math import sin, cos, radians
        #~ a = radians(-45)
        #~ qtransform = QtGui.QTransform(cos(a), -sin(a), sin(a), cos(a), 0, 0)
        #~ #affine_transform = qtransform
        #~ transform = lambda x, y, z: qtransform.map(x, y)

        ds = ogr.Open(filename)
        if ds is None:
            raise ValueError('unable to open "$s"' % filename)

        if ds.GetLayerCount() > MAX_LAYER_COUNT:
            raise RuntimeError('too many layers: %d' % ds.GetLayerCount())

        for index, layer in enumerate(ds):
            qlayer = ogrqt4.layerToGraphicsItem(layer, srs, transform)
            #qlayer.datasource = ds.GetName()
            #qlayer.index = index
            qlayer.setData(ogrqt4.DATAKEY['datasource'], ds.GetName())
            qlayer.setData(ogrqt4.DATAKEY['index'], index)

            if affine_transform:
                qlayer.setTransform(affine_transform)

            if qlayer.childItems():
                self.scene.addItem(qlayer)

                item = QtGui.QStandardItem(layer.GetName())
                item.setCheckable(True)
                item.setCheckState(QtCore.Qt.Checked)
                item.setData(qlayer)
                item.setToolTip(self.tr('Layer "%s": %d features.' % (
                                                    layer.GetName(),
                                                    len(qlayer.childItems()))))

                self.model.appendRow(item)

        self.treeview.resizeColumnToContents(0)

    def _autocolor(self):
        COLORS = (
            #QtCore.Qt.white,
            QtCore.Qt.black,
            QtCore.Qt.red,
            QtCore.Qt.darkRed,
            QtCore.Qt.green,
            QtCore.Qt.darkGreen,
            QtCore.Qt.blue,
            QtCore.Qt.darkBlue,
            QtCore.Qt.cyan,
            QtCore.Qt.darkCyan,
            QtCore.Qt.magenta,
            QtCore.Qt.darkMagenta,
            QtCore.Qt.yellow,
            QtCore.Qt.darkYellow,
            QtCore.Qt.gray,
            QtCore.Qt.darkGray,
            QtCore.Qt.lightGray,
            #QtCore.Qt.transparent,
            #QtCore.Qt.color0,
            #QtCore.Qt.color1,
        )

        ncolors = len(COLORS)
        nrows = self.model.rowCount()
        for row in range(nrows):
            color = COLORS[row % ncolors]

            qlayer = self.model.item(row).data()

            #~ pen = qlayer.pen()
            #~ pen.setColor(color)
            #~ #pen.setWidth(1)
            #~ qlayer.setPen(pen)

            brush = qlayer.brush()
            brush.setColor(color)
            brush.setStyle(QtCore.Qt.SolidPattern)
            qlayer.setBrush(brush)
Ejemplo n.º 10
0
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        #~ self.graphicsview.setMouseTracking(True)    # @TODO: check
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.addMode(RubberBandMode)
        self.mousemanager.addMode(qt4draw.DrawPointMode)
        self.mousemanager.addMode(qt4draw.DrawLineMode)
        #self.mousemanager.addMode(qt4draw.DrawPolygonMode)
        self.mousemanager.addMode(qt4draw.DrawRectMode)
        self.mousemanager.addMode(qt4draw.DrawEllipseMode)
        self.mousemanager.mode = 'hand'

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('&File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('&View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('&Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.scene.setSceneRect(0, 0, 800, 600)
        self.statusBar().showMessage('Ready')
Ejemplo n.º 11
0
class GraphicsDrawApp(QtGui.QMainWindow):
    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        QtGui.QMainWindow.__init__(self, parent, flags)
        self.statusBar().show()

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        #~ self.graphicsview.setMouseTracking(True)    # @TODO: check
        self.setCentralWidget(self.graphicsview)

        self.mousemanager = MouseManager(self)
        self.mousemanager.register(self.graphicsview)
        self.mousemanager.addMode(RubberBandMode)
        self.mousemanager.addMode(qt4draw.DrawPointMode)
        self.mousemanager.addMode(qt4draw.DrawLineMode)
        #self.mousemanager.addMode(qt4draw.DrawPolygonMode)
        self.mousemanager.addMode(qt4draw.DrawRectMode)
        self.mousemanager.addMode(qt4draw.DrawEllipseMode)
        self.mousemanager.mode = 'hand'

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtGui.QMenu('&File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)
        self._filemenu = menu

        toolbar = QtGui.QToolBar('File toolbar', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtGui.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Mouse toolbar')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # View Actions
        self.viewactions = self._setupViewActions()

        menu = QtGui.QMenu('&View', self)
        menu.addActions(self.viewactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('View toolbar')
        toolbar.addActions(self.viewactions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtGui.QMenu('&Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtGui.QToolBar('Help toolbar', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.scene.setSceneRect(0, 0, 800, 600)
        self.statusBar().showMessage('Ready')

    def _setupFileActions(self):
        style = self.style()

        actions = QtGui.QActionGroup(self)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogOpenButton)
        QtGui.QAction(icon,
                      self.tr('&Open'),
                      actions,
                      objectName='openAction',
                      shortcut=self.tr('Ctrl+O'),
                      statusTip=self.tr('Open'),
                      triggered=self.onOpen)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogSaveButton)
        QtGui.QAction(icon,
                      self.tr('&Save As'),
                      actions,
                      objectName='saveAsAction',
                      shortcut=self.tr('Ctrl+S'),
                      statusTip=self.tr('Save as'),
                      triggered=self.onSave)

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/print-32.png')
        QtGui.QAction(icon,
                      self.tr('&Print'),
                      actions,
                      objectName='printAction',
                      shortcut=self.tr('Ctrl+P'),
                      statusTip=self.tr('Print'),
                      triggered=self.onPrint)

        QtGui.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogCancelButton)
        QtGui.QAction(icon,
                      self.tr('&Quit'),
                      actions,
                      objectName='exitAction',
                      shortcut=self.tr('Ctrl+Q'),
                      statusTip=self.tr('Quit'),
                      triggered=self.close)

        return actions

    def _setupViewActions(self):
        style = self.style()
        actions = QtGui.QActionGroup(self)

        icon = style.standardIcon(QtGui.QStyle.SP_DialogResetButton)
        QtGui.QAction(icon,
                      self.tr('Reset'),
                      actions,
                      objectName='resetAction',
                      statusTip=self.tr('Reset'),
                      triggered=self.onReset)

        QtGui.QAction(actions).setSeparator(True)

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-in-32.png')
        QtGui.QAction(icon,
                      self.tr('Zoom In'),
                      actions,
                      objectName='zoomInAction',
                      statusTip=self.tr('Zoom In'),
                      shortcut=self.tr('Ctrl++'),
                      triggered=lambda: self.graphicsview.scale(1.2, 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/zoom-out-32.png')
        QtGui.QAction(
            icon,
            self.tr('Zoom Out'),
            actions,
            objectName='zoomOutAction',
            statusTip=self.tr('Zoom Out'),
            shortcut=self.tr('Ctrl+-'),
            triggered=lambda: self.graphicsview.scale(1 / 1.2, 1 / 1.2))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/page-setup-24.png')
        QtGui.QAction(icon,
                      self.tr('Zoom 1:1'),
                      actions,
                      objectName='zoomResetAction',
                      statusTip=self.tr('Zoom 1:1'),
                      triggered=lambda: self.graphicsview.setMatrix(
                          QtGui.QMatrix(1, 0, 0, -1, 0, 0)))

        icon = QtGui.QIcon(
            ':/trolltech/dialogs/qprintpreviewdialog/images/fit-page-32.png')
        QtGui.QAction(
            icon,
            self.tr('Zoom Fit'),
            actions,
            objectName='zoomFitAction',
            statusTip=self.tr('Zoom Fit'),
            #checkable=True,
            triggered=lambda: self.graphicsview.fitInView(
                self.graphicsview.sceneRect(), QtCore.Qt.KeepAspectRatio))

        return actions

    def _setupHelpActions(self):
        actions = QtGui.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtGui.QAction(icon,
                      self.tr('About'),
                      actions,
                      objectName='aboutAction',
                      statusTip=self.tr('About'),
                      triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtGui.QAction(icon,
                      self.tr('About Qt'),
                      actions,
                      objectName='aboutQtAction',
                      statusTip=self.tr('About Qt'),
                      triggered=QtGui.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def about(self):
        title = self.tr('Qt4 Graphics Draw Example')
        text = [
            '<h1>Graphics Draw</h1>'
            '<p>Example program for the basic Qt4 graphics drawing.</p>',
            '<p>Copyright (C): 2010-2011 '
            '<a href="mailto:[email protected]">'
            'Antonio Valentino<a>.</p>'
        ]
        text = self.tr('\n'.join(text))
        QtGui.QMessageBox.about(self, title, text)

    @QtCore.Slot()
    def reset(self):
        self.scene.clear()
        self.scene.setSceneRect(0, 0, 800, 600)
        self.graphicsview.setTransform(QtGui.QTransform())

    @QtCore.Slot()
    def onReset(self):
        ret = QtGui.QMessageBox.question(
            self, self.tr('Reset'),
            self.tr('Are you sure you want to reset the document?\n'
                    'All changes will be lost.'))
        if ret == QtGui.QMessageBox.Ok:
            self.reset()

    @QtCore.Slot()
    def onOpen(self):
        filters = [
            self.tr('All files (*)'),
        ]
        filters.extend('%s file (*.%s)' % (str(f).upper(), str(f))
                       for f in QtGui.QImageReader.supportedImageFormats())

        filename, filter_ = QtGui.QFileDialog.getOpenFileNameAndFilter(
            self, self.tr('Load picture'),
            QtCore.QDir.home().absolutePath(),
            ';;'.join(filters))  # , filters[1])
        if filename:
            if '.svg' in filename:
                item = QtSvg.QGraphicsSvgItem(filename)
            else:
                image = QtGui.QImage(filename)
                item = QtGui.QGraphicsPixmapItem(image)

            item.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
            item.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)

            self.scene.addItem(item)

    @QtCore.Slot()
    def onSave(self):
        qt4support.imgexport(self.scene, self)

    @QtCore.Slot()
    def onPrint(self):
        qt4support.printObject(self.scene, parent=self)
Ejemplo n.º 12
0
class MainWin(QtWidgets.QMainWindow):

    def __init__(self, parent=None, flags=QtCore.Qt.WindowFlags(0)):
        super(MainWin, self).__init__(parent, flags)

        self.mousemanager = MouseManager(self)

        from gsdview.mousemanager import RubberBandMode
        rubberbandmode = RubberBandMode()
        self.mousemanager.addMode(rubberbandmode)

        def callback(rect):
            print('rect', rect)

        rubberbandmode.rubberBandSeclection.connect(callback)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsview = QtWidgets.QGraphicsView(self.scene, self)
        self.setCentralWidget(self.graphicsview)

        self.mousemanager.register(self.graphicsview)

        # File Actions
        self.fileactions = self._setupFileActions()

        menu = QtWidgets.QMenu('File', self)
        menu.addActions(self.fileactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('File', self)
        toolbar.addActions(self.fileactions.actions())
        self.addToolBar(toolbar)

        # Mouse Actions
        menu = QtWidgets.QMenu('Mouse', self)
        menu.addActions(self.mousemanager.actions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Mouse')
        toolbar.addActions(self.mousemanager.actions.actions())
        self.addToolBar(toolbar)

        # Help action
        self.helpactions = self._setupHelpActions()

        menu = QtWidgets.QMenu('Help', self)
        menu.addActions(self.helpactions.actions())
        self.menuBar().addMenu(menu)

        toolbar = QtWidgets.QToolBar('Help', self)
        toolbar.addActions(self.helpactions.actions())
        self.addToolBar(toolbar)

        self.resize(700, 500)

    def _setupFileActions(self):
        style = self.style()

        actions = QtWidgets.QActionGroup(self)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogOpenButton)
        QtWidgets.QAction(icon, 'Open', actions, triggered=self.openfile)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogCloseButton)
        QtWidgets.QAction(icon, 'Close', actions, triggered=self.scene.clear)

        QtWidgets.QAction(actions).setSeparator(True)

        icon = style.standardIcon(QtWidgets.QStyle.SP_DialogCancelButton)
        QtWidgets.QAction(icon, 'Exit', actions, triggered=self.close)

        return actions

    def _setupHelpActions(self):
        actions = QtWidgets.QActionGroup(self)

        icon = QtGui.QIcon(
            ':/trolltech/styles/commonstyle/images/fileinfo-32.png')
        QtWidgets.QAction(icon, 'About', actions, triggered=self.about)

        icon = QtGui.QIcon(':/trolltech/qmessagebox/images/qtlogo-64.png')
        QtWidgets.QAction(icon, 'About Qt', actions,
                          triggered=QtWidgets.QApplication.aboutQt)

        return actions

    @QtCore.Slot()
    def openfile(self):
        self.scene.clear()
        self.graphicsview.setTransform(QtGui.QTransform())
        filename = QtWidgets.QFileDialog.getOpenFileName()
        if filename:
            pixmap = QtGui.QPixmap(filename)
            item = self.scene.addPixmap(pixmap)
            self.scene.setSceneRect(item.boundingRect())

    @QtCore.Slot()
    def about(self):
        title = self.tr('MouseManager Example')
        text = ['<h1>Mouse Manager</h1>'
                '<p>Example program for the Mouse manager component.</p>',
                '<p>Copyright (C): 2009-2015 '
                '<a href="mailto:[email protected]">'
                'Antonio Valentino'
                '<a>.</p>']
        text = self.tr('\n'.join(text))
        QtWidgets.QMessageBox.about(self, title, text)