/* Copyright 2010-2012 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 . */ #include #include #include "FormulaDelegate.hpp" #include #include "../wxUI.hpp" #include 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(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; } FormulaDelegate* _this = const_cast(this); // hum hum ... _this->_operations->at(index.row()).formula = value; 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(editor); QString s = qVariantValue(index.data()); if ((*_operations)[index.row()].formula.length()) line->setText((*_operations)[index.row()].formula); else line->setText(s); }