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, Rupert Seidl |
||
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 | |||
340 | werner | 20 | #include "global.h" |
21 | #include "timeevents.h" |
||
22 | #include "helper.h" |
||
23 | #include "csvfile.h" |
||
584 | werner | 24 | #include "model.h" |
340 | werner | 25 | |
26 | TimeEvents::TimeEvents() |
||
27 | { |
||
28 | } |
||
1102 | werner | 29 | static QString lastLoadedFile; |
340 | werner | 30 | bool TimeEvents::loadFromFile(const QString &fileName) |
31 | { |
||
32 | QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName)); |
||
33 | if (source.isEmpty()) |
||
34 | throw IException(QString("TimeEvents: input file does not exist or is empty (%1)").arg(fileName)); |
||
35 | lastLoadedFile=fileName; |
||
36 | return loadFromString(source); |
||
37 | } |
||
38 | |||
39 | bool TimeEvents::loadFromString(const QString &source) |
||
40 | { |
||
41 | CSVFile infile; |
||
42 | infile.loadFromString(source); |
||
43 | QStringList captions = infile.captions(); |
||
44 | int yearcol = infile.columnIndex("year"); |
||
45 | if (yearcol==-1) |
||
46 | throw IException(QString("TimeEvents: input file '%1' has no 'year' column.").arg(lastLoadedFile)); |
||
47 | int year; |
||
48 | QVariantList line; |
||
49 | QPair<QString, QVariant> entry; |
||
50 | for (int row=0;row<infile.rowCount();row++) { |
||
51 | year = infile.value(row, yearcol).toInt(); |
||
52 | line = infile.values(row); |
||
53 | for (int col=0;col<line.count();col++) { |
||
54 | if (col!=yearcol) { |
||
55 | entry.first = captions[col]; |
||
56 | entry.second = line[col]; |
||
57 | mData.insert(year, entry); |
||
58 | } |
||
59 | } |
||
60 | } // for each row |
||
61 | qDebug() << QString("loaded TimeEvents (file: %1). %2 items stored.").arg(lastLoadedFile).arg(mData.count()); |
||
62 | return true; |
||
63 | } |
||
64 | |||
341 | werner | 65 | void TimeEvents::run() |
66 | { |
||
67 | int current_year = GlobalSettings::instance()->currentYear(); |
||
68 | QList<QPair<QString, QVariant> > entries = mData.values(current_year); |
||
342 | werner | 69 | if (entries.count()==0) |
70 | return; |
||
71 | |||
341 | werner | 72 | QString key; |
73 | int values_set = 0; |
||
74 | for (int i=0;i<entries.count();i++) { |
||
75 | key = entries[i].first; // key |
||
76 | // special values: if (key=="xxx" -> |
||
584 | werner | 77 | if (key=="script" || key=="javascript") { |
78 | // execute as javascript expression within the management script context... |
||
1079 | werner | 79 | if (!entries[i].second.toString().isEmpty()) { |
80 | qDebug() << "executing Javascript time event:" << entries[i].second.toString(); |
||
767 | werner | 81 | GlobalSettings::instance()->executeJavascript(entries[i].second.toString()); |
1079 | werner | 82 | } |
584 | werner | 83 | |
84 | } else { |
||
85 | // no special value: a xml node... |
||
86 | if (! const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, entries[i].second.toString())) |
||
87 | qDebug() << "TimeEvents: Error: Key " << key << "not found! (tried to set to" << entries[i].second.toString() << ")"; |
||
88 | else |
||
89 | qDebug() << "TimeEvents: set" << key << "to" << entries[i].second.toString(); |
||
90 | } |
||
341 | werner | 91 | values_set++; |
92 | } |
||
93 | if (values_set) |
||
94 | qDebug() << "TimeEvents: year" << current_year << ":" << values_set << "values set."; |
||
95 | } |
||
977 | werner | 96 | |
97 | // read value for key 'key' and year 'year' from the list of items. |
||
98 | // return a empty QVariant if for 'year' no value is set |
||
99 | QVariant TimeEvents::value(int year, const QString &key) const |
||
100 | { |
||
101 | QMultiMap<int, QPair<QString, QVariant> >::ConstIterator it = mData.find(year); |
||
102 | while (it!=mData.constEnd()) { |
||
103 | if (it->first == key) |
||
104 | return it->second; |
||
105 | ++it; |
||
106 | } |
||
107 | return QVariant(); |
||
108 | } |