Hello. I'm trying to build an application which converts between formats. I plan to do it using two IconViews: one for the input, and another one just at the side for the output. That way, the user drags and drops one file into the input IconView, then she can see that file listed, and the backend program starts to convert it, then generating the output at the other IconView. Internally, a temporal file would be generated transparently for the user, and then another one for the output also. But when the user drags the file from the output IconView, it gets the proper name (not the randomly generated temporal one).
Until now, I have programmed the InputView, and give it the ability to input a file which gets listed (anything happens to that file: it's only displayed). I attach the current application. How can I show up the icon for the file which is dragged into it?? Also, how can I display a proper name (only the filename, not the path) linking that name to a temporal file with random filename?? This is necessary to hide the user the complicated name of the temporal files, and also to give the application some consistency. I have also a related question: How can I stablish a relationship between this icon and the file with different name? I don't understand that point in the documentation. Of course, I have read it throughfully. Thank you!! Any ideas are welcome, I'm just a beginner. -- Néstor +34 687 96 74 81 [email protected]
#!/usr/bin/env python # -*- coding: utf-8 -*- # (c) 2009 Néstor Amigo Cairo <[email protected]> # Licensed under GPLv3 license or later, at your option # A first test for the Component GUI system. import pygtk pygtk.require('2.0') import gtk import gtk.gdk import time DND_URI_TUPLE = ("text/uri-list", 0, 25) DND_FILESOURCE_TUPLE = ("pitivi/file-source", 0, 26) if gtk.gtk_version < (2, 12): import warnings msg = ('This example tested with version 2.12.9 of gtk. Your using version %d.%d.%d. Your milage may vary.' % gtk.gtk_version) warnings.warn(msg) class FileList(object): # First create an iconview view = gtk.IconView() # Create a store for our iconview and fill it with stock icons store = gtk.ListStore(str, gtk.gdk.Pixbuf) # Pack our iconview into a scrolled window swin = gtk.ScrolledWindow() # pack the scrolled window into a simple dialog and run it dialog = gtk.Dialog('MSG to MIME conversor') def __init__(self): # for attr in dir(gtk): # if attr.startswith('STOCK_'): # stock_id = getattr(gtk, attr) # pixbuf = self.view.render_icon(stock_id, # size=gtk.ICON_SIZE_BUTTON, detail=None) # if pixbuf: # self.store.append(['gtk.%s' % attr, pixbuf]) # Connect our iconview with our store self.view.set_model(self.store) # Map store text and pixbuf columns to iconview self.view.set_text_column(0) self.view.set_pixbuf_column(1) self.swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.swin.add_with_viewport(self.view) self.swin.show_all() self.view.ipcTargets = [('image/png', 0, 0)] # view.ipcTargets = [('application/x-ole-storage', 0, 0)] # view.drag_source_set(gtk.gdk.BUTTON1_MASK, view.ipcTargets, gtk.gdk.ACTION_COPY) # view.connect('drag_data_get', self.drag_data_get # Drag and drop self.view.drag_dest_set(gtk.DEST_DEFAULT_DROP | gtk.DEST_DEFAULT_MOTION, [DND_URI_TUPLE], gtk.gdk.ACTION_COPY) # self.view.drag_source_set(gtk.gdk.BUTTON1_MASK, # [DND_URI_TUPLE, DND_FILESOURCE_TUPLE], # gtk.gdk.ACTION_COPY) self.view.connect("drag_begin", self._dnd_icon_begin) self.view.connect("drag_data_get", self._dnd_icon_data_get) self.view.connect("drag_data_received", self._dnd_data_received) close = self.dialog.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_NONE) self.dialog.set_default_size(400,400) self.dialog.vbox.pack_start(self.swin) self.dialog.run() # def drag_data_get(view, widget, context, selection_data, info, timestamp): # "Provide data to be copied in a drag-drop" # selected = view.get_selected_items() # if len(selected) is 0: # contents = '' # else: # contents = dialog.store[selected[0][0]][0] # the image xml # selection_data.set(selection_data.target, 8, contents) # # def on_iconview_drag_begin(dialog, widget, context): # selected = view.get_selected_items() # if selected: # view.drag_source_set_icon_pixbuf(store[selected[0][0]][1]) # else: # view.drag_source_set_icon_stock('gtk-dnd') def _dnd_data_received(self, widget, context, x, y, selection, targetType, time): # filenames = [x.strip() for x in selection.data.strip().split("\n")] # self.view.add_files(filenames) print 'targetType: %i' %targetType if targetType == 25: filename = selection.data pixbuf = gtk.gdk.pixbuf_new_from_file('/home/nestor/Proyectos/componentGui/dragtargets.py') self.store.append([filename, 0]) else: print 'This type of target (%i) is not allowed to be pasted here.' %targetType print 'Please, only paste UNIX files.' def _dnd_icon_begin(self, widget, context): print "icon drag_begin" items = self.view.get_selected_items() print "got", len(items), "items" if len(items) < 1: context.drag_abort(int(time.time())) else: if len(items) == 1: thumbnail = self.store.get_value(self.store.get_iter(items[0]), 0) self.view.drag_source_set_icon_pixbuf(thumbnail) def _dnd_icon_data_get(self, widget, context, selection, targetType, eventTime): # calls context.drag_abort(time) if not in a valide place print "icon list data_get, type:", targetType # get the list of selected uris uris = [self.store.get_value(self.store.get_iter(x), 5) for x in self.view.get_selected_items()] if len(uris) < 1: return if targetType == DND_TYPE_PITIVI_FILESOURCE: selection.set(selection.target, 8, uris[0]) elif targetType == DND_TYPE_URI_LIST: selection.set(selection.target, 8, string.join(uris, "\n")) if __name__ == '__main__': dialog = FileList()
_______________________________________________ pygtk mailing list [email protected] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
