QtiPlot 0.9.8.2
Matrix.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : Matrix.h
00003     Project              : QtiPlot
00004     --------------------------------------------------------------------
00005     Copyright            : (C) 2006 - 2009 by Ion Vasilief
00006     Email (use @ for *)  : ion_vasilief*yahoo.fr
00007     Description          : Matrix worksheet class
00008 
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *  This program is free software; you can redistribute it and/or modify   *
00014  *  it under the terms of the GNU General Public License as published by   *
00015  *  the Free Software Foundation; either version 2 of the License, or      *
00016  *  (at your option) any later version.                                    *
00017  *                                                                         *
00018  *  This program is distributed in the hope that it will be useful,        *
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00021  *  GNU General Public License for more details.                           *
00022  *                                                                         *
00023  *   You should have received a copy of the GNU General Public License     *
00024  *   along with this program; if not, write to the Free Software           *
00025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
00026  *   Boston, MA  02110-1301  USA                                           *
00027  *                                                                         *
00028  ***************************************************************************/
00029 #ifndef MATRIX_H
00030 #define MATRIX_H
00031 
00032 #include <QHeaderView>
00033 #include <QTableView>
00034 #include <QPrinter>
00035 #include <QMessageBox>
00036 
00037 #include "MatrixModel.h"
00038 #include <MdiSubWindow.h>
00039 #include <ScriptingEnv.h>
00040 #include <Script.h>
00041 #include <LinearColorMap.h>
00042 
00043 #include <qwt_double_rect.h>
00044 
00045 #include <math.h>
00046 
00047 // (maximum) initial matrix size
00048 #define _Matrix_initial_rows_ 10
00049 #define _Matrix_initial_columns_ 3
00050 
00051 class QLabel;
00052 class QStackedWidget;
00053 class QShortcut;
00054 class QUndoStack;
00055 
00057 class Matrix: public MdiSubWindow, public scripted
00058 {
00059     Q_OBJECT
00060 
00061 public:
00062 
00075     Matrix(ScriptingEnv *env, int r, int c, const QString& label, ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
00076     Matrix(ScriptingEnv *env, const QImage& image, const QString& label, ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
00077     ~Matrix();
00078 
00079     enum Operation{Transpose, Invert, FlipHorizontally, FlipVertically, RotateClockwise,
00080                   RotateCounterClockwise, FFT, Clear, Calculate, MuParserCalculate, SetImage, ImportAscii};
00081     enum HeaderViewType{ColumnRow, XY};
00082     enum ViewType{TableView, ImageView};
00083     enum ColorMapType{Default, GrayScale, Rainbow, Custom};
00084     enum ImportMode {
00085         NewColumns, 
00086         NewRows, 
00087         Overwrite 
00088     };
00089     enum ResamplingMethod{Bilinear, Bicubic};
00090 
00091     void setViewType(ViewType, bool renderImage = true);
00092     ViewType viewType(){return d_view_type;};
00093 
00094     HeaderViewType headerViewType(){return d_header_view_type;};
00095     void setHeaderViewType(HeaderViewType type);
00096 
00097     QImage image();
00098     void displayImage(const QImage& image);
00099     void importImage(const QString& fn);
00100     void importImage(const QImage& image);
00101     void exportRasterImage(const QString& fileName, int quality = 100, int dpi = 0, int compression = 0);
00102     void exportSVG(const QString& fileName);
00103     void exportToFile(const QString& fileName);
00104     void exportVector(const QString& fileName, int res = 0, bool color = true);
00105     void exportVector(QPrinter *printer, int res = 0, bool color = true);
00106     void exportEMF(const QString& fileName);
00107 
00108     MatrixModel * matrixModel(){return d_matrix_model;};
00109     QUndoStack *undoStack(){return d_undo_stack;};
00110 
00111     QItemSelectionModel * selectionModel(){return d_table_view->selectionModel();};
00112 
00114     int numRows(){return d_matrix_model->rowCount();};
00115     void setNumRows(int rows){d_matrix_model->setRowCount(rows);};
00116 
00118     int numCols(){return d_matrix_model->columnCount();};
00119     void setNumCols(int cols){d_matrix_model->setColumnCount(cols);};
00120 
00121     void resample(int rows, int cols, const ResamplingMethod& method = Bilinear);
00122     void smooth();
00123 
00124     //event handlers
00126 
00129     void customEvent(QEvent *e);
00130 
00131     void resetView();
00132     void moveCell(const QModelIndex& index);
00133 
00134     void flipVertically();
00135     void flipHorizontally();
00136     void rotate90(bool clockwise = true);
00137     void fft(bool inverse = false);
00138 
00139     ColorMapType colorMapType(){return d_color_map_type;};
00140     void setColorMapType(ColorMapType mapType);
00141 
00142     LinearColorMap colorMap(){return d_color_map;};
00143     LinearColorMap *colorMapPointer(){return &d_color_map;};
00144     void setColorMap(const LinearColorMap& map);
00146     void setDefaultColorMap();
00147     void setGrayScale();
00148     void setRainbowColorMap();
00150     double integrate();
00152     double determinant();
00154     void transpose();
00156     void invert();
00157 
00159     bool calculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1, bool forceMuParser = false);
00161     bool muParserCalculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1);
00162 
00163     bool exportODF(const QString& fname, bool exportSelection);
00164     bool exportExcel(const QString& fname, bool exportSelection);
00165     bool exportOdsSpreadsheet(const QString& fname, bool exportSelection);
00166 
00167     bool exportASCII(const QString& fname, const QString& separator, bool exportSelection);
00168     void importASCII(const QString &fname, const QString &sep, int ignoredLines, bool stripSpaces,
00169                     bool simplifySpaces, const QString& commentString, ImportMode importAs = Overwrite,
00170                     const QLocale& l = QLocale(), int endLineChar = 0, int maxRows = -1);
00171 
00172     virtual QString sizeToString();
00173 
00174 public slots:
00175     void exportPDF(const QString& fileName);
00177     void print();
00178     void print(QPrinter *);
00180     void print(const QString& fileName);
00181 
00183     int columnsWidth(){return d_column_width;};
00185     void setColumnsWidth(int width);
00186 
00188     void setDimensions(int rows, int cols);
00189 
00191     QString text(int row, int col);
00193     void setText(int row, int col, const QString & new_text );
00195     double cell(int row, int col);
00197     void setCell(int row, int col, double value );
00198 
00204     QChar textFormat(){return txt_format;};
00211     int precision(){return num_precision;};
00217     void setNumericPrecision(int prec);
00218 
00227     void setTextFormat(const QChar &format, int precision);
00228     void setNumericFormat(const QChar & f, int prec);
00229 
00231     QString formula(){return formula_str;};
00233     void setFormula(const QString &s){formula_str = s;};
00234 
00236     void restore(const QStringList &l);
00239     void save(const QString &, const QString &, bool saveAsTemplate = false);
00240 
00241     // selection operations
00243     void cutSelection();
00245     void copySelection();
00247     void clearSelection();
00249     void pasteSelection();
00250 
00252     void insertRow();
00254     void deleteSelectedRows();
00256     int numSelectedRows();
00257 
00259     void insertColumn();
00261     void deleteSelectedColumns();
00263     int numSelectedColumns();
00264 
00266     double xStart(){return x_start;};
00268     double xEnd(){return x_end;};
00270     double yStart(){return y_start;};
00272     double yEnd(){return y_end;};
00273 
00275     double dx(){return fabs(x_end - x_start)/(double)(numCols() - 1);};
00277     double dy(){return fabs(y_end - y_start)/(double)(numRows() - 1);};
00278 
00280     QwtDoubleRect boundingRect();
00282     void setCoordinates(double xs, double xe, double ys, double ye);
00283 
00285     void range(double *min, double *max);
00287     bool isEmpty();
00288 
00290     QwtDoubleInterval colorRange();
00291 
00293     void goToRow(int row);
00295     void goToColumn(int col);
00296 
00298     static double** allocateMatrixData(int rows, int columns, bool init = false);
00300     static void freeMatrixData(double **data, int rows);
00301 
00302     int verticalHeaderWidth(){return d_table_view->verticalHeader()->width();}
00303 
00304     void copy(Matrix *m);
00306     double *initWorkspace(int size);
00307     void freeWorkspace(){free(d_workspace); d_workspace = NULL;};
00308 
00309     bool canCalculate(bool useMuParser = true);
00310     void notifyModifiedData(){emit modifiedData(this);};
00311 
00312 signals:
00313     void modifiedData(Matrix *);
00314 
00315 private:
00316     bool eventFilter(QObject *, QEvent *);
00317 
00319     void initTable(int rows, int cols);
00320     void initImage(const QImage& image);
00321     void initImageView();
00322     void initTableView();
00323     void initGlobals();
00324     bool ignoreUndo();
00325 
00326     QStackedWidget *d_stack;
00327     MatrixModel *d_matrix_model;
00329     QTableView *d_table_view;
00331     QLabel *imageLabel;
00333     QString formula_str;
00335     QChar txt_format;
00337     int num_precision;
00338     double x_start, 
00339     x_end,  
00340     y_start,  
00341     y_end;  
00342 
00344     double d_color_min;
00346     double d_color_max;
00347 
00349     ViewType d_view_type;
00351     HeaderViewType d_header_view_type;
00353     LinearColorMap d_color_map;
00355     ColorMapType d_color_map_type;
00357     int d_column_width;
00358     QShortcut *d_select_all_shortcut;
00360     QUndoStack *d_undo_stack;
00362     double *d_workspace;
00363 };
00364 
00365 #endif