2013-01-23 12:31:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-01-23 12:31:22 +01:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-01-23 12:31:22 +01:00
|
|
|
**
|
2015-09-18 11:34:48 +02:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-01-23 12:31:22 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
2014-11-19 17:58:33 +01:00
|
|
|
#include <projectexplorer/projecttree.h>
|
2013-01-23 12:31:22 +01:00
|
|
|
#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>
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
2013-01-23 12:31:22 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2015-07-16 17:06:52 +02:00
|
|
|
bool explicitComponent = componentText.contains(QLatin1String("Component"));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 17:52:04 +02:00
|
|
|
AbstractView *DesignDocument::view() const
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 15:50:34 +02:00
|
|
|
QList<RewriterError> DesignDocument::qmlParseWarnings() const
|
|
|
|
|
{
|
|
|
|
|
return m_rewriterView->warnings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignDocument::hasQmlParseWarnings() const
|
|
|
|
|
{
|
|
|
|
|
return currentModel()->rewriterView() && !currentModel()->rewriterView()->warnings().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<RewriterError> DesignDocument::qmlParseErrors() const
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
return m_rewriterView->errors();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 15:50:34 +02:00
|
|
|
bool DesignDocument::hasQmlParseErrors() const
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
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
|
|
|
|
|
{
|
2015-07-09 14:02:36 +02:00
|
|
|
return fileName().toString();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2015-07-09 14:02:36 +02:00
|
|
|
return fileName().fileName();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-21 21:54:30 +02:00
|
|
|
void DesignDocument::updateFileName(const Utils::FileName & /*oldFileName*/, const Utils::FileName &newFileName)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
|
|
|
|
if (m_documentModel)
|
2014-12-21 21:54:30 +02:00
|
|
|
m_documentModel->setFileUrl(QUrl::fromLocalFile(newFileName.toString()));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
if (m_inFileComponentModel)
|
2014-12-21 21:54:30 +02:00
|
|
|
m_inFileComponentModel->setFileUrl(QUrl::fromLocalFile(newFileName.toString()));
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2014-12-21 21:54:30 +02:00
|
|
|
viewManager().setItemLibraryViewResourcePath(newFileName.toFileInfo().absolutePath());
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
emit displayNameChanged(displayName());
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-09 14:02:36 +02:00
|
|
|
Utils::FileName DesignDocument::fileName() const
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
2015-07-09 14:02:36 +02:00
|
|
|
return editor()->document()->filePath();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
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)));
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
m_documentTextModifier.reset(new BaseTextEditModifier(dynamic_cast<TextEditor::TextEditorWidget*>(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
|
|
|
|
2015-07-09 14:02:36 +02:00
|
|
|
updateFileName(Utils::FileName(), fileName());
|
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();
|
|
|
|
|
|
2015-07-09 14:02:36 +02:00
|
|
|
QmlDesignerPlugin::instance()->viewManager().pushFileOnCrumbleBar(fileName().toString());
|
2013-09-26 18:46:39 +02:00
|
|
|
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);
|
2015-07-09 14:02:36 +02:00
|
|
|
m_subComponentManager->update(QUrl::fromLocalFile(fileName().toString()), 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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-28 11:15:37 +01:00
|
|
|
} catch (const 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);
|
2015-07-16 17:06:52 +02:00
|
|
|
view.rootModelNode().setIdWithRefactoring(QLatin1String("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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 20:21:14 +03:00
|
|
|
static void scatterItem(const ModelNode &pastedNode, const ModelNode &targetNode, int offset = -2000)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
2014-08-07 17:01:31 +02:00
|
|
|
if (targetNode.metaInfo().isValid() && targetNode.metaInfo().isLayoutable())
|
|
|
|
|
return;
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2015-07-16 17:06:52 +02:00
|
|
|
if (rootNode.id() == QLatin1String("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);
|
2014-11-28 11:15:37 +01:00
|
|
|
} catch (const RewritingException &e) {
|
2013-01-23 12:31:22 +01:00
|
|
|
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);
|
2014-11-28 11:15:37 +01:00
|
|
|
} catch (const RewritingException &e) {
|
2013-01-23 12:31:22 +01:00
|
|
|
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;
|
2014-12-21 21:54:30 +02:00
|
|
|
connect(editor->document(), &Core::IDocument::filePathChanged,
|
|
|
|
|
this, &DesignDocument::updateFileName);
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
TextEditor::BaseTextEditor *DesignDocument::textEditor() const
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
2014-07-23 19:10:38 +02:00
|
|
|
return qobject_cast<TextEditor::BaseTextEditor*>(editor());
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
static inline Kit *getActiveKit(DesignDocument *designDocument)
|
2013-01-23 12:31:22 +01:00
|
|
|
{
|
2015-07-09 13:53:12 +02:00
|
|
|
ProjectExplorer::Project *currentProject = ProjectExplorer::SessionManager::projectForFile(designDocument->fileName());
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2014-03-05 15:17:43 +01:00
|
|
|
if (!currentProject)
|
2015-07-09 13:53:12 +02:00
|
|
|
currentProject = ProjectExplorer::ProjectTree::currentProject();
|
2014-03-05 15:17:43 +01:00
|
|
|
|
2013-01-23 12:31:22 +01:00
|
|
|
if (!currentProject)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-02-20 13:59:31 +01:00
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
QObject::connect(ProjectTree::instance(), &ProjectTree::currentProjectChanged,
|
2014-11-19 17:58:33 +01:00
|
|
|
designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2016-05-24 15:57:24 +02:00
|
|
|
QObject::connect(currentProject, &Project::activeProjectConfigurationChanged,
|
2015-02-01 18:44:47 +02:00
|
|
|
designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
Target *target = currentProject->activeTarget();
|
2013-01-23 12:31:22 +01:00
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2015-07-09 13:53:12 +02:00
|
|
|
if (!target->kit()->isValid())
|
|
|
|
|
return 0;
|
2015-02-01 18:44:47 +02:00
|
|
|
QObject::connect(target, &Target::kitChanged,
|
|
|
|
|
designDocument, &DesignDocument::updateActiveQtVersion, Qt::UniqueConnection);
|
2014-03-26 19:41:51 +01:00
|
|
|
|
|
|
|
|
return target->kit();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignDocument::updateActiveQtVersion()
|
|
|
|
|
{
|
2016-05-24 15:57:24 +02:00
|
|
|
Kit *k = getActiveKit(this);
|
|
|
|
|
if (k == m_currentKit)
|
|
|
|
|
return;
|
|
|
|
|
m_currentKit = k;
|
2014-03-26 19:41:51 +01:00
|
|
|
viewManager().setNodeInstanceViewKit(m_currentKit);
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignDocument::contextHelpId() const
|
|
|
|
|
{
|
2014-05-13 17:53:04 +02:00
|
|
|
if (view())
|
2015-07-09 13:50:03 +02:00
|
|
|
return view()->contextHelpId();
|
2013-01-23 12:31:22 +01:00
|
|
|
|
2014-05-13 17:53:04 +02:00
|
|
|
return QString();
|
2013-01-23 12:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|