2
* Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
21
#include "addattributedialog.h"
22
#include "cppsupportpart.h"
23
#include "backgroundparser.h"
24
#include "cppsupport_utils.h"
26
#include <kdevpartcontroller.h>
28
#include <codemodel.h>
30
#include <kfiledialog.h>
31
#include <kparts/part.h>
32
#include <ktexteditor/editinterface.h>
33
#include <klineedit.h>
35
#include <qfileinfo.h>
36
#include <qcombobox.h>
37
#include <qlistview.h>
38
#include <qcheckbox.h>
39
#include <qpushbutton.h>
40
#include <qtoolbutton.h>
41
#include <qtextstream.h>
43
AddAttributeDialog::AddAttributeDialog( CppSupportPart* cppSupport, ClassDom klass,
44
QWidget* parent, const char* name, bool modal, WFlags fl )
45
: AddAttributeDialogBase( parent, name, modal, fl ), m_cppSupport( cppSupport ), m_klass( klass ), m_count( 0 )
47
access->insertStringList( QStringList() << "Public" << "Protected" << "Private" );
49
storage->insertStringList( QStringList() << "Normal" << "Static" );
51
returnType->setAutoCompletion( true );
52
returnType->insertStringList( QStringList()
65
returnType->insertStringList( typeNameList( m_cppSupport->codeModel() ) );
71
AddAttributeDialog::~AddAttributeDialog()
74
void AddAttributeDialog::reject()
79
void AddAttributeDialog::accept()
81
m_cppSupport->partController()->editDocument( KURL( m_klass->fileName() ) );
82
KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( m_cppSupport->partController() ->activePart() );
85
/// @todo show messagebox
91
m_klass->getEndPosition( &line, &column );
93
// compute the insertion point map
94
QMap<QString, QPair<int, int> > points;
95
QStringList accessList;
97
const VariableList variableList = m_klass->variableList();
98
for ( VariableList::ConstIterator it = variableList.begin(); it != variableList.end(); ++it )
100
int varEndLine, varEndColumn;
101
( *it ) ->getEndPosition( &varEndLine, &varEndColumn );
102
QString access = accessID( *it );
103
QPair<int, int> varEndPoint = qMakePair( varEndLine, varEndColumn );
105
if ( !points.contains( access ) || points[ access ] < varEndPoint )
107
accessList.remove( access );
108
accessList.push_back( access ); // move 'access' at the end of the list
110
points[ access ] = varEndPoint;
114
int insertedLine = 0;
116
accessList += newAccessList( accessList );
118
for ( QStringList::iterator it = accessList.begin(); it != accessList.end(); ++it )
120
QListViewItem* item = attributes->firstChild();
123
QListViewItem * currentItem = item;
125
item = item->nextSibling();
127
if ( currentItem->text( 0 ) != *it )
130
QString access = ( *it ).lower();
132
QString str = variableDeclaration( currentItem );
135
if ( points.contains( *it ) )
141
str.prepend( access + ":\n" );
142
points[ *it ] = qMakePair( line - 1, 0 );
143
pt = points[ *it ]; // end of class declaration
146
editIface->insertText( pt.first + insertedLine + 1, 0 /*pt.second*/, str );
147
insertedLine += str.contains( QChar( '\n' ) );
151
m_cppSupport->backgroundParser() ->addFile( m_klass->fileName() );
156
QString AddAttributeDialog::variableDeclaration( QListViewItem* item ) const
159
QTextStream stream( &str, IO_WriteOnly );
161
ind.fill( QChar( ' ' ), 4 );
164
if ( item->text( 1 ) == "Static" )
166
stream << item->text( 2 ) << " " << item->text( 3 );
173
void AddAttributeDialog::updateGUI()
175
bool enable = attributes->selectedItem() != 0;
177
returnType->setEnabled( enable );
178
declarator->setEnabled( enable );
179
access->setEnabled( enable );
180
storage->setEnabled( enable );
182
deleteAttributeButton->setEnabled( enable );
186
QListViewItem * item = attributes->selectedItem();
187
item->setText( 0, access->currentText() );
188
item->setText( 1, storage->currentText() );
189
item->setText( 2, returnType->currentText() );
190
item->setText( 3, declarator->text() );
194
void AddAttributeDialog::addAttribute()
196
QListViewItem * item = new QListViewItem( attributes, "Protected", "Normal",
197
"int", QString( "attribute_%1" ).arg( ++m_count ) );
198
attributes->setCurrentItem( item );
199
attributes->setSelected( item, true );
201
returnType->setFocus();
204
void AddAttributeDialog::deleteCurrentAttribute()
206
delete( attributes->currentItem() );
209
void AddAttributeDialog::currentChanged( QListViewItem* item )
213
QString _access = item->text( 0 );
214
QString _storage = item->text( 1 );
215
QString _returnType = item->text( 2 );
216
QString _declarator = item->text( 3 );
218
access->setCurrentText( _access );
219
storage->setCurrentText( _storage );
220
returnType->setCurrentText( _returnType );
221
declarator->setText( _declarator );
227
QStringList AddAttributeDialog::newAccessList( const QStringList& accessList ) const
229
QStringList newAccessList;
231
QListViewItem* item = attributes->firstChild();
234
QListViewItem * currentItem = item;
236
item = item->nextSibling();
238
QString access = currentItem->text( 0 );
239
if ( !( accessList.contains( access ) || newAccessList.contains( access ) ) )
240
newAccessList.push_back( access );
243
return newAccessList;
246
QString AddAttributeDialog::accessID( VariableDom var ) const
248
switch ( var->access() )
250
case CodeModelItem::Public:
251
return QString::fromLatin1( "Public" );
253
case CodeModelItem::Protected:
254
return QString::fromLatin1( "Protected" );
256
case CodeModelItem::Private:
257
return QString::fromLatin1( "Private" );
260
return QString::null;
263
#include "addattributedialog.moc"
264
//kate: indent-mode csands; tab-width 4; space-indent off;