labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

matrixcommands.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : matrixcommands.h
3  Project : LabPlot
4  Description : Commands used in Matrix (part of the undo/redo framework)
5  --------------------------------------------------------------------
6  Copyright : (C) 2008 Tilman Benkert (thzs@gmx.net)
7  Copyright : (C) 2015 Alexander Semke (alexander.semke@web.de)
8  Copyright : (C) 2017 Stefan Gerlach (stefan.gerlach@uni.kn)
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 
31 #ifndef MATRIX_COMMANDS_H
32 #define MATRIX_COMMANDS_H
33 
34 #include <QUndoCommand>
35 #include <KLocalizedString>
36 #include "Matrix.h"
37 #include "MatrixPrivate.h"
38 
39 //! Insert columns
40 class MatrixInsertColumnsCmd : public QUndoCommand {
41 public:
42  MatrixInsertColumnsCmd(MatrixPrivate*, int before, int count, QUndoCommand* = nullptr);
43  void redo() override;
44  void undo() override;
45 
46 private:
48  int m_before; //! Column to insert before
49  int m_count; //! The number of new columns
50 };
51 
52 //! Insert rows
53 class MatrixInsertRowsCmd : public QUndoCommand {
54 public:
55  MatrixInsertRowsCmd(MatrixPrivate*, int before, int count, QUndoCommand* = nullptr);
56  void redo() override;
57  void undo() override;
58 
59 private:
61  int m_before; //! Row to insert before
62  int m_count; //! The number of new rows
63 };
64 
65 //! Remove columns
66 template <typename T>
67 class MatrixRemoveColumnsCmd : public QUndoCommand {
68 public:
69  MatrixRemoveColumnsCmd(MatrixPrivate* private_obj, int first, int count, QUndoCommand* parent = nullptr)
70  : QUndoCommand(parent), m_private_obj(private_obj), m_first(first), m_count(count) {
71  setText(i18np("%1: remove %2 column", "%1: remove %2 columns", m_private_obj->name(), m_count));
72  }
73  void redo() override {
74  if(m_backups.isEmpty()) {
75  int last_row = m_private_obj->rowCount-1;
76  for (int i = 0; i < m_count; i++)
77  m_backups.append(m_private_obj->columnCells<T>(m_first+i, 0, last_row));
78  }
81  }
82  void undo() override {
84  int last_row = m_private_obj->rowCount-1;
85  //TODO: use memcopy to copy from the backup vector
86  for (int i = 0; i < m_count; i++)
87  m_private_obj->setColumnCells(m_first+i, 0, last_row, m_backups.at(i));
88 
90  }
91 
92 private:
94 
95  int m_first; //! First column to remove
96  int m_count; //! The number of columns to remove
97  QVector<QVector<T>> m_backups; //! Backups of the removed columns
98 };
99 
100 //! Remove rows
101 template <typename T>
102 class MatrixRemoveRowsCmd : public QUndoCommand {
103 public:
104  MatrixRemoveRowsCmd(MatrixPrivate* private_obj, int first, int count, QUndoCommand* parent = nullptr)
105  : QUndoCommand(parent), m_private_obj(private_obj), m_first(first), m_count(count) {
106  setText(i18np("%1: remove %2 row", "%1: remove %2 rows", m_private_obj->name(), m_count));
107  }
108  void redo() override {
109  if(m_backups.isEmpty()) {
110  int last_row = m_first+m_count-1;
111  for (int col = 0; col < m_private_obj->columnCount; col++)
112  m_backups.append(m_private_obj->columnCells<T>(col, m_first, last_row));
113  }
116  }
117  void undo() override {
119  int last_row = m_first+m_count-1;
120  for (int col = 0; col < m_private_obj->columnCount; col++)
121  m_private_obj->setColumnCells(col, m_first, last_row, m_backups.at(col));
123  }
124 
125 private:
127  int m_first; //! First row to remove
128  int m_count; //! The number of rows to remove
129  QVector< QVector<T> > m_backups; //! Backups of the removed rows
130 };
131 
132 //! Clear matrix
133 template <typename T>
134 class MatrixClearCmd : public QUndoCommand {
135 public:
136  explicit MatrixClearCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr)
137  : QUndoCommand(parent), m_private_obj(private_obj) {
138  setText(i18n("%1: clear", m_private_obj->name()));
139  }
140  void redo() override {
141  if(m_backups.isEmpty()) {
142  int last_row = m_private_obj->rowCount-1;
143 
144  for (int i = 0; i < m_private_obj->columnCount; i++)
145  m_backups.append(m_private_obj->columnCells<T>(i, 0, last_row));
146  }
147 
148  for (int i = 0; i < m_private_obj->columnCount; i++)
150  }
151  void undo() override {
152  int last_row = m_private_obj->rowCount-1;
153  for (int i = 0; i < m_private_obj->columnCount; i++)
154  m_private_obj->setColumnCells(i, 0, last_row, m_backups.at(i));
155  }
156 
157 private:
159  QVector<QVector<T>> m_backups; //! Backups of the cleared cells
160 };
161 
162 //! Clear matrix column
163 template <typename T>
164 class MatrixClearColumnCmd : public QUndoCommand {
165 public:
166  MatrixClearColumnCmd(MatrixPrivate* private_obj, int col, QUndoCommand* parent = nullptr)
167  : QUndoCommand(parent), m_private_obj(private_obj), m_col(col) {
168  setText(i18n("%1: clear column %2", m_private_obj->name(), m_col+1));
169  }
170  void redo() override {
171  if(m_backup.isEmpty())
174  }
175  void undo() override {
177  }
178 
179 private:
181  int m_col; //! The index of the column
182  QVector<T> m_backup; //! Backup of the cleared column
183 };
184 
185 // Set cell value
186 template <typename T>
187 class MatrixSetCellValueCmd : public QUndoCommand {
188 public:
189  MatrixSetCellValueCmd(MatrixPrivate* private_obj, int row, int col, T value, QUndoCommand* parent = nullptr)
190  : QUndoCommand(parent), m_private_obj(private_obj), m_row(row), m_col(col), m_value(value), m_old_value(value) {
191  // remark: don't use many QString::arg() calls in ctors of commands that might be called often,
192  // they use a lot of execution time
193  setText(i18n("%1: set cell value", m_private_obj->name()));
194  }
195  void redo() override {
198  }
199  void undo() override {
201  }
202 
203 private:
205  int m_row; //! The index of the row
206  int m_col; //! The index of the column
207  T m_value; //! New cell value
208  T m_old_value; //! Backup of the changed value
209 };
210 
211 // Set matrix coordinates
212 class MatrixSetCoordinatesCmd : public QUndoCommand {
213 public:
214  MatrixSetCoordinatesCmd(MatrixPrivate*, double x1, double x2, double y1, double y2, QUndoCommand* = nullptr);
215  void redo() override;
216  void undo() override;
217 
218 private:
220  double m_new_x1;
221  double m_new_x2;
222  double m_new_y1;
223  double m_new_y2;
224  double m_old_x1{-1};
225  double m_old_x2{-1};
226  double m_old_y1{-1};
227  double m_old_y2{-1};
228 };
229 
230 //! Set matrix formula
231 class MatrixSetFormulaCmd : public QUndoCommand {
232 public:
233  MatrixSetFormulaCmd(MatrixPrivate*, QString formula);
234  void redo() override;
235  void undo() override;
236 
237 private:
240 };
241 
242 // Set cell values for (a part of) a column at once
243 template <typename T>
244 class MatrixSetColumnCellsCmd : public QUndoCommand {
245 public:
246  MatrixSetColumnCellsCmd(MatrixPrivate* private_obj, int col, int first_row, int last_row, const QVector<T>& values, QUndoCommand* parent = nullptr)
247  : QUndoCommand(parent), m_private_obj(private_obj), m_col(col), m_first_row(first_row), m_last_row(last_row), m_values(values) {
248  setText(i18n("%1: set cell values", m_private_obj->name()));
249  }
250  void redo() override {
251  if (m_old_values.isEmpty())
254  }
255  void undo() override {
257  }
258 
259 private:
261  int m_col; //! The index of the column
262  int m_first_row; //! The index of the first row
263  int m_last_row; //! The index of the last row
264  QVector<T> m_values; //! New cell values
265  QVector<T> m_old_values; //! Backup of the changed values
266 };
267 
268 //! Set cell values for (a part of) a row at once
269 template <typename T>
270 class MatrixSetRowCellsCmd : public QUndoCommand {
271 public:
272  MatrixSetRowCellsCmd(MatrixPrivate* private_obj, int row, int first_column, int last_column, const QVector<T>& values, QUndoCommand* parent = nullptr)
273  : QUndoCommand(parent), m_private_obj(private_obj), m_row(row), m_first_column(first_column),
274  m_last_column(last_column), m_values(values) {
275  setText(i18n("%1: set cell values", m_private_obj->name()));
276  }
277  void redo() override {
278  if (m_old_values.isEmpty())
281  }
282  void undo() override {
284  }
285 
286 private:
288  int m_row; //! The index of the row
289  int m_first_column; //! The index of the first column
290  int m_last_column; //! The index of the last column
291  QVector<T> m_values; //! New cell values
292  QVector<T> m_old_values; //! Backup of the changed values
293 };
294 
295 //! Transpose the matrix
296 template <typename T>
297 class MatrixTransposeCmd : public QUndoCommand {
298 public:
299  explicit MatrixTransposeCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr)
300  : QUndoCommand(parent), m_private_obj(private_obj) {
301  setText(i18n("%1: transpose", m_private_obj->name()));
302  }
303  void redo() override {
304  int rows = m_private_obj->rowCount;
305  int cols = m_private_obj->columnCount;
306  int temp_size = qMax(rows, cols);
308  if (cols < rows)
309  m_private_obj->insertColumns(cols, temp_size - cols);
310  else if (cols > rows)
311  m_private_obj->insertRows(rows, temp_size - rows);
312 
313  for (int i = 1; i < temp_size; i++) {
314  QVector<T> row = m_private_obj->rowCells<T>(i, 0, i-1);
315  QVector<T> col = m_private_obj->columnCells<T>(i, 0, i-1);
316  m_private_obj->setRowCells(i, 0, i-1, col);
317  m_private_obj->setColumnCells(i, 0, i-1, row);
318  }
319 
320  if (cols < rows)
321  m_private_obj->removeRows(cols, temp_size - cols);
322  else if (cols > rows)
323  m_private_obj->removeColumns(rows, temp_size - rows);
326  }
327  void undo() override {
328  redo();
329  }
330 
331 private:
333 };
334 
335 //! Mirror the matrix horizontally
336 template <typename T>
337 class MatrixMirrorHorizontallyCmd : public QUndoCommand {
338 public:
339  explicit MatrixMirrorHorizontallyCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr)
340  : QUndoCommand(parent), m_private_obj(private_obj) {
341  setText(i18n("%1: mirror horizontally", m_private_obj->name()));
342  }
343  void redo() override {
344  int rows = m_private_obj->rowCount;
345  int cols = m_private_obj->columnCount;
346  int middle = cols/2;
348 
349  for (int i = 0; i<middle; i++) {
350  QVector<T> temp = m_private_obj->columnCells<T>(i, 0, rows-1);
351  m_private_obj->setColumnCells(i, 0, rows-1, m_private_obj->columnCells<T>(cols-i-1, 0, rows-1));
352  m_private_obj->setColumnCells(cols-i-1, 0, rows-1, temp);
353  }
355  m_private_obj->emitDataChanged(0, 0, rows-1, cols-1);
356  }
357  void undo() override {
358  redo();
359  }
360 
361 private:
363 };
364 
365 // Mirror the matrix vertically
366 template <typename T>
367 class MatrixMirrorVerticallyCmd : public QUndoCommand {
368 public:
369  explicit MatrixMirrorVerticallyCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr)
370  : QUndoCommand(parent), m_private_obj(private_obj) {
371  setText(i18n("%1: mirror vertically", m_private_obj->name()));
372  }
373  void redo() override {
374  int rows = m_private_obj->rowCount;
375  int cols = m_private_obj->columnCount;
376  int middle = rows/2;
378 
379  for (int i = 0; i < middle; i++) {
380  QVector<T> temp = m_private_obj->rowCells<T>(i, 0, cols-1);
381  m_private_obj->setRowCells(i, 0, cols-1, m_private_obj->rowCells<T>(rows-i-1, 0, cols-1));
382  m_private_obj->setRowCells(rows-i-1, 0, cols-1, temp);
383  }
384 
386  m_private_obj->emitDataChanged(0, 0, rows-1, cols-1);
387  }
388  void undo() override {
389  redo();
390  }
391 
392 private:
394 };
395 
396 // Replace matrix values
397 class MatrixReplaceValuesCmd : public QUndoCommand {
398 public:
399  explicit MatrixReplaceValuesCmd(MatrixPrivate*, void* new_values, QUndoCommand* = nullptr);
400  void redo() override;
401  void undo() override;
402 
403 private:
405  void* m_old_values{nullptr};
407 };
408 
409 #endif // MATRIX_COMMANDS_H
Clear matrix.
void undo() override
MatrixPrivate * m_private_obj
MatrixClearCmd(MatrixPrivate *private_obj, QUndoCommand *parent=nullptr)
QVector< QVector< T > > m_backups
void redo() override
Clear matrix column.
MatrixClearColumnCmd(MatrixPrivate *private_obj, int col, QUndoCommand *parent=nullptr)
void undo() override
MatrixPrivate * m_private_obj
void redo() override
QVector< T > m_backup
The index of the column.
MatrixInsertColumnsCmd(MatrixPrivate *, int before, int count, QUndoCommand *=nullptr)
MatrixPrivate * m_private_obj
int m_count
Column to insert before.
void redo() override
MatrixInsertRowsCmd(MatrixPrivate *, int before, int count, QUndoCommand *=nullptr)
MatrixPrivate * m_private_obj
int m_count
Row to insert before.
void undo() override
Mirror the matrix horizontally.
MatrixMirrorHorizontallyCmd(MatrixPrivate *private_obj, QUndoCommand *parent=nullptr)
MatrixPrivate * m_private_obj
MatrixMirrorVerticallyCmd(MatrixPrivate *private_obj, QUndoCommand *parent=nullptr)
void removeRows(int first, int count)
Definition: Matrix.cpp:965
void clearColumn(int col)
Fill column with zeroes.
Definition: Matrix.cpp:1003
void insertColumns(int before, int count)
Definition: Matrix.cpp:840
void removeColumns(int first, int count)
Definition: Matrix.cpp:887
QVector< T > columnCells(int col, int first_row, int last_row)
Definition: MatrixPrivate.h:70
void setCell(int row, int col, T value)
Definition: MatrixPrivate.h:59
void setRowCells(int row, int first_column, int last_column, const QVector< T > &values)
T cell(int row, int col) const
Definition: MatrixPrivate.h:50
QVector< T > rowCells(int row, int first_column, int last_column)
bool suppressDataChange
void insertRows(int before, int count)
Definition: Matrix.cpp:921
void setColumnCells(int col, int first_row, int last_row, const QVector< T > &values)
Definition: MatrixPrivate.h:84
void emitDataChanged(int top, int left, int bottom, int right)
QString name() const
Definition: MatrixPrivate.h:46
void undo() override
MatrixPrivate * m_private_obj
QVector< QVector< T > > m_backups
The number of columns to remove.
MatrixRemoveColumnsCmd(MatrixPrivate *private_obj, int first, int count, QUndoCommand *parent=nullptr)
void redo() override
int m_count
First column to remove.
QVector< QVector< T > > m_backups
The number of rows to remove.
MatrixRemoveRowsCmd(MatrixPrivate *private_obj, int first, int count, QUndoCommand *parent=nullptr)
int m_count
First row to remove.
void undo() override
void redo() override
MatrixPrivate * m_private_obj
MatrixReplaceValuesCmd(MatrixPrivate *, void *new_values, QUndoCommand *=nullptr)
MatrixPrivate * m_private_obj
T m_value
The index of the column.
T m_old_value
New cell value.
MatrixPrivate * m_private_obj
MatrixSetCellValueCmd(MatrixPrivate *private_obj, int row, int col, T value, QUndoCommand *parent=nullptr)
void redo() override
void undo() override
int m_col
The index of the row.
MatrixSetColumnCellsCmd(MatrixPrivate *private_obj, int col, int first_row, int last_row, const QVector< T > &values, QUndoCommand *parent=nullptr)
QVector< T > m_values
The index of the last row.
QVector< T > m_old_values
New cell values.
MatrixPrivate * m_private_obj
int m_first_row
The index of the column.
int m_last_row
The index of the first row.
MatrixSetCoordinatesCmd(MatrixPrivate *, double x1, double x2, double y1, double y2, QUndoCommand *=nullptr)
MatrixPrivate * m_private_obj
Set matrix formula.
void redo() override
MatrixSetFormulaCmd(MatrixPrivate *, QString formula)
MatrixPrivate * m_private_obj
void undo() override
Set cell values for (a part of) a row at once.
int m_last_column
The index of the first column.
QVector< T > m_values
The index of the last column.
void redo() override
int m_first_column
The index of the row.
MatrixSetRowCellsCmd(MatrixPrivate *private_obj, int row, int first_column, int last_column, const QVector< T > &values, QUndoCommand *parent=nullptr)
void undo() override
QVector< T > m_old_values
New cell values.
MatrixPrivate * m_private_obj
Transpose the matrix.
MatrixPrivate * m_private_obj
MatrixTransposeCmd(MatrixPrivate *private_obj, QUndoCommand *parent=nullptr)
void undo() override
void redo() override
void columnCountChanged(int)
void rowCountChanged(int)
#define i18n(m)
Definition: nsl_common.h:38