Re: [pygtk] Drag and drop (DND) of gtk.Notebook tab to another widget [SOLVED]

2010-07-13 Thread Zyphos
Because there it seems that there is no source of this on internet, here
is a way to achieve this.
This is a hack, we are going to override the data parameter with the
page number instead of the C pointer to the widget.

===Code.py===
notebook = gtk.notebook()
drag_page_number = 0 # global var

notebook.connect_after(drag_begin, _sig_notebook_drag_begin)
notebook.connect_after(drag_data_get, _sig_notebook_drag_data_get)
# do not use drag_source_set on Notebook, you will get a Segmentation
Fault, use set_tab_detachable instead.
# https://bugs.launchpad.net/ubuntu/+source/pygtk/+bug/604534
# notebook.drag_source_set(gtk.gdk.BUTTON1_MASK, [], gtk.gdk.ACTION_COPY)

# add tabs to notebook
notebook.append_page(child_widget, label)
notebook.set_tab_detachable(child_widget, True)
...

def _sig_notebook_drag_begin(widget, context):
# Drag and drop begin, save current page number
drag_page_number = notebook.get_current_page()

def _sig_notebook_drag_data_get(widget, context, selection, info,
timestamp):
# Override the C pointer to the widget with page_number
selection.set(selection.target, 8, str(drag_page_number)) # data is
only of string type !

# The widget that will receive the drag and drop
button = gtk.Button(Test)
button.connect(drag_drop, _sig_drag_drop)
button.connect(drag_data_received, _sig_drag_data_received)
button.drag_dest_set(0, [], 0) # button can receive any drag and drop

def _sig_drag_drop(widget, context, x, y, time):
# widget = button here
if GTK_NOTEBOOK_TAB in context.targets:
widget.drag_get_data(context, GTK_NOTEBOOK_TAB)
context.finish(True, False, time)
return True

def _sig_drag_data_received(widget, context, x, y, selection, info,
timestamp):
# widget = button here
src_widget = context.get_source_widget() # src_widget = notebook here
the_page_number = int(selection.data)
child_widget = src_widget.get_nth_page(the_page_number)

===End of code===

Zyphos


Zyphos a écrit :
 Hi,
 I try to make a DND from a gtk.Notebook to another widget.
 I receive the 'selection' on the other widget.

 The documentation say:
 The notebook will fill the selection with a reference to the child
 widget that corresponds to the dropped tab.

 Src:
 http://library.gnome.org/devel/pygtk/stable/class-gtknotebook.html#method-gtknotebook--set-tab-detachable

 Here is the selection of this DND ('drag-data-received'):

 Selection: SelectionData: GtkSelectionData at 0xbfcc1cc4
 .data: str: ��
 .format: int: 8
 .selection: str: XdndSelection
 .target: str: GTK_NOTEBOOK_TAB
 .type: str: GTK_NOTEBOOK_TAB

 But selection.data is a string with this str: ��  (I think this is a
 pointer reference to the C code child widget) it isn't usable with Python.

 How can I retrieve the notebook child widget which the DND come from ?

 Thank you for any suggestion.

   

___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Re: [pygtk] push huge array into liststore

2010-07-13 Thread Zyphos
Hi,
a solution is written into the FAQ:
http://faq.pygtk.org/index.py?req=showfile=faq13.043.htp

treeview.freeze_child_notify()
 treeview.set_model(None)

 # Add rows to the model
 # ...

 treeview.set_model(model)
 treeview.thaw_child_notify()

Regards,

Zyphos


Tim Evans a écrit :
 On 2010-07-13 3:48, Cornelius Kölbel wrote:
   
 Dear list,

 I got an array of dictionaries, that I want to add to a GTKListStore,
 that is displayed an a treeview.

 This is very slow. I already replaced the liststore.append by
 liststore.insert, which is much much faster.
 But still, filling the 10.000 values takes about 50 seconds.

 Is there any cool mapping function, to push the array to the liststore?
 I used a for loop to iterate over the array...
 I also tried while an to pop the array elements...

 It is something like this:

 data: array of dictionaries

 data = array( { 'v1' : 'something', 'v2': 'something else' } ,
{ 'v1' : 'another',   'v2': 'something completely diff' }
  )


 for d in data:
 self.liststore.insert( 0, ( d.get(v1), d.get(v2) ))


 ...is there a better way than doing a for loop?
 ...or a way to not have to interate over the 10.000 dicts?

 ...or is there a cool reading on performance tuning pyton?
 

 Some general ideas:
   - Make sure the store isn't being viewed when you insert data, that
 will slow it down.

   - Do attribute lookups outside big loops:

  append = self.liststore.append
  get = d.get
  for d in data:
  append(0, (get('v1'), get('v2'), ...))

   - Put the treeview into fixed height and width mode. Automatic sizing
 is the enemy of treeview performance. This only works if all your
 rows are the same height. The function calls you need are:
   gtk.TreeViewColumn.set_sizing
  with the gtk.TREE_VIEW_COLUMN_FIXED value
   gtk.TreeViewColumn.set_fixed_width
   gtk.TreeView.set_fixed_height_mode

   - If your list is large enough it may be worth subclassing
 gtk.TreeModel and overriding the required methods. It's complex, but
 it can avoid referencing all your data at tree build time, instead
 loading as the user scrolls.

   - Write it in C. Always valid, even if as a last resort.

   

___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


[pygtk] Drag and drop (DND) of gtk.Notebook tab to another widget

2010-07-12 Thread Zyphos
Hi,
I try to make a DND from a gtk.Notebook to another widget.
I receive the 'selection' on the other widget.

The documentation say:
The notebook will fill the selection with a reference to the child
widget that corresponds to the dropped tab.

Src:
http://library.gnome.org/devel/pygtk/stable/class-gtknotebook.html#method-gtknotebook--set-tab-detachable

Here is the selection of this DND ('drag-data-received'):

Selection: SelectionData: GtkSelectionData at 0xbfcc1cc4
.data: str: ��
.format: int: 8
.selection: str: XdndSelection
.target: str: GTK_NOTEBOOK_TAB
.type: str: GTK_NOTEBOOK_TAB

But selection.data is a string with this str: ��  (I think this is a
pointer reference to the C code child widget) it isn't usable with Python.

How can I retrieve the notebook child widget which the DND come from ?

Thank you for any suggestion.
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/