Forum: Ruby-Gnome 2 VisualRuby - How to clear the rows in listview

B96a542ba3969981493f90081bf63958?d=identicon&s=25 John S. (jhs408)
on 2014-04-17 05:31
In my program, I need to display data which periodically changes, that
means
the number of rows and content in the listview completely change.

I would like to clear the rows in the listview and re-populate the
information for the data source. The columns still remain the same.

Can you give me an example or snippet of code to clear the rows.

Thanks,
John
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2014-04-17 21:40
I usually write a refresh() method that deletes all the records, then
fills it in again.  I've had no speed problems because I've never done
huge tables.

You can use the clear() method on the model.

@listview.model.clear

Here is the code from visualruby where the "Choose Project" dialog box
appears.  The listview is populated with all the visualruby projects
found on your hard disk:

def refresh(show_backup = false)
    model.set_sort_column_id(1)
    self.model.clear
    pattern = ""
    backup_path = Dir.pwd.gsub(ENV["HOME"], File.join(ENV["HOME"],
"visualruby_backup"))
    backup_ar = File.split(backup_path)
    backup_ar << ["**", VR_ENV::SETTINGS_FILE]
    if show_backup
      pattern = File.join(*backup_ar)
    else
      pattern = File.join(ENV["HOME"], "visualruby", "**",
VR_ENV::SETTINGS_FILE)
    end
    Dir.glob(pattern, File::FNM_DOTMATCH).each do |fn|
      next if not show_backup and fn =~ / Backup /
      next if show_backup and not fn =~ / Backup /
      t = File.stat(fn).mtime
      row = add_row()
      row[:pix] = PIX
      row[:file_name] = File.dirname(fn)
      row[:modified] = DateTime.parse(t.to_s)
    end
    model.set_sort_column_id(id(:modified), Gtk::SORT_DESCENDING)
  end

Hope that helps,
Eric
B96a542ba3969981493f90081bf63958?d=identicon&s=25 John S. (jhs408)
on 2014-04-17 22:43
Eric,

The  "self.model.clear" worked, it cleared out all of the lines in
the listview and program was able to repopulate the list with new
data.

What command is used to clear one line in the listview ( ie del 5th
line)?

Is it possible to change the background of just one field in the
listview,
  ie - row[:status].....

Thanks,
John
B96a542ba3969981493f90081bf63958?d=identicon&s=25 John S. (jhs408)
on 2014-04-17 22:49
Eric,

FYI:  A listview with 4000 entries and 6 fields was able to clear
      with the  "self.model.clear" command and repopulated
      with just a quick blink of the display (no pause). Impressive!!!
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2014-04-18 00:04
Yes, you can change the background color.  See the listview example
program.  Go to Open project.  Look in the examples folder for the
listview.  It has a button to change the background to yellow.

If I remember correctly, its difficult to do individual cells, but there
is a way.

Eric
B96a542ba3969981493f90081bf63958?d=identicon&s=25 John S. (jhs408)
on 2014-04-19 19:00
Hi Eric,

Looked at "listview" example, it changes the background color
of the whole column.

I would like to change only the color of a specific item in the row,
something like    row[:name].setbackgroundcolor="red"

Thank you for your help,
John
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2014-04-22 00:48
I found this:

http://stackoverflow.com/questions/5171300/how-to-...

Seems hokey.  I don't know the answer now.  I think I remember looking
at this issue, but I can't remember the answer.

Eric
0d130f179f85401f248e6ebc2ef8292f?d=identicon&s=25 Eric C. (eric_c)
on 2014-04-22 01:01
Ok I found it.  When you define an object to be a column in a listview,
you create a method called visual_attributes().  This method returns a
hash with the attributes in it.  e.g. hash[:foreground] = "white".

Look at this example from the active_record2 project found in the
examples folder:

class Employee < ActiveRecord::Base
  has_many :paychecks
  belongs_to :employer

  include GladeGUI

  def visual_attributes
    bal = balance().to_f
    hash = { :text => name }
    hash[:foreground] = (bal < 0) ? "white" : "black"
                hash[:background] = (bal < 0) ? "red" : "white"
    return hash
  end

  def balance()
    Paycheck.sum(:amount, :conditions => "employee_id = #
{id}").round(2).to_s
#    Paycheck.find("employee_id = #{id}").calculate(:sum,
:amount).round(2).to_s
  end

end

This project isn't working right now.  I beleive its due to activerecord
constantle changing their syntax on me.  Its not summing the balance
correctly.  Perhaps someone can debug the balance() method for me.  When
the balance() method returns a negative value, the individual cell will
appear red.

I think you'll agree that this is a very good solution.

Eric
Cd3af3dcd7387ee11e399e2f40af6680?d=identicon&s=25 Daisy Laut (msdaisy)
on 2014-12-15 04:29
Thank you for your help!

This is a very good solution.



____________

http://betdownload.com
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.