DeleteSetOfPoints function working
[engrid.git] / settingssheet.cpp
blob0d7221797feffdcf740f72c91c40b7f1ba12ecd3
1 //
2 // C++ Implementation: settingssheet
3 //
4 // Description:
5 //
6 //
7 // Author: Mike Taverne <mtaverne@engits.com>, (C) 2009
8 //
9 // Copyright: See COPYING file that comes with this distribution
12 #include <QFile>
13 #include <QMessageBox>
14 #include <QApplication>
15 #include <QtGui>
16 #include "egvtkobject.h"
17 #include "settingssheet.h"
18 #include "vertexdelegate.h"
19 #include <iostream>
20 using namespace std;
22 /* Here is how we we get QTextStreams that look like iostreams */
23 QTextStream Qcin2(stdin, QIODevice::ReadOnly);
24 QTextStream Qcout2(stdout, QIODevice::WriteOnly);
25 QTextStream Qcerr2(stderr, QIODevice::WriteOnly);
27 SettingsSheet::SettingsSheet(QWidget *parent)
28 : QTableWidget(parent)
33 SettingsSheet::~SettingsSheet()
37 Cell *SettingsSheet::cell(int row, int column) const
39 return static_cast<Cell *>(item(row, column));
42 bool SettingsSheet::readFile(const QString &fileName,int verbose)
44 QFile file(fileName);
45 if (!file.open(QIODevice::ReadOnly)) {
46 if(verbose>0) QMessageBox::warning(this, tr("SettingsSheet"),tr("Cannot read file %1:\n%2.").arg(file.fileName()).arg(file.errorString()));
47 return false;
50 QDataStream in(&file);
51 in.setVersion(QDataStream::Qt_4_1);
53 quint32 magic;
54 in >> magic;
55 if (magic != MagicNumber) {
56 QMessageBox::warning(this, tr("SettingsSheet"),tr("The file is not a SettingsSheet file."));
57 return false;
60 int RowCount=0;
61 int ColumnCount=0;
63 in >> RowCount;
64 in >> ColumnCount;
65 /* cout<<"RowCount="<<RowCount<<endl;
66 cout<<"ColumnCount="<<ColumnCount<<endl;*/
68 if(ColumnCount!=this->columnCount()) {
69 if(verbose>0) QMessageBox::warning(this, tr("SettingsSheet"),tr("The file is not compatible with the number of boundary codes."));
70 return false;
73 quint16 row;
74 quint16 column;
75 QString str;
77 QApplication::setOverrideCursor(Qt::WaitCursor);
79 this->setRowCount(RowCount);
80 this->clearContents();
82 // cout<<"===LOADING==="<<endl;
83 while (!in.atEnd()) {
84 in >> row >> column >> str;
85 // Qcout2<<"row="<<row<<"column="<<column<<"str="<<str<<endl;
86 setFormula(row, column, str);
88 // cout<<"===LOADING DONE==="<<endl;
90 QApplication::restoreOverrideCursor();
91 return true;
94 bool SettingsSheet::writeFile(const QString &fileName)
96 QFile file(fileName);
97 if (!file.open(QIODevice::WriteOnly)) {
98 QMessageBox::warning(this, tr("SettingsSheet"),
99 tr("Cannot write file %1:\n%2.")
100 .arg(file.fileName())
101 .arg(file.errorString()));
102 return false;
105 QDataStream out(&file);
106 out.setVersion(QDataStream::Qt_4_1);
108 out << quint32(MagicNumber);
110 QApplication::setOverrideCursor(Qt::WaitCursor);
111 int RowCount=this->rowCount();
112 int ColumnCount=this->columnCount();
113 out << RowCount;
114 out << ColumnCount;
115 // cout<<"===SAVING==="<<endl;
116 for (int row = 0; row < RowCount; ++row) {
117 for (int column = 0; column < ColumnCount; ++column) {
118 QString str = formula(row, column);
119 out << quint16(row) << quint16(column) << str;
120 // Qcout2 << quint16(row) <<" "<< quint16(column) <<" "<< str <<endl;
123 // cout<<"===SAVING DONE==="<<endl;
124 QApplication::restoreOverrideCursor();
125 return true;
128 void SettingsSheet::setFormula(int row, int column,
129 const QString &formula)
131 Cell *c = cell(row, column);
132 if (!c) {
133 // Qcout2<<" (row,column)="<<"("<<row<<","<<column<<")"<<formula<<endl;
134 c = new Cell;
135 setItem(row, column, c);
137 if(column<this->columnCount()-3){
138 // cout<<" checkbox"<<endl;
139 TriStateTableWidgetItem *newBC = new TriStateTableWidgetItem();
140 newBC->setFlags(Qt::ItemIsTristate | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
141 newBC->setCheckState(int2CheckState(formula.toInt()));
142 this->setItem(row, column, newBC);
143 // Qcout2<<" (row,column)="<<"("<<row<<","<<column<<")"<<formula<<endl;
145 else{
146 // cout<<" string"<<endl;
147 c->setFormula(formula);
151 QString SettingsSheet::formula(int row, int column) const
153 int RowCount=this->rowCount();
154 int ColumnCount=this->columnCount();
155 Cell *c = cell(row, column);
156 if (c) {
157 if(column<ColumnCount-3){//checkbox
158 return QString::number(CheckState2int(c->checkState()));
160 else{
161 return c->formula();
163 } else {
164 return "";
168 Cell::Cell()
170 setDirty();
173 QTableWidgetItem *Cell::clone() const
175 return new Cell(*this);
178 void Cell::setData(int role, const QVariant &value)
180 QTableWidgetItem::setData(role, value);
181 if (role == Qt::EditRole)
182 setDirty();
185 QVariant Cell::data(int role) const
187 if (role == Qt::DisplayRole) {
188 if (value().isValid()) {
189 return value().toString();
190 } else {
191 return "####";
193 } else if (role == Qt::TextAlignmentRole) {
194 if (value().type() == QVariant::String) {
195 return int(Qt::AlignLeft | Qt::AlignVCenter);
196 } else {
197 return int(Qt::AlignRight | Qt::AlignVCenter);
199 } else {
200 return QTableWidgetItem::data(role);
204 void Cell::setFormula(const QString &formula)
206 setData(Qt::EditRole, formula);
209 QString Cell::formula() const
211 return data(Qt::EditRole).toString();
214 void Cell::setDirty()
216 cacheIsDirty = true;
219 const QVariant Invalid;
221 QVariant Cell::value() const
223 if (cacheIsDirty) {
224 cacheIsDirty = false;
226 QString formulaStr = formula();
227 if (formulaStr.startsWith('\'')) {
228 cachedValue = formulaStr.mid(1);
229 } else if (formulaStr.startsWith('=')) {
230 cachedValue = Invalid;
231 QString expr = formulaStr.mid(1);
232 expr.replace(" ", "");
233 expr.append(QChar::Null);
235 int pos = 0;
236 cachedValue = evalExpression(expr, pos);
237 if (expr[pos] != QChar::Null)
238 cachedValue = Invalid;
239 } else {
240 bool ok;
241 double d = formulaStr.toDouble(&ok);
242 if (ok) {
243 cachedValue = d;
244 } else {
245 cachedValue = formulaStr;
249 return cachedValue;
252 QVariant Cell::evalExpression(const QString &str, int &pos) const
254 QVariant result = evalTerm(str, pos);
255 while (str[pos] != QChar::Null) {
256 QChar op = str[pos];
257 if (op != '+' && op != '-')
258 return result;
259 ++pos;
261 QVariant term = evalTerm(str, pos);
262 if (result.type() == QVariant::Double
263 && term.type() == QVariant::Double) {
264 if (op == '+') {
265 result = result.toDouble() + term.toDouble();
266 } else {
267 result = result.toDouble() - term.toDouble();
269 } else {
270 result = Invalid;
273 return result;
276 QVariant Cell::evalTerm(const QString &str, int &pos) const
278 QVariant result = evalFactor(str, pos);
279 while (str[pos] != QChar::Null) {
280 QChar op = str[pos];
281 if (op != '*' && op != '/')
282 return result;
283 ++pos;
285 QVariant factor = evalFactor(str, pos);
286 if (result.type() == QVariant::Double
287 && factor.type() == QVariant::Double) {
288 if (op == '*') {
289 result = result.toDouble() * factor.toDouble();
290 } else {
291 if (factor.toDouble() == 0.0) {
292 result = Invalid;
293 } else {
294 result = result.toDouble() / factor.toDouble();
297 } else {
298 result = Invalid;
301 return result;
304 QVariant Cell::evalFactor(const QString &str, int &pos) const
306 QVariant result;
307 bool negative = false;
309 if (str[pos] == '-') {
310 negative = true;
311 ++pos;
314 if (str[pos] == '(') {
315 ++pos;
316 result = evalExpression(str, pos);
317 if (str[pos] != ')')
318 result = Invalid;
319 ++pos;
320 } else {
321 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
322 QString token;
324 while (str[pos].isLetterOrNumber() || str[pos] == '.') {
325 token += str[pos];
326 ++pos;
329 if (regExp.exactMatch(token)) {
330 int column = token[0].toUpper().unicode() - 'A';
331 int row = token.mid(1).toInt() - 1;
333 Cell *c = static_cast<Cell *>(
334 tableWidget()->item(row, column));
335 if (c) {
336 result = c->value();
337 } else {
338 result = 0.0;
340 } else {
341 bool ok;
342 result = token.toDouble(&ok);
343 if (!ok)
344 result = Invalid;
348 if (negative) {
349 if (result.type() == QVariant::Double) {
350 result = -result.toDouble();
351 } else {
352 result = Invalid;
355 return result;