KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
modifyelementcommand.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14*/
15
17
18#include "element.h"
19
20#include "debug.h"
21
22using namespace KDSME;
23
24namespace {
25
26void setElementGeometry(Element *item, const QRectF &geometry)
27{
28 item->setPos(geometry.topLeft());
29 item->setWidth(geometry.width());
30 item->setHeight(geometry.height());
31}
32
33}
34
36 : Command(QString(), parent)
37 , m_operation(NoOperation)
38 , m_item(item)
39{
40 Q_ASSERT(item);
41}
42
44{
45 return m_item;
46}
47
49{
50 m_moveByData = QPointF(dx, dy);
51 m_operation = MoveOperation;
52 updateText();
53}
54
55void ModifyElementCommand::setGeometry(const QRectF &geometry)
56{
57 m_newGeometry = geometry;
59 updateText();
60}
61
63{
64 if (!m_item || m_operation == NoOperation) {
65 qCDebug(KDSME_VIEW) << "Invalid item or no operation requested";
66 return;
67 }
68
69 switch (m_operation) {
70 case MoveOperation:
71 m_item->setPos(m_item->pos() + m_moveByData);
72 break;
73 case SetGeometryOperation:
74 m_oldGeometry = QRectF(m_item->pos(), QSizeF(m_item->width(), m_item->height()));
75 setElementGeometry(m_item.data(), m_newGeometry);
76 break;
77 }
78}
79
81{
82 if (!m_item || m_operation == NoOperation) {
83 qCDebug(KDSME_VIEW) << "Invalid item or no operation requested";
84 return;
85 }
86
87 switch (m_operation) {
88 case MoveOperation:
89 m_item->setPos(m_item->pos() - m_moveByData);
90 break;
91 case SetGeometryOperation:
92 setElementGeometry(m_item.data(), m_oldGeometry);
93 break;
94 }
95}
96
97bool ModifyElementCommand::mergeWith(const QUndoCommand *other)
98{
99 if (other->id() != id()) {
100 return false;
101 }
102
103 auto cmd = static_cast<const ModifyElementCommand *>(other);
104 if (cmd->m_item != m_item || cmd->m_operation != m_operation) {
105 return false;
106 }
107
108 m_moveByData += cmd->m_moveByData;
109 m_newGeometry = cmd->m_newGeometry;
110 return true;
111}
112
113void KDSME::ModifyElementCommand::updateText()
114{
115 const QString itemLabel = m_item ? m_item->label() : tr("<Unknown>");
116
117 switch (m_operation) {
118 case MoveOperation:
119 setText(tr("Moving item %1").arg(itemLabel));
120 break;
121 case SetGeometryOperation:
122 setText(tr("Resizing item %1").arg(itemLabel));
123 break;
124 default:
125 setText(QString());
126 break;
127 }
128}
void setPos(const QPointF &pos)
Definition element.cpp:97
void setWidth(qreal width)
Definition element.cpp:111
void setHeight(qreal height)
Definition element.cpp:125
Command for modifying properties of KDSME::Element.
Q_INVOKABLE void moveBy(qreal dx, qreal dy)
ModifyElementCommand(Element *item, QUndoCommand *parent=nullptr)
bool mergeWith(const QUndoCommand *other) override
Q_INVOKABLE void setGeometry(const QRectF &geometry)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8