Files
qt-creator/src/libs/cplusplus/OverviewModel.cpp

227 lines
7.1 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: http://www.qt-project.org/
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 12:01:29 +01:00
#include "OverviewModel.h"
#include "Overview.h"
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include <Scope.h>
#include <Literals.h>
#include <Symbols.h>
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
using namespace CPlusPlus;
OverviewModel::OverviewModel(QObject *parent)
: QAbstractItemModel(parent)
{ }
OverviewModel::~OverviewModel()
{ }
bool OverviewModel::hasDocument() const
2008-12-02 14:09:21 +01:00
{
return _cppDocument;
}
2008-12-02 12:01:29 +01:00
Document::Ptr OverviewModel::document() const
2008-12-02 14:09:21 +01:00
{
return _cppDocument;
}
2008-12-02 12:01:29 +01:00
unsigned OverviewModel::globalSymbolCount() const
{
unsigned count = 0;
if (_cppDocument)
count += _cppDocument->globalSymbolCount();
return count;
}
Symbol *OverviewModel::globalSymbolAt(unsigned index) const
{ return _cppDocument->globalSymbolAt(index); }
QModelIndex OverviewModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
if (row == 0) // account for no symbol item
return createIndex(row, column);
Symbol *symbol = globalSymbolAt(row-1); // account for no symbol item
2008-12-02 12:01:29 +01:00
return createIndex(row, column, symbol);
} else {
Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
Q_ASSERT(parentSymbol);
2008-12-02 12:01:29 +01:00
2010-08-11 12:26:02 +02:00
Scope *scope = parentSymbol->asScope();
Q_ASSERT(scope != 0);
return createIndex(row, 0, scope->memberAt(row));
2008-12-02 12:01:29 +01:00
}
}
QModelIndex OverviewModel::parent(const QModelIndex &child) const
{
Symbol *symbol = static_cast<Symbol *>(child.internalPointer());
if (!symbol) // account for no symbol item
return QModelIndex();
2008-12-02 12:01:29 +01:00
if (Scope *scope = symbol->enclosingScope()) {
if (scope->enclosingScope()) {
QModelIndex index;
if (scope->enclosingScope() && scope->enclosingScope()->enclosingScope()) // the parent doesn't have a parent
2010-08-11 12:26:02 +02:00
index = createIndex(scope->index(), 0, scope);
else //+1 to account for no symbol item
2010-08-11 12:26:02 +02:00
index = createIndex(scope->index() + 1, 0, scope);
return index;
}
2008-12-02 12:01:29 +01:00
}
return QModelIndex();
}
int OverviewModel::rowCount(const QModelIndex &parent) const
{
if (hasDocument()) {
if (!parent.isValid()) {
return globalSymbolCount()+1; // account for no symbol item
2008-12-02 12:01:29 +01:00
} else {
if (!parent.parent().isValid() && parent.row() == 0) // account for no symbol item
return 0;
2008-12-02 12:01:29 +01:00
Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer());
Q_ASSERT(parentSymbol);
2008-12-02 12:01:29 +01:00
2010-08-11 12:26:02 +02:00
if (Scope *parentScope = parentSymbol->asScope()) {
if (!parentScope->isFunction() && !parentScope->isObjCMethod()) {
return parentScope->memberCount();
2008-12-02 12:01:29 +01:00
}
}
return 0;
2008-12-02 12:01:29 +01:00
}
}
if (!parent.isValid())
return 1; // account for no symbol item
2008-12-02 12:01:29 +01:00
return 0;
}
int OverviewModel::columnCount(const QModelIndex &) const
2008-12-02 14:09:21 +01:00
{
return 1;
}
2008-12-02 12:01:29 +01:00
QVariant OverviewModel::data(const QModelIndex &index, int role) const
{
// account for no symbol item
if (!index.parent().isValid() && index.row() == 0) {
switch (role) {
case Qt::DisplayRole:
if (rowCount() > 1)
return tr("<Select Symbol>");
else
return tr("<No Symbols>");
default:
return QVariant();
} //switch
}
2008-12-02 12:01:29 +01:00
switch (role) {
case Qt::DisplayRole: {
Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
QString name = _overview.prettyName(symbol->name());
if (name.isEmpty())
name = QLatin1String("anonymous");
if (symbol->isObjCForwardClassDeclaration())
name = QLatin1String("@class ") + name;
if (symbol->isObjCForwardProtocolDeclaration() || symbol->isObjCProtocol())
name = QLatin1String("@protocol ") + name;
if (symbol->isObjCClass()) {
ObjCClass *clazz = symbol->asObjCClass();
if (clazz->isInterface())
name = QLatin1String("@interface ") + name;
else
name = QLatin1String("@implementation ") + name;
if (clazz->isCategory())
name += QLatin1String(" (") + _overview.prettyName(clazz->categoryName()) + QLatin1Char(')');
}
if (symbol->isObjCPropertyDeclaration())
name = QLatin1String("@property ") + name;
if (symbol->isObjCMethod()) {
ObjCMethod *method = symbol->asObjCMethod();
if (method->isStatic())
name = QLatin1Char('+') + name;
else
name = QLatin1Char('-') + name;
2010-08-12 12:35:22 +02:00
} else if (! symbol->isScope() || symbol->isFunction()) {
2008-12-02 12:01:29 +01:00
QString type = _overview.prettyType(symbol->type());
if (Function *f = symbol->type()->asFunctionType()) {
2008-12-02 12:01:29 +01:00
name += type;
type = _overview.prettyType(f->returnType());
2008-12-02 12:01:29 +01:00
}
if (! type.isEmpty())
name += QLatin1String(": ") + type;
2008-12-02 12:01:29 +01:00
}
return name;
}
case Qt::EditRole: {
Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
QString name = _overview.prettyName(symbol->name());
if (name.isEmpty())
name = QLatin1String("anonymous");
return name;
}
case Qt::DecorationRole: {
Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
return _icons.iconForSymbol(symbol);
} break;
case FileNameRole: {
Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
}
case LineNumberRole: {
Symbol *symbol = static_cast<Symbol *>(index.internalPointer());
return symbol->line();
}
default:
return QVariant();
} // switch
}
Symbol *OverviewModel::symbolFromIndex(const QModelIndex &index) const
2008-12-02 14:09:21 +01:00
{
return static_cast<Symbol *>(index.internalPointer());
}
2008-12-02 12:01:29 +01:00
void OverviewModel::rebuild(Document::Ptr doc)
{
_cppDocument = doc;
reset();
}