template.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. {0}
  5. -------------------
  6. released : {1}
  7. author : (C) {2} by {3}
  8. email : {4}
  9. made in : easyPlugin by Pavel Pereverzev
  10. credits to : Gary Sherman and Alexandre Neto
  11. ***************************************************************************/
  12. /***************************************************************************
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. ***************************************************************************/
  20. """
  21. # import system, PyQt and QGIS libraries
  22. from __future__ import absolute_import
  23. import os.path
  24. from PyQt5 import QtCore
  25. from PyQt5.QtCore import *
  26. from PyQt5.QtGui import *
  27. from PyQt5.QtWidgets import * # only used widgets can be listed here
  28. from qgis.core import *
  29. from qgis._gui import *
  30. from qgis.utils import iface
  31. from .template_tools import *
  32. class {5}(object):
  33. # main plugin class
  34. def __init__(self, iface):
  35. """Constructor.
  36. :param iface: An interface instance that will be passed to this class
  37. which provides the hook by which you can manipulate the QGIS
  38. application at run time.
  39. :type iface: QgsInterface
  40. """
  41. # Save reference to the QGIS interface
  42. self.iface = iface
  43. # initialize plugin directory
  44. self.plugin_dir = os.path.dirname(__file__)
  45. # initialize locale
  46. locale = QSettings().value('locale/userLocale')[0:2]
  47. locale_path = os.path.join(
  48. self.plugin_dir,
  49. 'i18n',
  50. '{6}_{{}}.qm'.format(locale))
  51. if os.path.exists(locale_path):
  52. self.translator = QTranslator()
  53. self.translator.load(locale_path)
  54. QCoreApplication.installTranslator(self.translator)
  55. # Declare instance attributes
  56. self.actions = []
  57. self.menu = self.tr(u"{6}")
  58. # Check if plugin was started the first time in current QGIS session
  59. # Must be set in initGui() to survive plugin reloads
  60. self.first_start = None
  61. def initGui(self):
  62. # Create the menu entries and toolbar icons inside the QGIS GUI
  63. icon_path = QIcon(os.path.join(self.plugin_dir, "icon.png"))
  64. self.icon_action = self.add_action(
  65. icon_path,
  66. text=self.tr(u"{6}"),
  67. callback=self.run,
  68. checkable={8},
  69. parent=self.iface.mainWindow())
  70. # will be set False in run()
  71. self.first_start = True
  72. def warning_message(self, err_text):
  73. msg = QMessageBox()
  74. msg.warning(self, "Warning", err_text)
  75. def add_action(
  76. self,
  77. icon_path,
  78. text,
  79. callback,
  80. enabled_flag=True,
  81. checkable=False,
  82. add_to_menu=False,
  83. add_to_toolbar=True,
  84. status_tip=None,
  85. whats_this=None,
  86. parent=None):
  87. icon = QIcon(icon_path)
  88. action = QAction(icon, text, parent)
  89. action.triggered.connect(callback)
  90. action.setEnabled(enabled_flag)
  91. action.setCheckable(checkable)
  92. if status_tip is not None:
  93. action.setStatusTip(status_tip)
  94. if whats_this is not None:
  95. action.setWhatsThis(whats_this)
  96. if add_to_toolbar:
  97. # Adds plugin icon to Plugins toolbar
  98. self.iface.addToolBarIcon(action)
  99. if add_to_menu:
  100. self.iface.addPluginToMenu(
  101. self.menu,
  102. action)
  103. self.actions.append(action)
  104. return action
  105. def unload(self):
  106. # Removes the plugin menu item and icon from QGIS GUI
  107. for action in self.actions:
  108. self.iface.removeToolBarIcon(action)
  109. def tr(self, text):
  110. return QCoreApplication.translate("{6}", text)
  111. # custom actions, feel free to edit them
  112. def simple_action(self):
  113. # run a simple action like in python console of QGIS
  114. self.iface.messageBar().pushMessage("Simple", "Action", level=Qgis.Info)
  115. def simple_gui(self):
  116. # run a widget with some actions
  117. self.app = SimpleGui()
  118. def simple_map_tool(self):
  119. # run a map tool, also making an action button checkable
  120. if self.icon_action.isChecked():
  121. self.rband_tool_anchor = PointTool(self.icon_action)
  122. iface.mapCanvas().setMapTool(self.rband_tool_anchor)
  123. else:
  124. self.rband_tool_anchor.deactivate()
  125. iface.mapCanvas().unsetMapTool(self.rband_tool_anchor)
  126. def custom_tool(self):
  127. try:
  128. {9}
  129. except Exception as e:
  130. print(e)
  131. self.warning_message("Error in script\nSee Python console for details")
  132. # MAIN ACTION FUNCTION IS HERE
  133. def run(self):
  134. # run method that performs all the real work
  135. {7}