Coverage for grm\GRM.py : 0%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3/***************************************************************************
4 GRM
5 A QGIS plugin
6 GRM
7 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
8 -------------------
9 begin : 2020-01-08
10 git sha : $Format:%H$
11 copyright : (C) 2020 by Hermesys
12 email : shpark@hermesys.co.kr
13 ***************************************************************************/
15/***************************************************************************
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 ***************************************************************************/
23"""
24import os.path
26from qgis.PyQt.QtCore import QCoreApplication, QSettings, Qt, QTranslator
27from qgis.PyQt.QtGui import QIcon
28from qgis.PyQt.QtWidgets import QAction
30# Import the code for the DockWidget
31from grm.GRM_dockwidget import GRMDockWidget
33# Initialize Qt resources from file resources.py
34from grm.resources import *
37class GRM:
38 """QGIS Plugin Implementation."""
40 def __init__(self, iface):
41 """Constructor.
43 :param iface: An interface instance that will be passed to this class
44 which provides the hook by which you can manipulate the QGIS
45 application at run time.
46 :type iface: QgsInterface
47 """
48 # Save reference to the QGIS interface
49 self.iface = iface
51 # initialize plugin directory
52 self.plugin_dir = os.path.dirname(__file__)
54 # initialize locale
55 locale = QSettings().value("locale/userLocale")[0:2]
56 locale_path = os.path.join(self.plugin_dir, "i18n", "GRM_{}.qm".format(locale))
58 if os.path.exists(locale_path):
59 self.translator = QTranslator()
60 self.translator.load(locale_path)
61 QCoreApplication.installTranslator(self.translator)
63 # Declare instance attributes
64 self.actions = []
65 self.menu = self.tr("&QGRM")
66 # TODO: We are going to let the user set this up in a future iteration
67 self.toolbar = self.iface.addToolBar("GRM")
68 self.toolbar.setObjectName("GRM")
70 # print "** INITIALIZING GRM"
72 self.pluginIsActive = False
73 self.dockwidget = None
75 # noinspection PyMethodMayBeStatic
76 def tr(self, message):
77 """Get the translation for a string using Qt translation API.
79 We implement this ourselves since we do not inherit QObject.
81 :param message: String for translation.
82 :type message: str, QString
84 :returns: Translated version of message.
85 :rtype: QString
86 """
87 # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
88 return QCoreApplication.translate("GRM", message)
90 def add_action(
91 self,
92 icon_path,
93 text,
94 callback,
95 enabled_flag=True,
96 add_to_menu=True,
97 add_to_toolbar=True,
98 status_tip=None,
99 whats_this=None,
100 parent=None,
101 ):
102 """Add a toolbar icon to the toolbar.
104 :param icon_path: Path to the icon for this action. Can be a resource
105 path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
106 :type icon_path: str
108 :param text: Text that should be shown in menu items for this action.
109 :type text: str
111 :param callback: Function to be called when the action is triggered.
112 :type callback: function
114 :param enabled_flag: A flag indicating if the action should be enabled
115 by default. Defaults to True.
116 :type enabled_flag: bool
118 :param add_to_menu: Flag indicating whether the action should also
119 be added to the menu. Defaults to True.
120 :type add_to_menu: bool
122 :param add_to_toolbar: Flag indicating whether the action should also
123 be added to the toolbar. Defaults to True.
124 :type add_to_toolbar: bool
126 :param status_tip: Optional text to show in a popup when mouse pointer
127 hovers over the action.
128 :type status_tip: str
130 :param parent: Parent widget for the new action. Defaults None.
131 :type parent: QWidget
133 :param whats_this: Optional text to show in the status bar when the
134 mouse pointer hovers over the action.
136 :returns: The action that was created. Note that the action is also
137 added to self.actions list.
138 :rtype: QAction
139 """
141 icon = QIcon(icon_path)
142 action = QAction(icon, text, parent)
143 action.triggered.connect(callback)
144 action.setEnabled(enabled_flag)
146 if status_tip is not None:
147 action.setStatusTip(status_tip)
149 if whats_this is not None:
150 action.setWhatsThis(whats_this)
152 if add_to_toolbar:
153 self.toolbar.addAction(action)
155 if add_to_menu:
156 self.iface.addPluginToMenu(self.menu, action)
158 self.actions.append(action)
160 return action
162 def initGui(self):
163 """Create the menu entries and toolbar icons inside the QGIS GUI."""
165 icon_path = ":/plugins/GRM/icon.png"
166 self.add_action(
167 icon_path,
168 text=self.tr(""),
169 callback=self.run,
170 parent=self.iface.mainWindow(),
171 )
173 # --------------------------------------------------------------------------
175 def onClosePlugin(self):
176 """Cleanup necessary items here when plugin dockwidget is closed"""
178 # print "** CLOSING GRM"
180 # disconnects
181 self.dockwidget.closingPlugin.disconnect(self.onClosePlugin)
183 # remove this statement if dockwidget is to remain
184 # for reuse if plugin is reopened
185 # Commented next statement since it causes QGIS crashe
186 # when closing the docked window:
187 # self.dockwidget = None
189 self.pluginIsActive = False
191 def unload(self):
192 """Removes the plugin menu item and icon from QGIS GUI."""
194 # print "** UNLOAD GRM"
196 for action in self.actions:
197 self.iface.removePluginMenu(self.tr("&QGRM"), action)
198 self.iface.removeToolBarIcon(action)
199 # remove the toolbar
200 del self.toolbar
202 # --------------------------------------------------------------------------
204 def run(self):
205 """Run method that loads and starts the plugin"""
207 if not self.pluginIsActive:
208 self.pluginIsActive = True
210 # print "** STARTING GRM"
212 # dockwidget may not exist if:
213 # first run of plugin
214 # removed on close (see self.onClosePlugin method)
215 if self.dockwidget is None:
216 # Create the dockwidget (after translation) and keep reference
217 self.dockwidget = GRMDockWidget()
219 # connect to provide cleanup on closing of dockwidget
220 self.dockwidget.closingPlugin.connect(self.onClosePlugin)
222 # show the dockwidget
223 # TODO: fix to allow choice of dock location
224 self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
225 self.dockwidget.show()