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 
00042 #include <qwt_double_rect.h>
00043 #include <qwt_color_map.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);
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 exportEMF(const QString& fileName);
00106 
00107     MatrixModel * matrixModel(){return d_matrix_model;};
00108     QUndoStack *undoStack(){return d_undo_stack;};
00109 
00110     QItemSelectionModel * selectionModel(){return d_table_view->selectionModel();};
00111 
00113     int numRows(){return d_matrix_model->rowCount();};
00114     void setNumRows(int rows){d_matrix_model->setRowCount(rows);};
00115 
00117     int numCols(){return d_matrix_model->columnCount();};
00118     void setNumCols(int cols){d_matrix_model->setColumnCount(cols);};
00119 
00120     void resample(int rows, int cols, const ResamplingMethod& method = Bilinear);
00121     void smooth();
00122 
00123     //event handlers
00125 
00128     void customEvent(QEvent *e);
00129 
00130     void resetView();
00131     void moveCell(const QModelIndex& index);
00132 
00133     void flipVertically();
00134     void flipHorizontally();
00135     void rotate90(bool clockwise = true);
00136     void fft(bool inverse = false);
00137 
00138     ColorMapType colorMapType(){return d_color_map_type;};
00139     void setColorMapType(ColorMapType mapType);
00140 
00141     QwtLinearColorMap colorMap(){return d_color_map;};
00142     QwtLinearColorMap *colorMapPointer(){return &d_color_map;};
00143     void setColorMap(const QwtLinearColorMap& map);
00145     void setColorMap(const QStringList& lst);
00147     void setDefaultColorMap();
00148     void setGrayScale();
00149     void setRainbowColorMap();
00151     double integrate();
00153     double determinant();
00155     void transpose();
00157     void invert();
00158 
00160     bool calculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1, bool forceMuParser = false);
00162     bool muParserCalculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1);
00163 
00164     bool exportODF(const QString& fname, bool exportSelection);
00165     bool exportExcel(const QString& fname, bool exportSelection);
00166     bool exportOdsSpreadsheet(const QString& fname, bool exportSelection);
00167 
00168     bool exportASCII(const QString& fname, const QString& separator, bool exportSelection);
00169     void importASCII(const QString &fname, const QString &sep, int ignoredLines, bool stripSpaces,
00170                     bool simplifySpaces, const QString& commentString, ImportMode importAs = Overwrite,
00171                     const QLocale& l = QLocale(), int endLineChar = 0, int maxRows = -1);
00172 
00173     virtual QString sizeToString();
00174 
00175 public slots:
00176     void exportPDF(const QString& fileName);
00178     void print();
00179     void print(QPrinter *);
00181     void print(const QString& fileName);
00182 
00184     int columnsWidth(){return d_column_width;};
00186     void setColumnsWidth(int width);
00187 
00189     void setDimensions(int rows, int cols);
00190 
00192     QString text(int row, int col);
00194     void setText(int row, int col, const QString & new_text );
00196     double cell(int row, int col);
00198     void setCell(int row, int col, double value );
00199 
00205     QChar textFormat(){return txt_format;};
00212     int precision(){return num_precision;};
00218     void setNumericPrecision(int prec);
00219 
00228     void setTextFormat(const QChar &format, int precision);
00229     void setNumericFormat(const QChar & f, int prec);
00230 
00232     QString formula(){return formula_str;};
00234     void setFormula(const QString &s){formula_str = s;};
00235 
00237     void restore(const QStringList &l);
00240     void save(const QString &, const QString &, bool saveAsTemplate = false);
00241 
00242     // selection operations
00244     void cutSelection();
00246     void copySelection();
00248     void clearSelection();
00250     void pasteSelection();
00251 
00253     void insertRow();
00255     void deleteSelectedRows();
00257     int numSelectedRows();
00258 
00260     void insertColumn();
00262     void deleteSelectedColumns();
00264     int numSelectedColumns();
00265 
00267     double xStart(){return x_start;};
00269     double xEnd(){return x_end;};
00271     double yStart(){return y_start;};
00273     double yEnd(){return y_end;};
00274 
00276     double dx(){return fabs(x_end - x_start)/(double)(numCols() - 1);};
00278     double dy(){return fabs(y_end - y_start)/(double)(numRows() - 1);};
00279 
00281     QwtDoubleRect boundingRect();
00283     void setCoordinates(double xs, double xe, double ys, double ye);
00284 
00286     void range(double *min, double *max);
00288     bool isEmpty();
00289 
00291     void goToRow(int row);
00293     void goToColumn(int col);
00294 
00296     static double** allocateMatrixData(int rows, int columns);
00298     static void freeMatrixData(double **data, int rows);
00299 
00300     int verticalHeaderWidth(){return d_table_view->verticalHeader()->width();}
00301 
00302     void copy(Matrix *m);
00304     double *initWorkspace(int size);
00305     void freeWorkspace(){free(d_workspace); d_workspace = NULL;};
00306 
00307     bool canCalculate(bool useMuParser = true);
00308     void notifyModifiedData(){emit modifiedData(this);};
00309 
00310 signals:
00311     void modifiedData(Matrix *);
00312 
00313 private:
00314     bool eventFilter(QObject *, QEvent *);
00315 
00317     void initTable(int rows, int cols);
00318     void initImage(const QImage& image);
00319     void initImageView();
00320     void initTableView();
00321     void initGlobals();
00322     bool ignoreUndo();
00323 
00324     QStackedWidget *d_stack;
00325     MatrixModel *d_matrix_model;
00327     QTableView *d_table_view;
00329     QLabel *imageLabel;
00331     QString formula_str;
00333     QChar txt_format;
00335     int num_precision;
00336     double x_start, 
00337     x_end,  
00338     y_start,  
00339     y_end;  
00340 
00342     ViewType d_view_type;
00344     HeaderViewType d_header_view_type;
00346     QwtLinearColorMap d_color_map;
00348     ColorMapType d_color_map_type;
00350     int d_column_width;
00351     QShortcut *d_select_all_shortcut;
00353     QUndoStack *d_undo_stack;
00355     double *d_workspace;
00356 };
00357 
00358 #endif