def __init__(self, iface): canvas = iface.mapCanvas() super(MapSwipeTool, self).__init__(canvas) self.view = iface.layerTreeView() self.msgBar = iface.messageBar() self.swipe = SwipeMap(canvas) self.checkDirection = self.hasSwipe = self.disabledSwipe = None self.firstPoint = QPoint() self.cursorV = QCursor(Qt.SplitVCursor) self.cursorH = QCursor(Qt.SplitHCursor)
def __init__(self, iface): canvas = iface.mapCanvas() super(MapSwipeTool, self).__init__( canvas ) self.view = iface.layerTreeView() self.msgBar = iface.messageBar() self.swipe = SwipeMap( canvas ) self.checkDirection = self.hasSwipe = self.disabledSwipe = None self.firstPoint = QPoint() self.cursorV = QCursor( Qt.SplitVCursor ) self.cursorH = QCursor( Qt.SplitHCursor )
class MapSwipeTool(QgsMapTool): def __init__(self, iface): canvas = iface.mapCanvas() super(MapSwipeTool, self).__init__(canvas) self.view = iface.layerTreeView() self.msgBar = iface.messageBar() self.swipe = SwipeMap(canvas) self.checkDirection = self.hasSwipe = self.disabledSwipe = None self.firstPoint = QPoint() self.cursorV = QCursor(Qt.SplitVCursor) self.cursorH = QCursor(Qt.SplitHCursor) def tr(self, sourceText): context = 'MapSwipeTool' return QCoreApplication.translate(context, sourceText) def activate(self): self.canvas().setCursor(QCursor(Qt.CrossCursor)) self._connect() self.hasSwipe = False self.disabledSwipe = False self.setLayersSwipe(None, None) self.checkLayer() def canvasPressEvent(self, e): if self.checkLayer(): self.hasSwipe = True self.firstPoint.setX(e.x()) self.firstPoint.setY(e.y()) self.checkDirection = True def canvasReleaseEvent(self, e): self.hasSwipe = False self.canvas().setCursor(QCursor(Qt.CrossCursor)) def canvasMoveEvent(self, e): if self.hasSwipe: if self.checkDirection: dX = abs(e.x() - self.firstPoint.x()) dY = abs(e.y() - self.firstPoint.y()) isVertical = dX > dY self.swipe.setIsVertical(isVertical) self.checkDirection = False self.canvas().setCursor( self.cursorH if isVertical else self.cursorV) self.swipe.setLength(e.x(), e.y()) def _connect(self, isConnect=True): signal_slot = ({ 'signal': self.canvas().mapCanvasRefreshed, 'slot': self.swipe.setMap }, { 'signal': self.view.selectionModel().selectionChanged, 'slot': self.setLayersSwipe }, { 'signal': QgsMapLayerRegistry.instance().removeAll, 'slot': self.disable }) if isConnect: for item in signal_slot: item['signal'].connect(item['slot']) else: for item in signal_slot: item['signal'].disconnect(item['slot']) def checkLayer(self): if len(self.swipe.layers) == 0: msg = self.tr( "Select active Layer or Group(with layers) in legend.") self.msgBar.clearWidgets() self.msgBar.pushMessage("MapSwipeTool", msg, QgsMessageBar.WARNING, 4) return False else: return True @pyqtSlot("QModelIndex, QModelIndex") def setLayersSwipe(self, selected=None, deselected=None): if self.disabledSwipe: return ids = msg = None node = self.view.currentNode() if isinstance(node, QgsLayerTreeLayer): layer = node.layer() ids = [layer.id()] msg = self.tr("Active layer is '%s'.") % layer.name() else: group = self.view.currentGroupNode() if group.parent() is None: # Root return ids = group.findLayerIds() msg = self.tr("Active group is '%s'.") % group.name() self.swipe.clear() self.swipe.setLayersId(ids) self.swipe.setMap() if self.checkLayer(): self.msgBar.clearWidgets() self.msgBar.pushMessage("MapSwipeTool", msg, QgsMessageBar.INFO, 2) @pyqtSlot() def disable(self): self.swipe.clear() self.hasSwipe = False self.disabledSwipe = True def deactivate(self): super(MapSwipeTool, self).deactivate() self.deactivated.emit() self.swipe.clear() self._connect(False)
class MapSwipeTool(QgsMapTool): def __init__(self, iface): canvas = iface.mapCanvas() super(MapSwipeTool, self).__init__( canvas ) self.view = iface.layerTreeView() self.msgBar = iface.messageBar() self.swipe = SwipeMap( canvas ) self.checkDirection = self.hasSwipe = self.disabledSwipe = None self.firstPoint = QPoint() self.cursorV = QCursor( Qt.SplitVCursor ) self.cursorH = QCursor( Qt.SplitHCursor ) def activate(self): self.canvas().setCursor( QCursor( Qt.CrossCursor ) ) self._connect() self.hasSwipe = False self.disabledSwipe = False self.setLayersSwipe( self.view.currentIndex() ) def canvasPressEvent(self, e): if len(self.swipe.layers) == 0: msg = "Select active Layer or Group(with layers) in legend." self.msgBar.clearWidgets() self.msgBar.pushMessage( "MapSwipeTool", msg, QgsMessageBar.WARNING, 4 ) return self.hasSwipe = True self.firstPoint.setX( e.x() ) self.firstPoint.setY( e.y() ) self.checkDirection = True def canvasReleaseEvent(self, e): self.hasSwipe = False self.canvas().setCursor( QCursor( Qt.CrossCursor ) ) def canvasMoveEvent(self, e): if self.hasSwipe: if self.checkDirection: dX = abs( e.x() - self.firstPoint.x() ) dY = abs( e.y() - self.firstPoint.y() ) isVertical = dX > dY self.swipe.setIsVertical( isVertical ) self.checkDirection = False self.canvas().setCursor( self.cursorH if isVertical else self.cursorV ) self.swipe.setLength( e.x(), e.y() ) def _connect(self, isConnect = True): signal_slot = ( { 'signal': self.canvas().mapCanvasRefreshed, 'slot': self.swipe.setMap }, { 'signal': self.view.activated, 'slot': self.setLayersSwipe }, { 'signal': QgsMapLayerRegistry.instance().removeAll, 'slot': self.disable } ) if isConnect: for item in signal_slot: item['signal'].connect( item['slot'] ) else: for item in signal_slot: item['signal'].disconnect( item['slot'] ) @pyqtSlot( "QModelIndex" ) def setLayersSwipe(self, index): if self.disabledSwipe: return ids = msg = None node = self.view.currentNode() if isinstance( node, QgsLayerTreeLayer ): layer = node.layer() ids = [ layer.id() ] msg = "Active layer is '%s'." % layer.name() else: group = self.view.currentGroupNode() if group.parent() is None: # Root return ids = group.findLayerIds() msg = "Active group is '%s'." % group.name() self.swipe.clear() self.swipe.setLayersId( ids ) self.msgBar.clearWidgets() self.msgBar.pushMessage( "MapSwipeTool", msg, QgsMessageBar.INFO, 2 ) self.swipe.setMap() @pyqtSlot() def disable(self): self.swipe.clear() self.hasSwipe = False self.disabledSwipe = True def deactivate(self): super( MapSwipeTool, self ).deactivate() self.deactivated.emit() self.swipe.clear() self._connect( False )