summaryrefslogtreecommitdiffstats
path: root/parts/quickopen/quickopenclassdialog.cpp
blob: 37987166e1fc330359c76cc4890a6b75d0b6ab9d (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
/*
 *  Copyright (C) 2003 Roberto Raggi ([email protected])
 *
 *  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 <kdevproject.h>
#include <kdevpartcontroller.h>
#include <kdevlanguagesupport.h>

#include <tdelistbox.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <kcompletion.h>
#include <klineedit.h>

#include <tqregexp.h>
#include <tqlabel.h>

#include "quickopenclassdialog.h"
#include "quickopen_part.h"
#include "quickopenfunctionchooseform.h"

QuickOpenClassDialog::QuickOpenClassDialog(QuickOpenPart* part, TQWidget* parent, const char* name, bool modal, WFlags fl)
    : QuickOpenDialog( part, parent, name, modal, fl )
{
    nameLabel->setText( i18n("Class &name:") );
    itemListLabel->setText( i18n("Class &list:") );

    findAllClasses( m_items );
    TQStringList_unique( m_items );

    nameEdit->setFocus();

    itemList->insertStringList( m_items );
    itemList->setCurrentItem(0);
}

QuickOpenClassDialog::~QuickOpenClassDialog()
{
}

void QuickOpenClassDialog::slotExecuted( TQListBoxItem* /*item*/ )
{
    accept();
}

void QuickOpenClassDialog::accept()
{
    if( TQListBoxItem* item = itemList->selectedItem() )
    {
        ClassList klasses = findClass( item->text() );
        if( klasses.count() == 1 )
        {
            ClassDom klass = klasses.first();
            int startLine, startColumn;
            klass->getStartPosition( &startLine, &startColumn );
            m_part->partController()->editDocument( KURL( klass->fileName() ), startLine );
            selectClassViewItem( ItemDom(&(*klass)) );
        }
        else if (klasses.count() > 1 )
        {
            //several classes with the same name found
            TQString fileStr;

            QuickOpenFunctionChooseForm fdlg( this, "" );
            fdlg.setCaption(i18n("Select The Location of Class %1").arg(klasses.first()->name()));
            fdlg.textLabel2->setText(i18n("Class name:"));

            for( ClassList::const_iterator it = klasses.constBegin(); it != klasses.constEnd() ; ++it )
            {
                ClassDom klass = *it;
                //assemble class name to display (maybe with scope info and specialization)
                TQString classStr = m_part->languageSupport()->formatModelItem(klass);
                if(klass->hasSpecializationDeclaration())
                  classStr += klass->getSpecializationDeclaration();
                if(!klass->scope().isEmpty())
                  classStr += "   (in " + klass->scope().join("::") + ")";
                fdlg.argBox->insertItem(classStr);

                fileStr = KURL( klass->fileName() ).fileName();
                KURL full_url( klass->fileName() );
                KURL base_url( m_part->project()->projectDirectory()+"/" );
                fdlg.fileBox->insertItem(fileStr);
                fdlg.setRelativePath(fdlg.fileBox->count()-1,
                    KURL::relativeURL( base_url, full_url ));
            }
            if( fdlg.exec() ){
                int id = fdlg.argBox->currentItem();
                if( id>-1 && id < (int) klasses.count() ){
                    ClassDom model = klasses[id];
                    int line, col;
                    model->getStartPosition( &line, &col );
                    selectClassViewItem( ItemDom(&(*model)) );
                    TQString fileNameStr = model->fileName();
                    m_part->partController()->editDocument( KURL(fileNameStr), line );
                }
            }
        }
    }

    TQDialog::accept();
}

void QuickOpenClassDialog::slotReturnPressed( )
{
    maybeUpdateSelection();
    accept();
}

void QuickOpenClassDialog::findAllClasses( TQStringList& lst )
{
    findAllClasses( lst, m_part->codeModel()->globalNamespace() );
}

void QuickOpenClassDialog::findAllClasses( TQStringList& lst, const ClassDom klass )
{
    TQStringList fullName = klass->scope();
    fullName << klass->name();
    lst << fullName.join( "::" );

    const ClassList classList = klass->classList();
    for( ClassList::ConstIterator it=classList.begin(); it!=classList.end(); ++it )
        findAllClasses( lst, *it );
}

void QuickOpenClassDialog::findAllClasses( TQStringList& lst, const NamespaceDom ns )
{
    const NamespaceList namespaceList = ns->namespaceList();
    for( NamespaceList::ConstIterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
        findAllClasses( lst, *it );

    const ClassList classList = ns->classList();
    for( ClassList::ConstIterator it=classList.begin(); it!=classList.end(); ++it )
        findAllClasses( lst, *it );
}

ClassList QuickOpenClassDialog::findClass( const TQString& name )
{
    TQStringList path = TQStringList::split( "::", name );
    return findClass( path, m_part->codeModel()->globalNamespace() );
}

ClassList QuickOpenClassDialog::findClass( TQStringList& path, const NamespaceDom ns )
{
    ClassList list;
    if( path.isEmpty() )
        return list;

    TQString current = path.front();
    if( ns->hasNamespace(current) )
    {
        path.pop_front();
        list += findClass( path, ns->namespaceByName(current) );
        path.push_front( current );
    }

    if( ns->hasClass(current) )
    {
        path.pop_front();
        list += findClass( path, ns->classByName(current) );
    }

    return list;
}

ClassList QuickOpenClassDialog::findClass( TQStringList& path, const ClassList klasses )
{
    ClassList list;
    if( path.isEmpty() )
    {
        list += klasses;
        return list;
    }

    for (ClassList::const_iterator it = klasses.constBegin(); it != klasses.constEnd(); ++it)
    {
        list += findClass(path, *it);
    }

    return list;
}

ClassList QuickOpenClassDialog::findClass( TQStringList &path, const ClassDom klass )
{
    ClassList list;
    if( path.isEmpty() )
    {
        list << klass;
        return list;
    }

    TQString current = path.front();
    if( klass->hasClass(current) )
    {
        path.pop_front();
        list += findClass( path, klass->classByName(current) );
        path.push_front(current);
    }

    return list;
}

#include "quickopenclassdialog.moc"