FilePath.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. FilePath
  5. A QGIS plugin
  6. This plugin copies the file path of the selected layer and opens the file location
  7. ***************************************************************************/
  8. """
  9. from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
  10. from PyQt5.QtGui import QIcon
  11. from PyQt5.QtWidgets import QAction, QToolBar
  12. from qgis.core import QgsProject, Qgis
  13. from PyQt5.Qt import QApplication
  14. import os
  15. import subprocess
  16. # Initialize Qt resources from file resources.py
  17. from .resources import *
  18. class FilePath:
  19. def __init__(self, iface):
  20. self.iface = iface
  21. def initGui(self):
  22. # Specify the absolute path of the icon
  23. icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon.png")
  24. # Create action that will start plugin configuration
  25. self.action_copy = QAction(QIcon(icon_path), "Copy file path", self.iface.mainWindow())
  26. self.action_copy.triggered.connect(self.copy_layer_path)
  27. # Create a new toolbar named "File Path Toolbar" and add the action to it
  28. self.toolbar = self.iface.addToolBar("File Path Toolbar")
  29. self.toolbar.setObjectName("FilePathToolbar")
  30. self.toolbar.addAction(self.action_copy)
  31. # Also, add action to the plugin menu
  32. self.iface.addPluginToMenu("&File Path", self.action_copy)
  33. # Create another action to open the file location
  34. self.action_open = QAction(QIcon(icon_path), "Open file location", self.iface.mainWindow())
  35. self.action_open.triggered.connect(self.open_file_location)
  36. self.toolbar.addAction(self.action_open)
  37. self.iface.addPluginToMenu("&File Path", self.action_open)
  38. def unload(self):
  39. # Remove the plugin menu item and icon
  40. self.iface.removePluginMenu("&File Path", self.action_copy)
  41. self.iface.removeToolBarIcon(self.action_copy)
  42. self.iface.removePluginMenu("&File Path", self.action_open)
  43. self.iface.removeToolBarIcon(self.action_open)
  44. # Remove the toolbar when the plugin is unloaded
  45. del self.toolbar
  46. def copy_layer_path(self):
  47. # Get the current layer
  48. layer = self.iface.activeLayer()
  49. if layer is None:
  50. self.iface.messageBar().pushMessage("Error", "No layer selected", level=Qgis.Warning)
  51. return
  52. # Get the layer source
  53. layer_path = layer.source()
  54. # Copy the layer source to the clipboard
  55. clipboard = QApplication.clipboard()
  56. clipboard.setText(layer_path)
  57. self.iface.messageBar().pushMessage("Success", "Layer path copied to clipboard", level=Qgis.Success)
  58. def open_file_location(self):
  59. # Get the current layer
  60. layer = self.iface.activeLayer()
  61. if layer is None:
  62. self.iface.messageBar().pushMessage("Error", "No layer selected", level=Qgis.Warning)
  63. return
  64. # Get the layer source
  65. layer_path = layer.source()
  66. # Open the containing folder
  67. folder_path = os.path.dirname(layer_path)
  68. subprocess.Popen(f'explorer "{folder_path}"')