Download - comboboxentry - glade - ruby - guide
A Exercise on Interdependent select (or chained select) using ComboBoxEntry in Glade3 And Ruby
Core:
w are going to develop chained selection module using comboboxentry .
In first comboboxentry, will list out countries names, when user select any one of countries from this list, application will display another dependent comboboxentry.
The second comboboxentry have to list out “states” which are all corresponding to selected country in first comboboxentry box.
Then it will be displayed , third comboboxentry & it will list out cities corresponding to the selected state in second comboboxentry box.
How to install ruby :
$sudo apt-get install ruby
How to install glade :
$sudo apt-get install glade
Or goto 'synaptic package mamager' and find glade3, select mark for installation and click 'Apply' button.
ruby-glade-create-template command :
To run this command we have to install ruby package,
$sudo apt-get install libglade2-ruby
How to install active-record :
$sudo apt-get install rubygems
$sudo gem install activerecord
Error :
While installing activerecord , it will show following error:
usr/bin/gem:10: undefined method `manage_gems' for Gem:Module (NoMethodError)
For that do the following:
$sudo gedit /usr/bin/gem
add the following two lines in top of the coding :
require 'rubygems' require 'rubygems/gem_runner' and comment the next line by #Gem.manage_gems
Glade Guide
1). Drag and drop “Window” from Top Level Window2). Place “Fixed Layer” from Container in window3). Take three “ComboBoxEntry” and place in window.4). Set lable name to each comboboxentry and comboboxentry-entry1 to identify.
Signal:
set “changed “ signal to all comboboxentry widget. And save it as “combo.glade”
Open Glade3 from Programming Application
Opened Glade Window
Select Window Widget and drop in design area
Drag and drop “fixed” widget and plce inside toplevel window
Take comboboxentry
Change your comboboxentry widget label name to identify
Allign and customize comboboxentry widget box size
Set 'change siganl' to all comboboxentry from the “Signal”menu side panel
Drag and drop the 'Label' widget & change label name & allign it.
Final design view
Ruby Guide
$ ruby-glade-create-template combo.glade > combo.rb
Initialize Method :
Add one line inside initialize method as follow . @glade[“Window1”].show_all
“Window1” is a default lable name of top level window. If u want change that lable name...
Program :
# Active Record
require 'libglade2'require 'rubygems' require 'active_record' ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection( :adapter => "mysql", :encoding => "utf8", :database => "country", :username => "root", :password =>"password", :socket => "/var/run/mysqld/mysqld.sock", :dbfile => ":memory:" )
ActiveRecord::Schema.define do create_table :countries do |table| table.column :name, :string end
create_table :states do |table| table.column :country_id, :integer table.column :name, :string end
create_table :cities do |table| table.column :state_id, :integer table.column :name, :string end
end
=begin
create database “ country” in your mysql by excuting this query “mysql > create database country; “.
And set your mysql root user name “root” and “password” in the above code.
You can understand easily by seeing the above code, how active record creates tables and corresponding fields.
Create_table: 'table name' - - - > It will create table in db.Column: name, :String ( Field Type) - - -> It will set column field by this type and name.
=end
# Relationships :
class Country < ActiveRecord::Base has_many :state end
class State < ActiveRecord::Base belongs_to :country has_many :cities end
class City < ActiveRecord::Base belongs_to :state end
=begin
This tells about relationships among those tables...
“Country “ table record entries having relationship with so many “State” table record entries...simply we can represent by one to many relationship, so in this case we have used “ has_many ”. This means a country “has_many” states.
“State” table record entries having relations under “Country” table record entries.... It can be represented by “ belongs_to “. Since every state belongs to a country hence we have used “belongs_to” in the state class to say that it belongs to a country.
=end
# Populating values into tables :
country=Country.new(:name=>'India') country.save
# here we are inserting value 'India' into country table
country.state << State.new(:name => 'TamilNadu')
# it will insert value 'TamilNadu' into state table with country_id
country.state.find(:all,:conditions => {:name => 'TamilNadu'}).first.cities << City.new(:name =>'Chennai')
# it will append the new entries ' Chennai ' in “city” table with 'Country_id' and ' State_id' .
country=Country.new(:name=>'America') country.save country.state << State.new(:name => 'Ohio')country.state.find(:all,:conditions => {:name => 'Ohio'}).first.cities << City.new(:name =>'Chicago')
# Like wise, you can insert values into three tables as your wish # and insert more country, states and city names in corresponding tables . . .
class ComboCountryGlade include GetText
attr :glade def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE) bindtextdomain(domain, localedir, nil, "UTF-8") @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
@glade['window1'].show_all country_combo _method end
# ComboBoxEntry Methods
# country method :
def country_combo_method@glade["country_comboboxentry"].show_all @glade["state_comboboxentry"].hide_all @glade["State_label"].hide_all @glade["city_comboboxentry"].hide_all @glade["City_label"].hide_all
# here, we are hiding state and city comboboxentry and their lables
@country_combobox = @glade["country_comboboxentry"] @country_list = Gtk::ListStore.new(String) @country_cell = Gtk::CellRendererText.new()
# here, we are setting comboboxentry properties to box by calling inbuild Gtk methods
@country_combobox.pack_start(@country_cell, true) @country_combobox.add_attribute(@country_cell, 'text',0) @country_combobox.set_model(@country_list)
@country=Country.all
# here, we calling active record class. # [Country.all] ---> it will return all countries entries
@country.each do |country_name| @country_iter =@country_list.append @country_iter[0] =country_name.name
end end
=begin here, c_name is iterative variable of @country array record. From that, we need “ country name field alone”, so that we use [c_name.name] ----> it will return countries name.
[@c_list_cat.append] -----> it will helps to append strings in comboboxentry. =end
# ComboBoxEntry change signal code : for country
def on_country_comboboxentry_changed(widget) @country_click_iter= @glade['country_comboboxentry'].active_iter @country_list.each do |model,path,@iter_country| if path.to_s == @country_click_iter.to_s @glade['country_comboboxentry-entry1'].text =@iter_country[0] country_name=@glade['country_comboboxentry-entry1'].text @state_combo = @glade["state_comboboxentry"] @state_combo.clear state_combo_method(country_name) end end end
=begin
active_iter : return , pointer of comboboxentry list by corresponding to mouse or keyboard press on list.
path.to_s : to identify TreeView path of combobox entry.
state_combo(country_name) : Iside country change signal method, we are going to call “ State Comboboxentry and passing argument as country name, which is selected by user at run time.
same like this logic we have to write code for state and city comboboxentries also .
=end
# State mathod :
def state_combo_method(country_name) @glade["state_comboboxentry"].show_all @glade["State_label"].show_all @glade["city_comboboxentry"].hide_all @glade["City_label"].hide_all
@state_combobox = @glade["state_comboboxentry"] @state_list = Gtk::ListStore.new(String) @state_cell = Gtk::CellRendererText.new() @glade['state_comboboxentry-entry2'].text=""
@state_combobox.pack_start(@state_cell, true) @state_combobox.add_attribute(@state_cell, 'text',0) @state_combobox.set_model(@state_list)
country1=Country.find(:all,:conditions=>{:name=>country_name}).first.state country1.each do |state_name|
@state_iter =@state_list.append @state_iter[0] =state_name.name
end
end
# ComboBoxEntry change signal code : for state
def on_state_comboboxentry_changed(widget)
state_click_iter= @glade['state_comboboxentry'].active_iter @state_list.each do |model,path,@iter_state|
if path.to_s == state_click_iter.to_s @glade['state_comboboxentry-entry2'].text =@iter_state[0] state_name= @glade['state_comboboxentry-entry2'].text
@city_combo=@glade["city_comboboxentry"] @city_combo.clear
city_combo_method( state_name) end
end end end
# city method :
def city_combo_method(state_name) @glade["city_comboboxentry"].show_all @glade["City_label"].show_all @city_combo = @glade["city_comboboxentry"] @city_list = Gtk::ListStore.new(String) @city_cell = Gtk::CellRendererText.new() @glade['city_comboboxentry-entry3'].text="" @city_combo.pack_start(@city_cell, true)
@city_combo.add_attribute(@city_cell, 'text',0) @city_combo.set_model(@city_list)
city=State.all(:conditions=>{:name=>state_name}).first.cities
city.each do |city_name| @city_iter =@city_list.append @city_iter[0] =city_name.name
end end
# ComboBoxEntry change signal code : for city
def on_city_comboboxentry_changed(widget) @city_click_iter= @glade['city_comboboxentry'].active_iter
@city_list.each do |model,path,@iter_city| if path.to_s == @city_click_iter.to_s @glade['city_comboboxentry-entry3'].text =@iter_city[0] end
end end
# Main program for gladeif __FILE__ == $0 # Set values as your own application. PROG_PATH = "combo_country.glade" PROG_NAME = "YOUR_APPLICATION_NAME" ComboCountryGlade.new(PROG_PATH, nil, PROG_NAME) Gtk.main end
# End of program
=begin
Save the whole program as combo.rb
And then run this program in terminal.
$ruby combo.rb
while running this combo.rb , combo.glade file must contain in the same folder.
=end
Output window :1
Output window : 2
Output window : 3
Output window : 4
Output window : 5
Output window : 6
Output window : 7
Use :
Using this Chained Selection program , we are developing a school management application with following scenerio .
first select one 'class' from class_combobox_entry and then choose 'section' from section_combobox_entry and then finally select 'student name' from student_combobox_entry.
You can download this source code from : http://gist.github.com/127848
http://comboboxentry-glade-ruby.blogspot.com/
Enjoy with Ruby and Glade .
Best wishes from Arulalan.T