QtiPlot 0.9.8.2
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 minColumnValue(int col, int startRow = 0, int endRow = -1);
00085     double maxColumnValue(int col, int startRow = 0, int endRow = -1);
00086     Table* extractData(const QString& name, const QString& condition, int startRow = 0, int endRow = -1);
00087     static QDateTime dateTime(double val);
00088     static double fromDateTime(const QDateTime& dt);
00089     static double fromTime(const QTime& t);
00090 
00091 public slots:
00092     MyTable* table(){return d_table;};
00093     void copy(Table *m, bool values = true);
00094     int numRows();
00095     int numCols();
00096     void setNumRows(int rows);
00097     void setNumCols(int cols);
00098     void resizeRows(int);
00099     void resizeCols(int);
00100 
00102     double cell(int row, int col);
00103     void setCell(int row, int col, double val);
00104 
00105     QString text(int row, int col);
00106     QStringList columnsList();
00107     QStringList colNames(){return col_label;}
00108     QString colName(int col);
00109     void setColName(int col, const QString& text, bool enumerateRight = false, bool warn = true);
00110     QString colLabel(int col);
00111     int colIndex(const QString& name);
00112 
00113     int colPlotDesignation(int col){return col_plot_type[col];};
00114     void setColPlotDesignation(int col, PlotDesignation pd);
00115     void setPlotDesignation(PlotDesignation pd, bool rightColumns = false);
00116     QList<int> plotDesignations(){return col_plot_type;};
00117 
00118     void setHeader(QStringList header);
00119     void loadHeader(QStringList header);
00120     void setHeaderColType();
00121     void setText(int row,int col,const QString & text);
00122     void setRandomValues();
00123     void setRandomValues(int col, int startRow = 0, int endRow = -1);
00124     void setNormalRandomValues();
00125     void setNormalRandomValues(int col, int startRow = 0, int endRow = -1, double sigma = 1.0);
00126     void setAscValues();
00127 
00128     void cellEdited(int,int col);
00129     void moveCurrentCell();
00130     void clearCell(int row, int col);
00131     bool isEmptyRow(int row);
00132     bool isEmptyColumn(int col);
00133     int nonEmptyRows();
00134 
00135     void print();
00136     void print(QPrinter *);
00137     void print(const QString& fileName);
00138     void exportPDF(const QString& fileName);
00139 
00141 
00142     bool eventFilter(QObject *object, QEvent *e);
00143     void customEvent( QEvent* e);
00145 
00147 
00148     void removeCol();
00149     void removeCol(const QStringList& list);
00150     void insertCol();
00151     virtual void insertCols(int start, int count);
00152     virtual void addCol(PlotDesignation pd = Y);
00153     void addColumns(int c);
00154     virtual void moveColumn(int, int, int);
00155     void swapColumns(int, int);
00156     void moveColumnBy(int cols);
00157     void hideSelectedColumns();
00158     void showAllColumns();
00159     void hideColumn(int col, bool = true);
00160     bool isColumnHidden(int col){return d_table->isColumnHidden(col);};
00162 
00164 
00165 
00168     void sortColAsc();
00172     void sortColDesc();
00177     void sortColumn(int col = -1, int order = 0);
00182     void sortTableDialog();
00184     void sort(int type = 0, int order  = 0, const QString& leadCol = QString());
00186     void sortColumns(int type = 0, int order = 0, const QString& leadCol = QString());
00193     void sortColumns(const QStringList& cols, int type = 0, int order = 0, const QString& leadCol = QString());
00198     void sortColumnsDialog();
00200 
00202 
00203     void normalizeCol(int col=-1);
00204     void normalizeSelection();
00205     void normalize();
00207 
00208     QVarLengthArray<double> col(int ycol);
00209     void columnRange(int c, double *min, double *max);
00210 
00211     int firstXCol();
00212     bool noXColumn();
00213     bool noYColumn();
00214     int colX(int col);
00215     int colY(int col, int xCol = -1);
00216 
00217     QStringList getCommands(){return commands;};
00219     void clearCommands();
00221     void setCommands(const QStringList& com);
00223     void setCommands(const QString& com);
00225     void setCommand(int col, const QString& com);
00227     bool calculate(int col, int startRow, int endRow, bool forceMuParser = false, bool notifyChanges = true);
00229     bool muParserCalculate(int col, int startRow, int endRow, bool notifyChanges = true);
00231     bool calculate();
00233     void updateValues(Table*, const QString& columnName);
00234 
00236 
00237     void deleteSelectedRows();
00238     void deleteRows(int startRow, int endRow);
00239     void insertRow();
00240     void moveRow(bool up = true);
00242 
00244 
00245     void cutSelection();
00246     void copySelection();
00247     void clearSelection();
00248     void pasteSelection();
00249     void selectAllTable();
00250     void deselect();
00251     void clear();
00253 
00254     void init(int rows, int cols);
00255     QStringList writableSelectedColumns();
00256     QStringList selectedColumns();
00257     QStringList selectedYColumns();
00258     QStringList selectedErrColumns();
00259     QStringList selectedYLabels();
00260     QStringList drawableColumnSelection();
00261     QStringList YColumns();
00262     int selectedColsNumber();
00263 
00264     void setColumnWidth(int width, bool allCols);
00265     void setColumnWidth(int col, int width);
00266     int columnWidth(int col);
00267     QStringList columnWidths();
00268     void setColWidths(const QStringList& widths);
00269     void adjustColumnsWidth(bool selection = true);
00270 
00271     void setSelectedCol(int col){selectedCol = col;};
00272     int selectedColumn(){return selectedCol;};
00273     int firstSelectedColumn();
00274     int numSelectedRows();
00275     bool isRowSelected(int row, bool full=false) { return d_table->isRowSelected(row, full); }
00276     bool isColumnSelected(int col, bool full=false) { return d_table->isColumnSelected(col, full); }
00278     void goToRow(int row);
00280     void goToColumn(int col);
00281 
00282     void columnNumericFormat(int col, char *f, int *precision);
00283     void columnNumericFormat(int col, int *f, int *precision);
00284     int columnType(int col){return colTypes[col];};
00285 
00286     QList<int> columnTypes(){return colTypes;};
00287     void setColumnTypes(QList<int> ctl){colTypes = ctl;};
00288     void setColumnTypes(const QStringList& ctl);
00289     void setColumnType(int col, ColType val) { colTypes[col] = val; }
00290 
00291     void saveToMemory(double **cells){d_saved_cells = cells;};
00292     void saveToMemory();
00293     void freeMemory();
00294 
00295     bool isReadOnlyColumn(int col);
00296     void setReadOnlyColumn(int col, bool on = true);
00297 
00298     QString columnFormat(int col){return col_format[col];};
00299     QStringList getColumnsFormat(){return col_format;};
00300     void setColumnsFormat(const QStringList& lst);
00301 
00302     void setTextFormat(int col);
00303     void setColNumericFormat(int col);
00304     void setColNumericFormat(int f, int prec, int col, bool updateCells = true);
00305     bool setDateFormat(const QString& format, int col, bool updateCells = true);
00306     bool setTimeFormat(const QString& format, int col, bool updateCells = true);
00307     void setMonthFormat(const QString& format, int col, bool updateCells = true);
00308     void setDayFormat(const QString& format, int col, bool updateCells = true);
00309 
00310     bool exportExcel(const QString& fname, bool withLabels, bool exportComments, bool exportSelection);
00311     bool exportOdsSpreadsheet(const QString& fname, bool withLabels, bool exportComments, bool exportSelection);
00312     bool exportODF(const QString& fname, bool withLabels, bool exportComments, bool exportSelection);
00313     bool exportASCII(const QString& fname, const QString& separator, bool withLabels = false,
00314                      bool exportComments = false, bool exportSelection = false);
00315     void importASCII(const QString &fname, const QString &sep = "\t", int ignoredLines = 0, bool renameCols = false,
00316                     bool stripSpaces = false, bool simplifySpaces = false, bool importComments = false,
00317                     const QString& commentString = "", bool readOnly = false,
00318                     ImportMode importAs = Overwrite, const QLocale& importLocale = QLocale(), int endLine = 0, int maxRows = -1,
00319                     const QList<int>& newColTypes = QList<int>(), const QStringList& colFormats = QStringList());
00320 
00322 
00323     virtual void save(const QString &fn, const QString& geometry, bool = false);
00324     void restore(const QStringList& lst);
00325 
00326     QString saveHeader();
00327     QString saveComments();
00328     QString saveCommands();
00329     QString saveColumnWidths();
00330     QString saveColumnTypes();
00331     QString saveReadOnlyInfo();
00332     QString saveHiddenColumnsInfo();
00333 
00334     void setBackgroundColor(const QColor& col);
00335     void setTextColor(const QColor& col);
00336     void setHeaderColor(const QColor& col);
00337     void setTextFont(const QFont& fnt);
00338     void setHeaderFont(const QFont& fnt);
00339 
00340     int verticalHeaderWidth(){return d_table->verticalHeader()->width();};
00341 
00342     QString comment(int col);
00343     void setColComment(int col, const QString& s);
00344     QStringList colComments(){return comments;};
00345     void setColComments(const QStringList& lst){comments = lst;};
00346     void showComments(bool on = true);
00347     bool commentsEnabled(){return d_show_comments;}
00348 
00350     void notifyChanges();
00351     void notifyChanges(const QString& colName);
00352 
00354     void colWidthModified(int, int, int);
00355 
00356 signals:
00357     void changedColHeader(const QString&, const QString&);
00358     void removedCol(const QString&);
00359     void addedCol(const QString&);
00360     void removedCol(int);
00361     void colIndexChanged(int, int);
00362     void modifiedData(Table *, const QString&);
00363     void optionsDialog();
00364     void colValuesDialog();
00365     void resizedTable(QWidget*);
00366     void showContextMenu(bool selection);
00367 
00368 protected:
00369     MyTable *d_table;
00370 
00371 private:
00372     void clearCol();
00373 
00374     bool d_show_comments;
00375     QStringList commands, col_format, comments, col_label;
00376     QList<int> colTypes, col_plot_type;
00377     int selectedCol;
00378     int d_numeric_precision;
00379     double **d_saved_cells;
00380 
00382     void setColumnHeader(int index, const QString& label);
00383 };
00384 
00385 #endif