copy_path.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from PyQt5.QtWidgets import QApplication, QFileDialog
  2. from PyQt5.QtCore import QSettings
  3. from qgis.core import QgsProject
  4. from .uri import Uri
  5. from os.path import relpath
  6. from os import sep
  7. from pathlib import Path
  8. def copy_path(view, reference_type, text_=None, settings=None):
  9. layer = view.currentLayer()
  10. if layer is None:
  11. return
  12. uri = Uri(layer)
  13. uri.strip_filter()
  14. path = None
  15. if reference_type == 'full path':
  16. path = uri.uri()
  17. if settings is not None and settings.dont_copy_layer_name_if_same and uri.database_layer_name_same():
  18. path = uri.database()
  19. elif reference_type == 'database path':
  20. path = uri.database()
  21. elif reference_type == 'file name':
  22. path = uri.layer_name()
  23. if not uri.is_memory_layer():
  24. path = f'{path}{uri.ext()}'
  25. elif reference_type == 'relative path workspace':
  26. wor = QgsProject.instance().absolutePath()
  27. if not wor:
  28. path = uri.uri()
  29. if settings is not None and settings.dont_copy_layer_name_if_same and uri.database_layer_name_same():
  30. path = uri.database()
  31. elif Path(uri.database()).anchor != Path(wor).anchor:
  32. path = uri.uri()
  33. if settings is not None and settings.dont_copy_layer_name_if_same and uri.database_layer_name_same():
  34. path = uri.database()
  35. else:
  36. db = relpath(uri.database(), wor)
  37. uri_ = Uri()
  38. uri_.set_database(db)
  39. if uri.is_database():
  40. uri_.set_layer_name(uri.layer_name())
  41. uri_.build(uri)
  42. path = uri_.uri()
  43. if settings is not None and settings.dont_copy_layer_name_if_same and uri_.database_layer_name_same():
  44. path = uri_.database()
  45. elif reference_type == 'relative path':
  46. if not text_:
  47. wor = QFileDialog.getExistingDirectory(view, 'Relative to Directory', uri.database())
  48. if wor is None:
  49. return
  50. else:
  51. recent_paths = QSettings().value('file_management/recent_rel_paths', [])
  52. if wor not in recent_paths:
  53. recent_paths.insert(0, wor)
  54. else:
  55. recent_paths.remove(wor)
  56. recent_paths.insert(0, wor)
  57. if len(recent_paths) > 5:
  58. recent_paths = recent_paths[:5]
  59. QSettings().setValue('file_management/recent_rel_paths', recent_paths)
  60. else:
  61. wor = text_
  62. if not wor:
  63. path = uri.uri()
  64. if settings is not None and settings.dont_copy_layer_name_if_same and uri.database_layer_name_same():
  65. path = uri.database()
  66. elif Path(uri.database()).anchor != Path(wor).anchor:
  67. path = uri.uri()
  68. if settings is not None and settings.dont_copy_layer_name_if_same and uri.database_layer_name_same():
  69. path = uri.database()
  70. else:
  71. db = relpath(uri.database(), wor)
  72. uri_ = Uri()
  73. uri_.set_database(db)
  74. if uri.is_database():
  75. uri_.set_layer_name(uri.layer_name())
  76. uri_.build(uri)
  77. path = uri_.uri()
  78. if settings is not None and settings.dont_copy_layer_name_if_same and uri_.database_layer_name_same():
  79. path = uri_.database()
  80. if path is not None:
  81. path = path.replace('\\', sep).replace('/', sep)
  82. if settings is not None and not settings.use_default_delimiter and '|layername=' in path:
  83. path = path.replace('|layername=', settings.custom_delimiter)
  84. clipboard = QApplication.clipboard()
  85. clipboard.setText(path)