2013-01-23 12:31:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-01-23 12:31:22 +01:00
|
|
|
** 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"
|
2013-08-02 13:25:45 +02:00
|
|
|
#include "documentmanager.h"
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
#include <metainfo.h>
|
|
|
|
|
#include <qmlobjectnode.h>
|
|
|
|
|
#include <rewritingexception.h>
|
|
|
|
|
#include <nodelistproperty.h>
|
|
|
|
|
#include <variantproperty.h>
|
|
|
|
|
#include <qmldesignerplugin.h>
|
2013-02-05 14:52:25 +01:00
|
|
|
#include <viewmanager.h>
|
2013-11-06 15:47:59 +01:00
|
|
|
#include <nodeinstanceview.h>
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
2014-03-05 15:17:43 +01:00
|
|
|
#include <projectexplorer/session.h>
|
2014-03-26 19:41:51 +01:00
|
|
|
#include <projectexplorer/kit.h>
|
2013-01-23 12:31:22 +01:00
|
|
|
#include <qtsupport/qtkitinformation.h>
|
|
|
|
|
#include <qtsupport/qtsupportconstants.h>
|
|
|
|
|
#include <qtsupport/qtversionmanager.h>
|
|
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
#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),
|
2014-03-26 19:41:51 +01:00
|
|
|
m_currentKit(0)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DesignDocument::~DesignDocument()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Model *DesignDocument::currentModel() const
|
|
|
|
|
{
|
2013-07-10 18:12:07 +02:00
|
|
|
if (m_inFileComponentModel)
|
|
|
|
|
return m_inFileComponentModel.data();
|
|
|
|
|
|
|
|
|
|
return m_documentModel.data();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-04 11:16:46 +01:00
|
|
|
bool DesignDocument::loadInFileComponent(const ModelNode &componentNode)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
QString componentText = rewriterView()->extractText(QList<ModelNode>() << componentNode).value(componentNode);
|
|
|
|
|
|
|
|
|
|
if (componentText.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!componentNode.isRootNode()) {
|
|
|
|
|
//change to subcomponent model
|
2013-08-01 11:48:36 +02:00
|
|
|
changeToInFileComponentModel(createComponentTextModifier(m_documentTextModifier.data(), rewriterView(), componentText, componentNode));
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 15:45:09 +02:00
|
|
|
AbstractView *DesignDocument::view()
|
2013-02-05 14:52:25 +01:00
|
|
|
{
|
2013-07-31 15:45:09 +02:00
|
|
|
return viewManager().nodeInstanceView();
|
2013-02-05 14:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
Model* DesignDocument::createInFileComponentModel()
|
|
|
|
|
{
|
|
|
|
|
Model *model = Model::create("QtQuick.Item", 1, 0);
|
|
|
|
|
model->setFileUrl(m_documentModel->fileUrl());
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 12:31:22 +01:00
|
|
|
/*!
|
|
|
|
|
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
|
|
|
|
|
{
|
2013-07-10 18:12:07 +02:00
|
|
|
return currentModel()->rewriterView() && !currentModel()->rewriterView()->errors().isEmpty();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignDocument::displayName() const
|
|
|
|
|
{
|
|
|
|
|
return fileName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignDocument::simplfiedDisplayName() const
|
|
|
|
|
{
|
2013-11-11 22:20:47 +02:00
|
|
|
if (rootModelNode().id().isEmpty())
|
2013-01-23 12:31:22 +01:00
|
|
|
return rootModelNode().id();
|
2013-11-11 22:20:47 +02:00
|
|
|
else
|
2013-01-23 12:31:22 +01:00
|
|
|
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
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
return editor()->document()->filePath();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-26 19:41:51 +01:00
|
|
|
ProjectExplorer::Kit *DesignDocument::currentKit() const
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
2014-03-26 19:41:51 +01:00
|
|
|
return m_currentKit;
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignDocument::isDocumentLoaded() const
|
|
|
|
|
{
|
|
|
|
|
return m_documentLoaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignDocument::resetToDocumentModel()
|
|
|
|
|
{
|
2013-07-10 18:12:07 +02:00
|
|
|
m_inFileComponentModel.reset();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-04 15:03:46 +01:00
|
|
|
void DesignDocument::loadDocument(QPlainTextEdit *edit)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
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)));
|
|
|
|
|
|
2013-02-06 17:12:04 +01:00
|
|
|
m_documentTextModifier.reset(new BaseTextEditModifier(dynamic_cast<TextEditor::BaseTextEditorWidget*>(plainTextEdit())));
|
2013-08-01 11:48:36 +02:00
|
|
|
m_documentModel->setTextModifier(m_documentTextModifier.data());
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2013-02-06 17:12:04 +01:00
|
|
|
m_inFileComponentTextModifier.reset();
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
updateFileName(QString(), fileName());
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
m_subComponentManager->update(QUrl::fromLocalFile(fileName()), currentModel()->imports());
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2013-02-04 15:03:46 +01:00
|
|
|
m_documentLoaded = true;
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 13:25:58 +01:00
|
|
|
void DesignDocument::changeToDocumentModel()
|
|
|
|
|
{
|
|
|
|
|
viewManager().detachRewriterView();
|
|
|
|
|
viewManager().detachViewsExceptRewriterAndComponetView();
|
|
|
|
|
|
2013-09-26 18:46:39 +02:00
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
m_inFileComponentModel.reset();
|
2013-02-07 13:25:58 +01:00
|
|
|
|
2013-08-01 11:48:36 +02:00
|
|
|
viewManager().attachRewriterView();
|
2013-02-07 13:25:58 +01:00
|
|
|
viewManager().attachViewsExceptRewriterAndComponetView();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-01 11:48:36 +02:00
|
|
|
void DesignDocument::changeToInFileComponentModel(ComponentTextModifier *textModifer)
|
2013-02-07 13:25:58 +01:00
|
|
|
{
|
2013-08-01 11:48:36 +02:00
|
|
|
m_inFileComponentTextModifier.reset(textModifer);
|
2013-02-07 13:25:58 +01:00
|
|
|
viewManager().detachRewriterView();
|
|
|
|
|
viewManager().detachViewsExceptRewriterAndComponetView();
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
m_inFileComponentModel.reset(createInFileComponentModel());
|
2013-08-01 11:48:36 +02:00
|
|
|
m_inFileComponentModel->setTextModifier(m_inFileComponentTextModifier.data());
|
2013-02-07 13:25:58 +01:00
|
|
|
|
2013-08-01 11:48:36 +02:00
|
|
|
viewManager().attachRewriterView();
|
2013-02-07 13:25:58 +01:00
|
|
|
viewManager().attachViewsExceptRewriterAndComponetView();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 17:30:50 +02:00
|
|
|
void DesignDocument::changeToSubComponent(const ModelNode &componentNode)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
if (QmlDesignerPlugin::instance()->currentDesignDocument() != this)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
if (m_inFileComponentModel)
|
2013-01-23 12:31:22 +01:00
|
|
|
changeToDocumentModel();
|
|
|
|
|
|
2013-02-04 11:16:46 +01:00
|
|
|
bool subComponentLoaded = loadInFileComponent(componentNode);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2013-02-06 17:12:04 +01:00
|
|
|
if (subComponentLoaded)
|
2013-08-01 11:48:36 +02:00
|
|
|
attachRewriterToModel();
|
2013-08-12 17:30:50 +02:00
|
|
|
|
|
|
|
|
QmlDesignerPlugin::instance()->viewManager().pushInFileComponentOnCrumbleBar(componentNode);
|
2013-09-26 18:46:39 +02:00
|
|
|
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());
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-01 11:48:36 +02:00
|
|
|
void DesignDocument::attachRewriterToModel()
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
|
|
|
Q_ASSERT(m_documentModel);
|
|
|
|
|
|
2013-08-01 11:48:36 +02:00
|
|
|
viewManager().attachRewriterView();
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-29 09:50:31 +01:00
|
|
|
void DesignDocument::updateSubcomponentManager()
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_subComponentManager);
|
2013-07-10 18:12:07 +02:00
|
|
|
m_subComponentManager->update(QUrl::fromLocalFile(fileName()), currentModel()->imports());
|
2013-01-29 09:50:31 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-23 12:31:22 +01:00
|
|
|
void DesignDocument::deleteSelected()
|
|
|
|
|
{
|
2013-07-10 18:12:07 +02:00
|
|
|
if (!currentModel())
|
2013-01-23 12:31:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try {
|
2014-01-06 12:51:56 +01:00
|
|
|
RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::deleteSelected"));
|
2013-07-31 15:45:09 +02:00
|
|
|
QList<ModelNode> toDelete = view()->selectedModelNodes();
|
2013-01-23 12:31:22 +01:00
|
|
|
foreach (ModelNode node, toDelete) {
|
2013-07-24 15:58:51 +02:00
|
|
|
if (node.isValid() && !node.isRootNode() && QmlObjectNode::isValidQmlObjectNode(node))
|
2013-01-23 12:31:22 +01:00
|
|
|
QmlObjectNode(node).destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (RewritingException &e) {
|
2014-03-12 12:06:58 +01:00
|
|
|
e.showException();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->attachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->detachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
copyModel->attachView(&view);
|
|
|
|
|
view.replaceModel(selectedNode);
|
|
|
|
|
|
|
|
|
|
Q_ASSERT(view.rootModelNode().isValid());
|
|
|
|
|
Q_ASSERT(view.rootModelNode().type() != "empty");
|
|
|
|
|
|
|
|
|
|
view.toClipboard();
|
|
|
|
|
} else { //multi items selected
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->detachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
copyModel->attachView(&view);
|
|
|
|
|
|
2014-04-24 15:21:00 +02:00
|
|
|
foreach (ModelNode node, view.rootModelNode().directSubModelNodes()) {
|
2013-01-23 12:31:22 +01:00
|
|
|
node.destroy();
|
|
|
|
|
}
|
|
|
|
|
view.changeRootNodeType("QtQuick.Rectangle", 1, 0);
|
2014-04-29 12:30:48 +02:00
|
|
|
view.rootModelNode().setIdWithRefactoring("designer__Selection");
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
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;
|
2014-04-24 15:21:00 +02:00
|
|
|
foreach (const ModelNode &childNode, targetNode.directSubModelNodes()) {
|
2013-01-23 12:31:22 +01:00
|
|
|
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;
|
2013-07-25 12:34:32 +02:00
|
|
|
pastedNode.variantProperty("x").setValue(int(x));
|
|
|
|
|
pastedNode.variantProperty("y").setValue(int(y));
|
2013-01-23 12:31:22 +01:00
|
|
|
} else {
|
|
|
|
|
double x = pastedNode.variantProperty("x").value().toDouble();
|
|
|
|
|
double y = pastedNode.variantProperty("y").value().toDouble();
|
|
|
|
|
x = x + offset;
|
|
|
|
|
y = y + offset;
|
2013-07-25 12:34:32 +02:00
|
|
|
pastedNode.variantProperty("x").setValue(int(x));
|
|
|
|
|
pastedNode.variantProperty("y").setValue(int(y));
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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") {
|
2014-04-24 15:21:00 +02:00
|
|
|
QList<ModelNode> selectedNodes = rootNode.directSubModelNodes();
|
2013-01-23 12:31:22 +01:00
|
|
|
pasteModel->detachView(&view);
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->attachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
ModelNode targetNode;
|
|
|
|
|
|
|
|
|
|
if (!view.selectedModelNodes().isEmpty())
|
|
|
|
|
targetNode = view.selectedModelNodes().first();
|
|
|
|
|
|
|
|
|
|
//In case we copy and paste a selection we paste in the parent item
|
2013-08-06 16:11:11 +02:00
|
|
|
if ((view.selectedModelNodes().count() == selectedNodes.count()) && targetNode.isValid() && targetNode.hasParentProperty())
|
2013-01-23 12:31:22 +01:00
|
|
|
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 {
|
2014-01-06 12:51:56 +01:00
|
|
|
RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste1"));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
int offset = double(qrand()) / RAND_MAX * 20 - 10;
|
|
|
|
|
|
|
|
|
|
foreach (const ModelNode &node, selectedNodes) {
|
2013-03-05 12:19:19 +01:00
|
|
|
PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
|
2013-01-23 12:31:22 +01:00
|
|
|
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 {
|
2014-01-06 12:51:56 +01:00
|
|
|
RewriterTransaction transaction(rewriterView(), QByteArrayLiteral("DesignDocument::paste2"));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
pasteModel->detachView(&view);
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->attachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
ModelNode pastedNode(view.insertModel(rootNode));
|
|
|
|
|
ModelNode targetNode;
|
|
|
|
|
|
|
|
|
|
if (!view.selectedModelNodes().isEmpty())
|
|
|
|
|
targetNode = view.selectedModelNodes().first();
|
|
|
|
|
|
|
|
|
|
if (!targetNode.isValid())
|
|
|
|
|
targetNode = view.rootModelNode();
|
|
|
|
|
|
2013-07-24 15:58:51 +02:00
|
|
|
if (targetNode.hasParentProperty() &&
|
2013-01-23 12:31:22 +01:00
|
|
|
(pastedNode.simplifiedTypeName() == targetNode.simplifiedTypeName()) &&
|
|
|
|
|
(pastedNode.variantProperty("width").value() == targetNode.variantProperty("width").value()) &&
|
|
|
|
|
(pastedNode.variantProperty("height").value() == targetNode.variantProperty("height").value()))
|
|
|
|
|
|
|
|
|
|
targetNode = targetNode.parentProperty().parentModelNode();
|
|
|
|
|
|
2013-03-05 12:19:19 +01:00
|
|
|
PropertyName defaultProperty(targetNode.metaInfo().defaultPropertyName());
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
scatterItem(pastedNode, targetNode);
|
2014-03-05 15:57:40 +01:00
|
|
|
if (targetNode.metaInfo().propertyIsListProperty(defaultProperty)) {
|
2013-01-23 12:31:22 +01:00
|
|
|
targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
|
2014-03-05 15:57:40 +01:00
|
|
|
} else {
|
|
|
|
|
qWarning() << "Cannot reparent to" << targetNode;
|
|
|
|
|
}
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
NodeMetaInfo::clearCache();
|
|
|
|
|
|
|
|
|
|
view.setSelectedModelNodes(QList<ModelNode>() << pastedNode);
|
|
|
|
|
} catch (RewritingException &e) {
|
|
|
|
|
qWarning() << e.description(); //silent error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignDocument::selectAll()
|
|
|
|
|
{
|
2013-07-10 18:12:07 +02:00
|
|
|
if (!currentModel())
|
2013-01-23 12:31:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DesignDocumentView view;
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->attachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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(),
|
2013-08-07 17:36:35 +02:00
|
|
|
SIGNAL(filePathChanged(QString,QString)),
|
2013-01-23 12:31:22 +01:00
|
|
|
SLOT(updateFileName(QString,QString)));
|
2013-02-20 13:59:31 +01:00
|
|
|
|
|
|
|
|
updateActiveQtVersion();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 13:59:31 +01:00
|
|
|
static bool isFileInProject(DesignDocument *designDocument, ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
foreach (const QString &fileNameInProject, project->files(ProjectExplorer::Project::ExcludeGeneratedFiles)) {
|
2013-11-11 22:20:47 +02:00
|
|
|
if (designDocument->fileName() == fileNameInProject)
|
2013-02-20 13:59:31 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 19:41:51 +01:00
|
|
|
static inline ProjectExplorer::Kit *getActiveKit(DesignDocument *designDocument)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
|
|
|
|
|
ProjectExplorer::Project *currentProject = projectExplorer->currentProject();
|
|
|
|
|
|
2014-03-05 15:17:43 +01:00
|
|
|
if (!currentProject)
|
|
|
|
|
currentProject = ProjectExplorer::SessionManager::projectForFile(designDocument->fileName());
|
|
|
|
|
|
2013-01-23 12:31:22 +01:00
|
|
|
if (!currentProject)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-02-20 13:59:31 +01:00
|
|
|
if (!isFileInProject(designDocument, currentProject))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
designDocument->disconnect(designDocument, SLOT(updateActiveQtVersion()));
|
|
|
|
|
designDocument->connect(projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project*)), designDocument, SLOT(updateActiveQtVersion()));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2013-02-20 13:59:31 +01:00
|
|
|
designDocument->connect(currentProject, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)), designDocument, SLOT(updateActiveQtVersion()));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ProjectExplorer::Target *target = currentProject->activeTarget();
|
|
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-02-20 13:59:31 +01:00
|
|
|
designDocument->connect(target, SIGNAL(kitChanged()), designDocument, SLOT(updateActiveQtVersion()));
|
2014-03-26 19:41:51 +01:00
|
|
|
|
|
|
|
|
return target->kit();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignDocument::updateActiveQtVersion()
|
|
|
|
|
{
|
2014-03-26 19:41:51 +01:00
|
|
|
m_currentKit = getActiveKit(this);
|
|
|
|
|
viewManager().setNodeInstanceViewKit(m_currentKit);
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignDocument::contextHelpId() const
|
|
|
|
|
{
|
|
|
|
|
DesignDocumentView view;
|
2013-07-10 18:12:07 +02:00
|
|
|
currentModel()->attachView(&view);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
QList<ModelNode> nodes = view.selectedModelNodes();
|
|
|
|
|
QString helpId;
|
|
|
|
|
if (!nodes.isEmpty()) {
|
|
|
|
|
helpId = nodes.first().type();
|
|
|
|
|
helpId.replace("QtQuick", "QML");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return helpId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|