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

« back to all changes in this revision

Viewing changes to kdevdesigner/designer/formfile.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) 2000 Trolltech AS.  All rights reserved.
3
 
**
4
 
** This file is part of Qt Designer.
5
 
**
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.
10
 
**
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.
14
 
**
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.
17
 
**
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.
21
 
**
22
 
** Contact info@trolltech.com if any conditions of this licensing are
23
 
** not clear to you.
24
 
**
25
 
**********************************************************************/
26
 
 
27
 
#include "formfile.h"
28
 
#include "timestamp.h"
29
 
#include "project.h"
30
 
#include "formwindow.h"
31
 
#include "command.h"
32
 
#include "sourceeditor.h"
33
 
#include "mainwindow.h"
34
 
#include "../interfaces/languageinterface.h"
35
 
#include "resource.h"
36
 
#include "workspace.h"
37
 
#include <qmessagebox.h>
38
 
#include <qfile.h>
39
 
#include <qstatusbar.h>
40
 
#include "propertyeditor.h"
41
 
#include <qworkspace.h>
42
 
#include <stdlib.h>
43
 
#include "designerappiface.h"
44
 
#include <qapplication.h>
45
 
 
46
 
#include <kfiledialog.h>
47
 
#include <klocale.h>
48
 
#include "kdevdesigner_part.h"
49
 
 
50
 
static QString make_func_pretty( const QString &s )
51
 
{
52
 
    QString res = s;
53
 
    if ( res.find( ")" ) - res.find( "(" ) == 1 )
54
 
        return res;
55
 
    res.replace( "(", "( " );
56
 
    res.replace( ")", " )" );
57
 
    res.replace( "&", " &" );
58
 
    res.replace( "*", " *" );
59
 
    res.replace( ",", ", " );
60
 
    res.replace( ":", " : " );
61
 
    res = res.simplifyWhiteSpace();
62
 
    res.replace( " : : ", "::" );
63
 
    res.replace( ">>", "> >" );
64
 
    return res;
65
 
}
66
 
 
67
 
FormFile::FormFile( const QString &fn, bool temp, Project *p, const char *name )
68
 
    : filename( fn ), fileNameTemp( temp ), pro( p ), fw( 0 ), ed( 0 ),
69
 
      timeStamp( 0, fn + codeExtension() ), codeEdited( FALSE ), pkg( FALSE ),
70
 
      cm( FALSE ), codeFileStat( None )
71
 
{
72
 
    MetaDataBase::addEntry( this );
73
 
    fake = qstrcmp( name, "qt_fakewindow" ) == 0;
74
 
    //codeFileStat = FormFile::None;
75
 
    pro->addFormFile( this );
76
 
    loadCode();
77
 
    if ( !temp )
78
 
        checkFileName( FALSE );
79
 
    
80
 
    connect(this, SIGNAL(somethingChanged(FormFile* )), this, SLOT(emitNewStatus(FormFile* )));
81
 
}
82
 
 
83
 
FormFile::~FormFile()
84
 
{
85
 
    pro->removeFormFile( this );
86
 
    if ( formWindow() )
87
 
        formWindow()->setFormFile( 0 );
88
 
}
89
 
 
90
 
void FormFile::setFormWindow( FormWindow *f )
91
 
{
92
 
    if ( f == fw )
93
 
        return;
94
 
    if ( fw )
95
 
        fw->setFormFile( 0 );
96
 
    fw = f;
97
 
    if ( fw )
98
 
        fw->setFormFile( this );
99
 
    parseCode( cod, FALSE );
100
 
    QTimer::singleShot( 0, this, SLOT( notifyFormWindowChange() ) );
101
 
}
102
 
 
103
 
void FormFile::setEditor( SourceEditor *e )
104
 
{
105
 
    ed = e;
106
 
}
107
 
 
108
 
void FormFile::setFileName( const QString &fn )
109
 
{
110
 
    if ( fn == filename )
111
 
        return;
112
 
    if ( fn.isEmpty() ) {
113
 
        fileNameTemp = TRUE;
114
 
        if ( filename.find( "unnamed" ) != 0 )
115
 
            filename = createUnnamedFileName();
116
 
        return;
117
 
    } else {
118
 
        fileNameTemp = FALSE;
119
 
    }
120
 
    filename = fn;
121
 
    timeStamp.setFileName( filename + codeExtension() );
122
 
    cod = "";
123
 
    loadCode();
124
 
}
125
 
 
126
 
void FormFile::setCode( const QString &c )
127
 
{
128
 
    cod = c;
129
 
}
130
 
 
131
 
FormWindow *FormFile::formWindow() const
132
 
{
133
 
    return fw;
134
 
}
135
 
 
136
 
SourceEditor *FormFile::editor() const
137
 
{
138
 
    return ed;
139
 
}
140
 
 
141
 
QString FormFile::fileName() const
142
 
{
143
 
    return filename;
144
 
}
145
 
 
146
 
QString FormFile::absFileName() const
147
 
{
148
 
    return pro->makeAbsolute( filename );
149
 
}
150
 
 
151
 
QString FormFile::codeFile() const
152
 
{
153
 
    QString codeExt = codeExtension();
154
 
    if ( codeExt.isEmpty() )
155
 
        return "";
156
 
    return filename + codeExt;
157
 
}
158
 
 
159
 
QString FormFile::code()
160
 
{
161
 
    return cod;
162
 
}
163
 
 
164
 
bool FormFile::save( bool withMsgBox, bool ignoreModified )
165
 
{
166
 
    if ( fileNameTemp )
167
 
        return saveAs();
168
 
    if ( !ignoreModified && !isModified() )
169
 
        return TRUE;
170
 
    if ( ed )
171
 
        ed->save();
172
 
 
173
 
    if ( formWindow() && isModified( WFormWindow ) ) {
174
 
        if ( withMsgBox ) {
175
 
            if ( !formWindow()->checkCustomWidgets() )
176
 
                return FALSE;
177
 
        }
178
 
 
179
 
        if ( QFile::exists( pro->makeAbsolute( filename ) ) ) {
180
 
            QString fn( pro->makeAbsolute( filename ) );
181
 
#if defined(Q_OS_WIN32)
182
 
            fn += ".bak";
183
 
#else
184
 
            fn += "~";
185
 
#endif
186
 
            QFile f( pro->makeAbsolute( filename ) );
187
 
            if ( f.open( IO_ReadOnly ) ) {
188
 
                QFile f2( fn );
189
 
                if ( f2.open( IO_WriteOnly | IO_Translate ) ) {
190
 
                    QCString data( f.size() );
191
 
                    f.readBlock( data.data(), f.size() );
192
 
                    f2.writeBlock( data );
193
 
                } else {
194
 
                    QMessageBox::warning( MainWindow::self, i18n( "Save" ),
195
 
                                  i18n( "The file %1 could not be saved" ).arg( codeFile() ) );
196
 
                }
197
 
            }
198
 
        }
199
 
    }
200
 
 
201
 
    if ( isModified( WFormCode ) ) {
202
 
        if ( QFile::exists( pro->makeAbsolute( codeFile() ) ) ) {
203
 
            QString fn( pro->makeAbsolute( codeFile() ) );
204
 
#if defined(Q_OS_WIN32)
205
 
            fn += ".bak";
206
 
#else
207
 
            fn += "~";
208
 
#endif
209
 
            QFile f( pro->makeAbsolute( codeFile() ) );
210
 
            if ( f.open( IO_ReadOnly ) ) {
211
 
                QFile f2( fn );
212
 
                if ( f2.open( IO_WriteOnly | IO_Translate) ) {
213
 
                    QCString data( f.size() );
214
 
                    f.readBlock( data.data(), f.size() );
215
 
                    f2.writeBlock( data );
216
 
                } else if ( qApp->type() != QApplication::Tty ) {
217
 
            QMessageBox::warning( MainWindow::self, i18n( "Save" ),
218
 
                                  i18n( "The file %1 could not be saved" ).arg( codeFile() ) );
219
 
                }
220
 
            }
221
 
        }
222
 
    }
223
 
 
224
 
    if ( formWindow() ) {
225
 
        Resource resource( MainWindow::self );
226
 
        resource.setWidget( formWindow() );
227
 
        bool formCodeOnly = isModified( WFormCode ) && !isModified( WFormWindow );
228
 
        if ( !resource.save( pro->makeAbsolute( filename ), formCodeOnly ) ) {
229
 
            if ( MainWindow::self )
230
 
                MainWindow::self->statusMessage( i18n( "Failed to save file '%1'.").arg( formCodeOnly ? codeFile(): filename ) );
231
 
            if ( formCodeOnly )
232
 
                return FALSE;
233
 
            return saveAs();
234
 
        }
235
 
        if ( MainWindow::self )
236
 
            MainWindow::self->statusMessage( i18n( "'%1' saved.").
237
 
                                                    arg( formCodeOnly ? codeFile() : filename ));
238
 
    } else {
239
 
        if ( !Resource::saveFormCode(this, MetaDataBase::languageInterface(pro->language())) )
240
 
            return FALSE;
241
 
    }
242
 
    timeStamp.update();
243
 
    setModified( FALSE );
244
 
    return TRUE;
245
 
}
246
 
 
247
 
bool FormFile::saveAs( bool ignoreModified )
248
 
{
249
 
    QString f = pro->makeAbsolute( fileName() );
250
 
    if ( fileNameTemp && formWindow() ) {
251
 
        f = QString( formWindow()->name() ).lower();
252
 
        f.replace( "::", "_" );
253
 
        f = pro->makeAbsolute( f + ".ui" );
254
 
    }
255
 
    bool saved = FALSE;
256
 
    if ( ignoreModified ) {
257
 
        QString dir = QStringList::split( ':', pro->iFace()->customSetting( "QTSCRIPT_PACKAGES" ) ).first();
258
 
        f = QFileInfo( f ).fileName();
259
 
        f.prepend( dir + "/" );
260
 
    }
261
 
    QString fn;
262
 
    while ( !saved ) {
263
 
        fn = KFileDialog::getSaveFileName( f,
264
 
                                           i18n( "*.ui|Qt User-Interface Files" ) + "\n" +
265
 
                                           i18n( "*|All Files" ), MainWindow::self, /*0,*/
266
 
                                           i18n( "Save Form '%1' As").arg( formName() )/*,
267
 
                                           MainWindow::self ? &MainWindow::self->lastSaveFilter : 0*/ );
268
 
        if ( fn.isEmpty() )
269
 
            return FALSE;
270
 
        QFileInfo fi( fn );
271
 
        if ( fi.extension() != "ui" )
272
 
            fn += ".ui";
273
 
        fileNameTemp = FALSE;
274
 
        filename = pro->makeRelative( fn );
275
 
        QFileInfo relfi( filename );
276
 
        if ( relfi.exists() ) {
277
 
            if ( QMessageBox::warning( MainWindow::self, i18n( "File Already Exists" ),
278
 
                i18n( "The file already exists. Do you wish to overwrite it?" ),
279
 
                QMessageBox::Yes,
280
 
                QMessageBox::No ) == QMessageBox::Yes ) {
281
 
                saved = TRUE;
282
 
            } else {
283
 
                filename = f;
284
 
            }
285
 
 
286
 
        } else {
287
 
            saved = TRUE;
288
 
        }
289
 
    }
290
 
    if ( !checkFileName( TRUE ) ) {
291
 
        filename = f;
292
 
        return FALSE;
293
 
    }
294
 
    pro->setModified( TRUE );
295
 
    timeStamp.setFileName( pro->makeAbsolute( codeFile() ) );
296
 
    if ( ed && formWindow() )
297
 
        ed->setCaption( i18n( "Edit %1" ).arg( formWindow()->name() ) );
298
 
    setModified( TRUE );
299
 
    if ( pro->isDummy() )
300
 
        fw->mainWindow()->addRecentlyOpenedFile( fn );
301
 
    return save( TRUE, ignoreModified );
302
 
}
303
 
 
304
 
bool FormFile::close()
305
 
{
306
 
    if ( editor() ) {
307
 
        editor()->save();
308
 
        editor()->close();
309
 
    }
310
 
    if ( formWindow() )
311
 
        return formWindow()->close();
312
 
    return TRUE;
313
 
}
314
 
 
315
 
bool FormFile::closeEvent()
316
 
{
317
 
    if ( !isModified() && fileNameTemp ) {
318
 
        pro->removeFormFile( this );
319
 
        return TRUE;
320
 
    }
321
 
 
322
 
    if ( !isModified() )
323
 
        return TRUE;
324
 
 
325
 
    if ( editor() )
326
 
        editor()->save();
327
 
 
328
 
    switch ( QMessageBox::warning( MainWindow::self, i18n( "Save Form" ),
329
 
                                   i18n( "Save changes to form '%1'?" ).arg( filename ),
330
 
                                   i18n( "&Yes" ), i18n( "&No" ), i18n( "&Cancel" ), 0, 2 ) ) {
331
 
    case 0: // save
332
 
        if ( !save() )
333
 
            return FALSE;
334
 
    case 1: // don't save
335
 
        loadCode();
336
 
        if ( ed )
337
 
            ed->editorInterface()->setText( cod );
338
 
        if ( fileNameTemp )
339
 
            pro->removeFormFile( this );
340
 
        if ( MainWindow::self )
341
 
            MainWindow::self->workspace()->update();
342
 
        break;
343
 
    case 2: // cancel
344
 
        return FALSE;
345
 
    default:
346
 
        break;
347
 
    }
348
 
 
349
 
    setModified( FALSE );
350
 
    if ( MainWindow::self )
351
 
        MainWindow::self->updateFunctionList();
352
 
    setCodeEdited( FALSE );
353
 
    return TRUE;
354
 
}
355
 
 
356
 
void FormFile::setModified( bool m, int who )
357
 
{
358
 
    if ( ( who & WFormWindow ) == WFormWindow )
359
 
        setFormWindowModified( m );
360
 
    if ( ( who & WFormCode ) == WFormCode )
361
 
        setCodeModified( m );
362
 
}
363
 
 
364
 
bool FormFile::isModified( int who )
365
 
{
366
 
    if ( who == WFormWindow )
367
 
        return isFormWindowModified();
368
 
    if ( who == WFormCode )
369
 
        return isCodeModified();
370
 
    return isCodeModified() || isFormWindowModified();
371
 
}
372
 
 
373
 
bool FormFile::isFormWindowModified() const
374
 
{
375
 
    if ( !formWindow()  || !formWindow()->commandHistory() )
376
 
        return FALSE;
377
 
    return formWindow()->commandHistory()->isModified();
378
 
}
379
 
 
380
 
bool FormFile::isCodeModified() const
381
 
{
382
 
    if ( !editor() )
383
 
        return cm;
384
 
    return editor()->isModified();
385
 
}
386
 
 
387
 
void FormFile::setFormWindowModified( bool m )
388
 
{
389
 
    bool b = isFormWindowModified();
390
 
    if ( m == b )
391
 
        return;
392
 
    if ( !formWindow() || !formWindow()->commandHistory() )
393
 
        return;
394
 
    formWindow()->commandHistory()->setModified( m );
395
 
    emit somethingChanged( this );
396
 
}
397
 
 
398
 
void FormFile::setCodeModified( bool m )
399
 
{
400
 
    bool b = isCodeModified();
401
 
    if ( m == b )
402
 
        return;
403
 
    emit somethingChanged( this );
404
 
    cm = m;
405
 
    if ( !editor() )
406
 
        return;
407
 
    editor()->setModified( m );
408
 
}
409
 
 
410
 
void FormFile::showFormWindow()
411
 
{
412
 
    if ( !MainWindow::self )
413
 
        return;
414
 
    if ( formWindow() ) {
415
 
        if ( ( formWindow()->hasFocus() ||
416
 
              MainWindow::self->qWorkspace()->activeWindow() == formWindow() ) &&
417
 
             MainWindow::self->propertyeditor()->formWindow() != formWindow() ) {
418
 
            MainWindow::self->propertyeditor()->setWidget( formWindow()->currentWidget(), formWindow() );
419
 
            MainWindow::self->objectHierarchy()->setFormWindow( formWindow(),
420
 
                                                                formWindow()->currentWidget() );
421
 
        }
422
 
        formWindow()->setFocus();
423
 
        return;
424
 
    }
425
 
    MainWindow::self->openFormWindow( pro->makeAbsolute( filename ), TRUE, this );
426
 
}
427
 
 
428
 
bool FormFile::setupUihFile( bool askForUih )
429
 
{
430
 
    if ( !pro->isCpp() || !askForUih ) {
431
 
        if ( !hasFormCode() ) {
432
 
            createFormCode();
433
 
            setModified( TRUE );
434
 
        }
435
 
        codeFileStat = FormFile::Ok;
436
 
        return TRUE;
437
 
    }
438
 
    if ( codeFileStat != FormFile::Ok && !ed ) {
439
 
        if ( hasFormCode() ) {
440
 
            int i = QMessageBox::information( MainWindow::self, i18n( "Using ui.h File" ),
441
 
                                              i18n( "An \"ui.h\" file for this form already exists.\n"
442
 
                                              "Do you want to use it or create a new one?" ),
443
 
                                              i18n( "Use Existing" ), i18n( "Create New" ),
444
 
                                              i18n( "Cancel" ), 2, 2 );
445
 
            if ( i == 2 )
446
 
                return FALSE;
447
 
            if ( i == 1 )
448
 
                createFormCode();
449
 
        } else {
450
 
            if ( QMessageBox::Yes != QMessageBox::information( MainWindow::self, i18n( "Creating ui.h file" ),
451
 
                                                 i18n( "Do you want to create an new \"ui.h\" file?" ),
452
 
                                                 QMessageBox::Yes, QMessageBox::No ) )
453
 
                return FALSE;
454
 
            createFormCode();
455
 
        }
456
 
        setModified( TRUE );
457
 
    }
458
 
    codeFileStat = FormFile::Ok;
459
 
    return TRUE;
460
 
}
461
 
 
462
 
SourceEditor *FormFile::showEditor( bool askForUih )
463
 
{
464
 
    if ( !MainWindow::self )
465
 
        return 0;
466
 
    showFormWindow();
467
 
    //emit signal to the embedding IDE if there is one and return 0 because
468
 
    //no source editing is possible
469
 
    emit fw->mainWindow()->part()->emitEditSource(fw->fileName());
470
 
    return 0;
471
 
/*    if ( !setupUihFile( askForUih ) )
472
 
        return 0;
473
 
    return MainWindow::self->openSourceEditor();*/
474
 
}
475
 
 
476
 
static int ui_counter = 0;
477
 
QString FormFile::createUnnamedFileName()
478
 
{
479
 
    return QString( "unnamed" ) + QString::number( ++ui_counter ) + QString( ".ui" );
480
 
}
481
 
 
482
 
QString FormFile::codeExtension() const
483
 
{
484
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
485
 
    if ( iface )
486
 
        return iface->formCodeExtension();
487
 
    return "";
488
 
}
489
 
 
490
 
QString FormFile::codeComment() const
491
 
{
492
 
    return QString( "/****************************************************************************\n"
493
 
    "** ui.h extension file, included from the uic-generated form implementation.\n"
494
 
    "**\n"
495
 
    "** If you want to add, delete, or rename functions or slots, use\n"
496
 
    "** Qt Designer to update this file, preserving your code.\n"
497
 
    "**\n"
498
 
    "** You should not define a constructor or destructor in this file.\n"
499
 
    "** Instead, write your code in functions called init() and destroy().\n"
500
 
    "** These will automatically be called by the form's constructor and\n"
501
 
    "** destructor.\n"
502
 
    "*****************************************************************************/\n" );
503
 
}
504
 
 
505
 
bool FormFile::hasFormCode() const
506
 
{
507
 
    return !cod.isEmpty() && cod != QString( codeComment() );
508
 
}
509
 
 
510
 
int FormFile::codeFileState() const
511
 
{
512
 
    return hasFormCode() ? codeFileStat : None;
513
 
}
514
 
 
515
 
void FormFile::setCodeFileState( UihState s )
516
 
{
517
 
    codeFileStat = s;
518
 
}
519
 
 
520
 
void FormFile::createFormCode()
521
 
{
522
 
    if ( !formWindow() )
523
 
        return;
524
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
525
 
    if ( !iface )
526
 
        return;
527
 
    if ( pro->isCpp() )
528
 
        cod = codeComment();
529
 
    QValueList<MetaDataBase::Function> functionList = MetaDataBase::functionList( formWindow() );
530
 
    for ( QValueList<MetaDataBase::Function>::Iterator it = functionList.begin(); it != functionList.end(); ++it ) {
531
 
        cod += (!cod.isEmpty() ? "\n\n" : "") +
532
 
               iface->createFunctionStart( formWindow()->name(), make_func_pretty((*it).function),
533
 
                                           (*it).returnType.isEmpty() ?
534
 
                                           QString( "void" ) :
535
 
                                           (*it).returnType, (*it).access ) +
536
 
               "\n" + iface->createEmptyFunction();
537
 
    }
538
 
    parseCode( cod, FALSE );
539
 
}
540
 
 
541
 
void FormFile::load()
542
 
{
543
 
    showFormWindow();
544
 
    code();
545
 
}
546
 
 
547
 
bool FormFile::loadCode()
548
 
{
549
 
    QFile f( pro->makeAbsolute( codeFile() ) );
550
 
    if ( !f.open( IO_ReadOnly ) ) {
551
 
        cod = "";
552
 
        setCodeFileState( FormFile::None );
553
 
        return FALSE;
554
 
    }
555
 
    QTextStream ts( &f );
556
 
    cod = ts.read();
557
 
    parseCode( cod, FALSE );
558
 
    if ( hasFormCode() && codeFileStat != FormFile::Ok )
559
 
        setCodeFileState( FormFile::Deleted );
560
 
    timeStamp.update();
561
 
    return TRUE;
562
 
}
563
 
 
564
 
bool FormFile::isCodeEdited() const
565
 
{
566
 
    return codeEdited;
567
 
}
568
 
 
569
 
void FormFile::setCodeEdited( bool b )
570
 
{
571
 
    codeEdited = b;
572
 
}
573
 
 
574
 
void FormFile::parseCode( const QString &txt, bool allowModify )
575
 
{
576
 
    if ( !formWindow() )
577
 
        return;
578
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
579
 
    if ( !iface )
580
 
        return;
581
 
    QValueList<LanguageInterface::Function> functions;
582
 
    QValueList<MetaDataBase::Function> newFunctions, oldFunctions;
583
 
    oldFunctions = MetaDataBase::functionList( formWindow() );
584
 
    iface->functions( txt, &functions );
585
 
    QMap<QString, QString> funcs;
586
 
    for ( QValueList<LanguageInterface::Function>::Iterator it = functions.begin();
587
 
          it != functions.end(); ++it ) {
588
 
        bool found = FALSE;
589
 
        for ( QValueList<MetaDataBase::Function>::Iterator fit = oldFunctions.begin();
590
 
              fit != oldFunctions.end(); ++fit ) {
591
 
            QString f( (*fit).function );
592
 
            if ( MetaDataBase::normalizeFunction( f ) ==
593
 
                 MetaDataBase::normalizeFunction( (*it).name ) ) {
594
 
                found = TRUE;
595
 
                MetaDataBase::Function function;
596
 
                function.function = make_func_pretty( (*it).name );
597
 
                function.specifier = (*fit).specifier;
598
 
                function.type = (*fit).type;
599
 
                if ( !pro->isCpp() )
600
 
                    function.access = (*it).access;
601
 
                else
602
 
                    function.access = (*fit).access;
603
 
                function.language = (*fit).language;
604
 
                function.returnType = (*it).returnType;
605
 
                newFunctions << function;
606
 
                funcs.insert( (*it).name, (*it).body );
607
 
                oldFunctions.remove( fit );
608
 
                break;
609
 
            }
610
 
        }
611
 
        if ( !found ) {
612
 
            MetaDataBase::Function function;
613
 
            function.function = make_func_pretty( (*it).name );
614
 
            function.specifier = "virtual";
615
 
            function.access = "public";
616
 
            function.language = pro->language();
617
 
            function.returnType = (*it).returnType;
618
 
            if ( function.returnType == "void" )
619
 
                function.type = "slot";
620
 
            else
621
 
                function.type = "function";
622
 
            if ( function.function == "init()" || function.function == "destroy()" ) {
623
 
                function.type = "function";
624
 
                function.access = "private";
625
 
                function.specifier = "non virtual";
626
 
            }
627
 
            newFunctions << function;
628
 
            funcs.insert( (*it).name, (*it).body );
629
 
            if ( allowModify )
630
 
                setFormWindowModified( TRUE );
631
 
        }
632
 
    }
633
 
 
634
 
    if ( allowModify && oldFunctions.count() > 0 )
635
 
        setFormWindowModified( TRUE );
636
 
 
637
 
    MetaDataBase::setFunctionList( formWindow(), newFunctions );
638
 
}
639
 
 
640
 
void FormFile::syncCode()
641
 
{
642
 
    if ( !editor() )
643
 
        return;
644
 
    parseCode( editor()->editorInterface()->text(), TRUE );
645
 
    cod = editor()->editorInterface()->text();
646
 
}
647
 
 
648
 
void FormFile::checkTimeStamp()
649
 
{
650
 
    if ( timeStamp.isUpToDate() )
651
 
        return;
652
 
    timeStamp.update();
653
 
    if ( codeEdited ) {
654
 
        if ( QMessageBox::information( MainWindow::self, i18n( "Qt Designer" ),
655
 
                                       i18n( "File '%1' has been changed outside Qt Designer.\n"
656
 
                                           "Do you want to reload it?" ).arg( timeStamp.fileName() ),
657
 
                                       i18n( "&Yes" ), i18n( "&No" ) ) == 0 ) {
658
 
            QFile f( timeStamp.fileName() );
659
 
            if ( f.open( IO_ReadOnly ) ) {
660
 
                QTextStream ts( &f );
661
 
                editor()->editorInterface()->setText( ts.read() );
662
 
                editor()->save();
663
 
                if ( MainWindow::self )
664
 
                    MainWindow::self->functionsChanged();
665
 
            }
666
 
        }
667
 
    } else {
668
 
        loadCode();
669
 
    }
670
 
}
671
 
 
672
 
bool FormFile::isUihFileUpToDate()
673
 
{
674
 
    if ( timeStamp.isUpToDate() )
675
 
        return TRUE;
676
 
    if ( !editor() ) {
677
 
        MainWindow::self->editSource();
678
 
        qDebug( "parse Code" );
679
 
        parseCode( editor()->editorInterface()->text(), TRUE );
680
 
    }
681
 
    checkTimeStamp();
682
 
    return FALSE;
683
 
}
684
 
 
685
 
void FormFile::addFunctionCode( MetaDataBase::Function function )
686
 
{
687
 
    if ( pro->isCpp() && !hasFormCode() && !codeEdited )
688
 
        return;
689
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
690
 
    if ( !iface )
691
 
        return;
692
 
 
693
 
    QValueList<LanguageInterface::Function> funcs;
694
 
    iface->functions( cod, &funcs );
695
 
    bool hasFunc = FALSE;
696
 
    for ( QValueList<LanguageInterface::Function>::Iterator it = funcs.begin();
697
 
          it != funcs.end(); ++it ) {
698
 
        if ( MetaDataBase::normalizeFunction( (*it).name ) == MetaDataBase::normalizeFunction( function.function ) ) {
699
 
            hasFunc = TRUE;
700
 
            break;
701
 
        }
702
 
    }
703
 
 
704
 
    if ( !hasFunc ) {
705
 
        if ( !codeEdited && !timeStamp.isUpToDate() )
706
 
            loadCode();
707
 
        MetaDataBase::MetaInfo mi = MetaDataBase::metaInfo( formWindow() );
708
 
        QString cn;
709
 
        if ( mi.classNameChanged )
710
 
            cn = mi.className;
711
 
        if ( cn.isEmpty() )
712
 
            cn = formWindow()->name();
713
 
        QString body = "\n\n" + iface->createFunctionStart( cn,
714
 
                                                            make_func_pretty( function.function ),
715
 
                                                            function.returnType.isEmpty() ?
716
 
                                                            QString( "void" ) :
717
 
                                                            function.returnType, function.access ) +
718
 
                       "\n" + iface->createEmptyFunction();
719
 
        cod += body;
720
 
        if ( codeEdited ) {
721
 
            setModified( TRUE );
722
 
            emit somethingChanged( this );
723
 
        }
724
 
    }
725
 
}
726
 
 
727
 
void FormFile::removeFunctionCode( MetaDataBase::Function function )
728
 
{
729
 
    if ( pro->isCpp() && !hasFormCode() && !codeEdited )
730
 
        return;
731
 
 
732
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
733
 
    if ( !iface )
734
 
        return;
735
 
 
736
 
    checkTimeStamp();
737
 
    QString sourceCode = code();
738
 
    if ( sourceCode.isEmpty() )
739
 
        return;
740
 
    QValueList<LanguageInterface::Function> functions;
741
 
    iface->functions( sourceCode, &functions );
742
 
    QString fu = MetaDataBase::normalizeFunction( function.function );
743
 
    for ( QValueList<LanguageInterface::Function>::Iterator fit = functions.begin(); fit != functions.end(); ++fit ) {
744
 
        if ( MetaDataBase::normalizeFunction( (*fit).name ) == fu ) {
745
 
            int line = 0;
746
 
            int start = 0;
747
 
            while ( line < (*fit).start - 1 ) {
748
 
                start = sourceCode.find( '\n', start );
749
 
                if ( start == -1 )
750
 
                    return;
751
 
                start++;
752
 
                line++;
753
 
            }
754
 
            if ( start == -1 )
755
 
                return;
756
 
            int end = start;
757
 
            while ( line < (*fit).end + 1 ) {
758
 
                end = sourceCode.find( '\n', end );
759
 
                if ( end == -1 ) {
760
 
                    if ( line <= (*fit).end )
761
 
                        end = sourceCode.length() - 1;
762
 
                    else
763
 
                        return;
764
 
                }
765
 
                end++;
766
 
                line++;
767
 
            }
768
 
            if ( end < start )
769
 
                return;
770
 
            sourceCode.remove( start, end - start );
771
 
            setCode( sourceCode );
772
 
        }
773
 
    }
774
 
}
775
 
 
776
 
void FormFile::functionNameChanged( const QString &oldName, const QString &newName )
777
 
{
778
 
    if ( !cod.isEmpty() ) {
779
 
        QString funcStart = QString( formWindow()->name() ) + QString( "::" );
780
 
        int i = cod.find( funcStart + oldName );
781
 
        if ( i != -1 ) {
782
 
            cod.remove( i + funcStart.length(), oldName.length() );
783
 
            cod.insert( i + funcStart.length(), newName );
784
 
        }
785
 
    }
786
 
}
787
 
 
788
 
void FormFile::functionRetTypeChanged( const QString &fuName, const QString &oldType, const QString &newType )
789
 
{
790
 
    if ( !cod.isEmpty() ) {
791
 
        QString oldFunct = oldType + " " + QString( formWindow()->name() ) + "::" + fuName;
792
 
        QString newFunct = newType + " " + QString( formWindow()->name() ) + "::" + fuName;
793
 
 
794
 
        int i = cod.find( oldFunct );
795
 
        if ( i != -1 ) {
796
 
            cod.remove( i, oldFunct.length() );
797
 
            cod.insert( i, newFunct );
798
 
        }
799
 
    }
800
 
}
801
 
 
802
 
QString FormFile::formName() const
803
 
{
804
 
    FormFile* that = (FormFile*) this;
805
 
    if ( formWindow() ) {
806
 
        that->cachedFormName = formWindow()->name();
807
 
        return cachedFormName;
808
 
    }
809
 
    if ( !cachedFormName.isNull() )
810
 
        return cachedFormName;
811
 
    QFile f( pro->makeAbsolute( filename ) );
812
 
    if ( f.open( IO_ReadOnly ) ) {
813
 
        QTextStream ts( &f );
814
 
        QString line;
815
 
        QString className;
816
 
        while ( !ts.eof() ) {
817
 
            line = ts.readLine();
818
 
            if ( !className.isEmpty() ) {
819
 
                int end = line.find( "</class>" );
820
 
                if ( end == -1 ) {
821
 
                    className += line;
822
 
                } else {
823
 
                    className += line.left( end );
824
 
                    break;
825
 
                }
826
 
                continue;
827
 
            }
828
 
            int start;
829
 
            if ( ( start = line.find( "<class>" ) ) != -1 ) {
830
 
                int end = line.find( "</class>" );
831
 
                if ( end == -1 ) {
832
 
                    className = line.mid( start + 7 );
833
 
                } else {
834
 
                    className = line.mid( start + 7, end - ( start + 7 ) );
835
 
                    break;
836
 
                }
837
 
            }
838
 
        }
839
 
        that->cachedFormName =  className;
840
 
    }
841
 
    if ( cachedFormName.isEmpty() )
842
 
        that->cachedFormName = filename;
843
 
    return cachedFormName;
844
 
}
845
 
 
846
 
void FormFile::formWindowChangedSomehow()
847
 
{
848
 
    emit somethingChanged( this );
849
 
}
850
 
 
851
 
bool FormFile::checkFileName( bool allowBreak )
852
 
{
853
 
    FormFile *ff = pro->findFormFile( filename, this );
854
 
    if ( ff )
855
 
        QMessageBox::warning( MainWindow::self, i18n( "Invalid Filename" ),
856
 
                              i18n( "The project already contains a form with a\n"
857
 
                                  "filename of '%1'. Please choose a new filename." ).arg( filename ) );
858
 
    while ( ff ) {
859
 
        QString fn;
860
 
        while ( fn.isEmpty() ) {
861
 
            fn = KFileDialog::getSaveFileName( pro->makeAbsolute( fileName() ),
862
 
                                               i18n( "*.ui|Qt User-Interface Files" ) + "\n" +
863
 
                                               i18n( "*|All Files" ), MainWindow::self,/* 0,*/
864
 
                                               i18n( "Save Form '%1' As").
865
 
                                               arg( formWindow()->name() )/*,
866
 
                                               MainWindow::self ? &MainWindow::self->lastSaveFilter : 0 */);
867
 
            if ( allowBreak && fn.isEmpty() )
868
 
                return FALSE;
869
 
        }
870
 
        filename = pro->makeRelative( fn );
871
 
        ff = pro->findFormFile( filename, this );
872
 
     }
873
 
    return TRUE;
874
 
}
875
 
 
876
 
void FormFile::addConnection( const QString &sender, const QString &signal,
877
 
                              const QString &receiver, const QString &slot )
878
 
{
879
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
880
 
    if ( iface )
881
 
        iface->addConnection( sender, signal, receiver, slot, &cod );
882
 
    if ( ed )
883
 
        ed->editorInterface()->setText( cod );
884
 
}
885
 
 
886
 
void FormFile::removeConnection( const QString &sender, const QString &signal,
887
 
                                 const QString &receiver, const QString &slot )
888
 
{
889
 
    LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
890
 
    if ( iface )
891
 
        iface->removeConnection( sender, signal, receiver, slot, &cod );
892
 
    if ( ed )
893
 
        ed->editorInterface()->setText( cod );
894
 
}
895
 
 
896
 
void FormFile::notifyFormWindowChange()
897
 
{
898
 
    if ( fw )
899
 
        pro->formOpened( fw );
900
 
}
901
 
 
902
 
void FormFile::emitNewStatus(FormFile *file)
903
 
{
904
 
    fw->mainWindow()->part()->emitNewStatus(fileName(), isModified());
905
 
}