qmljsoutline.cpp 5.96 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
13
14
15
16
17
18
19
20
21
22
23
#include <QtGui/QVBoxLayout>

using namespace QmlJS;

enum {
    debug = false
};

namespace QmlJSEditor {
namespace Internal {


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

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

43
44
45
46
47
48
49
50
51
52
53
54
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);
}

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

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

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

76
77
    m_treeView->setModel(m_filterModel);

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

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

84
    m_showBindingsAction = new QAction(this);
85
    m_showBindingsAction->setText(tr("Show all bindings"));
86
87
    m_showBindingsAction->setCheckable(true);
    m_showBindingsAction->setChecked(true);
88
    connect(m_showBindingsAction, SIGNAL(toggled(bool)), this, SLOT(setShowBindings(bool)));
89

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

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

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

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

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

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

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

123
124
125
126
127
128
129
130
131
132
133
134
135
136
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());
}

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

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

    m_blockCursorSync = true;
148
    m_treeView->selectionModel()->select(m_filterModel->mapFromSource(index), QItemSelectionModel::ClearAndSelect);
149
    m_treeView->scrollTo(index);
150
151
152
153
154
155
156
157
158
159
160
161
162
    m_blockCursorSync = false;
}

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


    if (!selection.indexes().isEmpty()) {
        QModelIndex index = selection.indexes().first();
        AST::SourceLocation location = index.data(QmlOutlineModel::SourceLocationRole).value<AST::SourceLocation>();

163
164
165
166
        Core::EditorManager *editorManager = Core::EditorManager::instance();
        editorManager->cutForwardNavigationHistory();
        editorManager->addCurrentPositionToNavigationHistory();

167
        QTextCursor textCursor = m_editor->textCursor();
168
169
        m_blockCursorSync = true;
        textCursor.setPosition(location.offset);
170
171
        m_editor->setTextCursor(textCursor);
        m_editor->centerCursor();
172
173
174
175
        m_blockCursorSync = false;
    }
}

176
177
178
179
180
181
void QmlJSOutlineWidget::setShowBindings(bool showBindings)
{
    m_filterModel->setFilterBindings(!showBindings);
    modelUpdated();
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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