summaryrefslogtreecommitdiffstats
path: root/parts/classview/classtreebase.cpp
blob: f5c0f4b9b74f3ec8d3e52670d6b2618cedc38703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/***************************************************************************
 *   Copyright (C) 1999 by Jonas Nordin                                    *
 *   jonas.nordin@syncom.se                                                *
 *   Copyright (C) 2000-2001 by Bernd Gehrmann                             *
 *   bernd@kdevelop.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "classtreebase.h"

#include <qtooltip.h>
#include <qheader.h>
#include <qregexp.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kpopupmenu.h>
#include <klocale.h>
#include <kiconloader.h>

#include "kdevcore.h"
#include "kdevlanguagesupport.h"
#include "kdevmainwindow.h"
#include "kdevpartcontroller.h"
#include "classstore.h"

#include "classviewpart.h"
#include "classtooldlg.h"

KPopupMenu *ClassTreeItem::createPopup()
{
    if (!m_item || m_item->itemType() == PIT_SCOPE)
        return 0;

    KDevLanguageSupport::Features features = classTree()->m_part->languageSupport()->features();

    KPopupMenu *popup = new KPopupMenu();
    if (features & KDevLanguageSupport::Declarations)
        popup->insertItem( i18n("Go to Declaration"), classTree(), SLOT(slotGotoDeclaration()) );
    if (m_item->itemType() == PIT_METHOD)
        popup->insertItem( i18n("Go to Definition"), classTree(), SLOT(slotGotoImplementation()) );

    QString title;
    switch(m_item->itemType()) {
    case PIT_CLASS:
        {
            title = i18n("Class");
            bool hasAddMethod = features & KDevLanguageSupport::AddMethod;
            bool hasAddAttribute = features & KDevLanguageSupport::AddAttribute;
            if (hasAddMethod)
                popup->insertItem( i18n("Add Method..."), classTree(), SLOT(slotAddMethod()));
            if (hasAddAttribute)
                popup->insertItem( i18n("Add Attribute..."), classTree(), SLOT(slotAddAttribute()));
            popup->insertSeparator();
            popup->insertItem( i18n("Parent Classes..."), classTree(), SLOT(slotClassBaseClasses()));
            popup->insertItem( i18n("Child Classes..."), classTree(), SLOT(slotClassDerivedClasses()));
            popup->insertItem( i18n("Class Tool..."), classTree(), SLOT(slotClassTool()));
            }
        break;
    case PIT_STRUCT:
        title = i18n("Struct");
        break;
    case PIT_ATTRIBUTE:
        if (m_item->isGlobal())
            title = i18n("Variable");
        else
            title = i18n("Attribute");
        break;
    case PIT_METHOD:
        if (static_cast<ParsedMethod*>(m_item)->isSlot())
            title = i18n("Slot");
        else if (static_cast<ParsedMethod*>(m_item)->isSignal())
            title = i18n("Signal");
        else if (m_item->isGlobal())
            title = i18n("Function");
        else
            title = i18n("Method");
        break;
    default:
        ;
    }
    popup->insertSeparator();
    popup->insertTitle(title, -1, 0);
        
    return popup;
}


QString ClassTreeItem::scopedText() const
{
    if (m_item)
        return m_item->path();

    return QString::null;
}


void ClassTreeItem::getDeclaration(QString *toFile, int *toLine)
{
    if (m_item) {
        *toFile = m_item->declaredInFile();
        *toLine = m_item->declaredOnLine();
    }
}


void ClassTreeItem::getImplementation(QString *toFile, int *toLine)
{
    if (m_item) {
        *toFile = m_item->definedInFile();
        *toLine = m_item->definedOnLine();
    }
}


QString ClassTreeItem::text( int ) const
{
    if (m_item)
        return m_item->asString();
    return QString::null;
}


QString ClassTreeItem::tipText() const
{
    // Purposefully avoid virtual dispatch here
    return ClassTreeItem::text(0);
}


void ClassTreeOrganizerItem::init()
{
    setExpandable(true);
    setPixmap(0, SmallIcon("folder"));
}


void ClassTreeScopeItem::init()
{
    setExpandable(true);
    setPixmap(0, UserIcon("CVnamespace", KIcon::DefaultState, ClassViewFactory::instance()));
}


QString ClassTreeScopeItem::text( int col ) const
{
    if (!m_item)
        return QString::null;
    if (m_item->name().isEmpty())
        return i18n("Global");
    return ClassTreeItem::text( col );
}


void ClassTreeScopeItem::setOpen(bool o)
{
    if ( !m_item)
        return;

    kdDebug(9003) << (o? "Open scope item" : "Close scope item") << endl;
    if (o && childCount() == 0) {

        ParsedScopeContainer *pScope = static_cast<ParsedScopeContainer*>(m_item);
        ClassTreeItem *lastItem = 0;

        // Ok, this is a hack...
        KDevLanguageSupport::Features features = classTree()->m_part->languageSupport()->features();
        
        // Add namespaces
        QValueList<ParsedScopeContainer*> scopeList = pScope->getSortedScopeList();
        QValueList<ParsedScopeContainer*>::ConstIterator it;
        for (it = scopeList.begin(); it != scopeList.end(); ++it)
            lastItem = new ClassTreeScopeItem(this, lastItem, *it);

        if (features & KDevLanguageSupport::Classes) {
            // Add classes
            QValueList<ParsedClass*> classList = pScope->getSortedClassList();
            QValueList<ParsedClass*>::ConstIterator it;
            for (it = classList.begin(); it != classList.end(); ++it)
                lastItem = new ClassTreeClassItem(this, lastItem, *it);
        }
            
        if (features & KDevLanguageSupport::Structs) {
            // Add structs
            QValueList<ParsedClass*> structList = pScope->getSortedStructList();
            QValueList<ParsedClass*>::ConstIterator it;
            for (it = structList.begin(); it != structList.end(); ++it)
                lastItem = new ClassTreeClassItem(this, lastItem, *it, true);
        }

        if (features & KDevLanguageSupport::Functions) {
            // Add functions
            QValueList<ParsedMethod*> methodList = pScope->getSortedMethodList();
            QValueList<ParsedMethod*>::ConstIterator it;
            for (it = methodList.begin(); it != methodList.end(); ++it)
                lastItem = new ClassTreeMethodItem(this, lastItem, *it);
        }

        if (features & KDevLanguageSupport::Variables) {
            // Add attributes
            QValueList<ParsedAttribute*> attrList = pScope->getSortedAttributeList();
            QValueList<ParsedAttribute*>::ConstIterator it;
            for (it = attrList.begin(); it != attrList.end(); ++it)
                lastItem = new ClassTreeAttrItem(this, lastItem, *it);
        }

    }

    ClassTreeItem::setOpen(o);
}


void ClassTreeClassItem::init()
{
    setExpandable(true);
    setPixmap(0, UserIcon(m_isStruct ? "CVstruct" : "CVclass", KIcon::DefaultState, ClassViewFactory::instance()));
}


void ClassTreeClassItem::setOpen(bool o)
{
    if ( !m_item )
        return;
    kdDebug(9003) << (o? "Open class item" : "Close class item") << endl;
    if (o && childCount() == 0) {

        ParsedClass *pClass = static_cast<ParsedClass*>(m_item);
        ClassTreeItem *lastItem = 0;

        // Add nested classes
        QValueList<ParsedClass*> classList = pClass->getSortedClassList();
        QValueList<ParsedClass*>::ConstIterator classIt;
        for (classIt = classList.begin(); classIt != classList.end(); ++classIt)
            lastItem = new ClassTreeClassItem(this, lastItem, *classIt);

        // Add nested structs
        QValueList<ParsedClass*> structList = pClass->getSortedStructList();
        QValueList<ParsedClass*>::ConstIterator structIt;
        for (structIt = structList.begin(); structIt != structList.end(); ++structIt)
            lastItem = new ClassTreeClassItem(this, lastItem, *structIt, true);

        // Add methods
        QValueList<ParsedMethod*> methodList = pClass->getSortedMethodList();
        QValueList<ParsedMethod*>::ConstIterator methodIt;
        for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt)
            lastItem = new ClassTreeMethodItem(this, lastItem, *methodIt);

        // Add slots
        QValueList<ParsedMethod*> slotList = pClass->getSortedSlotList();
        QValueList<ParsedMethod*>::ConstIterator slotIt;
        for (slotIt = slotList.begin(); slotIt != slotList.end(); ++slotIt)
            lastItem = new ClassTreeMethodItem(this, lastItem, *slotIt);

        // Add signals
        QValueList<ParsedMethod*> signalList = pClass->getSortedSignalList();
        QValueList<ParsedMethod*>::ConstIterator signalIt;
        for (signalIt = signalList.begin(); signalIt != signalList.end(); ++signalIt)
            lastItem = new ClassTreeMethodItem(this, lastItem, *signalIt);

        // Add attributes
        QValueList<ParsedAttribute*> attrList = pClass->getSortedAttributeList();
        QValueList<ParsedAttribute*>::ConstIterator attrIt;
        for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt)
            lastItem = new ClassTreeAttrItem(this, lastItem, *attrIt);
        
    }
    
    ClassTreeItem::setOpen(o);
}

ClassTreeMethodItem::ClassTreeMethodItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
                                         ParsedMethod *parsedMethod)
    : ClassTreeItem(parent, lastSibling, parsedMethod)
{
    QString icon;

    if ( !parsedMethod )
      return;

    if (parsedMethod->isSignal())
        icon = "CVpublic_signal";
    else if (parsedMethod->isSlot()) {
        if (parsedMethod->isPublic())
            icon = "CVpublic_slot";
        else if (parsedMethod->isProtected())
            icon = "CVprotected_slot";
        else
            icon = "CVprivate_slot";
    }
    else if (parsedMethod->isPublic())
        icon = "CVpublic_meth";
    else if (parsedMethod->isProtected())
        icon = "CVprotected_meth";
    else if (parsedMethod->isPrivate())
        icon = "CVprivate_meth";
    else if (parsedMethod->isPackage())
        icon = "CVpackage_meth";
    else
        icon = "CVglobal_meth";

    setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
}

QString ClassTreeMethodItem::text( int ) const
{
    QString str;

    if ( !m_item )
        return QString::null;
 
    ParsedMethod* method = static_cast<ParsedMethod*>(m_item);

    str = method->name();
    
    if( method->arguments.count() > 0 ) {
        str += "( ";
        for ( ParsedArgument *arg = method->arguments.first(); arg != NULL; arg = method->arguments.next() ) {
            if ( arg != method->arguments.getFirst() )
                str += ", ";

            str += arg->toString();
        }
        str += " )";
    } else {
        str += "()";
    }
    
    if( method->isConst() )
        str += " const";

    return str;
}


ClassTreeAttrItem::ClassTreeAttrItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
                                     ParsedAttribute *parsedAttr)
    : ClassTreeItem(parent, lastSibling, parsedAttr)
{
    QString icon;
    
    if ( !parsedAttr )
      return;

    if (parsedAttr->isPublic())
        icon = "CVpublic_var";
    else if (parsedAttr->isProtected())
        icon = "CVprotected_var";
    else if (parsedAttr->isPrivate())
        icon = "CVprivate_var";
    else if (parsedAttr->isPackage())
        icon = "CVpackage_var";
    else
        icon = "CVglobal_var";

    setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
}


QString ClassTreeAttrItem::text( int ) const
{
    if ( !m_item )
        return QString::null;
    return m_item->name();
}

ClassTreeScriptItem::ClassTreeScriptItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
                                     ParsedScript *parsedScript)
    : ClassTreeItem(parent, lastSibling, parsedScript)
{
    QString icon;

    if ( !parsedScript )
      return;

    setExpandable(true);

    //need a icon for scripts
    icon = "CVpublic_var";
    setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
}


QString ClassTreeScriptItem::text( int ) const
{
    if ( !m_item )
        return QString::null;
    return m_item->name();
}

void ClassTreeScriptItem::setOpen(bool o)
{
    if ( !m_item )
        return;
    kdDebug(9003) << (o? "Open script item" : "Close script item") << endl;
    if (o && childCount() == 0) {

        ParsedScript *pClass = static_cast<ParsedScript*>(m_item);
        ClassTreeItem *lastItem = 0;

        // Add methods
        QValueList<ParsedMethod*> methodList = pClass->getSortedMethodList();
        QValueList<ParsedMethod*>::ConstIterator methodIt;
        for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt)
            lastItem = new ClassTreeMethodItem(this, lastItem, *methodIt);

        // Add attributes
        QValueList<ParsedAttribute*> attrList = pClass->getSortedAttributeList();
        QValueList<ParsedAttribute*>::ConstIterator attrIt;
        for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt)
            lastItem = new ClassTreeAttrItem(this, lastItem, *attrIt);

    }

    ClassTreeItem::setOpen(o);
}


class ClassToolTip : public QToolTip
{
public:
    ClassToolTip( QWidget *parent )
        : QToolTip(parent)
        {}

protected:
    void maybeTip(const QPoint &p);
};


void ClassToolTip::maybeTip(const QPoint &p)
{
    ClassTreeBase *ctw = static_cast<ClassTreeBase*>(parentWidget());

    QListViewItem *item = ctw->itemAt(p);
    QRect r = ctw->itemRect(item);

    if (item && r.isValid()) {
        ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
        QString str = ctitem->tipText();
        if (!str.isEmpty())
            tip(r, str);
    }
}


ClassTreeBase::ClassTreeBase(ClassViewPart *part, QWidget *parent, const char *name)
    : KListView(parent, name)
{
    setFocusPolicy(ClickFocus);
    setRootIsDecorated(true);
    setResizeMode(QListView::LastColumn);
    setSorting(-1);
    header()->hide();
    addColumn(QString::null);

    (void) new ClassToolTip(this);
    
    connect( this, SIGNAL(executed(QListViewItem*)),
             this, SLOT(slotItemExecuted(QListViewItem*)) );
    connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)),
             this, SLOT(slotItemPressed(int, QListViewItem*)) );
    connect( this, SIGNAL(returnPressed( QListViewItem*)), 
             SLOT( slotItemExecuted(QListViewItem*)) );
    connect( this, SIGNAL(contextMenuRequested(QListViewItem*, const QPoint&, int)),
             this, SLOT(slotContextMenuRequested(QListViewItem*, const QPoint&)) );

    m_part = part;
}


ClassTreeBase::~ClassTreeBase()
{}


ClassTreeBase::TreeState ClassTreeBase::treeState() const
{
    TreeState state;

    ClassTreeBase *that = const_cast<ClassTreeBase*>(this);
    QListViewItemIterator it(that);
    for (; it.current(); ++it)
        if (it.current()->isOpen()) {
            QStringList path;
            QListViewItem *item = it.current();
            while (item) {
                path.prepend(item->text(0));
                item = item->parent();
            }
            state.append(path);
        }

    return state;
}


void ClassTreeBase::setTreeState(TreeState state)
{
    TreeStateIterator tsit;
    for (tsit = state.begin(); tsit != state.end(); ++tsit) {
        QListViewItemIterator it(this);
        for (; it.current(); ++it) {
            QStringList path;
            QListViewItem *item = it.current();
            while (item) {
                path.prepend(item->text(0));
                item = item->parent();
            }
            if (*tsit == path) {
                it.current()->setOpen(true);
                break;
            }
        }
    }
}



  
void ClassTreeBase::slotItemExecuted( QListViewItem* item )
{
    if (!item)
        return;

    // toggle open state for parents
    if (item->childCount() > 0)
        setOpen(item, !isOpen(item));

    // We assume here that ALL (!) items in the list view
    // are ClassTreeItem's
    ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
    if (ctitem->isOrganizer())
        return;
        
    QString toFile;
    int toLine = -1;
    if (dynamic_cast<ClassTreeClassItem*>(item)) {
        ctitem->getDeclaration(&toFile, &toLine);
    }
    else {
	ctitem->getImplementation(&toFile, &toLine);
    }
    m_part->partController()->editDocument(toFile, toLine);
    m_part->mainWindow()->lowerView(this);
}


void ClassTreeBase::slotItemPressed(int button, QListViewItem *item)
{
    if (!item)
        return;

    // We assume here that ALL (!) items in the list view
    // are ClassTreeItem's
    ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
    if (ctitem->isOrganizer())
        return;

    if (button == MidButton) {
        QString toFile;
        int toLine = -1;
        ctitem->getDeclaration(&toFile, &toLine);
        m_part->partController()->editDocument(toFile, toLine);
        m_part->mainWindow()->lowerView(this);
    }
}

void ClassTreeBase::slotContextMenuRequested(QListViewItem *item, const QPoint &p)
{
    contextItem = static_cast<ClassTreeItem*>(item);
    
    KPopupMenu *popup = createPopup();
    popup->exec(p);
    delete popup;
}

void ClassTreeBase::slotGotoDeclaration()
{
    QString toFile;
    int toLine = -1;
    
    contextItem->getDeclaration(&toFile, &toLine);
    m_part->partController()->editDocument(toFile, toLine);
}


void ClassTreeBase::slotGotoImplementation()
{
    QString toFile;
    int toLine = -1;
    
    contextItem->getImplementation(&toFile, &toLine);
    m_part->partController()->editDocument(toFile, toLine);
}


void ClassTreeBase::slotAddMethod()
{
    if (m_part->languageSupport())
        m_part->languageSupport()->addMethod(contextItem->scopedText());
}


void ClassTreeBase::slotAddAttribute()
{
    if (m_part->languageSupport())
        m_part->languageSupport()->addAttribute(contextItem->scopedText());
}


void ClassTreeBase::slotClassBaseClasses()
{
    ClassToolDialog *dlg = new ClassToolDialog(m_part);
    dlg->setClassName(contextItem->scopedText());
    dlg->viewParents();
}


void ClassTreeBase::slotClassDerivedClasses()
{
    ClassToolDialog *dlg = new ClassToolDialog(m_part);
    dlg->setClassName(contextItem->scopedText());
    dlg->viewChildren();
}


void ClassTreeBase::slotClassTool()
{
    ClassToolDialog *dlg = new ClassToolDialog(m_part);
    dlg->setClassName(contextItem->scopedText());
    dlg->viewNone();
}

#include "classtreebase.moc"