File indexing completed on 2024-05-12 03:47:47

0001 /*
0002     File                 : matrixcommands.cpp
0003     Project              : LabPlot
0004     Description          : Commands used in Matrix (part of the undo/redo framework)
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2008 Tilman Benkert <thzs@gmx.net>
0007     SPDX-FileCopyrightText: 2015 Alexander Semke <alexander.semke@web.de>
0008     SPDX-FileCopyrightText: 2017 Stefan Gerlach <stefan.gerlach@uni.kn>
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "matrixcommands.h"
0013 #include "MatrixPrivate.h"
0014 
0015 // Insert columns
0016 MatrixInsertColumnsCmd::MatrixInsertColumnsCmd(MatrixPrivate* private_obj, int before, int count, QUndoCommand* parent)
0017     : QUndoCommand(parent)
0018     , m_private_obj(private_obj)
0019     , m_before(before)
0020     , m_count(count) {
0021     setText(i18np("%1: insert %2 column", "%1: insert %2 columns", m_private_obj->name(), m_count));
0022 }
0023 
0024 void MatrixInsertColumnsCmd::redo() {
0025     m_private_obj->insertColumns(m_before, m_count);
0026     Q_EMIT m_private_obj->q->columnCountChanged(m_private_obj->columnCount);
0027 }
0028 
0029 void MatrixInsertColumnsCmd::undo() {
0030     m_private_obj->removeColumns(m_before, m_count);
0031     Q_EMIT m_private_obj->q->columnCountChanged(m_private_obj->columnCount);
0032 }
0033 
0034 // Insert rows
0035 MatrixInsertRowsCmd::MatrixInsertRowsCmd(MatrixPrivate* private_obj, int before, int count, QUndoCommand* parent)
0036     : QUndoCommand(parent)
0037     , m_private_obj(private_obj)
0038     , m_before(before)
0039     , m_count(count) {
0040     setText(i18np("%1: insert %2 row", "%1: insert %2 rows", m_private_obj->name(), m_count));
0041 }
0042 
0043 void MatrixInsertRowsCmd::redo() {
0044     m_private_obj->insertRows(m_before, m_count);
0045     Q_EMIT m_private_obj->q->rowCountChanged(m_private_obj->rowCount);
0046 }
0047 
0048 void MatrixInsertRowsCmd::undo() {
0049     m_private_obj->removeRows(m_before, m_count);
0050     Q_EMIT m_private_obj->q->rowCountChanged(m_private_obj->rowCount);
0051 }
0052 
0053 // set coordinates
0054 MatrixSetCoordinatesCmd::MatrixSetCoordinatesCmd(MatrixPrivate* private_obj, double x1, double x2, double y1, double y2, QUndoCommand* parent)
0055     : QUndoCommand(parent)
0056     , m_private_obj(private_obj)
0057     , m_new_x1(x1)
0058     , m_new_x2(x2)
0059     , m_new_y1(y1)
0060     , m_new_y2(y2) {
0061     setText(i18n("%1: set matrix coordinates", m_private_obj->name()));
0062 }
0063 
0064 void MatrixSetCoordinatesCmd::redo() {
0065     m_old_x1 = m_private_obj->xStart;
0066     m_old_x2 = m_private_obj->xEnd;
0067     m_old_y1 = m_private_obj->yStart;
0068     m_old_y2 = m_private_obj->yEnd;
0069     m_private_obj->xStart = m_new_x1;
0070     m_private_obj->xEnd = m_new_x2;
0071     m_private_obj->yStart = m_new_y1;
0072     m_private_obj->yEnd = m_new_y2;
0073 }
0074 
0075 void MatrixSetCoordinatesCmd::undo() {
0076     m_private_obj->xStart = m_old_x1;
0077     m_private_obj->xEnd = m_old_x2;
0078     m_private_obj->yStart = m_old_y1;
0079     m_private_obj->yEnd = m_old_y2;
0080 }
0081 
0082 // set formula
0083 MatrixSetFormulaCmd::MatrixSetFormulaCmd(MatrixPrivate* private_obj, QString formula)
0084     : m_private_obj(private_obj)
0085     , m_other_formula(std::move(formula)) {
0086     setText(i18n("%1: set formula", m_private_obj->name()));
0087 }
0088 
0089 void MatrixSetFormulaCmd::redo() {
0090     QString tmp = m_private_obj->formula;
0091     m_private_obj->formula = m_other_formula;
0092     m_other_formula = tmp;
0093 }
0094 
0095 void MatrixSetFormulaCmd::undo() {
0096     redo();
0097 }
0098 
0099 // replace values
0100 MatrixReplaceValuesCmd::MatrixReplaceValuesCmd(MatrixPrivate* private_obj, void* new_values, QUndoCommand* parent)
0101     : QUndoCommand(parent)
0102     , m_private_obj(private_obj)
0103     , m_new_values(new_values) {
0104     setText(i18n("%1: replace values", m_private_obj->name()));
0105 }
0106 
0107 void MatrixReplaceValuesCmd::redo() {
0108     m_old_values = m_private_obj->data;
0109     m_private_obj->data = m_new_values;
0110     m_private_obj->emitDataChanged(0, 0, m_private_obj->rowCount - 1, m_private_obj->columnCount - 1);
0111 }
0112 
0113 void MatrixReplaceValuesCmd::undo() {
0114     m_new_values = m_private_obj->data;
0115     m_private_obj->data = m_old_values;
0116     m_private_obj->emitDataChanged(0, 0, m_private_obj->rowCount - 1, m_private_obj->columnCount - 1);
0117 }