2014-08-29 11:19:11 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vformula.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 28 8, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2014-08-29 11:19:11 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2014-08-29 11:19:11 +02:00
|
|
|
**
|
|
|
|
** Valentina 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.
|
|
|
|
**
|
|
|
|
** Valentina 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 Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vformula.h"
|
2019-04-17 12:02:22 +02:00
|
|
|
#include "vformula_p.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
#include <QtDebug>
|
2023-07-13 18:32:07 +02:00
|
|
|
#include <limits>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include "../qmuparser/qmuparsererror.h"
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
|
|
|
#include "../vmisc/vcommonsettings.h"
|
2015-06-18 10:45:38 +02:00
|
|
|
#include "calculator.h"
|
|
|
|
#include "vcontainer.h"
|
|
|
|
#include "vtranslatevars.h"
|
2016-05-24 14:06:35 +02:00
|
|
|
|
2023-10-07 17:56:39 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
|
|
|
|
#include "../vmisc/compatibility.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
2023-07-13 18:31:54 +02:00
|
|
|
// VFormula
|
2014-08-29 11:19:11 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VFormula::VFormula()
|
2023-07-13 18:31:54 +02:00
|
|
|
: d(new VFormulaData)
|
|
|
|
{
|
|
|
|
}
|
2014-08-29 11:19:11 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VFormula::VFormula(const QString &formula, const VContainer *container)
|
2023-07-13 18:31:54 +02:00
|
|
|
: d(new VFormulaData(formula, container))
|
|
|
|
{
|
|
|
|
}
|
2014-08-29 11:19:11 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::operator=(const VFormula &formula) -> VFormula &
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2023-07-13 18:31:54 +02:00
|
|
|
if (&formula == this)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2019-04-17 12:02:22 +02:00
|
|
|
d = formula.d;
|
2014-08-29 11:19:11 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-07-15 09:58:28 +02:00
|
|
|
COPY_CONSTRUCTOR_IMPL(VFormula)
|
2019-04-17 12:02:22 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-07-13 18:31:54 +02:00
|
|
|
VFormula::~VFormula() = default;
|
2014-08-29 11:19:11 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::operator==(const VFormula &formula) const -> bool
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->formula == formula.GetFormula() && d->strValue == formula.getStringValue() &&
|
2023-07-13 18:31:54 +02:00
|
|
|
d->checkZero == formula.getCheckZero() && d->checkLessThanZero == formula.getCheckLessThanZero() &&
|
|
|
|
d->data == formula.getData() && d->toolId == formula.getToolId() && d->postfix == formula.getPostfix() &&
|
|
|
|
d->error == formula.error() && VFuzzyComparePossibleNulls(d->dValue, formula.getDoubleValue());
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
2024-02-17 10:41:47 +01:00
|
|
|
#if __cplusplus < 202002L
|
2018-01-20 14:37:15 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::operator!=(const VFormula &formula) const -> bool
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
|
|
|
return !VFormula::operator==(formula);
|
|
|
|
}
|
2024-02-17 10:41:47 +01:00
|
|
|
#endif
|
2014-08-29 11:19:11 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::GetFormula(FormulaType type) const -> QString
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2014-08-29 15:42:53 +02:00
|
|
|
if (type == FormulaType::ToUser)
|
|
|
|
{
|
2021-02-06 14:52:21 +01:00
|
|
|
return VTranslateVars::TryFormulaToUser(d->formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
|
2014-08-29 15:42:53 +02:00
|
|
|
}
|
2023-07-13 18:31:54 +02:00
|
|
|
|
|
|
|
return d->formula;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-03 11:17:04 +01:00
|
|
|
void VFormula::SetFormula(const QString &value, FormulaType type)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
if (d->formula != value)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
if (type == FormulaType::FromUser)
|
2014-08-29 15:42:53 +02:00
|
|
|
{
|
2023-07-13 18:31:54 +02:00
|
|
|
d->formula =
|
|
|
|
VTranslateVars::TryFormulaFromUser(value, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
|
2014-08-29 15:42:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->formula = value;
|
2014-08-29 15:42:53 +02:00
|
|
|
}
|
2019-04-17 12:02:22 +02:00
|
|
|
|
|
|
|
ResetState();
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getStringValue() const -> QString
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->strValue;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 16:34:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getDoubleValue() const -> qreal
|
2014-09-01 16:34:45 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->dValue;
|
2014-09-01 16:34:45 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 11:19:11 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getCheckZero() const -> bool
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->checkZero;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::setCheckZero(bool value)
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->checkZero = value;
|
|
|
|
ResetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getCheckLessThanZero() const -> bool
|
2019-04-17 12:02:22 +02:00
|
|
|
{
|
|
|
|
return d->checkLessThanZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::setCheckLessThanZero(bool value)
|
|
|
|
{
|
|
|
|
d->checkLessThanZero = value;
|
|
|
|
ResetState();
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getData() const -> const VContainer *
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->data;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::setData(const VContainer *value)
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
if (d->data != value && value != nullptr)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->data = value;
|
|
|
|
ResetState();
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getToolId() const -> quint32
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->toolId;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-04-17 12:02:22 +02:00
|
|
|
void VFormula::setToolId(quint32 value)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->toolId = value;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::getPostfix() const -> QString
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->postfix;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::setPostfix(const QString &value)
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->postfix = value;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::error() const -> bool
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
return d->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::Reason() const -> QString
|
2019-04-17 12:02:22 +02:00
|
|
|
{
|
|
|
|
return d->reason;
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VFormula::FormulaTypeId() -> int
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
|
|
|
return qMetaTypeId<VFormula>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::Eval()
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
ResetState();
|
|
|
|
|
|
|
|
if (d->data == nullptr)
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->reason = tr("Data container is empty");
|
2014-08-29 11:19:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-02-22 14:03:11 +01:00
|
|
|
|
|
|
|
qreal result = 0;
|
|
|
|
|
2019-04-17 12:02:22 +02:00
|
|
|
if (not d->formula.isEmpty())
|
2014-08-29 11:19:11 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-02-19 17:09:56 +01:00
|
|
|
QScopedPointer<Calculator> const cal(new Calculator());
|
2019-04-17 12:02:22 +02:00
|
|
|
result = cal->EvalFormula(d->data->DataVariables(), d->formula);
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
catch (qmu::QmuParserError &e)
|
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->reason = tr("Math parser error: %1").arg(e.GetMsg());
|
2018-02-22 14:03:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:02:22 +02:00
|
|
|
d->dValue = result;
|
|
|
|
|
|
|
|
if (qIsInf(result))
|
|
|
|
{
|
|
|
|
d->reason = tr("Result is infinite");
|
|
|
|
}
|
|
|
|
else if (qIsNaN(result))
|
|
|
|
{
|
|
|
|
d->reason = tr("Result is NaN");
|
|
|
|
}
|
|
|
|
else if (d->checkZero && qFuzzyIsNull(result))
|
|
|
|
{
|
|
|
|
d->reason = tr("Result is zero");
|
|
|
|
}
|
|
|
|
else if (d->checkLessThanZero && result < 0)
|
2018-02-22 14:03:11 +01:00
|
|
|
{
|
2019-04-17 12:02:22 +02:00
|
|
|
d->reason = tr("Result less than zero");
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
2019-04-17 12:02:22 +02:00
|
|
|
else
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
d->strValue = VAbstractApplication::VApp()->LocaleToString(result) + ' '_L1 + d->postfix;
|
2019-04-17 12:02:22 +02:00
|
|
|
d->error = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->reason = tr("Formula is empty");
|
2014-08-29 11:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 12:02:22 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VFormula::ResetState()
|
|
|
|
{
|
|
|
|
d->strValue = tr("Error");
|
|
|
|
d->error = true;
|
2023-07-13 18:32:07 +02:00
|
|
|
d->dValue = std::numeric_limits<qreal>::quiet_NaN();
|
2019-04-17 12:02:22 +02:00
|
|
|
d->reason = tr("Not evaluated");
|
|
|
|
}
|