Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
671 | werner | 1 | /******************************************************************************************** |
2 | ** iLand - an individual based forest landscape and disturbance model |
||
3 | ** http://iland.boku.ac.at |
||
4 | ** Copyright (C) 2009- Werner Rammer |
||
5 | ** |
||
6 | ** This program is free software: you can redistribute it and/or modify |
||
7 | ** it under the terms of the GNU General Public License as published by |
||
8 | ** the Free Software Foundation, either version 3 of the License, or |
||
9 | ** (at your option) any later version. |
||
10 | ** |
||
11 | ** This program is distributed in the hope that it will be useful, |
||
12 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | ** GNU General Public License for more details. |
||
15 | ** |
||
16 | ** You should have received a copy of the GNU General Public License |
||
17 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | ********************************************************************************************/ |
||
19 | |||
243 | werner | 20 | #ifndef CSVFILE_H |
21 | #define CSVFILE_H |
||
22 | |||
23 | #include <QObject> |
||
827 | werner | 24 | #include <QStringList> |
793 | werner | 25 | #include <QJSEngine> |
243 | werner | 26 | class CSVFile : public QObject |
27 | { |
||
28 | Q_OBJECT |
||
766 | werner | 29 | Q_PROPERTY(bool captions WRITE setHasCaptions READ hasCaptions) ///< if true, the first line are considered to be headers |
30 | Q_PROPERTY(bool flat WRITE setFlat READ flat) ///< if true, there is only one column (a flat file) |
||
31 | Q_PROPERTY(int colCount READ colCount) |
||
32 | Q_PROPERTY(int rowCount READ rowCount) |
||
243 | werner | 33 | public: |
245 | werner | 34 | CSVFile(QObject *parent=0); |
500 | werner | 35 | CSVFile(const QString &fileName) { mHasCaptions = true; mFlat = false; mFixedWidth=false; loadFile(fileName);} ///< ctor, load @p fileName. |
243 | werner | 36 | // actions |
37 | bool openFile(const QString &fileName); ///< open file in streaming mode. |
||
245 | werner | 38 | QVariant colValue(const int col); ///< get value of column with index @p col. Use in streaming mode. |
243 | werner | 39 | bool next(); ///< advance to next record (i.e. line). return false if end of file is reached. |
40 | // properties |
||
258 | werner | 41 | bool streamingMode() const { return mStreamingMode; } ///< return true, if in "streaming mode" (for large files) |
42 | bool hasCaptions() const { return mHasCaptions; } ///< true, if first line contains headers |
||
250 | werner | 43 | bool flat() const { return mFlat; } ///< simple list, not multiple columns |
243 | werner | 44 | int rowCount() const { return mRowCount; } ///< number or rows (excl. captions), or -1. |
45 | int colCount() const { return mColCount; } ///< number of columns, or -1 |
||
1208 | werner | 46 | bool isEmpty() const { return mIsEmpty; } /// returns true when no valid file has been loaded (returns false when a file with 0 rows is loaded) |
280 | werner | 47 | QStringList captions() const { return mCaptions; } ///< retrieve (a copy) of column headers |
48 | QStringList column(const int col) const; ///< retrieve a string list of a given row |
||
340 | werner | 49 | QVariantList values(const int row) const; ///< get a list of the values in row "row" |
243 | werner | 50 | // setters |
51 | void setHasCaptions(const bool hasCaps) { mHasCaptions = hasCaps; } |
||
500 | werner | 52 | void setFixedWidth(const bool hasFixedWidth) { mFixedWidth = hasFixedWidth; } |
250 | werner | 53 | void setFlat(const bool isflat) { mFlat = isflat; } |
793 | werner | 54 | static void addToScriptEngine(QJSEngine &engine); // called during setup of ScriptEngine |
245 | werner | 55 | public slots: |
56 | bool loadFile(const QString &fileName); ///< load @p fileName. load the complete file at once. |
||
346 | werner | 57 | bool loadFromString(const QString &content); ///< load content from a given string. |
245 | werner | 58 | QString columnName(const int col) { if (col<mColCount) return mCaptions[col]; return QString(); } ///< get caption of ith column. |
258 | werner | 59 | int columnIndex(const QString &columnName) const { return mCaptions.indexOf(columnName); } ///< index of column or -1 if not available |
566 | werner | 60 | // value function with a column name |
61 | QVariant value(const int row, const QString column_name) const { return value(row, columnIndex(column_name)); } |
||
62 | |||
280 | werner | 63 | QVariant value(const int row, const int col) const; ///< get value of cell denoted by @p row and @p cell. Not available in streaming mode. |
245 | werner | 64 | QVariant row(const int row); ///< retrieve content of the full row @p row as a Variant |
243 | werner | 65 | |
500 | werner | 66 | void setValue(const int row, const int col, QVariant value); ///< set the value of the column |
67 | void saveFile(const QString &fileName); ///< save the current content to a file |
||
68 | |||
243 | werner | 69 | private: |
70 | void clear(); |
||
1208 | werner | 71 | bool mIsEmpty; |
243 | werner | 72 | bool mHasCaptions; |
500 | werner | 73 | bool mFixedWidth; |
250 | werner | 74 | bool mFlat; |
243 | werner | 75 | bool mStreamingMode; |
76 | QStringList mCaptions; |
||
77 | QStringList mRows; |
||
78 | QString mSeparator; |
||
79 | int mRowCount; |
||
80 | int mColCount; |
||
81 | }; |
||
82 | |||
83 | #endif // CSVFILE_H |