QtiPlot 0.9.7.3

Table.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : Table.h
00003     Project              : QtiPlot
00004     --------------------------------------------------------------------
00005     Copyright            : (C) 2006 by Ion Vasilief, Knut Franke
00006     Email (use @ for *)  : ion_vasilief*yahoo.fr
00007     Description          : Table 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 TABLE_H
00030 #define TABLE_H
00031 
00032 #include <q3table.h>
00033 #include <q3header.h>
00034 #include <QVarLengthArray>
00035 #include <QLocale>
00036 
00037 #include <MdiSubWindow.h>
00038 #include <ScriptingEnv.h>
00039 #include <Script.h>
00040 
00041 class MyTable : public Q3Table
00042 {
00043 public:
00044     MyTable(QWidget * parent = 0, const char * name = 0);
00045     MyTable(int numRows, int numCols, QWidget * parent = 0, const char * name = 0);
00046 
00047 private:
00048     void activateNextCell();
00049 };
00050 
00057 class Table: public MdiSubWindow, public scripted
00058 {
00059     Q_OBJECT
00060 
00061 public:
00062     enum PlotDesignation{All = -1, None = 0, X = 1, Y = 2, Z = 3, xErr = 4, yErr = 5, Label = 6};
00063     enum ColType{Numeric = 0, Text = 1, Date = 2, Time = 3, Month = 4, Day = 5};
00064     enum NumericFormat{Default = 0, Decimal = 1, Scientific = 2};
00065     enum ImportMode {
00066         NewColumns, 
00067         NewRows, 
00068         Overwrite 
00069     };
00070 
00071     Table(ScriptingEnv *env, int r,int c, const QString &label, ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
00072 
00073     Q3TableSelection getSelection();
00074 
00076     void setNumericPrecision(int prec);
00078     void updateDecimalSeparators(const QLocale& oldSeparators);
00079     void setAutoUpdateValues(bool on = true);
00080     virtual QString sizeToString();
00081 
00082     double avg(int col, int startRow = 0, int endRow = -1);
00083     double sum(int col, int startRow = 0, int endRow = -1);
00084     double min(int col, int startRow = 0, int endRow = -1);
00085     double max(int col, int startRow = 0, int endRow = -1);
00086     Table* extractData(const QString& name, const QString& condition, int startRow = 0, int endRow = -1);
00087 
00088 public slots:
00089     MyTable* table(){return d_table;};
00090     void copy(Table *m, bool values = true);
00091     int numRows();
00092     int numCols();
00093     void setNumRows(int rows);
00094     void setNumCols(int cols);
00095     void resizeRows(int);
00096     void resizeCols(int);
00097 
00099     double cell(int row, int col);
00100     void setCell(int row, int col, double val);
00101 
00102     QString text(int row, int col);
00103     QStringList columnsList();
00104     QStringList colNames(){return col_label;}
00105     QString colName(int col);
00106     void setColName(int col, const QString& text, bool enumerateRight = false, bool warn = true);
00107     QString colLabel(int col){return col_label[col];};
00108     int colIndex(const QString& name);
00109 
00110     int colPlotDesignation(int col){return col_plot_type[col];};
00111     void setColPlotDesignation(int col, PlotDesignation pd);
00112     void setPlotDesignation(PlotDesignation pd, bool rightColumns = false);
00113     QList<int> plotDesignations(){return col_plot_type;};
00114 
00115     void setHeader(QStringList header);
00116     void loadHeader(QStringList header);
00117     void setHeaderColType();
00118     void setText(int row,int col,const QString & text);
00119     void setRandomValues();
00120     void setAscValues();
00121 
00122     void cellEdited(int,int col);
00123     void moveCurrentCell();
00124     void clearCell(int row, int col);
00125     bool isEmptyRow(int row);
00126     bool isEmptyColumn(int col);
00127     int nonEmptyRows();
00128 
00129     void print();
00130     void print(QPrinter *);
00131     void print(const QString& fileName);
00132     void exportPDF(const QString& fileName);
00133 
00135 
00136     bool eventFilter(QObject *object, QEvent *e);
00137     void customEvent( QEvent* e);
00139 
00141 
00142     void removeCol();
00143     void removeCol(const QStringList& list);
00144     void insertCol();
00145     virtual void insertCols(int start, int count);
00146     virtual void addCol(PlotDesignation pd = Y);
00147     void addColumns(int c);
00148     virtual void moveColumn(int, int, int);
00149     void swapColumns(int, int);
00150     void moveColumnBy(int cols);
00151     void hideSelectedColumns();
00152     void showAllColumns();
00153     void hideColumn(int col, bool = true);
00154     bool isColumnHidden(int col){return d_table->isColumnHidden(col);};
00156 
00158 
00159 
00162     void sortColAsc();
00166     void sortColDesc();
00171     void sortColumn(int col = -1, int order = 0);
00176     void sortTableDialog();
00178     void sort(int type = 0, int order  = 0, const QString& leadCol = QString());
00180     void sortColumns(int type = 0, int order = 0, const QString& leadCol = QString());
00187     void sortColumns(const QStringList& cols, int type = 0, int order = 0, const QString& leadCol = QString());
00192     void sortColumnsDialog();
00194 
00196 
00197     void normalizeCol(int col=-1);
00198     void normalizeSelection();
00199     void normalize();
00201 
00202     QVarLengthArray<double> col(int ycol);
00203     void columnRange(int c, double *min, double *max);
00204 
00205     int firstXCol();
00206     bool noXColumn();
00207     bool noYColumn();
00208     int colX(int col);
00209     int colY(int col, int xCol = -1);
00210 
00211     QStringList getCommands(){return commands;};
00213     void clearCommands();
00215     void setCommands(const QStringList& com);
00217     void setCommands(const QString& com);
00219     void setCommand(int col, const QString& com);
00221     bool calculate(int col, int startRow, int endRow, bool forceMuParser = false, bool notifyChanges = true);
00223     bool muParserCalculate(int col, int startRow, int endRow, bool notifyChanges = true);
00225     bool calculate();
00227     void updateValues(Table*, const QString& columnName);
00228 
00230 
00231     void deleteSelectedRows();
00232     void deleteRows(int startRow, int endRow);
00233     void insertRow();
00234     void moveRow(bool up = true);
00236 
00238 
00239     void cutSelection();
00240     void copySelection();
00241     void clearSelection();
00242     void pasteSelection();
00243     void selectAllTable();
00244     void deselect();
00245     void clear();
00247 
00248     void init(int rows, int cols);
00249     QStringList selectedColumns();
00250     QStringList selectedYColumns();
00251     QStringList selectedErrColumns();
00252     QStringList selectedYLabels();
00253     QStringList drawableColumnSelection();
00254     QStringList YColumns();
00255     int selectedColsNumber();
00256 
00257     void setColumnWidth(int width, bool allCols);
00258     void setColumnWidth(int col, int width);
00259     int columnWidth(int col);
00260     QStringList columnWidths();
00261     void setColWidths(const QStringList& widths);
00262     void adjustColumnsWidth();
00263 
00264     void setSelectedCol(int col){selectedCol = col;};
00265     int selectedColumn(){return selectedCol;};
00266     int firstSelectedColumn();
00267     int numSelectedRows();
00268     bool isRowSelected(int row, bool full=false) { return d_table->isRowSelected(row, full); }
00269     bool isColumnSelected(int col, bool full=false) { return d_table->isColumnSelected(col, full); }
00271     void goToRow(int row);
00273     void goToColumn(int col);
00274 
00275     void columnNumericFormat(int col, char *f, int *precision);
00276     void columnNumericFormat(int col, int *f, int *precision);
00277     int columnType(int col){return colTypes[col];};
00278 
00279     QList<int> columnTypes(){return colTypes;};
00280     void setColumnTypes(QList<int> ctl){colTypes = ctl;};
00281     void setColumnTypes(const QStringList& ctl);
00282     void setColumnType(int col, ColType val) { colTypes[col] = val; }
00283 
00284     void saveToMemory(double **cells){d_saved_cells = cells;};
00285     void saveToMemory();
00286     void freeMemory();
00287 
00288     bool isReadOnlyColumn(int col);
00289     void setReadOnlyColumn(int col, bool on = true);
00290 
00291     QString columnFormat(int col){return col_format[col];};
00292     QStringList getColumnsFormat(){return col_format;};
00293     void setColumnsFormat(const QStringList& lst);
00294 
00295     void setTextFormat(int col);
00296     void setColNumericFormat(int f, int prec, int col, bool updateCells = true);
00297     bool setDateFormat(const QString& format, int col, bool updateCells = true);
00298     bool setTimeFormat(const QString& format, int col, bool updateCells = true);
00299     void setMonthFormat(const QString& format, int col, bool updateCells = true);
00300     void setDayFormat(const QString& format, int col, bool updateCells = true);
00301 
00302 #ifdef XLS_IMPORT
00303         bool exportExcel(const QString& fname, bool withLabels, bool exportComments, bool exportSelection);
00304 #endif
00305     bool exportODF(const QString& fname, bool withLabels, bool exportComments, bool exportSelection);
00306     bool exportASCII(const QString& fname, const QString& separator, bool withLabels = false,
00307                      bool exportComments = false, bool exportSelection = false);
00308     void importASCII(const QString &fname, const QString &sep = "\t", int ignoredLines = 0, bool renameCols = false,
00309                     bool stripSpaces = false, bool simplifySpaces = false, bool importComments = false,
00310                     const QString& commentString = "", bool readOnly = false,
00311                     ImportMode importAs = Overwrite, const QLocale& importLocale = QLocale(), int endLine = 0, int maxRows = -1,
00312                     const QList<int>& newColTypes = QList<int>(), const QStringList& colFormats = QStringList());
00313 
00315 
00316     virtual void save(const QString &fn, const QString& geometry, bool = false);
00317     void restore(const QStringList& lst);
00318 
00319     QString saveHeader();
00320     QString saveComments();
00321     QString saveCommands();
00322     QString saveColumnWidths();
00323     QString saveColumnTypes();
00324     QString saveReadOnlyInfo();
00325     QString saveHiddenColumnsInfo();
00326 
00327     void setBackgroundColor(const QColor& col);
00328     void setTextColor(const QColor& col);
00329     void setHeaderColor(const QColor& col);
00330     void setTextFont(const QFont& fnt);
00331     void setHeaderFont(const QFont& fnt);
00332 
00333     int verticalHeaderWidth(){return d_table->verticalHeader()->width();};
00334 
00335     QString comment(int col);
00336     void setColComment(int col, const QString& s);
00337     QStringList colComments(){return comments;};
00338     void setColComments(const QStringList& lst){comments = lst;};
00339     void showComments(bool on = true);
00340     bool commentsEnabled(){return d_show_comments;}
00341 
00343     void notifyChanges();
00344     void notifyChanges(const QString& colName);
00345 
00347     void colWidthModified(int, int, int);
00348 
00349 signals:
00350     void changedColHeader(const QString&, const QString&);
00351     void removedCol(const QString&);
00352     void removedCol(int);
00353     void colIndexChanged(int, int);
00354     void modifiedData(Table *, const QString&);
00355     void optionsDialog();
00356     void colValuesDialog();
00357     void resizedTable(QWidget*);
00358     void showContextMenu(bool selection);
00359 
00360 protected:
00361     MyTable *d_table;
00362 
00363 private:
00364     void clearCol();
00365 
00366     bool d_show_comments;
00367     QStringList commands, col_format, comments, col_label;
00368     QList<int> colTypes, col_plot_type;
00369     int selectedCol;
00370     int d_numeric_precision;
00371     double **d_saved_cells;
00372 
00374     void setColumnHeader(int index, const QString& label);
00375 };
00376 
00377 #endif