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

View File

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

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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;