valentina/src/libs/vpatterndb/vformula.cpp

288 lines
9.0 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vformula.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 28 8, 2014
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2013-2015 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** 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"
#include "vformula_p.h"
#include <QMessageLogger>
#include <QScopedPointer>
#include <QtDebug>
2023-07-13 18:32:07 +02:00
#include <limits>
#include "../qmuparser/qmuparsererror.h"
#include "../vmisc/def.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vcommonsettings.h"
#include "calculator.h"
#include "vcontainer.h"
#include "vtranslatevars.h"
#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
//---------------------------------------------------------------------------------------------------------------------
VFormula::VFormula()
2023-07-13 18:31:54 +02:00
: d(new VFormulaData)
{
}
//---------------------------------------------------------------------------------------------------------------------
VFormula::VFormula(const QString &formula, const VContainer *container)
2023-07-13 18:31:54 +02:00
: d(new VFormulaData(formula, container))
{
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::operator=(const VFormula &formula) -> VFormula &
{
2023-07-13 18:31:54 +02:00
if (&formula == this)
{
return *this;
}
d = formula.d;
return *this;
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-15 09:58:28 +02:00
COPY_CONSTRUCTOR_IMPL(VFormula)
//---------------------------------------------------------------------------------------------------------------------
2023-07-13 18:31:54 +02:00
VFormula::~VFormula() = default;
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::operator==(const VFormula &formula) const -> bool
{
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());
}
#if __cplusplus < 202002L
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::operator!=(const VFormula &formula) const -> bool
{
return !VFormula::operator==(formula);
}
#endif
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::GetFormula(FormulaType type) const -> QString
{
if (type == FormulaType::ToUser)
{
2021-02-06 14:52:21 +01:00
return VTranslateVars::TryFormulaToUser(d->formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
2023-07-13 18:31:54 +02:00
return d->formula;
}
//---------------------------------------------------------------------------------------------------------------------
2015-02-03 11:17:04 +01:00
void VFormula::SetFormula(const QString &value, FormulaType type)
{
if (d->formula != value)
{
if (type == FormulaType::FromUser)
{
2023-07-13 18:31:54 +02:00
d->formula =
VTranslateVars::TryFormulaFromUser(value, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
else
{
d->formula = value;
}
ResetState();
}
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getStringValue() const -> QString
{
return d->strValue;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getDoubleValue() const -> qreal
{
return d->dValue;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getCheckZero() const -> bool
{
return d->checkZero;
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::setCheckZero(bool value)
{
d->checkZero = value;
ResetState();
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getCheckLessThanZero() const -> bool
{
return d->checkLessThanZero;
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::setCheckLessThanZero(bool value)
{
d->checkLessThanZero = value;
ResetState();
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getData() const -> const VContainer *
{
return d->data;
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::setData(const VContainer *value)
{
if (d->data != value && value != nullptr)
{
d->data = value;
ResetState();
}
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getToolId() const -> quint32
{
return d->toolId;
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::setToolId(quint32 value)
{
d->toolId = value;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::getPostfix() const -> QString
{
return d->postfix;
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::setPostfix(const QString &value)
{
d->postfix = value;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::error() const -> bool
{
return d->error;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::Reason() const -> QString
{
return d->reason;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VFormula::FormulaTypeId() -> int
{
return qMetaTypeId<VFormula>();
}
//---------------------------------------------------------------------------------------------------------------------
void VFormula::Eval()
{
ResetState();
if (d->data == nullptr)
{
d->reason = tr("Data container is empty");
return;
}
qreal result = 0;
if (not d->formula.isEmpty())
{
try
{
QScopedPointer<Calculator> cal(new Calculator());
result = cal->EvalFormula(d->data->DataVariables(), d->formula);
}
catch (qmu::QmuParserError &e)
{
d->reason = tr("Math parser error: %1").arg(e.GetMsg());
return;
}
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)
{
d->reason = tr("Result less than zero");
}
else
{
d->strValue = VAbstractApplication::VApp()->LocaleToString(result) + ' '_L1 + d->postfix;
d->error = false;
}
}
else
{
d->reason = tr("Formula is empty");
}
}
//---------------------------------------------------------------------------------------------------------------------
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();
d->reason = tr("Not evaluated");
}