1 /***************************************************************************
2     File                 : MatrixCommand.cpp
3     Project              : QtiPlot
4     --------------------------------------------------------------------
5     Copyright            : (C) 2008 by Ion Vasilief,
6     Email (use @ for *)  : ion_vasilief*yahoo.fr
7     Description          : Matrix undo/redo commands
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 "MatrixCommand.h"
31 #include <QApplication>
32 #include <gsl/gsl_math.h>
33 
34 /*************************************************************************/
35 /*           Class MatrixEditCellCommand                                 */
36 /*************************************************************************/
MatrixEditCellCommand(MatrixModel * model,const QModelIndex & index,double valBefore,double valAfter,const QString & text)37 MatrixEditCellCommand::MatrixEditCellCommand(MatrixModel *model, const QModelIndex & index,
38                         double valBefore, double valAfter, const QString & text):
39 QUndoCommand(text),
40 d_model(model),
41 d_index(index),
42 d_val_before(valBefore),
43 d_val_after(valAfter)
44 {
45     setText(model->matrix()->objectName() + ": " + text);
46 }
47 
redo()48 void MatrixEditCellCommand::redo()
49 {
50     if (!d_model)
51         return;
52 
53     Matrix *m = d_model->matrix();
54     if (m){
55         d_model->setCell(d_index.row(), d_index.column(), d_val_after);
56         m->resetView();
57         m->notifyChanges();
58     }
59 }
60 
undo()61 void MatrixEditCellCommand::undo()
62 {
63     if (!d_model)
64         return;
65 
66     Matrix *m = d_model->matrix();
67     if (m){
68         d_model->setCell(d_index.row(), d_index.column(), d_val_before);
69         m->resetView();
70         m->notifyChanges();
71     }
72 }
73 
74 /*************************************************************************/
75 /*           Class MatrixSetFormulaCommand                               */
76 /*************************************************************************/
MatrixSetFormulaCommand(Matrix * m,const QString & oldFormula,const QString & newFormula,const QString & text)77 MatrixSetFormulaCommand::MatrixSetFormulaCommand(Matrix *m, const QString& oldFormula,
78                         const QString& newFormula, const QString & text):
79 QUndoCommand(text),
80 d_matrix(m),
81 d_old_formula(oldFormula),
82 d_new_formula(newFormula)
83 {
84     setText(m->objectName() + ": " + text);
85 }
86 
redo()87 void MatrixSetFormulaCommand::redo()
88 {
89     if (!d_matrix)
90         return;
91 
92     d_matrix->setFormula(d_new_formula);
93 }
94 
undo()95 void MatrixSetFormulaCommand::undo()
96 {
97     if (!d_matrix)
98         return;
99 
100     d_matrix->setFormula(d_old_formula);
101 }
102 
103 /*************************************************************************/
104 /*           Class MatrixSetViewCommand                                  */
105 /*************************************************************************/
MatrixSetViewCommand(Matrix * m,Matrix::ViewType oldView,Matrix::ViewType newView,const QString & text)106 MatrixSetViewCommand::MatrixSetViewCommand(Matrix *m, Matrix::ViewType oldView,
107                     Matrix::ViewType newView, const QString & text):
108 QUndoCommand(text),
109 d_matrix(m),
110 d_old_view(oldView),
111 d_new_view(newView)
112 {
113     setText(m->objectName() + ": " + text);
114 }
115 
redo()116 void MatrixSetViewCommand::redo()
117 {
118     if (!d_matrix)
119         return;
120 
121     d_matrix->setViewType(d_new_view);
122 }
123 
undo()124 void MatrixSetViewCommand::undo()
125 {
126     if (!d_matrix)
127         return;
128 
129     d_matrix->setViewType(d_old_view);
130 }
131 
132 /*************************************************************************/
133 /*           Class MatrixSetHeaderViewCommand                            */
134 /*************************************************************************/
MatrixSetHeaderViewCommand(Matrix * m,Matrix::HeaderViewType oldView,Matrix::HeaderViewType newView,const QString & text)135 MatrixSetHeaderViewCommand::MatrixSetHeaderViewCommand(Matrix *m, Matrix::HeaderViewType oldView,
136                     Matrix::HeaderViewType newView, const QString & text):
137 QUndoCommand(text),
138 d_matrix(m),
139 d_old_view(oldView),
140 d_new_view(newView)
141 {
142     setText(m->objectName() + ": " + text);
143 }
144 
redo()145 void MatrixSetHeaderViewCommand::redo()
146 {
147     if (!d_matrix)
148         return;
149 
150     d_matrix->setHeaderViewType(d_new_view);
151 }
152 
undo()153 void MatrixSetHeaderViewCommand::undo()
154 {
155     if (!d_matrix)
156         return;
157 
158     d_matrix->setHeaderViewType(d_old_view);
159 }
160 
161 /*************************************************************************/
162 /*           Class MatrixSetColWidthCommand                              */
163 /*************************************************************************/
MatrixSetColWidthCommand(Matrix * m,int oldWidth,int newWidth,const QString & text)164 MatrixSetColWidthCommand::MatrixSetColWidthCommand(Matrix *m, int oldWidth, int newWidth, const QString & text):
165 QUndoCommand(text),
166 d_matrix(m),
167 d_old_width(oldWidth),
168 d_new_width(newWidth)
169 {
170     setText(m->objectName() + ": " + text);
171 }
172 
redo()173 void MatrixSetColWidthCommand::redo()
174 {
175     if (!d_matrix)
176         return;
177 
178     d_matrix->setColumnsWidth(d_new_width);
179 }
180 
undo()181 void MatrixSetColWidthCommand::undo()
182 {
183     if (!d_matrix)
184         return;
185 
186     d_matrix->setColumnsWidth(d_old_width);
187 }
188 
189 /*************************************************************************/
190 /*           Class MatrixSetPrecisionCommand                             */
191 /*************************************************************************/
MatrixSetPrecisionCommand(Matrix * m,const QChar & oldFormat,const QChar & newFormat,int oldPrec,int newPrec,const QString & text)192 MatrixSetPrecisionCommand::MatrixSetPrecisionCommand(Matrix *m, const QChar& oldFormat,
193                 const QChar& newFormat, int oldPrec, int newPrec, const QString & text):
194 QUndoCommand(text),
195 d_matrix(m),
196 d_old_format(oldFormat),
197 d_new_format(newFormat),
198 d_old_prec(oldPrec),
199 d_new_prec(newPrec)
200 {
201     setText(m->objectName() + ": " + text);
202 }
203 
redo()204 void MatrixSetPrecisionCommand::redo()
205 {
206     if (!d_matrix)
207         return;
208 
209     d_matrix->setNumericFormat(d_new_format, d_new_prec);
210 }
211 
undo()212 void MatrixSetPrecisionCommand::undo()
213 {
214     if (!d_matrix)
215         return;
216 
217     d_matrix->setNumericFormat(d_old_format, d_old_prec);
218 }
219 
220 /*************************************************************************/
221 /*           Class MatrixSetCoordinatesCommand                           */
222 /*************************************************************************/
MatrixSetCoordinatesCommand(Matrix * m,double oxs,double oxe,double oys,double oye,double nxs,double nxe,double nys,double nye,const QString & text)223 MatrixSetCoordinatesCommand::MatrixSetCoordinatesCommand(Matrix *m, double oxs, double oxe,
224     double oys, double oye, double nxs, double nxe, double nys, double nye, const QString & text):
225 QUndoCommand(text),
226 d_matrix(m),
227 d_old_xs(oxs),
228 d_old_xe(oxe),
229 d_old_ys(oys),
230 d_old_ye(oye),
231 d_new_xs(nxs),
232 d_new_xe(nxe),
233 d_new_ys(nys),
234 d_new_ye(nye)
235 {
236     setText(m->objectName() + ": " + text);
237 }
238 
redo()239 void MatrixSetCoordinatesCommand::redo()
240 {
241     if (!d_matrix)
242         return;
243 
244     d_matrix->setCoordinates(d_new_xs, d_new_xe, d_new_ys, d_new_ye);
245 }
246 
undo()247 void MatrixSetCoordinatesCommand::undo()
248 {
249     if (!d_matrix)
250         return;
251 
252     d_matrix->setCoordinates(d_old_xs, d_old_xe, d_old_ys, d_old_ye);
253 }
254 
255 /*************************************************************************/
256 /*           Class MatrixSetColorMapCommand                              */
257 /*************************************************************************/
MatrixSetColorMapCommand(Matrix * m,Matrix::ColorMapType type_before,const LinearColorMap & map_before,Matrix::ColorMapType type_after,const LinearColorMap & map_after,const QString & text)258 MatrixSetColorMapCommand::MatrixSetColorMapCommand(Matrix *m, Matrix::ColorMapType type_before,
259 							const LinearColorMap& map_before, Matrix::ColorMapType type_after,
260 							const LinearColorMap& map_after, const QString & text):
261 QUndoCommand(text),
262 d_matrix(m),
263 d_map_type_before(type_before),
264 d_map_type_after(type_after)
265 {
266     setText(m->objectName() + ": " + text);
267 
268 	d_map_before = LinearColorMap(map_before);
269 	d_map_after = LinearColorMap(map_after);
270 }
271 
redo()272 void MatrixSetColorMapCommand::redo()
273 {
274     if (!d_matrix)
275         return;
276 
277 	switch(d_map_type_after){
278         case Matrix::Default:
279 			d_matrix->setDefaultColorMap();
280 		break;
281 
282 		case Matrix::GrayScale:
283 			d_matrix->setGrayScale();
284 		break;
285 
286 		case Matrix::Rainbow:
287 			d_matrix->setRainbowColorMap();
288 		break;
289 
290 		case Matrix::Custom:
291 			d_matrix->setColorMap(d_map_after);
292 		break;
293 	}
294 }
295 
undo()296 void MatrixSetColorMapCommand::undo()
297 {
298     if (!d_matrix)
299         return;
300 
301     switch(d_map_type_before){
302         case Matrix::Default:
303 			d_matrix->setDefaultColorMap();
304 		break;
305 
306 		case Matrix::GrayScale:
307 			d_matrix->setGrayScale();
308 		break;
309 
310 		case Matrix::Rainbow:
311 			d_matrix->setRainbowColorMap();
312 		break;
313 
314 		case Matrix::Custom:
315 			d_matrix->setColorMap(d_map_before);
316 		break;
317 	}
318 }
319 
320 /*************************************************************************/
321 /*           Class MatrixDeleteRowsCommand                               */
322 /*************************************************************************/
MatrixDeleteRowsCommand(MatrixModel * model,int startRow,int count,double * data,const QString & text)323 MatrixDeleteRowsCommand::MatrixDeleteRowsCommand(MatrixModel *model, int startRow, int count, double* data, const QString& text):
324 QUndoCommand(text),
325 d_model(model),
326 d_start_row(startRow),
327 d_count(count),
328 d_data(data)
329 {
330     setText(model->matrix()->objectName() + ": " + text);
331 }
332 
redo()333 void MatrixDeleteRowsCommand::redo()
334 {
335     if (!d_model)
336         return;
337 
338 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
339     d_model->removeRows(d_start_row, d_count);
340 	QApplication::restoreOverrideCursor();
341 }
342 
undo()343 void MatrixDeleteRowsCommand::undo()
344 {
345     if (!d_model)
346         return;
347 
348 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
349 
350     d_model->insertRows(d_start_row, d_count);
351 	double *data = d_model->dataVector();
352 	int cols = d_model->columnCount();
353 	int size = cols * d_count;
354 	int cell = d_start_row*cols;
355 	for (int i = 0; i<size; i++)
356 		data[cell++] = d_data[i];
357 
358 	QApplication::restoreOverrideCursor();
359 }
360 
361 /*************************************************************************/
362 /*           Class MatrixInsertRowCommand                                */
363 /*************************************************************************/
MatrixInsertRowCommand(MatrixModel * model,int startRow,const QString & text)364 MatrixInsertRowCommand::MatrixInsertRowCommand(MatrixModel *model, int startRow, const QString& text):
365 QUndoCommand(text),
366 d_model(model),
367 d_start_row(startRow)
368 {
369     setText(model->matrix()->objectName() + ": " + text);
370 }
371 
redo()372 void MatrixInsertRowCommand::redo()
373 {
374     if (!d_model)
375         return;
376 
377 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
378     d_model->insertRows(d_start_row, 1);
379 	QApplication::restoreOverrideCursor();
380 }
381 
undo()382 void MatrixInsertRowCommand::undo()
383 {
384     if (!d_model)
385         return;
386 
387 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
388     d_model->removeRows(d_start_row, 1);
389 	QApplication::restoreOverrideCursor();
390 }
391 
392 /*************************************************************************/
393 /*           Class MatrixDeleteColsCommand                               */
394 /*************************************************************************/
MatrixDeleteColsCommand(MatrixModel * model,int startCol,int count,double * data,const QString & text)395 MatrixDeleteColsCommand::MatrixDeleteColsCommand(MatrixModel *model, int startCol, int count, double* data, const QString& text):
396 QUndoCommand(text),
397 d_model(model),
398 d_start_col(startCol),
399 d_count(count),
400 d_data(data)
401 {
402     setText(model->matrix()->objectName() + ": " + text);
403 }
404 
redo()405 void MatrixDeleteColsCommand::redo()
406 {
407     if (!d_model)
408         return;
409 
410 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
411     d_model->removeColumns(d_start_col, d_count);
412 	QApplication::restoreOverrideCursor();
413 }
414 
undo()415 void MatrixDeleteColsCommand::undo()
416 {
417     if (!d_model)
418         return;
419 
420 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
421 
422     d_model->insertColumns(d_start_col, d_count);
423 	double *data = d_model->dataVector();
424 	int rows = d_model->rowCount();
425 	int cols = d_model->columnCount();
426 	for (int i = 0; i<rows; i++){
427 		int aux = i*cols + d_start_col;
428 		int aux2 = i*d_count;
429 		for (int j = 0; j<d_count; j++)
430 			data[aux++] = d_data[aux2++];
431 	}
432 	QApplication::restoreOverrideCursor();
433 }
434 
435 /*************************************************************************/
436 /*           Class MatrixInsertColCommand                                */
437 /*************************************************************************/
MatrixInsertColCommand(MatrixModel * model,int startCol,const QString & text)438 MatrixInsertColCommand::MatrixInsertColCommand(MatrixModel *model, int startCol, const QString& text):
439 QUndoCommand(text),
440 d_model(model),
441 d_start_col(startCol)
442 {
443     setText(model->matrix()->objectName() + ": " + text);
444 }
445 
redo()446 void MatrixInsertColCommand::redo()
447 {
448     if (!d_model)
449         return;
450 
451 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
452     d_model->insertColumns(d_start_col, 1);
453 	QApplication::restoreOverrideCursor();
454 }
455 
undo()456 void MatrixInsertColCommand::undo()
457 {
458     if (!d_model)
459         return;
460 
461 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
462     d_model->removeColumns(d_start_col, 1);
463 	QApplication::restoreOverrideCursor();
464 }
465 
466 /*************************************************************************/
467 /*           Class MatrixSetSizeCommand                                */
468 /*************************************************************************/
MatrixSetSizeCommand(MatrixModel * model,const QSize & oldSize,const QSize & newSize,double * data,const QString & text)469 MatrixSetSizeCommand::MatrixSetSizeCommand(MatrixModel *model, const QSize& oldSize, const QSize& newSize, double *data, const QString& text):
470 QUndoCommand(text),
471 d_model(model),
472 d_old_size(oldSize),
473 d_new_size(newSize),
474 d_backup(data)
475 {
476     setText(model->matrix()->objectName() + ": " + text);
477 }
478 
redo()479 void MatrixSetSizeCommand::redo()
480 {
481     if (!d_model)
482         return;
483 
484     d_model->setDimensions(d_new_size.width(), d_new_size.height());
485 	d_model->matrix()->resetView();
486 }
487 
undo()488 void MatrixSetSizeCommand::undo()
489 {
490     if (!d_model)
491         return;
492 
493 	int rows = d_old_size.width();
494 	int cols = d_old_size.height();
495     d_model->setDimensions(rows, cols);
496 
497 	double *data = d_model->dataVector();
498     if (!data)
499         return;
500 
501 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
502 	int cell = 0;
503     for (int i = 0; i < rows; i++){
504         for (int j = 0; j < cols; j++){
505             data[cell] = d_backup[cell];
506 			cell++;
507 		}
508     }
509 	d_model->matrix()->resetView();
510 	QApplication::restoreOverrideCursor();
511 }
512 
513 /*************************************************************************/
514 /*           Class MatrixSmoothCommand                                */
515 /*************************************************************************/
MatrixSmoothCommand(MatrixModel * model,double * data,const QString & text)516 MatrixSmoothCommand::MatrixSmoothCommand(MatrixModel *model, double *data, const QString& text):
517 QUndoCommand(text),
518 d_model(model),
519 d_backup(data)
520 {
521 	setText(model->matrix()->objectName() + ": " + text);
522 }
523 
redo()524 void MatrixSmoothCommand::redo()
525 {
526 	if (!d_model)
527 		return;
528 
529 	int r = d_model->rowCount();
530 	int c = d_model->columnCount();
531 
532 	if (r < 32 || c < 32){
533 		if(!d_model->canResize(2*r, 2*c))
534 			return;
535 
536 		for (int i = 0; i < 2; i++){
537 			d_model->resample(2*r, 2*c);
538 			d_model->resample(r, c);
539 		}
540 	} else {
541 		d_model->resample(r/2, c/2);
542 		d_model->resample(r, c);
543 	}
544 }
545 
undo()546 void MatrixSmoothCommand::undo()
547 {
548 	if (!d_model)
549 		return;
550 
551 	double *data = d_model->dataVector();
552 	if (!data)
553 		return;
554 
555 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
556 	int cell = 0;
557 	for (int i = 0; i < d_model->rowCount(); i++){
558 		for (int j = 0; j < d_model->columnCount(); j++){
559 			data[cell] = d_backup[cell];
560 			cell++;
561 		}
562 	}
563 	d_model->matrix()->resetView();
564 	QApplication::restoreOverrideCursor();
565 }
566 
567 /*************************************************************************/
568 /*           Class MatrixResampleCommand                                 */
569 /*************************************************************************/
MatrixResampleCommand(MatrixModel * model,const QSize & oldSize,const QSize & newSize,int method,double * data,const QString & text)570 MatrixResampleCommand::MatrixResampleCommand(MatrixModel *model, const QSize& oldSize, const QSize& newSize, int method, double *data, const QString& text):
571 MatrixSetSizeCommand(model, oldSize, newSize, data, text),
572 d_method(method)
573 {
574 	setText(model->matrix()->objectName() + ": " + text);
575 }
576 
redo()577 void MatrixResampleCommand::redo()
578 {
579 #ifdef HAVE_ALGLIB
580 	if (!d_model)
581 		return;
582 
583 	d_model->resample(d_new_size.width(), d_new_size.height(), d_method);
584 #endif
585 }
586 
587 /*************************************************************************/
588 /*           Class MatrixUndoCommand                                     */
589 /*************************************************************************/
MatrixUndoCommand(MatrixModel * model,Matrix::Operation op,int startRow,int endRow,int startCol,int endCol,double * data,const QString & text)590 MatrixUndoCommand::MatrixUndoCommand(MatrixModel *model, Matrix::Operation op, int startRow, int endRow, int startCol, int endCol,
591 									double *data, const QString& text):
592 QUndoCommand(text),
593 d_model(model),
594 d_operation(op),
595 d_start_row(startRow),
596 d_end_row(endRow),
597 d_start_col(startCol),
598 d_end_col(endCol),
599 d_data(data)
600 {
601     setText(model->matrix()->objectName() + ": " + text);
602 }
603 
redo()604 void MatrixUndoCommand::redo()
605 {
606     if (!d_model)
607         return;
608 
609 	switch(d_operation){
610 		case Matrix::Clear:
611 			d_model->clear(d_start_row, d_end_row, d_start_col, d_end_col);
612 		break;
613 		case Matrix::Calculate:
614 			d_model->calculate(d_start_row, d_end_row, d_start_col, d_end_col);
615 		break;
616 		case Matrix::MuParserCalculate:
617 			d_model->muParserCalculate(d_start_row, d_end_row, d_start_col, d_end_col);
618 		break;
619 		default:
620 		break;
621 	}
622     d_model->matrix()->resetView();
623 }
624 
undo()625 void MatrixUndoCommand::undo()
626 {
627     if (!d_model)
628         return;
629 
630     double *data = d_model->dataVector();
631     if (!data)
632         return;
633 
634 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
635 
636     int cols = d_model->columnCount();
637 	int aux = 0;
638     for (int i = d_start_row; i <= d_end_row; i++){
639         int row = i*cols + d_start_col;
640         for (int j = d_start_col; j <= d_end_col; j++)
641             data[row++] = d_data[aux++];
642     }
643     d_model->matrix()->resetView();
644 	QApplication::restoreOverrideCursor();
645 }
646 
647 /*************************************************************************/
648 /*           Class MatrixFftCommand                                      */
649 /*************************************************************************/
MatrixFftCommand(bool inverse,MatrixModel * model,int startRow,int endRow,int startCol,int endCol,double * data,const QString & text)650 MatrixFftCommand::MatrixFftCommand(bool inverse, MatrixModel *model, int startRow, int endRow,
651 									int startCol, int endCol, double *data, const QString& text):
652 MatrixUndoCommand(model, Matrix::FFT, startRow, endRow, startCol, endCol, data, text),
653 d_inverse(inverse)
654 {
655 }
656 
redo()657 void MatrixFftCommand::redo()
658 {
659     if (!d_model)
660         return;
661 
662     d_model->fft(d_inverse);
663 }
664 
665 /*************************************************************************/
666 /*           Class MatrixSetImageCommand                           */
667 /*************************************************************************/
MatrixSetImageCommand(MatrixModel * model,const QImage & image,Matrix::ViewType oldView,int startRow,int endRow,int startCol,int endCol,double * data,const QString & text)668 MatrixSetImageCommand::MatrixSetImageCommand(MatrixModel *model, const QImage& image, Matrix::ViewType oldView,
669 						int startRow, int endRow, int startCol, int endCol, double *data, const QString& text):
670 MatrixUndoCommand(model, Matrix::SetImage, startRow, endRow, startCol, endCol, data, text),
671 d_image(image),
672 d_old_view(oldView)
673 {
674 }
675 
undo()676 void MatrixSetImageCommand::undo()
677 {
678     if (!d_model)
679         return;
680 
681     d_model->setDimensions(d_end_row - d_start_row + 1, d_end_col - d_start_col + 1);
682 	d_model->matrix()->setViewType(d_old_view);
683 	MatrixUndoCommand::undo();
684 }
685 
redo()686 void MatrixSetImageCommand::redo()
687 {
688     if (!d_model)
689         return;
690 
691     d_model->setImage(d_image);
692     Matrix *m = d_model->matrix();
693 	m->setViewType(Matrix::ImageView, false);
694 	m->displayImage(d_image);
695 }
696 
697 /*************************************************************************/
698 /*           Class MatrixImportAsciiCommand                              */
699 /*************************************************************************/
MatrixImportAsciiCommand(const QString & fname,const QString & sep,int ignoredLines,bool stripSpaces,bool simplifySpaces,const QString & commentString,Matrix::ImportMode importAs,const QLocale & locale,int endLineChar,int maxRows,MatrixModel * model,int startRow,int endRow,int startCol,int endCol,double * data,const QString & text)700 MatrixImportAsciiCommand::MatrixImportAsciiCommand(const QString &fname, const QString &sep,
701 						int ignoredLines, bool stripSpaces, bool simplifySpaces,
702 						const QString& commentString, Matrix::ImportMode importAs, const QLocale& locale,
703 						int endLineChar, int maxRows, MatrixModel *model, int startRow, int endRow,
704 						int startCol, int endCol, double *data, const QString& text):
705 MatrixUndoCommand(model, Matrix::ImportAscii, startRow, endRow, startCol, endCol, data, text),
706 d_path(fname),
707 d_sep(sep),
708 d_comment(commentString),
709 d_ignore_lines(ignoredLines),
710 d_end_line(endLineChar),
711 d_max_rows(maxRows),
712 d_strip_spaces(stripSpaces),
713 d_simplify_spaces(simplifySpaces),
714 d_mode(importAs),
715 d_locale(locale)
716 {
717 }
718 
redo()719 void MatrixImportAsciiCommand::redo()
720 {
721     if (!d_model)
722         return;
723 
724 	d_model->importASCII(d_path, d_sep, d_ignore_lines, d_strip_spaces, d_simplify_spaces,
725 						d_comment, d_mode, d_locale, d_end_line, d_max_rows);
726 }
727 
728 /*************************************************************************/
729 /*           Class MatrixSymmetryOperation                                */
730 /*************************************************************************/
MatrixSymmetryOperation(MatrixModel * model,Matrix::Operation op,const QString & text)731 MatrixSymmetryOperation::MatrixSymmetryOperation(MatrixModel *model, Matrix::Operation op, const QString& text):
732 QUndoCommand(text),
733 d_model(model),
734 d_operation(op)
735 {
736     setText(model->matrix()->objectName() + ": " + text);
737 }
738 
redo()739 void MatrixSymmetryOperation::redo()
740 {
741     if (!d_model)
742         return;
743 
744     switch(d_operation){
745 		case Matrix::Transpose:
746 			d_model->transpose();
747 		break;
748 		case Matrix::Invert:
749 			d_model->invert();
750 		break;
751 		case Matrix::FlipVertically:
752 			d_model->flipVertically();
753 		break;
754 		case Matrix::FlipHorizontally:
755 			d_model->flipHorizontally();
756 		break;
757 		case Matrix::RotateClockwise:
758 			d_model->rotate90(true);
759 		break;
760 		case Matrix::RotateCounterClockwise:
761 			d_model->rotate90(false);
762 		break;
763 		default:
764 		break;
765 	}
766 	d_model->matrix()->resetView();
767 }
768 
undo()769 void MatrixSymmetryOperation::undo()
770 {
771     if (!d_model)
772         return;
773 
774     switch(d_operation){
775 		case Matrix::Transpose:
776 			d_model->transpose();
777 		break;
778 		case Matrix::Invert:
779 			d_model->invert();
780 		break;
781 		case Matrix::FlipVertically:
782 			d_model->flipVertically();
783 		break;
784 		case Matrix::FlipHorizontally:
785 			d_model->flipHorizontally();
786 		break;
787 		case Matrix::RotateClockwise:
788 			d_model->rotate90(false);
789 		break;
790 		case Matrix::RotateCounterClockwise:
791 			d_model->rotate90(true);
792 		break;
793 		default:
794 		break;
795 	}
796 	d_model->matrix()->resetView();
797 }
798 
799 /*************************************************************************/
800 /*           Class MatrixPasteCommand                               	 */
801 /*************************************************************************/
MatrixPasteCommand(MatrixModel * model,int startRow,int endRow,int startCol,int endCol,double * clipboardData,int rows,int cols,double * backupData,int oldRows,int oldCols,const QString & text)802 MatrixPasteCommand::MatrixPasteCommand(MatrixModel *model, int startRow, int endRow, int startCol, int endCol,
803 					double *clipboardData, int rows, int cols, double *backupData, int oldRows, int oldCols,
804 					const QString& text):
805 QUndoCommand(text),
806 d_model(model),
807 d_start_row(startRow),
808 d_end_row(endRow),
809 d_start_col(startCol),
810 d_end_col(endCol),
811 d_rows(rows),
812 d_cols(cols),
813 d_old_rows(oldRows),
814 d_old_cols(oldCols),
815 d_clipboard_data(clipboardData),
816 d_backup_data(backupData)
817 {
818     setText(model->matrix()->objectName() + ": " + text);
819 }
820 
redo()821 void MatrixPasteCommand::redo()
822 {
823     if (!d_model)
824         return;
825 
826 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
827     d_model->pasteData(d_clipboard_data, d_start_row, d_start_col, d_rows, d_cols);
828 	d_model->matrix()->resetView();
829 	QApplication::restoreOverrideCursor();
830 }
831 
undo()832 void MatrixPasteCommand::undo()
833 {
834 	if (!d_model)
835         return;
836 
837  	double *data = d_model->dataVector();
838     if (!data)
839         return;
840 
841 	QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
842 
843 	if (d_old_rows != d_model->rowCount())
844 		d_model->setRowCount(d_old_rows);
845 	if (d_old_cols != d_model->columnCount())
846 		d_model->setColumnCount(d_old_cols);
847 
848     int cols = d_model->columnCount();
849 	int aux = 0;
850     for (int i = d_start_row; i <= d_end_row; i++){
851         int row = i*cols + d_start_col;
852         for (int j = d_start_col; j <= d_end_col; j++)
853             data[row++] = d_backup_data[aux++];
854     }
855     d_model->matrix()->resetView();
856 	QApplication::restoreOverrideCursor();
857 }
858