Class: Ruber::GuiPlugin

Inherits:
Plugin show all
Defined in:
lib/ruber/plugin.rb

Overview

Base class for all plugins which provide a GUI (that is, menu or toolbar entries).

Direct Known Subclasses

ExternalProgramPlugin

Constant Summary

Constants inherited from Plugin

Plugin::LICENSES

Instance Attribute Summary

Attributes included from PluginLike

#plugin_description

Instance Method Summary collapse

Methods inherited from Plugin

#about_data

Methods included from PluginLike

#add_extensions_to_project, #add_options_to_project, #add_widgets_to_project, #plugin_name, #query_close, #register_with_project, #remove_extensions_from_project, #remove_from_project, #remove_options_from_project, #remove_widgets_from_project, #restore_session, #save_settings, #session_data, #shutdown, #update_project

Constructor Details

#initialize(psf) ⇒ GuiPlugin

Creates an instance and initializes the GUI with the plugin

Parameters:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ruber/plugin.rb', line 134

def initialize psf
  super
  @gui = KDE::XMLGUIClient.new Ruber[:main_window]
  @gui.send :set_XML_file, psf.ui_file
# TODO when the KDE::ComponentData which takes a KDE::AboutData constructor
# works, construct the KDE::ComponentData using the value returned by the about_data
# method. As the following lines are (hopefully) temporarily, I only add the minimum
# to make the plugins show correctly in the shortcuts editor dialog.
  @gui.component_data = KDE::ComponentData.new Qt::ByteArray.new(plugin_name.to_s)
  data = @gui.component_data.about_data
  data.program_name = KDE.ki18n(@plugin_description.about.human_name)
  setup_actions
  Ruber[:main_window].factory.add_client @gui
end

Instance Method Details

#action_collectionKDE::ActionCollection

Returns the action collection used to contain the plugin’s actions.

Returns:

  • (KDE::ActionCollection)

    the action collection used to contain the plugin’s actions



161
162
163
# File 'lib/ruber/plugin.rb', line 161

def action_collection
  @gui.action_collection
end

#execute_action(name, *args) ⇒ Boolean

Executes the action with a given name, if it belongs to the plugin’s action collection

To execute the action, this method first checks whether it is included in the @actions@ entry of the PSF. If so, then the associated slot is directly called, passing _<notextile>*</notextile>args_ as argument. If the action is not included in that entry, then the action object is forced to emit a signal according to its class: @toggled(bool)@ for a @KDE::ToggleAction@, @triggered()@ for a @KDE::Action, and @triggered(*args)@ for all other actions.

If a plugin needs a different behaviour, for example because the slot connected to the action uses @Qt::Object.sender@, and thus can only be called from a signal, you’ll need to override this method and have the action emit the signal. To do so, you can use the following code:

bc.

a = action_collection.action(name) a.instance_evalsignal_name(arg)

where @name@ is the name of the action, @signal_name@ is the name of the signal to emit and @arg@ is the argument of the signal (you can pass more than one argument, if needed).

If the slot is called directly, args are the arguments to be passed to the slot. If the signal is emitted, args are the arguments passed to the signal.

Note: emitting the signal can (in rare cases) have unwanted results. This can happen if there are more than one slot connected to the signal, in which case all of them will be called. Usually, this shouldn’t be an issue since it’s common to connect only one signal to each action, but it can happen. This is why this method prefers to call the slot directly, whenever possible.

Parameters:

  • name (String, Symbol)

    the name of the action to execute

  • args (<Object>)

    the arguments to pass to the slot or the signal

Returns:

  • (Boolean)

    true if an action called name was found and false otherwise



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ruber/plugin.rb', line 202

def execute_action name, *args
  data = plugin_description.actions[name.to_s]
  if data
    slot = data.slot.sub(/\(.*/, '')
    instance_eval(data.receiver).send slot, *args
    true
  elsif (action = action_collection.action(name))
    if action.class == KDE::ToggleAction then KDE::ToggleAction
      action.instance_eval{emit toggled(*args)}
    elsif action.class == KDE::Action 
      action.instance_eval{emit triggered()}
    else action.instance_eval{emit triggered(*args)}
    end
    true
  else false
  end
end

#unloadnil

Removes the GUI provided by this plugin from the application’s GUI

Returns:

  • (nil)


153
154
155
156
# File 'lib/ruber/plugin.rb', line 153

def unload
  @gui.factory.remove_client @gui
  super
end