2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008,2009 Oliver Gloth +
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. +
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. +
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/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 #include <QMessageBox>
25 #include <QApplication>
28 #include "egvtkobject.h"
29 #include "utilities.h"
30 #include "settingssheet.h"
31 #include "vertexdelegate.h"
32 #include "guimainwindow.h"
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
);
61 in
>> row_count
>> column_count
;
63 if(column_count
!= this->columnCount()) {
65 QMessageBox::warning(this, tr("SettingsSheet"),tr("The file is not compatible with the number of boundary codes."));
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}}}") {
82 setFormula(row
, column
, str
);
89 void SettingsSheet::writeFile()
91 qDebug()<<"SettingsSheet::writeFile() called";
94 QTextStream
out(&buffer
, QIODevice::WriteOnly
);
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
);
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
);
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
);
124 c
->setFormula(formula
);
128 QString
SettingsSheet::formula(int row
, int column
) const
130 int ColumnCount
=this->columnCount();
131 Cell
*c
= cell(row
, column
);
133 if(column
<ColumnCount
-3){//checkbox
134 return QString::number(CheckState2int(c
->checkState()));
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
)
161 QVariant
Cell::data(int role
) const
163 if (role
== Qt::DisplayRole
) {
164 if (value().isValid()) {
165 return value().toString();
169 } else if (role
== Qt::TextAlignmentRole
) {
170 if (value().type() == QVariant::String
) {
171 return int(Qt::AlignLeft
| Qt::AlignVCenter
);
173 return int(Qt::AlignRight
| Qt::AlignVCenter
);
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()
195 const QVariant Invalid
;
197 QVariant
Cell::value() const
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(" ", "");
212 cachedValue
= evalExpression(expr
, pos
);
213 if (expr
[pos
] != 0x0000)
214 cachedValue
= Invalid
;
217 double d
= formulaStr
.toDouble(&ok
);
221 cachedValue
= formulaStr
;
228 QVariant
Cell::evalExpression(const QString
&str
, int &pos
) const
230 QVariant result
= evalTerm(str
, pos
);
231 while (str
[pos
] != 0x0000) {
233 if (op
!= '+' && op
!= '-')
237 QVariant term
= evalTerm(str
, pos
);
238 if (result
.type() == QVariant::Double
239 && term
.type() == QVariant::Double
) {
241 result
= result
.toDouble() + term
.toDouble();
243 result
= result
.toDouble() - term
.toDouble();
252 QVariant
Cell::evalTerm(const QString
&str
, int &pos
) const
254 QVariant result
= evalFactor(str
, pos
);
255 while (str
[pos
] != 0x0000) {
257 if (op
!= '*' && op
!= '/')
261 QVariant factor
= evalFactor(str
, pos
);
262 if (result
.type() == QVariant::Double
263 && factor
.type() == QVariant::Double
) {
265 result
= result
.toDouble() * factor
.toDouble();
267 if (factor
.toDouble() == 0.0) {
270 result
= result
.toDouble() / factor
.toDouble();
280 QVariant
Cell::evalFactor(const QString
&str
, int &pos
) const
283 bool negative
= false;
285 if (str
[pos
] == '-') {
290 if (str
[pos
] == '(') {
292 result
= evalExpression(str
, pos
);
297 QRegExp
regExp("[A-Za-z][1-9][0-9]{0,2}");
300 while (str
[pos
].isLetterOrNumber() || str
[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
));
318 result
= token
.toDouble(&ok
);
325 if (result
.type() == QVariant::Double
) {
326 result
= -result
.toDouble();