FTP.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. Resource
  5. A QGIS plugin
  6. Resource
  7. Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
  8. -------------------
  9. begin : 2024-09-03
  10. git sha : $Format:%H$
  11. copyright : (C) 2024 by siwei
  12. email : admin@siwei.com
  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 subprocess
  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 .FTP_dialog import FTPDialog
  31. import os.path
  32. import os
  33. import inspect
  34. class FTP:
  35. """QGIS Plugin Implementation."""
  36. def __init__(self, iface):
  37. """Constructor.
  38. :param iface: An interface instance that will be passed to this class
  39. which provides the hook by which you can manipulate the QGIS
  40. application at run time.
  41. :type iface: QgsInterface
  42. """
  43. # Save reference to the QGIS interface
  44. self.iface = iface
  45. # initialize plugin directory
  46. self.plugin_dir = os.path.dirname(__file__)
  47. # initialize locale
  48. locale = QSettings().value('locale/userLocale')[0:2]
  49. locale_path = os.path.join(
  50. self.plugin_dir,
  51. 'i18n',
  52. 'FTP_{}.qm'.format(locale))
  53. if os.path.exists(locale_path):
  54. self.translator = QTranslator()
  55. self.translator.load(locale_path)
  56. QCoreApplication.installTranslator(self.translator)
  57. # Declare instance attributes
  58. self.actions = []
  59. self.menu = self.tr(u'&Resource')
  60. # Check if plugin was started the first time in current QGIS session
  61. # Must be set in initGui() to survive plugin reloads
  62. self.first_start = None
  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('Resource', 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. # Adds plugin icon to Plugins toolbar
  123. self.iface.addToolBarIcon(action)
  124. if add_to_menu:
  125. self.iface.addPluginToMenu(
  126. self.menu,
  127. action)
  128. self.actions.append(action)
  129. return action
  130. def initGui(self):
  131. """Create the menu entries and toolbar icons inside the QGIS GUI."""
  132. current_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  133. self.add_action(
  134. QIcon(os.path.join(current_directory, "icon.png")),
  135. text=self.tr(u'文件服务器'),
  136. callback=self.run,
  137. parent=self.iface.mainWindow())
  138. # will be set False in run()
  139. self.first_start = True
  140. def unload(self):
  141. """Removes the plugin menu item and icon from QGIS GUI."""
  142. for action in self.actions:
  143. self.iface.removePluginMenu(
  144. self.tr(u'&Resource'),
  145. action)
  146. self.iface.removeToolBarIcon(action)
  147. def run(self):
  148. """Run method that performs all the real work"""
  149. # # Create the dialog with elements (after translation) and keep reference
  150. # # Only create GUI ONCE in callback, so that it will only load when the plugin is started
  151. # if self.first_start == True:
  152. # self.first_start = False
  153. # self.dlg = FTPDialog()
  154. #
  155. # # show the dialog
  156. # self.dlg.show()
  157. # # Run the dialog event loop
  158. # result = self.dlg.exec_()
  159. # # See if OK was pressed
  160. # if result:
  161. # # Do something useful here - delete the line containing pass and
  162. # # substitute with your code.
  163. # pass
  164. command = 'python-qgis-ltr.bat "D:\\Program Files\\QGIS 3.34.9\\apps\\qgis-ltr\\python\\plugins\\processing\\tools\\Resource\\ResourceTree.py"'
  165. process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  166. creationflags=subprocess.CREATE_NO_WINDOW)