cppeditoroutline.cpp 8.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "cppeditoroutline.h"

#include "cppeditor.h"
#include "cppeditorplugin.h"

35
#include <cpptools/cppmodelmanager.h>
36 37 38 39 40 41 42 43 44

#include <cplusplus/OverviewModel.h>
#include <utils/treeviewcombobox.h>

#include <QSortFilterProxyModel>
#include <QTimer>

/*!
    \class CppEditor::Internal::CppEditorOutline
45
    \brief A helper class of CppEditorWidget that provides the outline model
46 47 48 49 50 51
           and widget, e.g. for the editor's tool bar.

    \internal

    The caller is responsible for deleting the widget returned by widget().

52
    \sa CppEditor::Internal::CppEditorWidget
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 */

enum { UpdateOutlineIntervalInMs = 500 };

namespace {

class OverviewProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT

public:
    OverviewProxyModel(CPlusPlus::OverviewModel *sourceModel, QObject *parent)
        : QSortFilterProxyModel(parent)
        , m_sourceModel(sourceModel)
    {
        setSourceModel(m_sourceModel);
    }

    bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const
    {
        // 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())
            return false;

        return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
    }
private:
    CPlusPlus::OverviewModel *m_sourceModel;
};

85
QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName)
86 87
{
    QTimer *timer = new QTimer(parent);
88
    timer->setObjectName(objectName);
89 90 91 92 93 94 95 96 97 98
    timer->setSingleShot(true);
    timer->setInterval(msInternal);
    return timer;
}

} // anonymous namespace

namespace CppEditor {
namespace Internal {

99
CppEditorOutline::CppEditorOutline(CppEditorWidget *editorWidget)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    : QObject(editorWidget)
    , m_editorWidget(editorWidget)
    , m_combo(new Utils::TreeViewComboBox)
    , m_model(new CPlusPlus::OverviewModel(this))
    , m_proxyModel(new OverviewProxyModel(m_model, this))
{
    // Set up proxy model
    if (CppEditorPlugin::instance()->sortedOutline())
        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);

    m_combo->setMinimumContentsLength(22);
    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());
    connect(m_sortAction, SIGNAL(toggled(bool)),
            CppEditorPlugin::instance(), SLOT(setSortedOutline(bool)));
    m_combo->addAction(m_sortAction);

    connect(m_combo, SIGNAL(activated(int)), this, SLOT(gotoSymbolInEditor()));
    connect(m_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateToolTip()));

    // Set up timers
135 136
    m_updateTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
                                       QLatin1String("CppEditorOutline::m_updateTimer"));
137 138
    connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateNow()));

139 140
    m_updateIndexTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
                                            QLatin1String("CppEditorOutline::m_updateIndexTimer"));
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    connect(m_updateIndexTimer, SIGNAL(timeout()), this, SLOT(updateIndexNow()));
}

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);
        bool block = m_sortAction->blockSignals(true);
        m_sortAction->setChecked(m_proxyModel->sortColumn() == 0);
        m_sortAction->blockSignals(block);
        updateIndexNow();
    }
}

CPlusPlus::OverviewModel *CppEditorOutline::model() const
{
    return m_model;
}

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

void CppEditorOutline::updateNow()
{
192
    CppTools::CppModelManager *cmmi = CppTools::CppModelManager::instance();
193 194 195 196
    if (!cmmi)
        return;

    const CPlusPlus::Snapshot snapshot = cmmi->snapshot();
197
    const QString filePath = m_editorWidget->textDocument()->filePath();
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    CPlusPlus::Document::Ptr document = snapshot.document(filePath);
    if (!document)
        return;

    if (document->editorRevision() != (unsigned) m_editorWidget->document()->revision()) {
        m_updateTimer->start();
        return;
    }

    m_model->rebuild(document);

    m_combo->view()->expandAll();
    updateIndexNow();
}

void CppEditorOutline::updateIndex()
{
    m_updateIndexTimer->start();
}

void CppEditorOutline::updateIndexNow()
{
    if (!m_model->document())
        return;

    const unsigned revision = m_editorWidget->document()->revision();
    if (m_model->document()->editorRevision() != revision) {
        m_updateIndexTimer->start();
        return;
    }

    m_updateIndexTimer->stop();

    m_modelIndex = QModelIndex(); //invalidate
    QModelIndex comboIndex = modelIndex();

    if (comboIndex.isValid()) {
        bool blocked = m_combo->blockSignals(true);
        m_combo->setCurrentIndex(m_proxyModel->mapFromSource(comboIndex));
        updateToolTip();
        m_combo->blockSignals(blocked);
    }
}

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);
    CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(sourceIndex);
    if (!symbol)
        return;

255
    const TextEditor::BaseTextEditorWidget::Link &link = CppEditorWidget::linkToSymbol(symbol);
256 257 258 259 260
    if (!link.hasValidTarget())
        return;

    Core::EditorManager::cutForwardNavigationHistory();
    Core::EditorManager::addCurrentPositionToNavigationHistory();
261
    m_editorWidget->gotoLine(link.targetLine, link.targetColumn);
262
    m_editorWidget->activateEditor();
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

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))
            break;
        lastIndex = index;
    }

    if (lastIndex != rootIndex) {
        // recurse
        lastIndex = indexForPosition(line, column, lastIndex);
    }

    return lastIndex;
}

} // namespace Internal
} // namespace CppEditor

#include <cppeditoroutline.moc>