diff options
author | Ivan Donchevskii <[email protected]> | 2018-02-01 15:47:22 +0100 |
---|---|---|
committer | Ivan Donchevskii <[email protected]> | 2018-02-09 12:03:18 +0000 |
commit | c6d4308ccd5819602864eb128c2ba4b5e31e6511 (patch) | |
tree | 6f73949859933d3478293c55bc94153c40cde5be /src/libs/cplusplus | |
parent | 9a7e7f7d4268b833c490c37327b266574e0735e9 (diff) |
CPlusPlus: CppEditor: refactor overview model
Introduce abstract model to be able to use clang
based version of it in follow up patches.
Fix warnings and modernize source code a little.
Move OverviewModel to CppTools.
Change-Id: Idcc9bf03cad047026a456bd01063597a1eb95147
Reviewed-by: Nikolai Kosjar <[email protected]>
Diffstat (limited to 'src/libs/cplusplus')
-rw-r--r-- | src/libs/cplusplus/OverviewModel.cpp | 280 | ||||
-rw-r--r-- | src/libs/cplusplus/OverviewModel.h | 75 | ||||
-rw-r--r-- | src/libs/cplusplus/cplusplus-lib.pri | 6 | ||||
-rw-r--r-- | src/libs/cplusplus/cplusplus.qbs | 1 |
4 files changed, 2 insertions, 360 deletions
diff --git a/src/libs/cplusplus/OverviewModel.cpp b/src/libs/cplusplus/OverviewModel.cpp deleted file mode 100644 index 7b3649ed03c..00000000000 --- a/src/libs/cplusplus/OverviewModel.cpp +++ /dev/null @@ -1,280 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -****************************************************************************/ - -#include "OverviewModel.h" - -#include "Overview.h" - -#include <cplusplus/Icons.h> -#include <cplusplus/Scope.h> -#include <cplusplus/Literals.h> -#include <cplusplus/Symbols.h> -#include <utils/dropsupport.h> - -using namespace CPlusPlus; - -OverviewModel::OverviewModel(QObject *parent) - : QAbstractItemModel(parent) -{ } - -OverviewModel::~OverviewModel() -{ } - -bool OverviewModel::hasDocument() const -{ - return _cppDocument; -} - -Document::Ptr OverviewModel::document() const -{ - return _cppDocument; -} - -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 - return createIndex(row, column, symbol); - } else { - Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer()); - Q_ASSERT(parentSymbol); - - if (Template *t = parentSymbol->asTemplate()) - if (Symbol *templateParentSymbol = t->declaration()) - parentSymbol = templateParentSymbol; - - Scope *scope = parentSymbol->asScope(); - Q_ASSERT(scope != 0); - return createIndex(row, 0, scope->memberAt(row)); - } -} - -QModelIndex OverviewModel::parent(const QModelIndex &child) const -{ - Symbol *symbol = static_cast<Symbol *>(child.internalPointer()); - if (!symbol) // account for no symbol item - return QModelIndex(); - - if (Scope *scope = symbol->enclosingScope()) { - if (scope->isTemplate() && scope->enclosingScope()) - scope = scope->enclosingScope(); - if (scope->enclosingScope()) { - QModelIndex index; - if (scope->enclosingScope() && scope->enclosingScope()->enclosingScope()) // the parent doesn't have a parent - index = createIndex(scope->index(), 0, scope); - else //+1 to account for no symbol item - index = createIndex(scope->index() + 1, 0, scope); - return index; - } - } - - return QModelIndex(); -} - -int OverviewModel::rowCount(const QModelIndex &parent) const -{ - if (hasDocument()) { - if (!parent.isValid()) { - return globalSymbolCount()+1; // account for no symbol item - } else { - if (!parent.parent().isValid() && parent.row() == 0) // account for no symbol item - return 0; - Symbol *parentSymbol = static_cast<Symbol *>(parent.internalPointer()); - Q_ASSERT(parentSymbol); - - if (Template *t = parentSymbol->asTemplate()) - if (Symbol *templateParentSymbol = t->declaration()) - parentSymbol = templateParentSymbol; - - if (Scope *parentScope = parentSymbol->asScope()) { - if (!parentScope->isFunction() && !parentScope->isObjCMethod()) - return parentScope->memberCount(); - } - return 0; - } - } - if (!parent.isValid()) - return 1; // account for no symbol item - return 0; -} - -int OverviewModel::columnCount(const QModelIndex &) const -{ - return 1; -} - -QVariant OverviewModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) - return QVariant(); - - // 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 - } - - 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 (Template *t = symbol->asTemplate()) - if (Symbol *templateDeclaration = t->declaration()) { - QStringList parameters; - parameters.reserve(t->templateParameterCount()); - for (unsigned i = 0; i < t->templateParameterCount(); ++i) - parameters.append(_overview.prettyName(t->templateParameterAt(i)->name())); - name += QLatin1Char('<') + parameters.join(QLatin1String(", ")) + QLatin1Char('>'); - symbol = templateDeclaration; - } - if (symbol->isObjCMethod()) { - ObjCMethod *method = symbol->asObjCMethod(); - if (method->isStatic()) - name = QLatin1Char('+') + name; - else - name = QLatin1Char('-') + name; - } else if (! symbol->isScope() || symbol->isFunction()) { - QString type = _overview.prettyType(symbol->type()); - if (Function *f = symbol->type()->asFunctionType()) { - name += type; - type = _overview.prettyType(f->returnType()); - } - if (! type.isEmpty()) - name += QLatin1String(": ") + type; - } - 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 -{ - return static_cast<Symbol *>(index.internalPointer()); -} - -void OverviewModel::rebuild(Document::Ptr doc) -{ - beginResetModel(); - _cppDocument = doc; - endResetModel(); -} - -Qt::ItemFlags OverviewModel::flags(const QModelIndex &index) const -{ - if (!index.isValid()) - return 0; - - return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled; -} - -Qt::DropActions OverviewModel::supportedDragActions() const -{ - return Qt::MoveAction; -} - -QStringList OverviewModel::mimeTypes() const -{ - return Utils::DropSupport::mimeTypesForFilePaths(); -} - -QMimeData *OverviewModel::mimeData(const QModelIndexList &indexes) const -{ - auto mimeData = new Utils::DropMimeData; - foreach (const QModelIndex &index, indexes) { - const QVariant fileName = data(index, FileNameRole); - if (!fileName.canConvert<QString>()) - continue; - const QVariant lineNumber = data(index, LineNumberRole); - if (!fileName.canConvert<unsigned>()) - continue; - mimeData->addFile(fileName.toString(), lineNumber.value<unsigned>()); - } - return mimeData; -} - diff --git a/src/libs/cplusplus/OverviewModel.h b/src/libs/cplusplus/OverviewModel.h deleted file mode 100644 index 48b29aad1cd..00000000000 --- a/src/libs/cplusplus/OverviewModel.h +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -****************************************************************************/ - -#pragma once - -#include "CppDocument.h" -#include "Overview.h" - -#include <QAbstractItemModel> - -namespace CPlusPlus { - -class CPLUSPLUS_EXPORT OverviewModel : public QAbstractItemModel -{ - Q_OBJECT - -public: - enum Role { - FileNameRole = Qt::UserRole + 1, - LineNumberRole - }; - -public: - OverviewModel(QObject *parent = 0); - virtual ~OverviewModel(); - - virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex parent(const QModelIndex &child) const; - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - - Document::Ptr document() const; - Symbol *symbolFromIndex(const QModelIndex &index) const; - - Qt::ItemFlags flags(const QModelIndex &index) const; - Qt::DropActions supportedDragActions() const; - QStringList mimeTypes() const; - QMimeData *mimeData(const QModelIndexList &indexes) const; - - void rebuild(Document::Ptr doc); - -private: - bool hasDocument() const; - unsigned globalSymbolCount() const; - Symbol *globalSymbolAt(unsigned index) const; - -private: - Document::Ptr _cppDocument; - Overview _overview; -}; - -} // namespace CPlusPlus diff --git a/src/libs/cplusplus/cplusplus-lib.pri b/src/libs/cplusplus/cplusplus-lib.pri index 2e65f13d10f..0931855a553 100644 --- a/src/libs/cplusplus/cplusplus-lib.pri +++ b/src/libs/cplusplus/cplusplus-lib.pri @@ -17,15 +17,13 @@ HEADERS += \ $$PWD/Icons.h \ $$PWD/ExpressionUnderCursor.h \ $$PWD/BackwardsScanner.h \ - $$PWD/MatchingText.h \ - $$PWD/OverviewModel.h + $$PWD/MatchingText.h SOURCES += \ $$PWD/Icons.cpp \ $$PWD/ExpressionUnderCursor.cpp \ $$PWD/BackwardsScanner.cpp \ - $$PWD/MatchingText.cpp \ - $$PWD/OverviewModel.cpp + $$PWD/MatchingText.cpp } HEADERS += \ diff --git a/src/libs/cplusplus/cplusplus.qbs b/src/libs/cplusplus/cplusplus.qbs index a0e0d66178c..4eb65814981 100644 --- a/src/libs/cplusplus/cplusplus.qbs +++ b/src/libs/cplusplus/cplusplus.qbs @@ -113,7 +113,6 @@ Project { "MatchingText.cpp", "MatchingText.h", "NamePrettyPrinter.cpp", "NamePrettyPrinter.h", "Overview.cpp", "Overview.h", - "OverviewModel.cpp", "OverviewModel.h", "PPToken.cpp", "PPToken.h", "PreprocessorClient.cpp", "PreprocessorClient.h", "PreprocessorEnvironment.cpp", "PreprocessorEnvironment.h", |