1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "OverviewModel.h"
44 using namespace CPlusPlus;
46 OverviewModel::OverviewModel(QObject *parent)
47 : QAbstractItemModel(parent)
50 OverviewModel::~OverviewModel()
53 bool OverviewModel::hasDocument() const
58 Document::Ptr OverviewModel::document() const
63 unsigned OverviewModel::globalSymbolCount() const
67 count += _cppDocument->globalSymbolCount();
71 Symbol *OverviewModel::globalSymbolAt(unsigned index) const
72 { return _cppDocument->globalSymbolAt(index); }
74 QModelIndex OverviewModel::index(int row, int column, const QModelIndex &parent) const
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);
82 Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
83 Q_ASSERT(parentSymbol);
85 Scope *scope = parentSymbol->asScope();
87 return createIndex(row, 0, scope->memberAt(row));
91 QModelIndex OverviewModel::parent(const QModelIndex &child) const
93 Symbol *symbol = static_cast<Symbol *>(child.internalPointer());
94 if (!symbol) // account for no symbol item
97 if (Scope *scope = symbol->enclosingScope()) {
98 if (scope->enclosingScope()) {
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);
108 return QModelIndex();
111 int OverviewModel::rowCount(const QModelIndex &parent) const
114 if (!parent.isValid()) {
115 return globalSymbolCount()+1; // account for no symbol item
117 if (!parent.parent().isValid() && parent.row() == 0) // account for no symbol item
119 Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
120 Q_ASSERT(parentSymbol);
122 if (Scope *parentScope = parentSymbol->asScope()) {
123 if (!parentScope->isFunction() && !parentScope->isObjCMethod()) {
124 return parentScope->memberCount();
130 if (!parent.isValid())
131 return 1; // account for no symbol item
135 int OverviewModel::columnCount(const QModelIndex &) const
140 QVariant OverviewModel::data(const QModelIndex &index, int role) const
142 // account for no symbol item
143 if (!index.parent().isValid() && index.row() == 0) {
145 case Qt::DisplayRole:
147 return tr("<Select Symbol>");
149 return tr("<No Symbols>");
156 case Qt::DisplayRole: {
157 Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
158 QString name = _overview.prettyName(symbol->name());
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;
170 name = QLatin1String("@implementation ") + name;
172 if (clazz->isCategory())
173 name += QLatin1String(" (") + _overview.prettyName(clazz->categoryName()) + QLatin1Char(')');
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;
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(": ");
195 Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
196 QString name = _overview.prettyName(symbol->name());
198 name = QLatin1String("anonymous");
202 case Qt::DecorationRole: {
203 Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
204 return _icons.iconForSymbol(symbol);
208 Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
209 return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
212 case LineNumberRole: {
213 Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
214 return symbol->line();
222 Symbol *OverviewModel::symbolFromIndex(const QModelIndex &index) const
224 return static_cast<Symbol *>(index.internalPointer());
227 void OverviewModel::rebuild(Document::Ptr doc)