forked from qt-creator/qt-creator
QmlDesigner: Implement extract component feature
A feature in the navigator where user can click a context menu button to extract a component into the the parent view Task-number: QDS-14799 Change-Id: I415fc521a226574489c38c95b0167b433412c8be Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
@@ -72,6 +72,7 @@ inline constexpr char jumpToCodeCommandId[] = "JumpToCode";
|
|||||||
inline constexpr char mergeTemplateCommandId[] = "MergeTemplate";
|
inline constexpr char mergeTemplateCommandId[] = "MergeTemplate";
|
||||||
inline constexpr char goToImplementationCommandId[] = "GoToImplementation";
|
inline constexpr char goToImplementationCommandId[] = "GoToImplementation";
|
||||||
inline constexpr char makeComponentCommandId[] = "MakeComponent";
|
inline constexpr char makeComponentCommandId[] = "MakeComponent";
|
||||||
|
inline constexpr char extractComponentCommandId[] = "ExtractComponent";
|
||||||
inline constexpr char importComponentCommandId[] = "ImportComponent";
|
inline constexpr char importComponentCommandId[] = "ImportComponent";
|
||||||
inline constexpr char exportComponentCommandId[] = "ExportComponent";
|
inline constexpr char exportComponentCommandId[] = "ExportComponent";
|
||||||
inline constexpr char editMaterialCommandId[] = "EditMaterial";
|
inline constexpr char editMaterialCommandId[] = "EditMaterial";
|
||||||
@@ -167,6 +168,8 @@ inline constexpr char goToImplementationDisplayName[] = QT_TRANSLATE_NOOP("QmlDe
|
|||||||
"Go to Implementation");
|
"Go to Implementation");
|
||||||
inline constexpr char makeComponentDisplayName[] = QT_TRANSLATE_NOOP("QmlDesignerContextMenu",
|
inline constexpr char makeComponentDisplayName[] = QT_TRANSLATE_NOOP("QmlDesignerContextMenu",
|
||||||
"Create Component");
|
"Create Component");
|
||||||
|
inline constexpr char extractComponentDisplayName[] = QT_TRANSLATE_NOOP("QmlDesignerContextMenu",
|
||||||
|
"Extract Component");
|
||||||
inline constexpr char editMaterialDisplayName[] = QT_TRANSLATE_NOOP("QmlDesignerContextMenu",
|
inline constexpr char editMaterialDisplayName[] = QT_TRANSLATE_NOOP("QmlDesignerContextMenu",
|
||||||
"Edit Material");
|
"Edit Material");
|
||||||
inline constexpr char addToContentLibraryDisplayName[] = QT_TRANSLATE_NOOP(
|
inline constexpr char addToContentLibraryDisplayName[] = QT_TRANSLATE_NOOP(
|
||||||
|
@@ -1958,7 +1958,7 @@ void DesignerActionManager::createDefaultDesignerActions()
|
|||||||
contextIcon(DesignerIcons::EnterComponentIcon),
|
contextIcon(DesignerIcons::EnterComponentIcon),
|
||||||
rootCategory,
|
rootCategory,
|
||||||
QKeySequence(Qt::Key_F2),
|
QKeySequence(Qt::Key_F2),
|
||||||
Priorities::ComponentActions + 4,
|
Priorities::ComponentActions + 5,
|
||||||
&goIntoComponentOperation,
|
&goIntoComponentOperation,
|
||||||
&selectionIsEditableComponent));
|
&selectionIsEditableComponent));
|
||||||
|
|
||||||
@@ -2027,13 +2027,24 @@ void DesignerActionManager::createDefaultDesignerActions()
|
|||||||
&singleSelection,
|
&singleSelection,
|
||||||
&singleSelection));
|
&singleSelection));
|
||||||
|
|
||||||
|
addDesignerAction(new ModelNodeContextMenuAction(
|
||||||
|
extractComponentCommandId,
|
||||||
|
extractComponentDisplayName,
|
||||||
|
contextIcon(DesignerIcons::MakeComponentIcon),
|
||||||
|
rootCategory,
|
||||||
|
QKeySequence(),
|
||||||
|
Priorities::ComponentActions + 3,
|
||||||
|
&extractComponent,
|
||||||
|
&singleSelection,
|
||||||
|
&isFileComponent));
|
||||||
|
|
||||||
addDesignerAction(new ModelNodeContextMenuAction(
|
addDesignerAction(new ModelNodeContextMenuAction(
|
||||||
editInEffectComposerCommandId,
|
editInEffectComposerCommandId,
|
||||||
editInEffectComposerDisplayName,
|
editInEffectComposerDisplayName,
|
||||||
contextIcon(DesignerIcons::EditIcon),
|
contextIcon(DesignerIcons::EditIcon),
|
||||||
rootCategory,
|
rootCategory,
|
||||||
QKeySequence(),
|
QKeySequence(),
|
||||||
Priorities::ComponentActions + 3,
|
Priorities::ComponentActions + 4,
|
||||||
&editInEffectComposer,
|
&editInEffectComposer,
|
||||||
&SelectionContextFunctors::always, // If action is visible, it is usable
|
&SelectionContextFunctors::always, // If action is visible, it is usable
|
||||||
&singleSelectionEffectComposer));
|
&singleSelectionEffectComposer));
|
||||||
|
@@ -48,6 +48,20 @@ inline bool inBaseState(const SelectionContext &selectionState)
|
|||||||
return selectionState.isInBaseState();
|
return selectionState.isInBaseState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool isFileComponent(const SelectionContext &selectionContext)
|
||||||
|
{
|
||||||
|
//TODO: FLAG to hide/show the action until it's completed
|
||||||
|
bool shouldShowAction = false;
|
||||||
|
if (shouldShowAction && selectionContext.isValid() && selectionContext.singleNodeIsSelected()) {
|
||||||
|
ModelNode node = selectionContext.currentSingleSelectedNode();
|
||||||
|
if (node.hasMetaInfo()) {
|
||||||
|
NodeMetaInfo nodeInfo = node.metaInfo();
|
||||||
|
return nodeInfo.isFileComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool singleSelection(const SelectionContext &selectionState)
|
inline bool singleSelection(const SelectionContext &selectionState)
|
||||||
{
|
{
|
||||||
return selectionState.singleNodeIsSelected();
|
return selectionState.singleNodeIsSelected();
|
||||||
|
@@ -14,12 +14,15 @@
|
|||||||
|
|
||||||
#include <bindingproperty.h>
|
#include <bindingproperty.h>
|
||||||
#include <choosefrompropertylistdialog.h>
|
#include <choosefrompropertylistdialog.h>
|
||||||
|
#include <designdocumentview.h>
|
||||||
#include <designermcumanager.h>
|
#include <designermcumanager.h>
|
||||||
#include <designmodewidget.h>
|
#include <designmodewidget.h>
|
||||||
#include <documentmanager.h>
|
#include <documentmanager.h>
|
||||||
#include <itemlibraryentry.h>
|
#include <itemlibraryentry.h>
|
||||||
|
#include <modelmerger.h>
|
||||||
#include <modelnode.h>
|
#include <modelnode.h>
|
||||||
#include <modelnodeutils.h>
|
#include <modelnodeutils.h>
|
||||||
|
#include <modelutils.h>
|
||||||
#include <nodehints.h>
|
#include <nodehints.h>
|
||||||
#include <nodeinstanceview.h>
|
#include <nodeinstanceview.h>
|
||||||
#include <nodelistproperty.h>
|
#include <nodelistproperty.h>
|
||||||
@@ -59,6 +62,7 @@
|
|||||||
#include <qtsupport/qtkitaspect.h>
|
#include <qtsupport/qtkitaspect.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/fileutils.h>
|
||||||
#include <utils/qtcprocess.h>
|
#include <utils/qtcprocess.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/smallstring.h>
|
#include <utils/smallstring.h>
|
||||||
@@ -865,6 +869,90 @@ void moveToComponent(const SelectionContext &selectionContext)
|
|||||||
selectionContext.view()->model()->rewriterView()->moveToComponent(modelNode);
|
selectionContext.view()->model()->rewriterView()->moveToComponent(modelNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void extractComponent(const SelectionContext &selectionContext)
|
||||||
|
{
|
||||||
|
ModelNode selectedNode = selectionContext.currentSingleSelectedNode();
|
||||||
|
AbstractView *contextView = selectionContext.view();
|
||||||
|
|
||||||
|
// Get the path of the qml component
|
||||||
|
QString filePath = ModelUtils::componentFilePath(selectedNode);
|
||||||
|
if (filePath.isEmpty()) {
|
||||||
|
qWarning() << "Qml file for component " << selectedNode.displayName() << "not found!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the content of the qml component
|
||||||
|
QString componentText;
|
||||||
|
Utils::FilePath path = Utils::FilePath::fromString(filePath);
|
||||||
|
Utils::FileReader reader;
|
||||||
|
if (!reader.fetch(path)) {
|
||||||
|
qWarning() << "Cannot open component file " << filePath;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
componentText = QString::fromUtf8(reader.data());
|
||||||
|
|
||||||
|
#ifdef QDS_USE_PROJECTSTORAGE
|
||||||
|
ModelPointer inputModel = contextView->model()->createModel("Rectangle");
|
||||||
|
#else
|
||||||
|
ModelPointer inputModel = Model::create("QtQuick.Rectangle", 1, 0, contextView->model());
|
||||||
|
inputModel->setFileUrl(contextView->model()->fileUrl());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Create ModelNodes from qml string
|
||||||
|
// This is not including the root node by default
|
||||||
|
QPlainTextEdit textEdit;
|
||||||
|
QString imports;
|
||||||
|
const QList<Import> modelImports = contextView->model()->imports();
|
||||||
|
for (const Import &import : modelImports)
|
||||||
|
imports += "import " + import.toString(true) + QLatin1Char('\n');
|
||||||
|
|
||||||
|
textEdit.setPlainText(imports + componentText);
|
||||||
|
NotIndentingTextEditModifier modifier(textEdit.document());
|
||||||
|
|
||||||
|
RewriterView rewriterView{contextView->externalDependencies()};
|
||||||
|
rewriterView.setCheckSemanticErrors(false);
|
||||||
|
rewriterView.setPossibleImportsEnabled(false);
|
||||||
|
rewriterView.setTextModifier(&modifier);
|
||||||
|
inputModel->setRewriterView(&rewriterView);
|
||||||
|
rewriterView.restoreAuxiliaryData();
|
||||||
|
|
||||||
|
if (rewriterView.errors().isEmpty() && rewriterView.rootModelNode().isValid()) {
|
||||||
|
try {
|
||||||
|
ModelMerger merger(contextView);
|
||||||
|
merger.insertModel(rewriterView.rootModelNode());
|
||||||
|
} catch(Exception &/*e*/) {
|
||||||
|
qWarning() << "Cannot add model " << rewriterView.rootModelNode().displayName();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge the nodes in to the current document model
|
||||||
|
ModelPointer pasteModel = DesignDocumentView::pasteToModel(contextView->externalDependencies());
|
||||||
|
QTC_ASSERT(pasteModel, return);
|
||||||
|
|
||||||
|
DesignDocumentView view{contextView->externalDependencies()};
|
||||||
|
pasteModel->attachView(&view);
|
||||||
|
QTC_ASSERT(view.rootModelNode().isValid(), return);
|
||||||
|
|
||||||
|
pasteModel->detachView(&view);
|
||||||
|
contextView->model()->attachView(&view);
|
||||||
|
ModelNode originalNode = rewriterView.rootModelNode();
|
||||||
|
view.executeInTransaction("DesignerActionManager::extractComponent", [=, &view]() {
|
||||||
|
// Acquire the root of selected node
|
||||||
|
const ModelNode rootOfSelection = selectedNode.parentProperty().parentModelNode();
|
||||||
|
QTC_ASSERT(rootOfSelection.isValid(), return);
|
||||||
|
|
||||||
|
ModelNode newNode = view.insertModel(originalNode);
|
||||||
|
rootOfSelection.defaultNodeListProperty().reparentHere(newNode);
|
||||||
|
|
||||||
|
// Delete current selected node
|
||||||
|
QmlDesignerPlugin::instance()->currentDesignDocument()->deleteSelected();
|
||||||
|
|
||||||
|
// Set selection to inserted nodes
|
||||||
|
contextView->setSelectedModelNode(newNode);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void add3DAssetToContentLibrary(const SelectionContext &selectionContext)
|
void add3DAssetToContentLibrary(const SelectionContext &selectionContext)
|
||||||
{
|
{
|
||||||
QmlDesignerPlugin::instance()->mainWidget()->showDockWidget("ContentLibrary");
|
QmlDesignerPlugin::instance()->mainWidget()->showDockWidget("ContentLibrary");
|
||||||
|
@@ -100,6 +100,7 @@ void addSignalHandlerOrGotoImplementation(const SelectionContext &selectionState
|
|||||||
void removeLayout(const SelectionContext &selectionContext);
|
void removeLayout(const SelectionContext &selectionContext);
|
||||||
void removePositioner(const SelectionContext &selectionContext);
|
void removePositioner(const SelectionContext &selectionContext);
|
||||||
void moveToComponent(const SelectionContext &selectionContext);
|
void moveToComponent(const SelectionContext &selectionContext);
|
||||||
|
void extractComponent(const SelectionContext &selectionContext);
|
||||||
void add3DAssetToContentLibrary(const SelectionContext &selectionContext);
|
void add3DAssetToContentLibrary(const SelectionContext &selectionContext);
|
||||||
PropertyName getIndexPropertyName(const ModelNode &modelNode);
|
PropertyName getIndexPropertyName(const ModelNode &modelNode);
|
||||||
void addItemToStackedContainer(const SelectionContext &selectionContext);
|
void addItemToStackedContainer(const SelectionContext &selectionContext);
|
||||||
|
Reference in New Issue
Block a user