OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / cplusplus / OverviewModel.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "OverviewModel.h"
35 #include "Overview.h"
36
37 #include <Scope.h>
38 #include <Literals.h>
39 #include <Symbols.h>
40
41 #include <QFile>
42 #include <QtDebug>
43
44 using namespace CPlusPlus;
45
46 OverviewModel::OverviewModel(QObject *parent)
47     : QAbstractItemModel(parent)
48 { }
49
50 OverviewModel::~OverviewModel()
51 { }
52
53 bool OverviewModel::hasDocument() const
54 {
55     return _cppDocument;
56 }
57
58 Document::Ptr OverviewModel::document() const
59 {
60     return _cppDocument;
61 }
62
63 unsigned OverviewModel::globalSymbolCount() const
64 {
65     unsigned count = 0;
66     if (_cppDocument)
67         count += _cppDocument->globalSymbolCount();
68     return count;
69 }
70
71 Symbol *OverviewModel::globalSymbolAt(unsigned index) const
72 { return _cppDocument->globalSymbolAt(index); }
73
74 QModelIndex OverviewModel::index(int row, int column, const QModelIndex &parent) const
75 {
76     if (!parent.isValid()) {
77         if (row == 0) // account for no symbol item
78             return createIndex(row, column);
79         Symbol *symbol = globalSymbolAt(row-1); // account for no symbol item
80         return createIndex(row, column, symbol);
81     } else {
82         Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
83         Q_ASSERT(parentSymbol);
84
85         Scope *scope = parentSymbol->asScope();
86         Q_ASSERT(scope != 0);
87         return createIndex(row, 0, scope->memberAt(row));
88     }
89 }
90
91 QModelIndex OverviewModel::parent(const QModelIndex &child) const
92 {
93     Symbol *symbol = static_cast<Symbol *>(child.internalPointer());
94     if (!symbol) // account for no symbol item
95         return QModelIndex();
96
97     if (Scope *scope = symbol->enclosingScope()) {
98         if (scope->enclosingScope()) {
99             QModelIndex index;
100             if (scope->enclosingScope() && scope->enclosingScope()->enclosingScope()) // the parent doesn't have a parent
101                 index = createIndex(scope->index(), 0, scope);
102             else //+1 to account for no symbol item
103                 index = createIndex(scope->index() + 1, 0, scope);
104             return index;
105         }
106     }
107
108     return QModelIndex();
109 }
110
111 int OverviewModel::rowCount(const QModelIndex &parent) const
112 {
113     if (hasDocument()) {
114         if (!parent.isValid()) {
115             return globalSymbolCount()+1; // account for no symbol item
116         } else {
117             if (!parent.parent().isValid() && parent.row() == 0) // account for no symbol item
118                 return 0;
119             Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
120             Q_ASSERT(parentSymbol);
121
122             if (Scope *parentScope = parentSymbol->asScope()) {
123                 if (!parentScope->isFunction() && !parentScope->isObjCMethod()) {
124                     return parentScope->memberCount();
125                 }
126             }
127             return 0;
128         }
129     }
130     if (!parent.isValid())
131         return 1; // account for no symbol item
132     return 0;
133 }
134
135 int OverviewModel::columnCount(const QModelIndex &) const
136 {
137     return 1;
138 }
139
140 QVariant OverviewModel::data(const QModelIndex &index, int role) const
141 {
142     // account for no symbol item
143     if (!index.parent().isValid() && index.row() == 0) {
144         switch (role) {
145         case Qt::DisplayRole:
146             if (rowCount() > 1)
147                 return tr("<Select Symbol>");
148             else
149                 return tr("<No Symbols>");
150         default:
151             return QVariant();
152         } //switch
153     }
154
155     switch (role) {
156     case Qt::DisplayRole: {
157         Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
158         QString name = _overview.prettyName(symbol->name());
159         if (name.isEmpty())
160             name = QLatin1String("anonymous");
161         if (symbol->isObjCForwardClassDeclaration())
162             name = QLatin1String("@class ") + name;
163         if (symbol->isObjCForwardProtocolDeclaration() || symbol->isObjCProtocol())
164             name = QLatin1String("@protocol ") + name;
165         if (symbol->isObjCClass()) {
166             ObjCClass *clazz = symbol->asObjCClass();
167             if (clazz->isInterface())
168                 name = QLatin1String("@interface ") + name;
169             else
170                 name = QLatin1String("@implementation ") + name;
171
172             if (clazz->isCategory())
173                 name += QLatin1String(" (") + _overview.prettyName(clazz->categoryName()) + QLatin1Char(')');
174         }
175         if (symbol->isObjCPropertyDeclaration())
176             name = QLatin1String("@property ") + name;
177         if (symbol->isObjCMethod()) {
178             ObjCMethod *method = symbol->asObjCMethod();
179             if (method->isStatic())
180                 name = QLatin1Char('+') + name;
181             else
182                 name = QLatin1Char('-') + name;
183         } else if (! symbol->isScope() || symbol->isFunction()) {
184             QString type = _overview.prettyType(symbol->type());
185             if (! type.isEmpty()) {
186                 if (! symbol->type()->isFunctionType())
187                     name += QLatin1String(": ");
188                 name += type;
189             }
190         }
191         return name;
192     }
193
194     case Qt::EditRole: {
195         Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
196         QString name = _overview.prettyName(symbol->name());
197         if (name.isEmpty())
198             name = QLatin1String("anonymous");
199         return name;
200     }
201
202     case Qt::DecorationRole: {
203         Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
204         return _icons.iconForSymbol(symbol);
205     } break;
206
207     case FileNameRole: {
208         Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
209         return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
210     }
211
212     case LineNumberRole: {
213         Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
214         return symbol->line();
215     }
216
217     default:
218         return QVariant();
219     } // switch
220 }
221
222 Symbol *OverviewModel::symbolFromIndex(const QModelIndex &index) const
223 {
224     return static_cast<Symbol *>(index.internalPointer());
225 }
226
227 void OverviewModel::rebuild(Document::Ptr doc)
228 {
229     _cppDocument = doc;
230     reset();
231 }