2011-10-20 17:29:22 +02:00
|
|
|
/*
|
|
|
|
Copyright 2010-2011 Grégory Soutadé
|
|
|
|
|
|
|
|
This file is part of KissCount.
|
|
|
|
|
|
|
|
KissCount is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
KissCount is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include "FormulaDelegate.hpp"
|
|
|
|
#include <ParseExp.hpp>
|
|
|
|
#include "../wxUI.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
QWidget * FormulaDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
|
|
|
|
{
|
|
|
|
return new QLineEdit(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormulaDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QLineEdit *line = qobject_cast<QLineEdit *>(editor);
|
|
|
|
QString s ;
|
|
|
|
double res = 0.0;
|
|
|
|
QString value = line->text();
|
|
|
|
struct ParseExp::parse_opt opt, *r;
|
|
|
|
const char* c;
|
|
|
|
char* str, *str2;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
value = value.trimmed();
|
|
|
|
if (value.startsWith("="))
|
|
|
|
{
|
|
|
|
value = value.replace(",", ".");
|
|
|
|
c = value.toStdString().c_str();
|
|
|
|
str2 = str = new char[strlen(c)];
|
|
|
|
strcpy(str, c+1);
|
|
|
|
memset(&opt, 0, sizeof(opt));
|
|
|
|
r = &opt;
|
|
|
|
try {
|
|
|
|
ParseExp::ParseExp(&str, r, false);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Invalid formula !"));
|
|
|
|
delete[] str2;
|
|
|
|
return;
|
|
|
|
}
|
2011-10-22 09:57:20 +02:00
|
|
|
|
|
|
|
FormulaDelegate* _this = const_cast<FormulaDelegate*>(this); // hum hum ...
|
|
|
|
_this->_operations->at(index.row()).formula = value;
|
2011-10-20 17:29:22 +02:00
|
|
|
delete[] str2;
|
|
|
|
res = ParseExp::EvaluateExpr(&opt, true);
|
|
|
|
model->setData(index, qVariantFromValue(s.sprintf("%.2lf", res)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = value.toDouble(&ok);
|
|
|
|
if (ok)
|
|
|
|
model->setData(index, qVariantFromValue(s.sprintf("%.2lf", res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormulaDelegate::setEditorData(QWidget *editor,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QLineEdit* line = qobject_cast<QLineEdit *>(editor);
|
|
|
|
QString s = qVariantValue<QString>(index.data());
|
|
|
|
|
2011-10-22 09:57:20 +02:00
|
|
|
if ((*_operations)[index.row()].formula.length())
|
|
|
|
line->setText((*_operations)[index.row()].formula);
|
2011-10-20 17:29:22 +02:00
|
|
|
else
|
|
|
|
line->setText(s);
|
|
|
|
}
|