summaryrefslogtreecommitdiffstats
path: root/parts/classview/classviewpart.cpp
blob: 200ef9bb38f110c3c2cd6bd2510dc446113e6377 (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
/*
 *  Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
 *  Copyright (C) 2003 Alexander Dymo (cloudtemple@mksat.net)
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include <tqwhatsthis.h>
#include <tqlistview.h>
#include <tqfileinfo.h>
#include <tqlineedit.h>

#include <kiconloader.h>
#include <klocale.h>
#include <kdevgenericfactory.h>
#include <kpopupmenu.h>
#include <kdebug.h>
#include <kmimetype.h>

#include <kdevcore.h>
#include <kdevmainwindow.h>
#include <kdevlanguagesupport.h>
#include <kcomboview.h>
#include <kdevpartcontroller.h>
#include <kdevproject.h>
#include <urlutil.h>
#include <kdevplugininfo.h>

#include <codemodel.h>
#include <codemodel_utils.h>

#include "classviewwidget.h"
#include "classviewpart.h"
#include "hierarchydlg.h"
#include "navigator.h"

#include "klistviewaction.h"

#include <ktexteditor/document.h>
#include <ktexteditor/editinterface.h>
#include <ktexteditor/view.h>
#include <ktexteditor/selectioninterface.h>
#include <ktexteditor/viewcursorinterface.h>
#include <ktexteditor/clipboardinterface.h>


class FunctionCompletion : public CustomCompleter {
    public:
        FunctionCompletion() {
					setOrder( Insertion ); 
        };
        typedef TQMap<TQString, TQString> FuncMap;
        FuncMap nameMap; /// real ->  short
        FuncMap revNameMap; /// short -> real
    
        static const TQString processName( TQString function ) {
            TQString args;
            TQString fScope;
            int cutpos;
    
            if((cutpos = function.find('(')) != -1) {
                args = function.right( function.length() - cutpos );
                function = function.left( cutpos );
            } else {
                return function;
            }
            if((cutpos = function.findRev(':')) != -1 || (cutpos = function.findRev('.')) != -1) {
                fScope = function.left( cutpos + 1 );
                function = function.right( function.length() - cutpos - 1);
            }

            return function;
        }
    
        virtual void addItem ( const TQString &item ) {
            TQString newItemName = item;
            KCompletion::addItem(newItemName);
            TQString tx = processName( item );
            tx += "  \"" + item + "\"";
            nameMap[item] = tx;
            revNameMap[tx] = item;
            if(tx.length() == 0) {
                kdDebug() << "function-name-extraction failed with \"" << item << "\"" << endl;
            }else{
                KCompletion::addItem( tx );
                kdDebug() << "addding \"" << item << "\" as \"" << tx << "\"" << endl;
            }
            KCompletion::addItem( item );
        }
    
        virtual void removeItem ( const TQString &item ) {
            FuncMap::iterator it = nameMap.find( item );
            if( it != nameMap.end() ) {
                KCompletion::removeItem( *it );
                revNameMap.remove( *it );
                nameMap.remove( it );
            }
        }
    
        virtual void clear() {
            nameMap.clear();
            revNameMap.clear();
            KCompletion::clear();
        }
    
        virtual void postProcessMatch ( TQString *match ) const  {
            FuncMap::const_iterator it = revNameMap.find( *match );
            if( it != revNameMap.end() ) {
                *match = *it;
            }
        };
    
        virtual void   postProcessMatches ( TQStringList *matches ) const {
            TQStringList::iterator it = matches->begin();
            
            while( it != matches->end() ) {
                postProcessMatch( &(*it) );
                ++it;
            }
        }
    
        virtual void   postProcessMatches ( KCompletionMatches *matches ) const {
        }
};


typedef KDevGenericFactory<ClassViewPart> ClassViewFactory;
static const KDevPluginInfo data("kdevclassview");
K_EXPORT_COMPONENT_FACTORY( libkdevclassview, ClassViewFactory( data ) )

ClassViewPart::ClassViewPart(TQObject *tqparent, const char *name, const TQStringList& )
    :/// KDevPlugin( &data, tqparent, name ? name : "ClassViewPart" ),
    KDevCodeBrowserFrontend( &data, tqparent, name ? name : "ClassViewPart" ),
    m_activeDocument(0), m_activeView(0), m_activeSelection(0), m_activeEditor(0), m_activeViewCursor(0), m_hierarchyDlg(0)
{
    setInstance(ClassViewFactory::instance());
    setXMLFile("kdevclassview.rc");
    
    navigator = new Navigator(this);

    setupActions();

    m_widget = new ClassViewWidget(this);
    m_widget->setIcon( SmallIcon("view_tree") );
    m_widget->setCaption(i18n("Class Browser"));
    mainWindow()->embedSelectView( m_widget, i18n("Classes"), i18n("Class browser") );
    TQWhatsThis::add(m_widget, i18n("<b>Class browser</b><p>"
            "The class browser shows all namespaces, classes and namespace and class members in a project."));

    connect( core(), TQT_SIGNAL(projectOpened()), this, TQT_SLOT(slotProjectOpened()) );
    connect( core(), TQT_SIGNAL(projectClosed()), this, TQT_SLOT(slotProjectClosed()) );
    connect( core(), TQT_SIGNAL(languageChanged()), this, TQT_SLOT(slotProjectOpened()) );
    connect( partController(), TQT_SIGNAL(activePartChanged(KParts::Part*)),
        this, TQT_SLOT(activePartChanged(KParts::Part*)));
}

bool ClassViewPart::jumpedToItem( ItemDom item ) {
    if(!m_widget) return false;
    return m_widget->selectItem(item);
}

ClassViewPart::~ClassViewPart()
{
    mainWindow()->removeView( m_widget );
    delete (ClassViewWidget*) m_widget;
    delete m_hierarchyDlg;
}

void ClassViewPart::slotProjectOpened( )
{
    connect( languageSupport(), TQT_SIGNAL(updatedSourceInfo()), navigator, TQT_SLOT(refresh()) );
    connect( languageSupport(), TQT_SIGNAL(addedSourceInfo(const TQString& )), navigator, TQT_SLOT(addFile(const TQString& )));
    navigator->refresh();
}

void ClassViewPart::slotProjectClosed( )
{
//    navigator->refresh();
//    disconnect( languageSupport(), TQT_SIGNAL(updatedSourceInfo()), navigator, TQT_SLOT(refresh()) );
}

void ClassViewPart::setupActions( )
{
    m_functionsnav = new KListViewAction( new KComboView(true, 150, 0, "m_functionsnav_combo", new FunctionCompletion() ), i18n("Functions Navigation"), 0, 0, 0, actionCollection(), "functionsnav_combo", true );
    connect(m_functionsnav->view(), TQT_SIGNAL(activated(TQListViewItem*)), navigator, TQT_SLOT(selectFunctionNav(TQListViewItem*)));
//    m_functionsnav->view()->setEditable(false);
    connect(m_functionsnav->view(), TQT_SIGNAL(focusGranted()), navigator, TQT_SLOT(functionNavFocused()));
    connect(m_functionsnav->view(), TQT_SIGNAL(focusLost()), navigator, TQT_SLOT(functionNavUnFocused()));
    m_functionsnav->setToolTip(i18n("Functions in file"));
    m_functionsnav->setWhatsThis(i18n("<b>Function navigator</b><p>Navigates over functions contained in the file."));
//    m_functionsnav->view()->setCurrentText(NAV_NODEFINITION);
    m_functionsnav->view()->setDefaultText(NAV_NODEFINITION);

    new KAction( i18n("Focus Navigator"), 0, this, TQT_SLOT(slotFocusNavbar()), actionCollection(), "focus_navigator" );

    if (langHasFeature(KDevLanguageSupport::Classes))
    {
        KAction *ac = new KAction(i18n("Class Inheritance Diagram"), "view_tree", 0, this, TQT_SLOT(graphicalClassView()), actionCollection(), "inheritance_dia");
        ac->setToolTip(i18n("Class inheritance diagram"));
        ac->setWhatsThis(i18n("<b>Class inheritance diagram</b><p>Displays inheritance relationship between classes in project. "
                                 "Note, it does not display classes outside inheritance hierarchy."));    
    }
}

bool ClassViewPart::langHasFeature(KDevLanguageSupport::Features feature)
{
    bool result = false;
    if (languageSupport())
        result = (feature & languageSupport()->features());
    return result;
}

void ClassViewPart::graphicalClassView( )
{
    if( !m_hierarchyDlg )
        m_hierarchyDlg = new HierarchyDialog(this);
    m_hierarchyDlg->refresh();
    m_hierarchyDlg->show();
}

void ClassViewPart::refresh() {
	if( navigator )
			navigator->refresh();
}

void ClassViewPart::activePartChanged( KParts::Part * part)
{
    navigator->stopTimer();
    if (m_activeView)
    {
        disconnect(m_activeView, TQT_SIGNAL(cursorPositionChanged()), 
            navigator, TQT_SLOT(slotCursorPositionChanged()));
    }
    
    kdDebug() << "ClassViewPart::activePartChanged()" << endl;

    m_activeDocument = dynamic_cast<KTextEditor::Document*>( part );
    m_activeView = part ? dynamic_cast<KTextEditor::View*>( part->widget() ) : 0;
    m_activeEditor = dynamic_cast<KTextEditor::EditInterface*>( part );
    m_activeSelection = dynamic_cast<KTextEditor::SelectionInterface*>( part );
    m_activeViewCursor = part ? dynamic_cast<KTextEditor::ViewCursorInterface*>( m_activeView ) : 0;

    m_activeFileName = TQString();

    if (m_activeDocument)
    {
        m_activeFileName = URLUtil::canonicalPath( m_activeDocument->url().path() );
        navigator->refreshNavBars(m_activeFileName);
        navigator->syncFunctionNavDelayed(200);
/*        if ( languageSupport()->mimeTypes().find(
            KMimeType::findByPath(m_activeFileName)) != languageSupport()->mimeTypes().end() )
            m_activeFileName = TQString();*/
    }
    if( m_activeViewCursor )
    {
        connect(m_activeView, TQT_SIGNAL(cursorPositionChanged()),
            navigator, TQT_SLOT(slotCursorPositionChanged()) );
    }
}
void ClassViewPart::slotFocusNavbar()
{
    m_functionsnav->view()->setFocus();
}

#include "classviewpart.moc"