1
/***************************************************************************
2
* Copyright (C) 1999 by Jonas Nordin *
3
* jonas.nordin@syncom.se *
4
* Copyright (C) 2000-2001 by Bernd Gehrmann *
7
* This program is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation; either version 2 of the License, or *
10
* (at your option) any later version. *
12
***************************************************************************/
14
#include "classtreebase.h"
21
#include <kpopupmenu.h>
23
#include <kiconloader.h>
26
#include "kdevlanguagesupport.h"
27
#include "kdevmainwindow.h"
28
#include "kdevpartcontroller.h"
29
#include "classstore.h"
31
#include "classviewpart.h"
32
#include "classtooldlg.h"
34
KPopupMenu *ClassTreeItem::createPopup()
36
if (!m_item || m_item->itemType() == PIT_SCOPE)
39
KDevLanguageSupport::Features features = classTree()->m_part->languageSupport()->features();
41
KPopupMenu *popup = new KPopupMenu();
42
if (features & KDevLanguageSupport::Declarations)
43
popup->insertItem( i18n("Go to Declaration"), classTree(), SLOT(slotGotoDeclaration()) );
44
if (m_item->itemType() == PIT_METHOD)
45
popup->insertItem( i18n("Go to Definition"), classTree(), SLOT(slotGotoImplementation()) );
48
switch(m_item->itemType()) {
51
title = i18n("Class");
52
bool hasAddMethod = features & KDevLanguageSupport::AddMethod;
53
bool hasAddAttribute = features & KDevLanguageSupport::AddAttribute;
55
popup->insertItem( i18n("Add Method..."), classTree(), SLOT(slotAddMethod()));
57
popup->insertItem( i18n("Add Attribute..."), classTree(), SLOT(slotAddAttribute()));
58
popup->insertSeparator();
59
popup->insertItem( i18n("Parent Classes..."), classTree(), SLOT(slotClassBaseClasses()));
60
popup->insertItem( i18n("Child Classes..."), classTree(), SLOT(slotClassDerivedClasses()));
61
popup->insertItem( i18n("Class Tool..."), classTree(), SLOT(slotClassTool()));
65
title = i18n("Struct");
68
if (m_item->isGlobal())
69
title = i18n("Variable");
71
title = i18n("Attribute");
74
if (static_cast<ParsedMethod*>(m_item)->isSlot())
76
else if (static_cast<ParsedMethod*>(m_item)->isSignal())
77
title = i18n("Signal");
78
else if (m_item->isGlobal())
79
title = i18n("Function");
81
title = i18n("Method");
86
popup->insertSeparator();
87
popup->insertTitle(title, -1, 0);
93
QString ClassTreeItem::scopedText() const
96
return m_item->path();
102
void ClassTreeItem::getDeclaration(QString *toFile, int *toLine)
105
*toFile = m_item->declaredInFile();
106
*toLine = m_item->declaredOnLine();
111
void ClassTreeItem::getImplementation(QString *toFile, int *toLine)
114
*toFile = m_item->definedInFile();
115
*toLine = m_item->definedOnLine();
120
QString ClassTreeItem::text( int ) const
123
return m_item->asString();
124
return QString::null;
128
QString ClassTreeItem::tipText() const
130
// Purposefully avoid virtual dispatch here
131
return ClassTreeItem::text(0);
135
void ClassTreeOrganizerItem::init()
138
setPixmap(0, SmallIcon("folder"));
142
void ClassTreeScopeItem::init()
145
setPixmap(0, UserIcon("CVnamespace", KIcon::DefaultState, ClassViewFactory::instance()));
149
QString ClassTreeScopeItem::text( int col ) const
152
return QString::null;
153
if (m_item->name().isEmpty())
154
return i18n("Global");
155
return ClassTreeItem::text( col );
159
void ClassTreeScopeItem::setOpen(bool o)
164
kdDebug(9003) << (o? "Open scope item" : "Close scope item") << endl;
165
if (o && childCount() == 0) {
167
ParsedScopeContainer *pScope = static_cast<ParsedScopeContainer*>(m_item);
168
ClassTreeItem *lastItem = 0;
170
// Ok, this is a hack...
171
KDevLanguageSupport::Features features = classTree()->m_part->languageSupport()->features();
174
QValueList<ParsedScopeContainer*> scopeList = pScope->getSortedScopeList();
175
QValueList<ParsedScopeContainer*>::ConstIterator it;
176
for (it = scopeList.begin(); it != scopeList.end(); ++it)
177
lastItem = new ClassTreeScopeItem(this, lastItem, *it);
179
if (features & KDevLanguageSupport::Classes) {
181
QValueList<ParsedClass*> classList = pScope->getSortedClassList();
182
QValueList<ParsedClass*>::ConstIterator it;
183
for (it = classList.begin(); it != classList.end(); ++it)
184
lastItem = new ClassTreeClassItem(this, lastItem, *it);
187
if (features & KDevLanguageSupport::Structs) {
189
QValueList<ParsedClass*> structList = pScope->getSortedStructList();
190
QValueList<ParsedClass*>::ConstIterator it;
191
for (it = structList.begin(); it != structList.end(); ++it)
192
lastItem = new ClassTreeClassItem(this, lastItem, *it, true);
195
if (features & KDevLanguageSupport::Functions) {
197
QValueList<ParsedMethod*> methodList = pScope->getSortedMethodList();
198
QValueList<ParsedMethod*>::ConstIterator it;
199
for (it = methodList.begin(); it != methodList.end(); ++it)
200
lastItem = new ClassTreeMethodItem(this, lastItem, *it);
203
if (features & KDevLanguageSupport::Variables) {
205
QValueList<ParsedAttribute*> attrList = pScope->getSortedAttributeList();
206
QValueList<ParsedAttribute*>::ConstIterator it;
207
for (it = attrList.begin(); it != attrList.end(); ++it)
208
lastItem = new ClassTreeAttrItem(this, lastItem, *it);
213
ClassTreeItem::setOpen(o);
217
void ClassTreeClassItem::init()
220
setPixmap(0, UserIcon(m_isStruct ? "CVstruct" : "CVclass", KIcon::DefaultState, ClassViewFactory::instance()));
224
void ClassTreeClassItem::setOpen(bool o)
228
kdDebug(9003) << (o? "Open class item" : "Close class item") << endl;
229
if (o && childCount() == 0) {
231
ParsedClass *pClass = static_cast<ParsedClass*>(m_item);
232
ClassTreeItem *lastItem = 0;
234
// Add nested classes
235
QValueList<ParsedClass*> classList = pClass->getSortedClassList();
236
QValueList<ParsedClass*>::ConstIterator classIt;
237
for (classIt = classList.begin(); classIt != classList.end(); ++classIt)
238
lastItem = new ClassTreeClassItem(this, lastItem, *classIt);
240
// Add nested structs
241
QValueList<ParsedClass*> structList = pClass->getSortedStructList();
242
QValueList<ParsedClass*>::ConstIterator structIt;
243
for (structIt = structList.begin(); structIt != structList.end(); ++structIt)
244
lastItem = new ClassTreeClassItem(this, lastItem, *structIt, true);
247
QValueList<ParsedMethod*> methodList = pClass->getSortedMethodList();
248
QValueList<ParsedMethod*>::ConstIterator methodIt;
249
for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt)
250
lastItem = new ClassTreeMethodItem(this, lastItem, *methodIt);
253
QValueList<ParsedMethod*> slotList = pClass->getSortedSlotList();
254
QValueList<ParsedMethod*>::ConstIterator slotIt;
255
for (slotIt = slotList.begin(); slotIt != slotList.end(); ++slotIt)
256
lastItem = new ClassTreeMethodItem(this, lastItem, *slotIt);
259
QValueList<ParsedMethod*> signalList = pClass->getSortedSignalList();
260
QValueList<ParsedMethod*>::ConstIterator signalIt;
261
for (signalIt = signalList.begin(); signalIt != signalList.end(); ++signalIt)
262
lastItem = new ClassTreeMethodItem(this, lastItem, *signalIt);
265
QValueList<ParsedAttribute*> attrList = pClass->getSortedAttributeList();
266
QValueList<ParsedAttribute*>::ConstIterator attrIt;
267
for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt)
268
lastItem = new ClassTreeAttrItem(this, lastItem, *attrIt);
272
ClassTreeItem::setOpen(o);
275
ClassTreeMethodItem::ClassTreeMethodItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
276
ParsedMethod *parsedMethod)
277
: ClassTreeItem(parent, lastSibling, parsedMethod)
284
if (parsedMethod->isSignal())
285
icon = "CVpublic_signal";
286
else if (parsedMethod->isSlot()) {
287
if (parsedMethod->isPublic())
288
icon = "CVpublic_slot";
289
else if (parsedMethod->isProtected())
290
icon = "CVprotected_slot";
292
icon = "CVprivate_slot";
294
else if (parsedMethod->isPublic())
295
icon = "CVpublic_meth";
296
else if (parsedMethod->isProtected())
297
icon = "CVprotected_meth";
298
else if (parsedMethod->isPrivate())
299
icon = "CVprivate_meth";
300
else if (parsedMethod->isPackage())
301
icon = "CVpackage_meth";
303
icon = "CVglobal_meth";
305
setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
308
QString ClassTreeMethodItem::text( int ) const
313
return QString::null;
315
ParsedMethod* method = static_cast<ParsedMethod*>(m_item);
317
str = method->name();
319
if( method->arguments.count() > 0 ) {
321
for ( ParsedArgument *arg = method->arguments.first(); arg != NULL; arg = method->arguments.next() ) {
322
if ( arg != method->arguments.getFirst() )
325
str += arg->toString();
332
if( method->isConst() )
339
ClassTreeAttrItem::ClassTreeAttrItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
340
ParsedAttribute *parsedAttr)
341
: ClassTreeItem(parent, lastSibling, parsedAttr)
348
if (parsedAttr->isPublic())
349
icon = "CVpublic_var";
350
else if (parsedAttr->isProtected())
351
icon = "CVprotected_var";
352
else if (parsedAttr->isPrivate())
353
icon = "CVprivate_var";
354
else if (parsedAttr->isPackage())
355
icon = "CVpackage_var";
357
icon = "CVglobal_var";
359
setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
363
QString ClassTreeAttrItem::text( int ) const
366
return QString::null;
367
return m_item->name();
370
ClassTreeScriptItem::ClassTreeScriptItem(ClassTreeItem *parent, ClassTreeItem *lastSibling,
371
ParsedScript *parsedScript)
372
: ClassTreeItem(parent, lastSibling, parsedScript)
381
//need a icon for scripts
382
icon = "CVpublic_var";
383
setPixmap(0, UserIcon(icon, KIcon::DefaultState, ClassViewFactory::instance()));
387
QString ClassTreeScriptItem::text( int ) const
390
return QString::null;
391
return m_item->name();
394
void ClassTreeScriptItem::setOpen(bool o)
398
kdDebug(9003) << (o? "Open script item" : "Close script item") << endl;
399
if (o && childCount() == 0) {
401
ParsedScript *pClass = static_cast<ParsedScript*>(m_item);
402
ClassTreeItem *lastItem = 0;
405
QValueList<ParsedMethod*> methodList = pClass->getSortedMethodList();
406
QValueList<ParsedMethod*>::ConstIterator methodIt;
407
for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt)
408
lastItem = new ClassTreeMethodItem(this, lastItem, *methodIt);
411
QValueList<ParsedAttribute*> attrList = pClass->getSortedAttributeList();
412
QValueList<ParsedAttribute*>::ConstIterator attrIt;
413
for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt)
414
lastItem = new ClassTreeAttrItem(this, lastItem, *attrIt);
418
ClassTreeItem::setOpen(o);
422
class ClassToolTip : public QToolTip
425
ClassToolTip( QWidget *parent )
430
void maybeTip(const QPoint &p);
434
void ClassToolTip::maybeTip(const QPoint &p)
436
ClassTreeBase *ctw = static_cast<ClassTreeBase*>(parentWidget());
438
QListViewItem *item = ctw->itemAt(p);
439
QRect r = ctw->itemRect(item);
441
if (item && r.isValid()) {
442
ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
443
QString str = ctitem->tipText();
450
ClassTreeBase::ClassTreeBase(ClassViewPart *part, QWidget *parent, const char *name)
451
: KListView(parent, name)
453
setFocusPolicy(ClickFocus);
454
setRootIsDecorated(true);
455
setResizeMode(QListView::LastColumn);
458
addColumn(QString::null);
460
(void) new ClassToolTip(this);
462
connect( this, SIGNAL(executed(QListViewItem*)),
463
this, SLOT(slotItemExecuted(QListViewItem*)) );
464
connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)),
465
this, SLOT(slotItemPressed(int, QListViewItem*)) );
466
connect( this, SIGNAL(returnPressed( QListViewItem*)),
467
SLOT( slotItemExecuted(QListViewItem*)) );
468
connect( this, SIGNAL(contextMenuRequested(QListViewItem*, const QPoint&, int)),
469
this, SLOT(slotContextMenuRequested(QListViewItem*, const QPoint&)) );
475
ClassTreeBase::~ClassTreeBase()
479
ClassTreeBase::TreeState ClassTreeBase::treeState() const
483
ClassTreeBase *that = const_cast<ClassTreeBase*>(this);
484
QListViewItemIterator it(that);
485
for (; it.current(); ++it)
486
if (it.current()->isOpen()) {
488
QListViewItem *item = it.current();
490
path.prepend(item->text(0));
491
item = item->parent();
500
void ClassTreeBase::setTreeState(TreeState state)
502
TreeStateIterator tsit;
503
for (tsit = state.begin(); tsit != state.end(); ++tsit) {
504
QListViewItemIterator it(this);
505
for (; it.current(); ++it) {
507
QListViewItem *item = it.current();
509
path.prepend(item->text(0));
510
item = item->parent();
513
it.current()->setOpen(true);
523
void ClassTreeBase::slotItemExecuted( QListViewItem* item )
528
// toggle open state for parents
529
if (item->childCount() > 0)
530
setOpen(item, !isOpen(item));
532
// We assume here that ALL (!) items in the list view
533
// are ClassTreeItem's
534
ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
535
if (ctitem->isOrganizer())
540
if (dynamic_cast<ClassTreeClassItem*>(item)) {
541
ctitem->getDeclaration(&toFile, &toLine);
544
ctitem->getImplementation(&toFile, &toLine);
546
m_part->partController()->editDocument(toFile, toLine);
547
m_part->mainWindow()->lowerView(this);
551
void ClassTreeBase::slotItemPressed(int button, QListViewItem *item)
556
// We assume here that ALL (!) items in the list view
557
// are ClassTreeItem's
558
ClassTreeItem *ctitem = static_cast<ClassTreeItem*>(item);
559
if (ctitem->isOrganizer())
562
if (button == MidButton) {
565
ctitem->getDeclaration(&toFile, &toLine);
566
m_part->partController()->editDocument(toFile, toLine);
567
m_part->mainWindow()->lowerView(this);
571
void ClassTreeBase::slotContextMenuRequested(QListViewItem *item, const QPoint &p)
573
contextItem = static_cast<ClassTreeItem*>(item);
575
KPopupMenu *popup = createPopup();
580
void ClassTreeBase::slotGotoDeclaration()
585
contextItem->getDeclaration(&toFile, &toLine);
586
m_part->partController()->editDocument(toFile, toLine);
590
void ClassTreeBase::slotGotoImplementation()
595
contextItem->getImplementation(&toFile, &toLine);
596
m_part->partController()->editDocument(toFile, toLine);
600
void ClassTreeBase::slotAddMethod()
602
if (m_part->languageSupport())
603
m_part->languageSupport()->addMethod(contextItem->scopedText());
607
void ClassTreeBase::slotAddAttribute()
609
if (m_part->languageSupport())
610
m_part->languageSupport()->addAttribute(contextItem->scopedText());
614
void ClassTreeBase::slotClassBaseClasses()
616
ClassToolDialog *dlg = new ClassToolDialog(m_part);
617
dlg->setClassName(contextItem->scopedText());
622
void ClassTreeBase::slotClassDerivedClasses()
624
ClassToolDialog *dlg = new ClassToolDialog(m_part);
625
dlg->setClassName(contextItem->scopedText());
630
void ClassTreeBase::slotClassTool()
632
ClassToolDialog *dlg = new ClassToolDialog(m_part);
633
dlg->setClassName(contextItem->scopedText());
637
#include "classtreebase.moc"