Class: QtLoader

Inherits:
Object show all
Defined in:
lib/knj/libqt.rb

Instance Method Summary collapse

Constructor Details

#initialize(paras) ⇒ QtLoader

Returns a new instance of QtLoader.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/knj/libqt.rb', line 22

def initialize(paras)
  @widgets = {}
  
  if (paras.class.to_s == "Hash")
    file_path = paras["file"]
    @object_connect = paras["object_connect"]
  elsif(paras.class.to_s == "String")
    file_path = paras
  elsif(paras.class.to_s == "Array")
    file_path = paras[0]
    @object_connect = paras[1]
  else
    raise "Unknown parameter"
  end
  
  file = Qt::File.new(file_path)
  file.open(Qt::File::ReadOnly)
  @loader = Qt::UiLoader.new
  @window = @loader.load(file, nil)
  file.close
  
  
  @window.findChildren(Qt::Object).each do |widget|
    object_name = widget.object_name.to_s
    
    if (object_name.length > 0)
      @widgets[object_name] = widget
      
      if (@object_connect)
        meta = widget.meta_object
        signals = {}
        0.upto(meta.methodCount - 1) do |count|
          method_info = meta.method(count)
          
          if (method_info.method_type == 1)
            method_name = method_info.signature.match(/^(.+)\(/)
            signals[method_name[1]] = method_info.signature
          end
        end
      end
      
      signals.each do |item|
        func_name = "on_" + object_name + "_" + item[0]
        if (@object_connect.respond_to?(func_name))
          params = item[1].sub(/[a-z]+(\(.*$)/, '\1')
          widget.connect(SIGNAL item[1]) do
            @object_connect.send(func_name)
          end
        end
      end
    end
  end
end

Instance Method Details

#loaderObject



10
11
12
# File 'lib/knj/libqt.rb', line 10

def loader
  return @loader
end

#object_connectObject



14
15
16
# File 'lib/knj/libqt.rb', line 14

def object_connect
  return @object_connect
end

#widget(name) ⇒ Object



18
19
20
# File 'lib/knj/libqt.rb', line 18

def widget(name)
  return @widgets[name]
end

#windowObject



6
7
8
# File 'lib/knj/libqt.rb', line 6

def window
  return @window
end