Class: Ruber::SettingsDialog

Inherits:
KDE::PageDialog
  • Object
show all
Defined in:
lib/ruber/settings_dialog.rb,
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:

class MyWidget < Qt::Widget</code>

<code>  def initialize parent = nil
    super
    @default_dir_widget = KDE::LineEdit.new self
    @default_file_widget = KDE::LineEdit.new self
  end</code>

<code>  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</code>
  
<code>  def store_settings
    path = File.join @default_dir_widget.text, @default_file_widget.text
    @settings_dialog.settings_container[:general, :default_path] = path
  end</code>
  
<code>  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</code>

<code>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

ProjectDialog

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SettingsDialog) initialize(container, options, widgets, title = nil)

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, options, widgets, title = nil
  super Ruber[:main_window]
  self.buttons = Ok|Apply|Cancel|Default
  self.window_title = title if title
  @container = container
  grouped_widgets = Hash.new{|h, k| h[k] = []}
  widgets.each{|w| grouped_widgets[w.caption] << w }
  @page_items = []
  @widgets = Hash.new{|h, k| h[k] = []}
  grouped_widgets.keys.sort.each do |c|
    icon = nil
    page = Qt::Widget.new self
    page.layout = Qt::VBoxLayout.new page
    grouped_widgets[c].each do |w| 
      widget = if w.respond_to?(:code) and w.code then widget_from_code(w.code) 
      else widget_from_class(w.class_obj)
      end
      widget.parent = page
      widget.instance_variable_set :@settings_dialog, self
      @widgets[c] << widget
      page.layout.add_widget widget
      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, options, @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)
  enable_button_apply false
end

Instance Attribute Details

- (Object) container 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

- (Object) exec

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.widget.layout.item_at(0).widget.set_focus
  end
  super
end

- (Object) read_default_settings

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

Slot Signature:

read_default_settings()

- (Object) read_settings

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

- (Object) show

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.widget.layout.item_at(0).widget.set_focus
  super
end

- (Object) store_settings

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

Slot Signature:

store_settings()

- (Object) widget_from_class(cls) (private)

Method called to create a widget which is specified using the class_obj entry in the third argument to initialize. cls is the widget’s class.

The widget is created simply calling new on cls (no arguments are passed to it). Derived classes may want to override it to change this behaviour (for example, they may want to pass some argument to new).



239
240
241
# File 'lib/ruber/settings_dialog.rb', line 239

def widget_from_class cls
  cls.new
end

- (Object) widget_from_code(code) (private)

Method called to create a widget which is specified using a code entry in the third argument to initialize. code is the string containing the code.

It evaluates code in the toplevel binding and returns the resulting object. Derived classes may override it to change this behaviour (for example, they may want to evaluate the code in another binding).



227
228
229
# File 'lib/ruber/settings_dialog.rb', line 227

def widget_from_code code
  eval code, TOPLEVEL_BINDING
end

- (Object) widgets

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 widgets
  res = []
  @widgets.each_value{|a| res += a}
  res
end