filesystem_selectors.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from PyQt5.QtWidgets import QGroupBox, QLabel, QSizePolicy, QPushButton, \
  2. QHBoxLayout, QFileDialog, QMessageBox
  3. import os
  4. def raiseInfo(str, parent):
  5. QMessageBox.information(parent, 'Warning!', str)
  6. class PathChooser(QGroupBox):
  7. def choose_path_and_init(self):
  8. pass
  9. def __init__(self, title, parent=None, button_name="Select"):
  10. super().__init__(title, parent=parent)
  11. self.path_label = QLabel()
  12. self.path_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
  13. self.choose_path_button = QPushButton(button_name)
  14. self.choose_path_button.setMaximumHeight(30)
  15. self.choose_path_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  16. self.choose_path_button.clicked.connect(self.choose_path_and_init)
  17. hbox = QHBoxLayout()
  18. hbox.addWidget(self.path_label)
  19. hbox.addWidget(self.choose_path_button)
  20. self.path_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
  21. self.setMaximumHeight(60)
  22. self.setLayout(hbox)
  23. def setText(self, text):
  24. self.path_label.setText(text)
  25. def getText(self):
  26. return self.path_label.text()
  27. class DirSelector(PathChooser):
  28. def __init__(self, title, parent=None, dialogTitle='',
  29. dialogDefaultPath=None, **kwargs):
  30. super().__init__(title, parent, **kwargs)
  31. self.dialogTitle = dialogTitle
  32. if dialogDefaultPath is None or not os.path.isdir(dialogDefaultPath):
  33. dialogDefaultPath = os.path.expanduser('~')
  34. self.dialogDefaultPath = dialogDefaultPath
  35. def choose_path_and_init(self):
  36. dirPath = self.choose_path()
  37. self.setText(dirPath)
  38. def choose_path(self):
  39. return QFileDialog.getExistingDirectory(parent=self,
  40. dir=self.dialogDefaultPath,
  41. caption=self.dialogTitle,
  42. options=QFileDialog.ShowDirsOnly
  43. )
  44. def setText(self, text):
  45. if os.path.isdir(text) or os.path.isfile(text) or text is '':
  46. self.path_label.setText(text)
  47. else:
  48. raiseInfo('No such file or directory: ' + text, self)
  49. # pass
  50. class FileSelector(PathChooser):
  51. def choose_path_and_init(self):
  52. file_path = self.choose_path()
  53. if file_path:
  54. self.setText(file_path)
  55. def choose_path(self):
  56. pass
  57. def __init__(self, widget_title, parent=None,
  58. dialog_title='', default_dir=None, filter='All Files(*.*)',
  59. **kwargs):
  60. super().__init__(widget_title, parent, **kwargs)
  61. self.dialogTitle = dialog_title
  62. if default_dir is None or not os.path.isdir(default_dir):
  63. default_dir = os.path.expanduser('~')
  64. self.dialogDefaultPath = default_dir
  65. self.dialogFileTypeFilter = filter
  66. class FileSelectorExisting(FileSelector):
  67. def __init__(self, **kwargs):
  68. super().__init__(**kwargs)
  69. def setText(self, text):
  70. if os.path.isdir(text) or os.path.isfile(text) or text is '':
  71. self.path_label.setText(text)
  72. else:
  73. raiseInfo('No such file or directory: ' + text, self)
  74. # pass
  75. def choose_path(self):
  76. filePath, filter = QFileDialog.getOpenFileName(parent=self,
  77. caption=self.dialogTitle,
  78. directory=self.dialogDefaultPath,
  79. filter=self.dialogFileTypeFilter)
  80. return filePath
  81. class FileSaver(FileSelector):
  82. def __init__(self, **kwargs):
  83. super().__init__(**kwargs)
  84. self.setText(self.dialogDefaultPath)
  85. def choose_path(self):
  86. filename, file_filter = QFileDialog.getSaveFileName(parent=self,
  87. caption=self.dialogTitle,
  88. directory=self.dialogDefaultPath,
  89. filter=self.dialogFileTypeFilter)
  90. return filename