custom.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. custom
  5. A QGIS plugin
  6. custom
  7. Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
  8. -------------------
  9. begin : 2024-08-14
  10. git sha : $Format:%H$
  11. copyright : (C) 2024 by custom
  12. email : custom
  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 PyQt5.QtWidgets import QDialog
  24. from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
  25. from qgis.PyQt.QtGui import QIcon
  26. from qgis.PyQt.QtWidgets import QAction
  27. # Initialize Qt resources from file resources.py
  28. from .resources import *
  29. # Import the code for the dialog
  30. from .custom_dialog import customDialog
  31. import os.path
  32. class custom:
  33. """QGIS Plugin Implementation."""
  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. 'custom_{}.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'&custom')
  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. # noinspection PyMethodMayBeStatic
  62. def tr(self, message):
  63. """Get the translation for a string using Qt translation API.
  64. We implement this ourselves since we do not inherit QObject.
  65. :param message: String for translation.
  66. :type message: str, QString
  67. :returns: Translated version of message.
  68. :rtype: QString
  69. """
  70. # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
  71. return QCoreApplication.translate('custom', message)
  72. def add_action(
  73. self,
  74. icon_path,
  75. text,
  76. callback,
  77. enabled_flag=True,
  78. add_to_menu=True,
  79. add_to_toolbar=True,
  80. status_tip=None,
  81. whats_this=None,
  82. parent=None):
  83. """Add a toolbar icon to the toolbar.
  84. :param icon_path: Path to the icon for this action. Can be a resource
  85. path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
  86. :type icon_path: str
  87. :param text: Text that should be shown in menu items for this action.
  88. :type text: str
  89. :param callback: Function to be called when the action is triggered.
  90. :type callback: function
  91. :param enabled_flag: A flag indicating if the action should be enabled
  92. by default. Defaults to True.
  93. :type enabled_flag: bool
  94. :param add_to_menu: Flag indicating whether the action should also
  95. be added to the menu. Defaults to True.
  96. :type add_to_menu: bool
  97. :param add_to_toolbar: Flag indicating whether the action should also
  98. be added to the toolbar. Defaults to True.
  99. :type add_to_toolbar: bool
  100. :param status_tip: Optional text to show in a popup when mouse pointer
  101. hovers over the action.
  102. :type status_tip: str
  103. :param parent: Parent widget for the new action. Defaults None.
  104. :type parent: QWidget
  105. :param whats_this: Optional text to show in the status bar when the
  106. mouse pointer hovers over the action.
  107. :returns: The action that was created. Note that the action is also
  108. added to self.actions list.
  109. :rtype: QAction
  110. """
  111. icon = QIcon(icon_path)
  112. action = QAction(icon, text, parent)
  113. action.triggered.connect(callback)
  114. action.setEnabled(enabled_flag)
  115. if status_tip is not None:
  116. action.setStatusTip(status_tip)
  117. if whats_this is not None:
  118. action.setWhatsThis(whats_this)
  119. if add_to_toolbar:
  120. # Adds plugin icon to Plugins toolbar
  121. self.iface.addToolBarIcon(action)
  122. if add_to_menu:
  123. self.iface.addPluginToMenu(
  124. self.menu,
  125. action)
  126. self.actions.append(action)
  127. return action
  128. def initGui(self):
  129. """Create the menu entries and toolbar icons inside the QGIS GUI."""
  130. icon_path = ':/plugins/custom/icon.png'
  131. self.add_action(
  132. icon_path,
  133. text=self.tr(u'custom'),
  134. callback=self.run,
  135. parent=self.iface.mainWindow())
  136. # will be set False in run()
  137. self.first_start = True
  138. def unload(self):
  139. """Removes the plugin menu item and icon from QGIS GUI."""
  140. for action in self.actions:
  141. self.iface.removePluginMenu(
  142. self.tr(u'&custom'),
  143. action)
  144. self.iface.removeToolBarIcon(action)
  145. def run(self):
  146. """Run method that performs all the real work"""
  147. # Create the dialog with elements (after translation) and keep reference
  148. # Only create GUI ONCE in callback, so that it will only load when the plugin is started
  149. if self.first_start == True:
  150. self.first_start = False
  151. self.dlg = customDialog()
  152. # show the dialog
  153. self.dlg.show()
  154. # Run the dialog event loop
  155. result = self.dlg.exec_()
  156. # See if OK was pressed
  157. if result:
  158. # Do something useful here - delete the line containing pass and
  159. # substitute with your code.
  160. pass