designdocument.cpp 20.7 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Marco Bubke's avatar
Marco Bubke committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Marco Bubke's avatar
Marco Bubke committed
15
**
16 17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18 19 20 21 22
** 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.
Marco Bubke's avatar
Marco Bubke committed
23 24 25 26 27
**
****************************************************************************/

#include "designdocument.h"
#include "designdocumentview.h"
28
#include "documentmanager.h"
Marco Bubke's avatar
Marco Bubke committed
29 30 31 32 33 34 35

#include <metainfo.h>
#include <qmlobjectnode.h>
#include <rewritingexception.h>
#include <nodelistproperty.h>
#include <variantproperty.h>
#include <qmldesignerplugin.h>
36
#include <viewmanager.h>
37
#include <nodeinstanceview.h>
Marco Bubke's avatar
Marco Bubke committed
38

39
#include <projectexplorer/projecttree.h>
Marco Bubke's avatar
Marco Bubke committed
40 41
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
42
#include <projectexplorer/session.h>
43
#include <projectexplorer/kit.h>
Marco Bubke's avatar
Marco Bubke committed
44 45 46
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtsupportconstants.h>
#include <qtsupport/qtversionmanager.h>
47
#include <coreplugin/idocument.h>
Marco Bubke's avatar
Marco Bubke committed
48

49 50
#include <qmljs/qmljsmodelmanagerinterface.h>

Marco Bubke's avatar
Marco Bubke committed
51 52 53 54 55 56 57
#include <QFileInfo>
#include <QUrl>
#include <QDebug>

#include <QPlainTextEdit>
#include <QApplication>

58 59
using namespace ProjectExplorer;

Marco Bubke's avatar
Marco Bubke committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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),
79
        m_currentKit(0)
Marco Bubke's avatar
Marco Bubke committed
80 81 82 83 84 85 86 87 88
{
}

DesignDocument::~DesignDocument()
{
}

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

    return m_documentModel.data();
Marco Bubke's avatar
Marco Bubke committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

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

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

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)
{
120
    bool explicitComponent = componentText.contains(QLatin1String("Component"));
Marco Bubke's avatar
Marco Bubke committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    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
140
bool DesignDocument::loadInFileComponent(const ModelNode &componentNode)
Marco Bubke's avatar
Marco Bubke committed
141 142 143 144 145 146 147 148
{
    QString componentText = rewriterView()->extractText(QList<ModelNode>() << componentNode).value(componentNode);

    if (componentText.isEmpty())
        return false;

    if (!componentNode.isRootNode()) {
        //change to subcomponent model
149
        changeToInFileComponentModel(createComponentTextModifier(m_documentTextModifier.data(), rewriterView(), componentText, componentNode));
Marco Bubke's avatar
Marco Bubke committed
150 151 152 153 154
    }

    return true;
}

155
AbstractView *DesignDocument::view() const
156
{
Marco Bubke's avatar
Marco Bubke committed
157
    return viewManager().nodeInstanceView();
158 159
}

160 161 162 163 164 165 166 167
Model* DesignDocument::createInFileComponentModel()
{
    Model *model = Model::create("QtQuick.Item", 1, 0);
    model->setFileUrl(m_documentModel->fileUrl());

    return model;
}

168 169 170 171 172 173 174
QList<RewriterError> DesignDocument::qmlParseWarnings() const
{
    return m_rewriterView->warnings();
}

bool DesignDocument::hasQmlParseWarnings() const
{
175
    return !m_rewriterView->warnings().isEmpty();
176 177 178
}

QList<RewriterError> DesignDocument::qmlParseErrors() const
Marco Bubke's avatar
Marco Bubke committed
179 180 181 182
{
    return m_rewriterView->errors();
}

183
bool DesignDocument::hasQmlParseErrors() const
Marco Bubke's avatar
Marco Bubke committed
184
{
185
    return !m_rewriterView->errors().isEmpty();
Marco Bubke's avatar
Marco Bubke committed
186 187 188 189
}

QString DesignDocument::displayName() const
{
190
    return fileName().toString();
Marco Bubke's avatar
Marco Bubke committed
191 192 193 194
}

QString DesignDocument::simplfiedDisplayName() const
{
Orgad Shaneh's avatar
Orgad Shaneh committed
195
    if (rootModelNode().id().isEmpty())
Marco Bubke's avatar
Marco Bubke committed
196
        return rootModelNode().id();
Orgad Shaneh's avatar
Orgad Shaneh committed
197
    else
Marco Bubke's avatar
Marco Bubke committed
198 199
        return rootModelNode().simplifiedTypeName();

200
    return fileName().fileName();
Marco Bubke's avatar
Marco Bubke committed
201 202
}

203
void DesignDocument::updateFileName(const Utils::FileName & /*oldFileName*/, const Utils::FileName &newFileName)
Marco Bubke's avatar
Marco Bubke committed
204 205
{
    if (m_documentModel)
206
        m_documentModel->setFileUrl(QUrl::fromLocalFile(newFileName.toString()));
Marco Bubke's avatar
Marco Bubke committed
207 208

    if (m_inFileComponentModel)
209
        m_inFileComponentModel->setFileUrl(QUrl::fromLocalFile(newFileName.toString()));
Marco Bubke's avatar
Marco Bubke committed
210

211
    viewManager().setItemLibraryViewResourcePath(newFileName.toFileInfo().absolutePath());
Marco Bubke's avatar
Marco Bubke committed
212 213 214 215

    emit displayNameChanged(displayName());
}

216
Utils::FileName DesignDocument::fileName() const
Marco Bubke's avatar
Marco Bubke committed
217
{
218
    return editor()->document()->filePath();
Marco Bubke's avatar
Marco Bubke committed
219 220
}

221
Kit *DesignDocument::currentKit() const
Marco Bubke's avatar
Marco Bubke committed
222
{
223
    return m_currentKit;
Marco Bubke's avatar
Marco Bubke committed
224 225 226 227 228 229 230 231 232
}

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

void DesignDocument::resetToDocumentModel()
{
233
    m_inFileComponentModel.reset();
Marco Bubke's avatar
Marco Bubke committed
234 235
}

236
void DesignDocument::loadDocument(QPlainTextEdit *edit)
Marco Bubke's avatar
Marco Bubke committed
237 238 239 240 241 242 243 244 245 246
{
    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)));

247
    m_documentTextModifier.reset(new BaseTextEditModifier(dynamic_cast<TextEditor::TextEditorWidget*>(plainTextEdit())));
248 249 250

    connect(m_documentTextModifier.data(), &TextModifier::textChanged, this, &DesignDocument::updateQrcFiles);

251
    m_documentModel->setTextModifier(m_documentTextModifier.data());
Marco Bubke's avatar
Marco Bubke committed
252

253
    m_inFileComponentTextModifier.reset();
Marco Bubke's avatar
Marco Bubke committed
254

255
    updateFileName(Utils::FileName(), fileName());
Marco Bubke's avatar
Marco Bubke committed
256

257 258
    updateQrcFiles();

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

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

267

268
    m_inFileComponentModel.reset();
269

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

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

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

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

287 288 289 290 291 292 293 294 295 296 297 298
void DesignDocument::updateQrcFiles()
{
    ProjectExplorer::Project *currentProject = ProjectExplorer::SessionManager::projectForFile(fileName());

    if (currentProject) {
        foreach (const QString &fileName, currentProject->files(ProjectExplorer::Project::SourceFiles)) {
            if (fileName.endsWith(".qrc"))
                QmlJS::ModelManagerInterface::instance()->updateQrcFile(fileName);
        }
    }
}

299
void DesignDocument::changeToSubComponent(const ModelNode &componentNode)
Marco Bubke's avatar
Marco Bubke committed
300 301 302 303
{
    if (QmlDesignerPlugin::instance()->currentDesignDocument() != this)
        return;

304
    if (m_inFileComponentModel)
Marco Bubke's avatar
Marco Bubke committed
305 306
        changeToDocumentModel();

Marco Bubke's avatar
Marco Bubke committed
307
    bool subComponentLoaded = loadInFileComponent(componentNode);
Marco Bubke's avatar
Marco Bubke committed
308

309
    if (subComponentLoaded)
310
        attachRewriterToModel();
311 312

    QmlDesignerPlugin::instance()->viewManager().pushInFileComponentOnCrumbleBar(componentNode);
313 314 315 316 317 318 319 320 321 322 323
    QmlDesignerPlugin::instance()->viewManager().setComponentNode(componentNode);
}

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

    if (m_inFileComponentModel)
        changeToDocumentModel();

324
    QmlDesignerPlugin::instance()->viewManager().pushFileOnCrumbleBar(fileName().toString());
325
    QmlDesignerPlugin::instance()->viewManager().setComponentNode(rootModelNode());
Marco Bubke's avatar
Marco Bubke committed
326 327
}

328
void DesignDocument::attachRewriterToModel()
Marco Bubke's avatar
Marco Bubke committed
329 330 331 332
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    Q_ASSERT(m_documentModel);

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

    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
359 360 361
void DesignDocument::updateSubcomponentManager()
{
    Q_ASSERT(m_subComponentManager);
362
    m_subComponentManager->update(QUrl::fromLocalFile(fileName().toString()), currentModel()->imports());
Thomas Hartmann's avatar
Thomas Hartmann committed
363 364
}

Marco Bubke's avatar
Marco Bubke committed
365 366
void DesignDocument::deleteSelected()
{
367
    if (!currentModel())
Marco Bubke's avatar
Marco Bubke committed
368 369 370
        return;

    try {
371
        RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::deleteSelected"));
Marco Bubke's avatar
Marco Bubke committed
372
        QList<ModelNode> toDelete = view()->selectedModelNodes();
Marco Bubke's avatar
Marco Bubke committed
373
        foreach (ModelNode node, toDelete) {
374
            if (node.isValid() && !node.isRootNode() && QmlObjectNode::isValidQmlObjectNode(node))
Marco Bubke's avatar
Marco Bubke committed
375 376 377
                QmlObjectNode(node).destroy();
        }

378
    } catch (const RewritingException &e) {
379
        e.showException();
Marco Bubke's avatar
Marco Bubke committed
380 381 382 383 384 385 386 387 388 389 390 391 392
    }
}

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;

393
    currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

    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;

413
        currentModel()->detachView(&view);
Marco Bubke's avatar
Marco Bubke committed
414 415 416 417 418 419 420 421 422

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

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

        view.toClipboard();
    } else { //multi items selected
423
        currentModel()->detachView(&view);
Marco Bubke's avatar
Marco Bubke committed
424 425
        copyModel->attachView(&view);

426
        foreach (ModelNode node, view.rootModelNode().directSubModelNodes()) {
Marco Bubke's avatar
Marco Bubke committed
427 428 429
            node.destroy();
        }
        view.changeRootNodeType("QtQuick.Rectangle", 1, 0);
430
        view.rootModelNode().setIdWithRefactoring(QLatin1String("designer__Selection"));
Marco Bubke's avatar
Marco Bubke committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

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

        view.toClipboard();
    }
}

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

447
static void scatterItem(const ModelNode &pastedNode, const ModelNode &targetNode, int offset = -2000)
Marco Bubke's avatar
Marco Bubke committed
448
{
449 450
    if (targetNode.metaInfo().isValid() && targetNode.metaInfo().isLayoutable())
        return;
Marco Bubke's avatar
Marco Bubke committed
451 452

    bool scatter = false;
453
    foreach (const ModelNode &childNode, targetNode.directSubModelNodes()) {
Marco Bubke's avatar
Marco Bubke committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467
        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;
468 469
        pastedNode.variantProperty("x").setValue(int(x));
        pastedNode.variantProperty("y").setValue(int(y));
Marco Bubke's avatar
Marco Bubke committed
470 471 472 473 474
    } else {
        double x = pastedNode.variantProperty("x").value().toDouble();
        double y = pastedNode.variantProperty("y").value().toDouble();
        x = x + offset;
        y = y + offset;
475 476
        pastedNode.variantProperty("x").setValue(int(x));
        pastedNode.variantProperty("y").setValue(int(y));
Marco Bubke's avatar
Marco Bubke committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    }
}

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;

501
    if (rootNode.id() == QLatin1String("designer__Selection")) {
502
        QList<ModelNode> selectedNodes = rootNode.directSubModelNodes();
Marco Bubke's avatar
Marco Bubke committed
503
        pasteModel->detachView(&view);
504
        currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
505 506 507 508 509 510 511

        ModelNode targetNode;

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

        //In case we copy and paste a selection we paste in the parent item
512
        if ((view.selectedModelNodes().count() == selectedNodes.count()) && targetNode.isValid() && targetNode.hasParentProperty())
Marco Bubke's avatar
Marco Bubke committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
            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 {
528
            RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste1"));
Marco Bubke's avatar
Marco Bubke committed
529 530 531 532

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

            foreach (const ModelNode &node, selectedNodes) {
533
                PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
Marco Bubke's avatar
Marco Bubke committed
534 535 536 537 538 539 540
                ModelNode pastedNode(view.insertModel(node));
                pastedNodeList.append(pastedNode);
                scatterItem(pastedNode, targetNode, offset);
                targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
            }

            view.setSelectedModelNodes(pastedNodeList);
541
        } catch (const RewritingException &e) {
Marco Bubke's avatar
Marco Bubke committed
542 543 544 545
            qWarning() << e.description(); //silent error
        }
    } else {
        try {
546
            RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste2"));
Marco Bubke's avatar
Marco Bubke committed
547 548

            pasteModel->detachView(&view);
549
            currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
550 551 552 553 554 555 556 557 558
            ModelNode pastedNode(view.insertModel(rootNode));
            ModelNode targetNode;

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

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

559
            if (targetNode.hasParentProperty() &&
Marco Bubke's avatar
Marco Bubke committed
560 561 562 563 564 565
                (pastedNode.simplifiedTypeName() == targetNode.simplifiedTypeName()) &&
                (pastedNode.variantProperty("width").value() == targetNode.variantProperty("width").value()) &&
                (pastedNode.variantProperty("height").value() == targetNode.variantProperty("height").value()))

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

566
            PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
Marco Bubke's avatar
Marco Bubke committed
567 568

            scatterItem(pastedNode, targetNode);
569
            if (targetNode.metaInfo().propertyIsListProperty(defaultProperty)) {
Marco Bubke's avatar
Marco Bubke committed
570
                targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
571 572 573
            } else {
                qWarning() << "Cannot reparent to" << targetNode;
            }
Marco Bubke's avatar
Marco Bubke committed
574 575 576 577 578

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

            view.setSelectedModelNodes(QList<ModelNode>() << pastedNode);
579
        } catch (const RewritingException &e) {
Marco Bubke's avatar
Marco Bubke committed
580 581 582 583 584 585 586
            qWarning() << e.description(); //silent error
        }
    }
}

void DesignDocument::selectAll()
{
587
    if (!currentModel())
Marco Bubke's avatar
Marco Bubke committed
588 589 590
        return;

    DesignDocumentView view;
591
    currentModel()->attachView(&view);
Marco Bubke's avatar
Marco Bubke committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606


    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;
607 608
    connect(editor->document(), &Core::IDocument::filePathChanged,
            this, &DesignDocument::updateFileName);
609 610

    updateActiveQtVersion();
Marco Bubke's avatar
Marco Bubke committed
611 612 613 614 615 616 617
}

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

618
TextEditor::BaseTextEditor *DesignDocument::textEditor() const
Marco Bubke's avatar
Marco Bubke committed
619
{
620
    return qobject_cast<TextEditor::BaseTextEditor*>(editor());
Marco Bubke's avatar
Marco Bubke committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
}

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();
}

652
static inline Kit *getActiveKit(DesignDocument *designDocument)
Marco Bubke's avatar
Marco Bubke committed
653
{
654
    ProjectExplorer::Project *currentProject = ProjectExplorer::SessionManager::projectForFile(designDocument->fileName());
Marco Bubke's avatar
Marco Bubke committed
655

656
    if (!currentProject)
657
        currentProject = ProjectExplorer::ProjectTree::currentProject();
658

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

662

663
    QObject::connect(ProjectTree::instance(), &ProjectTree::currentProjectChanged,
664
                     designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
Marco Bubke's avatar
Marco Bubke committed
665

666
    QObject::connect(currentProject, &Project::activeTargetChanged,
667
                     designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
Marco Bubke's avatar
Marco Bubke committed
668 669


670
    Target *target = currentProject->activeTarget();
Marco Bubke's avatar
Marco Bubke committed
671 672 673 674

    if (!target)
        return 0;

675 676
    if (!target->kit()->isValid())
        return 0;
677 678
    QObject::connect(target, &Target::kitChanged,
                     designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
679 680

    return target->kit();
Marco Bubke's avatar
Marco Bubke committed
681 682 683 684
}

void DesignDocument::updateActiveQtVersion()
{
685
    m_currentKit = getActiveKit(this);
686
    viewManager().setNodeInstanceViewKit(m_currentKit);
Marco Bubke's avatar
Marco Bubke committed
687 688 689 690
}

QString DesignDocument::contextHelpId() const
{
Marco Bubke's avatar
Marco Bubke committed
691
    if (view())
692
        return view()->contextHelpId();
Marco Bubke's avatar
Marco Bubke committed
693

Marco Bubke's avatar
Marco Bubke committed
694
    return QString();
Marco Bubke's avatar
Marco Bubke committed
695 696 697
}

} // namespace QmlDesigner