Class: Qt::Widget

Inherits:
Object show all
Defined in:
lib/qtext/extensions.rb

Class Method Summary collapse

Class Method Details

.construct_signal(signature, method_name = nil) ⇒ Object

add a signal with the given signature, and create a method called method_name which will emit the signal, a method called method_name_signal which will return the signal ready to be used in a connect. If method_name is not provided the method will be named as the name part of signature. Also create a handler on_method_name {|*args| }



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/qtext/extensions.rb', line 316

def self.construct_signal( signature, method_name = nil )
  # add the signal
  q_signal( signature )
  
  # add an accessor method that returns the result of SIGNAL
  if method_name.nil?
    md = /([\w_]+) *\(/.match( signature )
    base_method_name = md[1]
    method_name = base_method_name + '_signal'
  end
  
  # create the accessor method which returns
  # a signal ready to be connected
  define_method method_name do
    SIGNAL( signature )
  end
  
  # create the connection method
  line, st = __LINE__, <<-EOF
    def on_#{base_method_name}( &block )
      connect( #{method_name} ) do |*args|
        yield( *args )
      end
    end
  EOF
  class_eval st, __FILE__, line + 1
end

.signal(symbol) ⇒ Object

specify a signal symbol, and add the other methods, namely

  • symbol which emits the signal

  • symbol_signal which is the method added by q_signal, ie the actual Qt emitter

  • on_symbol {|*args| } which is the handler



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/qtext/extensions.rb', line 348

def self.signal( symbol )
  # add the signal emitter
  signature = "#{symbol.to_s}_signal( QVariant & )"
  q_signal( signature )
  
  # add an emitter
  line_number, st = __LINE__, <<-EOF
    def #{symbol.to_s}( *args )
      #~ puts "ingoing args: \#\{args.inspect\}"
      # convert args to variant(s)
      variant =
      case args.size
        when 0; Qt::Variant.new
        when 1; args[0].to_variant
        else; Qt::Variant.new( args )
      end
      #~ puts "ingoing variant: \#\{variant.inspect\}"
      emit #{symbol.to_s}_signal( variant )
    end
  EOF
  class_eval st, __FILE__, line_number + 1
  
  # create the connection method
  line_number, st = __LINE__, <<-EOF
    def on_#{symbol.to_s}( &block )
      Kernel.raise "on_#{symbol.to_s} must have a block" if block.nil?
      connect( SIGNAL( '#{signature}' ) ) do |variant|
        #~ puts "outcoming variant: \#\{variant.inspect\}"
        # convert variants to Ruby objects
        args =
        case variant.typeName
          when nil; nil
          when 'QVariantList'; variant.value.map{|x| x.value}
          else variant.value
        end
        #~ puts "outcoming args: \#\{args.inspect\}"
        yield( *args )
      end
    end
  EOF
  class_eval st, __FILE__, line_number + 1
end