class ImageList < Gtk::ScrolledWindow
def initialize
super
@files = Gtk::ListStore.new(String, String, Gdk::Pixbuf)
@fileList = Gtk::TreeView.new(@files)
renderer = Gtk::CellRendererPixbuf.new
col = Gtk::TreeViewColumn.new("Thumb", renderer, :pixbuf => 2)
@fileList.append_column(col)
renderer = Gtk::CellRendererText.new
col = Gtk::TreeViewColumn.new("File name", renderer, :text => 1)
@fileList.append_column(col)
self.add @fileList
end
def readDir (root)
Dir.foreach root do dir
file = root+'/'+dir
if !FileTest.directory?(file) && dir =~ /\.jpe?g$/ && FileTest.readable?(file)
thumb = getThumbnail(file)
iter = @files.append
iter[0] = file
iter[1] = dir
iter[2] = Gdk::Pixbuf.new thumb
File.delete(thumb)
end
end
end
end
When working like this, I find myself very soon in need of creating custom
signals: in this case I want to make a signal that ImageList emits when the
user select an image from the TreeView, like this:
fileList.signal_connect("selected") do filename
imageDetails.load(filename)
end
Now, how do I implement that extra 'selected' signal in my ImageList? Ruby
documentation is usually very good and easy to find, but this time Google had
little to say to me.
When the going gets tough, the tough gets to IRC:
enrico> Hi. http://ruby-gnome2.sourceforge.jp/?News_20031116_1 says that it is
possible to create custom signals, however I could not find any
documentation on how to do it. Anyone has a link or a short explanation?
pterjan> glib/sample in the source IIRC
pterjan> # define new signal "hoge"
pterjan> signal_new("hoge", # name
pterjan> GLib::Signal::RUN_FIRST, # flags
pterjan> nil, # accumulator (XXX: not supported yet)
pterjan> nil, # return type (void == nil)
pterjan> Integer, Integer # parameter types
pterjan> )
pterjan> in glib/sample/type-register.rb
enrico> ooh!
enrico> pterjan: thanks!
enrico> I'll blog about it, it might make it easier for others to google this answer
Thanks pterjan, and oh, coolness! So there are nice examples that I was silly
enough to miss. Are they included in the Debian packages? Oh, yes! Right in
/usr/share/doc/libglib2-ruby/examples/type-register.rb.
Here's the ImageList class with the new signal:
class ImageList < Gtk::ScrolledWindow
type_register
signal_new("selected", # name
GLib::Signal::RUN_FIRST, # flags
nil, # accumulator (XXX: not supported yet)
nil, # return type (void == nil)
String # parameter types
)
def initialize
super
@files = Gtk::ListStore.new(String, String, Gdk::Pixbuf)
@fileList = Gtk::TreeView.new(@files)
renderer = Gtk::CellRendererPixbuf.new
col = Gtk::TreeViewColumn.new("Thumb", renderer, :pixbuf => 2)
@fileList.append_column(col)
renderer = Gtk::CellRendererText.new
col = Gtk::TreeViewColumn.new("File name", renderer, :text => 1)
@fileList.append_column(col)
self.add @fileList
sel = @fileList.selection
sel.signal_connect("changed") do sel
if iter = sel.selected
puts "Double-clicked row contains name # iter[0] !"
self.signal_emit("selected", iter[0])
end
end
end
def signal_do_selected(file)
puts "Selected " + file
#p caller
end
def readDir (root)
Dir.foreach root do dir
file = root+'/'+dir
if !FileTest.directory?(file) && dir =~ /\.jpe?g$/ && FileTest.readable?(file)
thumb = getThumbnail(file)
iter = @files.append
iter[0] = file
iter[1] = dir
iter[2] = Gdk::Pixbuf.new thumb
File.delete(thumb)
end
end
end
end
And here's the controller code that binds the signal to some effect:
imageList.signal_connect("selected") do imagelist, filename
puts "File # filename was clicked!"
image.load(filename)
end
It's quite easy, and (finally!) it does what I want without needing to write
thousands of lines of code. YAY! And I finally fulfull my dream of connecting
closures instead of callbacks to GTK signals.
Yes, I could have done it in Perl. Yes I could have done it in Python. But
Ruby is soo cute!
mornfall> buchi automatons are not turing-equivalent and i don't think you
can express halting problem on them
mornfall> buchi automaton accepts w if it visits an accepting state
infinitely often on w
enrico> interestingly, "buchi" means "holes" in Italian
mornfall> :-)
mornfall> it's written B chi i think :)
enrico> please, tell me there's a culi automata somewhere
enrico> or a c li automata
mornfall> i can invent one and call it like that why? ;-)
enrico> Because "i buchi dei culi" is the holes of the asses, and also
sounds very funny. So I would have started writing a demostration
involving both automatha and giving a lecture in public about it
mornfall> lol
mornfall> i sure as hell must name one i invent like that
mornfall> and prove it's equivalent to buchi automaton ;-)
enrico> I swear I'll do the Italian translation of the proof
enrico> Or you could prove that the buchi automaton is contained in the
culi one
mornfall> that's even better =)
from gimpfu import *
def process (file):
img=pdb.file_xpm_load("/home/enrico/tmp/cazzeggio/"+file+".xpm", file+".xpm")
pdb.file_png_save_defaults(img, img.active_layer, "/home/enrico/tmp/cazzeggio/"+file+".png", file+".png")
# (update) added to dispose of the image to avoid gimp swap to grow
pdb.gimp_image_delete(img)
for i in range(1, 10000):
process("img_%05d"%i)
Tested in the gimp python-fu console, it works, with the save and load progress
bar popping up all the time grabbing the focus from whatever I was doing.
There is a run_mode that can be passed to those file_xpm_load and
file_png_save_defaults, however python-fu is happy to let me know that I don't
need to bother about it. I searched, searched searched and I found out that
this really means that there's no findable way to tell Gimp functions to STFU
with python-fu.
Now, how do I run that script AND be still able to use my computer?
gimp has a nice --batch commandline option, from which it seems to only
understand scheme. It has a --batch-interpreter option, but I couldn't find
out any possible value that could make --batch understand python.
So, it seems that the only way to run python is to either register a plugin, or
type it in the python console. And no STFU.
So I tried Xnest, so that I could at least iconise the whole thing out of my
way. But it seems that xnest is denying connection from all clients I try to
run in it.
So I've run a VNC server. In the VNC server I run Gimp. Then I fire up the
Python console inside that Gimp. Then I paste the script in the Python
console. Then it starts working and I close the VNC client to get rid of the
popups.
Finally, this script helps me delete the big XPM files after they've been
converted to PNG:
#!/bin/sh
for i in *.xpm
do
if [ -e basename $i .xpm .png ]
then
echo rm $i
rm $i
fi
done
If there were easier ways to do what I did, please mail me and I'll document
them: no matter how I searched, I couldn't find any.
Update: it seems that in the Gimp python script, the image doesn't get
automatically garbage collected. As a result, the file
~/.gimp-2.2/gimpswap.whatever keeps inflating, so space is not really saved by
converting the XPM files to PNG, unless I restart Gimp from time to time.
Update: I found a function to deallocate the image: maybe that avoids the
growth of the Gimp swap.
Here's how it works now:
- I only want to maintain information once, without copying around and information getting outdated.
- I need translations, and I want to translate using smart tools.
...and it's all driven by Makefiles. The day I feel like it, I'll add an XSLT to generate a CV in OpenDocument format. Javier Candeira called this "Rube Goldberg Hacking", but then noone could figure out simpler ways of doing it with the properties I wanted. Scripts available on request if someone is crazy enough to walk along a similar path.
It all starts with informations scattered around the file system.
Some Ruby scripts go around digging for them and generate some XML files.
The XML files have tags with special markers so that intltool can extract the strings inside and generate .pot files with them.
I translate .pot files with all the tools one normally uses with them.
Intltool generates multiple XML versions with clean tags and translated strings.
- At this point, I have XSLT sheets that can generate:
- My CV
- The Talks page
- A page with a summary of the free software projects I'm involved in
< mornfall> you are working on a timed contract?
< enrico> I'm probably worse than a timed contract: I had to setup a company
< enrico> The government is restricting them so badly that they couldn't hire
me even temporarily, and they had to buy software from me
< enrico> so I had to become a company
< enrico> I AM THE COMPANY
< mornfall> oh duh
< enrico> I'm my employer
< enrico> and my employee
< enrico> in all this, the government proudly shows statistics of lots of new
companies being created in Italy as a sign that our economy is so
good
< mornfall> be nice to your employee :-)
< mornfall> hmh
< enrico> so last friday there was a general strike and my company sent a
press release to the major labor union telling them that the CEO
together with all the employees decided to fully take part in the
strike
< enrico> Signed: Enrico Zini, directory of the company Enrico Zini;
Employees of the company Enrico Zini: Enrico Zini
< mornfall> that's getting tough
< enrico> I'm very nice to my employees, hes :)
< enrico> but sometimes I wonder if masturbation could be considered sexual
harassment from my boss
< mornfall> LOL
< mornfall> you need to appoint a secretary
< enrico> I have one: Enrico Zini. He's a bit messy, though.
< enrico> The only thing which is not called Enrico Zini is the accountant.
< enrico> Incidentally, he's called Vincenzo Zini, but he's isn't a relative
of mine :)
< mornfall> in that case i think the boss is harassing the secretary not the
other employee
< enrico> However, he is member of the local LUG and has a Jabber account :)
< mornfall> fun :)
< enrico> oh, ok. But the employee might be harassing the secretary, or the
secretary is harassing the employee and the boss
< enrico> IDEA
< enrico> Tonight I'll propose an orgy with the boss, the secretaries and the
employees!
< enrico> (but not the accountant)
(blogged with mornfall's permission)
$ (A=foo; echo $A); echo $A foo $(note: the boring presence of "foo" in these examples should be taken as a reminder of my call for no-op words)
hdparm -B 254 /dev/hdaThe noise has finally stopeed and I'm in harmony with my laptop once again. Note: the culprit for setting the original value of -B seems to be laptop-mode, specifically in /etc/laptop-mode/laptop-mode.conf, the default values of LM_AC_HD_POWERMGMT and NOLM_AC_HD_POWERMGMT.
Next.