Module: Qt::Internal

Defined in:
lib/Qt/qtruby4.rb,
lib/Qt/qtruby4.rb

Defined Under Namespace

Classes: ModuleIndex

Constant Summary collapse

AccessPrivate =

From the enum MethodFlags in qt-copy/src/tools/moc/generator.cpp

0x00
AccessProtected =
0x01
AccessPublic =
0x02
MethodMethod =
0x00
MethodSignal =
0x04
MethodSlot =
0x08
MethodCompatibility =
0x10
MethodCloned =
0x20
MethodScriptable =
0x40
@@classes =
{}
@@cpp_names =
{}
@@idclass =
[]
@@normalize_procs =
[]

Class Method Summary collapse

Class Method Details

.add_normalize_proc(func) ⇒ Object



2496
2497
2498
# File 'lib/Qt/qtruby4.rb', line 2496

def self.add_normalize_proc(func)
  @@normalize_procs << func
end

.checkarg(argtype, typename) ⇒ Object



2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
# File 'lib/Qt/qtruby4.rb', line 2535

def Internal.checkarg(argtype, typename)
  const_point = typename =~ /^const\s+/ ? -1 : 0
  if argtype == 'i'
    if typename =~ /^int&?$|^signed int&?$|^signed$|^qint32&?$/
      return 6 + const_point
    elsif typename =~ /^quint32&?$/
      return 4 + const_point         
    elsif typename =~ /^(?:short|ushort|unsigned short int|unsigned short|uchar|char|unsigned char|uint|long|ulong|unsigned long int|unsigned|float|double|WId|HBITMAP__\*|HDC__\*|HFONT__\*|HICON__\*|HINSTANCE__\*|HPALETTE__\*|HRGN__\*|HWND__\*|Q_PID|^quint16&?$|^qint16&?$)$/
      return 4 + const_point
    elsif typename =~ /^(quint|qint|qulong|qlong|qreal)/
      return 4 + const_point
    else 
      t = typename.sub(/^const\s+/, '')
      t.sub!(/[&*]$/, '')
      if isEnum(t)
        return 2
      end
    end
  elsif argtype == 'n'
    if typename =~ /^double$|^qreal$/
      return 6 + const_point
    elsif typename =~ /^float$/
      return 4 + const_point
    elsif typename =~ /^int&?$/
      return 2 + const_point
    elsif typename =~ /^(?:short|ushort|uint|long|ulong|signed|unsigned|float|double)$/
      return 2 + const_point
    else 
      t = typename.sub(/^const\s+/, '')
      t.sub!(/[&*]$/, '')
      if isEnum(t)
        return 2 + const_point
      end
    end
  elsif argtype == 'B'
    if typename =~ /^(?:bool)[*&]?$/
      return 2 + const_point
    end
  elsif argtype == 's'
    if typename =~ /^(const )?((QChar)[*&]?)$/
      return 6 + const_point
    elsif typename =~ /^(?:(u(nsigned )?)?char\*)$/
      return 4 + const_point
    elsif typename =~ /^(?:const (u(nsigned )?)?char\*)$/
      return 2 + const_point
    elsif typename =~ /^(?:(?:const )?(QString)[*&]?)$/
      return 8 + const_point
    end
  elsif argtype == 'a'
    # FIXME: shouldn't be hardcoded. Installed handlers should tell what ruby type they expect.
    if typename =~ /^(?:
        const\ QCOORD\*|
        (?:const\ )?
        (?:
            QStringList[\*&]?|
            QValueList<int>[\*&]?|
            QRgb\*|
            char\*\*
        )
              )$/x
      return 2 + const_point
    end
  elsif argtype == 'u'
    # Give nil matched against string types a higher score than anything else
    if typename =~ /^(?:u?char\*|const u?char\*|(?:const )?((Q(C?)String))[*&]?)$/
      return 4 + const_point
    # Numerics will give a runtime conversion error, so they fail the match
    elsif typename =~ /^(?:short|ushort|uint|long|ulong|signed|unsigned|int)$/
      return -99
    else
      return 2 + const_point
    end
  elsif argtype == 'U'
    if typename =~ /QStringList/
      return 4 + const_point
    else
      return 2 + const_point
    end
  else
    t = typename.sub(/^const\s+/, '')
    t.sub!(/(::)?Ptr$/, '')
    t.sub!(/[&*]$/, '')
    if argtype == t
      return 4 + const_point
    elsif classIsa(argtype, t)
      return 2 + const_point
    elsif isEnum(argtype) and 
        (t =~ /int|qint32|uint|quint32|long|ulong/ or isEnum(t))
      return 2 + const_point
    end
  end
  return -99
end

.classesObject



2484
2485
2486
# File 'lib/Qt/qtruby4.rb', line 2484

def self.classes
  return @@classes
end

.connect(src, signal, target, block) ⇒ Object

Handles calls of the form:

connect(myobj, SIGNAL('mysig(int)'), mytarget) {|arg(s)| ...}
connect(myobj, SIGNAL('mysig(int)')) {|arg(s)| ...}
connect(myobj, SIGNAL(:mysig), mytarget) { ...}
connect(myobj, SIGNAL(:mysig)) { ...}


2960
2961
2962
2963
2964
2965
2966
2967
# File 'lib/Qt/qtruby4.rb', line 2960

def Internal.connect(src, signal, target, block)
  args = (signal =~ /\((.*)\)/) ? $1 : ""
  signature = Qt::MetaObject.normalizedSignature("invoke(%s)" % args).to_s
  return Qt::Object.connect(  src,
                signal,
                Qt::BlockInvocation.new(target, block.to_proc, signature),
                SLOT(signature) )
end

.cpp_namesObject



2488
2489
2490
# File 'lib/Qt/qtruby4.rb', line 2488

def self.cpp_names
  return @@cpp_names
end

.create_qenum(num, enum_type) ⇒ Object



2830
2831
2832
# File 'lib/Qt/qtruby4.rb', line 2830

def Internal.create_qenum(num, enum_type)
  return Qt::Enum.new(num, enum_type)
end

.debug_levelObject



2531
2532
2533
# File 'lib/Qt/qtruby4.rb', line 2531

def Internal.debug_level
  Qt.debug_level
end

.do_method_missing(package, method, klass, this, *args) ⇒ Object

Looks up and executes a Qt method

package - Always the string ‘Qt’ method - Methodname as a string klass - Ruby class object this - instance of class args - arguments to method call



2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
# File 'lib/Qt/qtruby4.rb', line 2665

def Internal.do_method_missing(package, method, klass, this, *args)
  # Determine class name
  if klass.class == Module
    # If a module use the module's name - typically Qt
    classname = klass.name
  else
    # Lookup Qt class name from Ruby class name
    classname = @@cpp_names[klass.name]
    if classname.nil?
      # Make sure we haven't backed all the way up to Object
      if klass != Object and klass != Qt
        # Don't recognize this class so try the superclass
        return do_method_missing(package, method, klass.superclass, this, *args)
      else
        # Give up if we back all the way up to Object
        return nil
      end
    end
  end
 
  # Modify constructor method name from new to the name of the Qt class 
  # and remove any namespacing 
  if method == "new"
    method = classname.dup 
    method.gsub!(/^.*::/,"")
  end

  # If the method contains no letters it must be an operator, append "operator" to the 
  # method name
  method = "operator" + method.sub("@","") if method !~ /[a-zA-Z]+/

  # Change foobar= to setFoobar()          
  method = 'set' + method[0,1].upcase + method[1,method.length].sub("=", "") if method =~ /.*[^-+%\/|=]=$/ && method != 'operator='

  # Build list of munged method names which is the methodname followed
  # by symbols that indicate the basic type of the method's arguments
  #
  # Plain scalar = $
  # Object = #
  # Non-scalar (reference to array or hash, undef) = ?
  #
  methods = []
  methods << method.dup
  args.each do |arg|
    if arg.nil?
      # For each nil arg encountered, triple the number of munged method
      # templates, in order to cover all possible types that can match nil
      temp = []
      methods.collect! do |meth| 
        temp << meth + '?' 
        temp << meth + '#'
        meth << '$'
      end
      methods.concat(temp)
    elsif isObject(arg)
      methods.collect! { |meth| meth << '#' }
    elsif arg.kind_of? Array or arg.kind_of? Hash
      methods.collect! { |meth| meth << '?' }
    else
      methods.collect! { |meth| meth << '$' }
    end
  end
 
  # Create list of methodIds that match classname and munged method name 
  methodIds = []
  methods.collect { |meth| methodIds.concat( findMethod(classname, meth) ) }

  # If we didn't find any methods and the method name contains an underscore
  # then convert to camelcase and try again      
  if method =~ /._./ && methodIds.length == 0
    # If the method name contains underscores, convert to camel case
    # form and try again
    method.gsub!(/(.)_(.)/) {$1 + $2.upcase}
    return do_method_missing(package, method, klass, this, *args)
  end

  # Debugging output for method lookup
  if debug_level >= DebugLevel::High
    puts "Searching for #{classname}##{method}"
    puts "Munged method names:"
    methods.each {|meth| puts "        #{meth}"}
    puts "candidate list:"
    prototypes = dumpCandidates(methodIds).split("\n")
    line_len = (prototypes.collect { |p| p.length }).max
    prototypes.zip(methodIds) { 
      |prototype,id| puts "#{prototype.ljust line_len}  (smoke: #{id.smoke} index: #{id.index})" 
    }
  end
 
  # Find the best match 
  chosen = nil
  if methodIds.length > 0
    best_match = -1
    methodIds.each do
      |id|
      puts "matching => smoke: #{id.smoke} index: #{id.index}" if debug_level >= DebugLevel::High
      current_match = (isConstMethod(id) ? 1 : 0)
      (0...args.length).each do
        |i|
        typename = get_arg_type_name(id, i)
        argtype = get_value_type(args[i])
        score = checkarg(argtype, typename)
        current_match += score
        puts "        #{typename} (#{argtype}) score: #{score}" if debug_level >= DebugLevel::High
      end
      
      # Note that if current_match > best_match, then chosen must be nil
      if current_match > best_match
        best_match = current_match
        chosen = id
      # Ties are bad - but it is better to chose something than to fail
      elsif current_match == best_match && id.smoke == chosen.smoke
        puts " ****** warning: multiple methods with the same score of #{current_match}: #{chosen.index} and #{id.index}" if debug_level >= DebugLevel::Minimal
        chosen = id
      end
      puts "        match => smoke: #{id.smoke} index: #{id.index} score: #{current_match} chosen: #{chosen ? chosen.index : nil}" if debug_level >= DebugLevel::High
    end
  end

  # Additional debugging output
  if debug_level >= DebugLevel::Minimal && chosen.nil? && method !~ /^operator/
    id = find_pclassid(normalize_classname(klass.name))
    hash = findAllMethods(id)
    constructor_names = nil
    if method == classname
      puts "No matching constructor found, possibles:\n"
      constructor_names = hash.keys.grep(/^#{classname}/)
    else
      puts "Possible prototypes:"
      constructor_names = hash.keys
    end
    method_ids = hash.values_at(*constructor_names).flatten
    puts dumpCandidates(method_ids)
  else
    puts "setCurrentMethod(smokeList index: #{chosen.smoke}, meth index: #{chosen.index})" if debug_level >= DebugLevel::High && chosen
  end

  # Select the chosen method
  setCurrentMethod(chosen) if chosen
  return nil
end

.find_class(classname) ⇒ Object



2629
2630
2631
# File 'lib/Qt/qtruby4.rb', line 2629

def Internal.find_class(classname)
  @@classes[classname]
end

.get_qboolean(b) ⇒ Object



2838
2839
2840
# File 'lib/Qt/qtruby4.rb', line 2838

def Internal.get_qboolean(b)
  return b.value
end

.get_qenum_type(e) ⇒ Object



2834
2835
2836
# File 'lib/Qt/qtruby4.rb', line 2834

def Internal.get_qenum_type(e)
  return e.type
end

.get_qinteger(num) ⇒ Object



2822
2823
2824
# File 'lib/Qt/qtruby4.rb', line 2822

def Internal.get_qinteger(num)
  return num.value
end

.getAllParents(class_id, res) ⇒ Object



2846
2847
2848
2849
2850
2851
2852
# File 'lib/Qt/qtruby4.rb', line 2846

def Internal.getAllParents(class_id, res)
  getIsa(class_id).each do |s|
    c = findClass(s)
    res << c
    getAllParents(c, res)
  end
end

.getMetaObject(klass, qobject) ⇒ Object



2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
# File 'lib/Qt/qtruby4.rb', line 2927

def Internal.getMetaObject(klass, qobject)
  if klass.nil?
    klass = qobject.class
  end

  parentMeta = nil
  if @@cpp_names[klass.superclass.name].nil?
    parentMeta = getMetaObject(klass.superclass, qobject)
  end

  meta = Meta[klass.name]
  if meta.nil?
    meta = Qt::MetaInfo.new(klass) 
  end

  if meta.metaobject.nil? or meta.changed
    stringdata, data = (  qobject.class.name,
                      meta.classinfos,  
                      meta.dbus,
                      meta.signals, 
                      meta.slots )
    meta.metaobject = make_metaObject(qobject, parentMeta, stringdata, data)
    meta.changed = false
  end
  
  meta.metaobject
end

.idclassObject



2492
2493
2494
# File 'lib/Qt/qtruby4.rb', line 2492

def self.idclass
  return @@idclass
end

.init_all_classesObject



2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
# File 'lib/Qt/qtruby4.rb', line 2807

def Internal.init_all_classes()
  Qt::Internal::getClassList().each do |c|
    if c == "Qt"
      # Don't change Qt to Qt::t, just leave as is
      @@cpp_names["Qt"] = c
    elsif c != "QInternal" && !c.empty?
      Qt::Internal::init_class(c)
    end
  end

  @@classes['Qt::Integer'] = Qt::Integer
  @@classes['Qt::Boolean'] = Qt::Boolean
  @@classes['Qt::Enum'] = Qt::Enum
end

.init_class(c) ⇒ Object



2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
# File 'lib/Qt/qtruby4.rb', line 2517

def Internal.init_class(c)
  if c == "WebCore" || c == "std" || c == "QGlobalSpace"
    return
  end
  classname = Qt::Internal::normalize_classname(c)
  classId = Qt::Internal.findClass(c)
  insert_pclassid(classname, classId)
  @@idclass[classId.index] = classname
  @@cpp_names[classname] = c
  klass = isQObject(c) ? create_qobject_class(classname, Qt) \
                                               : create_qt_class(classname, Qt)
  @@classes[classname] = klass unless klass.nil?
end

.makeMetaData(classname, classinfos, dbus, signals, slots) ⇒ Object



2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
# File 'lib/Qt/qtruby4.rb', line 2874

def Internal.(classname, classinfos, dbus, signals, slots)
  # Each entry in 'stringdata' corresponds to a string in the
  # qt_meta_stringdata_<classname> structure.
  # 'pack_string' is used to convert 'stringdata' into the
  # binary sequence of null terminated strings for the metaObject
  stringdata = []
  pack_string = ""
  string_table = string_table_handler(stringdata, pack_string)

  # This is used to create the array of uints that make up the
  # qt_meta_data_<classname> structure in the metaObject
  data = [1,                 # revision
      string_table.call(classname),   # classname
      classinfos.length, classinfos.length > 0 ? 10 : 0,   # classinfo
      signals.length + slots.length, 
      10 + (2*classinfos.length),   # methods
      0, 0,               # properties
      0, 0]              # enums/sets

  classinfos.each do |entry|
    data.push string_table.call(entry[0])    # key
    data.push string_table.call(entry[1])    # value
  end

  signals.each do |entry|
    data.push string_table.call(entry.full_name)        # signature
    data.push string_table.call(entry.full_name.delete("^,"))  # parameters
    data.push string_table.call(entry.reply_type)        # type, "" means void
    data.push string_table.call("")        # tag
    if dbus
      data.push MethodScriptable | MethodSignal | AccessPublic
    else
      data.push entry.access  # flags, always protected for now
    end
  end

  slots.each do |entry|
    data.push string_table.call(entry.full_name)        # signature
    data.push string_table.call(entry.full_name.delete("^,"))  # parameters
    data.push string_table.call(entry.reply_type)        # type, "" means void
    data.push string_table.call("")        # tag
    if dbus
      data.push MethodScriptable | MethodSlot | AccessPublic  # flags, always public for now
    else
      data.push entry.access    # flags, always public for now
    end
  end

  data.push 0    # eod

  return [stringdata.pack(pack_string), data]
end

.method_connect(src, signal, target, method) ⇒ Object

Handles calls of the form:

connect(:mysig, mytarget, :mymethod))
connect(SIGNAL('mysignal(int)'), mytarget, :mymethod))


2984
2985
2986
2987
2988
2989
2990
2991
2992
# File 'lib/Qt/qtruby4.rb', line 2984

def Internal.method_connect(src, signal, target, method)
  signal = SIGNAL(signal) if signal.is_a?Symbol
  args = (signal =~ /\((.*)\)/) ? $1 : ""
  signature = Qt::MetaObject.normalizedSignature("invoke(%s)" % args).to_s
  return Qt::Object.connect(  src,
                signal,
                Qt::MethodInvocation.new(target, method, signature),
                SLOT(signature) )
end

.normalize_classname(classname) ⇒ Object



2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
# File 'lib/Qt/qtruby4.rb', line 2500

def Internal.normalize_classname(classname)
  @@normalize_procs.each do |func|
    ret = func.call(classname)
    if !ret.nil?
      return ret
    end
  end
  if classname =~ /^Q3/
    ruby_classname = classname.sub(/^Q3(?=[A-Z])/,'Qt3::')
  elsif classname =~ /^Q/
    ruby_classname = classname.sub(/^Q(?=[A-Z])/,'Qt::')
  else
    ruby_classname = classname
  end
  ruby_classname
end

.run_initializer_block(instance, block) ⇒ Object

If a block was passed to the constructor, then run that now. Either run the context of the new instance if no args were passed to the block. Or otherwise, run the block in the context of the arg.



2647
2648
2649
2650
2651
2652
2653
2654
2655
# File 'lib/Qt/qtruby4.rb', line 2647

def Internal.run_initializer_block(instance, block)
  if block.arity == -1 || block.arity == 0
    instance.instance_eval(&block)
  elsif block.arity == 1
    block.call(instance)
  else
    raise ArgumentError, "Wrong number of arguments to block(#{block.arity} for 1)"
  end
end

.set_qboolean(b, val) ⇒ Object



2842
2843
2844
# File 'lib/Qt/qtruby4.rb', line 2842

def Internal.set_qboolean(b, val)
  return b.value = val
end

.set_qinteger(num, val) ⇒ Object



2826
2827
2828
# File 'lib/Qt/qtruby4.rb', line 2826

def Internal.set_qinteger(num, val)
  return num.value = val
end

.signal_connect(src, signal, block) ⇒ Object

Handles calls of the form:

connect(SIGNAL(:mysig)) { ...}
connect(SIGNAL('mysig(int)')) {|arg(s)| ...}


2972
2973
2974
2975
2976
2977
2978
2979
# File 'lib/Qt/qtruby4.rb', line 2972

def Internal.signal_connect(src, signal, block)
  args = (signal =~ /\((.*)\)/) ? $1 : ""
  signature = Qt::MetaObject.normalizedSignature("invoke(%s)" % args).to_s
  return Qt::Object.connect(  src,
                signal,
                Qt::SignalBlockInvocation.new(src, block.to_proc, signature),
                SLOT(signature) )
end

.single_shot_timer_connect(interval, target, block) ⇒ Object

Handles calls of the form:

Qt::Timer.singleShot(500, myobj) { ...}


2996
2997
2998
2999
3000
# File 'lib/Qt/qtruby4.rb', line 2996

def Internal.single_shot_timer_connect(interval, target, block)
  return Qt::Timer.singleShot(  interval,
                  Qt::BlockInvocation.new(target, block, "invoke()"),
                  SLOT("invoke()") )
end

.string_table_handler(data, pack_str) ⇒ Object

Keeps a hash of strings against their corresponding offsets within the qt_meta_stringdata sequence of null terminated strings. Returns a proc to get an offset given a string. That proc also adds new strings to the ‘data’ array, and updates the corresponding ‘pack_str’ Array#pack template.



2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
# File 'lib/Qt/qtruby4.rb', line 2859

def Internal.string_table_handler(data, pack_str)
  hsh = {}
  offset = 0
  return lambda do |str|
    if !hsh.has_key? str
      hsh[str] = offset
      data << str
      pack_str << "a*x"
      offset += str.length + 1
    end

    return hsh[str]
  end
end

.try_initialize(instance, *args) ⇒ Object

Runs the initializer as far as allocating the Qt C++ instance. Then use a throw to jump back to here with the C++ instance wrapped in a new ruby variable of type T_DATA



2636
2637
2638
2639
2640
2641
# File 'lib/Qt/qtruby4.rb', line 2636

def Internal.try_initialize(instance, *args)
  initializer = instance.method(:initialize)
  catch :newqt do
    initializer.call(*args)
  end
end