quicktoolbar.cpp 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "quicktoolbar.h"
#include <contextpanewidget.h>
#include <quicktoolbarsettingspage.h>

#include <utils/changeset.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljspropertyreader.h>
#include <qmljs/qmljsrewriter.h>
#include <qmljs/qmljsindenter.h>
#include <qmljs/qmljslookupcontext.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljsscopebuilder.h>
15
#include <qmljs/qmljsevaluate.h>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <texteditor/basetexteditor.h>
#include <texteditor/tabsettings.h>
#include <coreplugin/icore.h>
#include <customcolordialog.h>
#include <QDebug>

using namespace QmlJS;
using namespace AST;
using namespace QmlEditorWidgets;

namespace QmlJSEditor {

static inline QString textAt(const Document* doc,
                                      const SourceLocation &from,
                                      const SourceLocation &to)
{
    return doc->source().mid(from.offset, to.end() - from.begin());
}

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
static inline const Interpreter::ObjectValue * getPropertyChangesTarget(Node *node, LookupContext::Ptr lookupContext)
{
    UiObjectInitializer *initializer = 0;
    if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(node))
        initializer = definition->initializer;
    if (UiObjectBinding *binding = cast<UiObjectBinding *>(node))
        initializer = binding->initializer;
    if (initializer) {
        for (UiObjectMemberList *members = initializer->members; members; members = members->next) {
            if (UiScriptBinding *scriptBinding = cast<UiScriptBinding *>(members->member)) {
                if (scriptBinding->qualifiedId
                    && scriptBinding->qualifiedId->name->asString() == QLatin1String("target")
                    && ! scriptBinding->qualifiedId->next) {
                    if (ExpressionStatement *expressionStatement = cast<ExpressionStatement *>(scriptBinding->statement)) {
                        Evaluate evaluator(lookupContext->context());
                        const Interpreter::Value *targetValue = evaluator(expressionStatement->expression);
                        if (const Interpreter::ObjectValue *targetObject = Interpreter::value_cast<const Interpreter::ObjectValue *>(targetValue)) {
                            return targetObject;
                        } else {
                            return 0;
                        }
                    }
                }
            }
        }
    }
Thomas Hartmann's avatar
Thomas Hartmann committed
61
    return 0;
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
QuickToolBar::QuickToolBar(QObject *parent) : ::QmlJS::IContextPane(parent), m_blockWriting(false)
{
    m_node = 0;
    contextWidget();

    m_propertyOrder
               << QLatin1String("id")
               << QLatin1String("name")
               << QLatin1String("target")
               << QLatin1String("property")
               << QLatin1String("x")
               << QLatin1String("y")
               << QLatin1String("width")
               << QLatin1String("height")
               << QLatin1String("position")
               << QLatin1String("color")
               << QLatin1String("radius")
               << QLatin1String("text")
               << QLatin1String("font.family")
               << QLatin1String("font.bold")
               << QLatin1String("font.italic")
               << QLatin1String("font.underline")
               << QLatin1String("font.strikeout")
               << QString::null
               << QLatin1String("states")
               << QLatin1String("transitions")
               ;
}

QuickToolBar::~QuickToolBar()
{
    //if the pane was never activated the widget is not in a widget tree
    if (!m_widget.isNull())
        delete m_widget.data();
        m_widget.clear();
}

101
void QuickToolBar::apply(TextEditor::BaseTextEditorEditable *editor, Document::Ptr document, LookupContext::Ptr lookupContext, AST::Node *node, bool update, bool force)
102
103
104
105
106
107
{
    if (!QuickToolBarSettings::get().enableContextPane && !force && !update) {
        contextWidget()->hide();
        return;
    }

108
    if (document.isNull())
109
110
111
112
113
114
115
        return;

    if (update && editor != m_editor)
        return; //do not update for different editor

    m_blockWriting = true;

116
    const Interpreter::ObjectValue *scopeObject = document->bind()->findQmlObject(node);
117

118
119
120
121
122
123
    if (!lookupContext.isNull()) {
        m_prototypes.clear();
        while (scopeObject) {
            m_prototypes.append(scopeObject->className());
            scopeObject =  scopeObject->prototype(lookupContext->context());
        }
124

125
126
127
128
129
130
131
        if (m_prototypes.contains("PropertyChanges")) {
            const Interpreter::ObjectValue *targetObject = getPropertyChangesTarget(node, lookupContext);
            m_prototypes.clear();
            while (targetObject) {
                m_prototypes.append(targetObject->className());
                targetObject =  targetObject->prototype(lookupContext->context());
            }
132
133
134
        }
    }

135
    setEnabled(document->isParsedCorrectly());
136
137
138
139
140
141
142
143
144
    m_editor = editor;
    contextWidget()->setParent(editor->widget()->parentWidget());
    contextWidget()->colorDialog()->setParent(editor->widget()->parentWidget());

    if (cast<UiObjectDefinition*>(node) || cast<UiObjectBinding*>(node)) {
        UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
        UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);

        QString name;
Thomas Hartmann's avatar
Thomas Hartmann committed
145
146
147
        quint32 offset = 0;
        quint32 end = 0;
        UiObjectInitializer *initializer = 0;
148
149
150
151
152
153
154
155
156
157
158
159
        if (objectDefinition) {
            name = objectDefinition->qualifiedTypeNameId->name->asString();
            initializer = objectDefinition->initializer;
            offset = objectDefinition->firstSourceLocation().offset;
            end = objectDefinition->lastSourceLocation().end();
        } else if (objectBinding) {
            name = objectBinding->qualifiedTypeNameId->name->asString();
            initializer = objectBinding->initializer;
            offset = objectBinding->firstSourceLocation().offset;
            end = objectBinding->lastSourceLocation().end();
        }

160
161
162
163
164
165
166
        if (lookupContext.isNull()) {
            if (name != m_oldType)
                m_prototypes.clear();
        }

        m_oldType = name;

167
168
        m_prototypes.append(name);

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        int line1;
        int column1;
        int line2;
        int column2;
        m_editor->convertPosition(offset, &line1, &column1); //get line
        m_editor->convertPosition(end, &line2, &column2); //get line

        QRegion reg;
        if (line1 > -1 && line2 > -1)
            reg = m_editor->editor()->translatedLineRegion(line1 - 1, line2);

        QRect rect;
        rect.setHeight(widget()->height() + 10);
        rect.setWidth(reg.boundingRect().width() - reg.boundingRect().left());
        rect.moveTo(reg.boundingRect().topLeft());
        reg = reg.intersect(rect);

186
        if (contextWidget()->acceptsType(m_prototypes)) {
187
            m_node = 0;
188
            PropertyReader propertyReader(document, initializer);
189
190
191
192
193
194
195
196
197
198
            QTextCursor tc(editor->editor()->document());
            tc.setPosition(offset); 
            QPoint p1 = editor->editor()->mapToParent(editor->editor()->viewport()->mapToParent(editor->editor()->cursorRect(tc).topLeft()) - QPoint(0, contextWidget()->height() + 10));
            tc.setPosition(end);
            QPoint p2 = editor->editor()->mapToParent(editor->editor()->viewport()->mapToParent(editor->editor()->cursorRect(tc).bottomLeft()) + QPoint(0, 10));
            QPoint offset = QPoint(10, 0);
            if (reg.boundingRect().width() < 400)
                offset = QPoint(400 - reg.boundingRect().width() + 10 ,0);
            QPoint p3 = editor->editor()->mapToParent(editor->editor()->viewport()->mapToParent(reg.boundingRect().topRight()) + offset);
            p2.setX(p1.x());
199
            contextWidget()->setType(m_prototypes);
200
201
202
203
204
            if (!update)
                contextWidget()->activate(p3 , p1, p2, QuickToolBarSettings::get().pinContextPane);
            else
                contextWidget()->rePosition(p3 , p1, p2, QuickToolBarSettings::get().pinContextPane);
            contextWidget()->setOptions(QuickToolBarSettings::get().enableContextPane, QuickToolBarSettings::get().pinContextPane);
205
            contextWidget()->setPath(document->path());
206
            contextWidget()->setProperties(&propertyReader); 
207
            m_doc = document;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
            m_node = node;
        } else {
            contextWidget()->setParent(0);
            contextWidget()->hide();
            contextWidget()->colorDialog()->hide();
        }
    } else {
        contextWidget()->setParent(0);
        contextWidget()->hide();
        contextWidget()->colorDialog()->hide();
    }

    m_blockWriting = false;

}

Thomas Hartmann's avatar
Thomas Hartmann committed
224
bool QuickToolBar::isAvailable(TextEditor::BaseTextEditorEditable *, Document::Ptr document, AST::Node *node)
225
{
226
    if (document.isNull())
227
228
229
230
231
        return false;

    if (!node)
        return false;

232
    QString name;
233

234
235
236
237
    UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
    UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);
    if (objectDefinition) {
        name = objectDefinition->qualifiedTypeNameId->name->asString();
238

239
240
    } else if (objectBinding) {
        name = objectBinding->qualifiedTypeNameId->name->asString();
241
    }
242

243
244
    QStringList prototypes;
    prototypes.append(name);
245

246
    if (prototypes.contains("Rectangle") ||
247
248
249
250
251
            prototypes.contains("Image") ||
            prototypes.contains("BorderImage") ||
            prototypes.contains("TextEdit") ||
            prototypes.contains("TextInput") ||
            prototypes.contains("PropertyAnimation") ||
252
253
254
            prototypes.contains("NumberAnimation") ||
            prototypes.contains("Text") ||
            prototypes.contains("PropertyChanges"))
255
        return true;
256

257
    return false;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
}

void QuickToolBar::setProperty(const QString &propertyName, const QVariant &value)
{

    QString stringValue = value.toString();
    if (value.type() == QVariant::Color)
        stringValue = QChar('\"') + value.toString() + QChar('\"');

    if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
        UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
        UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);

        UiObjectInitializer *initializer;
        if (objectDefinition)
            initializer = objectDefinition->initializer;
        else if (objectBinding)
            initializer = objectBinding->initializer;

        Utils::ChangeSet changeSet;
        Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);

        int line = -1;
        int endLine;

        Rewriter::BindingType bindingType = Rewriter::ScriptBinding;

        if (stringValue.contains("{") && stringValue.contains("}"))
            bindingType = Rewriter::ObjectBinding;

        PropertyReader propertyReader(m_doc, initializer);
        if (propertyReader.hasProperty(propertyName)) {
            rewriter.changeBinding(initializer, propertyName, stringValue, bindingType);
        } else {
            rewriter.addBinding(initializer, propertyName, stringValue, bindingType);
        }

        int column;

        int changeSetPos = changeSet.operationList().last().pos1;
        int changeSetLength = changeSet.operationList().last().text.length();
        QTextCursor tc = m_editor->editor()->textCursor();
        tc.beginEditBlock();
        changeSet.apply(&tc);

        m_editor->convertPosition(changeSetPos, &line, &column); //get line
        m_editor->convertPosition(changeSetPos + changeSetLength, &endLine, &column); //get line

        if (line > 0) {
            TextEditor::TabSettings ts = m_editor->editor()->tabSettings();
            QmlJSIndenter indenter;
            indenter.setTabSize(ts.m_tabSize);
            indenter.setIndentSize(ts.m_indentSize);

            for (int i=line;i<=endLine;i++) {
                QTextBlock start = m_editor->editor()->document()->findBlockByNumber(i);
                QTextBlock end = m_editor->editor()->document()->findBlockByNumber(i);

                if (end.isValid()) {
                    const int indent = indenter.indentForBottomLine(m_editor->editor()->document()->begin(), end.next(), QChar::Null);
                    ts.indentLine(start, indent);
                }
            }
        }
        tc.endEditBlock();
    }
}

void QuickToolBar::removeProperty(const QString &propertyName)
{
    if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
        UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
        UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);

        UiObjectInitializer *initializer;
        if (objectDefinition)
            initializer = objectDefinition->initializer;
        else if (objectBinding)
            initializer = objectBinding->initializer;

        PropertyReader propertyReader(m_doc, initializer);
        if (propertyReader.hasProperty(propertyName)) {
            Utils::ChangeSet changeSet;
            Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);
            rewriter.removeBindingByName(initializer, propertyName);
            QTextCursor tc(m_editor->editor()->document());
            changeSet.apply(&tc);
        }
    }
}

void QuickToolBar::setEnabled(bool b)
{
    if (m_widget)
        contextWidget()->currentWidget()->setEnabled(b);
    if (!b)
        widget()->hide();
}


QWidget* QuickToolBar::widget()
{
    return contextWidget();
}


void QuickToolBar::onPropertyChanged(const QString &name, const QVariant &value)
{
    if (m_blockWriting)
        return;
    if (!m_doc)
        return;

    setProperty(name, value);
    m_doc.clear(); //the document is outdated
}

void QuickToolBar::onPropertyRemovedAndChange(const QString &remove, const QString &change, const QVariant &value, bool removeFirst)
{
    if (m_blockWriting)
        return;

    if (!m_doc)
        return;

    QTextCursor tc(m_editor->editor()->document());
    tc.beginEditBlock();

    if (removeFirst) {
        removeProperty(remove);
        setProperty(change, value);
    } else {
        setProperty(change, value);
        removeProperty(remove);
    }


    tc.endEditBlock();

    m_doc.clear(); //the document is outdated

}

void QuickToolBar::onPinnedChanged(bool b)
{
    QuickToolBarSettings settings = QuickToolBarSettings::get();
    settings.pinContextPane = b;
    settings.set();
}

void QuickToolBar::onEnabledChanged(bool b)
{
    QuickToolBarSettings settings = QuickToolBarSettings::get();
    settings.pinContextPane = b;
    settings.enableContextPane = b;
    settings.set();
}

ContextPaneWidget* QuickToolBar::contextWidget()
{
    if (m_widget.isNull()) { //lazily recreate widget
        m_widget = new ContextPaneWidget;
        connect(m_widget.data(), SIGNAL(propertyChanged(QString,QVariant)), this, SLOT(onPropertyChanged(QString,QVariant)));
        connect(m_widget.data(), SIGNAL(removeProperty(QString)), this, SLOT(onPropertyRemoved(QString)));
        connect(m_widget.data(), SIGNAL(removeAndChangeProperty(QString,QString,QVariant, bool)), this, SLOT(onPropertyRemovedAndChange(QString,QString,QVariant, bool)));
        connect(m_widget.data(), SIGNAL(enabledChanged(bool)), this, SLOT(onEnabledChanged(bool)));
        connect(m_widget.data(), SIGNAL(pinnedChanged(bool)), this, SLOT(onPinnedChanged(bool)));
425
        connect(m_widget.data(), SIGNAL(closed()), this, SIGNAL(closed()));
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    }
    return m_widget.data();
}

void QuickToolBar::onPropertyRemoved(const QString &propertyName)
{
    if (m_blockWriting)
        return;

    if (!m_doc)
        return;

    removeProperty(propertyName);
    m_doc.clear(); //the document is outdated
}

} //QmlDesigner