summaryrefslogtreecommitdiff
path: root/src/plugins/qmljseditor/qmljsoutline.h
blob: 1571069afb12a3f78e32561dc3c7c52d42a29561 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef QMLJSOUTLINE_H
#define QMLJSOUTLINE_H

#include "qmljseditor.h"

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/inavigationwidgetfactory.h>
#include <texteditor/ioutlinewidget.h>
#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsicons.h>

#include <QtGui/QStandardItemModel>
#include <QtGui/QTreeView>
#include <QtGui/QWidget>

namespace Core {
class IEditor;
}

namespace QmlJS {
class Editor;
};

namespace QmlJSEditor {
namespace Internal {

class QmlOutlineModel : public QStandardItemModel
{
    Q_OBJECT
public:
    enum CustomRoles {
        SourceLocationRole = Qt::UserRole + 1
    };

    QmlOutlineModel(QObject *parent = 0);

    void startSync();

    QModelIndex enterElement(const QString &typeName, const QmlJS::AST::SourceLocation &location);
    void leaveElement();

    QModelIndex enterProperty(const QString &name, const QmlJS::AST::SourceLocation &location);
    void leaveProperty();

private:
    QStandardItem *enterNode(const QmlJS::AST::SourceLocation &location);
    void leaveNode();

    QStandardItem *parentItem();

    QList<int> m_treePos;
    QStandardItem *m_currentItem;
    QmlJS::Icons m_icons;
};

class QmlJSOutlineTreeView : public QTreeView
{
    Q_OBJECT
public:
    QmlJSOutlineTreeView(QWidget *parent = 0);
};


class QmlJSOutlineWidget : public TextEditor::IOutlineWidget
{
    Q_OBJECT
public:
    QmlJSOutlineWidget(QWidget *parent = 0);

    void setEditor(QmlJSTextEditor *editor);

    // IOutlineWidget
    virtual void setCursorSynchronization(bool syncWithCursor);

private slots:
    void updateOutline(const QmlJSEditor::Internal::SemanticInfo &semanticInfo);
    void updateSelectionInTree();
    void updateSelectionInText(const QItemSelection &selection);

private:
    QModelIndex indexForPosition(const QModelIndex &rootIndex, int cursorPosition);
    bool offsetInsideLocation(quint32 offset, const QmlJS::AST::SourceLocation &location);
    bool syncCursor();

private:
    QmlJSOutlineTreeView *m_treeView;
    QAbstractItemModel *m_model;
    QWeakPointer<QmlJSTextEditor> m_editor;

    bool m_enableCursorSync;
    bool m_blockCursorSync;
};

class QmlJSOutlineWidgetFactory : public TextEditor::IOutlineWidgetFactory
{
    Q_OBJECT
public:
    bool supportsEditor(Core::IEditor *editor) const;
    TextEditor::IOutlineWidget *createWidget(Core::IEditor *editor);
};

} // namespace Internal
} // namespace QmlJSEditor

Q_DECLARE_METATYPE(QmlJS::AST::SourceLocation);

#endif // QMLJSOUTLINE_H