File indexing completed on 2024-05-12 04:35:03
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013-2018 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "UndoSetProperty.h" 0021 #include "Path.h" 0022 #include "Document.h" 0023 #include "PropertyManager.h" 0024 0025 namespace tikz { 0026 namespace core { 0027 0028 UndoSetProperty::UndoSetProperty(Document * doc) 0029 : UndoItem("Change Property", doc) 0030 { 0031 } 0032 0033 UndoSetProperty::UndoSetProperty(const Uid & entityUid, const QString & propertyName, const QVariant & newValue) 0034 : UndoItem("Change Property", entityUid.document()) 0035 , m_entityUid(entityUid) 0036 , m_propertyName(propertyName) 0037 , m_redoValue(newValue) 0038 { 0039 // get path to save data 0040 auto entity = m_entityUid.entity(); 0041 Q_ASSERT(entity); 0042 0043 auto info = propertyManager().info(propertyName); 0044 if (info.isResettable()) { 0045 bool isModified = false; 0046 const bool ok = QMetaObject::invokeMethod(entity, 0047 info.modifiedFunction().toLatin1().data(), 0048 Qt::DirectConnection, 0049 Q_RETURN_ARG(bool, isModified) 0050 ); 0051 if (!ok) { 0052 qDebug() << "Unable to check modified state of property" + m_propertyName; 0053 entity->setProperty(m_propertyName.toLatin1().data(), m_undoValue); 0054 } 0055 0056 m_undoValue = (ok && isModified) ? entity->property(propertyName.toLatin1().data()) : QVariant(); 0057 } else { 0058 m_undoValue = entity->property(propertyName.toLatin1().data()); 0059 } 0060 } 0061 0062 UndoSetProperty::~UndoSetProperty() 0063 { 0064 } 0065 0066 void UndoSetProperty::undo() 0067 { 0068 auto entity = m_entityUid.entity(); 0069 Q_ASSERT(entity); 0070 0071 if (m_undoValue.isValid()) { 0072 entity->setProperty(m_propertyName.toLatin1().data(), m_undoValue); 0073 } else { 0074 const auto info = propertyManager().info(m_propertyName); 0075 const bool ok = QMetaObject::invokeMethod(entity, 0076 info.resetFunction().toLatin1().data(), 0077 Qt::DirectConnection 0078 ); 0079 if (!ok) { 0080 qDebug() << "Unable to reset property" + m_propertyName; 0081 entity->setProperty(m_propertyName.toLatin1().data(), m_undoValue); 0082 } 0083 } 0084 } 0085 0086 void UndoSetProperty::redo() 0087 { 0088 auto entity = m_entityUid.entity(); 0089 Q_ASSERT(entity); 0090 0091 if (m_redoValue.isValid()) { 0092 entity->setProperty(m_propertyName.toLatin1().data(), m_redoValue); 0093 } else { 0094 const auto info = propertyManager().info(m_propertyName); 0095 const bool ok = QMetaObject::invokeMethod(entity, 0096 info.resetFunction().toLatin1().data(), 0097 Qt::DirectConnection 0098 ); 0099 if (!ok) { 0100 qDebug() << "Unable to reset property" + m_propertyName; 0101 entity->setProperty(m_propertyName.toLatin1().data(), m_redoValue); 0102 } 0103 } 0104 } 0105 0106 bool UndoSetProperty::mergeWith(const UndoItem * command) 0107 { 0108 // only merge when command is of correct type 0109 auto other = static_cast<const UndoSetProperty*>(command); 0110 if (m_entityUid != other->m_entityUid || m_propertyName != other->m_propertyName) { 0111 return false; 0112 } 0113 0114 m_redoValue = other->m_redoValue; 0115 return true; 0116 } 0117 0118 void UndoSetProperty::loadData(const QJsonObject & json) 0119 { 0120 m_entityUid = Uid(json["uid"].toString(), document()); 0121 //m_redoStyle.load(json["style"].toObject()); 0122 } 0123 0124 QJsonObject UndoSetProperty::saveData() const 0125 { 0126 QJsonObject json; 0127 json["type"] = "entity-set-property"; 0128 json["uid"] = m_entityUid.toString(); 0129 // json["style"] = m_redoStyle.save(); 0130 return json; 0131 } 0132 0133 } 0134 } 0135 0136 // kate: indent-width 4; replace-tabs on;