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



2467
2468
2469
# File 'lib/Qt/qtruby4.rb', line 2467

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

.checkarg(argtype, typename) ⇒ Object



2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
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
# File 'lib/Qt/qtruby4.rb', line 2506

def Internal.checkarg(argtype, typename)
	puts "      #{typename} (#{argtype})" if debug_level >= DebugLevel::High
	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||unsigned char|uint|long|ulong|unsigned long int|unsigned|float|double|WId|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



2455
2456
2457
# File 'lib/Qt/qtruby4.rb', line 2455

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)) { …}



2901
2902
2903
2904
2905
2906
2907
2908
# File 'lib/Qt/qtruby4.rb', line 2901

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, signature),
								SLOT(signature) )
end

.cpp_namesObject



2459
2460
2461
# File 'lib/Qt/qtruby4.rb', line 2459

def self.cpp_names
	return @@cpp_names
end

.create_qenum(num, enum_type) ⇒ Object



2771
2772
2773
# File 'lib/Qt/qtruby4.rb', line 2771

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

.debug_levelObject



2502
2503
2504
# File 'lib/Qt/qtruby4.rb', line 2502

def Internal.debug_level
	Qt.debug_level
end

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



2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
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
# File 'lib/Qt/qtruby4.rb', line 2629

def Internal.do_method_missing(package, method, klass, this, *args)
	if klass.class == Module
		classname = klass.name
	else
		classname = @@cpp_names[klass.name]
		if classname.nil?
			if klass != Object and klass != Qt
				return do_method_missing(package, method, klass.superclass, this, *args)
			else
				return nil
			end
		end
	end
	
	if method == "new"
		method = classname.dup 
		method.gsub!(/^.*::/,"")
	end
	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='

	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
	
	methodIds = []
	methods.collect { |meth| methodIds.concat( findMethod(classname, meth) ) }
	
	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

	if debug_level >= DebugLevel::High
		puts "classname    == #{classname}"
		puts ":: method == #{method}"
		puts "-> methodIds == #{methodIds.inspect}"
		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
	
	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 = 0
			(0...args.length).each do
				|i|
				current_match += checkarg(get_value_type(args[i]), get_arg_type_name(id, i))
			end
			
			# Note that if current_match > best_match, then chosen must be nil
			if current_match > best_match
				best_match = current_match
				chosen = id
			# Multiple matches are an error; the equality test below _cannot_ be commented out.
			# If ambiguous matches occur the problem must be fixed be adjusting the relative
			# ranking of the arg types involved in checkarg().
			elsif current_match == best_match
				if !isConstMethod(id) and isConstMethod(chosen)
					chosen = id
				elsif isConstMethod(id) == isConstMethod(chosen)
					puts "multiple methods matching, this is an error" if debug_level >= DebugLevel::Minimal
					chosen = nil
				end
			end
			puts "match => #{id.index} score: #{current_match}" if debug_level >= DebugLevel::High
		end
			
		puts "Resolved to id: #{chosen.index}" if !chosen.nil? && debug_level >= DebugLevel::High
	end

	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
	end
	setCurrentMethod(chosen) if chosen
	return nil
end

.find_class(classname) ⇒ Object



2601
2602
2603
# File 'lib/Qt/qtruby4.rb', line 2601

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

.get_qboolean(b) ⇒ Object



2779
2780
2781
# File 'lib/Qt/qtruby4.rb', line 2779

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

.get_qenum_type(e) ⇒ Object



2775
2776
2777
# File 'lib/Qt/qtruby4.rb', line 2775

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

.get_qinteger(num) ⇒ Object



2763
2764
2765
# File 'lib/Qt/qtruby4.rb', line 2763

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

.getAllParents(class_id, res) ⇒ Object



2787
2788
2789
2790
2791
2792
2793
# File 'lib/Qt/qtruby4.rb', line 2787

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



2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
# File 'lib/Qt/qtruby4.rb', line 2868

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



2463
2464
2465
# File 'lib/Qt/qtruby4.rb', line 2463

def self.idclass
	return @@idclass
end

.init_all_classesObject



2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
# File 'lib/Qt/qtruby4.rb', line 2748

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



2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
# File 'lib/Qt/qtruby4.rb', line 2488

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



2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
# File 'lib/Qt/qtruby4.rb', line 2815

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



2925
2926
2927
2928
2929
2930
2931
2932
2933
# File 'lib/Qt/qtruby4.rb', line 2925

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



2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
# File 'lib/Qt/qtruby4.rb', line 2471

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

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.



2619
2620
2621
2622
2623
2624
2625
2626
2627
# File 'lib/Qt/qtruby4.rb', line 2619

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



2783
2784
2785
# File 'lib/Qt/qtruby4.rb', line 2783

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

.set_qinteger(num, val) ⇒ Object



2767
2768
2769
# File 'lib/Qt/qtruby4.rb', line 2767

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)| …}



2913
2914
2915
2916
2917
2918
2919
2920
# File 'lib/Qt/qtruby4.rb', line 2913

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, signature),
								SLOT(signature) )
end

.single_shot_timer_connect(interval, target, block) ⇒ Object

Handles calls of the form: Qt::Timer.singleShot(500, myobj) { …}



2937
2938
2939
2940
2941
# File 'lib/Qt/qtruby4.rb', line 2937

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.



2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
# File 'lib/Qt/qtruby4.rb', line 2800

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



2608
2609
2610
2611
2612
2613
# File 'lib/Qt/qtruby4.rb', line 2608

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