Search Results: "Enrico Zini"

22 December 2005

Enrico Zini: Custom signals with Ruby-GTK2

How to create a custom signal in Ruby-GTK2 I'm playing with the GTK2 bindings for Ruby. When coding GUI things, I usually like to build my own higher level widgets by subclassing containers and populating them with other widgets. For example, I'm creating an image browser and I like to make an ImageList class that subclasses a Gtk::ScrolledWindow and builds the TreeView inside it:
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!

10 December 2005

Enrico Zini: Call for a C li automaton

There are various kinds of automata in Computer Science. If you know of a Culi or C li automaton, please let me know. This is the reason: it's very important:
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 =)

8 December 2005

Enrico Zini: Making a film out of vncrec output

I have a vncrec output and I need to encode a film out of it. Note: this blog entry is getting updated with the feedback I receive by mail. All the part about recording an X session is being maintained in the Debian wiki. transcode won't work: tried everything, googled a lot: either it exits right away, or I use the dvd delay option and it shows an error from vncrec and does nothing. vncrec can generate xpm files, one per frame. However, imagemagick won't grok them complaining they have an illegal header. And xpmtoppm will complain that there is a line inside the file which is too long. The only thing that can load them is gimp. So, let's script gimp. I came up with this python script that kinda does the trick:
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.

5 December 2005

Enrico Zini: My curriculum

I finally finished restructuring my curriculum. It needed to have two properties:
  1. I only want to maintain information once, without copying around and information getting outdated.
  2. I need translations, and I want to translate using smart tools.
Here's how it works now:
  1. It all starts with informations scattered around the file system.

  2. Some Ruby scripts go around digging for them and generate some XML files.

  3. The XML files have tags with special markers so that intltool can extract the strings inside and generate .pot files with them.

  4. I translate .pot files with all the tools one normally uses with them.

  5. Intltool generates multiple XML versions with clean tags and translated strings.

  6. 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
...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.

Enrico Zini: My company

I had an interesting conversation in which I was explaining my current work situation. It might be fun to share:
< 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)

4 December 2005

Enrico Zini: Shell gotcha

Today I was puzzled as I found out that the bash one-liner A=foo echo $A does not print 'foo'. The answer came thanks to Vorlon and Wiggy: A=foo echo sets A in the environment of the echo command, and echo $A passes the value of $A in the shell as an argument to echo. $A is expanded before the line is run and at that time A=foo hasn't been processed yet. One quick fix is doing A=foo; echo $A, but this leaves A set to foo after the execution of the command. To make it so that A is foo only when executing echo, one can use parenthesis: (A=foo ; echo $A):
$ (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)

3 December 2005

Kyle McMartin: Enrico Zini is my new hero

Dude, I had exactly the same clicking problem with my laptop disk, and that totally fixed it. Cheers!

Enrico Zini: Stopped hard drive from clicking all the time

My laptop hard drive started clicking all the time, driving me crazy. Thanks to Vorlon and the other people on #debian-women I've been able to finally get to this page and this commandline:
hdparm -B 254 /dev/hda
The 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.

30 November 2005

Enrico Zini: Fixed order of posts

The 'tags' plugin of pyblosxom was kindly forgetting about sorting entries. pyblosxom is the first real python code I've put my hands on. pyblosxom isn't too bad, but my impression after seeing its plugins is: "why do people choose python because of its elegance, then write really ugly code with it?" At least the blog is currently fixed. If someone wants the patch, ask me and I'll be glad to send it: I gave up contributing them back to authors that haven't replied after months. I'll try to survive with this until a new Debian Stable includes Rails and a Rails blog I can play with.

Next.

Previous.