From e2ed5d82ff81fd8c60c9ba5a2637d13f2e3dabed Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Mon, 10 Apr 2006 19:49:24 +0000 Subject: [PATCH] new non-blocking ImageChooserDialog. Fixes #777 --- src/dialogs.py | 70 ++++++++++++++++++++++++++++++++++++++++- src/vcard.py | 85 +++++++++++++------------------------------------- 2 files changed, 91 insertions(+), 64 deletions(-) diff --git a/src/dialogs.py b/src/dialogs.py index 2a78894c0..3ea641c2d 100644 --- a/src/dialogs.py +++ b/src/dialogs.py @@ -1565,7 +1565,75 @@ class SoundChooserDialog(FileChooserDialog): self.add_filter(filter) self.set_filter(filter) - self.set_filename(path_to_snd_file) + if path_to_snd_file: + self.set_filename(path_to_snd_file) + +class ImageChooserDialog(FileChooserDialog): + def __init__(self, path_to_file = '', on_response_ok = None, + on_response_cancel = None): + '''optionally accepts path_to_snd_file so it has that as selected''' + def on_ok(widget, callback): + '''check if file exists and call callback''' + path_to_file = self.get_filename() + try: + path_to_file = path_to_file.decode( + sys.getfilesystemencoding()) + except: + pass + if os.path.exists(path_to_file): + callback(widget, path_to_file) + + try: + if os.name == 'nt': + path = helpers.get_my_pictures_path() + else: + path = os.environ['HOME'] + except: + path = '' + FileChooserDialog.__init__(self, + title_text = _('Choose Image'), + action = gtk.FILE_CHOOSER_ACTION_OPEN, + buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, gtk.RESPONSE_OK), + default_response = gtk.RESPONSE_OK, + current_folder = path, + on_response_ok = (on_ok, on_response_ok), + on_response_cancel = on_response_cancel) + + filter = gtk.FileFilter() + filter.set_name(_('All files')) + filter.add_pattern('*') + self.add_filter(filter) + + filter = gtk.FileFilter() + filter.set_name(_('Images')) + filter.add_mime_type('image/png') + filter.add_mime_type('image/jpeg') + filter.add_mime_type('image/gif') + filter.add_mime_type('image/tiff') + filter.add_mime_type('image/x-xpixmap') # xpm + self.add_filter(filter) + self.set_filter(filter) + + if path_to_file: + self.set_filename(path_to_file) + + self.set_use_preview_label(False) + self.set_preview_widget(gtk.Image()) + self.connect('selection-changed', self.update_preview) + + def update_preview(self, widget): + path_to_file = widget.get_preview_filename() + if path_to_file is None or os.path.isdir(path_to_file): + # nothing to preview or directory + # make sure you clean image do show nothing + widget.get_preview_widget().set_from_file(None) + return + try: + pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(path_to_file, 100, 100) + except gobject.GError: + return + widget.get_preview_widget().set_from_pixbuf(pixbuf) class AddSpecialNotificationDialog: def __init__(self, jid): diff --git a/src/vcard.py b/src/vcard.py index 400c67111..b416c74c4 100644 --- a/src/vcard.py +++ b/src/vcard.py @@ -172,70 +172,27 @@ class VcardWindow: def on_set_avatar_button_clicked(self, widget): f = None - dialog = gtk.FileChooserDialog(_('Choose Avatar'), None, - gtk.FILE_CHOOSER_ACTION_OPEN, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_OK)) - try: - if os.name == 'nt': - path = helpers.get_my_pictures_path() - else: - path = os.environ['HOME'] - except: - path = '' - if path: - dialog.set_current_folder(path) - dialog.set_default_response(gtk.RESPONSE_OK) - filtr = gtk.FileFilter() - filtr.set_name(_('All files')) - filtr.add_pattern('*') - dialog.add_filter(filtr) - - filtr = gtk.FileFilter() - filtr.set_name(_('Images')) - filtr.add_mime_type('image/png') - filtr.add_mime_type('image/jpeg') - filtr.add_mime_type('image/gif') - filtr.add_mime_type('image/tiff') - filtr.add_mime_type('image/x-xpixmap') # xpm - dialog.add_filter(filtr) - dialog.set_filter(filtr) - dialog.set_use_preview_label(False) - dialog.set_preview_widget(gtk.Image()) - dialog.connect('selection-changed', self.update_preview) - - done = False - while not done: - response = dialog.run() - - if response == gtk.RESPONSE_OK: - path_to_file = dialog.get_filename() - path_to_file = gtkgui_helpers.decode_filechooser_file_paths( - (path_to_file,))[0] - filesize = os.path.getsize(path_to_file) # in bytes - if filesize > 16384: # 16 kb - try: - pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file) - # get the image at 'notification size' - # and use that user did not specify in ACE crazy size - scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, - 'notification') - except gobject.GError, msg: # unknown format - dialogs.ErrorDialog(_('Could not load image'), msg) - continue - else: - if scaled_pixbuf: - path_to_file = os.path.join(gajim.TMP, - 'avatar_scaled.png') - scaled_pixbuf.save(path_to_file, 'png') - done = True + def on_ok(widget, path_to_file): + path_to_file = gtkgui_helpers.decode_filechooser_file_paths( + (path_to_file,))[0] + filesize = os.path.getsize(path_to_file) # in bytes + if filesize > 16384: # 16 kb + try: + pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file) + # get the image at 'notification size' + # and use that user did not specify in ACE crazy size + scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, + 'notification') + except gobject.GError, msg: # unknown format + dialogs.ErrorDialog(_('Could not load image'), msg) + return else: - done = True - else: # Cancel or WM X button - done = True - - dialog.destroy() - if response == gtk.RESPONSE_OK: + if scaled_pixbuf: + path_to_file = os.path.join(gajim.TMP, + 'avatar_scaled.png') + scaled_pixbuf.save(path_to_file, 'png') + self.dialog.destroy() + fd = open(path_to_file, 'rb') data = fd.read() pixbuf = gtkgui_helpers.get_pixbuf_from_data(data) @@ -247,6 +204,8 @@ class VcardWindow: # returns None if unknown type self.avatar_mime_type = mimetypes.guess_type(path_to_file)[0] + self.dialog = dialogs.ImageChooserDialog(on_response_ok = on_ok) + def set_value(self, entry_name, value): try: self.xml.get_widget(entry_name).set_text(value)