~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/addattributedialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*  Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
3
 
*
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.
8
 
*
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.
13
 
*
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.
18
 
*
19
 
*/
20
 
 
21
 
#include "addattributedialog.h"
22
 
#include "cppsupportpart.h"
23
 
#include "backgroundparser.h"
24
 
#include "cppsupport_utils.h"
25
 
 
26
 
#include <kdevpartcontroller.h>
27
 
 
28
 
#include <codemodel.h>
29
 
 
30
 
#include <kfiledialog.h>
31
 
#include <kparts/part.h>
32
 
#include <ktexteditor/editinterface.h>
33
 
#include <klineedit.h>
34
 
 
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>
42
 
 
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 )
46
 
{
47
 
        access->insertStringList( QStringList() << "Public" << "Protected" << "Private" );
48
 
        
49
 
        storage->insertStringList( QStringList() << "Normal" << "Static" );
50
 
        
51
 
        returnType->setAutoCompletion( true );
52
 
        returnType->insertStringList( QStringList()
53
 
                                      << "void"
54
 
                                      << "char"
55
 
                                      << "wchar_t"
56
 
                                      << "bool"
57
 
                                      << "short"
58
 
                                      << "int"
59
 
                                      << "long"
60
 
                                      << "signed"
61
 
                                      << "unsigned"
62
 
                                      << "float"
63
 
                                      << "double" );
64
 
        
65
 
        returnType->insertStringList( typeNameList( m_cppSupport->codeModel() ) );
66
 
        
67
 
        updateGUI();
68
 
        addAttribute();
69
 
}
70
 
 
71
 
AddAttributeDialog::~AddAttributeDialog()
72
 
{}
73
 
 
74
 
void AddAttributeDialog::reject()
75
 
{
76
 
        QDialog::reject();
77
 
}
78
 
 
79
 
void AddAttributeDialog::accept()
80
 
{
81
 
        m_cppSupport->partController()->editDocument( KURL( m_klass->fileName() ) );
82
 
        KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( m_cppSupport->partController() ->activePart() );
83
 
        if ( !editIface )
84
 
        {
85
 
                /// @todo show messagebox
86
 
                QDialog::accept();
87
 
                return ;
88
 
        }
89
 
        
90
 
        int line, column;
91
 
        m_klass->getEndPosition( &line, &column );
92
 
        
93
 
        // compute the insertion point map
94
 
        QMap<QString, QPair<int, int> > points;
95
 
        QStringList accessList;
96
 
        
97
 
        const VariableList variableList = m_klass->variableList();
98
 
        for ( VariableList::ConstIterator it = variableList.begin(); it != variableList.end(); ++it )
99
 
        {
100
 
                int varEndLine, varEndColumn;
101
 
                ( *it ) ->getEndPosition( &varEndLine, &varEndColumn );
102
 
                QString access = accessID( *it );
103
 
                QPair<int, int> varEndPoint = qMakePair( varEndLine, varEndColumn );
104
 
                
105
 
                if ( !points.contains( access ) || points[ access ] < varEndPoint )
106
 
                {
107
 
                        accessList.remove( access );
108
 
                        accessList.push_back( access ); // move 'access' at the end of the list
109
 
                        
110
 
                        points[ access ] = varEndPoint;
111
 
                }
112
 
        }
113
 
        
114
 
        int insertedLine = 0;
115
 
        
116
 
        accessList += newAccessList( accessList );
117
 
        
118
 
        for ( QStringList::iterator it = accessList.begin(); it != accessList.end(); ++it )
119
 
        {
120
 
                QListViewItem* item = attributes->firstChild();
121
 
                while ( item )
122
 
                {
123
 
                        QListViewItem * currentItem = item;
124
 
                        
125
 
                        item = item->nextSibling();
126
 
                        
127
 
                        if ( currentItem->text( 0 ) != *it )
128
 
                                continue;
129
 
                        
130
 
                        QString access = ( *it ).lower();
131
 
                        
132
 
                        QString str = variableDeclaration( currentItem );
133
 
                        
134
 
                        QPair<int, int> pt;
135
 
                        if ( points.contains( *it ) )
136
 
                        {
137
 
                                pt = points[ *it ];
138
 
                        }
139
 
                        else
140
 
                        {
141
 
                                str.prepend( access + ":\n" );
142
 
                                points[ *it ] = qMakePair( line - 1, 0 );
143
 
                                pt = points[ *it ]; // end of class declaration
144
 
                        }
145
 
                        
146
 
                        editIface->insertText( pt.first + insertedLine + 1, 0 /*pt.second*/, str );
147
 
                        insertedLine += str.contains( QChar( '\n' ) );
148
 
                }
149
 
        }
150
 
        
151
 
        m_cppSupport->backgroundParser() ->addFile( m_klass->fileName() );
152
 
        
153
 
        QDialog::accept();
154
 
}
155
 
 
156
 
QString AddAttributeDialog::variableDeclaration( QListViewItem* item ) const
157
 
{
158
 
        QString str;
159
 
        QTextStream stream( &str, IO_WriteOnly );
160
 
        QString ind;
161
 
        ind.fill( QChar( ' ' ), 4 );
162
 
        
163
 
        stream << ind;
164
 
        if ( item->text( 1 ) == "Static" )
165
 
                stream << "static ";
166
 
        stream << item->text( 2 ) << " " << item->text( 3 );
167
 
        stream << ";\n";
168
 
        
169
 
        return str;
170
 
}
171
 
 
172
 
 
173
 
void AddAttributeDialog::updateGUI()
174
 
{
175
 
        bool enable = attributes->selectedItem() != 0;
176
 
        
177
 
        returnType->setEnabled( enable );
178
 
        declarator->setEnabled( enable );
179
 
        access->setEnabled( enable );
180
 
        storage->setEnabled( enable );
181
 
        
182
 
        deleteAttributeButton->setEnabled( enable );
183
 
        
184
 
        if ( enable )
185
 
        {
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() );
191
 
        }
192
 
}
193
 
 
194
 
void AddAttributeDialog::addAttribute()
195
 
{
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 );
200
 
        
201
 
        returnType->setFocus();
202
 
}
203
 
 
204
 
void AddAttributeDialog::deleteCurrentAttribute()
205
 
{
206
 
        delete( attributes->currentItem() );
207
 
}
208
 
 
209
 
void AddAttributeDialog::currentChanged( QListViewItem* item )
210
 
{
211
 
        if ( item )
212
 
        {
213
 
                QString _access = item->text( 0 );
214
 
                QString _storage = item->text( 1 );
215
 
                QString _returnType = item->text( 2 );
216
 
                QString _declarator = item->text( 3 );
217
 
                
218
 
                access->setCurrentText( _access );
219
 
                storage->setCurrentText( _storage );
220
 
                returnType->setCurrentText( _returnType );
221
 
                declarator->setText( _declarator );
222
 
        }
223
 
        
224
 
        updateGUI();
225
 
}
226
 
 
227
 
QStringList AddAttributeDialog::newAccessList( const QStringList& accessList ) const
228
 
{
229
 
        QStringList newAccessList;
230
 
        
231
 
        QListViewItem* item = attributes->firstChild();
232
 
        while ( item )
233
 
        {
234
 
                QListViewItem * currentItem = item;
235
 
                
236
 
                item = item->nextSibling();
237
 
                
238
 
                QString access = currentItem->text( 0 );
239
 
                if ( !( accessList.contains( access ) || newAccessList.contains( access ) ) )
240
 
                        newAccessList.push_back( access );
241
 
        }
242
 
        
243
 
        return newAccessList;
244
 
}
245
 
 
246
 
QString AddAttributeDialog::accessID( VariableDom var ) const
247
 
{
248
 
        switch ( var->access() )
249
 
        {
250
 
        case CodeModelItem::Public:
251
 
                return QString::fromLatin1( "Public" );
252
 
                
253
 
        case CodeModelItem::Protected:
254
 
                return QString::fromLatin1( "Protected" );
255
 
                
256
 
        case CodeModelItem::Private:
257
 
                return QString::fromLatin1( "Private" );
258
 
        }
259
 
        
260
 
        return QString::null;
261
 
}
262
 
 
263
 
#include "addattributedialog.moc" 
264
 
//kate: indent-mode csands; tab-width 4; space-indent off;
265