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 | |||
15 | Werner | 20 | #include "helper.h" |
49 | Werner | 21 | #include <QtCore> |
780 | werner | 22 | #ifdef ILAND_GUI |
23 | #if QT_VERSION < 0x050000 |
||
93 | Werner | 24 | #include <QtGui> |
780 | werner | 25 | #else |
26 | #include <QtWidgets> |
||
27 | #endif |
||
28 | #endif |
||
72 | Werner | 29 | //#include "cycle.h" |
145 | Werner | 30 | #include <limits> |
15 | Werner | 31 | |
947 | werner | 32 | #ifdef ILAND_GUI |
989 | werner | 33 | #include <QColor> |
202 | werner | 34 | #include "version.h" |
947 | werner | 35 | #else |
1182 | werner | 36 | #include "version.h" |
947 | werner | 37 | #endif |
202 | werner | 38 | |
15 | Werner | 39 | Helper::Helper() |
40 | { |
||
41 | } |
||
42 | |||
199 | werner | 43 | QString Helper::currentRevision() |
44 | { |
||
203 | werner | 45 | //QString cur_revision="$Revision: 202 $"; |
323 | werner | 46 | QString cur_revision = QString(svnRevision()); |
202 | werner | 47 | return cur_revision; //.section(" ",1,1); |
199 | werner | 48 | |
49 | } |
||
50 | |||
15 | Werner | 51 | QString Helper::loadTextFile(const QString& fileName) |
52 | { |
||
53 | QFile file(fileName); |
||
54 | |||
55 | if (!file.open(QIODevice::ReadOnly)) { |
||
56 | return ""; |
||
57 | } |
||
58 | QTextStream s(&file); |
||
16 | Werner | 59 | //s.setCodec("UTF-8"); |
15 | Werner | 60 | QString result=s.readAll(); |
61 | return result; |
||
62 | } |
||
63 | |||
64 | void Helper::saveToTextFile(const QString& fileName, const QString& text) |
||
65 | { |
||
66 | QFile file(fileName); |
||
67 | |||
68 | if (!file.open(QIODevice::WriteOnly)) { |
||
69 | return; |
||
70 | } |
||
71 | QTextStream s(&file); |
||
72 | s << text; |
||
73 | } |
||
151 | iland | 74 | QByteArray Helper::loadFile(const QString& fileName) |
75 | { |
||
76 | QFile file(fileName); |
||
15 | Werner | 77 | |
151 | iland | 78 | if (!file.open(QIODevice::ReadOnly)) { |
79 | return QByteArray(); |
||
80 | } |
||
81 | QTextStream s(&file); |
||
82 | QByteArray result; |
||
83 | s >> result; |
||
84 | |||
85 | return result; |
||
86 | } |
||
87 | |||
88 | void Helper::saveToFile(const QString &fileName, const QByteArray &data) |
||
89 | { |
||
90 | QFile file(fileName); |
||
91 | |||
92 | if (!file.open(QIODevice::WriteOnly)) { |
||
93 | return; |
||
94 | } |
||
95 | QTextStream s(&file); |
||
96 | s << data; |
||
97 | } |
||
98 | |||
702 | werner | 99 | /// ask the user for a input value |
100 | QString Helper::userValue(const QString &message, const QString defaultValue, QWidget *parent) |
||
101 | { |
||
780 | werner | 102 | #ifdef ILAND_GUI |
702 | werner | 103 | bool ok; |
104 | QString result = QInputDialog::getText(parent, "input data", message, QLineEdit::Normal, defaultValue, &ok); |
||
105 | if (ok) |
||
106 | return result; |
||
107 | else |
||
108 | return defaultValue; |
||
780 | werner | 109 | #else |
110 | Q_UNUSED(defaultValue); Q_UNUSED(message); Q_UNUSED(parent); |
||
111 | return QString("not availabile in non-gui-mode"); |
||
112 | #endif |
||
702 | werner | 113 | } |
151 | iland | 114 | |
15 | Werner | 115 | void Helper::msg(const QString &message, QWidget *parent) |
116 | { |
||
780 | werner | 117 | #ifdef ILAND_GUI |
120 | Werner | 118 | QMessageBox::information(parent, "iLand", message); |
780 | werner | 119 | #else |
120 | Q_UNUSED(message); Q_UNUSED(parent); |
||
121 | #endif |
||
15 | Werner | 122 | } |
123 | |||
124 | bool Helper::question(const QString &message, QWidget *parent) |
||
125 | { |
||
780 | werner | 126 | #ifdef ILAND_GUI |
120 | Werner | 127 | return QMessageBox::question(parent, "iLand", message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes; |
780 | werner | 128 | #else |
129 | Q_UNUSED(message); Q_UNUSED(parent); |
||
130 | return false; |
||
131 | #endif |
||
15 | Werner | 132 | } |
133 | |||
1053 | werner | 134 | QString Helper::fileDialog(const QString &title, const QString &start_directory, const QString &filter, QWidget *parent) |
54 | Werner | 135 | { |
780 | werner | 136 | #ifdef ILAND_GUI |
480 | werner | 137 | QString the_filter = filter; |
138 | if (the_filter.isEmpty()) |
||
139 | the_filter = "All files (*.*)"; |
||
140 | else |
||
141 | the_filter += ";;All files (*.*)"; // as 2nd filter |
||
142 | |||
1053 | werner | 143 | QString fileName = QFileDialog::getOpenFileName(parent, |
480 | werner | 144 | title, start_directory, the_filter); |
780 | werner | 145 | #else |
1099 | werner | 146 | Q_UNUSED(title); Q_UNUSED(start_directory); Q_UNUSED(filter); Q_UNUSED(parent); |
780 | werner | 147 | QString fileName="undefined"; |
148 | #endif |
||
54 | Werner | 149 | return fileName; |
150 | } |
||
15 | Werner | 151 | |
152 | void Helper::openHelp(const QString& topic) |
||
153 | { |
||
780 | werner | 154 | #ifdef ILAND_GUI |
15 | Werner | 155 | QUrl url; |
156 | qDebug() << "current path" << QDir::currentPath(); |
||
157 | url.setUrl(QString("file:///%1/help/%2.html").arg(QDir::currentPath(),topic) , QUrl::TolerantMode); |
||
158 | qDebug() << url; |
||
159 | if (url.isValid()) |
||
160 | qDebug() << "url is valid"; |
||
161 | QDesktopServices::openUrl(url); |
||
780 | werner | 162 | #else |
163 | Q_UNUSED(topic); |
||
164 | #endif |
||
15 | Werner | 165 | } |
166 | |||
167 | QString Helper::stripHtml(const QString &source) |
||
168 | { |
||
169 | QString str = source.simplified(); |
||
170 | return str.replace(QRegExp("<[^>]+>"),""); |
||
171 | } |
||
172 | |||
173 | |||
174 | // debugging |
||
175 | bool Helper::m_quiet = true; |
||
176 | bool Helper::m_NoDebug = false; |
||
177 | |||
178 | |||
40 | Werner | 179 | |
180 | |||
15 | Werner | 181 | /** UpdateState. |
182 | |||
183 | */ |
||
184 | void UpdateState::invalidate(bool self) |
||
185 | { |
||
186 | if (self) |
||
187 | mVal++; |
||
188 | foreach (UpdateState *s, mChilds) |
||
189 | s->invalidate(true); |
||
190 | } |
||
191 | |||
192 | void UpdateState::saveState(UpdateState *state) |
||
193 | { |
||
194 | mSavedStates[state]=state->mVal; |
||
195 | } |
||
196 | bool UpdateState::hasChanged(UpdateState *state) |
||
197 | { |
||
198 | if (!mSavedStates.contains(state)) |
||
199 | return true; |
||
200 | qDebug() << "UpdateState::hasChanged: saved: " << mSavedStates[state] << "current: " << state->mVal; |
||
201 | return mSavedStates[state] != state->mVal; |
||
202 | } |
||
203 | // set internal state to the current state |
||
204 | void UpdateState::update() |
||
205 | { |
||
206 | mCurrentVal = mVal; |
||
207 | } |
||
208 | // check if state needs update |
||
209 | bool UpdateState::needsUpdate() |
||
210 | { |
||
211 | return mVal > mCurrentVal; |
||
212 | } |
||
213 | |||
19 | Werner | 214 | |
72 | Werner | 215 | |
216 |