/
layertreeview_sample.py
109 lines (80 loc) · 4.47 KB
/
layertreeview_sample.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LayerTreeViewSample
copyright : (C) 2020 by Chiakikun
email : chiakikungm@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
# Initialize Qt resources from file resources.py
from .resources import *
import os.path
import qgis
from qgis.core import *
from qgis.gui import *
class LayerTreeViewSample(QgsMapTool):
def changeLayer(self, layer):
if (layer == None): # レイヤウィンドウに何も無い状態
self.currentlayer = None
self.cleared.emit()
return
if self.currentlayer != None:
print(self.currentlayer.name() + 'が非アクティブになりました。')
print(layer.name()+'がアクティブになりました。')
self.currentlayer = layer
def start(self):
self.currentlayer = self.iface.layerTreeView().currentLayer()
self.iface.layerTreeView().currentLayerChanged.connect(self.changeLayer)
maptool = self # 場合によって書き換えて
self.canvas.setMapTool(maptool)
self.canvas.mapToolSet.connect(self.unsetTool) # このサンプル実行中に他のアイコンを押した場合
def finish(self):
self.iface.layerTreeView().currentLayerChanged.disconnect(self.changeLayer)
self.canvas.mapToolSet.disconnect(self.unsetTool)
def __init__(self, iface):
self.plugin_name = 'ダイアログ無し雛形' # プラグイン名
self.menu_pos = '雛形' # プラグインの登録場所(このサンプルの場合、メニューの「プラグイン/雛形/ダイアログ無し雛形」)
self.toolbar = True # Trueならツールバーにアイコンを表示する
self.checkable = True # Trueならプラグイン実行中はアイコンが凹んだままになる
self.iface = iface
self.canvas = self.iface.mapCanvas()
QgsMapTool.__init__(self, self.canvas)
# このプラグイン実行中に他のアイコンが押された場合、アイコンを元の状態に戻す
def unsetTool(self, tool):
if not isinstance(tool, LayerTreeViewSample):
self.finish()
self.action.setChecked(False)
def initGui(self):
icon = QIcon(os.path.dirname(__file__)+'/icon.png')
self.action = QAction(icon, self.plugin_name, self.iface.mainWindow())
self.action.triggered.connect(self.execSample) # アイコンを押下した時に実行されるメソッドを登録
self.action.setCheckable(self.checkable) # Trueだとアイコンを押下したら次に押下するまで凹んだままになる
if self.toolbar:
self.iface.addToolBarIcon(self.action) # ツールバーにこのツールのアイコンを表示する
self.iface.addPluginToMenu(self.menu_pos, self.action)
# このプラグインを無効にしたときに呼ばれる
def unload(self):
self.iface.removePluginMenu(self.menu_pos, self.action)
self.iface.removeToolBarIcon(self.action)
# このツールのアイコンを押下したときに呼ばれる
def execSample(self):
if self.checkable:
if self.action.isChecked(): # 凹状態になった
self.previousMapTool = self.canvas.mapTool() # 現在のマップツールを退避
self.start()
else: # 凸状態になった
self.finish()
self.canvas.setMapTool(self.previousMapTool) # このツール実行前に戻す
else:
self.start()