1 /***************************************************************************
2     File                 : matrixcommands.cpp
3     Project              : SciDAVis
4     Description          : Commands used in Matrix (part of the undo/redo framework)
5     --------------------------------------------------------------------
6     Copyright            : (C) 2008-2009 Tilman Benkert (thzs*gmx.net)
7                            (replace * with @ in the email addresses)
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *  This program is free software; you can redistribute it and/or modify   *
14  *  it under the terms of the GNU General Public License as published by   *
15  *  the Free Software Foundation; either version 2 of the License, or      *
16  *  (at your option) any later version.                                    *
17  *                                                                         *
18  *  This program is distributed in the hope that it will be useful,        *
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
21  *  GNU General Public License for more details.                           *
22  *                                                                         *
23  *   You should have received a copy of the GNU General Public License     *
24  *   along with this program; if not, write to the Free Software           *
25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
26  *   Boston, MA  02110-1301  USA                                           *
27  *                                                                         *
28  ***************************************************************************/
29 
30 #include "matrixcommands.h"
31 
32 ///////////////////////////////////////////////////////////////////////////
33 // class MatrixInsertColumnsCmd
34 ///////////////////////////////////////////////////////////////////////////
MatrixInsertColumnsCmd(future::Matrix::Private * private_obj,int before,int count,QUndoCommand * parent)35 MatrixInsertColumnsCmd::MatrixInsertColumnsCmd(future::Matrix::Private *private_obj, int before,
36                                                int count, QUndoCommand *parent)
37     : QUndoCommand(parent), d_private_obj(private_obj), d_before(before), d_count(count)
38 {
39     setText(QObject::tr("%1: insert %2 column(s)").arg(d_private_obj->name()).arg(d_count));
40 }
41 
~MatrixInsertColumnsCmd()42 MatrixInsertColumnsCmd::~MatrixInsertColumnsCmd() { }
43 
redo()44 void MatrixInsertColumnsCmd::redo()
45 {
46     d_private_obj->insertColumns(d_before, d_count);
47 }
48 
undo()49 void MatrixInsertColumnsCmd::undo()
50 {
51     d_private_obj->removeColumns(d_before, d_count);
52 }
53 ///////////////////////////////////////////////////////////////////////////
54 // end of class MatrixInsertColumnsCmd
55 ///////////////////////////////////////////////////////////////////////////
56 
57 ///////////////////////////////////////////////////////////////////////////
58 // class MatrixInsertRowsCmd
59 ///////////////////////////////////////////////////////////////////////////
MatrixInsertRowsCmd(future::Matrix::Private * private_obj,int before,int count,QUndoCommand * parent)60 MatrixInsertRowsCmd::MatrixInsertRowsCmd(future::Matrix::Private *private_obj, int before,
61                                          int count, QUndoCommand *parent)
62     : QUndoCommand(parent), d_private_obj(private_obj), d_before(before), d_count(count)
63 {
64     setText(QObject::tr("%1: insert %2 row(s)").arg(d_private_obj->name()).arg(d_count));
65 }
66 
~MatrixInsertRowsCmd()67 MatrixInsertRowsCmd::~MatrixInsertRowsCmd() { }
68 
redo()69 void MatrixInsertRowsCmd::redo()
70 {
71     d_private_obj->insertRows(d_before, d_count);
72 }
73 
undo()74 void MatrixInsertRowsCmd::undo()
75 {
76     d_private_obj->removeRows(d_before, d_count);
77 }
78 ///////////////////////////////////////////////////////////////////////////
79 // end of class MatrixInsertRowsCmd
80 ///////////////////////////////////////////////////////////////////////////
81 
82 ///////////////////////////////////////////////////////////////////////////
83 // class MatrixRemoveColumnsCmd
84 ///////////////////////////////////////////////////////////////////////////
MatrixRemoveColumnsCmd(future::Matrix::Private * private_obj,int first,int count,QUndoCommand * parent)85 MatrixRemoveColumnsCmd::MatrixRemoveColumnsCmd(future::Matrix::Private *private_obj, int first,
86                                                int count, QUndoCommand *parent)
87     : QUndoCommand(parent), d_private_obj(private_obj), d_first(first), d_count(count)
88 {
89     setText(QObject::tr("%1: remove %2 column(s)").arg(d_private_obj->name()).arg(d_count));
90 }
91 
~MatrixRemoveColumnsCmd()92 MatrixRemoveColumnsCmd::~MatrixRemoveColumnsCmd() { }
93 
redo()94 void MatrixRemoveColumnsCmd::redo()
95 {
96     if (d_backups.isEmpty()) {
97         int last_row = d_private_obj->rowCount() - 1;
98         for (int i = 0; i < d_count; i++)
99             d_backups.append(d_private_obj->columnCells(d_first + i, 0, last_row));
100     }
101     d_private_obj->removeColumns(d_first, d_count);
102 }
103 
undo()104 void MatrixRemoveColumnsCmd::undo()
105 {
106     d_private_obj->insertColumns(d_first, d_count);
107     int last_row = d_private_obj->rowCount() - 1;
108     for (int i = 0; i < d_count; i++)
109         d_private_obj->setColumnCells(d_first + i, 0, last_row, d_backups.at(i));
110 }
111 ///////////////////////////////////////////////////////////////////////////
112 // end of class MatrixRemoveColumnsCmd
113 ///////////////////////////////////////////////////////////////////////////
114 
115 ///////////////////////////////////////////////////////////////////////////
116 // class MatrixRemoveRowsCmd
117 ///////////////////////////////////////////////////////////////////////////
MatrixRemoveRowsCmd(future::Matrix::Private * private_obj,int first,int count,QUndoCommand * parent)118 MatrixRemoveRowsCmd::MatrixRemoveRowsCmd(future::Matrix::Private *private_obj, int first, int count,
119                                          QUndoCommand *parent)
120     : QUndoCommand(parent), d_private_obj(private_obj), d_first(first), d_count(count)
121 {
122     setText(QObject::tr("%1: remove %2 row(s)").arg(d_private_obj->name()).arg(d_count));
123 }
124 
~MatrixRemoveRowsCmd()125 MatrixRemoveRowsCmd::~MatrixRemoveRowsCmd() { }
126 
redo()127 void MatrixRemoveRowsCmd::redo()
128 {
129     if (d_backups.isEmpty()) {
130         int last_row = d_first + d_count - 1;
131         for (int col = 0; col < d_private_obj->columnCount(); col++)
132             d_backups.append(d_private_obj->columnCells(col, d_first, last_row));
133     }
134     d_private_obj->removeRows(d_first, d_count);
135 }
136 
undo()137 void MatrixRemoveRowsCmd::undo()
138 {
139     d_private_obj->insertRows(d_first, d_count);
140     int last_row = d_first + d_count - 1;
141     for (int col = 0; col < d_private_obj->columnCount(); col++)
142         d_private_obj->setColumnCells(col, d_first, last_row, d_backups.at(col));
143 }
144 ///////////////////////////////////////////////////////////////////////////
145 // end of class MatrixRemoveRowsCmd
146 ///////////////////////////////////////////////////////////////////////////
147 
148 ///////////////////////////////////////////////////////////////////////////
149 // class MatrixClearCmd
150 ///////////////////////////////////////////////////////////////////////////
MatrixClearCmd(future::Matrix::Private * private_obj,QUndoCommand * parent)151 MatrixClearCmd::MatrixClearCmd(future::Matrix::Private *private_obj, QUndoCommand *parent)
152     : QUndoCommand(parent), d_private_obj(private_obj)
153 {
154     setText(QObject::tr("%1: clear").arg(d_private_obj->name()));
155 }
156 
~MatrixClearCmd()157 MatrixClearCmd::~MatrixClearCmd() { }
158 
redo()159 void MatrixClearCmd::redo()
160 {
161     if (d_backups.isEmpty()) {
162         int last_row = d_private_obj->rowCount() - 1;
163         for (int i = 0; i < d_private_obj->columnCount(); i++)
164             d_backups.append(d_private_obj->columnCells(i, 0, last_row));
165     }
166     for (int i = 0; i < d_private_obj->columnCount(); i++)
167         d_private_obj->clearColumn(i);
168 }
169 
undo()170 void MatrixClearCmd::undo()
171 {
172     int last_row = d_private_obj->rowCount() - 1;
173     for (int i = 0; i < d_private_obj->columnCount(); i++)
174         d_private_obj->setColumnCells(i, 0, last_row, d_backups.at(i));
175 }
176 ///////////////////////////////////////////////////////////////////////////
177 // end of class MatrixClearCmd
178 ///////////////////////////////////////////////////////////////////////////
179 
180 ///////////////////////////////////////////////////////////////////////////
181 // class MatrixClearColumnCmd
182 ///////////////////////////////////////////////////////////////////////////
MatrixClearColumnCmd(future::Matrix::Private * private_obj,int col,QUndoCommand * parent)183 MatrixClearColumnCmd::MatrixClearColumnCmd(future::Matrix::Private *private_obj, int col,
184                                            QUndoCommand *parent)
185     : QUndoCommand(parent), d_private_obj(private_obj), d_col(col)
186 {
187     setText(QObject::tr("%1: clear column %2").arg(d_private_obj->name()).arg(d_col + 1));
188 }
189 
~MatrixClearColumnCmd()190 MatrixClearColumnCmd::~MatrixClearColumnCmd() { }
191 
redo()192 void MatrixClearColumnCmd::redo()
193 {
194     if (d_backup.isEmpty())
195         d_backup = d_private_obj->columnCells(d_col, 0, d_private_obj->rowCount() - 1);
196     d_private_obj->clearColumn(d_col);
197 }
198 
undo()199 void MatrixClearColumnCmd::undo()
200 {
201     d_private_obj->setColumnCells(d_col, 0, d_private_obj->rowCount() - 1, d_backup);
202 }
203 ///////////////////////////////////////////////////////////////////////////
204 // end of class MatrixClearColumnCmd
205 ///////////////////////////////////////////////////////////////////////////
206 
207 ///////////////////////////////////////////////////////////////////////////
208 // class MatrixSetCellValueCmd
209 ///////////////////////////////////////////////////////////////////////////
MatrixSetCellValueCmd(future::Matrix::Private * private_obj,int row,int col,double value,QUndoCommand * parent)210 MatrixSetCellValueCmd::MatrixSetCellValueCmd(future::Matrix::Private *private_obj, int row, int col,
211                                              double value, QUndoCommand *parent)
212     : QUndoCommand(parent), d_private_obj(private_obj), d_row(row), d_col(col), d_value(value)
213 {
214     // remark: don't use many QString::arg() calls in ctors of commands that might be called often,
215     // they use a lot of execution time
216     setText(QObject::tr("%1: set cell value").arg(d_private_obj->name()));
217 }
218 
~MatrixSetCellValueCmd()219 MatrixSetCellValueCmd::~MatrixSetCellValueCmd() { }
220 
redo()221 void MatrixSetCellValueCmd::redo()
222 {
223     d_old_value = d_private_obj->cell(d_row, d_col);
224     d_private_obj->setCell(d_row, d_col, d_value);
225 }
226 
undo()227 void MatrixSetCellValueCmd::undo()
228 {
229     d_private_obj->setCell(d_row, d_col, d_old_value);
230 }
231 ///////////////////////////////////////////////////////////////////////////
232 // end of class MatrixSetCellValueCmd
233 ///////////////////////////////////////////////////////////////////////////
234 
235 ///////////////////////////////////////////////////////////////////////////
236 // class MatrixSetCoordinatesCmd
237 ///////////////////////////////////////////////////////////////////////////
MatrixSetCoordinatesCmd(future::Matrix::Private * private_obj,double x1,double x2,double y1,double y2,QUndoCommand * parent)238 MatrixSetCoordinatesCmd::MatrixSetCoordinatesCmd(future::Matrix::Private *private_obj, double x1,
239                                                  double x2, double y1, double y2,
240                                                  QUndoCommand *parent)
241     : QUndoCommand(parent),
242       d_private_obj(private_obj),
243       d_new_x1(x1),
244       d_new_x2(x2),
245       d_new_y1(y1),
246       d_new_y2(y2)
247 {
248     setText(QObject::tr("%1: set matrix coordinates").arg(d_private_obj->name()));
249 }
250 
~MatrixSetCoordinatesCmd()251 MatrixSetCoordinatesCmd::~MatrixSetCoordinatesCmd() { }
252 
redo()253 void MatrixSetCoordinatesCmd::redo()
254 {
255     d_old_x1 = d_private_obj->xStart();
256     d_old_x2 = d_private_obj->xEnd();
257     d_old_y1 = d_private_obj->yStart();
258     d_old_y2 = d_private_obj->yEnd();
259     d_private_obj->setXStart(d_new_x1);
260     d_private_obj->setXEnd(d_new_x2);
261     d_private_obj->setYStart(d_new_y1);
262     d_private_obj->setYEnd(d_new_y2);
263 }
264 
undo()265 void MatrixSetCoordinatesCmd::undo()
266 {
267     d_private_obj->setXStart(d_old_x1);
268     d_private_obj->setXEnd(d_old_x2);
269     d_private_obj->setYStart(d_old_y1);
270     d_private_obj->setYEnd(d_old_y2);
271 }
272 
273 ///////////////////////////////////////////////////////////////////////////
274 // end of class MatrixSetCoordinatesCmd
275 ///////////////////////////////////////////////////////////////////////////
276 
277 ///////////////////////////////////////////////////////////////////////////
278 // class MatrixSetFormatCmd
279 ///////////////////////////////////////////////////////////////////////////
MatrixSetFormatCmd(future::Matrix::Private * private_obj,char new_format)280 MatrixSetFormatCmd::MatrixSetFormatCmd(future::Matrix::Private *private_obj, char new_format)
281     : d_private_obj(private_obj), d_other_format(new_format)
282 {
283     setText(QObject::tr("%1: set numeric format to '%2'")
284                     .arg(d_private_obj->name())
285                     .arg(new_format));
286 }
287 
redo()288 void MatrixSetFormatCmd::redo()
289 {
290     char tmp = d_private_obj->numericFormat();
291     d_private_obj->setNumericFormat(d_other_format);
292     d_other_format = tmp;
293 }
294 
undo()295 void MatrixSetFormatCmd::undo()
296 {
297     redo();
298 }
299 
300 ///////////////////////////////////////////////////////////////////////////
301 // end of class MatrixSetFormatCmd
302 ///////////////////////////////////////////////////////////////////////////
303 
304 ///////////////////////////////////////////////////////////////////////////
305 // class MatrixSetDigitsCmd
306 ///////////////////////////////////////////////////////////////////////////
MatrixSetDigitsCmd(future::Matrix::Private * private_obj,int new_digits)307 MatrixSetDigitsCmd::MatrixSetDigitsCmd(future::Matrix::Private *private_obj, int new_digits)
308     : d_private_obj(private_obj), d_other_digits(new_digits)
309 {
310     setText(QObject::tr("%1: set decimal digits to %2").arg(d_private_obj->name()).arg(new_digits));
311 }
312 
redo()313 void MatrixSetDigitsCmd::redo()
314 {
315     int tmp = d_private_obj->displayedDigits();
316     d_private_obj->setDisplayedDigits(d_other_digits);
317     d_other_digits = tmp;
318 }
319 
undo()320 void MatrixSetDigitsCmd::undo()
321 {
322     redo();
323 }
324 
325 ///////////////////////////////////////////////////////////////////////////
326 // end of class MatrixSetDigitsCmd
327 ///////////////////////////////////////////////////////////////////////////
328 
329 ///////////////////////////////////////////////////////////////////////////
330 // class MatrixSetFormulaCmd
331 ///////////////////////////////////////////////////////////////////////////
MatrixSetFormulaCmd(future::Matrix::Private * private_obj,QString formula)332 MatrixSetFormulaCmd::MatrixSetFormulaCmd(future::Matrix::Private *private_obj, QString formula)
333     : d_private_obj(private_obj), d_other_formula(formula)
334 {
335     setText(QObject::tr("%1: set formula").arg(d_private_obj->name()));
336 }
337 
redo()338 void MatrixSetFormulaCmd::redo()
339 {
340     QString tmp = d_private_obj->formula();
341     d_private_obj->setFormula(d_other_formula);
342     d_other_formula = tmp;
343 }
344 
undo()345 void MatrixSetFormulaCmd::undo()
346 {
347     redo();
348 }
349 
350 ///////////////////////////////////////////////////////////////////////////
351 // end of class MatrixSetFormulaCmd
352 ///////////////////////////////////////////////////////////////////////////
353 
354 ///////////////////////////////////////////////////////////////////////////
355 // class MatrixSetColumnCellsCmd
356 ///////////////////////////////////////////////////////////////////////////
MatrixSetColumnCellsCmd(future::Matrix::Private * private_obj,int col,int first_row,int last_row,const QVector<qreal> & values,QUndoCommand * parent)357 MatrixSetColumnCellsCmd::MatrixSetColumnCellsCmd(future::Matrix::Private *private_obj, int col,
358                                                  int first_row, int last_row,
359                                                  const QVector<qreal> &values, QUndoCommand *parent)
360     : QUndoCommand(parent),
361       d_private_obj(private_obj),
362       d_col(col),
363       d_first_row(first_row),
364       d_last_row(last_row),
365       d_values(values)
366 {
367     setText(QObject::tr("%1: set cell values").arg(d_private_obj->name()));
368 }
369 
~MatrixSetColumnCellsCmd()370 MatrixSetColumnCellsCmd::~MatrixSetColumnCellsCmd() { }
371 
redo()372 void MatrixSetColumnCellsCmd::redo()
373 {
374     if (d_old_values.isEmpty())
375         d_old_values = d_private_obj->columnCells(d_col, d_first_row, d_last_row);
376     d_private_obj->setColumnCells(d_col, d_first_row, d_last_row, d_values);
377 }
378 
undo()379 void MatrixSetColumnCellsCmd::undo()
380 {
381     d_private_obj->setColumnCells(d_col, d_first_row, d_last_row, d_old_values);
382 }
383 ///////////////////////////////////////////////////////////////////////////
384 // end of class MatrixSetColumnCellsCmd
385 ///////////////////////////////////////////////////////////////////////////
386 
387 ///////////////////////////////////////////////////////////////////////////
388 // class MatrixSetRowCellsCmd
389 ///////////////////////////////////////////////////////////////////////////
MatrixSetRowCellsCmd(future::Matrix::Private * private_obj,int row,int first_column,int last_column,const QVector<qreal> & values,QUndoCommand * parent)390 MatrixSetRowCellsCmd::MatrixSetRowCellsCmd(future::Matrix::Private *private_obj, int row,
391                                            int first_column, int last_column,
392                                            const QVector<qreal> &values, QUndoCommand *parent)
393     : QUndoCommand(parent),
394       d_private_obj(private_obj),
395       d_row(row),
396       d_first_column(first_column),
397       d_last_column(last_column),
398       d_values(values)
399 {
400     setText(QObject::tr("%1: set cell values").arg(d_private_obj->name()));
401 }
402 
~MatrixSetRowCellsCmd()403 MatrixSetRowCellsCmd::~MatrixSetRowCellsCmd() { }
404 
redo()405 void MatrixSetRowCellsCmd::redo()
406 {
407     if (d_old_values.isEmpty())
408         d_old_values = d_private_obj->rowCells(d_row, d_first_column, d_last_column);
409     d_private_obj->setRowCells(d_row, d_first_column, d_last_column, d_values);
410 }
411 
undo()412 void MatrixSetRowCellsCmd::undo()
413 {
414     d_private_obj->setRowCells(d_row, d_first_column, d_last_column, d_old_values);
415 }
416 ///////////////////////////////////////////////////////////////////////////
417 // end of class MatrixSetRowCellsCmd
418 ///////////////////////////////////////////////////////////////////////////
419 
420 ///////////////////////////////////////////////////////////////////////////
421 // class MatrixTransposeCmd
422 ///////////////////////////////////////////////////////////////////////////
MatrixTransposeCmd(future::Matrix::Private * private_obj,QUndoCommand * parent)423 MatrixTransposeCmd::MatrixTransposeCmd(future::Matrix::Private *private_obj, QUndoCommand *parent)
424     : QUndoCommand(parent), d_private_obj(private_obj)
425 {
426     setText(QObject::tr("%1: transpose").arg(d_private_obj->name()));
427 }
428 
~MatrixTransposeCmd()429 MatrixTransposeCmd::~MatrixTransposeCmd() { }
430 
redo()431 void MatrixTransposeCmd::redo()
432 {
433     int rows = d_private_obj->rowCount();
434     int cols = d_private_obj->columnCount();
435     int temp_size = qMax(rows, cols);
436     d_private_obj->blockChangeSignals(true);
437     if (cols < rows)
438         d_private_obj->insertColumns(cols, temp_size - cols);
439     else if (cols > rows)
440         d_private_obj->insertRows(rows, temp_size - rows);
441     for (int i = 1; i < temp_size; i++) {
442         QVector<qreal> row = d_private_obj->rowCells(i, 0, i - 1);
443         QVector<qreal> col = d_private_obj->columnCells(i, 0, i - 1);
444         d_private_obj->setRowCells(i, 0, i - 1, col);
445         d_private_obj->setColumnCells(i, 0, i - 1, row);
446     }
447     if (cols < rows)
448         d_private_obj->removeRows(cols, temp_size - cols);
449     else if (cols > rows)
450         d_private_obj->removeColumns(rows, temp_size - rows);
451     d_private_obj->blockChangeSignals(false);
452     d_private_obj->emitDataChanged(0, 0, d_private_obj->rowCount() - 1,
453                                    d_private_obj->columnCount() - 1);
454 }
455 
undo()456 void MatrixTransposeCmd::undo()
457 {
458     redo();
459 }
460 ///////////////////////////////////////////////////////////////////////////
461 // end of class MatrixTransposeCmd
462 ///////////////////////////////////////////////////////////////////////////
463 
464 ///////////////////////////////////////////////////////////////////////////
465 // class MatrixMirrorHorizontallyCmd
466 ///////////////////////////////////////////////////////////////////////////
MatrixMirrorHorizontallyCmd(future::Matrix::Private * private_obj,QUndoCommand * parent)467 MatrixMirrorHorizontallyCmd::MatrixMirrorHorizontallyCmd(future::Matrix::Private *private_obj,
468                                                          QUndoCommand *parent)
469     : QUndoCommand(parent), d_private_obj(private_obj)
470 {
471     setText(QObject::tr("%1: mirror horizontally").arg(d_private_obj->name()));
472 }
473 
~MatrixMirrorHorizontallyCmd()474 MatrixMirrorHorizontallyCmd::~MatrixMirrorHorizontallyCmd() { }
475 
redo()476 void MatrixMirrorHorizontallyCmd::redo()
477 {
478     int rows = d_private_obj->rowCount();
479     int cols = d_private_obj->columnCount();
480     int middle = cols / 2;
481     d_private_obj->blockChangeSignals(true);
482     for (int i = 0; i < middle; i++) {
483         QVector<qreal> temp = d_private_obj->columnCells(i, 0, rows - 1);
484         d_private_obj->setColumnCells(i, 0, rows - 1,
485                                       d_private_obj->columnCells(cols - i - 1, 0, rows - 1));
486         d_private_obj->setColumnCells(cols - i - 1, 0, rows - 1, temp);
487     }
488     d_private_obj->blockChangeSignals(false);
489     d_private_obj->emitDataChanged(0, 0, rows - 1, cols - 1);
490 }
491 
undo()492 void MatrixMirrorHorizontallyCmd::undo()
493 {
494     redo();
495 }
496 ///////////////////////////////////////////////////////////////////////////
497 // end of class MatrixMirrorHorizontallyCmd
498 ///////////////////////////////////////////////////////////////////////////
499 
500 ///////////////////////////////////////////////////////////////////////////
501 // class MatrixMirrorVerticallyCmd
502 ///////////////////////////////////////////////////////////////////////////
MatrixMirrorVerticallyCmd(future::Matrix::Private * private_obj,QUndoCommand * parent)503 MatrixMirrorVerticallyCmd::MatrixMirrorVerticallyCmd(future::Matrix::Private *private_obj,
504                                                      QUndoCommand *parent)
505     : QUndoCommand(parent), d_private_obj(private_obj)
506 {
507     setText(QObject::tr("%1: mirror vertically").arg(d_private_obj->name()));
508 }
509 
~MatrixMirrorVerticallyCmd()510 MatrixMirrorVerticallyCmd::~MatrixMirrorVerticallyCmd() { }
511 
redo()512 void MatrixMirrorVerticallyCmd::redo()
513 {
514     int rows = d_private_obj->rowCount();
515     int cols = d_private_obj->columnCount();
516     int middle = rows / 2;
517     d_private_obj->blockChangeSignals(true);
518     for (int i = 0; i < middle; i++) {
519         QVector<qreal> temp = d_private_obj->rowCells(i, 0, cols - 1);
520         d_private_obj->setRowCells(i, 0, cols - 1,
521                                    d_private_obj->rowCells(rows - i - 1, 0, cols - 1));
522         d_private_obj->setRowCells(rows - i - 1, 0, cols - 1, temp);
523     }
524     d_private_obj->blockChangeSignals(false);
525     d_private_obj->emitDataChanged(0, 0, rows - 1, cols - 1);
526 }
527 
undo()528 void MatrixMirrorVerticallyCmd::undo()
529 {
530     redo();
531 }
532 ///////////////////////////////////////////////////////////////////////////
533 // end of class MatrixMirrorVerticallyCmd
534 ///////////////////////////////////////////////////////////////////////////
535 
MatrixSetCellsCmd(future::Matrix::Private * private_obj,int first_row,int last_row,int first_column,int last_column,const std::vector<std::vector<std::pair<double,bool>>> & values,QUndoCommand * parent)536 MatrixSetCellsCmd::MatrixSetCellsCmd(
537         future::Matrix::Private *private_obj, int first_row, int last_row, int first_column,
538         int last_column, const std::vector<std::vector<std::pair<double, bool>>> &values,
539         QUndoCommand *parent)
540     : QUndoCommand(parent), d_private_obj{ private_obj },
541       d_first_row{ first_row },
542       d_last_row{ last_row },
543       d_first_column{ first_column },
544       d_last_column{ last_column },
545       d_values{ values },
546       d_old_values{ private_obj->getCells(first_row, last_row, first_column, last_column) }
547 {
548     setText(QObject::tr("%1: set values for multiple cells").arg(d_private_obj->name()));
549 }
550 
redo()551 void MatrixSetCellsCmd::redo()
552 {
553     d_private_obj->setCells(d_first_row, d_first_column, d_values);
554 }
555 
undo()556 void MatrixSetCellsCmd::undo()
557 {
558     d_private_obj->setCells(d_first_row, d_first_column, d_old_values);
559 }
560