Class: Ruber::SettingsDialog
- Defined in:
- lib/ruber/settings_dialog.rb
Overview
Dialog used to change the options contained in an SettingsContainer. It is a specialized KDE::PageDialog
.
The widgets added to the option container are displayed in pages corresponding to the captions included in the widgets’ description. If more than one widget has the same caption, they’re displayed in the same page, one below the other (using a Qt::VBoxLayout). The icon used for each page is the first one found among the widgets in that page.
The process of keeping the options in sync between the widgets and the option container can be automatized using the functionality provided by the internal SettingsDialogManager. If this automatic management can’t be achieved for a particular option (for example, because the value should be obtained combining the data of more than one widget), you can still automatically manage the other widgets and integrate the manual management of the widgets which need it with the automatic management. To do so, you’ll need to define a read_settings, a store_settings and a read_default_settings method in your widget. In this case, you can access the dialog using the @settings_dialog instance variable, which is created by this class when the widget is created.
Manual option management example
Suppose you have an option called :default_path
in the :general group which stores a path. In your widget, however, the path is split in two parts: the directory and the filename, which are shown in two line edit widgets, called respectively default_dir_widget
and default_file_widget
. Since the automatic option management system assumes that each option corresponds to a single widget, you can’t use it. So, you define a read_settings
, a store_settings
and a read_default_settings
method in your widget class like this:
bc.. class MyWidget < Qt::Widget
def initialize parent = nil
super
@default_dir_widget = KDE::LineEdit.new self
@default_file_widget = KDE::LineEdit.new self
end
def read_settings
path = @settings_dialog.settings_container[:general, :default_path]
@default_dir_widget.text = File.dirname(path)
@default_file_widget.text = File.basename(path)
end
def store_settings
path = File.join @default_dir_widget.text, @default_file_widget.text
@settings_dialog.settings_container[:general, :default_path] = path
end
def read_default_settings
path = @settings_dialog.settings_container.default(:general, :default_path)
@default_dir_widget.text = File.dirname(path)
@default_file_widget.text = File.basename(path)
end
end
-
Note that the
@settings_dialog
instance variable has been automatically
created by the dialog when the widget has been created and contains a reference to the dialog itself.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#container ⇒ Object
(also: #settings_container)
The option container.
Instance Method Summary collapse
-
#exec ⇒ Object
Override of
KDE::PageDialog#exec
which makes sure the first page is the current page and gives focus to the first widget on the page and reads the settings from the option container. -
#initialize(container, options, widgets, title = nil) ⇒ SettingsDialog
constructor
Creates a new SettingsDialog.
-
#read_default_settings ⇒ Object
This method works as read_settings except for the fact that it reads the default values of the options instad of the values set by the user.
-
#read_settings ⇒ Object
This method reads settings from the option container and displays them in the widgets.
-
#show ⇒ Object
Override of
KDE::PageDialog#show
which makes sure the first page is the current page and gives focus to the first widget on the page and reads the settings from the option container. -
#store_settings ⇒ Object
This method takes the values from the dialog widgets and stores them in to option container.
-
#widgets ⇒ Object
Returns an array containing all the widgets created for the dialog, that is the widgets created from the data in the third argument of the constructor.
Constructor Details
#initialize(container, options, widgets, title = nil) ⇒ SettingsDialog
Creates a new SettingsDialog. The dialog will store settings in the SettingsContainer container, manage the options options and display the widgets widgets. options is an array of option objects (see SettingsContainer#add_option). widgets is an array containing the description of the widgets to be displayed in the dialog (see SettingsContainer#add_widget).
This method defines a new instance variable for each widget it creates. The variable is called @settings_dialog and contains a reference to the dialog itself. It can be used from custom widgets’ implementation of read_settings, store_settings and read_default_settings methods to access the option container corresponding to the dialog.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ruber/settings_dialog.rb', line 111 def initialize container, , , title = nil super Ruber[:main_window] self. = Ok|Apply|Cancel|Default self.window_title = title if title @container = container = Hash.new{|h, k| h[k] = []} .each{|w| [w.] << w } @page_items = [] @widgets = Hash.new{|h, k| h[k] = []} .keys.sort.each do |c| icon = nil page = Qt::Widget.new self page.layout = Qt::VBoxLayout.new page [c].each do |w| = if w.respond_to?(:code) and w.code then (w.code) else (w.class_obj) end .parent = page .instance_variable_set :@settings_dialog, self @widgets[c] << page.layout. icon ||= KDE::Icon.new w.pixmap if w.pixmap rescue nil end @page_items << add_page(page, c) @page_items[-1].icon = icon if icon end @manager = SettingsDialogManager.new self, , @widgets.values.flatten connect self, SIGNAL(:okClicked), self, SLOT(:store_settings) connect self, SIGNAL(:applyClicked), self, SLOT(:store_settings) connect self, SIGNAL(:defaultClicked), self, SLOT(:read_default_settings) false end |
Instance Attribute Details
#container ⇒ Object Also known as: settings_container
The option container. This method can be used by the widgets which need to define the read_settings, store_settings and read_default_settings methods to access the option container of the dialog.
95 96 97 |
# File 'lib/ruber/settings_dialog.rb', line 95 def container @container end |
Instance Method Details
#exec ⇒ Object
Override of KDE::PageDialog#exec
which makes sure the first page is the current page and gives focus to the first widget on the page and reads the settings from the option container.
This is needed because usually the dialog object isn’t deleted after having been closed, so the interface should be updated manually.
193 194 195 196 197 198 199 200 |
# File 'lib/ruber/settings_dialog.rb', line 193 def exec read_settings if @page_items[0] self.current_page = @page_items[0] self.current_page..layout.item_at(0)..set_focus end super end |
#read_default_settings ⇒ Object
This method works as read_settings except for the fact that it reads the default values of the options instad of the values set by the user.
180 181 182 183 |
# File 'lib/ruber/settings_dialog.rb', line 180 def read_default_settings @manager.read_default_settings @widgets.values.flatten.each{|w| w.read_default_settings if w.respond_to? :read_default_settings} end |
#read_settings ⇒ Object
This method reads settings from the option container and displays them in the widgets. It calls the read_settings method of the option dialog manager and the read_settings method of each widgets which provide it.
159 160 161 162 |
# File 'lib/ruber/settings_dialog.rb', line 159 def read_settings @manager.read_settings @widgets.values.flatten.each{|w| w.read_settings if w.respond_to? :read_settings} end |
#show ⇒ Object
Override of KDE::PageDialog#show
which makes sure the first page is the current page and gives focus to the first widget on the page and reads the settings from the option container.
This is needed because usually the dialog object isn’t deleted after having been closed, so the interface should be updated manually.
210 211 212 213 214 215 |
# File 'lib/ruber/settings_dialog.rb', line 210 def show read_settings self.current_page = @page_items[0] self.current_page..layout.item_at(0)..set_focus super end |
#store_settings ⇒ Object
This method takes the values from the dialog widgets and stores them in to option container. It calls the store_settings method of the option dialog manager and the store_settings method of each widgets which provide it, then calls the write method of the container so that the options are written to file.
170 171 172 173 174 |
# File 'lib/ruber/settings_dialog.rb', line 170 def store_settings @manager.store_settings @widgets.values.flatten.each{|w| w.store_settings if w.respond_to? :store_settings} @container.write end |
#widgets ⇒ Object
Returns an array containing all the widgets created for the dialog, that is the widgets created from the data in the third argument of the constructor.
148 149 150 151 152 |
# File 'lib/ruber/settings_dialog.rb', line 148 def res = [] @widgets.each_value{|a| res += a} res end |