Re: [PyGObject] TreeView: Empty integer field or right aligned string

2018-05-09 Thread Luca Bacci
in python you can do

def cell_data_func(self, tree_column, cell, tree_model, iter, data):
if tree_column is self.col_a:
# code
else if tree_column is self.col_b:
# code

see How do I check if two variables reference the same object in Python?

you have make col_a and col_b class members


But I think it's better to just write two functions:

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Pango

class TreeView(Gtk.TreeView):
def __init__(self, model):
Gtk.TreeView.__init__(self, model)

cell_a = Gtk.CellRendererText()
col_a = Gtk.TreeViewColumn('int',
   cell_a)
col_a.set_cell_data_func(cell_a, self.func_a, None)
self.append_column(col_a)

cell_b = Gtk.CellRendererText()
col_b = Gtk.TreeViewColumn('str',
   cell_b)
col_b.set_cell_data_func(cell_b, self.func_b, None)
self.append_column(col_b)

def func_a(self, tree_column, cell, tree_model, tree_iter, data):
cur_value = tree_model[tree_iter][0]
if cur_value == 0:
cell.set_property('text', '')
else:
cell.set_property('text', str(cur_value))

def func_b(self, tree_column, cell, tree_model, tree_iter, data):
cell.set_property('text', tree_model[tree_iter][1])

class TreeModel(Gtk.ListStore):
def __init__(self):
Gtk.ListStore.__init__(self, int, str)

# int and string
self.append([1, '111'])

# first column "empty" but displayed as "0"
# second column "empty" (but a workaround)
self.append([0, 'Hi!'])

# RIGHT alignment (second column) not working
self.append([3, '3'])


class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='Mein Gtk-Fenster')
self.set_default_size(400, 300)

self.model = TreeModel()
self.view = TreeView(self.model)

# layout
self.layout = Gtk.Grid()
self.add(self.layout)
self.layout.attach(self.view, 0, 1, 1, 1)

self.connect('destroy', Gtk.main_quit)
self.show_all()

if __name__ == '__main__':
win = Window()
Gtk.main()



2018-05-08 22:09 GMT+02:00 :

> On 2018-05-08 14:20 Luca Bacci  wrote:
> >- Use Cell Data Func for ultimate flexibility
>
> That is the signature
>  Gtk.TreeCellDataFunc(tree_column, cell, tree_model, iter, data)
>
> But how do I know which cell (row and column) is affected?
> I can extrat the row with tree_model[iter] but not the column.
>
> "tree_column" is of type Gtk.TreeViewColumn but doesn't even know its
> position (first, second, ... column). There is no integer indicating
> that.
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: [PyGObject] TreeView: Empty integer field or right aligned string

2018-05-08 Thread c.buhtz
On 2018-05-08 14:20 Luca Bacci  wrote:
>- Use Cell Data Func for ultimate flexibility

That is the signature
 Gtk.TreeCellDataFunc(tree_column, cell, tree_model, iter, data)

But how do I know which cell (row and column) is affected?
I can extrat the row with tree_model[iter] but not the column.

"tree_column" is of type Gtk.TreeViewColumn but doesn't even know its
position (first, second, ... column). There is no integer indicating
that.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: [PyGObject] TreeView: Empty integer field or right aligned string

2018-05-08 Thread Luca Bacci
To have empty int cells, you can either

   - use strings, instead of ints, in ListStore as you said
   - make place for a third value in ListStore, of type bool, and control
   the "visible" property of the first Cell Renderer with it (see code below)
   - Use Cell Data Func for ultimate flexibility

And here's the code

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TreeView(Gtk.TreeView):
def __init__(self, model):
Gtk.TreeView.__init__(self, model)
col_a = Gtk.TreeViewColumn('int',
   Gtk.CellRendererText(),
   text=0,
   visible=2)
self.append_column(col_a)
col_b = Gtk.TreeViewColumn('str',
   Gtk.CellRendererText(),
   text=1)
self.append_column(col_b)


class TreeModel(Gtk.ListStore):
def __init__(self):
Gtk.ListStore.__init__(self, int, str, bool)

self.append([1, '111', True])
self.append([0, 'Hi!', False])
self.append([3, '3',   True])


class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='Mein Gtk-Fenster')
self.set_default_size(400, 300)

self.model = TreeModel()
self.view = TreeView(self.model)

# layout
self.layout = Gtk.Grid()
self.add(self.layout)
self.layout.attach(self.view, 0, 1, 1, 1)

self.connect('destroy', Gtk.main_quit)
self.show_all()

if __name__ == '__main__':
win = Window()
Gtk.main()

Luca

2018-05-08 12:08 GMT+02:00 Luca Bacci :

> Hello!
>
> You can achieve what you want with the "xalign" property of CellRenderers
>
> col_b = Gtk.TreeViewColumn('str',
>Gtk.CellRendererText(xalign=1),
>text=1)
>
> xalign takes a value between 0 and 1.
> xalign=0 -> left-justified
> xalign=1 -> right-justified
>
> Luca
>
>
> 2018-05-06 12:01 GMT+02:00 :
>
>> X-Post: https://stackoverflow.com/q/50194505/4865723
>>
>> I want to have in a Gtk.TreeView
>>
>> - empty cells in a "int row" of a TreeView
>> - or (as a workaround) a right aligned "string row"
>>
>> There is a screenshot and example code in the StackOverflow question
>> linked in the first line of this post.
>>
>> The problem is
>>
>> - When I give 'None' to a 'int row' a '0' is displayed. I would
>>   expect an empty cell. I want that cell to be
>>   absolute empty.
>>
>> - A workaround is to use strings instead of int and just display the
>>   numbers as strings doing str(int).
>>   But then the content of each cell is left aligned by default.
>>   I tried to modify that. But this also has no effect.
>>
>> I attached the full example.
>> ___
>> gtk-app-devel-list mailing list
>> gtk-app-devel-list@gnome.org
>> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>>
>
>
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: [PyGObject] TreeView: Empty integer field or right aligned string

2018-05-08 Thread Luca Bacci
Hello!

You can achieve what you want with the "xalign" property of CellRenderers

col_b = Gtk.TreeViewColumn('str',
   Gtk.CellRendererText(xalign=1),
   text=1)

xalign takes a value between 0 and 1.
xalign=0 -> left-justified
xalign=1 -> right-justified

Luca


2018-05-06 12:01 GMT+02:00 :

> X-Post: https://stackoverflow.com/q/50194505/4865723
>
> I want to have in a Gtk.TreeView
>
> - empty cells in a "int row" of a TreeView
> - or (as a workaround) a right aligned "string row"
>
> There is a screenshot and example code in the StackOverflow question
> linked in the first line of this post.
>
> The problem is
>
> - When I give 'None' to a 'int row' a '0' is displayed. I would
>   expect an empty cell. I want that cell to be
>   absolute empty.
>
> - A workaround is to use strings instead of int and just display the
>   numbers as strings doing str(int).
>   But then the content of each cell is left aligned by default.
>   I tried to modify that. But this also has no effect.
>
> I attached the full example.
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


[PyGObject] TreeView: Empty integer field or right aligned string

2018-05-06 Thread c.buhtz
X-Post: https://stackoverflow.com/q/50194505/4865723

I want to have in a Gtk.TreeView

- empty cells in a "int row" of a TreeView
- or (as a workaround) a right aligned "string row"

There is a screenshot and example code in the StackOverflow question
linked in the first line of this post.

The problem is

- When I give 'None' to a 'int row' a '0' is displayed. I would
  expect an empty cell. I want that cell to be
  absolute empty.

- A workaround is to use strings instead of int and just display the
  numbers as strings doing str(int).
  But then the content of each cell is left aligned by default.
  I tried to modify that. But this also has no effect.

I attached the full example.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list