SciDAVis  1.D4
future_Matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Matrix.h
3  Project : SciDAVis
4  Description : Aspect providing a spreadsheet to manage MxN matrix data
5  --------------------------------------------------------------------
6  Copyright : (C) 2006-2009 Tilman Benkert (thzs*gmx.net)
7  Copyright : (C) 2006-2009 Knut Franke (knut.franke*gmx.de)
8  Copyright : (C) 2006-2007 Ion Vasilief (ion_vasilief*yahoo.fr)
9  (replace * with @ in the email addresses)
10 
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  * This program is distributed in the hope that it will be useful, *
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23  * GNU General Public License for more details. *
24  * *
25  * You should have received a copy of the GNU General Public License *
26  * along with this program; if not, write to the Free Software *
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
28  * Boston, MA 02110-1301 USA *
29  * *
30  ***************************************************************************/
31 #ifndef FUTURE_MATRIX_H
32 #define FUTURE_MATRIX_H
33 
34 #ifndef LEGACY_CODE_0_2_x
35 #include "core/AbstractScriptingEngine.h"
36 #endif
37 #include "core/AbstractPart.h"
38 #include "matrix/MatrixView.h"
39 #include "lib/macros.h"
40 
41 class QContextMenuEvent;
42 class QEvent;
43 class ActionManager;
44 
45 // (maximum) initial matrix size (this is the size of the window, not the number of cells)
46 #define _Matrix_initial_rows_ 10
47 #define _Matrix_initial_columns_ 3
48 
49 // TODO: move all selection related stuff to the primary view
50 
51 namespace future{
52 #ifndef LEGACY_CODE_0_2_x
53 
54 class Matrix : public AbstractPart, public scripted
55 #else
56 class Matrix : public AbstractPart
57 #endif
58 {
59  Q_OBJECT
60 
61  public:
62  class Private;
63  friend class Private;
64 
73 #ifndef LEGACY_CODE_0_2_x
74  Matrix(AbstractScriptingEngine *engine, int rows, int cols, const QString& name);
75 #else
76  Matrix(void *, int rows, int cols, const QString& name);
77  void setView(MatrixView * view);
78 #endif
79  ~Matrix();
80 
82  virtual QIcon icon() const;
84 
87  virtual QMenu *createContextMenu() const;
89 
93  virtual QWidget *view();
95 
100  QMenu * createSelectionMenu(QMenu * append_to = 0);
102 
107  QMenu * createColumnMenu(QMenu * append_to = 0);
109 
114  QMenu * createRowMenu(QMenu * append_to = 0);
116 
121  QMenu * createMatrixMenu(QMenu * append_to = 0);
123 
126  virtual bool fillProjectMenu(QMenu * menu);
127 
128  void insertColumns(int before, int count);
129  void appendColumns(int count) { insertColumns(columnCount(), count); }
130  void removeColumns(int first, int count);
131  void insertRows(int before, int count);
132  void appendRows(int count) { insertRows(rowCount(), count); }
133  void removeRows(int first, int count);
135  void setDimensions(int rows, int cols);
137  int columnCount() const;
139  int rowCount() const;
140 
142 
145  void setPlotMenu(QMenu * menu);
147  double cell(int row, int col) const;
149  void setCell(int row, int col, double value );
151  QVector<qreal> columnCells(int col, int first_row, int last_row);
153  void setColumnCells(int col, int first_row, int last_row, const QVector<qreal> & values);
155  QVector<qreal> rowCells(int row, int first_column, int last_column);
157  void setRowCells(int row, int first_column, int last_column, const QVector<qreal> & values);
159  QString text(int row, int col);
160  void copy(Matrix * other);
161  double xStart() const;
162  double yStart() const;
163  double xEnd() const;
164  double yEnd() const;
165  QString formula() const;
166  void setFormula(const QString & formula);
167  void setXStart(double x);
168  void setXEnd(double x);
169  void setYStart(double y);
170  void setYEnd(double y);
171  void setCoordinates(double x1, double x2, double y1, double y2);
172  char numericFormat() const;
173  int displayedDigits() const;
174  void setNumericFormat(char format);
175  void setDisplayedDigits(int digits);
176 
178 
179 
180  virtual void save(QXmlStreamWriter *) const;
182  virtual bool load(XmlStreamReader *);
184 
186 
189  void setRowHeight(int row, int height);
191 
194  void setColumnWidth(int col, int width);
195  int rowHeight(int row) const;
196  int columnWidth(int col) const;
197 
198  public:
199  static ActionManager * actionManager();
200  static void initActionManager();
201  static int defaultColumnWidth() { return default_column_width; }
202  static int defaultRowHeight() { return default_row_height; }
203  static void setDefaultColumnWidth(int width) { default_column_width = width; }
204  static void setDefaultRowHeight(int height) { default_row_height = height; }
205 
206  private:
209  Matrix();
210  // TODO: the default sizes are to be controlled by the global Matrix settings
212  static int default_row_height;
213  public:
214  static Matrix * fromImage(const QImage & image);
215 
216  public slots:
218  void clear();
219  void transpose();
220  void mirrorVertically();
221  void mirrorHorizontally();
222 
223  void cutSelection();
224  void copySelection();
225  void pasteIntoSelection();
226  void clearSelectedCells();
227  void dimensionsDialog();
228  void goToCell();
230  void insertEmptyColumns();
232  void insertEmptyRows();
233  void removeSelectedColumns();
234  void removeSelectedRows();
235  void clearSelectedColumns();
236  void clearSelectedRows();
237  void selectAll();
239 
242  void showMatrixViewContextMenu(const QPoint& pos);
244 
247  void showMatrixViewColumnContextMenu(const QPoint& pos);
249 
252  void showMatrixViewRowContextMenu(const QPoint& pos);
253  void editFormat();
254  void editCoordinates();
255  void editFormula();
257  void addColumns();
259  void addRows();
260  void importImageDialog();
262  void duplicate();
263 #ifdef LEGACY_CODE_0_2_x
264  void recalculateSelectedCells();
265 #endif
266 
267  signals:
268  void columnsAboutToBeInserted(int before, int count);
269  void columnsInserted(int first, int count);
270  void columnsAboutToBeRemoved(int first, int count);
271  void columnsRemoved(int first, int count);
272  void rowsAboutToBeInserted(int before, int count);
273  void rowsInserted(int first, int count);
274  void rowsAboutToBeRemoved(int first, int count);
275  void rowsRemoved(int first, int count);
276  void dataChanged(int top, int left, int bottom, int right);
277  void coordinatesChanged();
278  void formulaChanged();
279  void formatChanged();
280 #ifdef LEGACY_CODE_0_2_x
281  void recalculate();
282 #endif
283 
284  private slots:
285  void adjustTabBarAction(bool visible);
286 
287  private:
288  void createActions();
289  void connectActions();
290  void addActionsToView();
291 
293  bool readDisplayElement(XmlStreamReader * reader);
297  bool readFormulaElement(XmlStreamReader * reader);
299  bool readCellElement(XmlStreamReader * reader);
300  bool readRowHeightElement(XmlStreamReader * reader);
302 
303  QMenu * d_plot_menu;
304 
306 
312 
313 
315  QAction * action_select_all;
317  QAction * action_go_to_cell;
324  QAction * action_duplicate;
325  QAction * action_transpose;
329 
330 
336 
337 
340  QAction * action_clear_rows;
341  QAction * action_add_rows;
343 
346 };
347 
364 {
365  public:
366  Private(Matrix *owner);
368 
374  void insertColumns(int before, int count);
376 
380  void removeColumns(int first, int count);
382 
388  void insertRows(int before, int count);
390 
394  void removeRows(int first, int count);
396  int columnCount() const { return d_column_count; }
398  int rowCount() const { return d_row_count; }
399  QString name() const { return d_owner->name(); }
401  double cell(int row, int col) const;
403  void setCell(int row, int col, double value);
405  QVector<qreal> columnCells(int col, int first_row, int last_row);
407  void setColumnCells(int col, int first_row, int last_row, const QVector<qreal> & values);
409  QVector<qreal> rowCells(int row, int first_column, int last_column);
411  void setRowCells(int row, int first_column, int last_column, const QVector<qreal> & values);
412  char numericFormat() const { return d_numeric_format; }
413  void setNumericFormat(char format) { d_numeric_format = format; emit d_owner->formatChanged(); }
414  int displayedDigits() const { return d_displayed_digits; }
415  void setDisplayedDigits(int digits) { d_displayed_digits = digits; emit d_owner->formatChanged(); }
417  void clearColumn(int col);
418  double xStart() const;
419  double yStart() const;
420  double xEnd() const;
421  double yEnd() const;
422  QString formula() const;
423  void setFormula(const QString & formula);
424  void setXStart(double x);
425  void setXEnd(double x);
426  void setYStart(double y);
427  void setYEnd(double y);
428  void setRowHeight(int row, int height) { d_row_heights[row] = height; }
429  void setColumnWidth(int col, int width) { d_column_widths[col] = width; }
430  int rowHeight(int row) const { return d_row_heights.at(row); }
431  int columnWidth(int col) const { return d_column_widths.at(col); }
433 
445  void blockChangeSignals(bool block) { d_block_change_signals = block; }
447  void emitDataChanged(int top, int left, int bottom, int right) { emit d_owner->dataChanged(top, left, bottom, right); }
448 
449  private:
457  QVector< QVector<qreal> > d_data;
459  QList<int> d_row_heights;
461  QList<int> d_column_widths;
463  QString d_formula; // TODO: should we support interval/rectangle based formulas?
468  double d_x_start,
469  d_x_end,
470  d_y_start,
471  d_y_end;
473 
474 };
475 
476 } // namespace
477 #endif