CppEditor: remove unneeded indirection for the outline model

Change-Id: I01e9fb0dae30d6df22c0f5d7255ab247c48749fc
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2022-08-15 09:42:49 +02:00
parent 8703bcd777
commit b39c7cd782
15 changed files with 86 additions and 150 deletions

View File

@@ -38,7 +38,6 @@
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <cppeditor/abstractoverviewmodel.h>
#include <cppeditor/cppcodemodelsettings.h>
#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppeditorwidget.h>

View File

@@ -5,7 +5,6 @@ add_qtc_plugin(CppEditor
PLUGIN_TEST_DEPENDS QbsProjectManager QmakeProjectManager
SOURCES
abstracteditorsupport.cpp abstracteditorsupport.h
abstractoverviewmodel.h
baseeditordocumentparser.cpp baseeditordocumentparser.h
baseeditordocumentprocessor.cpp baseeditordocumentprocessor.h
builtincursorinfo.cpp builtincursorinfo.h

View File

@@ -1,104 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2018 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 "cppeditor_global.h"
#include <utils/dropsupport.h>
#include <utils/treemodel.h>
#include <QSharedPointer>
#include <utility>
namespace CPlusPlus { class Document; }
namespace Utils {
class LineColumn;
class Link;
}
namespace CppEditor {
class CPPEDITOR_EXPORT AbstractOverviewModel : public Utils::TreeModel<>
{
Q_OBJECT
public:
enum Role {
FileNameRole = Qt::UserRole + 1,
LineNumberRole
};
virtual void rebuild(QSharedPointer<CPlusPlus::Document>) {}
virtual bool rebuild(const QString &) { return false; }
Qt::ItemFlags flags(const QModelIndex &index) const override
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
Qt::DropActions supportedDragActions() const override
{
return Qt::MoveAction;
}
QStringList mimeTypes() const override
{
return Utils::DropSupport::mimeTypesForFilePaths();
}
QMimeData *mimeData(const QModelIndexList &indexes) const override
{
auto mimeData = new Utils::DropMimeData;
for (const QModelIndex &index : indexes) {
const QVariant fileName = data(index, FileNameRole);
if (!fileName.canConvert<QString>())
continue;
const QVariant lineNumber = data(index, LineNumberRole);
if (!lineNumber.canConvert<unsigned>())
continue;
mimeData->addFile(Utils::FilePath::fromVariant(fileName),
static_cast<int>(lineNumber.value<unsigned>()));
}
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;
using Range = std::pair<Utils::LineColumn, Utils::LineColumn>;
virtual Range rangeFromIndex(const QModelIndex &) const = 0;
signals:
void needsUpdate();
};
} // namespace CppEditor

View File

@@ -113,11 +113,6 @@ TextEditor::BaseHoverHandler *BuiltinModelManagerSupport::createHoverHandler()
return new CppHoverHandler;
}
std::unique_ptr<AbstractOverviewModel> BuiltinModelManagerSupport::createOverviewModel()
{
return std::make_unique<OverviewModel>();
}
void BuiltinModelManagerSupport::followSymbol(const CursorInEditor &data,
const Utils::LinkHandler &processLinkCallback,
bool resolveTarget, bool inNextSplit)

View File

@@ -43,7 +43,6 @@ public:
CppCompletionAssistProvider *completionAssistProvider();
TextEditor::BaseHoverHandler *createHoverHandler();
std::unique_ptr<AbstractOverviewModel> createOverviewModel();
BaseEditorDocumentProcessor *createEditorDocumentProcessor(
TextEditor::TextDocument *baseTextDocument) final;

View File

@@ -29,7 +29,6 @@ QtcPlugin {
files: [
"abstracteditorsupport.cpp",
"abstracteditorsupport.h",
"abstractoverviewmodel.h",
"baseeditordocumentparser.cpp",
"baseeditordocumentparser.h",
"baseeditordocumentprocessor.cpp",

View File

@@ -57,7 +57,7 @@ class OverviewProxyModel : public QSortFilterProxyModel
Q_OBJECT
public:
OverviewProxyModel(CppEditor::AbstractOverviewModel &sourceModel, QObject *parent)
OverviewProxyModel(CppEditor::Internal::OverviewModel &sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel)
{
@@ -73,7 +73,7 @@ public:
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
private:
CppEditor::AbstractOverviewModel &m_sourceModel;
CppEditor::Internal::OverviewModel &m_sourceModel;
};
QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName)
@@ -94,7 +94,7 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
, m_editorWidget(editorWidget)
, m_combo(new Utils::TreeViewComboBox)
{
m_model = CppModelManager::instance()->createOverviewModel();
m_model = std::make_unique<OverviewModel>();
m_proxyModel = new OverviewProxyModel(*m_model, this);
m_proxyModel->setSourceModel(m_model.get());
@@ -130,7 +130,7 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
m_updateTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
QLatin1String("CppEditorOutline::m_updateTimer"));
connect(m_updateTimer, &QTimer::timeout, this, &CppEditorOutline::updateNow);
connect(m_model.get(), &AbstractOverviewModel::needsUpdate, this,
connect(m_model.get(), &OverviewModel::needsUpdate, this,
&CppEditorOutline::updateNow);
m_updateIndexTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
@@ -148,7 +148,7 @@ bool CppEditorOutline::isSorted() const
return m_proxyModel->sortColumn() == 0;
}
AbstractOverviewModel *CppEditorOutline::model() const
OverviewModel *CppEditorOutline::model() const
{
return m_model.get();
}
@@ -189,8 +189,7 @@ void CppEditorOutline::updateNow()
return;
}
if (!m_model->rebuild(filePath))
m_model->rebuild(m_document);
m_model->rebuild(m_document);
m_combo->view()->expandAll();
updateIndexNow();
@@ -244,7 +243,7 @@ void CppEditorOutline::gotoSymbolInEditor()
emit m_editorWidget->activateEditor();
}
static bool contains(const AbstractOverviewModel::Range &range, int line, int column)
static bool contains(const OverviewModel::Range &range, int line, int column)
{
if (line < range.first.line || line > range.second.line)
return false;
@@ -262,7 +261,7 @@ QModelIndex CppEditorOutline::indexForPosition(int line, int column,
const int rowCount = m_model->rowCount(rootIndex);
for (int row = 0; row < rowCount; ++row) {
const QModelIndex index = m_model->index(row, 0, rootIndex);
const AbstractOverviewModel::Range range = m_model->rangeFromIndex(index);
const OverviewModel::Range range = m_model->rangeFromIndex(index);
if (range.first.line > line)
break;
// Skip ranges that do not include current line and column.

View File

@@ -25,7 +25,7 @@
#pragma once
#include "abstractoverviewmodel.h"
#include "cppoverviewmodel.h"
#include <QModelIndex>
#include <QObject>
@@ -52,7 +52,7 @@ public:
void update();
AbstractOverviewModel *model() const;
OverviewModel *model() const;
QModelIndex modelIndex();
QWidget *widget() const; // Must be deleted by client.
@@ -77,7 +77,7 @@ private:
private:
QSharedPointer<CPlusPlus::Document> m_document;
std::unique_ptr<AbstractOverviewModel> m_model;
std::unique_ptr<OverviewModel> m_model;
TextEditor::TextEditorWidget *m_editorWidget;

View File

@@ -26,7 +26,7 @@
#include "cppmodelmanager.h"
#include "abstracteditorsupport.h"
#include "abstractoverviewmodel.h"
#include "cppoverviewmodel.h"
#include "baseeditordocumentprocessor.h"
#include "builtinindexingsupport.h"
#include "cppcodemodelinspectordumper.h"
@@ -549,11 +549,6 @@ Core::ILocatorFilter *CppModelManager::currentDocumentFilter() const
return d->m_currentDocumentFilter.get();
}
std::unique_ptr<AbstractOverviewModel> CppModelManager::createOverviewModel() const
{
return d->m_builtinModelManagerSupport.createOverviewModel();
}
QString CppModelManager::configurationFileName()
{
return Preprocessor::configurationFileName();

View File

@@ -61,7 +61,6 @@ class TextDocument;
namespace CppEditor {
class AbstractEditorSupport;
class AbstractOverviewModel;
class BaseEditorDocumentProcessor;
class CppCompletionAssistProvider;
class CppEditorDocumentHandle;
@@ -203,8 +202,6 @@ public:
static Core::ILocatorFilter *createAuxiliaryCurrentDocumentFilter();
std::unique_ptr<AbstractOverviewModel> createOverviewModel() const;
CppIndexingSupport *indexingSupport();
QStringList projectFiles();

View File

@@ -42,7 +42,7 @@ class BaseHoverHandler;
namespace CppEditor {
class AbstractOverviewModel;
class OverviewModel;
class BaseEditorDocumentProcessor;
class CppCompletionAssistProvider;
class ProjectPart;

View File

@@ -68,7 +68,7 @@ void CppOutlineTreeView::contextMenuEvent(QContextMenuEvent *event)
event->accept();
}
CppOutlineFilterModel::CppOutlineFilterModel(AbstractOverviewModel &sourceModel,
CppOutlineFilterModel::CppOutlineFilterModel(OverviewModel &sourceModel,
QObject *parent)
: QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel)
@@ -102,7 +102,7 @@ CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) :
m_blockCursorSync(false),
m_sorted(false)
{
AbstractOverviewModel *model = m_editor->outline()->model();
OverviewModel *model = m_editor->outline()->model();
m_proxyModel = new CppOutlineFilterModel(*model, this);
m_proxyModel->setSourceModel(model);
@@ -179,7 +179,7 @@ void CppOutlineWidget::updateSelectionInTree(const QModelIndex &index)
void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex)
{
QModelIndex index = m_proxyModel->mapToSource(proxyIndex);
AbstractOverviewModel *model = m_editor->outline()->model();
OverviewModel *model = m_editor->outline()->model();
Utils::LineColumn lineColumn = model->lineColumnFromIndex(index);
if (!lineColumn.isValid())
return;

View File

@@ -25,8 +25,8 @@
#pragma once
#include "abstractoverviewmodel.h"
#include "cppeditorwidget.h"
#include "cppoverviewmodel.h"
#include <texteditor/ioutlinewidget.h>
@@ -50,13 +50,13 @@ class CppOutlineFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
CppOutlineFilterModel(AbstractOverviewModel &sourceModel, QObject *parent);
CppOutlineFilterModel(OverviewModel &sourceModel, QObject *parent);
// QSortFilterProxyModel
bool filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const override;
Qt::DropActions supportedDragActions() const override;
private:
AbstractOverviewModel &m_sourceModel;
OverviewModel &m_sourceModel;
};
class CppOutlineWidget : public TextEditor::IOutlineWidget

View File

@@ -126,10 +126,10 @@ public:
case Qt::DecorationRole:
return Icons::iconForSymbol(symbol);
case AbstractOverviewModel::FileNameRole:
case OverviewModel::FileNameRole:
return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
case AbstractOverviewModel::LineNumberRole:
case OverviewModel::LineNumberRole:
return symbol->line();
default:
@@ -168,6 +168,40 @@ Symbol *OverviewModel::symbolFromIndex(const QModelIndex &index) const
return item ? item->symbol : nullptr;
}
Qt::ItemFlags OverviewModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
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;
for (const QModelIndex &index : indexes) {
const QVariant fileName = data(index, FileNameRole);
if (!fileName.canConvert<QString>())
continue;
const QVariant lineNumber = data(index, LineNumberRole);
if (!lineNumber.canConvert<unsigned>())
continue;
mimeData->addFile(Utils::FilePath::fromVariant(fileName),
static_cast<int>(lineNumber.value<unsigned>()));
}
return mimeData;
}
void OverviewModel::rebuild(Document::Ptr doc)
{
beginResetModel();

View File

@@ -25,25 +25,49 @@
#pragma once
#include "abstractoverviewmodel.h"
#include <utils/dropsupport.h>
#include <utils/treemodel.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/Overview.h>
#include <QSharedPointer>
#include <utility>
namespace Utils {
class LineColumn;
class Link;
} // namespace Utils
namespace CppEditor::Internal {
class SymbolItem;
class OverviewModel : public AbstractOverviewModel
class OverviewModel : public Utils::TreeModel<>
{
Q_OBJECT
public:
void rebuild(CPlusPlus::Document::Ptr doc) override;
enum Role {
FileNameRole = Qt::UserRole + 1,
LineNumberRole
};
bool isGenerated(const QModelIndex &sourceIndex) const override;
Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const override;
Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const override;
Range rangeFromIndex(const QModelIndex &sourceIndex) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
Qt::DropActions supportedDragActions() const override;
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
void rebuild(CPlusPlus::Document::Ptr doc);
bool isGenerated(const QModelIndex &sourceIndex) const;
Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const;
Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const;
using Range = std::pair<Utils::LineColumn, Utils::LineColumn>;
Range rangeFromIndex(const QModelIndex &sourceIndex) const;
signals:
void needsUpdate();
private:
CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const;