summaryrefslogtreecommitdiffstats
path: root/languages/cpp/cppimplementationwidget.cpp
blob: 1307e3d8cf46d8dfdaf0fcad637c87748ca186ef (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
/***************************************************************************
*   Copyright (C) 2004 by Alexander Dymo                                  *
*   adymo@kdevelop.org                                                    *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Library 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 Library 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 "cppimplementationwidget.h"

#include <tqfileinfo.h>
#include <tqtextstream.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <tqdom.h>

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdelistview.h>
#include <klineedit.h>

#include <kdevlanguagesupport.h>
#include <kdevproject.h>
#include <domutil.h>
#include <filetemplate.h>

CppImplementationWidget::CppImplementationWidget( KDevLanguageSupport *part,
        TQWidget *parent, const char *name, bool modal )
		: ImplementationWidget( part, parent, name, modal )
{}

TQStringList CppImplementationWidget::createClassFiles()
{
	TQString template_h = "#ifndef $DEFTEXT$_H\n#define $DEFTEXT$_H\n\n#include \"$BASEINCLUDE$\"\n\nclass $CLASSNAME$: public $BASECLASSNAME$ {\nTQ_OBJECT\n\npublic:\n    $CLASSNAME$(TQWidget *parent = 0, const char *name = 0);\n};\n\n#endif\n";
	TQString template_cpp = "#include \"$CLASSINCLUDE$\"\n\n$CLASSNAME$::$CLASSNAME$(TQWidget *parent, const char *name)\n    :$BASECLASSNAME$(parent, name)\n{\n}\n";
	if ( m_part->project() ->options() == KDevProject::UsesAutotoolsBuildSystem )
		template_cpp += "\n#include \"$MOCINCLUDE$\"\n";

	TQFileInfo formInfo( m_formName );
	template_h.replace( TQRegExp( "\\$BASEINCLUDE\\$" ), formInfo.baseName() + ".h" );
	template_h.replace( TQRegExp( "\\$CLASSNAME\\$" ), classNameEdit->text() );
	template_h.replace( TQRegExp( "\\$BASECLASSNAME\\$" ), m_baseClassName );
	template_h.replace( TQRegExp( "\\$DEFTEXT\\$" ), fileNameEdit->text().upper() );

	template_cpp.replace( TQRegExp( "\\$CLASSINCLUDE\\$" ), fileNameEdit->text() + ".h" );
	template_cpp.replace( TQRegExp( "\\$CLASSNAME\\$" ), classNameEdit->text() );
	template_cpp.replace( TQRegExp( "\\$BASECLASSNAME\\$" ), m_baseClassName );
	template_cpp.replace( TQRegExp( "\\$MOCINCLUDE\\$" ), fileNameEdit->text() + ".moc" );

	template_h = FileTemplate::read( m_part, "h" ) + template_h;
	template_cpp = FileTemplate::read( m_part, "cpp" ) + template_cpp;

	TQString file_h = fileNameEdit->text() + ".h";
	TQString file_cpp = fileNameEdit->text() + ".cpp";
	if ( !m_part->project() ->activeDirectory().isEmpty() )
	{
		file_h = m_part->project() ->activeDirectory() + "/" + file_h;
		file_cpp = m_part->project() ->activeDirectory() + "/" + file_cpp;
	}

	TQFile ifile( TQDir::cleanDirPath( m_part->project() ->projectDirectory() + "/" + file_cpp ) );
	if ( !ifile.open( IO_WriteOnly ) )
	{
		KMessageBox::error( this, i18n( "Cannot write to implementation file" ) );
		return TQStringList();
	}
	TQTextStream istream( &ifile );
	istream << template_cpp;
	ifile.close();

	TQFile hfile( TQDir::cleanDirPath( m_part->project() ->projectDirectory() + "/" + file_h ) );
	if ( !hfile.open( IO_WriteOnly ) )
	{
		KMessageBox::error( this, i18n( "Cannot write to header file" ) );
		return TQStringList();
	}
	TQTextStream hstream( &hfile );
	hstream << template_h;
	hfile.close();

	TQStringList fileList;
	fileList.append( file_h );
	fileList.append( file_cpp );

	return fileList;
}

#include "cppimplementationwidget.moc"