file_dialog.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from typing import Callable, Optional
  17. import os
  18. from PyQt5.QtCore import Qt
  19. from PyQt5.QtWidgets import QFileDialog
  20. class FileDialog:
  21. def __init__(self, title: str) -> None:
  22. self.callback: Optional[Callable[[str], None]] = None
  23. self.file_dialog = QFileDialog()
  24. self.file_dialog.setWindowModality(Qt.ApplicationModal)
  25. self.filename = ""
  26. def on_selected(path: str) -> None:
  27. self.filename = path
  28. self.file_dialog.fileSelected.connect(on_selected)
  29. def run(self, callback: Callable[[str], None]) -> None:
  30. self.callback = callback
  31. if self.callback:
  32. self.file_dialog.fileSelected.connect(self.callback)
  33. self.file_dialog.exec_()
  34. def get_filename(self) -> str:
  35. return self.filename
  36. def set_directory(self, *dirs: str) -> None:
  37. path = os.path.join(*dirs)
  38. self.filename = path
  39. self.file_dialog.setDirectory(path)
  40. class OpenFileDialog(FileDialog):
  41. def __init__(self, title: str, filters: tuple[str, ...] = ("All Files (*)",)) -> None:
  42. super().__init__(title)
  43. self.file_dialog.setNameFilters(filters)
  44. self.file_dialog.setAcceptMode(QFileDialog.AcceptOpen)
  45. self.file_dialog.setFileMode(QFileDialog.ExistingFile)
  46. class SaveFileDialog(FileDialog):
  47. def __init__(self, title: str, default_suffix: str = "") -> None:
  48. super().__init__(title)
  49. # FIXME: Not working!?
  50. self.file_dialog.setDefaultSuffix(default_suffix)
  51. self.file_dialog.setAcceptMode(QFileDialog.AcceptSave)
  52. self.file_dialog.setFileMode(QFileDialog.AnyFile)
  53. class OpenDirectoryDialog(OpenFileDialog):
  54. def __init__(self, title: str, filters: tuple[str, ...] = ("All Files (*)",)):
  55. super().__init__(title, filters)
  56. self.file_dialog.setFileMode(QFileDialog.Directory)
  57. class SaveDirectoryDialog(SaveFileDialog):
  58. def __init__(self, title: str) -> None:
  59. super().__init__(title)
  60. self.file_dialog.setFileMode(QFileDialog.Directory)
  61. # EOF #