2014-05-23 13:55:03 -04:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-05-23 13:55:03 -04:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2014-05-23 13:55:03 -04:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2014-05-23 13:55:03 -04:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppeditoroutline.h"
|
2018-02-01 15:47:22 +01:00
|
|
|
#include "cppmodelmanager.h"
|
|
|
|
|
#include "cppoverviewmodel.h"
|
|
|
|
|
#include "cpptoolsreuse.h"
|
|
|
|
|
#include "cpptoolssettings.h"
|
|
|
|
|
|
2015-01-23 14:06:42 +01:00
|
|
|
#include <texteditor/texteditor.h>
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2014-05-23 13:55:03 -04:00
|
|
|
|
2018-02-08 13:31:22 +01:00
|
|
|
#include <utils/linecolumn.h>
|
2014-05-23 13:55:03 -04:00
|
|
|
#include <utils/treeviewcombobox.h>
|
|
|
|
|
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <QAction>
|
2014-05-23 13:55:03 -04:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
|
|
/*!
|
2015-01-23 15:14:36 +01:00
|
|
|
\class CppTools::CppEditorOutline
|
2015-01-23 14:06:42 +01:00
|
|
|
\brief A helper class that provides the outline model and widget,
|
|
|
|
|
e.g. for the editor's tool bar.
|
2014-05-23 13:55:03 -04:00
|
|
|
|
|
|
|
|
The caller is responsible for deleting the widget returned by widget().
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
enum { UpdateOutlineIntervalInMs = 500 };
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class OverviewProxyModel : public QSortFilterProxyModel
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2018-02-08 13:31:22 +01:00
|
|
|
OverviewProxyModel(CppTools::AbstractOverviewModel &sourceModel, QObject *parent)
|
2014-05-23 13:55:03 -04:00
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
|
, m_sourceModel(sourceModel)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 15:47:22 +01:00
|
|
|
bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const override
|
2014-05-23 13:55:03 -04:00
|
|
|
{
|
|
|
|
|
// Ignore generated symbols, e.g. by macro expansion (Q_OBJECT)
|
2018-02-08 13:31:22 +01:00
|
|
|
const QModelIndex sourceIndex = m_sourceModel.index(sourceRow, 0, sourceParent);
|
|
|
|
|
if (m_sourceModel.isGenerated(sourceIndex))
|
2014-05-23 13:55:03 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
|
|
|
|
}
|
|
|
|
|
private:
|
2018-02-08 13:31:22 +01:00
|
|
|
CppTools::AbstractOverviewModel &m_sourceModel;
|
2014-05-23 13:55:03 -04:00
|
|
|
};
|
|
|
|
|
|
2014-08-27 11:50:53 +02:00
|
|
|
QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName)
|
2014-05-23 13:55:03 -04:00
|
|
|
{
|
2018-02-01 15:47:22 +01:00
|
|
|
auto *timer = new QTimer(parent);
|
2014-08-27 11:50:53 +02:00
|
|
|
timer->setObjectName(objectName);
|
2014-05-23 13:55:03 -04:00
|
|
|
timer->setSingleShot(true);
|
|
|
|
|
timer->setInterval(msInternal);
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2015-01-23 15:14:36 +01:00
|
|
|
namespace CppTools {
|
2014-05-23 13:55:03 -04:00
|
|
|
|
2015-01-23 14:06:42 +01:00
|
|
|
CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
|
2014-05-23 13:55:03 -04:00
|
|
|
: QObject(editorWidget)
|
|
|
|
|
, m_editorWidget(editorWidget)
|
|
|
|
|
, m_combo(new Utils::TreeViewComboBox)
|
|
|
|
|
{
|
2018-02-02 13:01:07 +01:00
|
|
|
m_model = CppModelManager::instance()->createOverviewModel();
|
2018-02-08 13:31:22 +01:00
|
|
|
m_proxyModel = new OverviewProxyModel(*m_model, this);
|
|
|
|
|
m_proxyModel->setSourceModel(m_model.get());
|
2018-02-01 15:47:22 +01:00
|
|
|
|
2014-05-23 13:55:03 -04:00
|
|
|
// Set up proxy model
|
2015-01-23 15:09:45 +01:00
|
|
|
if (CppTools::CppToolsSettings::instance()->sortedEditorDocumentOutline())
|
2014-05-23 13:55:03 -04:00
|
|
|
m_proxyModel->sort(0, Qt::AscendingOrder);
|
|
|
|
|
else
|
|
|
|
|
m_proxyModel->sort(-1, Qt::AscendingOrder); // don't sort yet, but set column for sortedOutline()
|
|
|
|
|
m_proxyModel->setDynamicSortFilter(true);
|
|
|
|
|
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
|
|
// Set up combo box
|
|
|
|
|
m_combo->setModel(m_proxyModel);
|
|
|
|
|
|
2018-04-23 17:03:50 +02:00
|
|
|
m_combo->setMinimumContentsLength(13);
|
2014-05-23 13:55:03 -04:00
|
|
|
QSizePolicy policy = m_combo->sizePolicy();
|
|
|
|
|
policy.setHorizontalPolicy(QSizePolicy::Expanding);
|
|
|
|
|
m_combo->setSizePolicy(policy);
|
|
|
|
|
m_combo->setMaxVisibleItems(40);
|
|
|
|
|
|
|
|
|
|
m_combo->setContextMenuPolicy(Qt::ActionsContextMenu);
|
|
|
|
|
m_sortAction = new QAction(tr("Sort Alphabetically"), m_combo);
|
|
|
|
|
m_sortAction->setCheckable(true);
|
|
|
|
|
m_sortAction->setChecked(isSorted());
|
2015-01-23 15:09:45 +01:00
|
|
|
connect(m_sortAction, &QAction::toggled,
|
|
|
|
|
CppTools::CppToolsSettings::instance(),
|
|
|
|
|
&CppTools::CppToolsSettings::setSortedEditorDocumentOutline);
|
2014-05-23 13:55:03 -04:00
|
|
|
m_combo->addAction(m_sortAction);
|
|
|
|
|
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(m_combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
|
|
|
|
|
this, &CppEditorOutline::gotoSymbolInEditor);
|
|
|
|
|
connect(m_combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
|
this, &CppEditorOutline::updateToolTip);
|
2014-05-23 13:55:03 -04:00
|
|
|
|
|
|
|
|
// Set up timers
|
2014-08-27 11:50:53 +02:00
|
|
|
m_updateTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
|
|
|
|
|
QLatin1String("CppEditorOutline::m_updateTimer"));
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(m_updateTimer, &QTimer::timeout, this, &CppEditorOutline::updateNow);
|
2018-03-22 13:05:59 +01:00
|
|
|
connect(m_model.get(), &CppTools::AbstractOverviewModel::needsUpdate, this,
|
|
|
|
|
&CppEditorOutline::updateNow);
|
2014-05-23 13:55:03 -04:00
|
|
|
|
2014-08-27 11:50:53 +02:00
|
|
|
m_updateIndexTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
|
|
|
|
|
QLatin1String("CppEditorOutline::m_updateIndexTimer"));
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(m_updateIndexTimer, &QTimer::timeout, this, &CppEditorOutline::updateIndexNow);
|
2014-05-23 13:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::update()
|
|
|
|
|
{
|
|
|
|
|
m_updateTimer->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppEditorOutline::isSorted() const
|
|
|
|
|
{
|
|
|
|
|
return m_proxyModel->sortColumn() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::setSorted(bool sort)
|
|
|
|
|
{
|
|
|
|
|
if (sort != isSorted()) {
|
|
|
|
|
if (sort)
|
|
|
|
|
m_proxyModel->sort(0, Qt::AscendingOrder);
|
|
|
|
|
else
|
|
|
|
|
m_proxyModel->sort(-1, Qt::AscendingOrder);
|
2017-09-30 07:12:57 +02:00
|
|
|
{
|
|
|
|
|
QSignalBlocker blocker(m_sortAction);
|
|
|
|
|
m_sortAction->setChecked(m_proxyModel->sortColumn() == 0);
|
|
|
|
|
}
|
2014-05-23 13:55:03 -04:00
|
|
|
updateIndexNow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 15:47:22 +01:00
|
|
|
CppTools::AbstractOverviewModel *CppEditorOutline::model() const
|
2014-05-23 13:55:03 -04:00
|
|
|
{
|
2018-02-08 13:31:22 +01:00
|
|
|
return m_model.get();
|
2014-05-23 13:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex CppEditorOutline::modelIndex()
|
|
|
|
|
{
|
|
|
|
|
if (!m_modelIndex.isValid()) {
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
m_editorWidget->convertPosition(m_editorWidget->position(), &line, &column);
|
|
|
|
|
m_modelIndex = indexForPosition(line, column);
|
|
|
|
|
emit modelIndexChanged(m_modelIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_modelIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *CppEditorOutline::widget() const
|
|
|
|
|
{
|
|
|
|
|
return m_combo;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 08:19:42 +01:00
|
|
|
QSharedPointer<CPlusPlus::Document> getDocument(const QString &filePath)
|
2014-05-23 13:55:03 -04:00
|
|
|
{
|
2014-09-15 00:12:27 +02:00
|
|
|
CppTools::CppModelManager *cmmi = CppTools::CppModelManager::instance();
|
2014-05-23 13:55:03 -04:00
|
|
|
const CPlusPlus::Snapshot snapshot = cmmi->snapshot();
|
2018-02-06 08:19:42 +01:00
|
|
|
return snapshot.document(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::updateNow()
|
|
|
|
|
{
|
2014-12-21 21:54:30 +02:00
|
|
|
const QString filePath = m_editorWidget->textDocument()->filePath().toString();
|
2018-02-06 08:19:42 +01:00
|
|
|
m_document = getDocument(filePath);
|
|
|
|
|
if (!m_document)
|
2014-05-23 13:55:03 -04:00
|
|
|
return;
|
|
|
|
|
|
2018-02-08 13:31:22 +01:00
|
|
|
if (m_document->editorRevision()
|
2018-02-01 15:47:22 +01:00
|
|
|
!= static_cast<unsigned>(m_editorWidget->document()->revision())) {
|
2014-05-23 13:55:03 -04:00
|
|
|
m_updateTimer->start();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 08:19:42 +01:00
|
|
|
if (!m_model->rebuild(filePath))
|
|
|
|
|
m_model->rebuild(m_document);
|
2014-05-23 13:55:03 -04:00
|
|
|
|
|
|
|
|
m_combo->view()->expandAll();
|
|
|
|
|
updateIndexNow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::updateIndex()
|
|
|
|
|
{
|
|
|
|
|
m_updateIndexTimer->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::updateIndexNow()
|
|
|
|
|
{
|
2018-02-08 13:31:22 +01:00
|
|
|
if (!m_document)
|
2014-05-23 13:55:03 -04:00
|
|
|
return;
|
|
|
|
|
|
2018-02-01 15:47:22 +01:00
|
|
|
const auto revision = static_cast<unsigned>(m_editorWidget->document()->revision());
|
2018-02-08 13:31:22 +01:00
|
|
|
if (m_document->editorRevision() != revision) {
|
2014-05-23 13:55:03 -04:00
|
|
|
m_updateIndexTimer->start();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_updateIndexTimer->stop();
|
|
|
|
|
|
|
|
|
|
m_modelIndex = QModelIndex(); //invalidate
|
|
|
|
|
QModelIndex comboIndex = modelIndex();
|
|
|
|
|
|
|
|
|
|
if (comboIndex.isValid()) {
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(m_combo);
|
2014-05-23 13:55:03 -04:00
|
|
|
m_combo->setCurrentIndex(m_proxyModel->mapFromSource(comboIndex));
|
|
|
|
|
updateToolTip();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::updateToolTip()
|
|
|
|
|
{
|
|
|
|
|
m_combo->setToolTip(m_combo->currentText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorOutline::gotoSymbolInEditor()
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex modelIndex = m_combo->view()->currentIndex();
|
|
|
|
|
const QModelIndex sourceIndex = m_proxyModel->mapToSource(modelIndex);
|
|
|
|
|
|
2018-02-08 13:31:22 +01:00
|
|
|
const Utils::Link link = m_model->linkFromIndex(sourceIndex);
|
2014-05-26 11:47:26 -04:00
|
|
|
if (!link.hasValidTarget())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Core::EditorManager::cutForwardNavigationHistory();
|
|
|
|
|
Core::EditorManager::addCurrentPositionToNavigationHistory();
|
2017-05-23 14:34:22 +02:00
|
|
|
m_editorWidget->gotoLine(link.targetLine, link.targetColumn, true, true);
|
2018-02-01 15:47:22 +01:00
|
|
|
emit m_editorWidget->activateEditor();
|
2014-05-23 13:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2018-02-08 13:31:22 +01:00
|
|
|
if (m_model->lineColumnFromIndex(index).line > line)
|
2014-05-23 13:55:03 -04:00
|
|
|
break;
|
|
|
|
|
lastIndex = index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastIndex != rootIndex) {
|
|
|
|
|
// recurse
|
|
|
|
|
lastIndex = indexForPosition(line, column, lastIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lastIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 15:14:36 +01:00
|
|
|
} // namespace CppTools
|
2014-05-23 13:55:03 -04:00
|
|
|
|
|
|
|
|
#include <cppeditoroutline.moc>
|