mncheck.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. MnCheck
  5. A QGIS plugin
  6. Contrôle des données FTTH format MN
  7. Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
  8. -------------------
  9. begin : 2018-12-07
  10. git sha : $Format:%H$
  11. copyright : (C) 2018 by Manche Numérique 2019
  12. email : olivier.massot@manchenumerique.fr
  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. import os.path
  24. from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
  25. from PyQt5.QtGui import QIcon
  26. from PyQt5.QtWidgets import QAction
  27. from ui.dlg_main import DlgMain
  28. from .resources import *
  29. # Initialize Qt resources from file resources.py
  30. # Import the code for the dialog
  31. class MnCheck:
  32. """QGIS Plugin Implementation."""
  33. def __init__(self, iface):
  34. """Constructor.
  35. :param iface: An interface instance that will be passed to this class
  36. which provides the hook by which you can manipulate the QGIS
  37. application at run time.
  38. :type iface: QgsInterface
  39. """
  40. # Save reference to the QGIS interface
  41. self.iface = iface
  42. # initialize plugin directory
  43. self.plugin_dir = os.path.dirname(__file__)
  44. # initialize locale
  45. locale = QSettings().value('locale/userLocale')[0:2]
  46. locale_path = os.path.join(
  47. self.plugin_dir,
  48. 'i18n',
  49. 'MnCheck_{}.qm'.format(locale))
  50. if os.path.exists(locale_path):
  51. self.translator = QTranslator()
  52. self.translator.load(locale_path)
  53. if qVersion() > '4.3.3':
  54. QCoreApplication.installTranslator(self.translator)
  55. # Create the dialog (after translation) and keep reference
  56. self.dlg = DlgMain()
  57. # Declare instance attributes
  58. self.actions = []
  59. self.menu = self.tr(u'&MnCheck')
  60. # TODO: We are going to let the user set this up in a future iteration
  61. self.toolbar = self.iface.addToolBar(u'MnCheck')
  62. self.toolbar.setObjectName(u'MnCheck')
  63. # noinspection PyMethodMayBeStatic
  64. def tr(self, message):
  65. """Get the translation for a string using Qt translation API.
  66. We implement this ourselves since we do not inherit QObject.
  67. :param message: String for translation.
  68. :type message: str, QString
  69. :returns: Translated version of message.
  70. :rtype: QString
  71. """
  72. # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
  73. return QCoreApplication.translate('MnCheck', message)
  74. def add_action(
  75. self,
  76. icon_path,
  77. text,
  78. callback,
  79. enabled_flag=True,
  80. add_to_menu=True,
  81. add_to_toolbar=True,
  82. status_tip=None,
  83. whats_this=None,
  84. parent=None):
  85. """Add a toolbar icon to the toolbar.
  86. :param icon_path: Path to the icon for this action. Can be a resource
  87. path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
  88. :type icon_path: str
  89. :param text: Text that should be shown in menu items for this action.
  90. :type text: str
  91. :param callback: Function to be called when the action is triggered.
  92. :type callback: function
  93. :param enabled_flag: A flag indicating if the action should be enabled
  94. by default. Defaults to True.
  95. :type enabled_flag: bool
  96. :param add_to_menu: Flag indicating whether the action should also
  97. be added to the menu. Defaults to True.
  98. :type add_to_menu: bool
  99. :param add_to_toolbar: Flag indicating whether the action should also
  100. be added to the toolbar. Defaults to True.
  101. :type add_to_toolbar: bool
  102. :param status_tip: Optional text to show in a popup when mouse pointer
  103. hovers over the action.
  104. :type status_tip: str
  105. :param parent: Parent widget for the new action. Defaults None.
  106. :type parent: QWidget
  107. :param whats_this: Optional text to show in the status bar when the
  108. mouse pointer hovers over the action.
  109. :returns: The action that was created. Note that the action is also
  110. added to self.actions list.
  111. :rtype: QAction
  112. """
  113. icon = QIcon(icon_path)
  114. action = QAction(icon, text, parent)
  115. action.triggered.connect(callback)
  116. action.setEnabled(enabled_flag)
  117. if status_tip is not None:
  118. action.setStatusTip(status_tip)
  119. if whats_this is not None:
  120. action.setWhatsThis(whats_this)
  121. if add_to_toolbar:
  122. self.toolbar.addAction(action)
  123. if add_to_menu:
  124. self.iface.addPluginToMenu(
  125. self.menu,
  126. action)
  127. self.actions.append(action)
  128. return action
  129. def initGui(self):
  130. """Create the menu entries and toolbar icons inside the QGIS GUI."""
  131. icon_path = ':/plugins/mncheck/icon.png'
  132. self.add_action(
  133. icon_path,
  134. text=self.tr(u'MnCheck'),
  135. callback=self.run,
  136. parent=self.iface.mainWindow())
  137. def unload(self):
  138. """Removes the plugin menu item and icon from QGIS GUI."""
  139. for action in self.actions:
  140. self.iface.removePluginMenu(
  141. self.tr(u'&MnCheck'),
  142. action)
  143. self.iface.removeToolBarIcon(action)
  144. # remove the toolbar
  145. del self.toolbar
  146. def run(self):
  147. """Run method that performs all the real work"""
  148. # show the dialog
  149. self.dlg.show()
  150. # Run the dialog event loop
  151. result = self.dlg.exec_()
  152. # See if OK was pressed
  153. if result:
  154. # Do something useful here - delete the line containing pass and
  155. # substitute with your code.
  156. pass