FileSelectionPanel.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. ***************************************************************************
  3. FileSelectionPanel.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. import warnings
  22. from qgis.PyQt import uic
  23. from qgis.PyQt.QtWidgets import QFileDialog
  24. from qgis.core import QgsSettings
  25. from processing.tools.system import isWindows
  26. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  27. with warnings.catch_warnings():
  28. warnings.filterwarnings("ignore", category=DeprecationWarning)
  29. WIDGET, BASE = uic.loadUiType(
  30. os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
  31. class FileSelectionPanel(BASE, WIDGET):
  32. def __init__(self, isFolder, ext=None):
  33. super().__init__(None)
  34. self.setupUi(self)
  35. self.ext = ext or '*'
  36. self.isFolder = isFolder
  37. self.btnSelect.clicked.connect(self.showSelectionDialog)
  38. def showSelectionDialog(self):
  39. # Find the file dialog's working directory
  40. settings = QgsSettings()
  41. text = self.leText.text()
  42. if os.path.isdir(text):
  43. path = text
  44. elif os.path.isdir(os.path.dirname(text)):
  45. path = os.path.dirname(text)
  46. elif settings.contains('/Processing/LastInputPath'):
  47. path = settings.value('/Processing/LastInputPath')
  48. else:
  49. path = ''
  50. if self.isFolder:
  51. folder = QFileDialog.getExistingDirectory(self,
  52. self.tr('Select Folder'), path)
  53. if folder:
  54. self.leText.setText(folder)
  55. settings.setValue('/Processing/LastInputPath',
  56. os.path.dirname(folder))
  57. else:
  58. filenames, selected_filter = QFileDialog.getOpenFileNames(self,
  59. self.tr('Select File'), path, self.tr('{} files').format(self.ext.upper()) + ' (*.' + self.ext + self.tr(');;All files (*.*)'))
  60. if filenames:
  61. self.leText.setText(';'.join(filenames))
  62. settings.setValue('/Processing/LastInputPath',
  63. os.path.dirname(filenames[0]))
  64. def getValue(self):
  65. s = self.leText.text()
  66. if isWindows():
  67. s = s.replace('\\', '/')
  68. return s
  69. def setText(self, text):
  70. self.leText.setText(text)