Friday, January 1, 2010

File Prompts ... never write File Selection Code again



Today I wrote two new functions in quidgets.prompts open_image_file, and close_image_file. These prompts allow a user to select and save a file, and include free functionality for the developer:
  • Built in filtering for images (note the "Images" filter set in the open dialog above).
  • Automatically starts in Pictures directory (unless a different default directory is included by the developer).
  • Automatic detection of an existing file in the save dialog and a prompt to replace or cancel.
Currently, these dialogs don't actually handle saving or opening the files, just get paths that the user selects, so the code for opening and saving files in Photobomb looks like this ...

   def open_image(self, widget, data=None):
result, image_path = prompts.open_image_file("Photobomb")
if result == gtk.RESPONSE_OK:
self.__image_path = image_path
self.builder.get_object("image1").set_from_file(self.__image_path)
def save_as(self, widget, data=None):
result, image_target = prompts.save_image_file("Save Photobomb")
if result == gtk.RESPONSE_OK:
image_source = os.path.join(self.__tmp_dir, "img.png")
shutil.copy2(image_source,image_target)
A significant reduction in the amount of code normally required. More open/save prompts to come, in the meantime, I just need to spend some time with the Gimp to make some more progress on Photo bomb before I upload it to my PPA.




About Quidgets
Quickly + Widgets = Quidgets
There is a Launchpad Project for Quidgets
The most up to date changes are in the Quidgets Trunk Branch

No comments:

Post a Comment