1
/**********************************************************************
2
** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
4
** This file is part of Qt Designer.
6
** This file may be distributed and/or modified under the terms of the
7
** GNU General Public License version 2 as published by the Free Software
8
** Foundation and appearing in the file LICENSE.GPL included in the
9
** packaging of this file.
11
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12
** licenses may use this file in accordance with the Qt Commercial License
13
** Agreement provided with the Software.
15
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
** See http://www.trolltech.com/gpl/ for GPL licensing information.
19
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20
** information about Qt Commercial License Agreements.
22
** Contact info@trolltech.com if any conditions of this licensing are
25
**********************************************************************/
27
#include "editfunctionsimpl.h"
28
#include "formwindow.h"
29
#include "metadatabase.h"
30
#include "asciivalidator.h"
31
#include "mainwindow.h"
32
#include "hierarchyview.h"
35
#include <kiconloader.h>
36
#include "kdevdesigner_part.h"
39
#include <qlistview.h>
40
#include <qpushbutton.h>
41
#include <qlineedit.h>
42
#include <qcombobox.h>
44
#include <qmessagebox.h>
47
#include <qgroupbox.h>
49
#include <qcheckbox.h>
53
EditFunctions::EditFunctions( QWidget *parent, FormWindow *fw, bool justSlots )
54
: EditFunctionsBase( parent, 0, TRUE ), formWindow( fw )
56
connect( helpButton, SIGNAL( clicked() ), MainWindow::self, SLOT( showDialogHelp() ) );
61
QValueList<MetaDataBase::Function> functionList = MetaDataBase::functionList( fw );
62
for ( QValueList<MetaDataBase::Function>::Iterator it = functionList.begin(); it != functionList.end(); ++it ) {
63
QListViewItem *i = new QListViewItem( functionListView );
65
i->setPixmap( 0, SmallIcon( "designer_editslots.png" , KDevDesignerPartFactory::instance()) );
66
i->setText( 0, (*it).function );
67
i->setText( 1, (*it).returnType );
68
i->setText( 2, (*it).specifier );
69
i->setText( 3, (*it).access );
70
i->setText( 4, (*it).type );
74
fui.oldName = (*it).function;
75
fui.newName = fui.oldName;
76
fui.oldRetTyp = (*it).returnType;
77
fui.retTyp = fui.oldRetTyp;
78
fui.oldSpec = (*it).specifier;
79
fui.spec = fui.oldSpec;
80
fui.oldAccess = (*it).access;
81
fui.access = fui.oldAccess;
82
fui.oldType = (*it).type;
83
fui.type = fui.oldType;
84
functList.append( fui );
86
functionIds.insert( i, id );
89
if ( (*it).type == "slot" ) {
90
if ( MetaDataBase::isSlotUsed( formWindow, MetaDataBase::normalizeFunction( (*it).function ).latin1() ) )
91
i->setText( 5, i18n( "Yes" ) );
93
i->setText( 5, i18n( "No" ) );
95
i->setText( 5, "---" );
99
boxProperties->setEnabled( FALSE );
100
functionName->setValidator( new AsciiValidator( TRUE, functionName ) );
102
if ( functionListView->firstChild() )
103
functionListView->setCurrentItem( functionListView->firstChild() );
105
showOnlySlots->setChecked( justSlots );
106
lastType = "function";
108
// Enable rename for all QListViewItems
109
QListViewItemIterator lvit = functionListView->firstChild();
110
for ( ; *lvit; lvit++ )
111
(*lvit)->setRenameEnabled( 0, TRUE );
113
// Connect listview signal to signal-relay
114
QObject::connect( functionListView,
115
SIGNAL( itemRenamed( QListViewItem*, int, const QString & ) ),
117
SLOT( emitItemRenamed(QListViewItem*, int, const QString&) ) );
119
// Connect signal-relay to QLineEdit "functionName"
120
QObjectList *l = parent->queryList( "QLineEdit", "functionName" );
122
QObjectListIt itemsLineEditIt( *l );
123
while ( (obj = itemsLineEditIt.current()) != 0 ) {
125
QObject::connect( this,
126
SIGNAL( itemRenamed( const QString & ) ),
128
SLOT( setText( const QString & ) ) );
132
functionSpecifier->insertItem( "non virtual" );
133
functionSpecifier->insertItem( "virtual" );
134
functionSpecifier->insertItem( "pure virtual" );
135
functionSpecifier->insertItem( "static" );
137
functionAccess->insertItem( "public" );
138
functionAccess->insertItem( "protected" );
139
functionAccess->insertItem( "private" );
141
functionType->insertItem( "slot" );
142
functionType->insertItem( "function" );
146
void EditFunctions::okClicked()
148
QValueList<MetaDataBase::Function> functionList = MetaDataBase::functionList( formWindow );
149
QString n = i18n( "Add/Remove functions of '%1'" ).arg( formWindow->name() );
150
QPtrList<Command> commands;
151
QValueList<MetaDataBase::Function>::Iterator fit;
152
if ( !functionList.isEmpty() ) {
153
for ( fit = functionList.begin(); fit != functionList.end(); ++fit ) {
154
bool functionFound = FALSE;
155
QValueList<FunctItem>::Iterator it = functList.begin();
156
for ( ; it != functList.end(); ++it ) {
157
if ( MetaDataBase::normalizeFunction( (*it).oldName ) ==
158
MetaDataBase::normalizeFunction( (*fit).function ) ) {
159
functionFound = TRUE;
163
if ( !functionFound )
164
commands.append( new RemoveFunctionCommand( i18n( "Remove Function" ),
165
formWindow, (*fit).function, (*fit).specifier,
168
formWindow->project()->language(),
169
(*fit).returnType ) );
173
bool invalidFunctions = FALSE;
174
QValueList<FunctItem> invalidItems;
176
if ( !functList.isEmpty() ) {
178
QValueList<FunctItem>::Iterator it = functList.begin();
179
for ( ; it != functList.end(); ++it ) {
180
MetaDataBase::Function function;
181
function.function = (*it).newName;
182
function.returnType = (*it).retTyp;
183
function.specifier = (*it).spec;
184
function.access = (*it).access;
185
function.type = (*it).type;
186
function.language = formWindow->project()->language();
187
if ( function.returnType.isEmpty() )
188
function.returnType = "void";
189
QString s = function.function;
190
s = s.simplifyWhiteSpace();
191
bool startNum = s[ 0 ] >= '0' && s[ 0 ] <= '9';
192
bool noParens = s.contains( '(' ) != 1 || s.contains( ')' ) != 1;
193
bool illegalSpace = s.find( ' ' ) != -1 && s.find( ' ' ) < s.find( '(' );
195
if ( startNum || noParens || illegalSpace || lst.find( function.function ) != -1 ) {
196
invalidFunctions = TRUE;
197
invalidItems.append( (*it) );
200
bool functionFound = FALSE;
201
for ( fit = functionList.begin(); fit != functionList.end(); ++fit ) {
202
if ( MetaDataBase::normalizeFunction( (*fit).function ) ==
203
MetaDataBase::normalizeFunction( (*it).oldName ) ) {
204
functionFound = TRUE;
208
if ( !functionFound )
209
commands.append( new AddFunctionCommand( i18n( "Add Function" ),
210
formWindow, function.function, function.specifier,
212
function.type, formWindow->project()->language(),
213
function.returnType ) );
214
if ( MetaDataBase::normalizeFunction( (*it).newName ) != MetaDataBase::normalizeFunction( (*it).oldName ) ||
215
(*it).spec != (*it).oldSpec || (*it).access != (*it).oldAccess || (*it).type != (*it).oldType ||
216
(*it).retTyp != (*it).oldRetTyp ) {
217
QString normalizedOldName = MetaDataBase::normalizeFunction( (*it).oldName );
218
if ((*it).oldName.endsWith("const")) // make sure we get the 'const' when we remove the old name
219
normalizedOldName += " const";
220
commands.append( new ChangeFunctionAttribCommand( i18n( "Change Function Attributes" ),
221
formWindow, function, normalizedOldName,
222
(*it).oldSpec, (*it).oldAccess, (*it).oldType,
223
formWindow->project()->language(), (*it).oldRetTyp ) );
225
lst.append( function.function );
229
if ( invalidFunctions ) {
230
if ( QMessageBox::information( this, i18n( "Edit Functions" ),
231
i18n( "Some syntactically incorrect functions have been defined.\n"
232
"Remove these functions?" ), i18n( "&Yes" ), i18n( "&No" ) ) == 0 ) {
233
QValueList<FunctItem>::Iterator it = functList.begin();
234
while ( it != functList.end() ) {
236
QValueList<FunctItem>::Iterator vit = invalidItems.begin();
237
for ( ; vit != invalidItems.end(); ++vit ) {
238
if ( (*vit).newName == (*it).newName ) {
239
invalidItems.remove( vit );
245
int delId = (*it).id;
246
it = functList.remove( it );
247
QMap<QListViewItem*, int>::Iterator fit = functionIds.begin();
248
while ( fit != functionIds.end() ) {
249
if ( *fit == delId ) {
250
QListViewItem *litem = fit.key();
251
functionIds.remove( fit );
253
if ( functionListView->currentItem() )
254
functionListView->setSelected( functionListView->currentItem(), TRUE );
255
currentItemChanged( functionListView->currentItem() );
264
if ( functionListView->firstChild() ) {
265
functionListView->setCurrentItem( functionListView->firstChild() );
266
functionListView->setSelected( functionListView->firstChild(), TRUE );
269
formWindow->mainWindow()->objectHierarchy()->updateFormDefinitionView();
273
if ( !commands.isEmpty() ) {
274
MacroCommand *cmd = new MacroCommand( n, formWindow, commands );
275
formWindow->commandHistory()->addCommand( cmd );
279
formWindow->mainWindow()->objectHierarchy()->updateFormDefinitionView();
283
void EditFunctions::functionAdd( const QString &access, const QString &type )
285
QListViewItem *i = new QListViewItem( functionListView );
286
i->setPixmap( 0, SmallIcon( "designer_editslots.png" , KDevDesignerPartFactory::instance()) );
287
i->setRenameEnabled( 0, TRUE );
288
i->setText( 1, "void" );
289
i->setText( 2, "virtual" );
291
if ( access.isEmpty() )
292
i->setText( 3, "public" );
294
i->setText( 3, access );
296
if( type.isEmpty() ) {
297
if ( showOnlySlots->isChecked() )
298
i->setText( 4, "slot" );
300
i->setText( 4, lastType );
303
i->setText( 4, type );
306
if ( i->text( 4 ) == "slot" ) {
307
i->setText( 0, "newSlot()" );
308
if ( MetaDataBase::isSlotUsed( formWindow, "newSlot()" ) )
309
i->setText( 5, i18n( "Yes" ) );
311
i->setText( 5, i18n( "No" ) );
313
i->setText( 0, "newFunction()" );
314
i->setText( 5, "---" );
317
functionListView->setCurrentItem( i );
318
functionListView->setSelected( i, TRUE );
319
functionListView->ensureItemVisible( i );
320
functionName->setFocus();
321
functionName->selectAll();
325
fui.oldName = i->text( 0 );
326
fui.newName = fui.oldName;
327
fui.oldRetTyp = i->text( 1 );
328
fui.retTyp = fui.oldRetTyp;
329
fui.oldSpec = i->text ( 2 );
330
fui.spec = fui.oldSpec;
331
fui.oldAccess = i->text( 3 );
332
fui.access = fui.oldAccess;
333
fui.oldType = i->text( 4 );
334
fui.type = fui.oldType;
335
lastType = fui.oldType;
336
functList.append( fui );
337
functionIds.insert( i, id );
341
void EditFunctions::functionRemove()
343
if ( !functionListView->currentItem() )
346
functionListView->blockSignals( TRUE );
347
removedFunctions << MetaDataBase::normalizeFunction( functionListView->currentItem()->text( 0 ) );
348
int delId = functionIds[ functionListView->currentItem() ];
349
QValueList<FunctItem>::Iterator it = functList.begin();
350
while ( it != functList.end() ) {
351
if ( (*it).id == delId ) {
352
functList.remove( it );
357
functionIds.remove( functionListView->currentItem() );
358
delete functionListView->currentItem();
359
if ( functionListView->currentItem() )
360
functionListView->setSelected( functionListView->currentItem(), TRUE );
361
functionListView->blockSignals( FALSE );
362
currentItemChanged( functionListView->currentItem() );
365
void EditFunctions::currentItemChanged( QListViewItem *i )
367
functionName->blockSignals( TRUE );
368
functionName->setText( "" );
369
functionAccess->setCurrentItem( 0 );
370
functionName->blockSignals( FALSE );
373
boxProperties->setEnabled( FALSE );
377
functionName->blockSignals( TRUE );
378
functionName->setText( i->text( 0 ) );
379
editType->setText( i->text( 1 ) );
380
QString specifier = i->text( 2 );
381
QString access = i->text( 3 );
382
QString type = i->text( 4 );
383
if ( specifier == "pure virtual" )
384
functionSpecifier->setCurrentItem( 2 );
385
else if ( specifier == "non virtual" )
386
functionSpecifier->setCurrentItem( 0 );
387
else if ( specifier == "virtual" )
388
functionSpecifier->setCurrentItem( 1 );
390
functionSpecifier->setCurrentItem( 3 );
391
if ( access == "private" )
392
functionAccess->setCurrentItem( 2 );
393
else if ( access == "protected" )
394
functionAccess->setCurrentItem( 1 );
396
functionAccess->setCurrentItem( 0 );
397
if ( type == "slot" )
398
functionType->setCurrentItem( 0 );
400
functionType->setCurrentItem( 1 );
402
functionName->blockSignals( FALSE );
403
boxProperties->setEnabled( TRUE );
406
void EditFunctions::currentTextChanged( const QString &txt )
408
if ( !functionListView->currentItem() )
411
changeItem( functionListView->currentItem(), Name, txt );
412
functionListView->currentItem()->setText( 0, txt );
414
if ( functionListView->currentItem()->text( 4 ) == "slot" ) {
415
if ( MetaDataBase::isSlotUsed( formWindow, MetaDataBase::normalizeFunction( txt.latin1() ).latin1() ) )
416
functionListView->currentItem()->setText( 5, i18n( "Yes" ) );
418
functionListView->currentItem()->setText( 5, i18n( "No" ) );
420
functionListView->currentItem()->setText( 5, "---" );
424
void EditFunctions::currentSpecifierChanged( const QString& s )
426
if ( !functionListView->currentItem() )
429
changeItem( functionListView->currentItem(), Specifier, s );
430
functionListView->currentItem()->setText( 2, s );
433
void EditFunctions::currentAccessChanged( const QString& a )
435
if ( !functionListView->currentItem() )
437
changeItem( functionListView->currentItem(), Access, a );
438
functionListView->currentItem()->setText( 3, a );
442
void EditFunctions::currentReturnTypeChanged( const QString &type )
444
if ( !functionListView->currentItem() )
446
changeItem( functionListView->currentItem(), ReturnType, type );
447
functionListView->currentItem()->setText( 1, type );
450
void EditFunctions::currentTypeChanged( const QString &type )
452
if ( !functionListView->currentItem() )
454
changeItem( functionListView->currentItem(), Type, type );
456
functionListView->currentItem()->setText( 4, type );
457
if ( type == "slot" ) {
458
if ( MetaDataBase::isSlotUsed( formWindow,
459
MetaDataBase::normalizeFunction( functionListView->currentItem()->text( 0 ).latin1() ).latin1() ) )
460
functionListView->currentItem()->setText( 5, i18n( "Yes" ) );
462
functionListView->currentItem()->setText( 5, i18n( "No" ) );
464
functionListView->currentItem()->setText( 5, "---" );
468
void EditFunctions::changeItem( QListViewItem *item, Attribute a, const QString &nV )
471
QMap<QListViewItem*, int>::Iterator fit = functionIds.find( item );
472
if ( fit != functionIds.end() )
477
QValueList<FunctItem>::Iterator it = functList.begin();
478
for ( ; it != functList.end(); ++it ) {
479
if ( (*it).id == itemId ) {
501
void EditFunctions::setCurrentFunction( const QString &function )
503
QListViewItemIterator it( functionListView );
504
while ( it.current() ) {
505
if ( MetaDataBase::normalizeFunction( it.current()->text( 0 ) ) == function ) {
506
functionListView->setCurrentItem( it.current() );
507
functionListView->setSelected( it.current(), TRUE );
508
currentItemChanged( it.current() );
515
void EditFunctions::displaySlots( bool justSlots )
518
functionListView->clear();
519
for ( QValueList<FunctItem>::Iterator it = functList.begin(); it != functList.end(); ++it ) {
520
if ( (*it).type == "function" && justSlots )
522
QListViewItem *i = new QListViewItem( functionListView );
523
functionIds.insert( i, (*it).id );
524
i->setPixmap( 0, SmallIcon( "designer_editslots.png" , KDevDesignerPartFactory::instance()) );
525
i->setText( 0, (*it).newName );
526
i->setText( 1, (*it).retTyp );
527
i->setText( 2, (*it).spec );
528
i->setText( 3, (*it).access );
529
i->setText( 4, (*it).type );
531
if ( (*it).type == "slot" ) {
532
if ( MetaDataBase::isSlotUsed( formWindow, MetaDataBase::normalizeFunction( (*it).newName ).latin1() ) )
533
i->setText( 5, i18n( "Yes" ) );
535
i->setText( 5, i18n( "No" ) );
537
i->setText( 5, "---" );
541
if ( functionListView->firstChild() )
542
functionListView->setSelected( functionListView->firstChild(), TRUE );
545
void EditFunctions::emitItemRenamed( QListViewItem *, int, const QString & text )
547
emit itemRenamed( text ); // Relay signal ( to QLineEdit )