qmljsoutline.cpp 6.65 KB
Newer Older
1
#include "qmljsoutline.h"
2
#include "qmloutlinemodel.h"
3
#include "qmljsoutlinetreeview.h"
4

5
#include <coreplugin/icore.h>
6
#include <coreplugin/ifile.h>
7
#include <coreplugin/editormanager/editormanager.h>
8
9
10

#include <QtCore/QDebug>
#include <QtCore/QSettings>
11
#include <QtGui/QAction>
12
#include <QtGui/QVBoxLayout>
13
#include <QtGui/QTextBlock>
14
15
16
17
18
19
20
21
22
23
24

using namespace QmlJS;

enum {
    debug = false
};

namespace QmlJSEditor {
namespace Internal {


25
26
27
QmlJSOutlineFilterModel::QmlJSOutlineFilterModel(QObject *parent) :
    QSortFilterProxyModel(parent)
{
28
    setDynamicSortFilter(true);
29
30
31
32
33
34
35
}

bool QmlJSOutlineFilterModel::filterAcceptsRow(int sourceRow,
                                               const QModelIndex &sourceParent) const
{
    if (m_filterBindings) {
        QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
36
        QVariant itemType = sourceIndex.data(QmlOutlineModel::ItemTypeRole);
37
        if (itemType == QmlOutlineModel::NonElementBindingType) {
38
39
40
41
42
43
            return false;
        }
    }
    return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

44
45
46
47
48
49
50
51
52
53
54
55
QVariant QmlJSOutlineFilterModel::data(const QModelIndex &index, int role) const
{
    if (role == QmlOutlineModel::AnnotationRole) {
        // Don't show element id etc behind element if the property is also visible
        if (!filterBindings()
                && index.data(QmlOutlineModel::ItemTypeRole) == QmlOutlineModel::ElementType) {
            return QVariant();
        }
    }
    return QSortFilterProxyModel::data(index, role);
}

56
57
58
59
60
61
62
bool QmlJSOutlineFilterModel::filterBindings() const
{
    return m_filterBindings;
}

void QmlJSOutlineFilterModel::setFilterBindings(bool filterBindings)
{
63
64
    m_filterBindings = filterBindings;
    invalidateFilter();
65
66
}

67
68
QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) :
    TextEditor::IOutlineWidget(parent),
69
    m_treeView(new QmlJSOutlineTreeView(this)),
70
    m_filterModel(new QmlJSOutlineFilterModel(this)),
71
    m_editor(0),
72
73
74
    m_enableCursorSync(true),
    m_blockCursorSync(false)
{
75
76
    m_filterModel->setFilterBindings(false);

77
78
    m_treeView->setModel(m_filterModel);

79
80
81
82
83
84
    QVBoxLayout *layout = new QVBoxLayout;

    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(m_treeView);

85
    m_showBindingsAction = new QAction(this);
Leena Miettinen's avatar
Leena Miettinen committed
86
    m_showBindingsAction->setText(tr("Show All Bindings"));
87
88
    m_showBindingsAction->setCheckable(true);
    m_showBindingsAction->setChecked(true);
89
    connect(m_showBindingsAction, SIGNAL(toggled(bool)), this, SLOT(setShowBindings(bool)));
90

91
92
93
94
95
96
97
    setLayout(layout);
}

void QmlJSOutlineWidget::setEditor(QmlJSTextEditor *editor)
{
    m_editor = editor;

98
    m_filterModel->setSourceModel(m_editor->outlineModel());
99
100
    modelUpdated();

101
102
    connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(updateSelectionInText(QItemSelection)));
103

104
    connect(m_editor, SIGNAL(outlineModelIndexChanged(QModelIndex)),
105
            this, SLOT(updateSelectionInTree(QModelIndex)));
106
    connect(m_editor->outlineModel(), SIGNAL(updated()),
107
            this, SLOT(modelUpdated()));
108
109
}

110
111
112
113
114
115
116
QList<QAction*> QmlJSOutlineWidget::filterMenuActions() const
{
    QList<QAction*> list;
    list.append(m_showBindingsAction);
    return list;
}

117
118
119
120
void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
{
    m_enableCursorSync = syncWithCursor;
    if (m_enableCursorSync)
121
        updateSelectionInTree(m_editor->outlineModelIndex());
122
123
}

124
125
126
127
128
129
130
131
132
133
134
135
136
137
void QmlJSOutlineWidget::restoreSettings(int position)
{
    QSettings *settings = Core::ICore::instance()->settings();
    bool showBindings = settings->value("QmlJSOutline."+QString::number(position)+".ShowBindings", true).toBool();
    m_showBindingsAction->setChecked(showBindings);
}

void QmlJSOutlineWidget::saveSettings(int position)
{
    QSettings *settings = Core::ICore::instance()->settings();
    settings->setValue("QmlJSOutline."+QString::number(position)+".ShowBindings",
                       m_showBindingsAction->isChecked());
}

138
void QmlJSOutlineWidget::modelUpdated()
139
140
141
142
{
    m_treeView->expandAll();
}

143
void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index)
144
145
146
147
148
{
    if (!syncCursor())
        return;

    m_blockCursorSync = true;
149
150
151
152
153
154
155
156
157
158

    QModelIndex baseIndex = index;
    QModelIndex filterIndex = m_filterModel->mapFromSource(baseIndex);
    while (baseIndex.isValid() && !filterIndex.isValid()) { // Search for ancestor index actually shown
        baseIndex = baseIndex.parent();
        filterIndex = m_filterModel->mapFromSource(baseIndex);
    }

    m_treeView->selectionModel()->select(filterIndex, QItemSelectionModel::ClearAndSelect);
    m_treeView->scrollTo(filterIndex);
159
160
161
162
163
164
165
166
167
168
    m_blockCursorSync = false;
}

void QmlJSOutlineWidget::updateSelectionInText(const QItemSelection &selection)
{
    if (!syncCursor())
        return;

    if (!selection.indexes().isEmpty()) {
        QModelIndex index = selection.indexes().first();
169
170
171
172
173
174
        QModelIndex sourceIndex = m_filterModel->mapToSource(index);

        AST::SourceLocation location = m_editor->outlineModel()->sourceLocation(sourceIndex);

        if (!location.isValid())
            return;
175

176
177
178
179
180
        const QTextBlock lastBlock = m_editor->document()->lastBlock();
        const uint textLength = lastBlock.position() + lastBlock.length();
        if (location.offset >= textLength)
            return;

181
182
183
184
        Core::EditorManager *editorManager = Core::EditorManager::instance();
        editorManager->cutForwardNavigationHistory();
        editorManager->addCurrentPositionToNavigationHistory();

185
        QTextCursor textCursor = m_editor->textCursor();
186
187
        m_blockCursorSync = true;
        textCursor.setPosition(location.offset);
188
189
        m_editor->setTextCursor(textCursor);
        m_editor->centerCursor();
190
191
192
193
        m_blockCursorSync = false;
    }
}

194
195
196
197
void QmlJSOutlineWidget::setShowBindings(bool showBindings)
{
    m_filterModel->setFilterBindings(!showBindings);
    modelUpdated();
198
    updateSelectionInTree(m_editor->outlineModelIndex());
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
bool QmlJSOutlineWidget::syncCursor()
{
    return m_enableCursorSync && !m_blockCursorSync;
}

bool QmlJSOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
{
    if (qobject_cast<QmlJSEditorEditable*>(editor))
        return true;
    return false;
}

TextEditor::IOutlineWidget *QmlJSOutlineWidgetFactory::createWidget(Core::IEditor *editor)
{
    QmlJSOutlineWidget *widget = new QmlJSOutlineWidget;

    QmlJSEditorEditable *qmlJSEditable = qobject_cast<QmlJSEditorEditable*>(editor);
    QmlJSTextEditor *qmlJSEditor = qobject_cast<QmlJSTextEditor*>(qmlJSEditable->widget());
    Q_ASSERT(qmlJSEditor);

    widget->setEditor(qmlJSEditor);

    return widget;
}

} // namespace Internal
} // namespace QmlJSEditor