designdocument.cpp 21.6 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
Marco Bubke's avatar
Marco Bubke committed
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
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "designdocument.h"
#include "designdocumentview.h"
32
#include "documentmanager.h"
Marco Bubke's avatar
Marco Bubke committed
33
34
35
36
37
38
39

#include <metainfo.h>
#include <qmlobjectnode.h>
#include <rewritingexception.h>
#include <nodelistproperty.h>
#include <variantproperty.h>
#include <qmldesignerplugin.h>
40
#include <viewmanager.h>
41
#include <nodeinstanceview.h>
Marco Bubke's avatar
Marco Bubke committed
42
43
44
45

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
46
#include <projectexplorer/session.h>
Marco Bubke's avatar
Marco Bubke committed
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
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtsupportconstants.h>
#include <qtsupport/qtversionmanager.h>

#include <QFileInfo>
#include <QUrl>
#include <QDebug>

#include <QMessageBox>
#include <QPlainTextEdit>
#include <QApplication>

enum {
    debug = false
};

namespace QmlDesigner {


/**
  \class QmlDesigner::DesignDocument

  DesignDocument acts as a facade to a model representing a qml document,
  and the different views/widgets accessing it.
  */
DesignDocument::DesignDocument(QObject *parent) :
        QObject(parent),
        m_documentModel(Model::create("QtQuick.Item", 1, 0)),
        m_subComponentManager(new SubComponentManager(m_documentModel.data(), this)),
        m_rewriterView (new RewriterView(RewriterView::Amend, m_documentModel.data())),
        m_documentLoaded(false),
        m_qtVersionId(-1)
{
}

DesignDocument::~DesignDocument()
{
}

Model *DesignDocument::currentModel() const
{
88
89
90
91
    if (m_inFileComponentModel)
        return m_inFileComponentModel.data();

    return m_documentModel.data();
Marco Bubke's avatar
Marco Bubke committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

Model *DesignDocument::documentModel() const
{
    return m_documentModel.data();
}

QWidget *DesignDocument::centralWidget() const
{
    return qobject_cast<QWidget*>(parent());
}

QString DesignDocument::pathToQt() const
{
hjk's avatar
hjk committed
106
    QtSupport::BaseQtVersion *activeQtVersion = QtSupport::QtVersionManager::version(m_qtVersionId);
Marco Bubke's avatar
Marco Bubke committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    if (activeQtVersion && (activeQtVersion->qtVersion() >= QtSupport::QtVersionNumber(4, 7, 1))
            && (activeQtVersion->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                || activeQtVersion->type() == QLatin1String(QtSupport::Constants::SIMULATORQT)))
        return activeQtVersion->qmakeProperty("QT_INSTALL_DATA");

    return QString();
}

const ViewManager &DesignDocument::viewManager() const
{
    return QmlDesignerPlugin::instance()->viewManager();
}

ViewManager &DesignDocument::viewManager()
{
    return QmlDesignerPlugin::instance()->viewManager();
}

static ComponentTextModifier *createComponentTextModifier(TextModifier *originalModifier,
                                                          RewriterView *rewriterView,
                                                          const QString &componentText,
                                                          const ModelNode &componentNode)
{
    bool explicitComponent = componentText.contains("Component");

    ModelNode rootModelNode = rewriterView->rootModelNode();

    int componentStartOffset;
    int componentEndOffset;

    int rootStartOffset = rewriterView->nodeOffset(rootModelNode);

    if (explicitComponent) { //the component is explciit we have to find the first definition inside
        componentStartOffset = rewriterView->firstDefinitionInsideOffset(componentNode);
        componentEndOffset = componentStartOffset + rewriterView->firstDefinitionInsideLength(componentNode);
    } else { //the component is implicit
        componentStartOffset = rewriterView->nodeOffset(componentNode);
        componentEndOffset = componentStartOffset + rewriterView->nodeLength(componentNode);
    }

    return new ComponentTextModifier (originalModifier, componentStartOffset, componentEndOffset, rootStartOffset);
}

Marco Bubke's avatar
Marco Bubke committed
150
bool DesignDocument::loadInFileComponent(const ModelNode &componentNode)
Marco Bubke's avatar
Marco Bubke committed
151
152
153
154
155
156
157
158
{
    QString componentText = rewriterView()->extractText(QList<ModelNode>() << componentNode).value(componentNode);

    if (componentText.isEmpty())
        return false;

    if (!componentNode.isRootNode()) {
        //change to subcomponent model
159
        changeToInFileComponentModel(createComponentTextModifier(m_documentTextModifier.data(), rewriterView(), componentText, componentNode));
Marco Bubke's avatar
Marco Bubke committed
160
161
162
163
164
    }

    return true;
}

Marco Bubke's avatar
Marco Bubke committed
165
AbstractView *DesignDocument::view()
166
{
Marco Bubke's avatar
Marco Bubke committed
167
    return viewManager().nodeInstanceView();
168
169
}

170
171
172
173
174
175
176
177
Model* DesignDocument::createInFileComponentModel()
{
    Model *model = Model::create("QtQuick.Item", 1, 0);
    model->setFileUrl(m_documentModel->fileUrl());

    return model;
}

Marco Bubke's avatar
Marco Bubke committed
178
179
180
181
182
183
184
185
186
187
/*!
  Returns any errors that happened when parsing the latest qml file.
  */
QList<RewriterView::Error> DesignDocument::qmlSyntaxErrors() const
{
    return m_rewriterView->errors();
}

bool DesignDocument::hasQmlSyntaxErrors() const
{
188
    return currentModel()->rewriterView() && !currentModel()->rewriterView()->errors().isEmpty();
Marco Bubke's avatar
Marco Bubke committed
189
190
191
192
193
194
195
196
197
}

QString DesignDocument::displayName() const
{
    return fileName();
}

QString DesignDocument::simplfiedDisplayName() const
{
Orgad Shaneh's avatar
Orgad Shaneh committed
198
    if (rootModelNode().id().isEmpty())
Marco Bubke's avatar
Marco Bubke committed
199
        return rootModelNode().id();
Orgad Shaneh's avatar
Orgad Shaneh committed
200
    else
Marco Bubke's avatar
Marco Bubke committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        return rootModelNode().simplifiedTypeName();

    QStringList list = displayName().split(QLatin1Char('/'));
    return list.last();
}

void DesignDocument::updateFileName(const QString & /*oldFileName*/, const QString &newFileName)
{
    if (m_documentModel)
        m_documentModel->setFileUrl(QUrl::fromLocalFile(newFileName));

    if (m_inFileComponentModel)
        m_inFileComponentModel->setFileUrl(QUrl::fromLocalFile(newFileName));

    viewManager().setItemLibraryViewResourcePath(QFileInfo(newFileName).absolutePath());

    emit displayNameChanged(displayName());
}

QString DesignDocument::fileName() const
{
222
    return editor()->document()->filePath();
Marco Bubke's avatar
Marco Bubke committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
}

int DesignDocument::qtVersionId() const
{
    return m_qtVersionId;
}

bool DesignDocument::isDocumentLoaded() const
{
    return m_documentLoaded;
}

void DesignDocument::resetToDocumentModel()
{
237
    m_inFileComponentModel.reset();
Marco Bubke's avatar
Marco Bubke committed
238
239
}

240
void DesignDocument::loadDocument(QPlainTextEdit *edit)
Marco Bubke's avatar
Marco Bubke committed
241
242
243
244
245
246
247
248
249
250
{
    Q_CHECK_PTR(edit);

    connect(edit, SIGNAL(undoAvailable(bool)),
            this, SIGNAL(undoAvailable(bool)));
    connect(edit, SIGNAL(redoAvailable(bool)),
            this, SIGNAL(redoAvailable(bool)));
    connect(edit, SIGNAL(modificationChanged(bool)),
            this, SIGNAL(dirtyStateChanged(bool)));

251
    m_documentTextModifier.reset(new BaseTextEditModifier(dynamic_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit())));
252
    m_documentModel->setTextModifier(m_documentTextModifier.data());
Marco Bubke's avatar
Marco Bubke committed
253

254
    m_inFileComponentTextModifier.reset();
Marco Bubke's avatar
Marco Bubke committed
255
256
257

    updateFileName(QString(), fileName());

258
    m_subComponentManager->update(QUrl::fromLocalFile(fileName()), currentModel()->imports());
Marco Bubke's avatar
Marco Bubke committed
259

260
    m_documentLoaded = true;
Marco Bubke's avatar
Marco Bubke committed
261
262
}

263
264
265
266
267
void DesignDocument::changeToDocumentModel()
{
    viewManager().detachRewriterView();
    viewManager().detachViewsExceptRewriterAndComponetView();

268

269
    m_inFileComponentModel.reset();
270

271
    viewManager().attachRewriterView();
272
273
274
    viewManager().attachViewsExceptRewriterAndComponetView();
}

275
void DesignDocument::changeToInFileComponentModel(ComponentTextModifier *textModifer)
276
{
277
    m_inFileComponentTextModifier.reset(textModifer);
278
279
280
    viewManager().detachRewriterView();
    viewManager().detachViewsExceptRewriterAndComponetView();

281
    m_inFileComponentModel.reset(createInFileComponentModel());
282
    m_inFileComponentModel->setTextModifier(m_inFileComponentTextModifier.data());
283

284
    viewManager().attachRewriterView();
285
286
287
    viewManager().attachViewsExceptRewriterAndComponetView();
}

288
void DesignDocument::changeToSubComponent(const ModelNode &componentNode)
Marco Bubke's avatar
Marco Bubke committed
289
290
291
292
{
    if (QmlDesignerPlugin::instance()->currentDesignDocument() != this)
        return;

293
    if (m_inFileComponentModel)
Marco Bubke's avatar
Marco Bubke committed
294
295
        changeToDocumentModel();

Marco Bubke's avatar
Marco Bubke committed
296
    bool subComponentLoaded = loadInFileComponent(componentNode);
Marco Bubke's avatar
Marco Bubke committed
297

298
    if (subComponentLoaded)
299
        attachRewriterToModel();
300
301

    QmlDesignerPlugin::instance()->viewManager().pushInFileComponentOnCrumbleBar(componentNode);
302
303
304
305
306
307
308
309
310
311
312
313
314
    QmlDesignerPlugin::instance()->viewManager().setComponentNode(componentNode);
}

void DesignDocument::changeToMaster()
{
    if (QmlDesignerPlugin::instance()->currentDesignDocument() != this)
        return;

    if (m_inFileComponentModel)
        changeToDocumentModel();

    QmlDesignerPlugin::instance()->viewManager().pushFileOnCrumbleBar(fileName());
    QmlDesignerPlugin::instance()->viewManager().setComponentNode(rootModelNode());
Marco Bubke's avatar
Marco Bubke committed
315
316
}

317
void DesignDocument::attachRewriterToModel()
Marco Bubke's avatar
Marco Bubke committed
318
319
320
321
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    Q_ASSERT(m_documentModel);

322
    viewManager().attachRewriterView();
Marco Bubke's avatar
Marco Bubke committed
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

    Q_ASSERT(m_documentModel);
    QApplication::restoreOverrideCursor();
}

bool DesignDocument::isUndoAvailable() const
{
    if (plainTextEdit())
        return plainTextEdit()->document()->isUndoAvailable();
    return false;
}

bool DesignDocument::isRedoAvailable() const
{
    if (plainTextEdit())
        return plainTextEdit()->document()->isRedoAvailable();
    return false;
}

void DesignDocument::close()
{
    m_documentLoaded = false;
    emit designDocumentClosed();
}

Thomas Hartmann's avatar
Thomas Hartmann committed
348
349
350
void DesignDocument::updateSubcomponentManager()
{
    Q_ASSERT(m_subComponentManager);
351
    m_subComponentManager->update(QUrl::fromLocalFile(fileName()), currentModel()->imports());
Thomas Hartmann's avatar
Thomas Hartmann committed
352
353
}

Marco Bubke's avatar
Marco Bubke committed
354
355
void DesignDocument::deleteSelected()
{
356
    if (!currentModel())
Marco Bubke's avatar
Marco Bubke committed
357
358
359
        return;

    try {
360
        RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::deleteSelected"));
Marco Bubke's avatar
Marco Bubke committed
361
        QList<ModelNode> toDelete = view()->selectedModelNodes();
Marco Bubke's avatar
Marco Bubke committed
362
        foreach (ModelNode node, toDelete) {
363
            if (node.isValid() && !node.isRootNode() && QmlObjectNode::isValidQmlObjectNode(node))
Marco Bubke's avatar
Marco Bubke committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
                QmlObjectNode(node).destroy();
        }

    } catch (RewritingException &e) {
        QMessageBox::warning(0, tr("Error"), e.description());
    }
}

void DesignDocument::copySelected()
{
    QScopedPointer<Model> copyModel(Model::create("QtQuick.Rectangle", 1, 0, currentModel()));
    copyModel->setFileUrl(currentModel()->fileUrl());
    copyModel->changeImports(currentModel()->imports(), QList<Import>());

    Q_ASSERT(copyModel);

    DesignDocumentView view;

382
    currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

    if (view.selectedModelNodes().isEmpty())
        return;

    QList<ModelNode> selectedNodes(view.selectedModelNodes());

    foreach (const ModelNode &node, selectedNodes) {
        foreach (const ModelNode &node2, selectedNodes) {
            if (node.isAncestorOf(node2))
                selectedNodes.removeAll(node2);
        }
    }

    if (selectedNodes.count() == 1) {
        ModelNode selectedNode(selectedNodes.first());

        if (!selectedNode.isValid())
            return;

402
        currentModel()->detachView(&view);
Marco Bubke's avatar
Marco Bubke committed
403
404
405
406
407
408
409
410
411

        copyModel->attachView(&view);
        view.replaceModel(selectedNode);

        Q_ASSERT(view.rootModelNode().isValid());
        Q_ASSERT(view.rootModelNode().type() != "empty");

        view.toClipboard();
    } else { //multi items selected
412
        currentModel()->detachView(&view);
Marco Bubke's avatar
Marco Bubke committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
        copyModel->attachView(&view);

        foreach (ModelNode node, view.rootModelNode().allDirectSubModelNodes()) {
            node.destroy();
        }
        view.changeRootNodeType("QtQuick.Rectangle", 1, 0);
        view.rootModelNode().setId("designer__Selection");

        foreach (const ModelNode &selectedNode, selectedNodes) {
            ModelNode newNode(view.insertModel(selectedNode));
            view.rootModelNode().nodeListProperty("data").reparentHere(newNode);
        }

        view.toClipboard();
    }
}

void DesignDocument::cutSelected()
{
    copySelected();
    deleteSelected();
}

static void scatterItem(ModelNode pastedNode, const ModelNode targetNode, int offset = -2000)
{

    bool scatter = false;
    foreach (const ModelNode &childNode, targetNode.allDirectSubModelNodes()) {
        if ((childNode.variantProperty("x").value() == pastedNode.variantProperty("x").value()) &&
            (childNode.variantProperty("y").value() == pastedNode.variantProperty("y").value()))
            scatter = true;
    }
    if (!scatter)
        return;

    if (offset == -2000) {
        double x = pastedNode.variantProperty("x").value().toDouble();
        double y = pastedNode.variantProperty("y").value().toDouble();
        double targetWidth = 20;
        double targetHeight = 20;
        x = x + double(qrand()) / RAND_MAX * targetWidth - targetWidth / 2;
        y = y + double(qrand()) / RAND_MAX * targetHeight - targetHeight / 2;
455
456
        pastedNode.variantProperty("x").setValue(int(x));
        pastedNode.variantProperty("y").setValue(int(y));
Marco Bubke's avatar
Marco Bubke committed
457
458
459
460
461
    } else {
        double x = pastedNode.variantProperty("x").value().toDouble();
        double y = pastedNode.variantProperty("y").value().toDouble();
        x = x + offset;
        y = y + offset;
462
463
        pastedNode.variantProperty("x").setValue(int(x));
        pastedNode.variantProperty("y").setValue(int(y));
Marco Bubke's avatar
Marco Bubke committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
    }
}

void DesignDocument::paste()
{
    QScopedPointer<Model> pasteModel(Model::create("empty", 1, 0, currentModel()));
    pasteModel->setFileUrl(currentModel()->fileUrl());
    pasteModel->changeImports(currentModel()->imports(), QList<Import>());

    Q_ASSERT(pasteModel);

    if (!pasteModel)
        return;

    DesignDocumentView view;
    pasteModel->attachView(&view);

    view.fromClipboard();

    ModelNode rootNode(view.rootModelNode());

    if (rootNode.type() == "empty")
        return;

    if (rootNode.id() == "designer__Selection") {
        QList<ModelNode> selectedNodes = rootNode.allDirectSubModelNodes();
        pasteModel->detachView(&view);
491
        currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
492
493
494
495
496
497
498

        ModelNode targetNode;

        if (!view.selectedModelNodes().isEmpty())
            targetNode = view.selectedModelNodes().first();

        //In case we copy and paste a selection we paste in the parent item
499
        if ((view.selectedModelNodes().count() == selectedNodes.count()) && targetNode.isValid() && targetNode.hasParentProperty())
Marco Bubke's avatar
Marco Bubke committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
            targetNode = targetNode.parentProperty().parentModelNode();

        if (!targetNode.isValid())
            targetNode = view.rootModelNode();

        foreach (const ModelNode &node, selectedNodes) {
            foreach (const ModelNode &node2, selectedNodes) {
                if (node.isAncestorOf(node2))
                    selectedNodes.removeAll(node2);
            }
        }

        QList<ModelNode> pastedNodeList;

        try {
515
            RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste1"));
Marco Bubke's avatar
Marco Bubke committed
516
517
518
519

            int offset = double(qrand()) / RAND_MAX * 20 - 10;

            foreach (const ModelNode &node, selectedNodes) {
520
                PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
Marco Bubke's avatar
Marco Bubke committed
521
522
523
524
525
526
527
528
529
530
531
532
                ModelNode pastedNode(view.insertModel(node));
                pastedNodeList.append(pastedNode);
                scatterItem(pastedNode, targetNode, offset);
                targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
            }

            view.setSelectedModelNodes(pastedNodeList);
        } catch (RewritingException &e) {
            qWarning() << e.description(); //silent error
        }
    } else {
        try {
533
            RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste2"));
Marco Bubke's avatar
Marco Bubke committed
534
535

            pasteModel->detachView(&view);
536
            currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
537
538
539
540
541
542
543
544
545
            ModelNode pastedNode(view.insertModel(rootNode));
            ModelNode targetNode;

            if (!view.selectedModelNodes().isEmpty())
                targetNode = view.selectedModelNodes().first();

            if (!targetNode.isValid())
                targetNode = view.rootModelNode();

546
            if (targetNode.hasParentProperty() &&
Marco Bubke's avatar
Marco Bubke committed
547
548
549
550
551
552
                (pastedNode.simplifiedTypeName() == targetNode.simplifiedTypeName()) &&
                (pastedNode.variantProperty("width").value() == targetNode.variantProperty("width").value()) &&
                (pastedNode.variantProperty("height").value() == targetNode.variantProperty("height").value()))

                targetNode = targetNode.parentProperty().parentModelNode();

553
            PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
Marco Bubke's avatar
Marco Bubke committed
554
555

            scatterItem(pastedNode, targetNode);
556
            if (targetNode.metaInfo().propertyIsListProperty(defaultProperty)) {
Marco Bubke's avatar
Marco Bubke committed
557
                targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
558
559
560
            } else {
                qWarning() << "Cannot reparent to" << targetNode;
            }
Marco Bubke's avatar
Marco Bubke committed
561
562
563
564
565
566
567
568
569
570
571
572
573

            transaction.commit();
            NodeMetaInfo::clearCache();

            view.setSelectedModelNodes(QList<ModelNode>() << pastedNode);
        } catch (RewritingException &e) {
            qWarning() << e.description(); //silent error
        }
    }
}

void DesignDocument::selectAll()
{
574
    if (!currentModel())
Marco Bubke's avatar
Marco Bubke committed
575
576
577
        return;

    DesignDocumentView view;
578
    currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594


    QList<ModelNode> allNodesExceptRootNode(view.allModelNodes());
    allNodesExceptRootNode.removeOne(view.rootModelNode());
    view.setSelectedModelNodes(allNodesExceptRootNode);
}

RewriterView *DesignDocument::rewriterView() const
{
    return m_rewriterView.data();
}

void DesignDocument::setEditor(Core::IEditor *editor)
{
    m_textEditor = editor;
    connect(editor->document(),
595
            SIGNAL(filePathChanged(QString,QString)),
Marco Bubke's avatar
Marco Bubke committed
596
            SLOT(updateFileName(QString,QString)));
597
598

    updateActiveQtVersion();
Marco Bubke's avatar
Marco Bubke committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
}

Core::IEditor *DesignDocument::editor() const
{
    return m_textEditor.data();
}

TextEditor::ITextEditor *DesignDocument::textEditor() const
{
    return qobject_cast<TextEditor::ITextEditor*>(editor());
}

QPlainTextEdit *DesignDocument::plainTextEdit() const
{
    if (editor())
        return qobject_cast<QPlainTextEdit*>(editor()->widget());

    return 0;
}

ModelNode DesignDocument::rootModelNode() const
{
    return rewriterView()->rootModelNode();
}

void DesignDocument::undo()
{
    if (rewriterView() && !rewriterView()->modificationGroupActive())
        plainTextEdit()->undo();

    viewManager().resetPropertyEditorView();
}

void DesignDocument::redo()
{
    if (rewriterView() && !rewriterView()->modificationGroupActive())
        plainTextEdit()->redo();

    viewManager().resetPropertyEditorView();
}

640
641
642
static bool isFileInProject(DesignDocument *designDocument, ProjectExplorer::Project *project)
{
    foreach (const QString &fileNameInProject, project->files(ProjectExplorer::Project::ExcludeGeneratedFiles)) {
Orgad Shaneh's avatar
Orgad Shaneh committed
643
        if (designDocument->fileName() == fileNameInProject)
644
645
646
647
648
649
650
            return true;
    }

    return false;
}

static inline QtSupport::BaseQtVersion *getActiveQtVersion(DesignDocument *designDocument)
Marco Bubke's avatar
Marco Bubke committed
651
652
653
654
{
    ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
    ProjectExplorer::Project *currentProject = projectExplorer->currentProject();

655
656
657
    if (!currentProject)
        currentProject = ProjectExplorer::SessionManager::projectForFile(designDocument->fileName());

Marco Bubke's avatar
Marco Bubke committed
658
659
660
    if (!currentProject)
        return 0;

661
662
663
664
665
    if (!isFileInProject(designDocument, currentProject))
        return 0;

    designDocument->disconnect(designDocument,  SLOT(updateActiveQtVersion()));
    designDocument->connect(projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project*)), designDocument, SLOT(updateActiveQtVersion()));
Marco Bubke's avatar
Marco Bubke committed
666

667
    designDocument->connect(currentProject, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)), designDocument, SLOT(updateActiveQtVersion()));
Marco Bubke's avatar
Marco Bubke committed
668
669
670
671
672
673
674


    ProjectExplorer::Target *target = currentProject->activeTarget();

    if (!target)
        return 0;

675
    designDocument->connect(target, SIGNAL(kitChanged()), designDocument, SLOT(updateActiveQtVersion()));
Marco Bubke's avatar
Marco Bubke committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    return QtSupport::QtKitInformation::qtVersion(target->kit());
}

void DesignDocument::updateActiveQtVersion()
{
    QtSupport::BaseQtVersion *newQtVersion = getActiveQtVersion(this);

    if (!newQtVersion ) {
        m_qtVersionId = -1;
        return;
    }

    if (m_qtVersionId == newQtVersion->uniqueId())
        return;

    m_qtVersionId = newQtVersion->uniqueId();

    viewManager().setNodeInstanceViewQtPath(pathToQt());
}

QString DesignDocument::contextHelpId() const
{
    DesignDocumentView view;
699
    currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
700
701
702
703
704
705
706
707
708
709
710
711

    QList<ModelNode> nodes = view.selectedModelNodes();
    QString helpId;
    if (!nodes.isEmpty()) {
        helpId = nodes.first().type();
        helpId.replace("QtQuick", "QML");
    }

    return helpId;
}

} // namespace QmlDesigner