File indexing completed on 2024-06-16 03:42:33

0001 /*
0002     File                 : columncommands.h
0003     Project              : LabPlot
0004     Description          : Commands to be called by Column to modify ColumnPrivate
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2007, 2008 Tilman Benkert <thzs@gmx.net>
0007     SPDX-FileCopyrightText: 2010 Knut Franke <knut.franke@gmx.de>
0008     SPDX-FileCopyrightText: 2009-2023 Alexander Semke <alexander.semke@web.de>
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #ifndef COLUMNCOMMANDS_H
0013 #define COLUMNCOMMANDS_H
0014 
0015 #include "backend/core/column/Column.h"
0016 #include "backend/core/column/ColumnPrivate.h"
0017 #include "backend/lib/IntervalAttribute.h"
0018 
0019 #include <KLocalizedString>
0020 
0021 #include <QDateTime>
0022 #include <QUndoCommand>
0023 
0024 class AbstractSimpleFilter;
0025 
0026 class ColumnSetModeCmd : public QUndoCommand {
0027 public:
0028     explicit ColumnSetModeCmd(ColumnPrivate* col, AbstractColumn::ColumnMode mode, QUndoCommand* parent = nullptr);
0029     ~ColumnSetModeCmd() override;
0030 
0031     void redo() override;
0032     void undo() override;
0033 
0034 private:
0035     ColumnPrivate* m_col;
0036     AbstractColumn::ColumnMode m_old_mode{AbstractColumn::ColumnMode::Double};
0037     AbstractColumn::ColumnMode m_mode;
0038     void* m_old_data{nullptr};
0039     void* m_new_data{nullptr};
0040     AbstractSimpleFilter* m_new_in_filter{nullptr};
0041     AbstractSimpleFilter* m_new_out_filter{nullptr};
0042     AbstractSimpleFilter* m_old_in_filter{nullptr};
0043     AbstractSimpleFilter* m_old_out_filter{nullptr};
0044     bool m_undone{false};
0045     bool m_executed{false};
0046 };
0047 
0048 class ColumnFullCopyCmd : public QUndoCommand {
0049 public:
0050     explicit ColumnFullCopyCmd(ColumnPrivate* col, const AbstractColumn* src, QUndoCommand* parent = nullptr);
0051     ~ColumnFullCopyCmd() override;
0052 
0053     void redo() override;
0054     void undo() override;
0055 
0056 private:
0057     ColumnPrivate* m_col;
0058     const AbstractColumn* m_src;
0059     ColumnPrivate* m_backup{nullptr};
0060     Column* m_backup_owner{nullptr};
0061 };
0062 
0063 class ColumnPartialCopyCmd : public QUndoCommand {
0064 public:
0065     explicit ColumnPartialCopyCmd(ColumnPrivate* col, const AbstractColumn* src, int src_start, int dest_start, int num_rows, QUndoCommand* parent = nullptr);
0066     ~ColumnPartialCopyCmd() override;
0067 
0068     void redo() override;
0069     void undo() override;
0070 
0071 private:
0072     ColumnPrivate* m_col;
0073     const AbstractColumn* m_src;
0074     ColumnPrivate* m_col_backup{nullptr};
0075     ColumnPrivate* m_src_backup{nullptr};
0076     Column* m_col_backup_owner{nullptr};
0077     Column* m_src_backup_owner{nullptr};
0078     int m_src_start;
0079     int m_dest_start;
0080     int m_num_rows;
0081     int m_old_row_count{0};
0082 };
0083 
0084 class ColumnInsertRowsCmd : public QUndoCommand {
0085 public:
0086     explicit ColumnInsertRowsCmd(ColumnPrivate* col, int before, int count, QUndoCommand* parent = nullptr);
0087 
0088     void redo() override;
0089     void undo() override;
0090     void finalize() const;
0091 
0092 private:
0093     ColumnPrivate* m_col;
0094     int m_before, m_count;
0095 };
0096 
0097 class ColumnRemoveRowsCmd : public QUndoCommand {
0098 public:
0099     explicit ColumnRemoveRowsCmd(ColumnPrivate* col, int first, int count, QUndoCommand* parent = nullptr);
0100     ~ColumnRemoveRowsCmd() override;
0101 
0102     void redo() override;
0103     void undo() override;
0104     void finalize() const;
0105 
0106 private:
0107     ColumnPrivate* m_col;
0108     int m_first, m_count;
0109     int m_data_row_count{0};
0110     int m_old_size{0};
0111     ColumnPrivate* m_backup{nullptr};
0112     Column* m_backup_owner{nullptr};
0113     IntervalAttribute<QString> m_formulas;
0114 };
0115 
0116 class ColumnSetPlotDesignationCmd : public QUndoCommand {
0117 public:
0118     explicit ColumnSetPlotDesignationCmd(ColumnPrivate* col, AbstractColumn::PlotDesignation pd, QUndoCommand* parent = nullptr);
0119 
0120     void redo() override;
0121     void undo() override;
0122 
0123 private:
0124     ColumnPrivate* m_col;
0125     AbstractColumn::PlotDesignation m_new_pd;
0126     AbstractColumn::PlotDesignation m_old_pd{AbstractColumn::PlotDesignation::X};
0127 };
0128 
0129 class ColumnClearCmd : public QUndoCommand {
0130 public:
0131     explicit ColumnClearCmd(ColumnPrivate* col, QUndoCommand* parent = nullptr);
0132     ~ColumnClearCmd() override;
0133 
0134     void redo() override;
0135     void undo() override;
0136 
0137 private:
0138     ColumnPrivate* m_col;
0139     void* m_data{nullptr};
0140     void* m_empty_data{nullptr};
0141     bool m_undone{false};
0142 };
0143 
0144 class ColumnSetGlobalFormulaCmd : public QUndoCommand {
0145 public:
0146     explicit ColumnSetGlobalFormulaCmd(ColumnPrivate* col,
0147                                        QString formula,
0148                                        QStringList variableNames,
0149                                        QVector<Column*> columns,
0150                                        bool autoUpdate,
0151                                        bool autoResize,
0152                                        QUndoCommand* parent = nullptr);
0153 
0154     void redo() override;
0155     void undo() override;
0156 
0157 private:
0158     ColumnPrivate* m_col;
0159     QString m_formula;
0160     QStringList m_variableNames;
0161     QVector<Column*> m_variableColumns;
0162     bool m_autoUpdate{false};
0163     bool m_autoResize{true};
0164     QString m_newFormula;
0165     QStringList m_newVariableNames;
0166     QVector<Column*> m_newVariableColumns;
0167     bool m_newAutoUpdate{false};
0168     bool m_newAutoResize{true};
0169     bool m_copied{false};
0170 };
0171 
0172 class ColumnSetFormulaCmd : public QUndoCommand {
0173 public:
0174     explicit ColumnSetFormulaCmd(ColumnPrivate* col, const Interval<int>& interval, QString formula, QUndoCommand* parent = nullptr);
0175 
0176     void redo() override;
0177     void undo() override;
0178 
0179 private:
0180     ColumnPrivate* m_col;
0181     Interval<int> m_interval;
0182     QString m_oldFormula;
0183     QString m_newFormula;
0184     IntervalAttribute<QString> m_formulas;
0185     bool m_copied{false};
0186 };
0187 
0188 class ColumnClearFormulasCmd : public QUndoCommand {
0189 public:
0190     explicit ColumnClearFormulasCmd(ColumnPrivate* col, QUndoCommand* parent = nullptr);
0191 
0192     void redo() override;
0193     void undo() override;
0194 
0195 private:
0196     ColumnPrivate* m_col;
0197     IntervalAttribute<QString> m_formulas;
0198     bool m_copied{false};
0199 };
0200 
0201 template<typename T>
0202 class ColumnSetCmd : public QUndoCommand {
0203 public:
0204     /**
0205      * \var ColumnSetTextCmd::m_col
0206      * \brief The private column data to modify
0207      */
0208     /**
0209      * \var ColumnSetTextCmd::m_row
0210      * \brief The row to modify
0211      */
0212     /**
0213      * \var ColumnSetTextCmd::m_new_value
0214      * \brief The new value
0215      */
0216     /**
0217      * \var ColumnSetTextCmd::m_old_value
0218      * \brief The old value
0219      */
0220     /**
0221      * \var ColumnSetTextCmd::m_row_count
0222      * \brief The old number of rows
0223      */
0224     explicit ColumnSetCmd(ColumnPrivate* col, int row, const T& old_value, const T& new_value, QUndoCommand* parent = nullptr)
0225         : QUndoCommand(parent)
0226         , m_col(col)
0227         , m_row(row)
0228         , m_new_value(std::move(new_value))
0229         , m_old_value(std::move(old_value)) {
0230         setText(i18n("%1: set value for row %2", col->name(), row));
0231     }
0232 
0233     void redo() override {
0234         m_row_count = m_col->rowCount();
0235         m_col->setValueAt(m_row, m_new_value);
0236     }
0237     void undo() override {
0238         m_col->setValueAt(m_row, m_old_value);
0239     }
0240 
0241 private:
0242     ColumnPrivate* m_col;
0243     int m_row;
0244     T m_new_value;
0245     T m_old_value;
0246     int m_row_count{0};
0247 };
0248 
0249 template<typename T>
0250 class ColumnReplaceCmd : public QUndoCommand {
0251 public:
0252     /**
0253      * \var ColumnReplaceTextsCmd::m_col
0254      * \brief The private column data to modify
0255      */
0256 
0257     /**
0258      * \var ColumnReplaceTextsCmd::m_first
0259      * \brief The first row to replace
0260      */
0261 
0262     /**
0263      * \var ColumnReplaceTextsCmd::m_new_values
0264      * \brief The new values
0265      */
0266 
0267     /**
0268      * \var ColumnReplaceTextsCmd::m_old_values
0269      * \brief The old values
0270      */
0271 
0272     /**
0273      * \var ColumnReplaceTextsCmd::m_copied
0274      * \brief Status flag
0275      */
0276 
0277     /**
0278      * \var ColumnReplaceTextsCmd::m_row_count
0279      * \brief The old number of rows
0280      */
0281     explicit ColumnReplaceCmd(ColumnPrivate* col, int first, const QVector<T>& new_values, QUndoCommand* parent = nullptr)
0282         : QUndoCommand(parent)
0283         , m_col(col)
0284         , m_first(first)
0285         , m_new_values(new_values) {
0286         if (m_first < 0)
0287             setText(i18n("%1: replace values", col->name()));
0288         else
0289             setText(i18n("%1: replace the values for rows %2 to %3", col->name(), first, first + new_values.count() - 1));
0290     }
0291 
0292     void redo() override {
0293         auto* data = m_col->data();
0294         if (!data)
0295             return;
0296 
0297         if (m_first < 0)
0298             m_old_values = *static_cast<QVector<T>*>(data);
0299         else
0300             m_old_values = static_cast<QVector<T>*>(data)->mid(m_first, m_new_values.count());
0301 
0302         m_col->replaceValues(m_first, m_new_values);
0303         m_new_values.clear(); // delete values, because otherwise we use a lot of ram even if we don't need it
0304     }
0305     void undo() override {
0306         auto* data = m_col->data();
0307         if (!data)
0308             return;
0309 
0310         if (m_first < 0)
0311             m_new_values = *static_cast<QVector<T>*>(data);
0312         else
0313             m_new_values = static_cast<QVector<T>*>(data)->mid(m_first, m_old_values.count());
0314 
0315         m_col->replaceValues(m_first, m_old_values);
0316         m_old_values.clear();
0317     }
0318 
0319 private:
0320     ColumnPrivate* m_col;
0321     int m_first;
0322     QVector<T> m_new_values;
0323     QVector<T> m_old_values;
0324 };
0325 
0326 #endif