GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Oct 20, 2014 1:05 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [~SOLVED] Iconview, dnd from two columns? Python3.2 and Gtk3
PostPosted: Sat Feb 04, 2012 2:00 pm 
Offline
Familiar Face

Joined: Sat Feb 04, 2012 12:41 pm
Posts: 10
Hi,
I have followed this example (at the end of the page):
http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/drag_and_drop.html?highlight=drag%20and%20drop
And it works well, but I need to change it to work as I need it to work.

Now I have two iconview widgets and I want to drag the selected pixbuf column and text column to the other iconview widget. As it is now I can just drag the image or the text. Just like the example except I am dragging it to my second iconview widget.

Can someone add one more iconview widget to that example and a button that lets you drag the pixbuf and corresponding text to that iconview, so it looks like it does now in the original iconview?

My own code is a mess right now after to much testing so it is easier to change the original example... :)

But this is how I add items to the second IconView

Code:
    def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
        model = widget.get_model()
        pixbuf = data.get_pixbuf()
        text = data.get_text()
        try:
            path, position = widget.get_dest_item_at_pos(x, y)
            myIter = model.get_iter(path)

        except:
            model.append([text, pixbuf])
            position = None

        if (position == Gtk.IconViewDropPosition.DROP_LEFT or position == Gtk.IconViewDropPosition.DROP_ABOVE):
            model.insert_before(myIter, [text, pixbuf])
        elif (position == Gtk.IconViewDropPosition.DROP_RIGHT or position == Gtk.IconViewDropPosition.DROP_BELOW):
            model.insert_after(myIter, [text, pixbuf])

pixbuf = data.get_pixbuf() # I only get this or
text = data.get_text() # this not both at the same time. :(


Last edited by BadOmen on Thu Mar 01, 2012 12:03 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Iconview, dnd from two columns? (Python3.2 and Gtk3)
PostPosted: Sun Feb 05, 2012 12:46 pm 
Offline
Familiar Face

Joined: Sat Feb 04, 2012 12:41 pm
Posts: 10
This is my code, if anyone pls can change it so it work as i have described. :)
Just change the path to the images (/home/USER/IMAGES/test.svg).
Code:
from gi.repository import Gtk, Gdk
from gi.repository.GdkPixbuf import Pixbuf

(TARGET_ENTRY_TEXT, TARGET_ENTRY_PIXBUF) = range(2)
(COLUMN_TEXT, COLUMN_PIXBUF) = range(2)
DRAG_ACTION = Gdk.DragAction.COPY

class DragDropWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Drag and Drop Demo")
        Gtk.Window.set_default_size(self,700,600)

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        hbox = Gtk.Box(spacing=12)
        vbox.pack_start(hbox, True, True, 0)

        pixbufImg = Pixbuf.new_from_file_at_size("/home/USER/IMAGES/test.svg", 128, -1)
        pixbufImg2 = Pixbuf.new_from_file_at_size("/home/USER/IMAGES/test2.svg", 128, -1)
        pixbufImg3 = Pixbuf.new_from_file_at_size("/home/USER/IMAGES/test3.svg", 128, -1)

        self.iconview = myIconView()
        self.iconview.add_item("image1", pixbufImg)
        self.iconview.add_item("image2", pixbufImg2)
        self.iconview.add_item("image3", pixbufImg3)
        self.iconview2 = myIconView()

        hbox.pack_start(self.iconview, True, True, 0)
        hbox.pack_start(self.iconview2, True, True, 0)

        button_box = Gtk.Box(spacing=6)
        vbox.pack_start(button_box, True, False, 0)

        image_button = Gtk.RadioButton.new_with_label_from_widget(None, "Images")
        image_button.connect("toggled", self.add_image_targets)
        button_box.pack_start(image_button, True, False, 0)

        text_button = Gtk.RadioButton.new_with_label_from_widget(image_button, "Text")
        text_button.connect("toggled", self.add_text_targets)
        button_box.pack_start(text_button, True, False, 0)

        self.add_image_targets()

    def add_image_targets(self, button=None):
        targets = Gtk.TargetList.new([])
        targets.add_image_targets(TARGET_ENTRY_PIXBUF, False)

        self.iconview2.drag_dest_set_target_list(targets)
        self.iconview.drag_source_set_target_list(targets)

    def add_text_targets(self, button=None):
        self.iconview2.drag_dest_set_target_list(None)
        self.iconview.drag_source_set_target_list(None)

        self.iconview2.drag_dest_add_text_targets()
        self.iconview.drag_source_add_text_targets()


class myIconView(Gtk.IconView):

    def __init__(self):
        Gtk.IconView.__init__(self)
        self.set_text_column(COLUMN_TEXT)
        self.set_pixbuf_column(COLUMN_PIXBUF)
        self.model = Gtk.ListStore(str, Pixbuf)
        self.set_model(self.model)
        self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
        self.set_columns(1)
        self.connect("drag-data-received", self.on_drag_data_received)
        self.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [], DRAG_ACTION)
        self.connect("drag-data-get", self.on_drag_data_get)

    def on_drag_data_get(self, widget, drag_context, data, info, time):
        selected_path = self.get_selected_items()[0]
        selected_iter = self.get_model().get_iter(selected_path)
        text = self.get_model().get_value(selected_iter, COLUMN_TEXT)
        pixbuf = self.get_model().get_value(selected_iter, COLUMN_PIXBUF)
        print(text)
        data.set_text(text, -1)
        data.set_pixbuf(pixbuf)

    def add_item(self, text, icon_name):
        self.get_model().append([text, icon_name])

    def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
        model = widget.get_model()
        pixbuf = data.get_pixbuf()
        text = data.get_text()
        print(text)
        try:
            path, position = widget.get_dest_item_at_pos(x, y)
            myIter = model.get_iter(path)

        except:
            model.append([text, pixbuf])
            position = None

        if (position == Gtk.IconViewDropPosition.DROP_LEFT or position == Gtk.IconViewDropPosition.DROP_ABOVE):
            model.insert_before(myIter, [data.get_text(), pixbuf])
        elif (position == Gtk.IconViewDropPosition.DROP_RIGHT or position == Gtk.IconViewDropPosition.DROP_BELOW):
            model.insert_after(myIter, [data.get_text(), pixbuf])

win = DragDropWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


Top
 Profile  
 
 Post subject: Re: Iconview, dnd from two columns? (Python3.2 and Gtk3)
PostPosted: Mon Feb 27, 2012 7:12 pm 
Offline
Familiar Face

Joined: Sat Feb 04, 2012 12:41 pm
Posts: 10
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()


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group