QtiPlot 0.9.7.3
|
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 00090 void setViewType(ViewType, bool renderImage = true); 00091 ViewType viewType(){return d_view_type;}; 00092 00093 HeaderViewType headerViewType(){return d_header_view_type;}; 00094 void setHeaderViewType(HeaderViewType type); 00095 00096 QImage image(); 00097 void displayImage(const QImage& image); 00098 void importImage(const QString& fn); 00099 void importImage(const QImage& image); 00100 void exportRasterImage(const QString& fileName, int quality = 100, int dpi = 0); 00101 void exportSVG(const QString& fileName); 00102 void exportToFile(const QString& fileName); 00103 void exportVector(const QString& fileName, int res = 0, bool color = true); 00104 #ifdef EMF_OUTPUT 00105 void exportEMF(const QString& fileName); 00106 #endif 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 //event handlers 00123 00126 void customEvent(QEvent *e); 00127 00128 void resetView(); 00129 void moveCell(const QModelIndex& index); 00130 00131 void flipVertically(); 00132 void flipHorizontally(); 00133 void rotate90(bool clockwise = true); 00134 void fft(bool inverse = false); 00135 00136 ColorMapType colorMapType(){return d_color_map_type;}; 00137 void setColorMapType(ColorMapType mapType); 00138 00139 QwtLinearColorMap colorMap(){return d_color_map;}; 00140 QwtLinearColorMap *colorMapPointer(){return &d_color_map;}; 00141 void setColorMap(const QwtLinearColorMap& map); 00143 void setColorMap(const QStringList& lst); 00145 void setDefaultColorMap(); 00146 void setGrayScale(); 00147 void setRainbowColorMap(); 00149 double integrate(); 00151 double determinant(); 00153 void transpose(); 00155 void invert(); 00156 00158 bool calculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1, bool forceMuParser = false); 00160 bool muParserCalculate(int startRow = 0, int endRow = -1, int startCol = 0, int endCol = -1); 00161 00162 bool exportODF(const QString& fname, bool exportSelection); 00163 #ifdef XLS_IMPORT 00164 bool exportExcel(const QString& fname, bool exportSelection); 00165 #endif 00166 bool exportASCII(const QString& fname, const QString& separator, bool exportSelection); 00167 void importASCII(const QString &fname, const QString &sep, int ignoredLines, bool stripSpaces, 00168 bool simplifySpaces, const QString& commentString, ImportMode importAs = Overwrite, 00169 const QLocale& l = QLocale(), int endLineChar = 0, int maxRows = -1); 00170 00171 virtual QString sizeToString(); 00172 00173 public slots: 00174 void exportPDF(const QString& fileName); 00176 void print(); 00177 void print(QPrinter *); 00179 void print(const QString& fileName); 00180 00182 int columnsWidth(){return d_column_width;}; 00184 void setColumnsWidth(int width); 00185 00187 void setDimensions(int rows, int cols); 00188 00190 QString text(int row, int col); 00192 void setText(int row, int col, const QString & new_text ); 00194 double cell(int row, int col); 00196 void setCell(int row, int col, double value ); 00197 00203 QChar textFormat(){return txt_format;}; 00210 int precision(){return num_precision;}; 00216 void setNumericPrecision(int prec); 00217 00226 void setTextFormat(const QChar &format, int precision); 00227 void setNumericFormat(const QChar & f, int prec); 00228 00230 QString formula(){return formula_str;}; 00232 void setFormula(const QString &s){formula_str = s;}; 00233 00235 void restore(const QStringList &l); 00238 void save(const QString &, const QString &, bool saveAsTemplate = false); 00239 00240 // selection operations 00242 void cutSelection(); 00244 void copySelection(); 00246 void clearSelection(); 00248 void pasteSelection(); 00249 00251 void insertRow(); 00253 void deleteSelectedRows(); 00255 int numSelectedRows(); 00256 00258 void insertColumn(); 00260 void deleteSelectedColumns(); 00262 int numSelectedColumns(); 00263 00265 double xStart(){return x_start;}; 00267 double xEnd(){return x_end;}; 00269 double yStart(){return y_start;}; 00271 double yEnd(){return y_end;}; 00272 00274 double dx(){return fabs(x_end - x_start)/(double)(numCols() - 1);}; 00276 double dy(){return fabs(y_end - y_start)/(double)(numRows() - 1);}; 00277 00279 QwtDoubleRect boundingRect(); 00281 void setCoordinates(double xs, double xe, double ys, double ye); 00282 00284 void range(double *min, double *max); 00286 bool isEmpty(); 00287 00289 void goToRow(int row); 00291 void goToColumn(int col); 00292 00294 static double** allocateMatrixData(int rows, int columns); 00296 static void freeMatrixData(double **data, int rows); 00297 00298 int verticalHeaderWidth(){return d_table_view->verticalHeader()->width();} 00299 00300 void copy(Matrix *m); 00302 double *initWorkspace(int size); 00303 void freeWorkspace(){free(d_workspace); d_workspace = NULL;}; 00304 00305 bool canCalculate(bool useMuParser = true); 00306 void notifyModifiedData(){emit modifiedData(this);}; 00307 00308 signals: 00309 void modifiedData(Matrix *); 00310 00311 private: 00312 bool eventFilter(QObject *, QEvent *); 00313 00315 void initTable(int rows, int cols); 00316 void initImage(const QImage& image); 00317 void initImageView(); 00318 void initTableView(); 00319 void initGlobals(); 00320 bool ignoreUndo(); 00321 00322 QStackedWidget *d_stack; 00323 MatrixModel *d_matrix_model; 00325 QTableView *d_table_view; 00327 QLabel *imageLabel; 00329 QString formula_str; 00331 QChar txt_format; 00333 int num_precision; 00334 double x_start, 00335 x_end, 00336 y_start, 00337 y_end; 00338 00340 ViewType d_view_type; 00342 HeaderViewType d_header_view_type; 00344 QwtLinearColorMap d_color_map; 00346 ColorMapType d_color_map_type; 00348 int d_column_width; 00349 QShortcut *d_select_all_shortcut; 00351 QUndoStack *d_undo_stack; 00353 double *d_workspace; 00354 }; 00355 00356 #endif