/GUI/Gtk/BaseFileDialogs.py

https://bitbucket.org/alsh/pygui-mirror · Python · 214 lines · 87 code · 24 blank · 103 comment · 26 complexity · f015fb090042b95359b0b3fe6a1a6d1d MD5 · raw file

  1. #
  2. # Python GUI - File selection dialogs - Gtk
  3. #
  4. import os
  5. import gtk
  6. from Files import FileRef
  7. from AlertFunctions import confirm
  8. from Applications import application
  9. #------------------------------------------------------------------
  10. class _FileDialog(gtk.FileChooserDialog):
  11. def __init__(self, ok_label, **kwds):
  12. gtk.FileChooserDialog.__init__(self, **kwds)
  13. self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)
  14. self.add_button(ok_label, gtk.RESPONSE_ACCEPT)
  15. self.connect('response', self.response)
  16. self.set_default_size(600, 600)
  17. self.set_position(gtk.WIN_POS_CENTER)
  18. def add_file_type(self, file_type):
  19. suffix = file_type.suffix
  20. if suffix:
  21. filter = gtk.FileFilter()
  22. name = file_type.name
  23. if name:
  24. filter.set_name(name)
  25. filter.add_pattern("*.%s" % suffix)
  26. self.add_filter(filter)
  27. def present_modally(self):
  28. return self.run() == gtk.RESPONSE_ACCEPT
  29. def response(self, _, id):
  30. #print "_FileDialog.response:", id ###
  31. if id == gtk.RESPONSE_ACCEPT:
  32. if not self.check():
  33. self.stop_emission('response')
  34. def check(self):
  35. return True
  36. #------------------------------------------------------------------
  37. class _SaveFileDialog(_FileDialog):
  38. def check(self):
  39. path = self.get_filename()
  40. #print "_SaveFileDialog.ok: checking path %r" % path ###
  41. #if path is None:
  42. # return False
  43. if not os.path.exists(path):
  44. return True
  45. else:
  46. result = confirm("Replace existing '%s'?" % os.path.basename(path),
  47. "Cancel", "Replace", cancel = None)
  48. return result == 0
  49. #------------------------------------------------------------------
  50. def _request_old(prompt, default_dir, file_types, dir, multiple):
  51. if prompt.endswith(":"):
  52. prompt = prompt[:-1]
  53. if dir:
  54. action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
  55. else:
  56. action = gtk.FILE_CHOOSER_ACTION_OPEN
  57. dlog = _FileDialog(title = prompt, action = action,
  58. ok_label = gtk.STOCK_OPEN)
  59. dlog.set_select_multiple(multiple)
  60. if file_types:
  61. for file_type in file_types:
  62. dlog.add_file_type(file_type)
  63. if default_dir:
  64. dlog.set_current_folder(default_dir.path)
  65. if dlog.present_modally():
  66. if multiple:
  67. result = [FileRef(path = path) for path in dlog.get_filenames()]
  68. else:
  69. result = FileRef(path = dlog.get_filename())
  70. else:
  71. result = None
  72. dlog.destroy()
  73. return result
  74. #------------------------------------------------------------------
  75. #def request_old_file(prompt = "Open File", default_dir = None,
  76. # file_types = None, multiple = False):
  77. # """Present a dialog for selecting an existing file or set of files.
  78. # Returns a FileRef, or None if cancelled."""
  79. #
  80. # if prompt.endswith(":"):
  81. # prompt = prompt[:-1]
  82. # dlog = _FileDialog(title = prompt, ok_label = gtk.STOCK_OPEN)
  83. # dlog.set_select_multiple(multiple)
  84. # if file_types:
  85. # for file_type in file_types:
  86. # dlog.add_file_type(file_type)
  87. # if default_dir:
  88. # dlog.set_current_folder(default_dir.path)
  89. # if dlog.present_modally():
  90. # if multiple:
  91. # result = [FileRef(path = path) for path in dlog.get_filenames()]
  92. # else:
  93. # result = FileRef(path = dlog.get_filename())
  94. # else:
  95. # result = None
  96. # dlog.destroy()
  97. # return result
  98. #------------------------------------------------------------------
  99. #def request_old_directory(prompt = "Choose Folder", default_dir = None,
  100. # multiple = False):
  101. # """Present a dialog for selecting an existing directory or set of directories.
  102. # Returns a DirRef, or None if cancelled."""
  103. #
  104. # if prompt.endswith(":"):
  105. # prompt = prompt[:-1]
  106. # dlog = _FileDialog(title = prompt, ok_label = gtk.STOCK_OPEN,
  107. # action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
  108. # dlog.set_select_multiple(multiple)
  109. # if default_dir:
  110. # dlog.set_current_folder(default_dir.path)
  111. # if dlog.present_modally():
  112. # if multiple:
  113. # result = [DirRef(path = path) for path in dlog.get_filenames()]
  114. # else:
  115. # result = DirRef(path = dlog.get_filename())
  116. # else:
  117. # result = None
  118. # dlog.destroy()
  119. # return result
  120. #------------------------------------------------------------------
  121. def _request_new(prompt, default_dir, default_name, file_type, dir):
  122. if dir:
  123. action = gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER
  124. else:
  125. action = gtk.FILE_CHOOSER_ACTION_SAVE
  126. if prompt.endswith(":"):
  127. prompt = prompt[:-1]
  128. dlog = _SaveFileDialog(title = prompt, action = action,
  129. ok_label = gtk.STOCK_SAVE)
  130. if file_type:
  131. dlog.add_file_type(file_type)
  132. if default_dir:
  133. dlog.set_current_folder(default_dir.path)
  134. if default_name:
  135. dlog.set_current_name(default_name)
  136. if dlog.present_modally():
  137. path = dlog.get_filename()
  138. if file_type:
  139. path = file_type._add_suffix(path)
  140. result = FileRef(path = path)
  141. else:
  142. result = None
  143. dlog.destroy()
  144. return result
  145. #------------------------------------------------------------------
  146. #def request_new_file(prompt = "Save File", default_dir = None,
  147. # default_name = "", file_type = None):
  148. # """Present a dialog requesting a name and location for a new file.
  149. # Returns a FileRef, or None if cancelled."""
  150. #
  151. # if prompt.endswith(":"):
  152. # prompt = prompt[:-1]
  153. # dlog = _SaveFileDialog(title = prompt, ok_label = gtk.STOCK_SAVE,
  154. # action = gtk.FILE_CHOOSER_ACTION_SAVE)
  155. # if file_type:
  156. # dlog.add_file_type(file_type)
  157. # if default_dir:
  158. # dlog.set_current_folder(default_dir.path)
  159. # if default_name:
  160. # dlog.set_current_name(default_name)
  161. # if dlog.present_modally():
  162. # path = dlog.get_filename()
  163. # if file_type:
  164. # path = file_type._add_suffix(path)
  165. # result = FileRef(path = path)
  166. # else:
  167. # result = None
  168. # dlog.destroy()
  169. # return result
  170. #------------------------------------------------------------------
  171. #def request_new_directory(prompt = "Create Folder", default_dir = None,
  172. # default_name = ""):
  173. # """Present a dialog requesting a name and location for a new directory.
  174. # Returns a FileRef, or None if cancelled."""
  175. #
  176. # if prompt.endswith(":"):
  177. # prompt = prompt[:-1]
  178. # dlog = _SaveFileDialog(title = prompt, ok_label = gtk.STOCK_SAVE,
  179. # action = gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER)
  180. # if default_dir:
  181. # dlog.set_current_folder(default_dir.path)
  182. # if default_name:
  183. # dlog.set_current_name(default_name)
  184. # if dlog.present_modally():
  185. # path = dlog.get_filename()
  186. # result = FileRef(path = path)
  187. # else:
  188. # result = None
  189. # dlog.destroy()
  190. # return result