deactivated deprecated start_engrid copy
[engrid.git] / src / settingssheet.cpp
bloba393447e89b21769d5b812801e333b0271839131
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include <QFile>
24 #include <QMessageBox>
25 #include <QApplication>
26 #include <QtGui>
28 #include "egvtkobject.h"
29 #include "utilities.h"
30 #include "settingssheet.h"
31 #include "vertexdelegate.h"
32 #include "guimainwindow.h"
34 #include <iostream>
37 using namespace std;
39 SettingsSheet::SettingsSheet(QWidget *parent)
40 : QTableWidget(parent)
45 SettingsSheet::~SettingsSheet()
49 Cell *SettingsSheet::cell(int row, int column) const
51 return static_cast<Cell *>(item(row, column));
54 bool SettingsSheet::readFile(int verbose)
56 QString buffer = GuiMainWindow::pointer()->getXmlSection("engrid/surface/table");
57 QTextStream in(&buffer, QIODevice::ReadOnly);
58 int row_count = 0;
59 int column_count = 0;
61 in >> row_count >> column_count;
63 if(column_count != this->columnCount()) {
64 if(verbose > 0) {
65 QMessageBox::warning(this, tr("SettingsSheet"),tr("The file is not compatible with the number of boundary codes."));
67 return false;
70 int row, column;
71 QString str;
73 this->setRowCount(row_count);
74 this->clearContents();
76 for (int i = 0; i < row_count; ++i) {
77 for (int j = 0; j < column_count; ++j) {
78 in >> row >> column >> str;
79 if (str == "{{{empty}}}") {
80 str = "";
82 setFormula(row, column, str);
86 return true;
89 void SettingsSheet::writeFile()
91 qDebug()<<"SettingsSheet::writeFile() called";
92 QString buffer = "";
94 QTextStream out(&buffer, QIODevice::WriteOnly);
95 out << "\n";
96 out << rowCount() << " " << columnCount() << "\n";
97 for (int row = 0; row < rowCount(); ++row) {
98 for (int column = 0; column < columnCount(); ++column) {
99 QString str = formula(row, column);
100 if (str.isEmpty()) {
101 str = "{{{empty}}}";
103 out << row << " " << column << " " << str << "\n";
107 GuiMainWindow::pointer()->setXmlSection("engrid/surface/table", buffer);
110 void SettingsSheet::setFormula(int row, int column, const QString &formula)
112 Cell *c = cell(row, column);
113 if (!c) {
114 c = new Cell;
115 setItem(row, column, c);
117 if(column<this->columnCount()-3){
118 TriStateTableWidgetItem *newBC = new TriStateTableWidgetItem();
119 newBC->setFlags(Qt::ItemIsTristate | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
120 newBC->setCheckState(int2CheckState(formula.toInt()));
121 this->setItem(row, column, newBC);
123 else{
124 c->setFormula(formula);
128 QString SettingsSheet::formula(int row, int column) const
130 int ColumnCount=this->columnCount();
131 Cell *c = cell(row, column);
132 if (c) {
133 if(column<ColumnCount-3){//checkbox
134 return QString::number(CheckState2int(c->checkState()));
136 else{
137 return c->formula();
139 } else {
140 return "";
144 Cell::Cell()
146 setDirty();
149 QTableWidgetItem *Cell::clone() const
151 return new Cell(*this);
154 void Cell::setData(int role, const QVariant &value)
156 QTableWidgetItem::setData(role, value);
157 if (role == Qt::EditRole)
158 setDirty();
161 QVariant Cell::data(int role) const
163 if (role == Qt::DisplayRole) {
164 if (value().isValid()) {
165 return value().toString();
166 } else {
167 return "####";
169 } else if (role == Qt::TextAlignmentRole) {
170 if (value().type() == QVariant::String) {
171 return int(Qt::AlignLeft | Qt::AlignVCenter);
172 } else {
173 return int(Qt::AlignRight | Qt::AlignVCenter);
175 } else {
176 return QTableWidgetItem::data(role);
180 void Cell::setFormula(const QString &formula)
182 setData(Qt::EditRole, formula);
185 QString Cell::formula() const
187 return data(Qt::EditRole).toString();
190 void Cell::setDirty()
192 cacheIsDirty = true;
195 const QVariant Invalid;
197 QVariant Cell::value() const
199 if (cacheIsDirty) {
200 cacheIsDirty = false;
202 QString formulaStr = formula();
203 if (formulaStr.startsWith('\'')) {
204 cachedValue = formulaStr.mid(1);
205 } else if (formulaStr.startsWith('=')) {
206 cachedValue = Invalid;
207 QString expr = formulaStr.mid(1);
208 expr.replace(" ", "");
209 expr.append(0x0000);
211 int pos = 0;
212 cachedValue = evalExpression(expr, pos);
213 if (expr[pos] != 0x0000)
214 cachedValue = Invalid;
215 } else {
216 bool ok;
217 double d = formulaStr.toDouble(&ok);
218 if (ok) {
219 cachedValue = d;
220 } else {
221 cachedValue = formulaStr;
225 return cachedValue;
228 QVariant Cell::evalExpression(const QString &str, int &pos) const
230 QVariant result = evalTerm(str, pos);
231 while (str[pos] != 0x0000) {
232 QChar op = str[pos];
233 if (op != '+' && op != '-')
234 return result;
235 ++pos;
237 QVariant term = evalTerm(str, pos);
238 if (result.type() == QVariant::Double
239 && term.type() == QVariant::Double) {
240 if (op == '+') {
241 result = result.toDouble() + term.toDouble();
242 } else {
243 result = result.toDouble() - term.toDouble();
245 } else {
246 result = Invalid;
249 return result;
252 QVariant Cell::evalTerm(const QString &str, int &pos) const
254 QVariant result = evalFactor(str, pos);
255 while (str[pos] != 0x0000) {
256 QChar op = str[pos];
257 if (op != '*' && op != '/')
258 return result;
259 ++pos;
261 QVariant factor = evalFactor(str, pos);
262 if (result.type() == QVariant::Double
263 && factor.type() == QVariant::Double) {
264 if (op == '*') {
265 result = result.toDouble() * factor.toDouble();
266 } else {
267 if (factor.toDouble() == 0.0) {
268 result = Invalid;
269 } else {
270 result = result.toDouble() / factor.toDouble();
273 } else {
274 result = Invalid;
277 return result;
280 QVariant Cell::evalFactor(const QString &str, int &pos) const
282 QVariant result;
283 bool negative = false;
285 if (str[pos] == '-') {
286 negative = true;
287 ++pos;
290 if (str[pos] == '(') {
291 ++pos;
292 result = evalExpression(str, pos);
293 if (str[pos] != ')')
294 result = Invalid;
295 ++pos;
296 } else {
297 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
298 QString token;
300 while (str[pos].isLetterOrNumber() || str[pos] == '.') {
301 token += str[pos];
302 ++pos;
305 if (regExp.exactMatch(token)) {
306 int column = token[0].toUpper().unicode() - 'A';
307 int row = token.mid(1).toInt() - 1;
309 Cell *c = static_cast<Cell *>(
310 tableWidget()->item(row, column));
311 if (c) {
312 result = c->value();
313 } else {
314 result = 0.0;
316 } else {
317 bool ok;
318 result = token.toDouble(&ok);
319 if (!ok)
320 result = Invalid;
324 if (negative) {
325 if (result.type() == QVariant::Double) {
326 result = -result.toDouble();
327 } else {
328 result = Invalid;
331 return result;