Class: Gtk2_expander_settings

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk2_expander_settings.rb

Overview

A class that helps saving and restoring settings for expanders using a database.

Constant Summary collapse

ALLOWED_ARGS =
[:db, :expander, :name]
DB_SCHEMA =
{
  "tables" => {
    "Gtk2_expander_settings" => {
      "columns" => [
        {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
        {"name" => "name", "type" => "varchar"},
        {"name" => "expanded", "type" => "enum", "maxlength" => "'0','1'"},
        {"name" => "saved", "type" => "enum", "maxlength" => "'0','1'"}
      ],
      "indexes" => ["name"]
    }
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Gtk2_expander_settings

Returns a new instance of Gtk2_expander_settings.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gtk2_expander_settings.rb', line 18

def initialize(args)
  #Load arguments and database.
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ALLOWED_ARGS.include?(key)
  end
  
  @db, @expander, @name = args[:db], args[:expander], args[:name]
  
  Knj::Db::Revision.new.init_db("db" => @db, "schema" => DB_SCHEMA)
  
  #Load or initialize saved data in database.
  if @data = @db.single(:Gtk2_expander_settings, :name => @name)
    @id = @data[:id]
  else
    @id = @db.insert(:Gtk2_expander_settings, {:name => @name, :saved => 0}, :return_id => true)
    @data = @db.single(:Gtk2_expander_settings, :id => @id)
  end
  
  #Restore saved value. Use timeouts to give window time to initialize first (to load code that may effect the expander like in OpenAll-Time-Applet).
  if @data[:saved].to_i == 1
    if @data[:expanded].to_i == 1 and !@expander.expanded?
      Gtk.timeout_add(25) do
        @expander.activate
        false
      end
    elsif @data[:expanded].to_i == 0 and @expander.expanded?
      Gtk.timeout_add(25) do
        @expander.activate
        false
      end
    end
  end
  
  #Connect signals in order to save new values.
  @expander.signal_connect_after("activate", &self.method(:on_expander_activated))
end

Instance Method Details

#on_expander_activated(*args) ⇒ Object

Called after the expander is activated.



56
57
58
59
60
61
62
63
64
# File 'lib/gtk2_expander_settings.rb', line 56

def on_expander_activated(*args)
  if @expander.expanded?
    exp_val = 1
  else
    exp_val = 0
  end
  
  @db.update(:Gtk2_expander_settings, {:expanded => exp_val, :saved => 1}, {:id => @id})
end