Forum: Ruby-Gnome 2 cell background of a liststore treeview ?

983428278161aebf0513417242794952?d=identicon&s=25 David Maciejak (Guest)
on 2012-11-24 20:09
(Received via mailing list)
Hi,

I need a treeview expert to set a background color of a particular
cell of a liststore,
here the code i extracted from my project.
The goal is to set only the cell corresponding to severity "high" to
background color red, as the data are dynamically added to the
liststore using the append method which is using the same cell
renderer all the column is set to red ;(

Any idea ?

thanks,
david

#######################################################################################

#!/usr/bin/ruby

require 'gtk2'
require 'gnomecanvas2'

Gtk.init
w = Gtk::Window.new

ls = Gtk::ListStore.new(Integer, String, Integer, String, String)
treeview = Gtk::TreeView.new(ls)
w.add treeview

column1 = Gtk::TreeViewColumn.new("Selected",
Gtk::CellRendererToggle.new, {:active => 0})
column2 = Gtk::TreeViewColumn.new("Protocol",
Gtk::CellRendererText.new, {:text => 1})
column3 = Gtk::TreeViewColumn.new("Port", Gtk::CellRendererText.new,
{:text => 2})
column4 = Gtk::TreeViewColumn.new("Service",
Gtk::CellRendererText.new, {:text => 3})
column5 = Gtk::TreeViewColumn.new("Severity",
Gtk::CellRendererText.new, {:text => 4})

treeview.append_column(column1)
treeview.append_column(column2)
treeview.append_column(column3)
treeview.append_column(column4)
treeview.append_column(column5)
treeview.show

array = [[1, "tcp",80,"http","high"], [1, "udp",53,"dns","medium"]]
array.each {|cb, proto, port, service, sev|
  iter = treeview.model.append
  iter[0] = cb
  iter[1] = proto
  iter[2] = port
  iter[3] = service
  iter[4] = sev
  if sev  == "high"
     sev_column = treeview.get_column(4)
     cells = sev_column.cell_renderers
     puts cells
     if cells.size > 0
   sev_column.cell_renderers[0].set_cell_background_gdk(Gdk::Color.new(64515,
0, 0))
     end
  end
}

w.show_all
Gtk.main
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2012-11-25 00:39
Hi:

The method you need to use is:

Gtk::TreeViewColumn#set_cell_data_func

http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%...


However, there's a much easier way to accomplish this using visualruby.
Using visualruby's listview object, you can load any type of object into
a listview, then define a method called "visual_attributes" to define
how you want the object to appear:

class Server
  def initialize(cb, proto, port)
    @cb = cb
    @proto = proto
    @port = port
  end

  def visual_attributes
    hash = { :text => @cb.to_s }
    hash[:background] = (@port < 0) ? "red" : "white"
    return hash
  end
end

Later you create a listview like this:

@list_view = VR::Listview.new(:cb => String, :serv => Server)

The Server objects will all appear with the text set to @cb,
and the background will be red for any port that's less than zero.

For more look at the Listview Tutorials on visualruby.net.

Yours,
Eric C
983428278161aebf0513417242794952?d=identicon&s=25 David Maciejak (Guest)
on 2012-11-25 01:05
(Received via mailing list)
On Sun, Nov 25, 2012 at 12:39 AM, Eric C.
<ruby-forum-incoming@andreas-s.net> wrote:
> Using visualruby's listview object, you can load any type of object into
>   def visual_attributes
> The Server objects will all appear with the text set to @cb,
> and the background will be red for any port that's less than zero.
>
> For more look at the Listview Tutorials on visualruby.net.

Thx Eric, i will have a look and try.

regards,
david
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2012-11-25 03:14
Hi David:

Look at the example program, "active_record2"  it has cells with
different colors in the same column.
983428278161aebf0513417242794952?d=identicon&s=25 David Maciejak (Guest)
on 2012-11-25 12:58
(Received via mailing list)
On Sun, Nov 25, 2012 at 3:14 AM, Eric C.
<ruby-forum-incoming@andreas-s.net> wrote:
> Hi David:
>
> Look at the example program, "active_record2"  it has cells with
> different colors in the same column.

Thx Eric, i made it works.

regards,
david
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.