source: trunk/tools/designer/plugins/cppeditor/cppcompletion.cpp

Last change on this file was 197, checked in by rudi, 14 years ago

Added QtDesigner

File size: 5.4 KB
Line 
1/**********************************************************************
2**
3** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
4**
5** This file is part of Qt Designer.
6**
7** This file may be distributed and/or modified under the terms of the
8** GNU General Public License version 2 as published by the Free Software
9** Foundation and appearing in the file LICENSE.GPL included in the
10** packaging of this file.
11**
12** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
13** licenses may use this file in accordance with the Qt Commercial License
14** Agreement provided with the Software.
15**
16** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18**
19** See http://www.trolltech.com/gpl/ for GPL licensing information.
20** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
21** information about Qt Commercial License Agreements.
22**
23** Contact info@trolltech.com if any conditions of this licensing are
24** not clear to you.
25**
26**********************************************************************/
27
28#include "cppcompletion.h"
29#include <qobject.h>
30#include <qmetaobject.h>
31#include <qobjectlist.h>
32#include <qregexp.h>
33
34CppEditorCompletion::CppEditorCompletion( Editor *e )
35 : EditorCompletion( e )
36{
37}
38
39bool CppEditorCompletion::doObjectCompletion( const QString &objName )
40{
41 if ( !ths )
42 return FALSE;
43 QString object( objName );
44 int i = -1;
45 if ( ( i = object.findRev( "->" ) ) != -1 )
46 object = object.mid( i + 2 );
47 if ( ( i = object.findRev( "." ) ) != -1 )
48 object = object.mid( i + 1 );
49 object = object.simplifyWhiteSpace();
50 QObject *obj = 0;
51 if ( ths->name() == object || object == "this" ) {
52 obj = ths;
53 } else {
54 obj = ths->child( object );
55 }
56
57 if ( !obj )
58 return FALSE;
59
60 QValueList<CompletionEntry> lst;
61
62 if ( obj->children() ) {
63 for ( QObjectListIt cit( *obj->children() ); cit.current(); ++cit ) {
64 QString s( cit.current()->name() );
65 if ( s.find( " " ) == -1 && s.find( "qt_" ) == -1 && s.find( "unnamed" ) == -1 ) {
66 CompletionEntry c;
67 c.type = "variable";
68 c.text = s;
69 c.prefix = "";
70 lst << c;
71 }
72 }
73 }
74
75 QStrList props = obj->metaObject()->propertyNames( TRUE );
76 for ( QPtrListIterator<char> pit( props ); pit.current(); ++pit ) {
77 QString f( pit.current() );
78 QChar c = f[ 0 ];
79 f.remove( (uint)0, 1 );
80 f.prepend( c.upper() );
81 f.prepend( "set" );
82
83 CompletionEntry ce;
84 ce.type = "property";
85 ce.text = f;
86 ce.postfix = "()";
87
88 if ( lst.find( ce ) == lst.end() )
89 lst << ce;
90 }
91
92 QStrList slts = obj->metaObject()->slotNames( TRUE );
93 for ( QPtrListIterator<char> sit( slts ); sit.current(); ++sit ) {
94 QString f( sit.current() );
95 f = f.left( f.find( "(" ) );
96 CompletionEntry c;
97 c.type = "slot";
98 c.text = f;
99 c.postfix = "()";
100 if ( lst.find( c ) == lst.end() )
101 lst << c;
102 }
103
104 if ( lst.isEmpty() )
105 return FALSE;
106
107 showCompletion( lst );
108 return TRUE;
109}
110
111QValueList<QStringList> CppEditorCompletion::functionParameters( const QString &expr, QChar &separator,
112 QString &prefix, QString &postfix )
113{
114 Q_UNUSED( prefix );
115 Q_UNUSED( postfix );
116 separator = ',';
117 if ( !ths )
118 return QValueList<QStringList>();
119 QString func;
120 QString objName;
121 int i = -1;
122
123 i = expr.findRev( "->" );
124 if ( i == -1 )
125 i = expr.findRev( "." );
126 else
127 ++i;
128 if ( i == -1 ) {
129 i = expr.findRev( " " );
130
131 if ( i == -1 )
132 i = expr.findRev( "\t" );
133 else
134 objName = ths->name();
135
136 if ( i == -1 && expr[ 0 ] != ' ' && expr[ 0 ] != '\t' )
137 objName = ths->name();
138 }
139
140 if ( !objName.isEmpty() ) {
141 func = expr.mid( i + 1 );
142 func = func.simplifyWhiteSpace();
143 } else {
144 func = expr.mid( i + 1 );
145 func = func.simplifyWhiteSpace();
146 QString ex( expr );
147 ex.remove( i, 0xFFFFFF );
148 if ( ex[ (int)ex.length() - 1 ] == '-' )
149 ex.remove( ex.length() - 1, 1 );
150 int j = -1;
151 j = ex.findRev( "->" );
152 if ( j == -1 )
153 j = ex.findRev( "." );
154 else
155 ++j;
156 if ( j == -1 ) {
157 j = ex.findRev( " " );
158
159 if ( j == -1 )
160 j = ex.findRev( "\t" );
161 else
162 objName = ths->name();
163
164 if ( j == -1 )
165 objName = ths->name();
166 }
167 objName = ex.mid( j + 1 );
168 objName = objName.simplifyWhiteSpace();
169 }
170
171 QObject *obj = 0;
172 if ( ths->name() == objName || objName == "this" ) {
173 obj = ths;
174 } else {
175 obj = ths->child( objName );
176 }
177
178 if ( !obj )
179 return QValueList<QStringList>();
180
181 QStrList slts = obj->metaObject()->slotNames( TRUE );
182 for ( QPtrListIterator<char> sit( slts ); sit.current(); ++sit ) {
183 QString f( sit.current() );
184 f = f.left( f.find( "(" ) );
185 if ( f == func ) {
186 f = QString( sit.current() );
187 f.remove( (uint)0, f.find( "(" ) + 1 );
188 f = f.left( f.find( ")" ) );
189 QStringList lst = QStringList::split( ',', f );
190 if ( !lst.isEmpty() ) {
191 QValueList<QStringList> l;
192 l << lst;
193 return l;
194 }
195 }
196 }
197
198 const QMetaProperty *prop =
199 obj->metaObject()->
200 property( obj->metaObject()->findProperty( func[ 3 ].lower() + func.mid( 4 ), TRUE ), TRUE );
201 if ( prop ) {
202 QValueList<QStringList> l;
203 l << QStringList( prop->type() );
204 return l;
205 }
206
207 return QValueList<QStringList>();
208}
209
210void CppEditorCompletion::setContext( QObject *this_ )
211{
212 ths = this_;
213}
Note: See TracBrowser for help on using the repository browser.