QElectroTech  0.70
qpropertyundocommand.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qpropertyundocommand.h"
19 #include <QPropertyAnimation>
20 
29 QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_value, const QVariant &new_value, QUndoCommand *parent) :
30  QUndoCommand(parent),
31  m_object(object),
32  m_property_name(property_name),
33  m_old_value(old_value),
34  m_new_value(new_value)
35 {}
36 
46 QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_value, QUndoCommand *parent) :
47  QUndoCommand(parent),
48  m_object(object),
49  m_property_name(property_name),
50  m_old_value(old_value)
51 {}
52 
54 {
55  m_object = other->m_object;
57  m_old_value = other->m_old_value;
58  m_new_value = other->m_new_value;
59  m_animate = other->m_animate;
60  m_first_time = other->m_first_time;
61  setText(other->text());
62 }
63 
69 void QPropertyUndoCommand::setNewValue(const QVariant &new_value) {
70  m_new_value = new_value;
71 }
72 
79  m_animate = animate;
80 }
81 
88 void QPropertyUndoCommand::setAnimated(bool animate, bool first_time)
89 {
90  m_animate = animate;
91  m_first_time = first_time;
92 }
93 
100 bool QPropertyUndoCommand::mergeWith(const QUndoCommand *other)
101 {
102  if (id() != other->id() || other->childCount()) return false;
103  QPropertyUndoCommand const *undo = static_cast<const QPropertyUndoCommand *>(other);
104  if (m_object != undo->m_object || m_property_name != undo->m_property_name) return false;
105  m_new_value = undo->m_new_value;
106  return true;
107 }
108 
114 {
115  if (m_object->property(m_property_name) != m_new_value)
116  {
117  if (m_animate && m_first_time)
118  {
119  QPropertyAnimation *animation = new QPropertyAnimation(m_object, m_property_name);
120  animation->setStartValue(m_old_value);
121  animation->setEndValue(m_new_value);
122  animation->start(QAbstractAnimation::DeleteWhenStopped);
123  }
124  else
125  {
126  m_object->setProperty(m_property_name, m_new_value);
127  m_first_time = true;
128  }
129  }
130 
131  QUndoCommand::redo();
132 }
133 
139 {
140  if (m_object->property(m_property_name) != m_old_value)
141  {
142  if (m_animate)
143  {
144  QPropertyAnimation *animation = new QPropertyAnimation(m_object, m_property_name);
145  animation->setStartValue(m_new_value);
146  animation->setEndValue(m_old_value);
147  animation->start(QAbstractAnimation::DeleteWhenStopped);
148  }
149  else
150  m_object->setProperty(m_property_name, m_old_value);
151  }
152 
153  QUndoCommand::undo();
154 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_value, const QVariant &new_value, QUndoCommand *parent=nullptr)
QPropertyUndoCommand::QPropertyUndoCommand Default constructor with old and new value This command do...
void enableAnimation(bool animate=true)
QPropertyUndoCommand::enableAnimation True to enable animation.
void undo() override
QPropertyUndoCommand::undo Undo this command.
bool mergeWith(const QUndoCommand *other) override
QPropertyUndoCommand::mergeWith Try to merge this command with other command.
void redo() override
QPropertyUndoCommand::redo Redo this command.
void setAnimated(bool animate=true, bool first_time=true)
QPropertyUndoCommand::setAnimated.
void setNewValue(const QVariant &new_value)
QPropertyUndoCommand::setNewValue Set the new value of the property (set with redo) to ...