QtiPlot  0.9.8.2
Matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Matrix.h
3  Project : QtiPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 - 2009 by Ion Vasilief
6  Email (use @ for *) : ion_vasilief*yahoo.fr
7  Description : Matrix worksheet class
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 #ifndef MATRIX_H
30 #define MATRIX_H
31 
32 #include <QHeaderView>
33 #include <QTableView>
34 #include <QPrinter>
35 #include <QMessageBox>
36 
37 #include "MatrixModel.h"
38 #include <MdiSubWindow.h>
39 #include <ScriptingEnv.h>
40 #include <Script.h>
41 #include <LinearColorMap.h>
42 
43 #include <qwt_double_rect.h>
44 
45 #include <math.h>
46 
47 // (maximum) initial matrix size
48 #define _Matrix_initial_rows_ 10
49 #define _Matrix_initial_columns_ 3
50 
51 class QLabel;
52 class QStackedWidget;
53 class QShortcut;
54 class QUndoStack;
55 
57 class Matrix: public MdiSubWindow, public scripted
58 {
59  Q_OBJECT
60 
61 public:
62 
75  Matrix(ScriptingEnv *env, int r, int c, const QString& label, ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
76  Matrix(ScriptingEnv *env, const QImage& image, const QString& label, ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
77  ~Matrix();
78 
84  enum ImportMode {
88  };
90 
91  void setViewType(ViewType, bool renderImage = true);
93 
96 
97  QImage image();
98  void displayImage(const QImage& image);
99  void importImage(const QString& fn);
100  void importImage(const QImage& image);
101  void exportRasterImage(const QString& fileName, int quality = 100, int dpi = 0, int compression = 0);
102  void exportSVG(const QString& fileName);
103  void exportToFile(const QString& fileName);
104  void exportVector(const QString& fileName, int res = 0, bool color = true);
105  void exportVector(QPrinter *printer, int res = 0, bool color = true);
106  void exportEMF(const QString& fileName);
107 
109  QUndoStack *undoStack(){return d_undo_stack;};
110 
111  QItemSelectionModel * selectionModel(){return d_table_view->selectionModel();};
112 
114  int numRows(){return d_matrix_model->rowCount();};
115  void setNumRows(int rows){d_matrix_model->setRowCount(rows);};
116 
118  int numCols(){return d_matrix_model->columnCount();};
119  void setNumCols(int cols){d_matrix_model->setColumnCount(cols);};
120 
121  void resample(int rows, int cols, const ResamplingMethod& method = Bilinear);
122  void smooth();
123 
124  //event handlers
126 
129  void customEvent(QEvent *e);
130 
131  void resetView();
132  void moveCell(const QModelIndex& index);
133 
134  void flipVertically();
135  void flipHorizontally();
136  void rotate90(bool clockwise = true);
137  void fft(bool inverse = false);
138 
140  void setColorMapType(ColorMapType mapType);
141 
144  void setColorMap(const LinearColorMap& map);
146  void setDefaultColorMap();
147  void setGrayScale();
148  void setRainbowColorMap();
150  double integrate();
152  double determinant();
154  void transpose();
156  void invert();
157 
159  bool calculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1, bool forceMuParser = false);
161  bool muParserCalculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1);
162 
163  bool exportODF(const QString& fname, bool exportSelection);
164  bool exportExcel(const QString& fname, bool exportSelection);
165  bool exportOdsSpreadsheet(const QString& fname, bool exportSelection);
166 
167  bool exportASCII(const QString& fname, const QString& separator, bool exportSelection);
168  void importASCII(const QString &fname, const QString &sep, int ignoredLines, bool stripSpaces,
169  bool simplifySpaces, const QString& commentString, ImportMode importAs = Overwrite,
170  const QLocale& l = QLocale(), int endLineChar = 0, int maxRows = -1);
171 
172  virtual QString sizeToString();
173 
174 public slots:
175  void exportPDF(const QString& fileName);
177  void print();
178  void print(QPrinter *);
180  void print(const QString& fileName);
181 
183  int columnsWidth(){return d_column_width;};
185  void setColumnsWidth(int width);
186 
188  void setDimensions(int rows, int cols);
189 
191  QString text(int row, int col);
193  void setText(int row, int col, const QString & new_text );
195  double cell(int row, int col);
197  void setCell(int row, int col, double value );
198 
204  QChar textFormat(){return txt_format;};
211  int precision(){return num_precision;};
217  void setNumericPrecision(int prec);
218 
227  void setTextFormat(const QChar &format, int precision);
228  void setNumericFormat(const QChar & f, int prec);
229 
231  QString formula(){return formula_str;};
233  void setFormula(const QString &s){formula_str = s;};
234 
236  void restore(const QStringList &l);
239  void save(const QString &, const QString &, bool saveAsTemplate = false);
240 
241  // selection operations
243  void cutSelection();
245  void copySelection();
247  void clearSelection();
249  void pasteSelection();
250 
252  void insertRow();
254  void deleteSelectedRows();
256  int numSelectedRows();
257 
259  void insertColumn();
261  void deleteSelectedColumns();
263  int numSelectedColumns();
264 
266  double xStart(){return x_start;};
268  double xEnd(){return x_end;};
270  double yStart(){return y_start;};
272  double yEnd(){return y_end;};
273 
275  double dx(){return fabs(x_end - x_start)/(double)(numCols() - 1);};
277  double dy(){return fabs(y_end - y_start)/(double)(numRows() - 1);};
278 
280  QwtDoubleRect boundingRect();
282  void setCoordinates(double xs, double xe, double ys, double ye);
283 
285  void range(double *min, double *max);
287  bool isEmpty();
288 
290  QwtDoubleInterval colorRange();
291 
293  void goToRow(int row);
295  void goToColumn(int col);
296 
298  static double** allocateMatrixData(int rows, int columns, bool init = false);
300  static void freeMatrixData(double **data, int rows);
301 
302  int verticalHeaderWidth(){return d_table_view->verticalHeader()->width();}
303 
304  void copy(Matrix *m);
306  double *initWorkspace(int size);
307  void freeWorkspace(){free(d_workspace); d_workspace = NULL;};
308 
309  bool canCalculate(bool useMuParser = true);
310  void notifyModifiedData(){emit modifiedData(this);};
311 
312 signals:
313  void modifiedData(Matrix *);
314 
315 private:
316  bool eventFilter(QObject *, QEvent *);
317 
319  void initTable(int rows, int cols);
320  void initImage(const QImage& image);
321  void initImageView();
322  void initTableView();
323  void initGlobals();
324  bool ignoreUndo();
325 
326  QStackedWidget *d_stack;
329  QTableView *d_table_view;
331  QLabel *imageLabel;
333  QString formula_str;
335  QChar txt_format;
338  double x_start,
339  x_end,
340  y_start,
341  y_end;
342 
344  double d_color_min;
346  double d_color_max;
347 
360  QUndoStack *d_undo_stack;
362  double *d_workspace;
363 };
364 
365 #endif