|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +/*************************************************************************** |
| 4 | + AutomaTracks |
| 5 | + A QGIS plugin |
| 6 | + Automatic tracks with constraint with path point |
| 7 | + ------------------- |
| 8 | + begin : 2018-01-25 |
| 9 | + git sha : $Format:%H$ |
| 10 | + copyright : (C) 2018 by Peillet Sébastien |
| 11 | + |
| 12 | + ***************************************************************************/ |
| 13 | +
|
| 14 | +/*************************************************************************** |
| 15 | + * * |
| 16 | + * This program is free software; you can redistribute it and/or modify * |
| 17 | + * it under the terms of the GNU General Public License as published by * |
| 18 | + * the Free Software Foundation; either version 2 of the License, or * |
| 19 | + * (at your option) any later version. * |
| 20 | + * * |
| 21 | + ***************************************************************************/ |
| 22 | +""" |
| 23 | +from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt |
| 24 | +from PyQt4.QtGui import QAction, QIcon |
| 25 | +# Initialize Qt resources from file resources.py |
| 26 | +import resources |
| 27 | + |
| 28 | +# Import the code for the DockWidget |
| 29 | +from AutoTrack_dockwidget import AutomaTracksDockWidget |
| 30 | +import os.path |
| 31 | + |
| 32 | + |
| 33 | +class AutomaTracks: |
| 34 | + """QGIS Plugin Implementation.""" |
| 35 | + |
| 36 | + def __init__(self, iface): |
| 37 | + """Constructor. |
| 38 | +
|
| 39 | + :param iface: An interface instance that will be passed to this class |
| 40 | + which provides the hook by which you can manipulate the QGIS |
| 41 | + application at run time. |
| 42 | + :type iface: QgsInterface |
| 43 | + """ |
| 44 | + # Save reference to the QGIS interface |
| 45 | + self.iface = iface |
| 46 | + |
| 47 | + # initialize plugin directory |
| 48 | + self.plugin_dir = os.path.dirname(__file__) |
| 49 | + |
| 50 | + # initialize locale |
| 51 | + locale = QSettings().value('locale/userLocale')[0:2] |
| 52 | + locale_path = os.path.join( |
| 53 | + self.plugin_dir, |
| 54 | + 'i18n', |
| 55 | + 'AutomaTracks_{}.qm'.format(locale)) |
| 56 | + |
| 57 | + if os.path.exists(locale_path): |
| 58 | + self.translator = QTranslator() |
| 59 | + self.translator.load(locale_path) |
| 60 | + |
| 61 | + if qVersion() > '4.3.3': |
| 62 | + QCoreApplication.installTranslator(self.translator) |
| 63 | + |
| 64 | + # Declare instance attributes |
| 65 | + self.actions = [] |
| 66 | + self.menu = self.tr(u'&AutomaTracks') |
| 67 | + # TODO: We are going to let the user set this up in a future iteration |
| 68 | + self.toolbar = self.iface.addToolBar(u'AutomaTracks') |
| 69 | + self.toolbar.setObjectName(u'AutomaTracks') |
| 70 | + |
| 71 | + #print "** INITIALIZING AutomaTracks" |
| 72 | + |
| 73 | + self.pluginIsActive = False |
| 74 | + self.dockwidget = None |
| 75 | + |
| 76 | + |
| 77 | + # noinspection PyMethodMayBeStatic |
| 78 | + def tr(self, message): |
| 79 | + """Get the translation for a string using Qt translation API. |
| 80 | +
|
| 81 | + We implement this ourselves since we do not inherit QObject. |
| 82 | +
|
| 83 | + :param message: String for translation. |
| 84 | + :type message: str, QString |
| 85 | +
|
| 86 | + :returns: Translated version of message. |
| 87 | + :rtype: QString |
| 88 | + """ |
| 89 | + # noinspection PyTypeChecker,PyArgumentList,PyCallByClass |
| 90 | + return QCoreApplication.translate('AutomaTracks', message) |
| 91 | + |
| 92 | + |
| 93 | + def add_action( |
| 94 | + self, |
| 95 | + icon_path, |
| 96 | + text, |
| 97 | + callback, |
| 98 | + enabled_flag=True, |
| 99 | + add_to_menu=True, |
| 100 | + add_to_toolbar=True, |
| 101 | + status_tip=None, |
| 102 | + whats_this=None, |
| 103 | + parent=None): |
| 104 | + """Add a toolbar icon to the toolbar. |
| 105 | +
|
| 106 | + :param icon_path: Path to the icon for this action. Can be a resource |
| 107 | + path (e.g. ':/plugins/foo/bar.png') or a normal file system path. |
| 108 | + :type icon_path: str |
| 109 | +
|
| 110 | + :param text: Text that should be shown in menu items for this action. |
| 111 | + :type text: str |
| 112 | +
|
| 113 | + :param callback: Function to be called when the action is triggered. |
| 114 | + :type callback: function |
| 115 | +
|
| 116 | + :param enabled_flag: A flag indicating if the action should be enabled |
| 117 | + by default. Defaults to True. |
| 118 | + :type enabled_flag: bool |
| 119 | +
|
| 120 | + :param add_to_menu: Flag indicating whether the action should also |
| 121 | + be added to the menu. Defaults to True. |
| 122 | + :type add_to_menu: bool |
| 123 | +
|
| 124 | + :param add_to_toolbar: Flag indicating whether the action should also |
| 125 | + be added to the toolbar. Defaults to True. |
| 126 | + :type add_to_toolbar: bool |
| 127 | +
|
| 128 | + :param status_tip: Optional text to show in a popup when mouse pointer |
| 129 | + hovers over the action. |
| 130 | + :type status_tip: str |
| 131 | +
|
| 132 | + :param parent: Parent widget for the new action. Defaults None. |
| 133 | + :type parent: QWidget |
| 134 | +
|
| 135 | + :param whats_this: Optional text to show in the status bar when the |
| 136 | + mouse pointer hovers over the action. |
| 137 | +
|
| 138 | + :returns: The action that was created. Note that the action is also |
| 139 | + added to self.actions list. |
| 140 | + :rtype: QAction |
| 141 | + """ |
| 142 | + |
| 143 | + icon = QIcon(icon_path) |
| 144 | + action = QAction(icon, text, parent) |
| 145 | + action.triggered.connect(callback) |
| 146 | + action.setEnabled(enabled_flag) |
| 147 | + |
| 148 | + if status_tip is not None: |
| 149 | + action.setStatusTip(status_tip) |
| 150 | + |
| 151 | + if whats_this is not None: |
| 152 | + action.setWhatsThis(whats_this) |
| 153 | + |
| 154 | + if add_to_toolbar: |
| 155 | + self.toolbar.addAction(action) |
| 156 | + |
| 157 | + if add_to_menu: |
| 158 | + self.iface.addPluginToMenu( |
| 159 | + self.menu, |
| 160 | + action) |
| 161 | + |
| 162 | + self.actions.append(action) |
| 163 | + |
| 164 | + return action |
| 165 | + |
| 166 | + |
| 167 | + def initGui(self): |
| 168 | + """Create the menu entries and toolbar icons inside the QGIS GUI.""" |
| 169 | + |
| 170 | + icon_path = ':/plugins/AutomaTracks/icon.png' |
| 171 | + self.add_action( |
| 172 | + icon_path, |
| 173 | + text=self.tr(u'AutomaTracks'), |
| 174 | + callback=self.run, |
| 175 | + parent=self.iface.mainWindow()) |
| 176 | + |
| 177 | + #-------------------------------------------------------------------------- |
| 178 | + |
| 179 | + def onClosePlugin(self): |
| 180 | + """Cleanup necessary items here when plugin dockwidget is closed""" |
| 181 | + |
| 182 | + #print "** CLOSING AutomaTracks" |
| 183 | + |
| 184 | + # disconnects |
| 185 | + self.dockwidget.closingPlugin.disconnect(self.onClosePlugin) |
| 186 | + |
| 187 | + # remove this statement if dockwidget is to remain |
| 188 | + # for reuse if plugin is reopened |
| 189 | + # Commented next statement since it causes QGIS crashe |
| 190 | + # when closing the docked window: |
| 191 | + # self.dockwidget = None |
| 192 | + |
| 193 | + self.pluginIsActive = False |
| 194 | + |
| 195 | + |
| 196 | + def unload(self): |
| 197 | + """Removes the plugin menu item and icon from QGIS GUI.""" |
| 198 | + |
| 199 | + #print "** UNLOAD AutomaTracks" |
| 200 | + |
| 201 | + for action in self.actions: |
| 202 | + self.iface.removePluginMenu( |
| 203 | + self.tr(u'&AutomaTracks'), |
| 204 | + action) |
| 205 | + self.iface.removeToolBarIcon(action) |
| 206 | + # remove the toolbar |
| 207 | + del self.toolbar |
| 208 | + |
| 209 | + #-------------------------------------------------------------------------- |
| 210 | + |
| 211 | + def run(self): |
| 212 | + """Run method that loads and starts the plugin""" |
| 213 | + |
| 214 | + if not self.pluginIsActive: |
| 215 | + self.pluginIsActive = True |
| 216 | + |
| 217 | + #print "** STARTING AutomaTracks" |
| 218 | + |
| 219 | + # dockwidget may not exist if: |
| 220 | + # first run of plugin |
| 221 | + # removed on close (see self.onClosePlugin method) |
| 222 | + if self.dockwidget == None: |
| 223 | + # Create the dockwidget (after translation) and keep reference |
| 224 | + self.dockwidget = AutomaTracksDockWidget() |
| 225 | + |
| 226 | + # connect to provide cleanup on closing of dockwidget |
| 227 | + self.dockwidget.closingPlugin.connect(self.onClosePlugin) |
| 228 | + |
| 229 | + # show the dockwidget |
| 230 | + # TODO: fix to allow choice of dock location |
| 231 | + self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget) |
| 232 | + self.dockwidget.show() |
| 233 | + |
0 commit comments