summaryrefslogtreecommitdiffstats
path: root/languages/cpp/creategettersetterdialog.cpp
blob: e63fcf235d546092256f9c855a63585033a76201 (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
//
// C++ Implementation: %{MODULE}
//
// Description:
//
//
// Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "creategettersetterdialog.h"

#include "cppsupportpart.h"
#include <klineedit.h>
#include <tqcheckbox.h>
#include <tqregexp.h>

#include "creategettersetterconfiguration.h"

CreateGetterSetterDialog::CreateGetterSetterDialog( CppSupportPart* part, ClassDom aClass,
	                                                VariableDom aVar, TQWidget *parent, const char *pName )
: CreateGetterSetterDialogBase( parent, pName ), m_part( part ), m_class( aClass ), m_var( aVar )
{
	TQString name = aVar->name();
	setCaption( "Create methods for " + name );

	if ( aVar->type().startsWith( "const" ) && !aVar->type().endsWith( "*" ) )
	{
		m_chkSet->setChecked( false );
		m_chkSet->setEnabled( false );
	}

	CreateGetterSetterConfiguration* config = m_part->createGetterSetterConfiguration();
	if ( config == 0 )
		return ;

	TQStringList prefixes = config->prefixVariable();
	unsigned int len = 0;

	TQStringList::ConstIterator theend = prefixes.end(); //find longest fitting prefix and remove it
	for ( TQStringList::ConstIterator ci = prefixes.begin(); ci != theend; ++ci )
	{
		if ( name.startsWith( *ci ) && ( *ci ).length() > len )
			len = ( *ci ).length();
	}

	if ( len > 0 )
		name.remove( 0, len );

	m_edtGet->setText( name );

	TQString getName = name;
	if ( ! config->prefixGet().isEmpty() )
		getName[ 0 ] = getName[ 0 ].upper();

	TQString setName = name;
	if ( ! config->prefixSet().isEmpty() )
		setName[ 0 ] = setName[ 0 ].upper();

	bool getIsChecked = config->isInlineGet();
	bool setIsChecked = config->isInlineSet();
	m_chkInlineGet->setChecked( getIsChecked );
	m_chkInlineSet->setChecked( setIsChecked );
	
	m_edtGet->setText( config->prefixGet() + getName );
	m_edtSet->setText( config->prefixSet() + setName );
}

void CreateGetterSetterDialog::accept( )
{
	CreateGetterSetterConfiguration * config = m_part->createGetterSetterConfiguration();
	
	if ( config == 0 )
		return ;
	
	if ( m_chkGet->isChecked() && !m_edtGet->text().isEmpty() )
		m_part->addMethod( m_class, m_edtGet->text(), m_var->type(), "", 
		                   CodeModelItem::Public, true, m_chkInlineGet->isChecked(),
		                   false, false, "\treturn " + m_var->name() + ";" );
	
	if ( m_chkSet->isChecked() && !m_edtSet->text().isEmpty() )
	{
		TQString parameterStr;
		
		if ( m_var->type().endsWith( "*" ) )
		{
			parameterStr = m_var->type() + " " + config->parameterName();
		}
		else
		{
			TQRegExp basicTypes( "((unsigned)?\\s*(char|byte|short|int|long))|double|float|bool" );
			if ( basicTypes.exactMatch( m_var->type() ) )
				parameterStr = m_var->type() + " " + config->parameterName();
			else
				parameterStr = "const " + m_var->type() + "& " + config->parameterName();
		}
		m_part->addMethod( m_class, m_edtSet->text(), "void", parameterStr, CodeModelItem::Public,
		                   false, m_chkInlineSet->isChecked(), false, false,
		                   "\t" + m_var->name() + " = " + config->parameterName() + ";" );
	}
	//@todo illegale eingaben nicht akzeptieren wie z.b. int& ...
	TQDialog::accept();
}

/**
 * store current settings wether get/set methods should be created inline.
 * this is done everytime one changes this behaviour in the dialog.
 */
void CreateGetterSetterDialog::slotInlineChanged( )
{
	CreateGetterSetterConfiguration * config = m_part->createGetterSetterConfiguration();
	if ( config == 0 )
		return ;

	config->setInlineGet( m_chkInlineGet->isChecked() );
	config->setInlineSet( m_chkInlineSet->isChecked() );
	config->store();
}

#include "creategettersetterdialog.moc"
//kate: indent-mode csands; tab-width 4; space-indent off;