KDevelop API Documentation

addattributedialog.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
00003  *
00004  *  This program is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU General Public
00006  *  License as published by the Free Software Foundation; either
00007  *  version 2 of the License, or (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  *
00019  */
00020 
00021 #include "addattributedialog.h"
00022 #include "cppsupportpart.h"
00023 #include "backgroundparser.h"
00024 #include "cppsupport_utils.h"
00025 
00026 #include <kdevpartcontroller.h>
00027 
00028 #include <codemodel.h>
00029 
00030 #include <kfiledialog.h>
00031 #include <kparts/part.h>
00032 #include <ktexteditor/editinterface.h>
00033 
00034 #include <qfileinfo.h>
00035 #include <qcombobox.h>
00036 #include <qlineedit.h>
00037 #include <qlistview.h>
00038 #include <qcheckbox.h>
00039 #include <qpushbutton.h>
00040 #include <qtoolbutton.h>
00041 #include <qtextstream.h>
00042 
00043 AddAttributeDialog::AddAttributeDialog(CppSupportPart* cppSupport, ClassDom klass,
00044                  QWidget* parent, const char* name, bool modal, WFlags fl)
00045     : AddAttributeDialogBase(parent,name, modal,fl), m_cppSupport( cppSupport ), m_klass( klass ), m_count( 0 )
00046 {
00047     access->insertStringList( QStringList() << "Public" << "Protected" << "Private" );
00048     
00049     storage->insertStringList( QStringList() << "Normal" << "Static" );
00050     
00051     returnType->setAutoCompletion( true );
00052     returnType->insertStringList( QStringList()
00053         << "void"
00054             << "char"
00055         << "wchar_t"
00056         << "bool"
00057         << "short"
00058         << "int"
00059         << "long"
00060         << "signed"
00061         << "unsigned"
00062         << "float"
00063         << "double" );
00064 
00065     returnType->insertStringList( typeNameList(m_cppSupport->codeModel()) );
00066 
00067     updateGUI();
00068     addAttribute();
00069 }
00070 
00071 AddAttributeDialog::~AddAttributeDialog()
00072 {
00073 }
00074 
00075 void AddAttributeDialog::reject()
00076 {
00077     QDialog::reject();
00078 }
00079 
00080 void AddAttributeDialog::accept()
00081 {
00082     m_cppSupport->partController()->editDocument( KURL( m_klass->fileName() ) );
00083     KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( m_cppSupport->partController()->activePart() );
00084     if( !editIface ){
00086     QDialog::accept();
00087     return;
00088     }
00089 
00090     int line, column;
00091     m_klass->getEndPosition( &line, &column );
00092 
00093     // compute the insertion point map
00094     QMap<QString, QPair<int,int> > points;
00095     QStringList accessList;
00096 
00097     const VariableList variableList = m_klass->variableList();
00098     for( VariableList::ConstIterator it=variableList.begin(); it!=variableList.end(); ++it )
00099     {
00100     int varEndLine, varEndColumn;
00101     (*it)->getEndPosition( &varEndLine, &varEndColumn );
00102     QString access = accessID( *it );
00103     QPair<int, int> varEndPoint = qMakePair( varEndLine, varEndColumn );
00104 
00105     if( !points.contains(access) || points[access] < varEndPoint ){
00106             accessList.remove( access );
00107             accessList.push_back( access ); // move 'access' at the end of the list
00108 
00109         points[ access ] = varEndPoint;
00110     }
00111     }
00112 
00113     int insertedLine = 0;
00114 
00115     accessList += newAccessList( accessList );
00116 
00117     for( QStringList::iterator it=accessList.begin(); it!=accessList.end(); ++it )
00118     {
00119     QListViewItem* item = attributes->firstChild();
00120     while( item ){
00121         QListViewItem* currentItem = item;
00122 
00123         item = item->nextSibling();
00124 
00125         if( currentItem->text(0) != *it )
00126         continue;
00127 
00128             QString access = (*it).lower();
00129 
00130             QString str = variableDeclaration( currentItem );
00131 
00132         QPair<int, int> pt;
00133             if( points.contains(*it) ) {
00134                 pt = points[ *it ];
00135             } else {
00136                 str.prepend( access + ":\n" );
00137                 points[ *it ] = qMakePair( line-1, 0 );
00138                 pt = points[ *it ]; // end of class declaration
00139             }
00140 
00141         editIface->insertText( pt.first + insertedLine + 1, 0 /*pt.second*/, str );
00142             insertedLine += str.contains( QChar('\n') );
00143     }
00144     }
00145 
00146     m_cppSupport->backgroundParser()->addFile( m_klass->fileName() );
00147 
00148     QDialog::accept();
00149 }
00150 
00151 QString AddAttributeDialog::variableDeclaration( QListViewItem* item ) const
00152 {
00153     QString str;
00154     QTextStream stream( &str, IO_WriteOnly );
00155     QString ind;
00156     ind.fill( QChar(' '), 4 );
00157 
00158     stream << ind;
00159     if( item->text(1) == "Static" )
00160         stream << "static ";
00161     stream << item->text( 2 ) << " " << item->text( 3 );
00162     stream << ";\n";
00163 
00164     return str;
00165  }
00166 
00167 
00168 void AddAttributeDialog::updateGUI()
00169 {
00170     bool enable = attributes->selectedItem() != 0;
00171 
00172     returnType->setEnabled( enable );
00173     declarator->setEnabled( enable );
00174     access->setEnabled( enable );
00175     storage->setEnabled( enable );
00176 
00177     deleteAttributeButton->setEnabled( enable );
00178 
00179     if( enable ){
00180     QListViewItem* item = attributes->selectedItem();
00181     item->setText( 0, access->currentText() );
00182     item->setText( 1, storage->currentText() );
00183     item->setText( 2, returnType->currentText() );
00184     item->setText( 3, declarator->text() );
00185     }
00186 }
00187 
00188 void AddAttributeDialog::addAttribute()
00189 {
00190     QListViewItem* item = new QListViewItem( attributes, "Protected", "Normal", "int", QString("attribute_%1").arg(++m_count) );
00191     attributes->setCurrentItem( item );
00192     attributes->setSelected( item, true );
00193 
00194     returnType->setFocus();
00195 }
00196 
00197 void AddAttributeDialog::deleteCurrentAttribute()
00198 {
00199     delete( attributes->currentItem() );
00200 }
00201 
00202 void AddAttributeDialog::currentChanged( QListViewItem* item )
00203 {
00204     if( item ){
00205         QString _access = item->text( 0 );
00206         QString _storage = item->text( 1 );
00207         QString _returnType = item->text( 2 );
00208         QString _declarator = item->text( 3 );
00209 
00210     access->setCurrentText( _access );
00211     storage->setCurrentText( _storage );
00212     returnType->setCurrentText( _returnType );
00213     declarator->setText( _declarator );
00214     }
00215 
00216     updateGUI();
00217 }
00218 
00219 QStringList AddAttributeDialog::newAccessList( const QStringList& accessList ) const
00220 {
00221     QStringList newAccessList;
00222 
00223     QListViewItem* item = attributes->firstChild();
00224     while( item ){
00225         QListViewItem* currentItem = item;
00226 
00227         item = item->nextSibling();
00228 
00229         QString access = currentItem->text( 0 );
00230         if( !(accessList.contains(access) || newAccessList.contains(access)) )
00231             newAccessList.push_back( access );
00232     }
00233 
00234     return newAccessList;
00235 }
00236 
00237 QString AddAttributeDialog::accessID( VariableDom var ) const
00238 {
00239     switch( var->access() )
00240     {
00241     case CodeModelItem::Public:
00242         return QString::fromLatin1( "Public" );
00243 
00244     case CodeModelItem::Protected:
00245         return QString::fromLatin1( "Protected" );
00246 
00247     case CodeModelItem::Private:
00248         return QString::fromLatin1( "Private" );
00249     }
00250 
00251     return QString::null;
00252 }
00253 
00254 #include "addattributedialog.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:28 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003