-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
335 lines (248 loc) · 12.8 KB
/
gui.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from PyQt5.QtWidgets import QAction, QMainWindow, QStatusBar, QProgressBar
from PyQt5.QtCore import Qt
from qgis.core import QgsProject
from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsMapToolZoom, QgsMapToolEmitPoint
import pyperclip
import shared
from shared import INPUT_RASTER_BACKGROUND
from simulationthread import SimulationThread
#======================================================================================================================
#
# Class for a map tool, which returns coordinates when mouse is clicked
#
#======================================================================================================================
class PointTool(QgsMapToolEmitPoint):
def __init__(self, canvas):
QgsMapToolEmitPoint.__init__(self, canvas)
#======================================================================================================================
#======================================================================================================================
def canvasReleaseEvent(self, mouseEvent):
# Get the coords
point = self.toMapCoordinates(mouseEvent.pos())
#shared.fpOut.write(point.x(), point.y())
# Show in the status bar
coordStr = str("Coordinates: {" + str(point.x()) + ", " + str(point.y()) + "}")
self.parent().parent().statusBar.showMessage(coordStr, shared.defaultMessageDisplayTime)
# Copy to clipboard
x = round(point.x() * 2) / 2
y = round(point.y() * 2) / 2
#pyperclip.copy("{:08.1f}, {:08.1f}".format(x, y))
pyperclip.copy("{:06.0f}, {:06.0f}".format(x, y))
#======================================================================================================================
#======================================================================================================================
#
# The class which defines the main window
#
#======================================================================================================================
class MainWindow(QMainWindow):
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-statements
def __init__(self, app, mapLayers, mapLayersCategory):
QMainWindow.__init__(self)
self.app = app
self.mapLayers = mapLayers
self.mapLayersCategory = mapLayersCategory
self.resize(shared.windowWidth, shared.windowHeight)
self.setWindowTitle(shared.progName + ": " + shared.progVer)
# Set up the map canvas
self.canvas = QgsMapCanvas()
self.setCentralWidget(self.canvas)
self.canvas.setCanvasColor(Qt.white)
self.canvas.enableAntiAliasing(True)
self.canvas.setCachingEnabled(True)
#self.canvas.setMapUpdateInterval(1000)
self.canvas.setParallelRenderingEnabled(True)
self.canvas.enableMapTileRendering(True)
self.canvas.setLayers(self.mapLayers)
self.canvas.setExtent(shared.extentRect)
self.canvas.setMagnificationFactor(shared.windowMagnification)
#mapSet = self.canvas.mapSettings()
#print(mapSet.flags())
# Create some actions
#self.actionExit = QAction(QIcon('exit.png'), '&Exit', self)
self.actionExit = QAction("&Exit", self)
self.actionExit.setShortcut("Ctrl+Q")
self.actionExit.setStatusTip("Exit " + shared.progName)
self.actionZoomIn = QAction("Zoom in", self)
self.actionZoomIn.setCheckable(True)
#self.actionExit.setShortcut("Ctrl++")
self.actionZoomIn.setStatusTip("Show more detail")
self.actionZoomOut = QAction("Zoom out", self)
self.actionZoomOut.setCheckable(True)
#self.actionExit.setShortcut("Ctrl+-")
self.actionZoomOut.setStatusTip("Show less detail")
self.actionPan = QAction("Pan", self)
self.actionPan.setCheckable(True)
self.actionPan.setStatusTip("Move the map laterally")
self.actionChangeBackground = QAction("Change background", self)
self.actionChangeBackground.setStatusTip("Change the raster background")
if not shared.haveRasterBackground:
self.actionChangeBackground.setEnabled(False)
self.actionCoords = QAction("Show coordinates", self)
self.actionCoords.setCheckable(True)
self.actionCoords.setStatusTip("Click to show coordinates")
self.actionRun = QAction("Simulate", self)
self.actionRun.setStatusTip("Route flow")
# Connect the actions
self.actionExit.triggered.connect(app.quit)
self.actionZoomIn.triggered.connect(self.zoomIn)
self.actionZoomOut.triggered.connect(self.zoomOut)
self.actionPan.triggered.connect(self.pan)
self.actionChangeBackground.triggered.connect(self.changeBackground)
self.actionCoords.triggered.connect(self.showCoords)
self.actionRun.triggered.connect(self.doRun)
# Create a menu bar and add menus
self.menubar = self.menuBar()
self.fileMenu = self.menubar.addMenu("&File")
self.fileMenu.addAction(self.actionExit)
self.editMenu = self.menubar.addMenu("&Edit")
#self.editMenu.addAction(self.actionExit)
self.viewMenu = self.menubar.addMenu("&View")
self.viewMenu.addAction(self.actionZoomIn)
self.viewMenu.addAction(self.actionZoomOut)
self.viewMenu.addAction(self.actionPan)
self.viewMenu.addAction(self.actionChangeBackground)
self.viewMenu.addAction(self.actionCoords)
self.runMenu = self.menubar.addMenu("&Run")
self.runMenu.addAction(self.actionRun)
# Create a tool bar and add some actions
self.toolbar = self.addToolBar("Default")
self.toolbar.setFloatable(True)
self.toolbar.addAction(self.actionRun)
self.toolbar.addAction(self.actionZoomIn)
self.toolbar.addAction(self.actionZoomOut)
self.toolbar.addAction(self.actionPan)
self.toolbar.addAction(self.actionCoords)
# Create some map tools
self.toolPan = QgsMapToolPan(self.canvas)
self.toolPan.setAction(self.actionPan)
self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # False = in
self.toolZoomIn.setAction(self.actionZoomIn)
self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # True = out
self.toolZoomOut.setAction(self.actionZoomOut)
self.toolCoords = PointTool(self.canvas)
self.toolCoords.setAction(self.actionCoords)
# Put into panning mode
self.pan()
# Add a status bar
self.statusBar = QStatusBar(self.canvas)
self.setStatusBar(self.statusBar)
# And put a progress indicator on the status bar
self.statusBar.progress = QProgressBar(self)
self.statusBar.progress.setRange(0, 100)
self.statusBar.progress.setMaximumWidth(500)
self.statusBar.progress.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
self.statusBar.addPermanentWidget(self.statusBar.progress)
self.myThread = None
return
#======================================================================================================================
#======================================================================================================================
def doRun(self):
# Delete all output features (in case we have some from a previous run)
listIDs = [feat.id() for feat in shared.outFlowMarkerPointLayer.getFeatures()]
prov = shared.outFlowMarkerPointLayer.dataProvider()
prov.deleteFeatures([featID for featID in listIDs])
shared.outFlowMarkerPointLayer.updateExtents()
shared.outFlowMarkerPointLayer.triggerRepaint()
listIDs = [feat.id() for feat in shared.outFlowLineLayer.getFeatures()]
prov = shared.outFlowLineLayer.dataProvider()
prov.deleteFeatures([featID for featID in listIDs])
shared.outFlowLineLayer.updateExtents()
shared.outFlowLineLayer.triggerRepaint()
self.canvas.repaint()
self.statusBar.progress.setValue(0)
self.actionRun.setEnabled(False)
# All is now ready, so run the simulation as a separate thread
self.myThread = SimulationThread(self.app, self)
self.myThread.refresh.connect(self.doRefresh)
self.myThread.runDone.connect(self.runDone)
self.canvas.freeze(True)
#self.canvas.refreshAllLayers()
#self.app.processEvents()
self.myThread.start()
print("\nThread started")
return
#======================================================================================================================
#======================================================================================================================
def doRefresh(self):
self.canvas.freeze(False)
if not self.canvas.isDrawing():
shared.outFlowMarkerPointLayer.triggerRepaint()
shared.outFlowLineLayer.triggerRepaint()
self.canvas.repaint()
self.canvas.freeze(True)
if not isinstance(shared.flowStartPoints, int):
doneSoFar = (float(shared.thisStartPoint) / float(len(shared.flowStartPoints) + 1)) * 100.0
#shared.fpOut.write(doneSoFar)
self.statusBar.progress.setValue(doneSoFar)
return
#======================================================================================================================
#======================================================================================================================
def zoomIn(self):
self.canvas.freeze(False)
self.canvas.setMapTool(self.toolZoomIn)
return
#======================================================================================================================
#======================================================================================================================
def zoomOut(self):
self.canvas.freeze(False)
self.canvas.setMapTool(self.toolZoomOut)
return
#======================================================================================================================
#======================================================================================================================
def pan(self):
self.canvas.freeze(False)
self.canvas.setMapTool(self.toolPan)
return
#======================================================================================================================
#======================================================================================================================
def showCoords(self):
self.canvas.setMapTool(self.toolCoords)
return
#======================================================================================================================
#======================================================================================================================
def close(self):
if self.myThread:
self.myThread.quit()
self.myThread = None
return
#======================================================================================================================
#======================================================================================================================
def runDone(self):
self.canvas.freeze(False)
shared.outFlowMarkerPointLayer.triggerRepaint()
shared.outFlowLineLayer.triggerRepaint()
self.canvas.repaint()
print("Thread done")
self.myThread.quit()
#self.myThread = None
self.statusBar.progress.setValue(100)
#QMessageBox.information(self, "End of run", shared.progName + ": flow routed")
self.statusBar.showMessage("End of run: flow routed", shared.defaultMessageDisplayTime)
# To prevent subsequent re-runs
# self.actionRun.setEnabled(False)
return
#======================================================================================================================
#======================================================================================================================
def changeBackground(self):
for n in range(len(shared.rasterInputLayersCategory)):
if shared.rasterInputLayersCategory[n] == INPUT_RASTER_BACKGROUND:
oldOpacity = shared.rasterInputLayers[n].renderer().opacity()
if oldOpacity == 0:
newOpacity = shared.rasterFileOpacity[n]
else:
newOpacity = 0
shared.rasterInputLayers[n].renderer().setOpacity(newOpacity)
#layerID = shared.rasterInputLayers[n].id()
#layerTreeNode = QgsProject.instance().layerTreeRoot().findLayer(layerID)
#print(layerTreeNode.dump())
#layerTreeNode.setItemVisibilityChecked(not layerTreeNode.itemVisibilityChecked())
#print(layerTreeNode.dump())
#print("*****************")
#for n in range(len(self.mapLayers)):
#if self.mapLayersCategory[n] == INPUT_RASTER_BACKGROUND:
#self.mapLayers[n].setVisible(not self.mapLayers[n].isVisible())
#self.canvas.setLayers(self.mapLayers)
self.doRefresh()
return
#======================================================================================================================