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

31 32
#include "cppoutline.h"

33
#include <cpptools/cppeditoroutline.h>
34

35
#include <cplusplus/OverviewModel.h>
36

Daniel Teske's avatar
Daniel Teske committed
37
#include <coreplugin/find/itemviewfind.h>
38
#include <utils/qtcassert.h>
39

40 41 42
#include <QDebug>
#include <QVBoxLayout>
#include <QMenu>
43

44 45
namespace CppEditor {
namespace Internal {
46 47 48 49 50 51

enum {
    debug = false
};

CppOutlineTreeView::CppOutlineTreeView(QWidget *parent) :
52
    Utils::NavigationTreeView(parent)
53 54
{
    setExpandsOnDoubleClick(false);
55 56
    setDragEnabled(true);
    setDragDropMode(QAbstractItemView::DragOnly);
57 58
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
void CppOutlineTreeView::contextMenuEvent(QContextMenuEvent *event)
{
    if (!event)
        return;

    QMenu contextMenu;

    contextMenu.addAction(tr("Expand All"), this, SLOT(expandAll()));
    contextMenu.addAction(tr("Collapse All"), this, SLOT(collapseAll()));

    contextMenu.exec(event->globalPos());

    event->accept();
}

74 75 76
CppOutlineFilterModel::CppOutlineFilterModel(CPlusPlus::OverviewModel *sourceModel, QObject *parent) :
    QSortFilterProxyModel(parent),
    m_sourceModel(sourceModel)
77
{
78
    setSourceModel(m_sourceModel);
79 80 81 82 83 84
}

bool CppOutlineFilterModel::filterAcceptsRow(int sourceRow,
                                             const QModelIndex &sourceParent) const
{
    // ignore artifical "<Select Symbol>" entry
85
    if (!sourceParent.isValid() && sourceRow == 0)
86
        return false;
87 88 89 90 91 92
    // 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;

93 94 95
    return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

96 97 98 99 100
Qt::DropActions CppOutlineFilterModel::supportedDragActions() const
{
    return sourceModel()->supportedDragActions();
}

101

102
CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) :
103 104 105
    TextEditor::IOutlineWidget(),
    m_editor(editor),
    m_treeView(new CppOutlineTreeView(this)),
106
    m_model(m_editor->outline()->model()),
107
    m_proxyModel(new CppOutlineFilterModel(m_model, this)),
108 109 110 111 112 113
    m_enableCursorSync(true),
    m_blockCursorSync(false)
{
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
Daniel Teske's avatar
Daniel Teske committed
114
    layout->addWidget(Core::ItemViewFind::createSearchableWrapper(m_treeView));
115 116 117
    setLayout(layout);

    m_treeView->setModel(m_proxyModel);
118
    setFocusProxy(m_treeView);
119

120 121
    connect(m_model, SIGNAL(modelReset()), this, SLOT(modelUpdated()));
    modelUpdated();
122

123
    connect(m_editor->outline(), SIGNAL(modelIndexChanged(QModelIndex)),
124
            this, SLOT(updateSelectionInTree(QModelIndex)));
125
    connect(m_treeView, SIGNAL(activated(QModelIndex)),
126
            this, SLOT(onItemActivated(QModelIndex)));
127 128
}

129 130 131 132 133
QList<QAction*> CppOutlineWidget::filterMenuActions() const
{
    return QList<QAction*>();
}

134 135 136 137
void CppOutlineWidget::setCursorSynchronization(bool syncWithCursor)
{
    m_enableCursorSync = syncWithCursor;
    if (m_enableCursorSync)
138
        updateSelectionInTree(m_editor->outline()->modelIndex());
139 140
}

141
void CppOutlineWidget::modelUpdated()
142
{
143
    m_treeView->expandAll();
144 145
}

146
void CppOutlineWidget::updateSelectionInTree(const QModelIndex &index)
147 148 149 150 151 152 153 154 155 156
{
    if (!syncCursor())
        return;

    QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);

    m_blockCursorSync = true;
    if (debug)
        qDebug() << "CppOutline - updating selection due to cursor move";

157
    m_treeView->setCurrentIndex(proxyIndex);
158
    m_treeView->scrollTo(proxyIndex);
159 160 161
    m_blockCursorSync = false;
}

162 163 164 165 166 167 168 169 170 171
void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex)
{
    QModelIndex index = m_proxyModel->mapToSource(proxyIndex);
    CPlusPlus::Symbol *symbol = m_model->symbolFromIndex(index);
    if (symbol) {
        m_blockCursorSync = true;

        if (debug)
            qDebug() << "CppOutline - moving cursor to" << symbol->line() << symbol->column() - 1;

172 173
        Core::EditorManager::cutForwardNavigationHistory();
        Core::EditorManager::addCurrentPositionToNavigationHistory();
174 175 176 177

        // line has to be 1 based, column 0 based!
        m_editor->gotoLine(symbol->line(), symbol->column() - 1);
        m_blockCursorSync = false;
178 179 180
    }
}

181
void CppOutlineWidget::onItemActivated(const QModelIndex &index)
182
{
183 184 185 186
    if (!index.isValid())
        return;

    updateTextCursor(index);
187 188 189
    m_editor->setFocus();
}

190 191 192 193 194 195 196
bool CppOutlineWidget::syncCursor()
{
    return m_enableCursorSync && !m_blockCursorSync;
}

bool CppOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
{
197
    if (qobject_cast<CppEditor*>(editor))
198 199 200 201 202 203
        return true;
    return false;
}

TextEditor::IOutlineWidget *CppOutlineWidgetFactory::createWidget(Core::IEditor *editor)
{
204
    CppEditor *cppEditor = qobject_cast<CppEditor*>(editor);
205
    CppEditorWidget *cppEditorWidget = qobject_cast<CppEditorWidget*>(cppEditor->widget());
206
    QTC_ASSERT(cppEditorWidget, return 0);
207

208
    CppOutlineWidget *widget = new CppOutlineWidget(cppEditorWidget);
209 210 211

    return widget;
}
212 213 214

} // namespace Internal
} // namespace CppEditor