Module: Ruber::KTextEditorWrapper

Included in:
Document, EditorView
Defined in:
lib/ruber/editor/ktexteditor_wrapper.rb

Overview

Module which abstracts some common functionality of Document and EditorView due to the fact that both are mostly simply wrappers around two KTextEditor classes.

Throught this documentation, the KTextEditor::Document or KTextEditor::View corresponding to the object will be referred to as the wrapped object.

In particular, this class:

  • provide a way to access methods provided by the different interfaces implemented by the wrapped object without directly accessing it (using InterfaceProxy)
  • redirect to the wrapped object any method call the object itself doesn’t respond to
  • provide a (private) method to access the wrapped object
  • provide some automatization in the process having the object emit an appropriate signal when the wrapped object emits a signal.

Defined Under Namespace

Classes: InterfaceProxy

Class Method Summary (collapse)

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args, &blk)

Forwards the method call to the wrapped object



156
157
158
159
160
161
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 156

def method_missing name, *args, &blk
  begin super
  rescue NoMethodError, NameError, NotImplementedError
    @_wrapped.send name, *args, &blk
  end
end

Class Method Details

+ (Object) prepare_wrapper_connections(cls, data)

Makes easier having the object emit a signal in corrispondence with signals emitted by the wrapped object. In particular, this method does the following (for each signal given in the arguments):

  • creates a signal for the object based on the name of the signal in the wrapped object
  • creates a slot for the object with a name obtained from the name of the signal, which takes the same arguments as the signal in the wrapped object and emits the associated signal of the object. The arguments of the emitted signal are taken from those passed to the slot, but they can be reordered. Also, self can be specified
  • returns a hash suitable to be passed as second argument to initialize_wrapper. Usually, this method is called when the class is created and the returned hash is stored in a class instance variable. The class’s initialize method then passes it as second argument to initialize_wrapper.

cls is the class for which signals and slots will be defined. data is a hash containing the information for the signal and slot to create.

  • its keys are the names of the signals converted to snakecase. This name will be converted to camelcase to obtain the name of the signal of the wrapped object, while will be used as is for the signal of the object.
  • its values are arrays. The first element is the signature of the signal of the wrapped object. The second element is an array of integers and *nil*s. An integer i in the position n of the array means that the _n_th argument of the new signal will be the _i_th argument of the signal of the wrapped object (both indexes start from 0). If the array contains nil in position n, the _n_th argument of the new signal will be self (in the signature of the new signal, this will mean QWidget* if cls derives from Qt::Widget and QObject* otherwise).


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
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 115

def self.prepare_wrapper_connections cls, data
  res = {}
  data.each_pair do |name, d|
    old_args = d[0].split(/,\s*/)
    new_args = d[1].map do |i| 
      if i then old_args[i]
      elsif cls.ancestors.include?(Qt::Widget) then 'QWidget*'
      else 'QObject*'
      end
    end
    new_args = new_args.join ', '
    new_signal = "#{name}(#{new_args})"
    old_signal = "#{name.camelcase :lower}(#{d[0]})"
    slot_name = "__emit_#{name}_signal(#{d[0]})"
    cls.send :signals, new_signal
    cls.send :slots, slot_name
    res[old_signal] = slot_name
    method_def = <<-EOS
def __emit_#{name}_signal(#{old_args.size.times.map{|i| "a#{i}"}.join ', '})
  emit #{name}(#{d[1].map{|i| i ? "a#{i}" : 'self'}.join ', '})
end
EOS
    cls.class_eval method_def
  end
  res
end

Instance Method Details

- (Object) connect_wrapped_signals(signals) (private)

Makes a connections between a series of signals and of slots. signals is a hash whose keys are the names of the signals and whose entries are the slots each signal should be connected to (only one slot can be connected to each signal). You can’t connect a signal to another signal with this method.

This method is mainly meant to be used in conjunction with prepare_wrapper_connections.



194
195
196
197
198
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 194

def connect_wrapped_signals signals
  signals.each_pair do |k, v|
    connect @_wrapped, SIGNAL(k), self, SLOT(v)
  end
end

- (Object) initialize_wrapper(obj, signals) (private)

Method which needs to be called before using any of the functionality provided by the module (usually, it’s called from initialize). obj is the wrapped object, while signals is a hash as described in connect_wrapped_signals.

It creates an InterfaceProxy for the wrapped object and calls connect_wrapped_signals passing signals as argument.



173
174
175
176
177
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 173

def initialize_wrapper obj, signals
  @_interface = InterfaceProxy.new obj
  @_wrapped = obj
  connect_wrapped_signals signals
end

- (Object) interface(name)

Returns an InterfaceProxy set to the interface name. See InterfaceProxy#interface= for the possible values for name. It is meant to be used this way (supposing doc is an object which wraps KTextEditor::Document) doc.interface(:modification_interface).modified_on_disk_warning = true



148
149
150
151
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 148

def interface name
  @_interface.interface = name
  @_interface
end

- (Object) internal (private)

Returns the wrapped object (this method is mainly for internal use)



182
183
184
# File 'lib/ruber/editor/ktexteditor_wrapper.rb', line 182

def internal
  @_wrapped
end