Clang: Use more generic methods in OverviewModel

Do not return Document or Symbol. Instead use Link
or LineColumn directly as return values.

Change-Id: I1863d7c3b4985ffe2ae5454622227075ebdc2de7
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-02-08 13:31:22 +01:00
parent c6d4308ccd
commit 8389aa144a
7 changed files with 86 additions and 78 deletions

View File

@@ -33,6 +33,8 @@
#include <coreplugin/find/itemviewfind.h> #include <coreplugin/find/itemviewfind.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <utils/linecolumn.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QDebug> #include <QDebug>
@@ -67,7 +69,7 @@ void CppOutlineTreeView::contextMenuEvent(QContextMenuEvent *event)
event->accept(); event->accept();
} }
CppOutlineFilterModel::CppOutlineFilterModel(CppTools::AbstractOverviewModel *sourceModel, CppOutlineFilterModel::CppOutlineFilterModel(CppTools::AbstractOverviewModel &sourceModel,
QObject *parent) QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel) , m_sourceModel(sourceModel)
@@ -81,9 +83,8 @@ bool CppOutlineFilterModel::filterAcceptsRow(int sourceRow,
if (!sourceParent.isValid() && sourceRow == 0) if (!sourceParent.isValid() && sourceRow == 0)
return false; return false;
// ignore generated symbols, e.g. by macro expansion (Q_OBJECT) // ignore generated symbols, e.g. by macro expansion (Q_OBJECT)
const QModelIndex sourceIndex = m_sourceModel->index(sourceRow, 0, sourceParent); const QModelIndex sourceIndex = m_sourceModel.index(sourceRow, 0, sourceParent);
CPlusPlus::Symbol *symbol = m_sourceModel->symbolFromIndex(sourceIndex); if (m_sourceModel.isGenerated(sourceIndex))
if (symbol && symbol->isGenerated())
return false; return false;
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
@@ -96,15 +97,14 @@ Qt::DropActions CppOutlineFilterModel::supportedDragActions() const
CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) : CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) :
TextEditor::IOutlineWidget(),
m_editor(editor), m_editor(editor),
m_treeView(new CppOutlineTreeView(this)), m_treeView(new CppOutlineTreeView(this)),
m_model(m_editor->outline()->model()),
m_enableCursorSync(true), m_enableCursorSync(true),
m_blockCursorSync(false) m_blockCursorSync(false)
{ {
m_proxyModel = new CppOutlineFilterModel(m_model, this); CppTools::AbstractOverviewModel *model = m_editor->outline()->model();
m_proxyModel->setSourceModel(m_model); m_proxyModel = new CppOutlineFilterModel(*model, this);
m_proxyModel->setSourceModel(model);
auto *layout = new QVBoxLayout; auto *layout = new QVBoxLayout;
layout->setMargin(0); layout->setMargin(0);
@@ -115,7 +115,7 @@ CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) :
m_treeView->setModel(m_proxyModel); m_treeView->setModel(m_proxyModel);
setFocusProxy(m_treeView); setFocusProxy(m_treeView);
connect(m_model, &QAbstractItemModel::modelReset, this, &CppOutlineWidget::modelUpdated); connect(model, &QAbstractItemModel::modelReset, this, &CppOutlineWidget::modelUpdated);
modelUpdated(); modelUpdated();
connect(m_editor->outline(), &CppTools::CppEditorOutline::modelIndexChanged, connect(m_editor->outline(), &CppTools::CppEditorOutline::modelIndexChanged,
@@ -157,18 +157,19 @@ void CppOutlineWidget::updateSelectionInTree(const QModelIndex &index)
void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex) void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex)
{ {
QModelIndex index = m_proxyModel->mapToSource(proxyIndex); QModelIndex index = m_proxyModel->mapToSource(proxyIndex);
CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(index); CppTools::AbstractOverviewModel *model = m_editor->outline()->model();
if (symbol) { Utils::LineColumn lineColumn = model->lineColumnFromIndex(index);
if (!lineColumn.isValid())
return;
m_blockCursorSync = true; m_blockCursorSync = true;
Core::EditorManager::cutForwardNavigationHistory(); Core::EditorManager::cutForwardNavigationHistory();
Core::EditorManager::addCurrentPositionToNavigationHistory(); Core::EditorManager::addCurrentPositionToNavigationHistory();
// line has to be 1 based, column 0 based! // line has to be 1 based, column 0 based!
m_editor->gotoLine(static_cast<int>(symbol->line()), static_cast<int>(symbol->column() - 1), m_editor->gotoLine(lineColumn.line, lineColumn.column - 1, true, true);
true, true);
m_blockCursorSync = false; m_blockCursorSync = false;
}
} }
void CppOutlineWidget::onItemActivated(const QModelIndex &index) void CppOutlineWidget::onItemActivated(const QModelIndex &index)

View File

@@ -51,13 +51,13 @@ class CppOutlineFilterModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
public: public:
CppOutlineFilterModel(CppTools::AbstractOverviewModel *sourceModel, QObject *parent); CppOutlineFilterModel(CppTools::AbstractOverviewModel &sourceModel, QObject *parent);
// QSortFilterProxyModel // QSortFilterProxyModel
bool filterAcceptsRow(int sourceRow, bool filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const override; const QModelIndex &sourceParent) const override;
Qt::DropActions supportedDragActions() const override; Qt::DropActions supportedDragActions() const override;
private: private:
CppTools::AbstractOverviewModel *m_sourceModel; CppTools::AbstractOverviewModel &m_sourceModel;
}; };
class CppOutlineWidget : public TextEditor::IOutlineWidget class CppOutlineWidget : public TextEditor::IOutlineWidget
@@ -80,7 +80,6 @@ private:
private: private:
CppEditorWidget *m_editor; CppEditorWidget *m_editor;
CppOutlineTreeView *m_treeView; CppOutlineTreeView *m_treeView;
CppTools::AbstractOverviewModel *m_model;
QSortFilterProxyModel *m_proxyModel; QSortFilterProxyModel *m_proxyModel;
bool m_enableCursorSync; bool m_enableCursorSync;

View File

@@ -32,9 +32,11 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QSharedPointer> #include <QSharedPointer>
namespace CPlusPlus { namespace CPlusPlus { class Document; }
class Document;
class Symbol; namespace Utils {
class LineColumn;
struct Link;
} }
namespace CppTools { namespace CppTools {
@@ -49,17 +51,7 @@ public:
LineNumberRole LineNumberRole
}; };
AbstractOverviewModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {} AbstractOverviewModel() : QAbstractItemModel(nullptr) {}
virtual QSharedPointer<CPlusPlus::Document> document() const
{
return {};
}
virtual CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &) const
{
return {};
}
virtual void rebuild(QSharedPointer<CPlusPlus::Document>) {} virtual void rebuild(QSharedPointer<CPlusPlus::Document>) {}
@@ -95,6 +87,10 @@ public:
} }
return mimeData; 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 } // namespace CppTools

View File

@@ -24,7 +24,6 @@
****************************************************************************/ ****************************************************************************/
#include "cppeditoroutline.h" #include "cppeditoroutline.h"
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "cppoverviewmodel.h" #include "cppoverviewmodel.h"
#include "cpptoolsreuse.h" #include "cpptoolsreuse.h"
@@ -34,6 +33,7 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <utils/linecolumn.h>
#include <utils/treeviewcombobox.h> #include <utils/treeviewcombobox.h>
#include <QAction> #include <QAction>
@@ -57,7 +57,7 @@ class OverviewProxyModel : public QSortFilterProxyModel
Q_OBJECT Q_OBJECT
public: public:
OverviewProxyModel(CppTools::AbstractOverviewModel *sourceModel, QObject *parent) OverviewProxyModel(CppTools::AbstractOverviewModel &sourceModel, QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel) , m_sourceModel(sourceModel)
{ {
@@ -66,15 +66,14 @@ public:
bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const override bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const override
{ {
// Ignore generated symbols, e.g. by macro expansion (Q_OBJECT) // Ignore generated symbols, e.g. by macro expansion (Q_OBJECT)
const QModelIndex sourceIndex = m_sourceModel->index(sourceRow, 0, sourceParent); const QModelIndex sourceIndex = m_sourceModel.index(sourceRow, 0, sourceParent);
CPlusPlus::Symbol *symbol = m_sourceModel->symbolFromIndex(sourceIndex); if (m_sourceModel.isGenerated(sourceIndex))
if (symbol && symbol->isGenerated())
return false; return false;
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
} }
private: private:
CppTools::AbstractOverviewModel *m_sourceModel; CppTools::AbstractOverviewModel &m_sourceModel;
}; };
QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName) QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName)
@@ -95,9 +94,9 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
, m_editorWidget(editorWidget) , m_editorWidget(editorWidget)
, m_combo(new Utils::TreeViewComboBox) , m_combo(new Utils::TreeViewComboBox)
{ {
m_model = new CppTools::OverviewModel(this); m_model = std::make_unique<CppTools::OverviewModel>();
m_proxyModel = new OverviewProxyModel(m_model, this); m_proxyModel = new OverviewProxyModel(*m_model, this);
m_proxyModel->setSourceModel(m_model); m_proxyModel->setSourceModel(m_model.get());
// Set up proxy model // Set up proxy model
if (CppTools::CppToolsSettings::instance()->sortedEditorDocumentOutline()) if (CppTools::CppToolsSettings::instance()->sortedEditorDocumentOutline())
@@ -167,7 +166,7 @@ void CppEditorOutline::setSorted(bool sort)
CppTools::AbstractOverviewModel *CppEditorOutline::model() const CppTools::AbstractOverviewModel *CppEditorOutline::model() const
{ {
return m_model; return m_model.get();
} }
QModelIndex CppEditorOutline::modelIndex() QModelIndex CppEditorOutline::modelIndex()
@@ -196,13 +195,14 @@ void CppEditorOutline::updateNow()
if (!document) if (!document)
return; return;
if (document->editorRevision() m_document = document;
if (m_document->editorRevision()
!= static_cast<unsigned>(m_editorWidget->document()->revision())) { != static_cast<unsigned>(m_editorWidget->document()->revision())) {
m_updateTimer->start(); m_updateTimer->start();
return; return;
} }
m_model->rebuild(document); m_model->rebuild(m_document);
m_combo->view()->expandAll(); m_combo->view()->expandAll();
updateIndexNow(); updateIndexNow();
@@ -215,12 +215,11 @@ void CppEditorOutline::updateIndex()
void CppEditorOutline::updateIndexNow() void CppEditorOutline::updateIndexNow()
{ {
const CPlusPlus::Document::Ptr document = m_model->document(); if (!m_document)
if (!document)
return; return;
const auto revision = static_cast<unsigned>(m_editorWidget->document()->revision()); const auto revision = static_cast<unsigned>(m_editorWidget->document()->revision());
if (document->editorRevision() != revision) { if (m_document->editorRevision() != revision) {
m_updateIndexTimer->start(); m_updateIndexTimer->start();
return; return;
} }
@@ -246,11 +245,8 @@ void CppEditorOutline::gotoSymbolInEditor()
{ {
const QModelIndex modelIndex = m_combo->view()->currentIndex(); const QModelIndex modelIndex = m_combo->view()->currentIndex();
const QModelIndex sourceIndex = m_proxyModel->mapToSource(modelIndex); 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()) if (!link.hasValidTarget())
return; return;
@@ -264,12 +260,10 @@ QModelIndex CppEditorOutline::indexForPosition(int line, int column,
const QModelIndex &rootIndex) const const QModelIndex &rootIndex) const
{ {
QModelIndex lastIndex = rootIndex; QModelIndex lastIndex = rootIndex;
const int rowCount = m_model->rowCount(rootIndex); const int rowCount = m_model->rowCount(rootIndex);
for (int row = 0; row < rowCount; ++row) { for (int row = 0; row < rowCount; ++row) {
const QModelIndex index = m_model->index(row, 0, rootIndex); const QModelIndex index = m_model->index(row, 0, rootIndex);
CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(index); if (m_model->lineColumnFromIndex(index).line > line)
if (symbol && symbol->line() > unsigned(line))
break; break;
lastIndex = index; lastIndex = index;
} }

View File

@@ -32,6 +32,8 @@
#include <QModelIndex> #include <QModelIndex>
#include <QObject> #include <QObject>
#include <memory>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QAction; class QAction;
class QSortFilterProxyModel; class QSortFilterProxyModel;
@@ -77,10 +79,12 @@ private:
const QModelIndex &rootIndex = QModelIndex()) const; const QModelIndex &rootIndex = QModelIndex()) const;
private: private:
QSharedPointer<CPlusPlus::Document> m_document;
std::unique_ptr<AbstractOverviewModel> m_model;
TextEditor::TextEditorWidget *m_editorWidget; TextEditor::TextEditorWidget *m_editorWidget;
Utils::TreeViewComboBox *m_combo; // Not owned Utils::TreeViewComboBox *m_combo; // Not owned
AbstractOverviewModel *m_model;
QSortFilterProxyModel *m_proxyModel; QSortFilterProxyModel *m_proxyModel;
QModelIndex m_modelIndex; QModelIndex m_modelIndex;
QAction *m_sortAction; QAction *m_sortAction;

View File

@@ -31,28 +31,17 @@
#include <cplusplus/Scope.h> #include <cplusplus/Scope.h>
#include <cplusplus/Symbols.h> #include <cplusplus/Symbols.h>
#include <utils/dropsupport.h> #include <utils/linecolumn.h>
#include <utils/link.h>
using namespace CPlusPlus; using namespace CPlusPlus;
namespace CppTools { namespace CppTools {
OverviewModel::OverviewModel(QObject *parent)
: AbstractOverviewModel(parent)
{ }
OverviewModel::~OverviewModel()
{ }
bool OverviewModel::hasDocument() const bool OverviewModel::hasDocument() const
{ {
return _cppDocument; return _cppDocument;
} }
Document::Ptr OverviewModel::document() const
{
return _cppDocument;
}
unsigned OverviewModel::globalSymbolCount() const unsigned OverviewModel::globalSymbolCount() const
{ {
unsigned count = 0; unsigned count = 0;
@@ -250,4 +239,30 @@ void OverviewModel::rebuild(Document::Ptr doc)
endResetModel(); 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 } // namespace CppTools

View File

@@ -37,9 +37,6 @@ class CPPTOOLS_EXPORT OverviewModel : public AbstractOverviewModel
Q_OBJECT Q_OBJECT
public: public:
OverviewModel(QObject *parent = nullptr);
~OverviewModel() override;
QModelIndex index(int row, int column, QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override; const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &child) const override; QModelIndex parent(const QModelIndex &child) const override;
@@ -47,12 +44,14 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) 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; 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: private:
CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const;
bool hasDocument() const; bool hasDocument() const;
unsigned globalSymbolCount() const; unsigned globalSymbolCount() const;
CPlusPlus::Symbol *globalSymbolAt(unsigned index) const; CPlusPlus::Symbol *globalSymbolAt(unsigned index) const;