2 // C++ Implementation: settingssheet
7 // Author: Mike Taverne <mtaverne@engits.com>, (C) 2009
9 // Copyright: See COPYING file that comes with this distribution
13 #include <QMessageBox>
14 #include <QApplication>
16 #include "egvtkobject.h"
17 #include "settingssheet.h"
18 #include "vertexdelegate.h"
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
)
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()));
50 QDataStream
in(&file
);
51 in
.setVersion(QDataStream::Qt_4_1
);
55 if (magic
!= MagicNumber
) {
56 QMessageBox::warning(this, tr("SettingsSheet"),tr("The file is not a SettingsSheet file."));
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."));
77 QApplication::setOverrideCursor(Qt::WaitCursor
);
79 this->setRowCount(RowCount
);
80 this->clearContents();
82 // cout<<"===LOADING==="<<endl;
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();
94 bool SettingsSheet::writeFile(const QString
&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()));
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();
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();
128 void SettingsSheet::setFormula(int row
, int column
,
129 const QString
&formula
)
131 Cell
*c
= cell(row
, column
);
133 // Qcout2<<" (row,column)="<<"("<<row<<","<<column<<")"<<formula<<endl;
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;
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
);
157 if(column
<ColumnCount
-3){//checkbox
158 return QString::number(CheckState2int(c
->checkState()));
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
)
185 QVariant
Cell::data(int role
) const
187 if (role
== Qt::DisplayRole
) {
188 if (value().isValid()) {
189 return value().toString();
193 } else if (role
== Qt::TextAlignmentRole
) {
194 if (value().type() == QVariant::String
) {
195 return int(Qt::AlignLeft
| Qt::AlignVCenter
);
197 return int(Qt::AlignRight
| Qt::AlignVCenter
);
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()
219 const QVariant Invalid
;
221 QVariant
Cell::value() const
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
);
236 cachedValue
= evalExpression(expr
, pos
);
237 if (expr
[pos
] != QChar::Null
)
238 cachedValue
= Invalid
;
241 double d
= formulaStr
.toDouble(&ok
);
245 cachedValue
= formulaStr
;
252 QVariant
Cell::evalExpression(const QString
&str
, int &pos
) const
254 QVariant result
= evalTerm(str
, pos
);
255 while (str
[pos
] != QChar::Null
) {
257 if (op
!= '+' && op
!= '-')
261 QVariant term
= evalTerm(str
, pos
);
262 if (result
.type() == QVariant::Double
263 && term
.type() == QVariant::Double
) {
265 result
= result
.toDouble() + term
.toDouble();
267 result
= result
.toDouble() - term
.toDouble();
276 QVariant
Cell::evalTerm(const QString
&str
, int &pos
) const
278 QVariant result
= evalFactor(str
, pos
);
279 while (str
[pos
] != QChar::Null
) {
281 if (op
!= '*' && op
!= '/')
285 QVariant factor
= evalFactor(str
, pos
);
286 if (result
.type() == QVariant::Double
287 && factor
.type() == QVariant::Double
) {
289 result
= result
.toDouble() * factor
.toDouble();
291 if (factor
.toDouble() == 0.0) {
294 result
= result
.toDouble() / factor
.toDouble();
304 QVariant
Cell::evalFactor(const QString
&str
, int &pos
) const
307 bool negative
= false;
309 if (str
[pos
] == '-') {
314 if (str
[pos
] == '(') {
316 result
= evalExpression(str
, pos
);
321 QRegExp
regExp("[A-Za-z][1-9][0-9]{0,2}");
324 while (str
[pos
].isLetterOrNumber() || str
[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
));
342 result
= token
.toDouble(&ok
);
349 if (result
.type() == QVariant::Double
) {
350 result
= -result
.toDouble();