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