Saving and Restoring a GTK:TreeModel

A

Andreas Drop

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Here is a small snippet of my code that saves and restores a
Gtk::Treemodel to/from a file. Just because i searched a while, before i
decided, i had to do it all by myself

The code is without any error handling, is tailored for a special
TreeStore and saves a model with two columns( string, string), which
should be no problem to change, if more columns or other datatypes occure.


class Tagtree

def self.store treemodel, file_name
file = File.new(file_name,'w+')
treemodel.each do |model, path, iter|
file.puts path.to_s << "|" << iter[0] << "|" << iter[1]
end
file.close
end

def self.load model, file_name
anchors=Array.new
olditer, depth =nil, 0
IO.foreach(file_name) do |line|
fields=line.split('|')
position=fields[0].split(':')
anchors[depth] = olditer
entry=model.append(anchors[position.length-1])
entry[0]=fields[1]
entry[1]=fields[2]
depth, olditer = position.length, entry
end
end
end


Usage:

Tagtree.store(treemodel,"filetostore")
Tagtree.loade(treemodel,"filetostore")


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkvAprkACgkQjsqVO1/Pdpw5GgCgp4sxA+du04rlmKlTclM/9XmB
W7YAoKb16OjNaAICIE/si2HoL+dBdXlg
=4YI4
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top