Commit 27f261f0 authored by Marco Bubke's avatar Marco Bubke

QmlDesigner: Refactor designer actions

Change-Id: Ie9c71e564e66874b0715ed35cfacc578ad8b58b0
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 46960958
......@@ -61,7 +61,7 @@ public:
virtual QString menuId() const = 0;
virtual int priority() const = 0;
virtual Type type() const = 0;
virtual void setCurrentContext(const SelectionContext &selectionState) = 0;
virtual void currentContextChanged(const SelectionContext &selectionState) = 0;
};
......
......@@ -42,7 +42,7 @@ DefaultDesignerAction::DefaultDesignerAction(DefaultAction *action)
{
}
void DefaultDesignerAction::setCurrentContext(const SelectionContext &selectionContext)
void DefaultDesignerAction::currentContextChanged(const SelectionContext &selectionContext)
{
m_selectionContext = selectionContext;
updateContext();
......
......@@ -59,12 +59,12 @@ public:
DefaultDesignerAction(DefaultAction *action);
QAction *action() const { return m_action; }
void setCurrentContext(const SelectionContext &selectionContext);
void currentContextChanged(const SelectionContext &selectionContext);
protected:
virtual void updateContext();
virtual bool isVisible(const SelectionContext &selectionState) const = 0;
virtual bool isEnabled(const SelectionContext &selectionState) const = 0;
virtual bool isVisible(const SelectionContext &selectionContext) const = 0;
virtual bool isEnabled(const SelectionContext &selectionContext) const = 0;
DefaultAction *defaultAction() const;
SelectionContext selectionContext() const;
......
......@@ -195,7 +195,7 @@ protected:
}
SelectionContext selectionContext(this);
foreach (AbstractDesignerAction* action, m_designerActionList) {
action->setCurrentContext(selectionContext);
action->currentContextChanged(selectionContext);
}
m_setupContextDirty = false;
}
......@@ -270,7 +270,7 @@ public:
}
if (m_action->isEnabled()) {
ModelNode parentNode;
if (m_selectionContext.singleSelected() && !m_selectionContext.currentSingleSelectedNode().isRootNode()) {
if (m_selectionContext.isSingleNodeIsSelected() && !m_selectionContext.currentSingleSelectedNode().isRootNode()) {
ActionTemplate *selectionAction = new ActionTemplate(QString(), &ModelNodeOperations::select);
selectionAction->setParent(m_menu.data());
......@@ -282,7 +282,7 @@ public:
m_menu->addAction(selectionAction);
}
foreach (const ModelNode &node, m_selectionContext.view()->allModelNodes()) {
foreach (const ModelNode &node, m_selectionContext.qmlModelView()->allModelNodes()) {
if (node != m_selectionContext.currentSingleSelectedNode()
&& node != parentNode
&& contains(node, m_selectionContext.scenePos())
......
......@@ -89,7 +89,7 @@ void populateMenu(QSet<AbstractDesignerAction* > &abstractDesignerActions,
foreach (AbstractDesignerAction* designerAction, matchingFactoriesList) {
if (designerAction->type() == AbstractDesignerAction::Menu) {
designerAction->setCurrentContext(selectionContext);
designerAction->currentContextChanged(selectionContext);
QMenu *newMenu = designerAction->action()->menu();
menu->addMenu(newMenu);
......@@ -98,7 +98,7 @@ void populateMenu(QSet<AbstractDesignerAction* > &abstractDesignerActions,
populateMenu(abstractDesignerActions, designerAction->menuId(), newMenu, selectionContext);
} else if (designerAction->type() == AbstractDesignerAction::Action) {
QAction* action = designerAction->action();
designerAction->setCurrentContext(selectionContext);
designerAction->currentContextChanged(selectionContext);
menu->addAction(action);
}
}
......
......@@ -56,7 +56,7 @@ inline bool inBaseState(const SelectionContext &selectionState)
inline bool singleSelection(const SelectionContext &selectionState)
{
return selectionState.singleSelected();
return selectionState.isSingleNodeIsSelected();
}
inline bool selectionEnabled(const SelectionContext &selectionState)
......@@ -71,7 +71,7 @@ inline bool selectionNotEmpty(const SelectionContext &selectionState)
inline bool singleSelectionNotRoot(const SelectionContext &selectionState)
{
return selectionState.singleSelected()
return selectionState.isSingleNodeIsSelected()
&& !selectionState.currentSingleSelectedNode().isRootNode();
}
......@@ -144,7 +144,7 @@ public:
AbstractDesignerAction::Type type() const { return AbstractDesignerAction::Menu; }
QAction *action() const { return m_action; }
virtual void setCurrentContext(const SelectionContext &selectionContext)
virtual void currentContextChanged(const SelectionContext &selectionContext)
{
m_selectionContext = selectionContext;
updateContext();
......@@ -184,7 +184,7 @@ public:
QString menuId() const { return QString(); }
int priority() const { return m_priority; }
Type type() const { return Action; }
void setCurrentContext(const SelectionContext &) {}
void currentContextChanged(const SelectionContext &) {}
private:
const QString m_category;
......
......@@ -122,19 +122,19 @@ void goIntoComponent(const ModelNode &modelNode)
void select(const SelectionContext &selectionState)
{
if (selectionState.view())
selectionState.view()->setSelectedModelNodes(QList<ModelNode>() << selectionState.targetNode());
if (selectionState.qmlModelView())
selectionState.qmlModelView()->setSelectedModelNodes(QList<ModelNode>() << selectionState.targetNode());
}
void deSelect(const SelectionContext &selectionState)
{
if (selectionState.view()) {
QList<ModelNode> selectedNodes = selectionState.view()->selectedModelNodes();
if (selectionState.qmlModelView()) {
QList<ModelNode> selectedNodes = selectionState.qmlModelView()->selectedModelNodes();
foreach (const ModelNode &node, selectionState.selectedModelNodes()) {
if (selectedNodes.contains(node))
selectedNodes.removeAll(node);
}
selectionState.view()->setSelectedModelNodes(selectedNodes);
selectionState.qmlModelView()->setSelectedModelNodes(selectedNodes);
}
}
......@@ -153,7 +153,7 @@ void deleteSelection(const SelectionContext &)
void toFront(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
......@@ -171,7 +171,7 @@ void toFront(const SelectionContext &selectionState)
void toBack(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
QmlItemNode node = selectionState.selectedModelNodes().first();
......@@ -188,11 +188,11 @@ void toBack(const SelectionContext &selectionState)
void raise(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
foreach (ModelNode modelNode, selectionState.selectedModelNodes()) {
QmlItemNode node = modelNode;
if (node.isValid()) {
......@@ -209,11 +209,11 @@ void raise(const SelectionContext &selectionState)
void lower(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
foreach (ModelNode modelNode, selectionState.selectedModelNodes()) {
QmlItemNode node = modelNode;
if (node.isValid()) {
......@@ -241,7 +241,7 @@ void redo(const SelectionContext &)
void setVisible(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
......@@ -254,11 +254,11 @@ void setVisible(const SelectionContext &selectionState)
void resetSize(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
foreach (ModelNode node, selectionState.selectedModelNodes()) {
node.removeProperty("width");
node.removeProperty("height");
......@@ -270,11 +270,11 @@ void resetSize(const SelectionContext &selectionState)
void resetPosition(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
try {
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
foreach (ModelNode node, selectionState.selectedModelNodes()) {
node.removeProperty("x");
node.removeProperty("y");
......@@ -295,10 +295,10 @@ void setId(const SelectionContext &)
void resetZ(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
foreach (ModelNode node, selectionState.selectedModelNodes()) {
node.removeProperty("z");
}
......@@ -326,10 +326,10 @@ static inline void restoreProperty(ModelNode node, const PropertyName &propertyN
void anchorsFill(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
ModelNode modelNode = selectionState.currentSingleSelectedNode();
......@@ -345,10 +345,10 @@ void anchorsFill(const SelectionContext &selectionState)
void anchorsReset(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
ModelNode modelNode = selectionState.currentSingleSelectedNode();
......@@ -428,10 +428,10 @@ static inline QPoint getUpperLeftPosition(const QList<ModelNode> &modelNodeList)
void layoutRow(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
NodeMetaInfo rowMetaInfo = selectionState.view()->model()->metaInfo("QtQuick.Row");
NodeMetaInfo rowMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Row");
if (!rowMetaInfo.isValid())
return;
......@@ -440,7 +440,7 @@ void layoutRow(const SelectionContext &selectionState)
ModelNode row;
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
if (!parent.isValid())
......@@ -448,13 +448,13 @@ void layoutRow(const SelectionContext &selectionState)
qDebug() << parent.modelNode().majorQtQuickVersion();
row = selectionState.view()->createModelNode("QtQuick.Row", rowMetaInfo.majorVersion(), rowMetaInfo.minorVersion());
row = selectionState.qmlModelView()->createModelNode("QtQuick.Row", rowMetaInfo.majorVersion(), rowMetaInfo.minorVersion());
reparentTo(row, parent);
}
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QPoint pos = getUpperLeftPosition(modelNodeList);
row.variantProperty("x") = pos.x();
......@@ -473,10 +473,10 @@ void layoutRow(const SelectionContext &selectionState)
void layoutColumn(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
NodeMetaInfo columnMetaInfo = selectionState.view()->model()->metaInfo("QtQuick.Column");
NodeMetaInfo columnMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Column");
if (!columnMetaInfo.isValid())
return;
......@@ -485,19 +485,19 @@ void layoutColumn(const SelectionContext &selectionState)
ModelNode column;
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
if (!parent.isValid())
return;
column = selectionState.view()->createModelNode("QtQuick.Column", columnMetaInfo.majorVersion(), columnMetaInfo.minorVersion());
column = selectionState.qmlModelView()->createModelNode("QtQuick.Column", columnMetaInfo.majorVersion(), columnMetaInfo.minorVersion());
reparentTo(column, parent);
}
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QPoint pos = getUpperLeftPosition(modelNodeList);
column.variantProperty("x") = pos.x();
......@@ -516,10 +516,10 @@ void layoutColumn(const SelectionContext &selectionState)
void layoutGrid(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
NodeMetaInfo gridMetaInfo = selectionState.view()->model()->metaInfo("QtQuick.Grid");
NodeMetaInfo gridMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Grid");
if (!gridMetaInfo.isValid())
return;
......@@ -528,20 +528,20 @@ void layoutGrid(const SelectionContext &selectionState)
ModelNode grid;
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
if (!parent.isValid())
return;
grid = selectionState.view()->createModelNode("QtQuick.Grid", gridMetaInfo.majorVersion(), gridMetaInfo.minorVersion());
grid = selectionState.qmlModelView()->createModelNode("QtQuick.Grid", gridMetaInfo.majorVersion(), gridMetaInfo.minorVersion());
grid.variantProperty("columns") = int(sqrt(double(modelNodeList.count())));
reparentTo(grid, parent);
}
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QPoint pos = getUpperLeftPosition(modelNodeList);
grid.variantProperty("x") = pos.x();
......@@ -560,10 +560,10 @@ void layoutGrid(const SelectionContext &selectionState)
void layoutFlow(const SelectionContext &selectionState)
{
if (!selectionState.view())
if (!selectionState.qmlModelView())
return;
NodeMetaInfo flowMetaInfo = selectionState.view()->model()->metaInfo("QtQuick.Flow");
NodeMetaInfo flowMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Flow");
if (!flowMetaInfo.isValid())
return;
......@@ -572,19 +572,19 @@ void layoutFlow(const SelectionContext &selectionState)
ModelNode flow;
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
if (!parent.isValid())
return;
flow = selectionState.view()->createModelNode("QtQuick.Flow", flowMetaInfo.majorVersion(), flowMetaInfo.minorVersion());
flow = selectionState.qmlModelView()->createModelNode("QtQuick.Flow", flowMetaInfo.majorVersion(), flowMetaInfo.minorVersion());
reparentTo(flow, parent);
}
{
RewriterTransaction transaction(selectionState.view());
RewriterTransaction transaction(selectionState.qmlModelView());
QPoint pos = getUpperLeftPosition(modelNodeList);
flow.variantProperty("x") = pos.x();
......
......@@ -33,27 +33,92 @@ namespace QmlDesigner {
SelectionContext::SelectionContext() :
m_view(0),
m_isInBaseState(false),
m_toggled(false)
{
}
SelectionContext::SelectionContext(QmlModelView *view) :
m_view(view),
m_isInBaseState(view->currentState().isBaseState()),
SelectionContext::SelectionContext(QmlModelView *qmlModelView) :
m_qmlModelView(qmlModelView),
m_isInBaseState(qmlModelView->currentState().isBaseState()),
m_toggled(false)
{
if (m_view && m_view->model())
m_selectedModelNodes = view->selectedModelNodes();
if (qmlModelView && qmlModelView->model())
m_selectedModelNodes = qmlModelView->selectedModelNodes();
}
void SelectionContext::setTargetNode(const ModelNode &modelNode)
{
m_targetNode = modelNode;
}
ModelNode SelectionContext::targetNode() const
{
return m_targetNode;
}
bool SelectionContext::isSingleNodeIsSelected() const
{
return m_selectedModelNodes.count() == 1;
}
bool SelectionContext::isInBaseState() const
{
return m_isInBaseState;
}
ModelNode SelectionContext::currentSingleSelectedNode() const
{
if (m_selectedModelNodes.count() != 1)
return ModelNode();
return m_selectedModelNodes.first();
}
QList<ModelNode> SelectionContext::selectedModelNodes() const
{
return m_selectedModelNodes;
}
QmlModelView *SelectionContext::qmlModelView() const
{
return m_qmlModelView.data();
}
void SelectionContext::setShowSelectionTools(bool show)
{
m_showSelectionTools = show;
}
bool SelectionContext::showSelectionTools() const
{
return m_showSelectionTools;
}
void SelectionContext::setScenePos(const QPoint &postition)
{
m_scenePosition = postition;
}
if (m_selectedModelNodes.count()== 1) {
m_singleSelected = true;
m_currentSingleSelectedNode = m_selectedModelNodes.first();
} else {
m_singleSelected = false;
}
QPoint SelectionContext::scenePos() const
{
return m_scenePosition;
}
void SelectionContext::setToggled(bool toggled)
{
m_toggled = toggled;
}
bool SelectionContext::toggled() const
{
return m_toggled;
}
bool SelectionContext::isValid() const
{
return qmlModelView() && qmlModelView()->model() && qmlModelView()->nodeInstanceView();
}
} //QmlDesigner
......@@ -39,59 +39,37 @@ class QMLDESIGNERCORE_EXPORT SelectionContext {
public:
SelectionContext();
SelectionContext(QmlModelView *view);
SelectionContext(QmlModelView *qmlModelView);
void setTargetNode(const ModelNode &modelNode)
{ m_targetNode = modelNode; }
void setTargetNode(const ModelNode &modelNode);
ModelNode targetNode() const;
bool singleSelected() const
{ return m_singleSelected; }
bool isSingleNodeIsSelected() const;
bool isInBaseState() const;
bool isInBaseState() const
{ return m_isInBaseState; }
ModelNode currentSingleSelectedNode() const;
QList<ModelNode> selectedModelNodes() const;
ModelNode targetNode() const
{ return m_targetNode; }
QmlModelView *qmlModelView() const;
ModelNode currentSingleSelectedNode() const
{ return m_currentSingleSelectedNode; }
void setShowSelectionTools(bool show);
bool showSelectionTools() const;
QList<ModelNode> selectedModelNodes() const
{ return m_selectedModelNodes; }
void setScenePos(const QPoint &postition);
QPoint scenePos() const;
QmlModelView *view() const
{ return m_view; }
void setToggled(bool toggled);
bool toggled() const;
void setShowSelectionTools(bool show)
{ m_showSelectionTools = show; }
bool showSelectionTools() const
{ return m_showSelectionTools; }
QPoint scenePos() const
{ return m_scenePos; }
void setScenePos(const QPoint &pos)
{ m_scenePos = pos; }
void setToggled(bool b)
{ m_toggled = b; }
bool toggled() const
{ return m_toggled; }
bool isValid() const
{ return view() && view()->model() && view()->nodeInstanceView(); }
bool isValid() const;
private:
QmlModelView *m_view;
bool m_singleSelected;
ModelNode m_currentSingleSelectedNode;
QWeakPointer<QmlModelView> m_qmlModelView;
ModelNode m_targetNode;
bool m_isInBaseState;
QList<ModelNode> m_selectedModelNodes;
bool m_showSelectionTools;
QPoint m_scenePos;