diff options
Diffstat (limited to 'src/plugins/cpptools')
-rw-r--r-- | src/plugins/cpptools/abstractoverviewmodel.h | 24 | ||||
-rw-r--r-- | src/plugins/cpptools/cppeditoroutline.cpp | 38 | ||||
-rw-r--r-- | src/plugins/cpptools/cppeditoroutline.h | 6 | ||||
-rw-r--r-- | src/plugins/cpptools/cppoverviewmodel.cpp | 41 | ||||
-rw-r--r-- | src/plugins/cpptools/cppoverviewmodel.h | 11 |
5 files changed, 64 insertions, 56 deletions
diff --git a/src/plugins/cpptools/abstractoverviewmodel.h b/src/plugins/cpptools/abstractoverviewmodel.h index 9f15715029..e0110084ed 100644 --- a/src/plugins/cpptools/abstractoverviewmodel.h +++ b/src/plugins/cpptools/abstractoverviewmodel.h @@ -32,9 +32,11 @@ #include <QAbstractItemModel> #include <QSharedPointer> -namespace CPlusPlus { -class Document; -class Symbol; +namespace CPlusPlus { class Document; } + +namespace Utils { +class LineColumn; +struct Link; } namespace CppTools { @@ -49,17 +51,7 @@ public: LineNumberRole }; - AbstractOverviewModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {} - - virtual QSharedPointer<CPlusPlus::Document> document() const - { - return {}; - } - - virtual CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &) const - { - return {}; - } + AbstractOverviewModel() : QAbstractItemModel(nullptr) {} virtual void rebuild(QSharedPointer<CPlusPlus::Document>) {} @@ -95,6 +87,10 @@ public: } return mimeData; } + + virtual bool isGenerated(const QModelIndex &) const { return false; } + virtual Utils::Link linkFromIndex(const QModelIndex &) const = 0; + virtual Utils::LineColumn lineColumnFromIndex(const QModelIndex &) const = 0; }; } // namespace CppTools diff --git a/src/plugins/cpptools/cppeditoroutline.cpp b/src/plugins/cpptools/cppeditoroutline.cpp index cf4e3dbafa..033f5e9090 100644 --- a/src/plugins/cpptools/cppeditoroutline.cpp +++ b/src/plugins/cpptools/cppeditoroutline.cpp @@ -24,7 +24,6 @@ ****************************************************************************/ #include "cppeditoroutline.h" - #include "cppmodelmanager.h" #include "cppoverviewmodel.h" #include "cpptoolsreuse.h" @@ -34,6 +33,7 @@ #include <texteditor/textdocument.h> #include <coreplugin/editormanager/editormanager.h> +#include <utils/linecolumn.h> #include <utils/treeviewcombobox.h> #include <QAction> @@ -57,7 +57,7 @@ class OverviewProxyModel : public QSortFilterProxyModel Q_OBJECT public: - OverviewProxyModel(CppTools::AbstractOverviewModel *sourceModel, QObject *parent) + OverviewProxyModel(CppTools::AbstractOverviewModel &sourceModel, QObject *parent) : QSortFilterProxyModel(parent) , m_sourceModel(sourceModel) { @@ -66,15 +66,14 @@ public: bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const override { // Ignore generated symbols, e.g. by macro expansion (Q_OBJECT) - const QModelIndex sourceIndex = m_sourceModel->index(sourceRow, 0, sourceParent); - CPlusPlus::Symbol *symbol = m_sourceModel->symbolFromIndex(sourceIndex); - if (symbol && symbol->isGenerated()) + const QModelIndex sourceIndex = m_sourceModel.index(sourceRow, 0, sourceParent); + if (m_sourceModel.isGenerated(sourceIndex)) return false; return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); } private: - CppTools::AbstractOverviewModel *m_sourceModel; + CppTools::AbstractOverviewModel &m_sourceModel; }; QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName) @@ -95,9 +94,9 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget) , m_editorWidget(editorWidget) , m_combo(new Utils::TreeViewComboBox) { - m_model = new CppTools::OverviewModel(this); - m_proxyModel = new OverviewProxyModel(m_model, this); - m_proxyModel->setSourceModel(m_model); + m_model = std::make_unique<CppTools::OverviewModel>(); + m_proxyModel = new OverviewProxyModel(*m_model, this); + m_proxyModel->setSourceModel(m_model.get()); // Set up proxy model if (CppTools::CppToolsSettings::instance()->sortedEditorDocumentOutline()) @@ -167,7 +166,7 @@ void CppEditorOutline::setSorted(bool sort) CppTools::AbstractOverviewModel *CppEditorOutline::model() const { - return m_model; + return m_model.get(); } QModelIndex CppEditorOutline::modelIndex() @@ -196,13 +195,14 @@ void CppEditorOutline::updateNow() if (!document) return; - if (document->editorRevision() + m_document = document; + if (m_document->editorRevision() != static_cast<unsigned>(m_editorWidget->document()->revision())) { m_updateTimer->start(); return; } - m_model->rebuild(document); + m_model->rebuild(m_document); m_combo->view()->expandAll(); updateIndexNow(); @@ -215,12 +215,11 @@ void CppEditorOutline::updateIndex() void CppEditorOutline::updateIndexNow() { - const CPlusPlus::Document::Ptr document = m_model->document(); - if (!document) + if (!m_document) return; const auto revision = static_cast<unsigned>(m_editorWidget->document()->revision()); - if (document->editorRevision() != revision) { + if (m_document->editorRevision() != revision) { m_updateIndexTimer->start(); return; } @@ -246,11 +245,8 @@ void CppEditorOutline::gotoSymbolInEditor() { const QModelIndex modelIndex = m_combo->view()->currentIndex(); const QModelIndex sourceIndex = m_proxyModel->mapToSource(modelIndex); - CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(sourceIndex); - if (!symbol) - return; - const Utils::Link &link = symbol->toLink(); + const Utils::Link link = m_model->linkFromIndex(sourceIndex); if (!link.hasValidTarget()) return; @@ -264,12 +260,10 @@ QModelIndex CppEditorOutline::indexForPosition(int line, int column, const QModelIndex &rootIndex) const { QModelIndex lastIndex = rootIndex; - const int rowCount = m_model->rowCount(rootIndex); for (int row = 0; row < rowCount; ++row) { const QModelIndex index = m_model->index(row, 0, rootIndex); - CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(index); - if (symbol && symbol->line() > unsigned(line)) + if (m_model->lineColumnFromIndex(index).line > line) break; lastIndex = index; } diff --git a/src/plugins/cpptools/cppeditoroutline.h b/src/plugins/cpptools/cppeditoroutline.h index 4f7c541ac0..d7352ea583 100644 --- a/src/plugins/cpptools/cppeditoroutline.h +++ b/src/plugins/cpptools/cppeditoroutline.h @@ -32,6 +32,8 @@ #include <QModelIndex> #include <QObject> +#include <memory> + QT_BEGIN_NAMESPACE class QAction; class QSortFilterProxyModel; @@ -77,10 +79,12 @@ private: const QModelIndex &rootIndex = QModelIndex()) const; private: + QSharedPointer<CPlusPlus::Document> m_document; + std::unique_ptr<AbstractOverviewModel> m_model; + TextEditor::TextEditorWidget *m_editorWidget; Utils::TreeViewComboBox *m_combo; // Not owned - AbstractOverviewModel *m_model; QSortFilterProxyModel *m_proxyModel; QModelIndex m_modelIndex; QAction *m_sortAction; diff --git a/src/plugins/cpptools/cppoverviewmodel.cpp b/src/plugins/cpptools/cppoverviewmodel.cpp index 7fe118f68c..1cf10b4b9f 100644 --- a/src/plugins/cpptools/cppoverviewmodel.cpp +++ b/src/plugins/cpptools/cppoverviewmodel.cpp @@ -31,28 +31,17 @@ #include <cplusplus/Scope.h> #include <cplusplus/Symbols.h> -#include <utils/dropsupport.h> +#include <utils/linecolumn.h> +#include <utils/link.h> using namespace CPlusPlus; namespace CppTools { -OverviewModel::OverviewModel(QObject *parent) - : AbstractOverviewModel(parent) -{ } - -OverviewModel::~OverviewModel() -{ } - bool OverviewModel::hasDocument() const { return _cppDocument; } -Document::Ptr OverviewModel::document() const -{ - return _cppDocument; -} - unsigned OverviewModel::globalSymbolCount() const { unsigned count = 0; @@ -250,4 +239,30 @@ void OverviewModel::rebuild(Document::Ptr doc) endResetModel(); } +bool OverviewModel::isGenerated(const QModelIndex &sourceIndex) const +{ + CPlusPlus::Symbol *symbol = symbolFromIndex(sourceIndex); + return symbol && symbol->isGenerated(); +} + +Utils::Link OverviewModel::linkFromIndex(const QModelIndex &sourceIndex) const +{ + CPlusPlus::Symbol *symbol = symbolFromIndex(sourceIndex); + if (!symbol) + return {}; + + return symbol->toLink(); +} + +Utils::LineColumn OverviewModel::lineColumnFromIndex(const QModelIndex &sourceIndex) const +{ + Utils::LineColumn lineColumn; + CPlusPlus::Symbol *symbol = symbolFromIndex(sourceIndex); + if (!symbol) + return lineColumn; + lineColumn.line = static_cast<int>(symbol->line()); + lineColumn.column = static_cast<int>(symbol->column()); + return lineColumn; +} + } // namespace CppTools diff --git a/src/plugins/cpptools/cppoverviewmodel.h b/src/plugins/cpptools/cppoverviewmodel.h index 453d3c63d8..42832552a5 100644 --- a/src/plugins/cpptools/cppoverviewmodel.h +++ b/src/plugins/cpptools/cppoverviewmodel.h @@ -37,9 +37,6 @@ class CPPTOOLS_EXPORT OverviewModel : public AbstractOverviewModel Q_OBJECT public: - OverviewModel(QObject *parent = nullptr); - ~OverviewModel() override; - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &child) const override; @@ -47,12 +44,14 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - CPlusPlus::Document::Ptr document() const override; - CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const override; - void rebuild(CPlusPlus::Document::Ptr doc) override; + bool isGenerated(const QModelIndex &sourceIndex) const override; + Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const override; + Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const override; + private: + CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const; bool hasDocument() const; unsigned globalSymbolCount() const; CPlusPlus::Symbol *globalSymbolAt(unsigned index) const; |