First, please, try to keep in the mailing list (Reply-to all).

Thank you for your response! This place is quite too calm.

I would say to generate it. I have managed to find the way to find the
type of the file using gnomevfs, but I'm not currently able to
generate it from that icon type and put it in a pixbuffer. I attach
some code. I'm using set_from_icon_name() with the path of the file as
the first parameter. The best I get it a Gtk.Image with -supposedly-
the right icon in it. Do you know what I'm speaking about??

Merci beaucoup.

On Tue, Feb 17, 2009 at 6:47 PM, nbd <[email protected]> wrote:
> Néstor a écrit :
>> Hi there again, people,
>>
>> I'm having a similar problem, but with dragging in an IconView. I want
>> to drag some files from the Nautilus window and drop into an iconview.
>> Then, show the filename and an icon, just it, without copying anything
>> on the disk. I'm able to show the filename, but not the icon (or a
>> file preview, or something which matches the file's content or just
>> it's extension).
>>
>> Any ideas?? I'm just a newbie, I have read the documentation
>> carefully, but this has not helped me very much.
>>
>>
> Your problem is too generate the icon or get the icon ?
>
>



-- 
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
from urlparse import urlparse
import os.path
import gnomevfs
 
 
class GnomeFileIcons:
	def __init__( self ):
		self.all_icons = gtk.icon_theme_get_default().list_icons()
		self.cache = {}
 
	def getIcon( self, path ):
		if not os.path.exists(path):
			return gtk.STOCK_FILE
 
		#get mime type
		mime_type = gnomevfs.get_mime_type( path ).replace( '/', '-' )
 
		#search in the cache
		if mime_type in self.cache:
			return self.cache[mime_type]
 
		#try gnome mime
		items = mime_type.split('-')
		for aux in xrange(len(items)-1):
			icon_name = "gnome-mime-" + '-'.join(items[:len(items)-aux])
			if icon_name in self.all_icons:
				#print "icon: " + icon_name
				self.cache[mime_type] = icon_name
				return icon_name
 
		#try folder
		if os.path.isdir(path):
			icon_name = 'folder'
			if icon_name in self.all_icons:
				#print "icon: " + icon_name
				self.cache[mime_type] = icon_name
				return icon_name
 
			#print "icon: " + icon_name
			icon_name = gtk.STOCK_DIRECTORY
			self.cache[mime_type] = icon_name
			return icon_name
 
		#try simple mime
		for aux in xrange(len(items)-1):
			icon_name = '-'.join(items[:len(items)-aux])
			if icon_name in self.all_icons:
				#print "icon: " + icon_name
				self.cache[mime_type] = icon_name
				return icon_name
 
		#file icon
		icon_name = gtk.STOCK_FILE
		self.cache[mime_type] = icon_name
		return icon_name


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
    filename = urlparse(selection.data)
    fileicons = GnomeFileIcons()
    #pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 64, 64)
    piximage = gtk.Image()
    piximage.set_from_icon_name(fileicons.getIcon(filename.path),25)
    pixbuf = piximage.get_pixbuf()
    print pixbuf
    print filename.path
    self.store.append([selection.data, pixbuf])
#    if targetType == 80: # If targetType == TEXT
#      print 'Error. No icon.'
#    else:
#      for x in selection.data.strip():
#        pixbuf = self.view.render_icon(stock_id,
#                  size=gtk.ICON_SIZE_BUTTON, detail=None)
#        if pixbuf:
#          self.store.append(['%s' %x, pixbuf])

  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/

Reply via email to