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

« back to all changes in this revision

Viewing changes to kdevdesigner/designer/connectionitems.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) 2002 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 "connectionitems.h"
28
 
#include "formwindow.h"
29
 
#include "mainwindow.h"
30
 
#include "metadatabase.h"
31
 
#include "widgetfactory.h"
32
 
#include "project.h"
33
 
#include <qpainter.h>
34
 
#include <qcombobox.h>
35
 
#include <qmetaobject.h>
36
 
#include <qcombobox.h>
37
 
#include <qlistbox.h>
38
 
#include <qaction.h>
39
 
#include <qdatabrowser.h>
40
 
 
41
 
static const char* const ignore_slots[] = {
42
 
    "destroyed()",
43
 
    "setCaption(const QString&)",
44
 
    "setIcon(const QPixmap&)",
45
 
    "setIconText(const QString&)",
46
 
    "setMouseTracking(bool)",
47
 
    "clearFocus()",
48
 
    "setUpdatesEnabled(bool)",
49
 
    "update()",
50
 
    "update(int,int,int,int)",
51
 
    "update(const QRect&)",
52
 
    "repaint()",
53
 
    "repaint(bool)",
54
 
    "repaint(int,int,int,int,bool)",
55
 
    "repaint(const QRect&,bool)",
56
 
    "repaint(const QRegion&,bool)",
57
 
    "show()",
58
 
    "hide()",
59
 
    "iconify()",
60
 
    "showMinimized()",
61
 
    "showMaximized()",
62
 
    "showFullScreen()",
63
 
    "showNormal()",
64
 
    "polish()",
65
 
    "constPolish()",
66
 
    "raise()",
67
 
    "lower()",
68
 
    "stackUnder(QWidget*)",
69
 
    "move(int,int)",
70
 
    "move(const QPoint&)",
71
 
    "resize(int,int)",
72
 
    "resize(const QSize&)",
73
 
    "setGeometry(int,int,int,int)",
74
 
    "setGeometry(const QRect&)",
75
 
    "focusProxyDestroyed()",
76
 
    "showExtension(bool)",
77
 
    "setUpLayout()",
78
 
    "showDockMenu(const QPoint&)",
79
 
    "init()",
80
 
    "destroy()",
81
 
    "deleteLater()",
82
 
    0
83
 
};
84
 
 
85
 
ConnectionItem::ConnectionItem( QTable *table, FormWindow *fw )
86
 
    : QComboTableItem( table, QStringList(), FALSE ), formWindow( fw ), conn( 0 )
87
 
{
88
 
    setReplaceable( FALSE );
89
 
}
90
 
 
91
 
void ConnectionItem::senderChanged( QObject * )
92
 
{
93
 
    emit changed();
94
 
    QWidget *w = table()->cellWidget( row(), col() );
95
 
    if ( w )
96
 
        setContentFromEditor( w );
97
 
}
98
 
 
99
 
void ConnectionItem::receiverChanged( QObject * )
100
 
{
101
 
    emit changed();
102
 
    QWidget *w = table()->cellWidget( row(), col() );
103
 
    if ( w )
104
 
        setContentFromEditor( w );
105
 
}
106
 
 
107
 
void ConnectionItem::signalChanged( const QString & )
108
 
{
109
 
    emit changed();
110
 
    QWidget *w = table()->cellWidget( row(), col() );
111
 
    if ( w )
112
 
        setContentFromEditor( w );
113
 
}
114
 
 
115
 
void ConnectionItem::slotChanged( const QString & )
116
 
{
117
 
    emit changed();
118
 
    QWidget *w = table()->cellWidget( row(), col() );
119
 
    if ( w )
120
 
        setContentFromEditor( w );
121
 
}
122
 
 
123
 
void ConnectionItem::setSender( SenderItem *i )
124
 
{
125
 
    connect( i, SIGNAL( currentSenderChanged( QObject * ) ),
126
 
             this, SLOT( senderChanged( QObject * ) ) );
127
 
}
128
 
 
129
 
void ConnectionItem::setReceiver( ReceiverItem *i )
130
 
{
131
 
    connect( i, SIGNAL( currentReceiverChanged( QObject * ) ),
132
 
             this, SLOT( receiverChanged( QObject * ) ) );
133
 
}
134
 
 
135
 
void ConnectionItem::setSignal( SignalItem *i )
136
 
{
137
 
    connect( i, SIGNAL( currentSignalChanged( const QString & ) ),
138
 
             this, SLOT( signalChanged( const QString & ) ) );
139
 
}
140
 
 
141
 
void ConnectionItem::setSlot( SlotItem *i )
142
 
{
143
 
    connect( i, SIGNAL( currentSlotChanged( const QString & ) ),
144
 
             this, SLOT( slotChanged( const QString & ) ) );
145
 
}
146
 
 
147
 
void ConnectionItem::paint( QPainter *p, const QColorGroup &cg,
148
 
                            const QRect &cr, bool selected )
149
 
{
150
 
    p->fillRect( 0, 0, cr.width(), cr.height(),
151
 
                 selected ? cg.brush( QColorGroup::Highlight )
152
 
                          : cg.brush( QColorGroup::Base ) );
153
 
 
154
 
    int w = cr.width();
155
 
    int h = cr.height();
156
 
 
157
 
    int x = 0;
158
 
 
159
 
    if ( currentText()[0] == '<' )
160
 
        p->setPen( QObject::red );
161
 
    else if ( selected )
162
 
        p->setPen( cg.highlightedText() );
163
 
    else
164
 
        p->setPen( cg.text() );
165
 
 
166
 
    QFont f( p->font() );
167
 
    QFont oldf( p->font() );
168
 
    if ( conn && conn->isModified() ) {
169
 
        f.setBold( TRUE );
170
 
        p->setFont( f );
171
 
    }
172
 
 
173
 
    p->drawText( x + 2, 0, w - x - 4, h, alignment(), currentText() );
174
 
    p->setFont( oldf );
175
 
}
176
 
 
177
 
void ConnectionItem::setConnection( ConnectionContainer *c )
178
 
{
179
 
    conn = c;
180
 
}
181
 
 
182
 
// ------------------------------------------------------------------
183
 
 
184
 
static void appendChildActions( QAction *action, QStringList &lst )
185
 
{
186
 
    QObjectListIt it( *action->children() );
187
 
    while ( it.current() ) {
188
 
        QObject *o = it.current();
189
 
        ++it;
190
 
        if ( !::qt_cast<QAction*>(o) )
191
 
            continue;
192
 
        lst << o->name();
193
 
        if ( o->children() && ::qt_cast<QActionGroup*>(o) )
194
 
            appendChildActions( (QAction*)o, lst );
195
 
    }
196
 
}
197
 
 
198
 
static QStringList flatActions( const QPtrList<QAction> &l )
199
 
{
200
 
    QStringList lst;
201
 
 
202
 
    QPtrListIterator<QAction> it( l );
203
 
    while ( it.current() ) {
204
 
        QAction *action = it.current();
205
 
        lst << action->name();
206
 
        if ( action->children() && ::qt_cast<QActionGroup*>(action) )
207
 
            appendChildActions( action, lst );
208
 
        ++it;
209
 
    }
210
 
 
211
 
    return lst;
212
 
}
213
 
 
214
 
// ------------------------------------------------------------------
215
 
 
216
 
SenderItem::SenderItem( QTable *table, FormWindow *fw )
217
 
    : ConnectionItem( table, fw )
218
 
{
219
 
    QStringList lst;
220
 
 
221
 
    QPtrDictIterator<QWidget> it( *formWindow->widgets() );
222
 
    while ( it.current() ) {
223
 
        if ( lst.find( it.current()->name() ) != lst.end() ) {
224
 
            ++it;
225
 
            continue;
226
 
        }
227
 
        if ( !QString( it.current()->name() ).startsWith( "qt_dead_widget_" ) &&
228
 
             !::qt_cast<QLayoutWidget*>(it.current()) &&
229
 
             !::qt_cast<Spacer*>(it.current()) &&
230
 
             !::qt_cast<SizeHandle*>(it.current()) &&
231
 
             qstrcmp( it.current()->name(), "central widget" ) != 0 ) {
232
 
            lst << it.current()->name();
233
 
        }
234
 
        ++it;
235
 
    }
236
 
 
237
 
    lst += flatActions( formWindow->actionList() );
238
 
 
239
 
    lst.prepend( "<No Sender>" );
240
 
    lst.sort();
241
 
    setStringList( lst );
242
 
}
243
 
 
244
 
QWidget *SenderItem::createEditor() const
245
 
{
246
 
    QComboBox *cb = (QComboBox*)ConnectionItem::createEditor();
247
 
    cb->listBox()->setMinimumWidth( cb->fontMetrics().width( "01234567890123456789012345678901234567890123456789" ) );
248
 
    connect( cb, SIGNAL( activated( const QString & ) ),
249
 
             this, SLOT( senderChanged( const QString & ) ) );
250
 
    return cb;
251
 
}
252
 
 
253
 
void SenderItem::setSenderEx( QObject *sender )
254
 
{
255
 
    setCurrentItem( sender->name() );
256
 
    emit currentSenderChanged( sender );
257
 
}
258
 
 
259
 
void SenderItem::senderChanged( const QString &sender )
260
 
{
261
 
    QObject *o = formWindow->child( sender, "QObject" );
262
 
    if ( !o )
263
 
        o = formWindow->findAction( sender );
264
 
    if ( !o )
265
 
        return;
266
 
    emit currentSenderChanged( o );
267
 
}
268
 
 
269
 
 
270
 
 
271
 
// ------------------------------------------------------------------
272
 
 
273
 
ReceiverItem::ReceiverItem( QTable *table, FormWindow *fw )
274
 
    : ConnectionItem( table, fw )
275
 
{
276
 
    QStringList lst;
277
 
 
278
 
    QPtrDictIterator<QWidget> it( *formWindow->widgets() );
279
 
    while ( it.current() ) {
280
 
        if ( lst.find( it.current()->name() ) != lst.end() ) {
281
 
            ++it;
282
 
            continue;
283
 
        }
284
 
        if ( !QString( it.current()->name() ).startsWith( "qt_dead_widget_" ) &&
285
 
             !::qt_cast<QLayoutWidget*>(it.current()) &&
286
 
             !::qt_cast<Spacer*>(it.current()) &&
287
 
             !::qt_cast<SizeHandle*>(it.current()) &&
288
 
             qstrcmp( it.current()->name(), "central widget" ) != 0 ) {
289
 
            lst << it.current()->name();
290
 
        }
291
 
        ++it;
292
 
    }
293
 
 
294
 
    lst += flatActions( formWindow->actionList() );
295
 
 
296
 
    lst.prepend( "<No Receiver>" );
297
 
    lst.sort();
298
 
    setStringList( lst );
299
 
}
300
 
 
301
 
QWidget *ReceiverItem::createEditor() const
302
 
{
303
 
    QComboBox *cb = (QComboBox*)ConnectionItem::createEditor();
304
 
    cb->listBox()->setMinimumWidth( cb->fontMetrics().width( "01234567890123456789012345678901234567890123456789" ) );
305
 
    connect( cb, SIGNAL( activated( const QString & ) ),
306
 
             this, SLOT( receiverChanged( const QString & ) ) );
307
 
    return cb;
308
 
}
309
 
 
310
 
void ReceiverItem::setReceiverEx( QObject *receiver )
311
 
{
312
 
    setCurrentItem( receiver->name() );
313
 
    emit currentReceiverChanged( receiver );
314
 
}
315
 
 
316
 
void ReceiverItem::receiverChanged( const QString &receiver )
317
 
{
318
 
    QObject *o = formWindow->child( receiver, "QObject" );
319
 
    if ( !o )
320
 
        o = formWindow->findAction( receiver );
321
 
    if ( !o )
322
 
        return;
323
 
    emit currentReceiverChanged( o );
324
 
}
325
 
 
326
 
 
327
 
 
328
 
// ------------------------------------------------------------------
329
 
 
330
 
SignalItem::SignalItem( QTable *table, FormWindow *fw )
331
 
    : ConnectionItem( table, fw )
332
 
{
333
 
    QStringList lst;
334
 
    lst << "<No Signal>";
335
 
    lst.sort();
336
 
    setStringList( lst );
337
 
}
338
 
 
339
 
void SignalItem::senderChanged( QObject *sender )
340
 
{
341
 
    QStrList sigs = sender->metaObject()->signalNames( TRUE );
342
 
    sigs.remove( "destroyed()" );
343
 
    sigs.remove( "destroyed(QObject*)" );
344
 
    sigs.remove( "accessibilityChanged(int)" );
345
 
    sigs.remove( "accessibilityChanged(int,int)" );
346
 
 
347
 
    QStringList lst = QStringList::fromStrList( sigs );
348
 
 
349
 
    if ( ::qt_cast<CustomWidget*>(sender) ) {
350
 
        MetaDataBase::CustomWidget *w = ( (CustomWidget*)sender )->customWidget();
351
 
        for ( QValueList<QCString>::Iterator it = w->lstSignals.begin();
352
 
              it != w->lstSignals.end(); ++it )
353
 
            lst << MetaDataBase::normalizeFunction( *it );
354
 
    }
355
 
 
356
 
    if ( sender == formWindow->mainContainer() ) {
357
 
        QStringList extra = MetaDataBase::signalList( formWindow );
358
 
        if ( !extra.isEmpty() )
359
 
            lst += extra;
360
 
    }
361
 
 
362
 
    lst.prepend( "<No Signal>" );
363
 
 
364
 
    lst.sort();
365
 
    setStringList( lst );
366
 
 
367
 
    ConnectionItem::senderChanged( sender );
368
 
}
369
 
 
370
 
QWidget *SignalItem::createEditor() const
371
 
{
372
 
    QComboBox *cb = (QComboBox*)ConnectionItem::createEditor();
373
 
    cb->listBox()->setMinimumWidth( cb->fontMetrics().width( "01234567890123456789012345678901234567890123456789" ) );
374
 
    connect( cb, SIGNAL( activated( const QString & ) ),
375
 
             this, SIGNAL( currentSignalChanged( const QString & ) ) );
376
 
    return cb;
377
 
}
378
 
 
379
 
// ------------------------------------------------------------------
380
 
 
381
 
SlotItem::SlotItem( QTable *table, FormWindow *fw )
382
 
    : ConnectionItem( table, fw )
383
 
{
384
 
    QStringList lst;
385
 
    lst << "<No Slot>";
386
 
    lst.sort();
387
 
    setStringList( lst );
388
 
 
389
 
    lastReceiver = 0;
390
 
    lastSignal = "<No Signal>";
391
 
}
392
 
 
393
 
void SlotItem::receiverChanged( QObject *receiver )
394
 
{
395
 
    lastReceiver = receiver;
396
 
    updateSlotList();
397
 
    ConnectionItem::receiverChanged( receiver );
398
 
}
399
 
 
400
 
void SlotItem::signalChanged( const QString &signal )
401
 
{
402
 
    lastSignal = signal;
403
 
    updateSlotList();
404
 
    ConnectionItem::signalChanged( signal );
405
 
}
406
 
 
407
 
bool SlotItem::ignoreSlot( const char* slot ) const
408
 
{
409
 
#ifndef QT_NO_SQL
410
 
    if ( qstrcmp( slot, "update()" ) == 0 &&
411
 
         ::qt_cast<QDataBrowser*>(lastReceiver) )
412
 
        return FALSE;
413
 
#endif
414
 
 
415
 
    for ( int i = 0; ignore_slots[i]; i++ ) {
416
 
        if ( qstrcmp( slot, ignore_slots[i] ) == 0 )
417
 
            return TRUE;
418
 
    }
419
 
 
420
 
    if ( !formWindow->isMainContainer( (QWidget*)lastReceiver ) ) {
421
 
        if ( qstrcmp( slot, "close()" ) == 0  )
422
 
            return TRUE;
423
 
    }
424
 
 
425
 
    if ( qstrcmp( slot, "setFocus()" ) == 0  )
426
 
        if ( lastReceiver->isWidgetType() &&
427
 
             ( (QWidget*)lastReceiver )->focusPolicy() == QWidget::NoFocus )
428
 
            return TRUE;
429
 
 
430
 
    return FALSE;
431
 
}
432
 
 
433
 
void SlotItem::updateSlotList()
434
 
{
435
 
    QStringList lst;
436
 
    if ( !lastReceiver || lastSignal == "<No Signal>" ) {
437
 
        lst << "<No Slot>";
438
 
        lst.sort();
439
 
        setStringList( lst );
440
 
        return;
441
 
    }
442
 
 
443
 
    QString signal = MetaDataBase::normalizeFunction( lastSignal );
444
 
    int n = lastReceiver->metaObject()->numSlots( TRUE );
445
 
    QStringList slts;
446
 
 
447
 
    for( int i = 0; i < n; ++i ) {
448
 
        // accept only public slots. For the form window, also accept protected slots
449
 
        const QMetaData* md = lastReceiver->metaObject()->slot( i, TRUE );
450
 
        if ( ( (lastReceiver->metaObject()->slot( i, TRUE )->access == QMetaData::Public) ||
451
 
               (formWindow->isMainContainer( (QWidget*)lastReceiver ) &&
452
 
                lastReceiver->metaObject()->slot(i, TRUE)->access ==
453
 
                QMetaData::Protected) ) &&
454
 
             !ignoreSlot( md->name ) &&
455
 
             checkConnectArgs( signal.latin1(), lastReceiver, md->name ) )
456
 
            if ( lst.find( md->name ) == lst.end() )
457
 
                lst << MetaDataBase::normalizeFunction( md->name );
458
 
    }
459
 
 
460
 
    LanguageInterface *iface =
461
 
        MetaDataBase::languageInterface( formWindow->project()->language() );
462
 
    if ( !iface || iface->supports( LanguageInterface::ConnectionsToCustomSlots ) ) {
463
 
        if ( formWindow->isMainContainer( (QWidget*)lastReceiver ) ) {
464
 
            QValueList<MetaDataBase::Function> moreSlots = MetaDataBase::slotList( formWindow );
465
 
            if ( !moreSlots.isEmpty() ) {
466
 
                for ( QValueList<MetaDataBase::Function>::Iterator it = moreSlots.begin();
467
 
                      it != moreSlots.end(); ++it ) {
468
 
                    QCString s = (*it).function;
469
 
                    if ( !s.data() )
470
 
                        continue;
471
 
                    s = MetaDataBase::normalizeFunction( s );
472
 
                    if ( checkConnectArgs( signal.latin1(), lastReceiver, s ) ) {
473
 
                        if ( lst.find( (*it).function ) == lst.end() )
474
 
                            lst << s;
475
 
                    }
476
 
                }
477
 
            }
478
 
        }
479
 
    }
480
 
 
481
 
    if ( ::qt_cast<CustomWidget*>(lastReceiver) ) {
482
 
        MetaDataBase::CustomWidget *w = ( (CustomWidget*)lastReceiver )->customWidget();
483
 
        for ( QValueList<MetaDataBase::Function>::Iterator it = w->lstSlots.begin();
484
 
              it != w->lstSlots.end(); ++it ) {
485
 
            QCString s = (*it).function;
486
 
            if ( !s.data() )
487
 
                continue;
488
 
            s = MetaDataBase::normalizeFunction( s );
489
 
            if ( checkConnectArgs( signal.latin1(), lastReceiver, s ) ) {
490
 
                if ( lst.find( (*it).function ) == lst.end() )
491
 
                    lst << s;
492
 
            }
493
 
        }
494
 
    }
495
 
 
496
 
    lst.prepend( "<No Slot>" );
497
 
    lst.sort();
498
 
    setStringList( lst );
499
 
}
500
 
 
501
 
QWidget *SlotItem::createEditor() const
502
 
{
503
 
    QComboBox *cb = (QComboBox*)ConnectionItem::createEditor();
504
 
    cb->listBox()->setMinimumWidth( cb->fontMetrics().width( "01234567890123456789012345678901234567890123456789" ) );
505
 
    connect( cb, SIGNAL( activated( const QString & ) ),
506
 
             this, SIGNAL( currentSlotChanged( const QString & ) ) );
507
 
    return cb;
508
 
}
509
 
 
510
 
void SlotItem::customSlotsChanged()
511
 
{
512
 
    QString currSlot = currentText();
513
 
    updateSlotList();
514
 
    setCurrentItem( "<No Slot>" );
515
 
    setCurrentItem( currSlot );
516
 
    emit currentSlotChanged( currentText() );
517
 
}