Module: GladeGUI
- Included in:
- VR::CalendarCol, VR::ImageCol, VR::TextCol
- Defined in:
- lib/GladeGUI.rb
Instance Attribute Summary collapse
-
#builder ⇒ Object
Returns the value of attribute builder.
Instance Method Summary collapse
- #active_record_valid?(show_errors = true) ⇒ Boolean
-
#class_name(obj) ⇒ Object
:nodoc:.
-
#destroy_window ⇒ Object
parent windows.).
-
#fill_control(glade_name, val) ⇒ Object
:nodoc:.
-
#get_glade_active_record(obj) ⇒ Object
:nodoc:.
-
#get_glade_all(obj = self) ⇒ Object
This method is the most useful method to retreive values from a glade form.
-
#get_glade_variables(obj = self) ⇒ Object
Populates the instance variables from the glade form.
-
#load_glade(caller__FILE__, parent = nil) ⇒ Object
This will Load the glade form.
-
#parse_signals ⇒ Object
:nodoc:.
-
#set_glade_active_record(obj = self) ⇒ Object
Populates the glade form from the fields of an ActiveRecord object.
-
#set_glade_all(obj = self) ⇒ Object
This method is the most useful method to populate a glade form.
-
#set_glade_variables(obj = self) ⇒ Object
Populates the glade form from the instance variables of the class.
-
#set_parent(parent) ⇒ Object
:nodoc: parent = class derived from class GladeGUI.
-
#show_window ⇒ Object
This shows the window and will start the Gtk.main loop (for top level windows.) This is called after calling load_glade() and setting-up all the form widgets.
Instance Attribute Details
#builder ⇒ Object
Returns the value of attribute builder.
103 104 105 |
# File 'lib/GladeGUI.rb', line 103 def builder @builder end |
Instance Method Details
#active_record_valid?(show_errors = true) ⇒ Boolean
346 347 348 349 350 351 352 353 354 |
# File 'lib/GladeGUI.rb', line 346 def active_record_valid?(show_errors = true) get_glade_all if not self.valid? VR.msg(self.errors..join("\n\n")) if show_errors self.reload return false end return true end |
#class_name(obj) ⇒ Object
:nodoc:
133 134 135 |
# File 'lib/GladeGUI.rb', line 133 def class_name(obj) # :nodoc: /.*\b(\w+)$/.match(obj.class.name)[1] end |
#destroy_window ⇒ Object
parent windows.)
339 340 341 342 343 344 |
# File 'lib/GladeGUI.rb', line 339 def destroy_window() if @builder["window1"].transient_for.nil? Gtk.main_quit end @builder["window1"].destroy end |
#fill_control(glade_name, val) ⇒ Object
:nodoc:
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/GladeGUI.rb', line 241 def fill_control(glade_name, val) # :nodoc: control = @builder[glade_name] control ||= @builder[glade_name.split(".")[1]] case control when Gtk::CheckButton then control.active = val when Gtk::TextView then control.buffer.text = val.to_s when Gtk::Entry then control.text = val.to_s when Gtk::FontButton then control.font_name = val.to_s when Gtk::LinkButton then control.uri = control.label = val.to_s when Gtk::Label, Gtk::Button then control.label = val.to_s when Gtk::Image then control.file = val.to_s when Gtk::SpinButton then control.value = val.to_f when Gtk::ProgressBar then control.fraction = val.to_f when Gtk::Calendar then control.select_month(val.month, val.year) ; control.select_day(val.day) ; control.mark_day(val.day) when Gtk::Adjustment then control.value = val.to_f end end |
#get_glade_active_record(obj) ⇒ Object
:nodoc:
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/GladeGUI.rb', line 300 def get_glade_active_record(obj) # :nodoc: return if not defined? @attributes obj.attributes.each_pair do |key, val| control = @builder[class_name(obj) + "." + key] control ||= @builder[key] case control when Gtk::CheckButton then self.send("#{key}=", control.active?) when Gtk::Entry then self.send("#{key}=", control.text) when Gtk::TextView then self.send("#{key}=", control.buffer.text) when Gtk::FontButton then self.send("#{key}=", control.font_name) when Gtk::Label, Gtk::Button then self.send("#{key}=", control.label) when Gtk::SpinButton then self.send("#{key}=", control.value) when Gtk::Image then self.send("#{key}=", control.file) when Gtk::ProgressBar then self.send("#{key}=", control.fraction) when Gtk::Calendar then self.send("#{key}=", DateTime.new(*control.date)) when Gtk::Adjustment then self.send("#{key}=", control.value) end end end |
#get_glade_all(obj = self) ⇒ Object
This method is the most useful method to retreive values from a glade form. It will
populate from active_record fields and instance variables. It will simply
call both of these methods:
get_glade_active_record()
get_glade_variables()
So, to retreive all the values of a form back into your ActiveRecord object and instance variables, simply call the set_glade_all() method instead.
178 179 180 181 |
# File 'lib/GladeGUI.rb', line 178 def get_glade_all(obj = self) get_glade_active_record(obj) get_glade_variables(obj) end |
#get_glade_variables(obj = self) ⇒ Object
Populates the instance variables from the glade form.
This works for Gtk:Button, Gtk::Entry, Gtk::Label and Gtk::Checkbutton.
So instead of having to assign instance variable:
@name = @builder["DataObjectGUI.name"].text
@address = @builder["DataObjectGUI.address"].text
@eamil = @builder["DataObjectGUI.email"].text
@phone = @builder["DataObjectGUI.phone"].text
you can write one line of code:
get_glade_variables()
The optional parameter is seldom used because you usually want the
glade form to populate from the calling class. If you passed another object,
the form would populate from it.
obj - type Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/GladeGUI.rb', line 279 def get_glade_variables(obj = self) obj.instance_variables.each do |v| v = v.to_s #fix for ruby 1.9 giving symbols control = @builder[class_name(obj) + v.gsub("@", ".")] control ||= @builder[v.gsub("@", "")] case control when Gtk::CheckButton then obj.instance_variable_set(v, control.active?) when Gtk::Entry then obj.instance_variable_set(v, control.text) when Gtk::TextView then obj.instance_variable_set(v, control.buffer.text) when Gtk::FontButton then obj.instance_variable_set(v, control.font_name) when Gtk::Label, Gtk::Button then obj.instance_variable_set(v, control.label) when Gtk::SpinButton then obj.instance_variable_set(v, control.value) when Gtk::Image then obj.instance_variable_set(v, control.file) when Gtk::ProgressBar then obj.instance_variable_set(v, control.fraction) when Gtk::Calendar then obj.instance_variable_set(v, DateTime.new(*control.date)) when Gtk::Adjustment then obj.instance_variable_set(v, control.value) end end end |
#load_glade(caller__FILE__, parent = nil) ⇒ Object
This will Load the glade form.
It will create a Gtk::Builder object from your glade file.
The Gtk::Builder object is stored in the instance variable, @builder.
You can get a reference to any of the widgets on the glade form by
using the @builder object:
widget = @builder["name"]
Normally, you should give your widgets names of instance variables: i.e. "MyClass.var"
so they can be autoloaded using the set_glade_all() method.
You can also pass a reference to a parent class that also includes GladeGUI.
Then the child window will run simultaniously with the parent.
125 126 127 128 129 130 131 |
# File 'lib/GladeGUI.rb', line 125 def load_glade(caller__FILE__, parent = nil) file_name = File.split(caller__FILE__)[0] + '/glade/' + class_name(self) + ".glade" @builder = Gtk::Builder.new.add_from_file(file_name) @builder.connect_signals{ |handle| method(handle) } set_parent(parent) unless parent.nil? parse_signals() end |
#parse_signals ⇒ Object
:nodoc:
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/GladeGUI.rb', line 137 def parse_signals() # :nodoc: meths = self.class.instance_methods() meths.each do |meth| meth = meth.to_s #bug fix ruby 1.9 gives stmbol glade_name, signal_name = *(meth.split("__")) next if (signal_name.to_s == "" or glade_name.to_s == "") #covers nil @builder.objects.each do |obj| next unless obj.respond_to?(:builder_name) if obj.builder_name == glade_name or obj.builder_name =~ /^(?:#{class_name(self)}\.|)#{glade_name}\[\d+\]$/ #arrays obj.signal_connect(signal_name) { |*args| method(meth.to_sym).call(*args) } end end obj = glade_name == "self" ? self : instance_variable_get("@" + glade_name) obj ||= eval(glade_name) if respond_to?(glade_name) and method(glade_name.to_sym).arity == 0 # no arguments! obj.signal_connect(signal_name) { |*args| method(meth.to_sym).call(*args) } if obj.respond_to?("signal_connect") end end |
#set_glade_active_record(obj = self) ⇒ Object
Populates the glade form from the fields of an ActiveRecord object.
So instead of having to assign each widget a value:
@builder["ARObject.name"].text = @name
@builder["ARObject.address"].text = @address
@builder["ARObject.email"].text = @eamil
@builder["ARObject.phone"].text = @phone
you can write one line of code:
set_glade_active_record()
The optional parameter is seldom used because you usually want the
glade form to populate from the calling class. If you passed another object,
the form would populate from it.
obj - type ActiveRecord::Base
202 203 204 205 |
# File 'lib/GladeGUI.rb', line 202 def set_glade_active_record(obj = self) return if not defined? @attributes obj.attributes.each_pair { |key, val| fill_control(class_name(obj) + "." + key, val) } end |
#set_glade_all(obj = self) ⇒ Object
This method is the most useful method to populate a glade form. It will
populate from active_record fields and instance variables. It will simply
call both of these methods:
set_glade_active_record()
set_glade_variables()
So, to set all the values of a form, simply call the set_glade_all() method instead.
164 165 166 167 |
# File 'lib/GladeGUI.rb', line 164 def set_glade_all(obj = self) set_glade_active_record(obj) set_glade_variables(obj) end |
#set_glade_variables(obj = self) ⇒ Object
Populates the glade form from the instance variables of the class. This works for Gtk:Button, Gtk::Entry, Gtk::Label and Gtk::Checkbutton. So instead of having to assign each widget a value:
@builder.text = @name @builder.text = @address @builder.text = @eamil @builder.text = @phone
you can write one line of code:
set_glade_variables()
The optional parameter is seldom used because you usually want the glade form to populate from the calling class. If you passed another object, the form would populate from it.
obj - type Object
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/GladeGUI.rb', line 227 def set_glade_variables(obj = self) obj.instance_variables.each do |name| name = name.to_s #ruby 1.9 passes symbol! v = obj.instance_variable_get(name) if v.class == Array (0..v.size-1).each do |i| fill_control(class_name(obj) + "." + name.gsub("@", "") + "[" + i.to_s + "]", v[i] ) end else fill_control(class_name(obj) + "." + name.gsub("@", ""), v) end end end |
#set_parent(parent) ⇒ Object
:nodoc: parent = class derived from class GladeGUI
105 106 107 |
# File 'lib/GladeGUI.rb', line 105 def set_parent(parent) # :nodoc: parent = class derived from class GladeGUI @builder["window1"].transient_for = parent.builder["window1"] end |
#show_window ⇒ Object
This shows the window and will start the Gtk.main loop (for top level windows.) This is called after calling load_glade() and setting-up all the form widgets. Using show_window() and destroy_window() is better than using Gtk.main and Gtk.main.quit because they automatically open and close windows properly.
329 330 331 332 |
# File 'lib/GladeGUI.rb', line 329 def show_window() @builder["window1"].show_all Gtk.main if @builder["window1"].transient_for.nil? end |