Now i have got it to work like i want, but I think it have to be a better way of doing it. But because i got no answers I do it my way :)
I am not dragging the pixbuf, instead i am dragging the path and name of the image in one string. Then I make a pixbuf out of the path and displays the pixbuf and the image name in the iconview.
ListStore "name:path", "name", Pixbu
Feel free to comment and improve the code...
Code:
#!/usr/bin/pyhon3.2
# -*- encoding:utf-8 -*-
from gi.repository import Gdk, Gtk
from gi.repository.GdkPixbuf import Pixbuf
COLUMN_TEXT_NAME_PATH, COLUMN_TEXT_NAME, COLUMN_PIXBUF = range(3)
(TARGET_ENTRY_TEXT, TARGET_ENTRY_PIXBUF) = range(2)
class mainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="dnd")
# Window possitions
self.set_default_size(750, 600)
self.set_position(Gtk.WindowPosition.CENTER)
# Style
screen = Gdk.Screen.get_default()
css_provider = Gtk.CssProvider()
css_provider.load_from_path("myCSS.css")
context = Gtk.StyleContext()
context.add_provider_for_screen(screen, css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
#Widget layout
hBox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
homogeneous=True, spacing=3)
hBox.set_border_width(6)
self.add(hBox)
self.source_iconview = myIconview()
self.source_iconview.set_name("source")
self.source_iconview.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.MOVE)
self.source_iconview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK,
[], Gdk.DragAction.COPY)
self.day1 = myIconview()
self.day1.set_name("day1")
self.day1.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY | Gdk.DragAction.MOVE)
self.day1.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK,
[], Gdk.DragAction.MOVE)
self.day2 = myIconview()
self.day2.set_name("day2")
self.day2.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY | Gdk.DragAction.MOVE)
self.day2.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK,
[], Gdk.DragAction.MOVE)
self.source_iconview.connect("drag-data-get", self.source_iconview.on_drag_get)
self.source_iconview.connect("drag-data-received", self.source_iconview.on_drag_recive)
self.day1.connect("drag-data-get", self.day1.on_drag_get)
self.day1.connect("drag-data-received", self.day1.on_drag_recive)
self.day2.connect("drag-data-get", self.day2.on_drag_get)
self.day2.connect("drag-data-received", self.day2.on_drag_recive)
self.source_iconview.drag_dest_add_text_targets()
self.source_iconview.drag_source_add_text_targets()
self.day1.drag_dest_add_text_targets()
self.day1.drag_source_add_text_targets()
self.day2.drag_dest_add_text_targets()
self.day2.drag_source_add_text_targets()
self.source_iconview.add_item_to_model("image1"+":"+"/home/user/test/image1.svg")
self.source_iconview.add_item_to_model("image2"+":"+ "/home/user/test/image2.jpg")
self.source_iconview.add_item_to_model("image3"+":"+"/home/user/test/image3.jpg")
hBox.pack_start(self.source_iconview, True, True, 0)
hBox.pack_start(self.day1, True, True, 0)
hBox.pack_start(self.day2, True, True, 0)
class myIconview(Gtk.IconView):
__gtype_name__ = 'MyIconView'
def __init__(self):
Gtk.IconView.__init__(self)
#self.set_text_column(COLUMN_TEXT_NAME_PATH)
self.set_row_spacing(4)
self.set_text_column(COLUMN_TEXT_NAME)
self.set_pixbuf_column(COLUMN_PIXBUF)
self.model = Gtk.ListStore(str, str, Pixbuf)
self.set_model(self.model)
self.set_columns(1)
def on_drag_get(self, widget, drag_context, data, info, time):
selected_path = self.get_selected_items()[0]
selected_iter = self.get_model().get_iter(selected_path)
print(self.get_name())
model = self.get_model()
#This gets the selected row columns value:
namePath = model.get_value(selected_iter, COLUMN_TEXT_NAME_PATH)
data.set_text(namePath, -1)
def on_drag_recive(self, widget, drag_context, x, y, selection_data, info, time):
model = self.get_model()
namePath = selection_data.get_text()
name, path = str.split(namePath,":")
pixbuf = Pixbuf.new_from_file_at_size(path,28, -1)
# Do not add an image dragged to the source iconview.
if self.get_name() != "source":
try:
path, position = widget.get_dest_item_at_pos(x, y)
myIter = model.get_iter(path)
except:
model.append([namePath, name, pixbuf])
print("append", [namePath, name, pixbuf])
position = None
if (position == Gtk.IconViewDropPosition.DROP_LEFT or position == Gtk.IconViewDropPosition.DROP_ABOVE or position == Gtk.IconViewDropPosition.DROP_INTO):
model.insert_before(myIter,[namePath,name, pixbuf])
print("left")
elif (position == Gtk.IconViewDropPosition.DROP_RIGHT or position == Gtk.IconViewDropPosition.DROP_BELOW):
model.insert_after(myIter, [namePath,name, pixbuf])
print("below")
def add_item_to_model(self, namePath):
name, path = str.split(namePath,":")
pixbuf = Pixbuf.new_from_file_at_size(path,28, -1)
self.model.append([(name + ":" + path), (name), (pixbuf)])
win = mainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()