-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapTool.py
139 lines (119 loc) · 5.04 KB
/
mapTool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
# @Author : llc
# @Time : 2020/3/10 14:58
import os
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QCursor, QPixmap
from qgis.core import QgsProject, QgsMapSettings, QgsMapRendererParallelJob
from qgis.gui import QgsMapTool
from .mapItem import SwipeMapItem
here = os.path.dirname(__file__)
project = QgsProject.instance()
class SwipeMapTool(QgsMapTool):
def __init__(self, layerCombobox, mapCanvas):
super(SwipeMapTool, self).__init__(mapCanvas)
self.layerCombobox = layerCombobox
self.mapCanvas = mapCanvas
self.mapItem = SwipeMapItem(self.mapCanvas)
self.startSwipe = False
self.controlDown = False
self.layers = []
self.cursorSV = QCursor(QPixmap(os.path.join(here, 'images/split_v.png')))
self.cursorSH = QCursor(QPixmap(os.path.join(here, 'images/split_h.png')))
self.cursorUp = QCursor(QPixmap(os.path.join(here, 'images/up.png')))
self.cursorDown = QCursor(QPixmap(os.path.join(here, 'images/down.png')))
self.cursorLeft = QCursor(QPixmap(os.path.join(here, 'images/left.png')))
self.cursorRight = QCursor(QPixmap(os.path.join(here, 'images/right.png')))
self.cursorBox = QCursor(QPixmap(os.path.join(here, 'images/box.png')))
def activate(self):
self.connect()
self.startSwipe = False
self.setLayersSwipe()
def connect(self, is_connect=True):
if is_connect:
self.mapCanvas.mapCanvasRefreshed.connect(self.setMapLayers)
self.layerCombobox.currentIndexChanged.connect(self.setLayersSwipe)
else:
self.mapCanvas.mapCanvasRefreshed.disconnect(self.setMapLayers)
self.layerCombobox.currentIndexChanged.disconnect(self.setLayersSwipe)
def setLayersSwipe(self, ):
self.layers = project.layerTreeRoot().checkedLayers()
currentLayer = project.mapLayer(self.layerCombobox.currentData())
if currentLayer in self.layers:
self.layers.remove(currentLayer)
self.setMapLayers()
def setMapLayers(self):
def finished():
self.mapItem.image = job.renderedImage()
self.mapItem.setRect(self.mapCanvas.extent())
if len(self.layers) == 0:
return
settings = QgsMapSettings(self.mapCanvas.mapSettings())
settings.setLayers(self.layers)
job = QgsMapRendererParallelJob(settings)
job.start()
job.finished.connect(finished)
job.waitForFinished()
def keyPressEvent(self, e):
if self.mapCanvas.isDrawing():
return
if e.modifiers() == Qt.ControlModifier:
self.mapCanvas.setCursor(self.cursorBox)
self.controlDown = True
def keyReleaseEvent(self, e) -> None:
if self.mapCanvas.isDrawing():
return
if not e.isAutoRepeat():
self.controlDown = False
pos = self.cursorBox.pos()
# 触发鼠标移动事件
self.cursorBox.setPos(pos.x() + 1, pos.y() + 1)
def canvasPressEvent(self, e):
if self.mapCanvas.isDrawing():
return
self.startSwipe = True
w, h = self.mapCanvas.width(), self.mapCanvas.height()
if not self.controlDown:
if 0.25 * w < e.x() < 0.75 * w and e.y() < 0.5 * h:
self.mapItem.direction = 0 # '⬇'
self.mapCanvas.setCursor(self.cursorSH)
elif 0.25 * w < e.x() < 0.75 * w and e.y() > 0.5 * h:
self.mapItem.direction = 1 # '⬆'
self.mapCanvas.setCursor(self.cursorSH)
elif e.x() < 0.25 * w:
self.mapItem.direction = 2 # '➡'
self.mapCanvas.setCursor(self.cursorSV)
else: # elif e.x() > 0.75 * w:
self.mapItem.direction = 3 # '⬅'
self.mapCanvas.setCursor(self.cursorSV)
self.mapItem.updateImageRect(e.x(), e.y())
else:
self.mapItem.direction = -1 # all
self.mapItem.updateImageRect(w, h)
def canvasMoveEvent(self, e):
if self.mapCanvas.isDrawing():
return
if self.controlDown:
return
if self.startSwipe:
self.mapItem.updateImageRect(e.x(), e.y())
else:
# 设置当前cursor
w, h = self.mapCanvas.width(), self.mapCanvas.height()
if e.x() < 0.25 * w:
self.mapCanvas.setCursor(self.cursorRight)
if e.x() > 0.75 * w:
self.mapCanvas.setCursor(self.cursorLeft)
if 0.25 * w < e.x() < 0.75 * w and e.y() < 0.5 * h:
self.mapCanvas.setCursor(self.cursorDown)
if 0.25 * w < e.x() < 0.75 * w and e.y() > 0.5 * h:
self.mapCanvas.setCursor(self.cursorUp)
def canvasReleaseEvent(self, e):
self.startSwipe = False
self.canvasMoveEvent(e)
# 鼠标释放后重新绘制
self.mapItem.startPaint = False
self.mapItem.update()
def deactivate(self):
self.connect(False)
self.deactivated.emit()