File indexing completed on 2024-05-12 15:27:00

0001 /***************************************************************************
0002     File                 : PropertyChangeCommand.h
0003     Project              : SciDAVis / LabPlot
0004     --------------------------------------------------------------------
0005     Copyright            : (C) 2010 Knut Franke
0006     Email (use @ for *)  : Knut.Franke*gmx.net
0007     Description          : Generic undo command changing a single variable.
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  This program is free software; you can redistribute it and/or modify   *
0014  *  it under the terms of the GNU General Public License as published by   *
0015  *  the Free Software Foundation; either version 2 of the License, or      *
0016  *  (at your option) any later version.                                    *
0017  *                                                                         *
0018  *  This program is distributed in the hope that it will be useful,        *
0019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0021  *  GNU General Public License for more details.                           *
0022  *                                                                         *
0023  *   You should have received a copy of the GNU General Public License     *
0024  *   along with this program; if not, write to the Free Software           *
0025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0026  *   Boston, MA  02110-1301  USA                                           *
0027  *                                                                         *
0028  ***************************************************************************/
0029 
0030 #ifndef PROPERTY_CHANGE_COMMAND_H
0031 #define PROPERTY_CHANGE_COMMAND_H
0032 
0033 #include <QUndoCommand>
0034 
0035 /**
0036  * \class PropertyChangeCommand
0037  * \brief Generic undo command changing a single variable.
0038  *
0039  * Given a pointer to a variable (usually a member of the class instantiating the command, or of
0040  * its private implementation class) and a new value, assigns the value to the variable. A backup
0041  * of the old value is made, so that undo/redo can switch back and forth between the two values.
0042  * The value type needs to support copy construction and assignment.
0043  */
0044 
0045 template<class T> class PropertyChangeCommand : public QUndoCommand {
0046 
0047 public:
0048     PropertyChangeCommand(const QString &text, T *property, const T &new_value)
0049         : m_property(property), m_other_value(new_value) {
0050             setText(text);
0051         }
0052 
0053     void redo() override {
0054         T tmp = *m_property;
0055         *m_property = m_other_value;
0056         m_other_value = tmp;
0057     }
0058 
0059     void undo() override {
0060         redo();
0061     }
0062 
0063     int id() const override {
0064         return reinterpret_cast<std::intptr_t>(m_property);
0065     }
0066 
0067     bool mergeWith(const QUndoCommand* other) override {
0068         if (other->id() != id())
0069             return false;
0070 
0071         if (std::is_same<T, bool>::value)
0072             *m_property = *(static_cast<const PropertyChangeCommand*>(other)->m_property);
0073         else
0074             *m_property += *(static_cast<const PropertyChangeCommand*>(other)->m_property);
0075 
0076         return true;
0077     }
0078 
0079     T *m_property;
0080 
0081 private:
0082     T m_other_value;
0083 };
0084 
0085 #endif // ifndef PROPERTY_CHANGE_COMMAND_H