texteditorview.cpp 8.54 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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "texteditorview.h"

28
29
#include "texteditorwidget.h"

30
#include <customnotifications.h>
31
32
#include <designmodecontext.h>
#include <designdocument.h>
33
#include <designersettings.h>
34
35
36
37
38
39
#include <modelnode.h>
#include <model.h>
#include <zoomaction.h>
#include <nodeabstractproperty.h>
#include <nodelistproperty.h>
#include <qmldesignerplugin.h>
40

41
42
43
44
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
45
#include <coreplugin/editormanager/editormanager.h>
46
47
#include <coreplugin/icore.h>

48
#include <texteditor/texteditor.h>
49
#include <texteditor/texteditorconstants.h>
50
51
#include <qmljseditor/qmljseditordocument.h>

52
#include <qmljs/qmljsreformatter.h>
53
#include <utils/changeset.h>
54
55
56
57
58
59
60
61
62

#include <QDebug>
#include <QPair>
#include <QString>
#include <QTimer>
#include <QPointer>

namespace QmlDesigner {

63
64
const char TEXTEDITOR_CONTEXT_ID[] = "QmlDesigner.TextEditorContext";

65
66
TextEditorView::TextEditorView(QObject *parent)
    : AbstractView(parent)
67
    , m_widget(new TextEditorWidget(this))
68
    , m_textEditorContext(new Internal::TextEditorContext(m_widget.get()))
69
{
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    Core::ICore::addContextObject(m_textEditorContext);

    Core::Context context(TEXTEDITOR_CONTEXT_ID);

    /*
     * We have to register our own active auto completion shortcut, because the original short cut will
     * use the cursor position of the original editor in the editor manager.
     */

    QAction *completionAction = new QAction(tr("Trigger Completion"), this);
    Core::Command *command = Core::ActionManager::registerAction(completionAction, TextEditor::Constants::COMPLETE_THIS, context);
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Space") : tr("Ctrl+Space")));

    connect(completionAction, &QAction::triggered, [this]() {
        if (m_widget->textEditor())
            m_widget->textEditor()->editorWidget()->invokeAssist(TextEditor::Completion);
    });
87
88
89
90
91
92
93
94
95
96
97
}

TextEditorView::~TextEditorView()
{
}

void TextEditorView::modelAttached(Model *model)
{
    Q_ASSERT(model);

    AbstractView::modelAttached(model);
98
99
100
101
102
103
104
105
106
107
108
109
110
111

    TextEditor::BaseTextEditor* textEditor = qobject_cast<TextEditor::BaseTextEditor*>(
                QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor()->duplicate());

    Core::Context context = textEditor->context();
    context.prepend(TEXTEDITOR_CONTEXT_ID);

    /*
     *  Set the context of the text editor, but we add another special context to override shortcuts.
     */

    m_textEditorContext->setContext(context);

    m_widget->setTextEditor(textEditor);
112
113
114
115
116
}

void TextEditorView::modelAboutToBeDetached(Model *model)
{
    AbstractView::modelAboutToBeDetached(model);
117
118
119
120

    m_widget->setTextEditor(0);

    QmlDesignerPlugin::instance()->emitCurrentTextEditorChanged(QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor());
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}

void TextEditorView::importsChanged(const QList<Import> &/*addedImports*/, const QList<Import> &/*removedImports*/)
{
}

void TextEditorView::nodeAboutToBeRemoved(const ModelNode &/*removedNode*/)
{
}

void TextEditorView::rootNodeTypeChanged(const QString &/*type*/, int /*majorVersion*/, int /*minorVersion*/)
{
}

void TextEditorView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/)
{
}

void TextEditorView::nodeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}

WidgetInfo TextEditorView::widgetInfo()
{
145
    return createWidgetInfo(m_widget.get(), 0, "TextEditor", WidgetInfo::CentralPane, 0, tr("Text Editor"), DesignerWidgetFlags::IgnoreErrors);
146
147
148
}

QString TextEditorView::contextHelpId() const
Thomas Hartmann's avatar
Thomas Hartmann committed
149
150
151
152
153
{
    return AbstractView::contextHelpId();
}

QString TextEditorView::qmlJSEditorHelpId() const
154
{
155
156
    if (m_widget->textEditor()) {
        QString contextHelpId = m_widget->textEditor()->contextHelpId();
157
        if (!contextHelpId.isEmpty())
158
            return m_widget->textEditor()->contextHelpId();
159
    }
Thomas Hartmann's avatar
Thomas Hartmann committed
160
    return QString();
161
162
163
164
165
166
167
168
169
}

void TextEditorView::nodeIdChanged(const ModelNode& /*node*/, const QString &/*newId*/, const QString &/*oldId*/)
{
}

void TextEditorView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/,
                                          const QList<ModelNode> &/*lastSelectedNodeList*/)
{
170
    m_widget->jumpTextCursorToSelectedModelNode();
171
172
}

173
void TextEditorView::customNotification(const AbstractView * /*view*/, const QString &identifier, const QList<ModelNode> &/*nodeList*/, const QList<QVariant> &/*data*/)
174
{
175
    if (identifier == StartRewriterApply)
176
        m_widget->setBlockCursorSelectionSynchronisation(true);
177
    else if (identifier == EndRewriterApply)
178
        m_widget->setBlockCursorSelectionSynchronisation(false);
179
180
}

181
void TextEditorView::documentMessagesChanged(const QList<DocumentMessage> &errors, const QList<DocumentMessage> &)
182
183
184
185
{
    if (errors.isEmpty()) {
        m_widget->clearStatusBar();
    } else {
186
        const DocumentMessage error = errors.first();
187
188
189
190
        m_widget->setStatusText(QString("%1 (Line: %2)").arg(error.description()).arg(error.line()));
    }
}

191
192
193
194
195
196
197
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
bool TextEditorView::changeToMoveTool()
{
    return true;
}

void TextEditorView::changeToDragTool()
{
}

bool TextEditorView::changeToMoveTool(const QPointF &/*beginPoint*/)
{
    return true;
}

void TextEditorView::changeToSelectionTool()
{
}

void TextEditorView::changeToResizeTool()
{
}

void TextEditorView::changeToTransformTools()
{
}

void TextEditorView::changeToCustomTool()
{
}

void TextEditorView::auxiliaryDataChanged(const ModelNode &/*node*/, const PropertyName &/*name*/, const QVariant &/*data*/)
{
}

void TextEditorView::instancesCompleted(const QVector<ModelNode> &/*completedNodeList*/)
{
}

void TextEditorView::instanceInformationsChanged(const QMultiHash<ModelNode, InformationName> &/*informationChangeHash*/)
{
}

void TextEditorView::instancesRenderImageChanged(const QVector<ModelNode> &/*nodeList*/)
{
}

void TextEditorView::instancesChildrenChanged(const QVector<ModelNode> &/*nodeList*/)
{
}

void TextEditorView::rewriterBeginTransaction()
{
}

void TextEditorView::rewriterEndTransaction()
{
}

249
250
251
252
253
254
void TextEditorView::gotoCursorPosition(int line, int column)
{
    if (m_widget)
        m_widget->gotoCursorPosition(line, column);
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
void TextEditorView::reformatFile()
{
    int oldLine = -1;

    if (m_widget)
        oldLine = m_widget->currentLine();

    QByteArray editorState = m_widget->textEditor()->saveState();

    auto document =
            qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::instance()->currentDocument());

    /* Reformat document if we have a .ui.qml file */
    if (document
            && document->filePath().toString().endsWith(".ui.qml")
270
            &&  DesignerSettings::getValue(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()) {
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

        const QString &newText = QmlJS::reformat(document->semanticInfo().document);
        QTextCursor tc(document->document());

        Utils::ChangeSet changeSet;
        changeSet.replace(0, document->plainText().length(), newText);
        changeSet.apply(&tc);

        m_widget->textEditor()->restoreState(editorState);

        if (m_widget)
            m_widget->gotoCursorPosition(oldLine, 0);
    }
}

286
287
288
void TextEditorView::instancePropertyChanged(const QList<QPair<ModelNode, PropertyName> > &/*propertyList*/)
{
}
289
290

} // namespace QmlDesigner
291