new non-blocking ImageChooserDialog. Fixes #777

This commit is contained in:
Yann Leboulanger 2006-04-10 19:49:24 +00:00
parent c6ce8c763a
commit e2ed5d82ff
2 changed files with 91 additions and 64 deletions

View File

@ -1565,7 +1565,75 @@ class SoundChooserDialog(FileChooserDialog):
self.add_filter(filter) self.add_filter(filter)
self.set_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: class AddSpecialNotificationDialog:
def __init__(self, jid): def __init__(self, jid):

View File

@ -172,70 +172,27 @@ class VcardWindow:
def on_set_avatar_button_clicked(self, widget): def on_set_avatar_button_clicked(self, widget):
f = None f = None
dialog = gtk.FileChooserDialog(_('Choose Avatar'), None, def on_ok(widget, path_to_file):
gtk.FILE_CHOOSER_ACTION_OPEN, path_to_file = gtkgui_helpers.decode_filechooser_file_paths(
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, (path_to_file,))[0]
gtk.STOCK_OPEN, gtk.RESPONSE_OK)) filesize = os.path.getsize(path_to_file) # in bytes
try: if filesize > 16384: # 16 kb
if os.name == 'nt': try:
path = helpers.get_my_pictures_path() pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
else: # get the image at 'notification size'
path = os.environ['HOME'] # and use that user did not specify in ACE crazy size
except: scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf,
path = '' 'notification')
if path: except gobject.GError, msg: # unknown format
dialog.set_current_folder(path) dialogs.ErrorDialog(_('Could not load image'), msg)
dialog.set_default_response(gtk.RESPONSE_OK) return
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
else: else:
done = True if scaled_pixbuf:
else: # Cancel or WM X button path_to_file = os.path.join(gajim.TMP,
done = True 'avatar_scaled.png')
scaled_pixbuf.save(path_to_file, 'png')
dialog.destroy() self.dialog.destroy()
if response == gtk.RESPONSE_OK:
fd = open(path_to_file, 'rb') fd = open(path_to_file, 'rb')
data = fd.read() data = fd.read()
pixbuf = gtkgui_helpers.get_pixbuf_from_data(data) pixbuf = gtkgui_helpers.get_pixbuf_from_data(data)
@ -247,6 +204,8 @@ class VcardWindow:
# returns None if unknown type # returns None if unknown type
self.avatar_mime_type = mimetypes.guess_type(path_to_file)[0] 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): def set_value(self, entry_name, value):
try: try:
self.xml.get_widget(entry_name).set_text(value) self.xml.get_widget(entry_name).set_text(value)