* A problem has been found with QtRuby when it is run with Ruby 1.9.1
[kdebindings.git] / ruby / qtruby / bin / rbqtapi
blob3a1434041497348db027890f8a23ce128b03afe9
1 #!/usr/bin/env ruby
3 # Note: this program is part of qtruby and makes use of its internal functions.
4 # You should not rely on those in your own programs.
6 require 'Qt4'
7 require 'getoptlong'
9 $usage = <<USAGE
10 rbqtapi - a qtruby introspection tool
12 usage: rbqtapi [-h] [-v] [-p] [-m <re> [-i]] [-r <extension> ...] [<class>]
14 options:
15 -m <re> : find all functions matching regular expression/keyword <re>
16 -i : together with -m, performs a case insensitive search
17 -p : also display inherited methods for <class>
18 -s | show method names with ruby types
19 -r : require a qtruby extension
20 -v : print qtruby and Qt versions
21 -h : print this help message
22 USAGE
24 pattern = helpOpt = matchOpt = parentsOpt = showOpt = versionOpt = insensitiveOpt = nil
26 opts = GetoptLong.new(
27 [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
28 [ "--match", "-m", GetoptLong::REQUIRED_ARGUMENT ],
29 [ "--parents", "-p", GetoptLong::NO_ARGUMENT ],
30 [ "--show", "-s", GetoptLong::NO_ARGUMENT ],
31 [ "--version", "-v", GetoptLong::NO_ARGUMENT ],
32 [ "--insensitive", "-i", GetoptLong::NO_ARGUMENT ],
33 [ "--require", "-r", GetoptLong::REQUIRED_ARGUMENT ]
36 opts.each do |opt, arg|
37 case opt
38 when '--parents'
39 parentsOpt = true
40 when '--insensitive'
41 insensitiveOpt = true
42 when '--match'
43 matchOpt = arg
44 when '--show'
45 showOpt = arg
46 when '--version'
47 versionOpt = true
48 when '--help'
49 helpOpt = true
50 when '--require'
51 require(arg)
52 end
53 end
55 if versionOpt
56 print "QtRuby #{Qt::qtruby_version} using Qt-#{Qt::version}\n"
57 exit 0
58 elsif helpOpt
59 print $usage
60 exit 0
61 end
63 if matchOpt
64 pattern = insensitiveOpt ? Regexp.new(matchOpt, Regexp::IGNORECASE) : Regexp.new(matchOpt)
65 end
67 classnames = []
68 if ARGV.length == 0
69 classnames = Qt::Internal.classes.keys
70 else
71 ARGV.each do |classname|
72 classid = Qt::Internal::find_pclassid(classname)
73 if !classid.index
74 puts "ERROR: class '#{classname}' not found"
75 exit 1
76 end
77 classnames << classname
78 end
79 end
81 candidates = ""
83 classnames.each do |classname|
84 classid = Qt::Internal::find_pclassid(classname)
85 if !classid.index
86 next
87 end
89 a = Qt::Internal.findAllMethods(classid)
90 ids = (a.keys.sort.map{|k|a[k]}).flatten
91 candidates << Qt::Internal::dumpCandidates(ids)
93 if parentsOpt
94 superclasses = []
95 Qt::Internal::getAllParents(classid, superclasses)
96 superclasses.each do |klass|
97 a = Qt::Internal::findAllMethods(klass)
98 ids = (a.keys.sort.map{|k|a[k]}).flatten
99 candidates << Qt::Internal::dumpCandidates(ids)
104 class String
105 def to_ruby_method(show)
106 if !show
107 return self
110 method_name = self.clone
112 if method_name =~ /([:A-Za-z0-9]+)\*.*[ :]+([A-Za-z0-9]+)::([A-Za-z0-9]+)(\(.*\))/ && $2 == $3
113 method_name = $1 + '#new' + $4 + "\n"
116 method_name.gsub!(/char*/, 'String')
117 method_name.gsub!(/[*&]/, '')
118 method_name.gsub!(/KSharedPtr<([^>]*)>/) { $1 }
119 method_name.gsub!(/QPair<([^>]*)>/) { '[' + $1 + ']' }
120 method_name.gsub!(/(static|void|const) /, '')
121 method_name.gsub!(/operator/, '')
122 method_name.gsub!(/ const/, '')
123 method_name.gsub!(/enum /, '')
124 method_name.gsub!(/QStringList/, '[String, ...]')
125 method_name.gsub!(/QString/, 'String')
126 method_name.gsub!(/bool/, 'Boolean')
127 method_name.gsub!(/mode_t|time_t|long/, 'Integer')
128 method_name.gsub!(/float|double|qreal/, 'Float')
129 method_name.gsub!(/q?u?longlong/, 'Integer')
130 method_name.gsub!(/^q?u?(int|long|short)[0-9]?[0-9]?/, 'Integer')
131 method_name.gsub!(/([^A-Za-z])q?u?(int|long|short)[0-9]?[0-9]?([,)])/) { $1 + 'Integer' + $3 }
132 method_name.gsub!(/Q(List|Vector)<([^>]+)>/) { '[' + $2 + ', ...]' }
133 method_name.gsub!(/Q(Hash|Map)<([^>]+),([^>]+)>/) { '{' + $2 + ' => ' + $3 + ', ...}' }
134 method_name.gsub!(/::setRange\(([a-zA-Z]+), ([a-zA-Z]+)\)/) { '::range=' + $1 + '..' + $2 }
135 method_name.gsub!(/::(is|has)([A-Z])([\w]+)\(/) { '::' + $2.downcase + $3 + "?" + "(" }
136 method_name.gsub!(/::set([A-Z])([a-zA-Z]+)(\([^,]*\))/) { '::' + $1.downcase + $2 + '=' + $3 }
137 method_name.sub!(/::([a-z])/) { '#' + $1 }
138 method_name.gsub!(/Q([A-Z])/) { 'Qt::' + $1 }
139 method_name.sub!('()', '')
140 return method_name
144 candidates.gsub!("\t", "")
145 candidates.each do |candidate|
146 if candidate =~ /~/ || candidate =~ /qt_metacall/ || candidate =~ /staticMetaObject/
147 next
150 print candidate.to_ruby_method(showOpt) if pattern.nil? || pattern =~ candidate
153 # kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;