Rev 1221 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1221 | Rev 1222 | ||
---|---|---|---|
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/output/outputmanager.cpp': |
1 | Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/output/outputmanager.cpp': |
2 | /********************************************************************************************
|
2 | /********************************************************************************************
|
3 | ** iLand - an individual based forest landscape and disturbance model
|
3 | ** iLand - an individual based forest landscape and disturbance model
|
4 | ** http://iland.boku.ac.at
|
4 | ** http://iland.boku.ac.at
|
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl
|
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl
|
6 | **
|
6 | **
|
7 | ** This program is free software: you can redistribute it and/or modify
|
7 | ** This program is free software: you can redistribute it and/or modify
|
8 | ** it under the terms of the GNU General Public License as published by
|
8 | ** it under the terms of the GNU General Public License as published by
|
9 | ** the Free Software Foundation, either version 3 of the License, or
|
9 | ** the Free Software Foundation, either version 3 of the License, or
|
10 | ** (at your option) any later version.
|
10 | ** (at your option) any later version.
|
11 | **
|
11 | **
|
12 | ** This program is distributed in the hope that it will be useful,
|
12 | ** This program is distributed in the hope that it will be useful,
|
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | ** GNU General Public License for more details.
|
15 | ** GNU General Public License for more details.
|
16 | **
|
16 | **
|
17 | ** You should have received a copy of the GNU General Public License
|
17 | ** You should have received a copy of the GNU General Public License
|
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 | ********************************************************************************************/
|
19 | ********************************************************************************************/
|
20 | 20 | ||
21 | /** @class OutputManager
|
21 | /** @class OutputManager
|
22 | Global container that handles data output.
|
22 | Global container that handles data output.
|
23 | 23 | ||
24 | */
|
24 | */
|
25 | 25 | ||
26 | #include "global.h"
|
26 | #include "global.h"
|
27 | #include "outputmanager.h"
|
27 | #include "outputmanager.h"
|
28 | #include "debugtimer.h"
|
28 | #include "debugtimer.h"
|
29 | #include <QtCore>
|
29 | #include <QtCore>
|
30 | 30 | ||
31 | // tree outputs
|
31 | // tree outputs
|
32 | #include "treeout.h"
|
32 | #include "treeout.h"
|
33 | #include "standout.h"
|
33 | #include "standout.h"
|
34 | #include "landscapeout.h"
|
34 | #include "landscapeout.h"
|
35 | #include "standdeadout.h"
|
35 | #include "standdeadout.h"
|
36 | #include "managementout.h"
|
36 | #include "managementout.h"
|
37 | #include "dynamicstandout.h"
|
37 | #include "dynamicstandout.h"
|
38 | #include "productionout.h"
|
38 | #include "productionout.h"
|
39 | #include "saplingout.h"
|
39 | #include "saplingout.h"
|
40 | #include "carbonout.h"
|
40 | #include "carbonout.h"
|
41 | #include "carbonflowout.h"
|
41 | #include "carbonflowout.h"
|
42 | #include "waterout.h"
|
42 | #include "waterout.h"
|
43 | 43 | ||
44 | 44 | ||
45 | // on creation of the output manager
|
45 | // on creation of the output manager
|
46 | // an instance of every iLand output
|
46 | // an instance of every iLand output
|
47 | // must be added to the list of outputs.
|
47 | // must be added to the list of outputs.
|
48 | OutputManager::OutputManager() |
48 | OutputManager::OutputManager() |
49 | {
|
49 | {
|
50 | mTransactionOpen = false; |
50 | mTransactionOpen = false; |
51 | // add all the outputs
|
51 | // add all the outputs
|
52 | mOutputs.append(new TreeOut); |
52 | mOutputs.append(new TreeOut); |
53 | mOutputs.append(new TreeRemovedOut); |
53 | mOutputs.append(new TreeRemovedOut); |
54 | mOutputs.append(new StandOut); |
54 | mOutputs.append(new StandOut); |
55 | mOutputs.append(new LandscapeOut); |
55 | mOutputs.append(new LandscapeOut); |
56 | mOutputs.append(new LandscapeRemovedOut); |
56 | mOutputs.append(new LandscapeRemovedOut); |
57 | mOutputs.append(new DynamicStandOut); |
57 | mOutputs.append(new DynamicStandOut); |
58 | mOutputs.append(new ProductionOut); |
58 | mOutputs.append(new ProductionOut); |
59 | mOutputs.append(new StandDeadOut); |
59 | mOutputs.append(new StandDeadOut); |
60 | mOutputs.append(new ManagementOut); |
60 | mOutputs.append(new ManagementOut); |
61 | mOutputs.append(new SaplingOut); |
61 | mOutputs.append(new SaplingOut); |
62 | mOutputs.append(new SaplingDetailsOut); |
62 | mOutputs.append(new SaplingDetailsOut); |
63 | mOutputs.append(new CarbonOut); |
63 | mOutputs.append(new CarbonOut); |
64 | mOutputs.append(new CarbonFlowOut); |
64 | mOutputs.append(new CarbonFlowOut); |
65 | mOutputs.append(new WaterOut); |
65 | mOutputs.append(new WaterOut); |
66 | }
|
66 | }
|
67 | 67 | ||
68 | void OutputManager::addOutput(Output *output) |
68 | void OutputManager::addOutput(Output *output) |
69 | {
|
69 | {
|
70 | mOutputs.append(output); |
70 | mOutputs.append(output); |
71 | }
|
71 | }
|
72 | 72 | ||
73 | void OutputManager::removeOutput(const QString &tableName) |
73 | void OutputManager::removeOutput(const QString &tableName) |
74 | {
|
74 | {
|
75 | Output *o = find(tableName); |
75 | Output *o = find(tableName); |
76 | if (o) { |
76 | if (o) { |
77 | mOutputs.removeAt(mOutputs.indexOf(o)); |
77 | mOutputs.removeAt(mOutputs.indexOf(o)); |
78 | delete o; |
78 | delete o; |
79 | }
|
79 | }
|
80 | }
|
80 | }
|
81 | 81 | ||
82 | 82 | ||
83 | OutputManager::~OutputManager() |
83 | OutputManager::~OutputManager() |
84 | {
|
84 | {
|
85 | qDeleteAll(mOutputs); |
85 | qDeleteAll(mOutputs); |
86 | }
|
86 | }
|
87 | 87 | ||
88 | void OutputManager::setup() |
88 | void OutputManager::setup() |
89 | {
|
89 | {
|
90 | //close();
|
90 | //close();
|
91 | XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings()); |
91 | XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings()); |
92 | QString nodepath;
|
92 | QString nodepath;
|
93 | foreach(Output *o, mOutputs) { |
93 | foreach(Output *o, mOutputs) { |
94 | nodepath = QString("output.%1").arg(o->tableName()); |
94 | nodepath = QString("output.%1").arg(o->tableName()); |
95 | xml.setCurrentNode(nodepath); |
95 | xml.setCurrentNode(nodepath); |
96 | qDebug() << "setup of output" << o->name(); |
96 | qDebug() << "setup of output" << o->name(); |
97 | o->setup(); |
97 | o->setup(); |
98 | bool enabled = xml.valueBool(".enabled", false); |
98 | bool enabled = xml.valueBool(".enabled", false); |
99 | o->setEnabled(enabled); |
99 | o->setEnabled(enabled); |
100 | if (enabled) |
100 | if (enabled) |
101 | o->open(); |
101 | o->open(); |
102 | }
|
102 | }
|
103 | endTransaction(); // just to be sure |
103 | endTransaction(); // just to be sure |
104 | }
|
104 | }
|
105 | 105 | ||
106 | Output *OutputManager::find(const QString& tableName) |
106 | Output *OutputManager::find(const QString& tableName) |
107 | {
|
107 | {
|
108 | foreach(Output* p,mOutputs) |
108 | foreach(Output* p,mOutputs) |
109 | if (p->tableName()==tableName) |
109 | if (p->tableName()==tableName) |
110 | return p; |
110 | return p; |
111 | return NULL; |
111 | return NULL; |
112 | }
|
112 | }
|
113 | 113 | ||
114 | void OutputManager::save() |
114 | void OutputManager::save() |
115 | {
|
115 | {
|
116 | endTransaction(); |
116 | endTransaction(); |
117 | }
|
117 | }
|
118 | 118 | ||
119 | void OutputManager::close() |
119 | void OutputManager::close() |
120 | {
|
120 | {
|
121 | qDebug() << "outputs closed"; |
121 | qDebug() << "outputs closed"; |
122 | foreach(Output *p, mOutputs) |
122 | foreach(Output *p, mOutputs) |
123 | p->close(); |
123 | p->close(); |
124 | }
|
124 | }
|
125 | 125 | ||
126 | /** start a database transaction.
|
126 | /** start a database transaction.
|
127 | do nothing if transaction is already open. */
|
127 | do nothing if transaction is already open. */
|
128 | void OutputManager::startTransaction() |
128 | void OutputManager::startTransaction() |
129 | {
|
129 | {
|
130 | if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) { |
130 | if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) { |
131 | if (GlobalSettings::instance()->dbout().transaction()) { |
131 | if (GlobalSettings::instance()->dbout().transaction()) { |
132 | qDebug() << "opening transaction"; |
132 | qDebug() << "opening transaction"; |
133 | mTransactionOpen = true; |
133 | mTransactionOpen = true; |
134 | }
|
134 | }
|
135 | }
|
135 | }
|
136 | }
|
136 | }
|
137 | void OutputManager::endTransaction() |
137 | void OutputManager::endTransaction() |
138 | {
|
138 | {
|
139 | if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) { |
139 | if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) { |
140 | if (GlobalSettings::instance()->dbout().commit()) { |
140 | if (GlobalSettings::instance()->dbout().commit()) { |
141 | mTransactionOpen = false; |
141 | mTransactionOpen = false; |
142 | qDebug() << "database transaction commited"; |
142 | qDebug() << "database transaction commited"; |
143 | }
|
143 | }
|
144 | }
|
144 | }
|
145 | }
|
145 | }
|
146 | 146 | ||
147 | bool OutputManager::execute(const QString& tableName) |
147 | bool OutputManager::execute(const QString& tableName) |
148 | {
|
148 | {
|
149 | DebugTimer t("OutputManager::execute()"); |
149 | DebugTimer t("OutputManager::execute()"); |
150 | t.setSilent(); |
150 | t.setSilent(); |
151 | Output *p = find(tableName); |
151 | Output *p = find(tableName); |
152 | if (p) { |
152 | if (p) { |
153 | if (!p->isEnabled()) |
153 | if (!p->isEnabled()) |
154 | return false; |
154 | return false; |
155 | if(!p->isOpen()) |
155 | if(!p->isOpen()) |
156 | return false; |
156 | return false; |
157 | if (!p->isRowEmpty()) { |
157 | if (!p->isRowEmpty()) { |
158 | qWarning() << "Output" << p->name() << "invalid (not at new row)!!!"; |
158 | qWarning() << "Output" << p->name() << "invalid (not at new row)!!!"; |
159 | return false; |
159 | return false; |
160 | }
|
160 | }
|
161 | 161 | ||
162 | startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction |
162 | startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction |
163 | p->exec(); |
163 | p->exec(); |
164 | 164 | ||
165 | return true; |
165 | return true; |
166 | }
|
166 | }
|
167 | qDebug() << "output" << tableName << "not found!"; |
167 | qDebug() << "output" << tableName << "not found!"; |
168 | return false; // no output found |
168 | return false; // no output found |
169 | }
|
169 | }
|
170 | 170 | ||
171 | QString OutputManager::wikiFormat() |
171 | QString OutputManager::wikiFormat() |
172 | {
|
172 | {
|
173 | QString result;
|
173 | QString result;
|
174 | foreach(const Output *o, mOutputs) |
174 | foreach(const Output *o, mOutputs) |
175 | result+=o->wikiFormat() + "\n\n"; |
175 | result+=o->wikiFormat() + "\n\n"; |
176 | return result; |
176 | return result; |
177 | }
|
177 | }
|
178 | 178 | ||
179 | 179 |