summaryrefslogtreecommitdiffstats
path: root/languages/cpp/tqtdesignercppintegration.cpp
blob: 6132f75813d1e66bc1b2494909d2ff66a5b61f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/***************************************************************************
*   Copyright (C) 2004 by Alexander Dymo                                  *
*   [email protected]                                                       *
*   Portions Copyright (C) 2003 Roberto Raggi ([email protected])      *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
***************************************************************************/
#include "tqtdesignercppintegration.h"

#include <tqpair.h>
#include <tqregexp.h>

#include <tdelocale.h>
#include <kdebug.h>
#include <tdemessagebox.h>
#include <kurl.h>
#include <tdetexteditor/editinterface.h>
#include <tdetexteditor/view.h>
#include <tdetexteditor/viewcursorinterface.h>

#include <domutil.h>
#include <kdevpartcontroller.h>
#include <kdevcreatefile.h>

#include "backgroundparser.h"
#include "cppsupportpart.h"
#include "codemodel_utils.h"
#include "implementationwidget.h"

QtDesignerCppIntegration::QtDesignerCppIntegration( KDevLanguageSupport *part,
	                                                ImplementationWidget *impl )
: QtDesignerIntegration( part, impl, true, 0 )
{}

void QtDesignerCppIntegration::addFunctionToClass( KInterfaceDesigner::Function function, ClassDom klass )
{
	m_part->partController() ->editDocument( KURL( klass->fileName() ) );
	KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( m_part->partController() ->activePart() );
	if ( !editIface )
	{
		/// @todo show messagebox
		//        TQDialog::accept();
		return ;
	}

	int line, column;
	klass->getEndPosition( &line, &column );

	// compute the insertion point map
	TQMap<TQString, TQPair<int, int> > points;

	const FunctionList functionList = klass->functionList();
	for ( FunctionList::ConstIterator it = functionList.begin(); it != functionList.end(); ++it )
	{
		int funEndLine, funEndColumn;
		( *it ) ->getEndPosition( &funEndLine, &funEndColumn );
		TQString access = accessID( *it );
		TQPair<int, int> funEndPoint = tqMakePair( funEndLine, funEndColumn );

		if ( !points.contains( access ) || points[ access ] < funEndPoint )
		{
			points[ access ] = funEndPoint;
		}
	}

	int insertedLine = 0;

	TQString access = function.access + ( function.type == KInterfaceDesigner::ftTQtSlot ? " slots" : "" );

	TQString str = function.returnType + " " + function.function;
	if ( function.specifier == "virtual" )
		str = "virtual " + str;
	else if ( function.specifier == "pure virtual" )
		str = "virtual " + str + " = 0";
	else if ( function.specifier == "static" )
		str = "static " + str;
	str += ";\n";
	str = "    " + str;

	TQPair<int, int> pt;
	if ( points.contains( access ) )
	{
		pt = points[ access ];
	}
	else
	{
		str.prepend( access + ":\n" );
		points[ access ] = tqMakePair( line - 1, 0 );
		pt = points[ access ]; // end of class declaration
	}

	editIface->insertText( pt.first + insertedLine + 1, 0 /*pt.second*/, str );
	insertedLine += str.contains( TQChar( '\n' ) );

	CppSupportPart *cppPart = dynamic_cast<CppSupportPart *>( m_part );
	cppPart->backgroundParser() ->addFile( klass->fileName() );

	if ( function.specifier == "pure virtual" )
		return ;


	//implementation
	TQString stri = function.returnType + " " + klass->name() + "::" + function.function;
	if ( function.specifier == "static" )
		stri = "static " + stri;
	stri += "\n{\n}\n";
	stri = "\n" + stri;

	TQFileInfo fi( klass->fileName() );
	TQString implementationFile = fi.absFilePath();
	implementationFile.replace( ".h", ".cpp" );
	
	TQFileInfo fileInfo( implementationFile );
	if ( !TQFile::exists( fileInfo.absFilePath() ) )
	{
		if ( KDevCreateFile * createFileSupp = m_part->extension<KDevCreateFile>( "TDevelop/CreateFile" ) )
			createFileSupp->createNewFile( fileInfo.extension(), fileInfo.dirPath( true ), fileInfo.fileName() );
	}
	
	m_part->partController() ->editDocument( KURL( implementationFile ) );
	editIface = dynamic_cast<KTextEditor::EditInterface*>( m_part->partController() ->activePart() );
	if ( !editIface )
		return ;
	
	int atLine = 0, atColumn = 0;
	TranslationUnitAST *translationUnit = 0;
	ParsedFilePointer p = cppPart->backgroundParser() ->translationUnit( implementationFile );
	if( p ) translationUnit = *p;
	if ( translationUnit )
	{
		translationUnit->getEndPosition( &atLine, &atColumn );
		kdDebug() << "atLine: " << atLine << endl;
		stri = "\n" + stri;
	}
	else
	{
		atLine = editIface->numLines();
		line = editIface->numLines();
		while ( line > 0 )
		{
			if ( editIface->textLine( line ).isEmpty() )
			{
				--line;
				continue;
			}
			else
			{
				if ( editIface->textLine( line ).contains( TQRegExp( ".*#include .*\\.moc.*" ) ) )
					atLine = line;
				break;
			}
		}
	kdDebug() << "atLine (2): " << atLine << endl;
		atColumn = 0;
	}
	
	//    editIface->insertLine( atLine + 1, TQString::fromLatin1("") );
	kdDebug() << "at line in intg: " << atLine << " atCol: " << atColumn << endl;
	kdDebug() << "text: " << stri << endl;
	editIface->insertText( atLine, atColumn, stri );
	KTextEditor::View *activeView = dynamic_cast<KTextEditor::View*>( m_part->partController() ->activePart() ->widget() );
	if ( activeView )
	{
		KTextEditor::ViewCursorInterface * cursor = dynamic_cast<KTextEditor::ViewCursorInterface*>( activeView );
		if ( cursor )
			cursor->setCursorPositionReal( atLine + 3, 1 );
	}
	
	cppPart->backgroundParser() ->addFile( implementationFile );
}

TQString QtDesignerCppIntegration::accessID( FunctionDom fun ) const
{
	if ( fun->isSignal() )
		return TQString::fromLatin1( "signals" );

	switch ( fun->access() )
	{
	case CodeModelItem::Public:
		if ( fun->isSlot() )
			return TQString::fromLatin1( "public slots" );
		return TQString::fromLatin1( "public" );

	case CodeModelItem::Protected:
		if ( fun->isSlot() )
			return TQString::fromLatin1( "protected slots" );
		return TQString::fromLatin1( "protected" );

	case CodeModelItem::Private:
		if ( fun->isSlot() )
			return TQString::fromLatin1( "private slots" );
		return TQString::fromLatin1( "private" );
	}

	return TQString();
}

void QtDesignerCppIntegration::processImplementationName( TQString &name )
{
	name.replace( ".h", ".cpp" );
}

#include "tqtdesignercppintegration.moc"