File indexing completed on 2024-05-12 15:26:59
0001 /*************************************************************************** 0002 File : commandtemplates.h 0003 Project : LabPlot 0004 Description : Undo/Redo command templates 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2009 Tilman Benkert (thzs@gmx.net) 0007 Copyright : (C) 2017 by Alexander Semke (alexander.semke@web.de) 0008 ***************************************************************************/ 0009 0010 /*************************************************************************** 0011 * * 0012 * This program is free software; you can redistribute it and/or modify * 0013 * it under the terms of the GNU General Public License as published by * 0014 * the Free Software Foundation; either version 2 of the License, or * 0015 * (at your option) any later version. * 0016 * * 0017 * This program is distributed in the hope that it will be useful, * 0018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0020 * GNU General Public License for more details. * 0021 * * 0022 * You should have received a copy of the GNU General Public License * 0023 * along with this program; if not, write to the Free Software * 0024 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0025 * Boston, MA 02110-1301 USA * 0026 * * 0027 ***************************************************************************/ 0028 0029 #ifndef COMMANDTEMPLATES_H 0030 #define COMMANDTEMPLATES_H 0031 0032 #include <QUndoCommand> 0033 0034 #include <KLocalizedString> 0035 0036 template <class target_class, typename value_type> 0037 class StandardSetterCmd : public QUndoCommand { 0038 public: 0039 StandardSetterCmd(target_class* target, value_type target_class::* field, value_type newValue, const KLocalizedString& description) // use ki18n("%1: ...") 0040 : m_target(target), m_field(field), m_otherValue(newValue) { 0041 setText(description.subs(m_target->name()).toString()); 0042 } 0043 0044 virtual void initialize() {}; 0045 virtual void finalize() {}; 0046 0047 void redo() override { 0048 initialize(); 0049 value_type tmp = *m_target.*m_field; 0050 *m_target.*m_field = m_otherValue; 0051 m_otherValue = tmp; 0052 finalize(); 0053 } 0054 0055 void undo() override { redo(); } 0056 0057 protected: 0058 target_class* m_target; 0059 value_type target_class::*m_field; 0060 value_type m_otherValue; 0061 }; 0062 0063 template <class target_class, typename value_type> 0064 class StandardMacroSetterCmd : public QUndoCommand { 0065 public: 0066 StandardMacroSetterCmd(target_class* target, value_type target_class::*field, value_type newValue, const KLocalizedString& description) // use ki18n("%1: ...") 0067 : m_target(target), m_field(field), m_otherValue(newValue) { 0068 setText(description.subs(m_target->name()).toString()); 0069 } 0070 0071 virtual void initialize() {}; 0072 virtual void finalize() {}; 0073 virtual void finalizeUndo() {}; 0074 0075 void redo() override { 0076 initialize(); 0077 value_type tmp = *m_target.*m_field; 0078 *m_target.*m_field = m_otherValue; 0079 m_otherValue = tmp; 0080 finalize(); 0081 } 0082 0083 //call finalizeUndo() at the end where only the signal is emitted 0084 //and no actual finalize-method is called that can potentially 0085 //cause new entries on the undo-stack 0086 void undo() override { 0087 initialize(); 0088 value_type tmp = *m_target.*m_field; 0089 *m_target.*m_field = m_otherValue; 0090 m_otherValue = tmp; 0091 finalizeUndo(); 0092 } 0093 0094 protected: 0095 target_class* m_target; 0096 value_type target_class::*m_field; 0097 value_type m_otherValue; 0098 }; 0099 0100 template <class target_class, typename value_type> 0101 class StandardSwapMethodSetterCmd : public QUndoCommand { 0102 public: 0103 StandardSwapMethodSetterCmd(target_class* target, value_type (target_class::*method)(value_type), value_type newValue, const KLocalizedString& description) // use ki18n("%1: ...") 0104 : m_target(target), m_method(method), m_otherValue(newValue) { 0105 setText(description.subs(m_target->name()).toString()); 0106 } 0107 0108 virtual void initialize() {}; 0109 virtual void finalize() {}; 0110 0111 void redo() override { 0112 initialize(); 0113 m_otherValue = (*m_target.*m_method)(m_otherValue); 0114 finalize(); 0115 } 0116 0117 void undo() override { redo(); } 0118 0119 protected: 0120 target_class* m_target; 0121 value_type (target_class::*m_method)(value_type); 0122 value_type m_otherValue; 0123 }; 0124 0125 #endif