diff --git a/src/app/container/calculator.cpp b/src/app/container/calculator.cpp index 39d1700dd..930a570f5 100644 --- a/src/app/container/calculator.cpp +++ b/src/app/container/calculator.cpp @@ -28,15 +28,7 @@ #include "calculator.h" #include - -#define DELIMITER 1 -#define VARIABLE 2 -#define NUMBER 3 -#define COMMAND 4 -#define STRING 5 -#define QUOTE 6 -#define FINISHED 10 -#define EOL 9 +#include "../widgets/vapplication.h" //--------------------------------------------------------------------------------------------------------------------- /** @@ -44,433 +36,187 @@ * @param data pointer to a variable container. */ Calculator::Calculator(const VContainer *data) - :errorMsg(nullptr), token(QString()), tok(0), token_type(0), prog(QString()), index(0), data(data), - debugFormula(QString()) -{} + :QmuParser() +{ + //String with all unique symbols for supported alpabets. + // See script alphabets.py for generation and more information. + const QString symbols = QStringLiteral("ցЀĆЈVӧĎАғΕĖӅИқΝĞơРңњΥĦШҫ̆جگĮаҳѕεشԶиһνԾрυلՆӝшËՎҔPÓՖXӛӟŞӣզhëծpóӞնxßվāŁЃֆĉЋCŬđ" + "ҐГΒęҘЛΚŘġҠУGاհЫدԱҰгβطԹõлκKՁÀуςهՉÈыvیՑÐSOřӘћաőcӐթèkàѓżűðsķչøӥӔĀփїІĈЎґĐΗЖҙĘȚ" + "ΟОҡĠآΧЦتЮұİزηжԸغοоÁՀقχцÉՈيюÑՐђӋіәťӆўáŠĺѐfөըnñŰӤӨӹոľЁրăЉŭċБӸēłΔҖЙŤěΜӜDСձģΤӰ" + "ЩīņحҮбưԳصδHйԻŇμӲӴсՃمτƠщՋєLQŹՓŕÖYśÞaգĽæiŽիӓîqճöyջþĂօЄӦĊЌΑĒДҗјΙȘĚМΡéĵĢФūӚΩبĪ" + "ЬүќαذԲдҷιظԺмρՂфÇωوՊьÏՒTŚĻJբdçժlïӪղtպӫAւąЇčŃЏĕӯЗΖEțŮĝПΞأĥĹЧΦثÆӳЯIسŲԵзζԽпξكՅ" + "ÄчφNMՍӌяӢՕÔWÎŝÜџёźեägխoӒյôwĶBžսüЂĄև̈ЊČƏљΓВҕĔӮΛКĜΣТҥĤکЪƯخγвŅԴŪضλкԼĴσтÅՄنъÍՌR" + "ӕՔZÝŜbåդﻩjíլļrӵմzýռپêЅքćچЍďӱҒЕůėژșΘØҚНğńءΠFҢХħΨҪЭųįҶرҲеԷňعθҺнԿفπÂхՇψÊэšՏÒU" + "əÚѝŻşҤӑâeէŐımկòuշÕúտŔ"); + + // Defining identifier character sets + DefineNameChars(QStringLiteral("0123456789_") + symbols); + DefineOprtChars(symbols + QStringLiteral("+-*^/?<>=#!$%&|~'_")); + + // Add variables + InitVariables(data); + + // Add unary operators + DefinePostfixOprt(cm_Oprt, CmUnit); + DefinePostfixOprt(mm_Oprt, MmUnit); + DefinePostfixOprt(in_Oprt, InchUnit); +} //--------------------------------------------------------------------------------------------------------------------- /** * @brief eval calculate formula. - * @param prog string of formula. - * @param errorMsg keep error message. + * @param formula string of formula. * @return value of formula. */ -qreal Calculator::eval(QString prog, QString *errorMsg) +qreal Calculator::EvalFormula(const QString &formula) { - this->errorMsg = errorMsg; - this->errorMsg->clear(); - debugFormula.clear(); - this->prog = prog; - //qDebug()<<"Formula: "<patternType() == Pattern::Standard) { - serror(2); - return 0; + DefineVar(data->SizeName(), data->size()); + DefineVar(data->HeightName(), data->height()); } - level2(&result); - putback(); - return result; -} -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief level2 method of addition and subtraction of two terms. - * @param result result of operation. - */ -void Calculator::level2(qreal *result) -{ - QChar op; - qreal hold; - - level3(result); - while ((op=token[0]) == '+' || op == '-') { - get_token(); - level3(&hold); - arith(op, result, &hold); - } -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief level3 method of multiplication, division, finding percent. - * @param result result of operation. - */ -void Calculator::level3(qreal *result) -{ - QChar op; - qreal hold; - - level4(result); - - while ((op = token[0]) == '*' || op == '/' || op == '%') - { - get_token(); - level4(&hold); - arith(op, result, &hold); - } -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief level4 method of degree two numbers. - * @param result result of operation. - */ -void Calculator::level4(qreal *result) -{ - qreal hold; - - level5(result); - if (token[0] == '^') - { - get_token(); - level4(&hold); - arith('^', result, &hold); - } -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief level5 method for finding unary plus or minus. - * @param result result of operation. - */ -void Calculator::level5(qreal *result) -{ - QChar op; - - op = '\0'; - if ((token_type==DELIMITER) && (token[0]=='+' || token[0]=='-')) - { - op = token[0]; - get_token(); - } - level6(result); - if (op != '\0') - { - unary(op, result); - } -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief level6 processing method of the expression in brackets. - * @param result result of operation. - */ -void Calculator::level6(qreal *result) -{ - if ((token[0] == '(') && (token_type == DELIMITER)) - { - get_token(); - level2(result); - if (token[0] != ')') + const QHash *lengthLines = data->DataLengthLines(); + QHash::const_iterator i = lengthLines->constBegin(); + while (i != lengthLines->constEnd()) { - serror(1); + DefineVar(i.key(), i.value()); + ++i; } - get_token(); - } else - primitive(result); -} + } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief primitive method of determining the value of a variable by its name. - * @param result result of operation. - */ -void Calculator::primitive(qreal *result) -{ - QString str; - switch (token_type) { - case VARIABLE: - *result = find_var(token); - str = QString("%1").arg(*result, 0, 'f', 3); - debugFormula.append(str); - get_token(); - return; - case NUMBER: - *result = token.toDouble(); - str = QString("%1").arg(*result, 0, 'f', 3); - debugFormula.append(str); - get_token(); - return; - default: - serror(0); + const QHash *lengthSplines = data->DataLengthSplines(); + QHash::const_iterator i = lengthSplines->constBegin(); + while (i != lengthSplines->constEnd()) + { + DefineVar(i.key(), i.value()); + ++i; + } + } + + { + const QHash *lengthArcs = data->DataLengthArcs(); + QHash::const_iterator i = lengthArcs->constBegin(); + while (i != lengthArcs->constEnd()) + { + DefineVar(i.key(), i.value()); + ++i; + } + } + + { + const QHash *lineAngles = data->DataLineAngles(); + QHash::const_iterator i = lineAngles->constBegin(); + while (i != lineAngles->constEnd()) + { + DefineVar(i.key(), i.value()); + ++i; + } + } + + { + const QHash *measurements = data->DataMeasurements(); + QHash::const_iterator i = measurements->constBegin(); + while (i != measurements->constEnd()) + { + if (qApp->patternType() == Pattern::Standard) + { + DefineVar(i.key(), i.value().GetValue(data->size(), data->height())); + } + else + { + DefineVar(i.key(), i.value().GetValue()); + } + ++i; + } + } + + { + const QHash *increments = data->DataIncrements(); + QHash::const_iterator i = increments->constBegin(); + while (i != increments->constEnd()) + { + if (qApp->patternType() == Pattern::Standard) + { + DefineVar(i.key(), i.value().GetValue(data->size(), data->height())); + } + else + { + DefineVar(i.key(), i.value().GetValue()); + } + ++i; + } } } //--------------------------------------------------------------------------------------------------------------------- -/** - * @brief arith perform the specified arithmetic. The result is written to the first element. - * @param o sign of operation. - * @param r first element. - * @param h second element. - */ -void Calculator::arith(QChar o, qreal *r, qreal *h) +qreal Calculator::CmUnit(qreal val) { - qreal t;//, ex; - - switch (o.toLatin1()) + qreal unit = val; + switch(qApp->patternUnit()) { - case '-': - *r = *r-*h; + case Valentina::Mm: + unit = val * 10.0; break; - case '+': - *r = *r+*h; + case Valentina::Cm: break; - case '*': - *r = *r * *h; - break; - case '/': - *r = (*r)/(*h); - break; - case '%': - t = (*r)/(*h); - *r = *r-(t*(*h)); - break; - case '^': - *r = pow(*r, *h); + case Valentina::Inch: + unit = val / 2.54; break; default: break; } + + return unit; } //--------------------------------------------------------------------------------------------------------------------- -/** - * @brief unary method changes the sign. - * @param o sign of symbol. - * @param r element. - */ -void Calculator::unary(QChar o, qreal *r) +qreal Calculator::MmUnit(qreal val) { - if (o=='-') + qreal unit = val; + switch(qApp->patternUnit()) { - *r = -(*r); + case Valentina::Mm: + break; + case Valentina::Cm: + unit = val / 10.0; + break; + case Valentina::Inch: + unit = val / 25.4; + break; + default: + break; } + + return unit; } //--------------------------------------------------------------------------------------------------------------------- -/** - * @brief find_var method is finding variable by name. - * @param s name of variable. - * @return value of variable. - */ -qreal Calculator::find_var(QString s) +qreal Calculator::InchUnit(qreal val) { - bool ok = false; - qreal value = data->FindVar(s, &ok); - if (ok == false) + qreal unit = val; + switch(qApp->patternUnit()) { - qDebug()<clear(); - *errorMsg = e[error]; - qDebug()</*%^=()", c) || c=='\n' || c=='\r' || c=='\0') - { - return true; - } - return false; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief iswhite checks whether c space or tab. - * @param c character. - * @return true - space or tab, false - don't space and don't tab. - */ -bool Calculator::iswhite(QChar c) -{ - if (c==' ' || c=='\t') - { - return true; - } - else - { - return false; - } -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief get_token return next token. - */ -void Calculator::get_token() -{ - QString *temp; - - token_type=0; tok=0; - token.clear(); - temp=&token; - - if (prog[index]=='\0') - { /* end of file */ - token="\0"; - tok=FINISHED; - token_type=DELIMITER; - return; - } - - while (iswhite(prog[index])) - { - ++index; /* skip spaces */ - } - - if (prog[index]=='\r') - { /* crtl */ - ++index; ++index; - tok= EOL; token='\r'; - token.append('\n');token.append("\0"); - token_type = DELIMITER; - return; - } - - if (StrChr("+-*^/%=;(),><", prog[index])) - { /* delimiter */ - *temp=prog[index]; - index++; /* jump to the next position */ - temp->append("\0"); - token_type=DELIMITER; - debugFormula.append(token); - return; - } - if (prog[index]=='"') - { /* quoted string */ - index++; - while (prog[index] != '"' && prog[index] != '\r') - { - temp->append(prog[index]); - index++; - } - if (prog[index]=='\r') - { - serror(1); - } - index++;temp->append("\0"); - token_type=QUOTE; - return; - } - if (prog[index].isDigit()) - { /* number */ - while (isdelim(prog[index]) == false) - { - temp->append(prog[index]); - index++; - } - temp->append('\0'); - token_type = NUMBER; - return; - } - - if (prog[index].isPrint()) - { /* variable or command */ - while (isdelim(prog[index]) == false) - { - temp->append(prog[index]); - index++; - } - token_type=STRING; - } - temp->append("\0"); - - /* Seen if there is a command line or a variable */ - if (token_type==STRING) - { - tok=look_up(token); - if (tok == false) - { - token_type = VARIABLE; - } - else - { - token_type = COMMAND; /* It is command */ - } - } - return; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief StrChr checks whether the character belongs to the line. - * @param string string with formula - * @param c character. - * @return true - belongs to the line, false - don't belongs to the line. - */ -bool Calculator::StrChr(QString string, QChar c) -{ - return string.contains(c, Qt::CaseInsensitive); -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief putback returns the readout token back into the flow. - */ -void Calculator::putback() -{ - QString t; - t = token; - index = index - t.size(); + + return unit; } diff --git a/src/app/container/calculator.h b/src/app/container/calculator.h index 51e074865..6b978f7f1 100644 --- a/src/app/container/calculator.h +++ b/src/app/container/calculator.h @@ -31,67 +31,20 @@ #include "vcontainer.h" -/** - * @brief The Calculator class calculate formulas of pattern. Support operation +,-,/,* and braces. - * Can replace name of variables her value. - */ -class Calculator +#include "../../libs/qmuparser/qmuparser.h" +using namespace qmu; + +class Calculator:public QmuParser { public: explicit Calculator(const VContainer *data); - qreal eval(QString prog, QString *errorMsg); + qreal EvalFormula(const QString &formula); private: Q_DISABLE_COPY(Calculator) - /** - * @brief errorMsg keeps error message of calculation. - */ - QString *errorMsg; - /** - * @brief token теперішня лексема. - */ - QString token; - /** - * @brief tok internal representation of token. - */ - qint32 tok; - /** - * @brief token_type type of token. - */ - qint32 token_type; - /** - * @brief prog string where keeps formula. - */ - QString prog; - /** - * @brief index number character in string of formula. - */ - qint32 index; - /** - * @brief data container with data container of all variables. - */ - const VContainer *data; - /** - * @brief debugFormula decoded string of formula. - */ - QString debugFormula; - qreal get_exp(); - void get_token(); - static bool StrChr(QString string, QChar c); - void putback(); - void level2(qreal *result); - void level3(qreal *result); - void level4(qreal *result); - void level5(qreal *result); - void level6(qreal *result); - void primitive(qreal *result); - static void arith(QChar o, qreal *r, qreal *h); - static void unary(QChar o, qreal *r); - qreal find_var(QString s); - // cppcheck-suppress functionStatic - void serror(qint32 error); - static char look_up(QString s); - static bool isdelim(QChar c); - static bool iswhite(QChar c); + void InitVariables(const VContainer *data); + static qreal CmUnit(qreal val); + static qreal MmUnit(qreal val); + static qreal InchUnit(qreal val); }; #endif // CALCULATOR_H diff --git a/src/app/container/vcontainer.cpp b/src/app/container/vcontainer.cpp index 6a5ceb691..7cf541dc1 100644 --- a/src/app/container/vcontainer.cpp +++ b/src/app/container/vcontainer.cpp @@ -35,6 +35,9 @@ quint32 VContainer::_id = 0; //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VContainer create empty container + */ VContainer::VContainer() :_size(50), sizeName(size_M), _height(176), heightName(height_M), gObjects(QHash()), measurements(QHash()), increments(QHash()), @@ -43,6 +46,11 @@ VContainer::VContainer() {} //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief operator = copy constructor + * @param data container + * @return copy container + */ VContainer &VContainer::operator =(const VContainer &data) { setData(data); @@ -50,6 +58,10 @@ VContainer &VContainer::operator =(const VContainer &data) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VContainer create container from another container + * @param data container + */ VContainer::VContainer(const VContainer &data) :_size(50), sizeName(size_M), _height(176), heightName(height_M), gObjects(QHash()), measurements(QHash()), increments(QHash()), @@ -67,6 +79,10 @@ VContainer::~VContainer() } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief setData copy data from container + * @param data container + */ void VContainer::setData(const VContainer &data) { _size = data.size(); @@ -111,6 +127,11 @@ void VContainer::setData(const VContainer &data) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetGObject returns a point by id + * @param id id of point + * @return point + */ // cppcheck-suppress unusedFunction const VGObject *VContainer::GetGObject(quint32 id)const { @@ -118,6 +139,12 @@ const VGObject *VContainer::GetGObject(quint32 id)const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetObject return object from container + * @param obj container + * @param id id of object + * @return Object + */ template const val VContainer::GetObject(const QHash &obj, key id) const { @@ -132,6 +159,12 @@ const val VContainer::GetObject(const QHash &obj, key id) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetObject return object from container + * @param obj container + * @param id id of object + * @return Object + */ template val VContainer::GetVariable(const QHash &obj, key id) const { @@ -146,6 +179,11 @@ val VContainer::GetVariable(const QHash &obj, key id) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetMeasurement return measurement by name + * @param name short measurement name + * @return measurement + */ const VMeasurement VContainer::GetMeasurement(const QString &name) const { Q_ASSERT(name.isEmpty()==false); @@ -153,6 +191,11 @@ const VMeasurement VContainer::GetMeasurement(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetIncrement return increment table row by name + * @param name name of increment table row + * @return increment + */ const VIncrement VContainer::GetIncrement(const QString& name) const { Q_ASSERT(name.isEmpty()==false); @@ -160,6 +203,11 @@ const VIncrement VContainer::GetIncrement(const QString& name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetLine return length of line by name + * @param name name of line + * @return length of line in mm + */ qreal VContainer::GetLine(const QString &name) const { Q_ASSERT(name.isEmpty()==false); @@ -167,6 +215,11 @@ qreal VContainer::GetLine(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetLengthArc return length of arc by name + * @param name name of arc + * @return length of arc in mm + */ qreal VContainer::GetLengthArc(const QString &name) const { Q_ASSERT(name.isEmpty()==false); @@ -174,6 +227,11 @@ qreal VContainer::GetLengthArc(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetLengthSpline return length of spline by name + * @param name name of spline + * @return length of spline in mm + */ qreal VContainer::GetLengthSpline(const QString &name) const { Q_ASSERT(name.isEmpty()==false); @@ -181,6 +239,11 @@ qreal VContainer::GetLengthSpline(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetLineAngle return angle of line + * @param name name of line angle + * @return angle in degree + */ qreal VContainer::GetLineAngle(const QString &name) const { Q_ASSERT(name.isEmpty()==false); @@ -188,18 +251,33 @@ qreal VContainer::GetLineAngle(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetDetail return detail by id + * @param id id of detail + * @return detail + */ const VDetail VContainer::GetDetail(quint32 id) const { return GetVariable(details, id); } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddPoint add new point to container + * @param point new point + * @return return id of new point in container + */ quint32 VContainer::AddGObject(VGObject *obj) { return AddObject(gObjects, obj); } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddDetail add new detail to container + * @param detail new detail + * @return return id of new detail in container + */ quint32 VContainer::AddDetail(VDetail detail) { quint32 id = getNextId(); @@ -208,12 +286,21 @@ quint32 VContainer::AddDetail(VDetail detail) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddIncrement add new row of increment table + * @param name name of new row of increment table + * @param row new row of increment table + */ void VContainer::AddIncrement(const QString &name, VIncrement incr) { increments[name] = incr; } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief getNextId generate next unique id + * @return next unique id + */ quint32 VContainer::getNextId() { _id++; @@ -221,6 +308,10 @@ quint32 VContainer::getNextId() } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateId update id. If new id bigger when current save new like current. + * @param newId id + */ void VContainer::UpdateId(quint32 newId) { if (newId > _id) @@ -230,6 +321,12 @@ void VContainer::UpdateId(quint32 newId) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateObject update object in container + * @param obj container + * @param id id of existing object + * @param point object + */ template void VContainer::UpdateObject(QHash &obj, const quint32 &id, val point) { @@ -246,6 +343,11 @@ void VContainer::UpdateObject(QHash &obj, const quint32 &id, val p } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddLengthSpline add length of spline to container + * @param name name of spline + * @param value length of spline + */ void VContainer::AddLengthSpline(const QString &name, const qreal &value) { Q_ASSERT(name.isEmpty() == false); @@ -253,6 +355,10 @@ void VContainer::AddLengthSpline(const QString &name, const qreal &value) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddLengthArc add length of arc to container + * @param id id of arc + */ void VContainer::AddLengthArc(const quint32 &id) { const VArc * arc = GeometricObject(id); @@ -260,6 +366,11 @@ void VContainer::AddLengthArc(const quint32 &id) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddLineAngle add angle of line to container + * @param name name of line angle + * @param value angle in degree + */ void VContainer::AddLineAngle(const QString &name, const qreal &value) { Q_ASSERT(name.isEmpty() == false); @@ -267,6 +378,11 @@ void VContainer::AddLineAngle(const QString &name, const qreal &value) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetValueStandardTableRow return value of measurement by name + * @param name name of measurement + * @return value in measurement units + */ qreal VContainer::GetValueStandardTableRow(const QString& name) const { const VMeasurement m = GetMeasurement(name); @@ -281,6 +397,11 @@ qreal VContainer::GetValueStandardTableRow(const QString& name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetValueIncrementTableRow return value of increment table row by name + * @param name name of row + * @return value of row in mm + */ qreal VContainer::GetValueIncrementTableRow(const QString& name) const { const VIncrement icr = GetIncrement(name); @@ -295,6 +416,9 @@ qreal VContainer::GetValueIncrementTableRow(const QString& name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief Clear clear data in container. Id will be 0. + */ void VContainer::Clear() { _id = 0; @@ -309,6 +433,9 @@ void VContainer::Clear() } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearObject points, splines, arcs, spline paths will be cleared. + */ void VContainer::ClearGObjects() { if (gObjects.size()>0) @@ -337,6 +464,12 @@ void VContainer::ClearCalculationGObjects() } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief FindVar return value of variable by name + * @param name name of variable + * @param ok false if can't find variable + * @return value of variable + */ qreal VContainer::FindVar(const QString &name, bool *ok)const { if (sizeName == name) @@ -384,6 +517,12 @@ qreal VContainer::FindVar(const QString &name, bool *ok)const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddLine add line to container + * @param firstPointId id of first point of line + * @param secondPointId id of second point of line + * @param mode mode of line + */ void VContainer::AddLine(const quint32 &firstPointId, const quint32 &secondPointId) { QString nameLine = GetNameLine(firstPointId, secondPointId); @@ -395,6 +534,12 @@ void VContainer::AddLine(const quint32 &firstPointId, const quint32 &secondPoint } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddObject add object to container + * @param obj container + * @param value object + * @return id of object in container + */ template quint32 VContainer::AddObject(QHash &obj, val value) { @@ -406,6 +551,12 @@ quint32 VContainer::AddObject(QHash &obj, val value) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetNameLine return name of line + * @param firstPoint id of first point of line + * @param secondPoint id of second point of line + * @return name of line + */ QString VContainer::GetNameLine(const quint32 &firstPoint, const quint32 &secondPoint) const { const VPointF *first = GeometricObject(firstPoint); @@ -415,6 +566,12 @@ QString VContainer::GetNameLine(const quint32 &firstPoint, const quint32 &second } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetNameLineAngle return name of line angle + * @param firstPoint id of first point of line + * @param secondPoint id of second point of line + * @return name of angle of line + */ QString VContainer::GetNameLineAngle(const quint32 &firstPoint, const quint32 &secondPoint) const { const VPointF *first = GeometricObject(firstPoint); @@ -424,12 +581,22 @@ QString VContainer::GetNameLineAngle(const quint32 &firstPoint, const quint32 &s } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdatePoint update point by id + * @param id id of existing point + * @param point point + */ void VContainer::UpdateGObject(quint32 id, VGObject* obj) { UpdateObject(gObjects, id, obj); } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateDetail update detail by id + * @param id id of existing detail + * @param detail detail + */ void VContainer::UpdateDetail(quint32 id, const VDetail &detail) { Q_ASSERT_X(id > 0, Q_FUNC_INFO, "id <= 0"); @@ -438,6 +605,11 @@ void VContainer::UpdateDetail(quint32 id, const VDetail &detail) } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddLengthLine add length of line to container + * @param name name of line + * @param value length of line + */ void VContainer::AddLengthLine(const QString &name, const qreal &value) { Q_ASSERT(name.isEmpty() == false); diff --git a/src/app/container/vcontainer.h b/src/app/container/vcontainer.h index be55c15f9..94ce7ded4 100644 --- a/src/app/container/vcontainer.h +++ b/src/app/container/vcontainer.h @@ -45,20 +45,8 @@ class VContainer { Q_DECLARE_TR_FUNCTIONS(VContainer) public: - /** - * @brief VContainer create empty container - */ VContainer(); - /** - * @brief operator = copy constructor - * @param data container - * @return copy container - */ VContainer &operator=(const VContainer &data); - /** - * @brief VContainer create container from another container - * @param data container - */ VContainer(const VContainer &data); ~VContainer(); template @@ -67,11 +55,7 @@ public: T *obj = new T(*data.GeometricObject(id)); UpdateGObject(id, obj); } - /** - * @brief setData copy data from container - * @param data container - */ - void setData(const VContainer &data); + void setData(const VContainer &data); template const T GeometricObject(const quint32 &id) const { @@ -100,288 +84,65 @@ public: return nullptr; } - /** - * @brief GetGObject returns a point by id - * @param id id of point - * @return point - */ - const VGObject *GetGObject(quint32 id) const; - /** - * @brief GetMeasurement return measurement by name - * @param name short measurement name - * @return measurement - */ - const VMeasurement GetMeasurement(const QString& name) const; - /** - * @brief GetIncrement return increment table row by name - * @param name name of increment table row - * @return increment - */ - const VIncrement GetIncrement(const QString& name) const; - /** - * @brief GetLine return length of line by name - * @param name name of line - * @return length of line in mm - */ - qreal GetLine(const QString &name) const; - /** - * @brief GetLengthArc return length of arc by name - * @param name name of arc - * @return length of arc in mm - */ - qreal GetLengthArc(const QString &name) const; - /** - * @brief GetLengthSpline return length of spline by name - * @param name name of spline - * @return length of spline in mm - */ - qreal GetLengthSpline(const QString &name) const; - /** - * @brief GetLineAngle return angle of line - * @param name name of line angle - * @return angle in degree - */ - qreal GetLineAngle(const QString &name) const; - /** - * @brief GetDetail return detail by id - * @param id id of detail - * @return detail - */ - const VDetail GetDetail(quint32 id) const; - /** - * @brief getId return current id - * @return current id - */ - static quint32 getId() {return _id;} - /** - * @brief AddPoint add new point to container - * @param point new point - * @return return id of new point in container - */ - quint32 AddGObject(VGObject *obj); - /** - * @brief AddDetail add new detail to container - * @param detail new detail - * @return return id of new detail in container - */ - quint32 AddDetail(VDetail detail); - /** - * @brief AddMeasurement add new measurement - * @param name short measurement name - * @param row measurement - */ - void AddMeasurement(const QString& name, const VMeasurement &m); - /** - * @brief AddIncrement add new row of increment table - * @param name name of new row of increment table - * @param row new row of increment table - */ - void AddIncrement(const QString& name, VIncrement incr); - /** - * @brief AddLengthLine add length of line to container - * @param name name of line - * @param value length of line - */ - void AddLengthLine(const QString &name, const qreal &value); - /** - * @brief AddLengthSpline add length of spline to container - * @param name name of spline - * @param value length of spline - */ - void AddLengthSpline(const QString &name, const qreal &value); - /** - * @brief AddLengthArc add length of arc to container - * @param id id of arc - */ - void AddLengthArc(const quint32 &id); - /** - * @brief AddLineAngle add angle of line to container - * @param name name of line angle - * @param value angle in degree - */ - void AddLineAngle(const QString &name, const qreal &value); - /** - * @brief AddLine add line to container - * @param firstPointId id of first point of line - * @param secondPointId id of second point of line - * @param mode mode of line - */ - void AddLine(const quint32 &firstPointId, const quint32 &secondPointId); - /** - * @brief GetNameLine return name of line - * @param firstPoint id of first point of line - * @param secondPoint id of second point of line - * @return name of line - */ + const VGObject *GetGObject(quint32 id) const; + const VMeasurement GetMeasurement(const QString& name) const; + const VIncrement GetIncrement(const QString& name) const; + qreal GetLine(const QString &name) const; + qreal GetLengthArc(const QString &name) const; + qreal GetLengthSpline(const QString &name) const; + qreal GetLineAngle(const QString &name) const; + const VDetail GetDetail(quint32 id) const; + static quint32 getId(){return _id;} + quint32 AddGObject(VGObject *obj); + quint32 AddDetail(VDetail detail); + void AddMeasurement(const QString& name, const VMeasurement &m); + void AddIncrement(const QString& name, VIncrement incr); + void AddLengthLine(const QString &name, const qreal &value); + void AddLengthSpline(const QString &name, const qreal &value); + void AddLengthArc(const quint32 &id); + void AddLineAngle(const QString &name, const qreal &value); + void AddLine(const quint32 &firstPointId, const quint32 &secondPointId); // cppcheck-suppress functionStatic - QString GetNameLine(const quint32 &firstPoint, const quint32 &secondPoint) const; - /** - * @brief GetNameLineAngle return name of line angle - * @param firstPoint id of first point of line - * @param secondPoint id of second point of line - * @return name of angle of line - */ + QString GetNameLine(const quint32 &firstPoint, const quint32 &secondPoint) const; // cppcheck-suppress functionStatic - QString GetNameLineAngle(const quint32 &firstPoint, const quint32 &secondPoint) const; - /** - * @brief UpdatePoint update point by id - * @param id id of existing point - * @param point point - */ - void UpdateGObject(quint32 id, VGObject* obj); - /** - * @brief UpdateDetail update detail by id - * @param id id of existing detail - * @param detail detail - */ - void UpdateDetail(quint32 id, const VDetail &detail); - /** - * @brief UpdateMeasurement update measurement by name - * @param name short measurement name - * @param m measurement - */ - void UpdateMeasurement(const QString& name, VMeasurement m); - /** - * @brief UpdateIncrement update increment table row by name - * @param name name of row - * @param row row - */ - void UpdateIncrement(const QString& name, VIncrement incr); - /** - * @brief GetValueStandardTableRow return value of measurement by name - * @param name name of measurement - * @return value in measurement units - */ - qreal GetValueStandardTableRow(const QString& name) const; - /** - * @brief GetValueIncrementTableRow return value of increment table row by name - * @param name name of row - * @return value of row in mm - */ - qreal GetValueIncrementTableRow(const QString& name) const; - /** - * @brief Clear clear data in container. Id will be 0. - */ - void Clear(); - /** - * @brief ClearObject points, splines, arcs, spline paths will be cleared. - */ - void ClearGObjects(); - void ClearCalculationGObjects(); - /** - * @brief ClearIncrementTable clear increment table - */ - void ClearIncrementTable(); - void ClearMeasurements(); - /** - * @brief ClearLengthLines clear length lines - */ - void ClearLengthLines(); - /** - * @brief ClearLengthSplines clear length splines - */ - void ClearLengthSplines(); - /** - * @brief ClearLengthArcs clear length arcs - */ - void ClearLengthArcs(); - /** - * @brief ClearLineAngles clear angles of lines - */ - void ClearLineAngles(); - void ClearDetails(); - /** - * @brief SetSize set value of size - * @param size value of size - */ - void SetSize(qreal size); - void SetSizeName(const QString &name); - /** - * @brief SetGrowth set value of growth - * @param growth value of growth - */ - void SetHeight(qreal height); - void SetHeightName(const QString &name); - /** - * @brief size return size - * @return size in mm - */ - qreal size() const; - QString SizeName()const; - /** - * @brief height return height - * @return height in pattern units - */ - qreal height() const; - QString HeightName()const; - /** - * @brief FindVar return value of variable by name - * @param name name of variable - * @param ok false if can't find variable - * @return value of variable - */ - qreal FindVar(const QString& name, bool *ok)const; - /** - * @brief IncrementTableContains check if increment table contains name - * @param name name of row - * @return true if contains - */ - bool IncrementTableContains(const QString& name); - /** - * @brief getNextId generate next unique id - * @return next unique id - */ - static quint32 getNextId(); - /** - * @brief RemoveIncrementTableRow remove row by name from increment table - * @param name name of existing row - */ - void RemoveIncrementTableRow(const QString& name); - /** - * @brief data container with datagObjects return container of gObjects - * @return pointer on container of gObjects - */ - const QHash *DataGObjects() const; - /** - * @brief DataMeasurements container with measurements. - * @return pointer to measurements. - */ + QString GetNameLineAngle(const quint32 &firstPoint, const quint32 &secondPoint) const; + void UpdateGObject(quint32 id, VGObject* obj); + void UpdateDetail(quint32 id, const VDetail &detail); + void UpdateMeasurement(const QString& name, VMeasurement m); + void UpdateIncrement(const QString& name, VIncrement incr); + qreal GetValueStandardTableRow(const QString& name) const; + qreal GetValueIncrementTableRow(const QString& name) const; + void Clear(); + void ClearGObjects(); + void ClearCalculationGObjects(); + void ClearIncrementTable(); + void ClearMeasurements(); + void ClearLengthLines(); + void ClearLengthSplines(); + void ClearLengthArcs(); + void ClearLineAngles(); + void ClearDetails(); + void SetSize(qreal size); + void SetSizeName(const QString &name); + void SetHeight(qreal height); + void SetHeightName(const QString &name); + qreal size() const; + QString SizeName()const; + qreal height() const; + QString HeightName()const; + qreal FindVar(const QString& name, bool *ok)const; + bool IncrementTableContains(const QString& name); + static quint32 getNextId(); + void RemoveIncrementTableRow(const QString& name); + static void UpdateId(quint32 newId); + const QHash *DataGObjects() const; const QHash *DataMeasurements() const; - /** - * @brief data container with dataIncrements return container of increment table - * @return pointer on container of increment table - */ - const QHash *DataIncrements() const; - /** - * @brief data container with dataLengthLines return container of lines lengths - * @return pointer on container of lines lengths - */ - const QHash *DataLengthLines() const; - /** - * @brief data container with dataLengthSplines return container of splines lengths - * @return pointer on container of splines lengths - */ - const QHash *DataLengthSplines() const; - /** - * @brief data container with dataLengthArcs return container of arcs length - * @return pointer on container of arcs length - */ - const QHash *DataLengthArcs() const; - /** - * @brief data container with dataLineAngles return container of angles of line - * @return pointer on container of angles of line - */ - const QHash *DataLineAngles() const; - /** - * @brief data container with dataDetails return container of details - * @return pointer on container of details - */ - const QHash *DataDetails() const; - /** - * @brief UpdateId update id. If new id bigger when current save new like current. - * @param newId id - */ - static void UpdateId(quint32 newId); + const QHash *DataIncrements() const; + const QHash *DataLengthLines() const; + const QHash *DataLengthSplines() const; + const QHash *DataLengthArcs() const; + const QHash *DataLineAngles() const; + const QHash *DataDetails() const; private: /** * @brief _id current id. New object will have value +1. For empty class equal 0. @@ -423,177 +184,272 @@ private: * @brief details container of details */ QHash details; + template - /** - * @brief GetObject return object from container - * @param obj container - * @param id id of object - * @return Object - */ // cppcheck-suppress functionStatic const val GetObject(const QHash &obj, key id) const; + template - /** - * @brief GetObject return object from container - * @param obj container - * @param id id of object - * @return Object - */ // cppcheck-suppress functionStatic val GetVariable(const QHash &obj, key id) const; + template - /** - * @brief UpdateObject update object in container - * @param obj container - * @param id id of existing object - * @param point object - */ void UpdateObject(QHash &obj, const quint32 &id, val point); + template - /** - * @brief AddObject add object to container - * @param obj container - * @param value object - * @return id of object in container - */ static quint32 AddObject(QHash &obj, val value); }; +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief AddMeasurement add new measurement + * @param name short measurement name + * @param row measurement + */ inline void VContainer::AddMeasurement(const QString &name, const VMeasurement &m) { measurements[name] = m; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateMeasurement update measurement by name + * @param name short measurement name + * @param m measurement + */ inline void VContainer::UpdateMeasurement(const QString &name, VMeasurement m) { measurements[name] = m; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateIncrement update increment table row by name + * @param name name of row + * @param row row + */ inline void VContainer::UpdateIncrement(const QString &name, VIncrement incr) { increments[name] = incr; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearIncrementTable clear increment table + */ inline void VContainer::ClearIncrementTable() { increments.clear(); } +//--------------------------------------------------------------------------------------------------------------------- inline void VContainer::ClearMeasurements() { measurements.clear(); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearLengthLines clear length lines + */ inline void VContainer::ClearLengthLines() { lengthLines.clear(); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearLengthSplines clear length splines + */ inline void VContainer::ClearLengthSplines() { lengthSplines.clear(); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearLengthArcs clear length arcs + */ inline void VContainer::ClearLengthArcs() { lengthArcs.clear(); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief ClearLineAngles clear angles of lines + */ inline void VContainer::ClearLineAngles() { lineAngles.clear(); } +//--------------------------------------------------------------------------------------------------------------------- inline void VContainer::ClearDetails() { details.clear(); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief SetSize set value of size + * @param size value of size + */ inline void VContainer::SetSize(qreal size) { _size = size; } +//--------------------------------------------------------------------------------------------------------------------- inline void VContainer::SetSizeName(const QString &name) { sizeName = name; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief SetGrowth set value of growth + * @param growth value of growth + */ inline void VContainer::SetHeight(qreal height) { _height = height; } +//--------------------------------------------------------------------------------------------------------------------- inline void VContainer::SetHeightName(const QString &name) { heightName = name; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief size return size + * @return size in mm + */ inline qreal VContainer::size() const { return _size; } +//--------------------------------------------------------------------------------------------------------------------- inline QString VContainer::SizeName() const { return sizeName; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief height return height + * @return height in pattern units + */ inline qreal VContainer::height() const { return _height; } +//--------------------------------------------------------------------------------------------------------------------- inline QString VContainer::HeightName() const { return heightName; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief IncrementTableContains check if increment table contains name + * @param name name of row + * @return true if contains + */ inline bool VContainer::IncrementTableContains(const QString &name) { return increments.contains(name); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief RemoveIncrementTableRow remove row by name from increment table + * @param name name of existing row + */ inline void VContainer::RemoveIncrementTableRow(const QString &name) { increments.remove(name); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with datagObjects return container of gObjects + * @return pointer on container of gObjects + */ inline const QHash *VContainer::DataGObjects() const { return &gObjects; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief DataMeasurements container with measurements. + * @return pointer to measurements. + */ inline const QHash *VContainer::DataMeasurements() const { return &measurements; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataIncrements return container of increment table + * @return pointer on container of increment table + */ inline const QHash *VContainer::DataIncrements() const { return &increments; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataLengthLines return container of lines lengths + * @return pointer on container of lines lengths + */ inline const QHash *VContainer::DataLengthLines() const { return &lengthLines; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataLengthSplines return container of splines lengths + * @return pointer on container of splines lengths + */ inline const QHash *VContainer::DataLengthSplines() const { return &lengthSplines; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataLengthArcs return container of arcs length + * @return pointer on container of arcs length + */ inline const QHash *VContainer::DataLengthArcs() const { return &lengthArcs; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataLineAngles return container of angles of line + * @return pointer on container of angles of line + */ inline const QHash *VContainer::DataLineAngles() const { return &lineAngles; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief data container with dataDetails return container of details + * @return pointer on container of details + */ inline const QHash *VContainer::DataDetails() const { return &details; diff --git a/src/app/dialogs/tools/dialogtool.cpp b/src/app/dialogs/tools/dialogtool.cpp index c10df30c0..b6534f4f1 100644 --- a/src/app/dialogs/tools/dialogtool.cpp +++ b/src/app/dialogs/tools/dialogtool.cpp @@ -32,6 +32,7 @@ #include "../../tools/vabstracttool.h" #include +#include "../../../libs/qmuparser/qmuparsererror.h" //--------------------------------------------------------------------------------------------------------------------- DialogTool::DialogTool(const VContainer *data, QWidget *parent) @@ -329,20 +330,29 @@ void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) } else { - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(edit->text(), &errorMsg); - if (errorMsg.isEmpty() == false) + try + { + Calculator cal(data); + const qreal result = cal.EvalFormula(edit->text()); + + label->setText(QString().setNum(result)); + flag = true; + palette.setColor(labelEditFormula->foregroundRole(), QColor(76, 76, 76)); + emit ToolTip(""); + } + catch(qmu::QmuParserError &e) { label->setText(tr("Error")); flag = false; palette.setColor(labelEditFormula->foregroundRole(), Qt::red); - } - else - { - label->setText(QString().setNum(result)); - flag = true; - palette.setColor(labelEditFormula->foregroundRole(), QColor(76, 76, 76)); + emit ToolTip(e.GetMsg()); + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); } } CheckState(); diff --git a/src/app/options.cpp b/src/app/options.cpp index 3bea6b6af..4ab63ca11 100644 --- a/src/app/options.cpp +++ b/src/app/options.cpp @@ -194,3 +194,8 @@ const QString min_F = QStringLiteral("min"); const QString max_F = QStringLiteral("max"); const QString sum_F = QStringLiteral("sum"); const QString avg_F = QStringLiteral("avg"); + +// Postfix operators +const QString cm_Oprt = QStringLiteral("cm"); +const QString mm_Oprt = QStringLiteral("mm"); +const QString in_Oprt = QStringLiteral("in"); diff --git a/src/app/options.h b/src/app/options.h index 119bf164d..7bcd53bc1 100644 --- a/src/app/options.h +++ b/src/app/options.h @@ -278,4 +278,9 @@ extern const QString max_F; extern const QString sum_F; extern const QString avg_F; +// Postfix operators +extern const QString cm_Oprt; +extern const QString mm_Oprt; +extern const QString in_Oprt; + #endif // OPTIONS_H diff --git a/src/app/tools/drawTools/vtoolalongline.cpp b/src/app/tools/drawTools/vtoolalongline.cpp index 6165f562e..debeca98c 100644 --- a/src/app/tools/drawTools/vtoolalongline.cpp +++ b/src/app/tools/drawTools/vtoolalongline.cpp @@ -179,40 +179,53 @@ void VToolAlongLine::Create(const quint32 _id, const QString &pointName, const Q const VPointF *firstPoint = data->GeometricObject(firstPointId); const VPointF *secondPoint = data->GeometricObject(secondPointId); QLineF line = QLineF(firstPoint->toQPointF(), secondPoint->toQPointF()); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + + try { + Calculator cal(data); + const qreal result = cal.EvalFormula(formula); line.setLength(qApp->toPixel(result)); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + } + catch(qmu::QmuParserError &e) + { + //TODO show error + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject( new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + data->AddLine(id, secondPointId); + } + else + { + data->UpdateGObject(id, new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + data->AddLine(id, secondPointId); + if (parse != Document::FullParse) { - id = data->AddGObject( new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - data->AddLine(id, secondPointId); - } - else - { - data->UpdateGObject(id, new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - data->AddLine(id, secondPointId); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::AlongLineTool, doc); - if (parse == Document::FullParse) - { - VToolAlongLine *point = new VToolAlongLine(doc, data, id, formula, firstPointId, - secondPointId, typeLine, typeCreation); - scene->addItem(point); - connect(point, &VToolAlongLine::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolAlongLine::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(firstPointId); - doc->IncrementReferens(secondPointId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::AlongLineTool, doc); + if (parse == Document::FullParse) + { + VToolAlongLine *point = new VToolAlongLine(doc, data, id, formula, firstPointId, + secondPointId, typeLine, typeCreation); + scene->addItem(point); + connect(point, &VToolAlongLine::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolAlongLine::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(firstPointId); + doc->IncrementReferens(secondPointId); + } } diff --git a/src/app/tools/drawTools/vtoolarc.cpp b/src/app/tools/drawTools/vtoolarc.cpp index 199b1139c..ac36dfb44 100644 --- a/src/app/tools/drawTools/vtoolarc.cpp +++ b/src/app/tools/drawTools/vtoolarc.cpp @@ -93,26 +93,27 @@ void VToolArc::Create(const quint32 _id, const quint32 ¢er, const QString &r { qreal calcRadius = 0, calcF1 = 0, calcF2 = 0; - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(radius, &errorMsg); - if (errorMsg.isEmpty()) + try { + Calculator cal(data); + + qreal result = cal.EvalFormula(radius); calcRadius = qApp->toPixel(result); - } - errorMsg.clear(); - result = cal.eval(f1, &errorMsg); - if (errorMsg.isEmpty()) - { - calcF1 = result; + calcF1 = cal.EvalFormula(f1); + calcF2 = cal.EvalFormula(f2); } - - errorMsg.clear(); - result = cal.eval(f2, &errorMsg); - if (errorMsg.isEmpty()) + catch(qmu::QmuParserError &e) { - calcF2 = result; + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; } VPointF c = *data->GeometricObject(center); diff --git a/src/app/tools/drawTools/vtoolbisector.cpp b/src/app/tools/drawTools/vtoolbisector.cpp index e770348d6..2834e915b 100644 --- a/src/app/tools/drawTools/vtoolbisector.cpp +++ b/src/app/tools/drawTools/vtoolbisector.cpp @@ -114,42 +114,55 @@ void VToolBisector::Create(const quint32 _id, const QString &formula, const quin const VPointF *secondPoint = data->GeometricObject(secondPointId); const VPointF *thirdPoint = data->GeometricObject(thirdPointId); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + qreal result = 0; + try { - QPointF fPoint = VToolBisector::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), - thirdPoint->toQPointF(), qApp->toPixel(result)); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + QPointF fPoint = VToolBisector::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), + thirdPoint->toQPointF(), qApp->toPixel(result)); + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + } + else + { + data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + if (parse != Document::FullParse) { - id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - } - else - { - data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::BisectorTool, doc); - if (parse == Document::FullParse) - { - VToolBisector *point = new VToolBisector(doc, data, id, typeLine, formula, firstPointId, secondPointId, - thirdPointId, typeCreation); - scene->addItem(point); - connect(point, &VToolBisector::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolBisector::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(firstPointId); - doc->IncrementReferens(secondPointId); - doc->IncrementReferens(thirdPointId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::BisectorTool, doc); + if (parse == Document::FullParse) + { + VToolBisector *point = new VToolBisector(doc, data, id, typeLine, formula, firstPointId, secondPointId, + thirdPointId, typeCreation); + scene->addItem(point); + connect(point, &VToolBisector::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolBisector::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(firstPointId); + doc->IncrementReferens(secondPointId); + doc->IncrementReferens(thirdPointId); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolcutarc.cpp b/src/app/tools/drawTools/vtoolcutarc.cpp index 41209fb78..ca6db78f1 100644 --- a/src/app/tools/drawTools/vtoolcutarc.cpp +++ b/src/app/tools/drawTools/vtoolcutarc.cpp @@ -95,63 +95,76 @@ void VToolCutArc::Create(const quint32 _id, const QString &pointName, const QStr VContainer *data, const Document::Documents &parse, const Valentina::Sources &typeCreation) { const VArc *arc = data->GeometricObject(arcId); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + qreal result = 0; + try { - VArc arc1; - VArc arc2; - QPointF point = arc->CutArc(qApp->toPixel(result), arc1, arc2); + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } - quint32 id = _id; - quint32 arc1id = 0; - quint32 arc2id = 0; - if (typeCreation == Valentina::FromGui) + VArc arc1; + VArc arc2; + QPointF point = arc->CutArc(qApp->toPixel(result), arc1, arc2); + + quint32 id = _id; + quint32 arc1id = 0; + quint32 arc2id = 0; + if (typeCreation == Valentina::FromGui) + { + VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); + id = data->AddGObject(p); + + VArc * ar1 = new VArc(arc1); + arc1id = data->AddGObject(ar1); + + VArc * ar2 = new VArc(arc2); + arc2id = data->AddGObject(ar2); + } + else + { + VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); + data->UpdateGObject(id, p); + + arc1id = id + 1; + arc2id = id + 2; + + VArc * ar1 = new VArc(arc1); + data->UpdateGObject(arc1id, ar1); + + VArc * ar2 = new VArc(arc2); + data->UpdateGObject(arc2id, ar2); + + if (parse != Document::FullParse) { - VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); - id = data->AddGObject(p); - - VArc * ar1 = new VArc(arc1); - arc1id = data->AddGObject(ar1); - - VArc * ar2 = new VArc(arc2); - arc2id = data->AddGObject(ar2); + doc->UpdateToolData(id, data); } - else - { - VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); - data->UpdateGObject(id, p); + } + data->AddLengthArc(arc1id); + data->AddLengthArc(arc2id); - arc1id = id + 1; - arc2id = id + 2; - - VArc * ar1 = new VArc(arc1); - data->UpdateGObject(arc1id, ar1); - - VArc * ar2 = new VArc(arc2); - data->UpdateGObject(arc2id, ar2); - - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - data->AddLengthArc(arc1id); - data->AddLengthArc(arc2id); - - VDrawTool::AddRecord(id, Valentina::CutArcTool, doc); - if (parse == Document::FullParse) - { - VToolCutArc *point = new VToolCutArc(doc, data, id, formula, arcId, arc1id, arc2id, typeCreation); - scene->addItem(point); - connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); - doc->AddTool(id, point); - doc->AddTool(arc1id, point); - doc->AddTool(arc2id, point); - doc->IncrementReferens(arcId); - } + VDrawTool::AddRecord(id, Valentina::CutArcTool, doc); + if (parse == Document::FullParse) + { + VToolCutArc *point = new VToolCutArc(doc, data, id, formula, arcId, arc1id, arc2id, typeCreation); + scene->addItem(point); + connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); + doc->AddTool(id, point); + doc->AddTool(arc1id, point); + doc->AddTool(arc2id, point); + doc->IncrementReferens(arcId); } } diff --git a/src/app/tools/drawTools/vtoolcutspline.cpp b/src/app/tools/drawTools/vtoolcutspline.cpp index cdb8153c1..df8ade3fe 100644 --- a/src/app/tools/drawTools/vtoolcutspline.cpp +++ b/src/app/tools/drawTools/vtoolcutspline.cpp @@ -96,64 +96,78 @@ void VToolCutSpline::Create(const quint32 _id, const QString &pointName, const Document::Documents &parse, const Valentina::Sources &typeCreation) { const VSpline *spl = data->GeometricObject(splineId); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + + qreal result = 0; + try { - QPointF spl1p2, spl1p3, spl2p2, spl2p3; - QPointF point = spl->CutSpline(qApp->toPixel(result), spl1p2, spl1p3, spl2p2, spl2p3); + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } - quint32 id = _id; - quint32 spl1id = 0; - quint32 spl2id = 0; - if (typeCreation == Valentina::FromGui) + QPointF spl1p2, spl1p3, spl2p2, spl2p3; + QPointF point = spl->CutSpline(qApp->toPixel(result), spl1p2, spl1p3, spl2p2, spl2p3); + + quint32 id = _id; + quint32 spl1id = 0; + quint32 spl2id = 0; + if (typeCreation == Valentina::FromGui) + { + VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); + id = data->AddGObject(p); + + VSpline *spline1 = new VSpline(spl->GetP1(), spl1p2, spl1p3, *p, spl->GetKcurve()); + spl1id = data->AddGObject(spline1); + data->AddLengthSpline(spline1->name(), qApp->fromPixel(spline1->GetLength())); + + VSpline *spline2 = new VSpline(*p, spl2p2, spl2p3, spl->GetP4(), spl->GetKcurve()); + spl2id = data->AddGObject(spline2); + data->AddLengthSpline(spline2->name(), qApp->fromPixel(spline2->GetLength())); + } + else + { + VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); + data->UpdateGObject(id, p); + + spl1id = id + 1; + spl2id = id + 2; + + VSpline *spline1 = new VSpline(spl->GetP1(), spl1p2, spl1p3, *p, spl->GetKcurve()); + data->UpdateGObject(spl1id, spline1); + data->AddLengthSpline(spline1->name(), qApp->fromPixel(spline1->GetLength())); + + VSpline *spline2 = new VSpline(*p, spl2p2, spl2p3, spl->GetP4(), spl->GetKcurve()); + data->UpdateGObject(spl2id, spline2); + data->AddLengthSpline(spline2->name(), qApp->fromPixel(spline2->GetLength())); + + if (parse != Document::FullParse) { - VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); - id = data->AddGObject(p); - - VSpline *spline1 = new VSpline(spl->GetP1(), spl1p2, spl1p3, *p, spl->GetKcurve()); - spl1id = data->AddGObject(spline1); - data->AddLengthSpline(spline1->name(), qApp->fromPixel(spline1->GetLength())); - - VSpline *spline2 = new VSpline(*p, spl2p2, spl2p3, spl->GetP4(), spl->GetKcurve()); - spl2id = data->AddGObject(spline2); - data->AddLengthSpline(spline2->name(), qApp->fromPixel(spline2->GetLength())); - } - else - { - VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); - data->UpdateGObject(id, p); - - spl1id = id + 1; - spl2id = id + 2; - - VSpline *spline1 = new VSpline(spl->GetP1(), spl1p2, spl1p3, *p, spl->GetKcurve()); - data->UpdateGObject(spl1id, spline1); - data->AddLengthSpline(spline1->name(), qApp->fromPixel(spline1->GetLength())); - - VSpline *spline2 = new VSpline(*p, spl2p2, spl2p3, spl->GetP4(), spl->GetKcurve()); - data->UpdateGObject(spl2id, spline2); - data->AddLengthSpline(spline2->name(), qApp->fromPixel(spline2->GetLength())); - - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::CutSplineTool, doc); - if (parse == Document::FullParse) - { - VToolCutSpline *point = new VToolCutSpline(doc, data, id, formula, splineId, spl1id, spl2id, typeCreation); - scene->addItem(point); - connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); - doc->AddTool(id, point); - doc->AddTool(spl1id, point); - doc->AddTool(spl2id, point); - doc->IncrementReferens(splineId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::CutSplineTool, doc); + if (parse == Document::FullParse) + { + VToolCutSpline *point = new VToolCutSpline(doc, data, id, formula, splineId, spl1id, spl2id, typeCreation); + scene->addItem(point); + connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); + doc->AddTool(id, point); + doc->AddTool(spl1id, point); + doc->AddTool(spl2id, point); + doc->IncrementReferens(splineId); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolcutsplinepath.cpp b/src/app/tools/drawTools/vtoolcutsplinepath.cpp index 55af235e5..d5ad485dd 100644 --- a/src/app/tools/drawTools/vtoolcutsplinepath.cpp +++ b/src/app/tools/drawTools/vtoolcutsplinepath.cpp @@ -97,136 +97,150 @@ void VToolCutSplinePath::Create(const quint32 _id, const QString &pointName, con { const VSplinePath *splPath = data->GeometricObject(splinePathId); Q_CHECK_PTR(splPath); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + + qreal result = 0; + try { - quint32 id = _id; - QPointF spl1p2, spl1p3, spl2p2, spl2p3; - qint32 p1 = 0, p2 = 0; + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } - const QPointF point = splPath->CutSplinePath(qApp->toPixel(result), p1, p2, spl1p2, spl1p3, spl2p2, spl2p3); - VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); - if (typeCreation == Valentina::FromGui) + quint32 id = _id; + QPointF spl1p2, spl1p3, spl2p2, spl2p3; + qint32 p1 = 0, p2 = 0; + + const QPointF point = splPath->CutSplinePath(qApp->toPixel(result), p1, p2, spl1p2, spl1p3, spl2p2, spl2p3); + VPointF *p = new VPointF(point.x(), point.y(), pointName, mx, my); + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(p); + } + else + { + data->UpdateGObject(id, p); + } + + quint32 splPath1id = id + 1; + quint32 splPath2id = id + 2; + + VSplinePoint splP1 = splPath->at(p1); + VSplinePoint splP2 = splPath->at(p2); + const VSpline spl1 = VSpline(splP1.P(), spl1p2, spl1p3, *p, splPath->getKCurve()); + const VSpline spl2 = VSpline(*p, spl2p2, spl2p3, splP2.P(), splPath->getKCurve()); + + VSplinePath *splPath1 = new VSplinePath(); + VSplinePath *splPath2 = new VSplinePath(); + if (typeCreation == Valentina::FromGui) + { + for (qint32 i = 0; i < splPath->CountPoint(); i++) { - id = data->AddGObject(p); - } - else - { - data->UpdateGObject(id, p); - } - - quint32 splPath1id = id + 1; - quint32 splPath2id = id + 2; - - VSplinePoint splP1 = splPath->at(p1); - VSplinePoint splP2 = splPath->at(p2); - const VSpline spl1 = VSpline(splP1.P(), spl1p2, spl1p3, *p, splPath->getKCurve()); - const VSpline spl2 = VSpline(*p, spl2p2, spl2p3, splP2.P(), splPath->getKCurve()); - - VSplinePath *splPath1 = new VSplinePath(); - VSplinePath *splPath2 = new VSplinePath(); - if (typeCreation == Valentina::FromGui) - { - for (qint32 i = 0; i < splPath->CountPoint(); i++) + if (i <= p1 && i < p2) { - if (i <= p1 && i < p2) + if (i == p1) { - if (i == p1) - { - splPath1->append(VSplinePoint(splP1.P(), splP1.KAsm1(), spl1.GetAngle1()+180, spl1.GetKasm1(), - spl1.GetAngle1())); - const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl1.GetAngle2(), - spl1.GetAngle2()+180, spl1.GetAngle2()); - splPath1->append(cutPoint); - continue; - } - splPath1->append(splPath->at(i)); - } - else - { - if (i == p2) - { - const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl2.GetAngle1()+180, - spl2.GetKasm1(), spl2.GetAngle1()); - splPath2->append(cutPoint); - splPath2->append(VSplinePoint(splP2.P(), spl2.GetKasm2(), spl2.GetAngle2(), splP2.KAsm2(), - spl2.GetAngle2()+180)); - continue; - } - splPath2->append(splPath->at(i)); + splPath1->append(VSplinePoint(splP1.P(), splP1.KAsm1(), spl1.GetAngle1()+180, spl1.GetKasm1(), + spl1.GetAngle1())); + const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl1.GetAngle2(), + spl1.GetAngle2()+180, spl1.GetAngle2()); + splPath1->append(cutPoint); + continue; } + splPath1->append(splPath->at(i)); } - - splPath1->setMaxCountPoints(splPath->CountPoint()); - splPath2->setMaxCountPoints(splPath->CountPoint()); - - splPath1id = data->AddGObject(splPath1); - data->AddLengthSpline(splPath1->name(), qApp->fromPixel(splPath1->GetLength())); - - splPath2id = data->AddGObject(splPath2); - data->AddLengthSpline(splPath2->name(), qApp->fromPixel(splPath2->GetLength())); - } - else - { - for (qint32 i = 0; i < splPath->CountPoint(); i++) + else { - if (i <= p1 && i < p2) + if (i == p2) { - if (i == p1) - { - splPath1->append(VSplinePoint(splP1.P(), splP1.KAsm1(), spl1.GetAngle1()+180, spl1.GetKasm1(), - spl1.GetAngle1())); - const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl1.GetAngle2(), - spl2.GetKasm1(), spl1.GetAngle2()+180); - splPath1->append(cutPoint); - continue; - } - splPath1->append(splPath->at(i)); + const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl2.GetAngle1()+180, + spl2.GetKasm1(), spl2.GetAngle1()); + splPath2->append(cutPoint); + splPath2->append(VSplinePoint(splP2.P(), spl2.GetKasm2(), spl2.GetAngle2(), splP2.KAsm2(), + spl2.GetAngle2()+180)); + continue; } - else - { - if (i == p2) - { - const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl2.GetAngle1()+180, - spl2.GetKasm1(), spl2.GetAngle1()); - splPath2->append(cutPoint); - splPath2->append(VSplinePoint(splP2.P(), spl2.GetKasm2(), spl2.GetAngle2(), splP2.KAsm2(), - spl2.GetAngle2()+180)); - continue; - } - splPath2->append(splPath->at(i)); - } - } - - splPath1->setMaxCountPoints(splPath->CountPoint()); - splPath2->setMaxCountPoints(splPath->CountPoint()); - - data->UpdateGObject(splPath1id, splPath1); - data->AddLengthSpline(splPath1->name(), qApp->fromPixel(splPath1->GetLength())); - - data->UpdateGObject(splPath2id, splPath2); - data->AddLengthSpline(splPath2->name(), qApp->fromPixel(splPath2->GetLength())); - - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); + splPath2->append(splPath->at(i)); } } - VDrawTool::AddRecord(id, Valentina::CutSplinePathTool, doc); - if (parse == Document::FullParse) + + splPath1->setMaxCountPoints(splPath->CountPoint()); + splPath2->setMaxCountPoints(splPath->CountPoint()); + + splPath1id = data->AddGObject(splPath1); + data->AddLengthSpline(splPath1->name(), qApp->fromPixel(splPath1->GetLength())); + + splPath2id = data->AddGObject(splPath2); + data->AddLengthSpline(splPath2->name(), qApp->fromPixel(splPath2->GetLength())); + } + else + { + for (qint32 i = 0; i < splPath->CountPoint(); i++) { - VToolCutSplinePath *point = new VToolCutSplinePath(doc, data, id, formula, splinePathId, splPath1id, - splPath2id, typeCreation); - scene->addItem(point); - connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); - doc->AddTool(id, point); - doc->AddTool(splPath1id, point); - doc->AddTool(splPath2id, point); - doc->IncrementReferens(splinePathId); + if (i <= p1 && i < p2) + { + if (i == p1) + { + splPath1->append(VSplinePoint(splP1.P(), splP1.KAsm1(), spl1.GetAngle1()+180, spl1.GetKasm1(), + spl1.GetAngle1())); + const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl1.GetAngle2(), + spl2.GetKasm1(), spl1.GetAngle2()+180); + splPath1->append(cutPoint); + continue; + } + splPath1->append(splPath->at(i)); + } + else + { + if (i == p2) + { + const VSplinePoint cutPoint = VSplinePoint(*p, spl1.GetKasm2(), spl2.GetAngle1()+180, + spl2.GetKasm1(), spl2.GetAngle1()); + splPath2->append(cutPoint); + splPath2->append(VSplinePoint(splP2.P(), spl2.GetKasm2(), spl2.GetAngle2(), splP2.KAsm2(), + spl2.GetAngle2()+180)); + continue; + } + splPath2->append(splPath->at(i)); + } } + + splPath1->setMaxCountPoints(splPath->CountPoint()); + splPath2->setMaxCountPoints(splPath->CountPoint()); + + data->UpdateGObject(splPath1id, splPath1); + data->AddLengthSpline(splPath1->name(), qApp->fromPixel(splPath1->GetLength())); + + data->UpdateGObject(splPath2id, splPath2); + data->AddLengthSpline(splPath2->name(), qApp->fromPixel(splPath2->GetLength())); + + if (parse != Document::FullParse) + { + doc->UpdateToolData(id, data); + } + } + VDrawTool::AddRecord(id, Valentina::CutSplinePathTool, doc); + if (parse == Document::FullParse) + { + VToolCutSplinePath *point = new VToolCutSplinePath(doc, data, id, formula, splinePathId, splPath1id, + splPath2id, typeCreation); + scene->addItem(point); + connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); + doc->AddTool(id, point); + doc->AddTool(splPath1id, point); + doc->AddTool(splPath2id, point); + doc->IncrementReferens(splinePathId); } } diff --git a/src/app/tools/drawTools/vtoolendline.cpp b/src/app/tools/drawTools/vtoolendline.cpp index 14f1b2a0e..83971c71f 100644 --- a/src/app/tools/drawTools/vtoolendline.cpp +++ b/src/app/tools/drawTools/vtoolendline.cpp @@ -88,40 +88,54 @@ void VToolEndLine::Create(const quint32 _id, const QString &pointName, const QSt { const VPointF *basePoint = data->GeometricObject(basePointId); QLineF line = QLineF(basePoint->toQPointF(), QPointF(basePoint->x()+100, basePoint->y())); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + + qreal result = 0; + try { - line.setLength(qApp->toPixel(result)); - line.setAngle(angle); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + line.setLength(qApp->toPixel(result)); + line.setAngle(angle); + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); + data->AddLine(basePointId, id); + } + else + { + data->UpdateGObject(id, new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); + data->AddLine(basePointId, id); + if (parse != Document::FullParse) { - id = data->AddGObject(new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); - data->AddLine(basePointId, id); - } - else - { - data->UpdateGObject(id, new VPointF(line.p2().x(), line.p2().y(), pointName, mx, my)); - data->AddLine(basePointId, id); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::EndLineTool, doc); - if (parse == Document::FullParse) - { - VToolEndLine *point = new VToolEndLine(doc, data, id, typeLine, formula, angle, - basePointId, typeCreation); - scene->addItem(point); - connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(basePointId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::EndLineTool, doc); + if (parse == Document::FullParse) + { + VToolEndLine *point = new VToolEndLine(doc, data, id, typeLine, formula, angle, + basePointId, typeCreation); + scene->addItem(point); + connect(point, &VToolPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPoint::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(basePointId); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolnormal.cpp b/src/app/tools/drawTools/vtoolnormal.cpp index 0c3c75b3d..560f604f1 100644 --- a/src/app/tools/drawTools/vtoolnormal.cpp +++ b/src/app/tools/drawTools/vtoolnormal.cpp @@ -90,41 +90,55 @@ void VToolNormal::Create(const quint32 _id, const QString &formula, const quint3 { const VPointF *firstPoint = data->GeometricObject(firstPointId); const VPointF *secondPoint = data->GeometricObject(secondPointId); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + + qreal result = 0; + try { - QPointF fPoint = VToolNormal::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), - qApp->toPixel(result), angle); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + QPointF fPoint = VToolNormal::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), + qApp->toPixel(result), angle); + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + } + else + { + data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + if (parse != Document::FullParse) { - id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - } - else - { - data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::NormalTool, doc); - if (parse == Document::FullParse) - { - VToolNormal *point = new VToolNormal(doc, data, id, typeLine, formula, angle, - firstPointId, secondPointId, typeCreation); - scene->addItem(point); - connect(point, &VToolNormal::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolNormal::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(firstPointId); - doc->IncrementReferens(secondPointId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::NormalTool, doc); + if (parse == Document::FullParse) + { + VToolNormal *point = new VToolNormal(doc, data, id, typeLine, formula, angle, + firstPointId, secondPointId, typeCreation); + scene->addItem(point); + connect(point, &VToolNormal::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolNormal::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(firstPointId); + doc->IncrementReferens(secondPointId); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolpointofcontact.cpp b/src/app/tools/drawTools/vtoolpointofcontact.cpp index 305ee98dd..6cae7f869 100644 --- a/src/app/tools/drawTools/vtoolpointofcontact.cpp +++ b/src/app/tools/drawTools/vtoolpointofcontact.cpp @@ -117,46 +117,59 @@ void VToolPointOfContact::Create(const quint32 _id, const QString &radius, const const VPointF *firstP = data->GeometricObject(firstPointId); const VPointF *secondP = data->GeometricObject(secondPointId); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(radius, &errorMsg); - if (errorMsg.isEmpty()) + qreal result = 0; + try { - QPointF fPoint = VToolPointOfContact::FindPoint(qApp->toPixel(result), centerP->toQPointF(), - firstP->toQPointF(), secondP->toQPointF()); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + Calculator cal(data); + result = cal.EvalFormula(radius); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + QPointF fPoint = VToolPointOfContact::FindPoint(qApp->toPixel(result), centerP->toQPointF(), + firstP->toQPointF(), secondP->toQPointF()); + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + data->AddLine(secondPointId, id); + data->AddLine(center, id); + } + else + { + data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(firstPointId, id); + data->AddLine(secondPointId, id); + data->AddLine(center, id); + if (parse != Document::FullParse) { - id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - data->AddLine(secondPointId, id); - data->AddLine(center, id); - } - else - { - data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(firstPointId, id); - data->AddLine(secondPointId, id); - data->AddLine(center, id); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::PointOfContact, doc); - if (parse == Document::FullParse) - { - VToolPointOfContact *point = new VToolPointOfContact(doc, data, id, radius, center, - firstPointId, secondPointId, typeCreation); - scene->addItem(point); - connect(point, &VToolPointOfContact::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPointOfContact::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(center); - doc->IncrementReferens(firstPointId); - doc->IncrementReferens(secondPointId); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::PointOfContact, doc); + if (parse == Document::FullParse) + { + VToolPointOfContact *point = new VToolPointOfContact(doc, data, id, radius, center, + firstPointId, secondPointId, typeCreation); + scene->addItem(point); + connect(point, &VToolPointOfContact::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolPointOfContact::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(center); + doc->IncrementReferens(firstPointId); + doc->IncrementReferens(secondPointId); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolshoulderpoint.cpp b/src/app/tools/drawTools/vtoolshoulderpoint.cpp index cdd026c81..a09e43163 100644 --- a/src/app/tools/drawTools/vtoolshoulderpoint.cpp +++ b/src/app/tools/drawTools/vtoolshoulderpoint.cpp @@ -119,45 +119,58 @@ void VToolShoulderPoint::Create(const quint32 _id, const QString &formula, const const VPointF *secondPoint = data->GeometricObject(p2Line); const VPointF *shoulderPoint = data->GeometricObject(pShoulder); - Calculator cal(data); - QString errorMsg; - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty()) + qreal result = 0; + try { - QPointF fPoint = VToolShoulderPoint::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), - shoulderPoint->toQPointF(), qApp->toPixel(result)); - quint32 id = _id; - if (typeCreation == Valentina::FromGui) + Calculator cal(data); + result = cal.EvalFormula(formula); + } + catch(qmu::QmuParserError &e) + { + //TODO show error message + qDebug() << "\nError:\n" + << "--------\n" + << "Message: " << e.GetMsg() << "\n" + << "Expression: \"" << e.GetExpr() << "\"\n" + << "Token: \"" << e.GetToken() << "\"\n" + << "Position: " << e.GetPos() << "\n" + << "Errc: " << QString::number(e.GetCode(), 16); + return; + } + + QPointF fPoint = VToolShoulderPoint::FindPoint(firstPoint->toQPointF(), secondPoint->toQPointF(), + shoulderPoint->toQPointF(), qApp->toPixel(result)); + quint32 id = _id; + if (typeCreation == Valentina::FromGui) + { + id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(p1Line, id); + data->AddLine(p2Line, id); + } + else + { + data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); + data->AddLine(p1Line, id); + data->AddLine(p2Line, id); + if (parse != Document::FullParse) { - id = data->AddGObject(new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(p1Line, id); - data->AddLine(p2Line, id); - } - else - { - data->UpdateGObject(id, new VPointF(fPoint.x(), fPoint.y(), pointName, mx, my)); - data->AddLine(p1Line, id); - data->AddLine(p2Line, id); - if (parse != Document::FullParse) - { - doc->UpdateToolData(id, data); - } - } - VDrawTool::AddRecord(id, Valentina::ShoulderPointTool, doc); - if (parse == Document::FullParse) - { - VToolShoulderPoint *point = new VToolShoulderPoint(doc, data, id, typeLine, formula, - p1Line, p2Line, pShoulder, - typeCreation); - scene->addItem(point); - connect(point, &VToolShoulderPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); - connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolShoulderPoint::SetFactor); - doc->AddTool(id, point); - doc->IncrementReferens(p1Line); - doc->IncrementReferens(p2Line); - doc->IncrementReferens(pShoulder); + doc->UpdateToolData(id, data); } } + VDrawTool::AddRecord(id, Valentina::ShoulderPointTool, doc); + if (parse == Document::FullParse) + { + VToolShoulderPoint *point = new VToolShoulderPoint(doc, data, id, typeLine, formula, + p1Line, p2Line, pShoulder, + typeCreation); + scene->addItem(point); + connect(point, &VToolShoulderPoint::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem); + connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolShoulderPoint::SetFactor); + doc->AddTool(id, point); + doc->IncrementReferens(p1Line); + doc->IncrementReferens(p2Line); + doc->IncrementReferens(pShoulder); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/qmuparser/qmuparserbase.cpp b/src/libs/qmuparser/qmuparserbase.cpp index 386c0f87d..fe313aeae 100644 --- a/src/libs/qmuparser/qmuparserbase.cpp +++ b/src/libs/qmuparser/qmuparserbase.cpp @@ -555,17 +555,12 @@ void QmuParserBase::DefineStrConst(const QString &a_strName, const QString &a_st /** * @brief Add a user defined variable. * @param [in] a_sName the variable name - * @param [in] a_pVar A pointer to the variable vaule. + * @param [in] a_pVar the variable vaule. * @post Will reset the Parser to string parsing mode. - * @throw ParserException in case the name contains invalid signs or a_pVar is NULL. + * @throw ParserException in case the name contains invalid signs. */ -void QmuParserBase::DefineVar(const QString &a_sName, qreal *a_pVar) +void QmuParserBase::DefineVar(const QString &a_sName, qreal a_pVar) { - if (a_pVar==0) - { - Error(ecINVALID_VAR_PTR); - } - // Test if a constant with that names already exists if (m_ConstDef.find(a_sName)!=m_ConstDef.end()) { @@ -1241,25 +1236,25 @@ qreal QmuParserBase::ParseCmdCodeBulk(int nOffset, int nThreadID) const // value and variable tokens case cmVAR: - Stack[++sidx] = *(pTok->Val.ptr + nOffset); + Stack[++sidx] = *(&pTok->Val.ptr + nOffset); continue; case cmVAL: Stack[++sidx] = pTok->Val.data2; continue; case cmVARPOW2: - buf = *(pTok->Val.ptr + nOffset); + buf = *(&pTok->Val.ptr + nOffset); Stack[++sidx] = buf*buf; continue; case cmVARPOW3: - buf = *(pTok->Val.ptr + nOffset); + buf = *(&pTok->Val.ptr + nOffset); Stack[++sidx] = buf*buf*buf; continue; case cmVARPOW4: - buf = *(pTok->Val.ptr + nOffset); + buf = *(&pTok->Val.ptr + nOffset); Stack[++sidx] = buf*buf*buf*buf; continue; case cmVARMUL: - Stack[++sidx] = *(pTok->Val.ptr + nOffset) * pTok->Val.data + pTok->Val.data2; + Stack[++sidx] = *(&pTok->Val.ptr + nOffset) * pTok->Val.data + pTok->Val.data2; continue; // Next is treatment of numeric functions case cmFUNC: @@ -1497,7 +1492,7 @@ void QmuParserBase::CreateRPN() const break; case cmVAR: stVal.push(opt); - m_vRPN.AddVar( static_cast(opt.GetVar()) ); + m_vRPN.AddVar( opt.GetVar() ); break; case cmVAL: stVal.push(opt); diff --git a/src/libs/qmuparser/qmuparserbase.h b/src/libs/qmuparser/qmuparserbase.h index d01e78ce2..348bd0156 100644 --- a/src/libs/qmuparser/qmuparserbase.h +++ b/src/libs/qmuparser/qmuparserbase.h @@ -79,7 +79,7 @@ public: EOprtAssociativity a_eAssociativity = oaLEFT, bool a_bAllowOpt = false); void DefineConst(const QString &a_sName, qreal a_fVal); void DefineStrConst(const QString &a_sName, const QString &a_strVal); - void DefineVar(const QString &a_sName, qreal *a_fVar); + void DefineVar(const QString &a_sName, qreal a_fVar); void DefinePostfixOprt(const QString &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true); void DefineInfixOprt(const QString &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true); diff --git a/src/libs/qmuparser/qmuparserbytecode.cpp b/src/libs/qmuparser/qmuparserbytecode.cpp index cdd8e63ce..55acc78c4 100644 --- a/src/libs/qmuparser/qmuparserbytecode.cpp +++ b/src/libs/qmuparser/qmuparserbytecode.cpp @@ -98,7 +98,7 @@ void QmuParserByteCode::Assign(const QmuParserByteCode &a_ByteCode) Q_DECL_NOEXC * @param a_pVar Pointer to be added. * @throw nothrow */ -void QmuParserByteCode::AddVar(qreal *a_pVar) Q_DECL_NOEXCEPT +void QmuParserByteCode::AddVar(qreal a_pVar) Q_DECL_NOEXCEPT { ++m_iStackPos; m_iMaxStackSize = qMax(m_iMaxStackSize, static_cast(m_iStackPos)); @@ -134,7 +134,7 @@ void QmuParserByteCode::AddVal(qreal a_fVal) Q_DECL_NOEXCEPT // If optimization does not apply SToken tok; tok.Cmd = cmVAL; - tok.Val.ptr = nullptr; + tok.Val.ptr = 0; tok.Val.data = 0; tok.Val.data2 = a_fVal; m_vRPN.push_back(tok); @@ -355,14 +355,14 @@ void QmuParserByteCode::AddOp(ECmdCode a_Oprt) (m_vRPN[sz-1].Cmd == cmVARMUL && m_vRPN[sz-2].Cmd == cmVARMUL && m_vRPN[sz-2].Val.ptr == m_vRPN[sz-1].Val.ptr) ) { - assert( (m_vRPN[sz-2].Val.ptr==nullptr && m_vRPN[sz-1].Val.ptr!=nullptr) || - (m_vRPN[sz-2].Val.ptr!=nullptr && m_vRPN[sz-1].Val.ptr==nullptr) || + assert( (m_vRPN[sz-2].Val.ptr==0 && m_vRPN[sz-1].Val.ptr!=0) || + (m_vRPN[sz-2].Val.ptr!=0 && m_vRPN[sz-1].Val.ptr==0) || (m_vRPN[sz-2].Val.ptr == m_vRPN[sz-1].Val.ptr) ); m_vRPN[sz-2].Cmd = cmVARMUL; - m_vRPN[sz-2].Val.ptr = reinterpret_cast( - reinterpret_cast(m_vRPN[sz-2].Val.ptr) | - reinterpret_cast(m_vRPN[sz-1].Val.ptr)); // variable + m_vRPN[sz-2].Val.ptr = static_cast( + static_cast(m_vRPN[sz-2].Val.ptr) | + static_cast(m_vRPN[sz-1].Val.ptr)); // variable m_vRPN[sz-2].Val.data2 += ((a_Oprt==cmSUB) ? -1 : 1) * m_vRPN[sz-1].Val.data2; // offset m_vRPN[sz-2].Val.data += ((a_Oprt==cmSUB) ? -1 : 1) * m_vRPN[sz-1].Val.data; // multiplikatior m_vRPN.pop_back(); @@ -374,9 +374,9 @@ void QmuParserByteCode::AddOp(ECmdCode a_Oprt) (m_vRPN[sz-1].Cmd == cmVAL && m_vRPN[sz-2].Cmd == cmVAR) ) { m_vRPN[sz-2].Cmd = cmVARMUL; - m_vRPN[sz-2].Val.ptr = reinterpret_cast( - reinterpret_cast(m_vRPN[sz-2].Val.ptr) | - reinterpret_cast(m_vRPN[sz-1].Val.ptr)); + m_vRPN[sz-2].Val.ptr = static_cast( + static_cast(m_vRPN[sz-2].Val.ptr) | + static_cast(m_vRPN[sz-1].Val.ptr)); m_vRPN[sz-2].Val.data = m_vRPN[sz-2].Val.data2 + m_vRPN[sz-1].Val.data2; m_vRPN[sz-2].Val.data2 = 0; m_vRPN.pop_back(); @@ -387,9 +387,9 @@ void QmuParserByteCode::AddOp(ECmdCode a_Oprt) { // Optimization: 2*(3*b+1) or (3*b+1)*2 -> 6*b+2 m_vRPN[sz-2].Cmd = cmVARMUL; - m_vRPN[sz-2].Val.ptr = reinterpret_cast( - reinterpret_cast(m_vRPN[sz-2].Val.ptr) | - reinterpret_cast(m_vRPN[sz-1].Val.ptr)); + m_vRPN[sz-2].Val.ptr = static_cast( + static_cast(m_vRPN[sz-2].Val.ptr) | + static_cast(m_vRPN[sz-1].Val.ptr)); if (m_vRPN[sz-1].Cmd == cmVAL) { m_vRPN[sz-2].Val.data *= m_vRPN[sz-1].Val.data2; @@ -491,7 +491,7 @@ void QmuParserByteCode::AddIfElse(ECmdCode a_Oprt) Q_DECL_NOEXCEPT * * @sa ParserToken::ECmdCode */ -void QmuParserByteCode::AddAssignOp(qreal *a_pVar) Q_DECL_NOEXCEPT +void QmuParserByteCode::AddAssignOp(qreal a_pVar) Q_DECL_NOEXCEPT { --m_iStackPos; @@ -691,19 +691,19 @@ void QmuParserByteCode::AsciiDump() qDebug() << "VAL \t" << "[" << m_vRPN[i].Val.data2 << "]\n"; break; case cmVAR: - qDebug() << "VAR \t" << "[ADDR: 0x" << QString::number(*m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; + qDebug() << "VAR \t" << "[ADDR: 0x" << QString::number(m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; break; case cmVARPOW2: - qDebug() << "VARPOW2 \t" << "[ADDR: 0x" << QString::number(*m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; + qDebug() << "VARPOW2 \t" << "[ADDR: 0x" << QString::number(m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; break; case cmVARPOW3: - qDebug() << "VARPOW3 \t" << "[ADDR: 0x" << QString::number(*m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; + qDebug() << "VARPOW3 \t" << "[ADDR: 0x" << QString::number(m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; break; case cmVARPOW4: - qDebug() << "VARPOW4 \t" << "[ADDR: 0x" << QString::number(*m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; + qDebug() << "VARPOW4 \t" << "[ADDR: 0x" << QString::number(m_vRPN[i].Val.ptr, 'f', 16) << "]\n"; break; case cmVARMUL: - qDebug() << "VARMUL \t" << "[ADDR: 0x" << QString::number(*m_vRPN[i].Val.ptr, 'f', 16) << "]" << " * [" + qDebug() << "VARMUL \t" << "[ADDR: 0x" << QString::number(m_vRPN[i].Val.ptr, 'f', 16) << "]" << " * [" << m_vRPN[i].Val.data << "]" << " + [" << m_vRPN[i].Val.data2 << "]\n"; break; case cmFUNC: diff --git a/src/libs/qmuparser/qmuparserbytecode.h b/src/libs/qmuparser/qmuparserbytecode.h index 09e27bef8..f2aa0ccbf 100644 --- a/src/libs/qmuparser/qmuparserbytecode.h +++ b/src/libs/qmuparser/qmuparserbytecode.h @@ -43,7 +43,7 @@ struct SToken { struct //SValData { - qreal *ptr; + qreal ptr; qreal data; qreal data2; } Val; @@ -85,11 +85,11 @@ public: QmuParserByteCode(const QmuParserByteCode &a_ByteCode) Q_DECL_NOEXCEPT; QmuParserByteCode& operator=(const QmuParserByteCode &a_ByteCode) Q_DECL_NOEXCEPT; void Assign(const QmuParserByteCode &a_ByteCode) Q_DECL_NOEXCEPT; - void AddVar(qreal *a_pVar) Q_DECL_NOEXCEPT; + void AddVar(qreal a_pVar) Q_DECL_NOEXCEPT; void AddVal(qreal a_fVal) Q_DECL_NOEXCEPT; void AddOp(ECmdCode a_Oprt); void AddIfElse(ECmdCode a_Oprt) Q_DECL_NOEXCEPT; - void AddAssignOp(qreal *a_pVar) Q_DECL_NOEXCEPT; + void AddAssignOp(qreal a_pVar) Q_DECL_NOEXCEPT; void AddFun(generic_fun_type a_pFun, int a_iArgc) Q_DECL_NOEXCEPT; void AddBulkFun(generic_fun_type a_pFun, int a_iArgc) Q_DECL_NOEXCEPT; void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx) Q_DECL_NOEXCEPT; diff --git a/src/libs/qmuparser/qmuparserdef.h b/src/libs/qmuparser/qmuparserdef.h index a15557d9c..a3b0e0ccb 100644 --- a/src/libs/qmuparser/qmuparserdef.h +++ b/src/libs/qmuparser/qmuparserdef.h @@ -173,7 +173,7 @@ typedef std::basic_stringstream < char_type, std::char_traits, std::a // Data container types /** @brief Type used for storing variables. */ -typedef std::map varmap_type; +typedef std::map varmap_type; /** @brief Type used for storing constants. */ typedef std::map valmap_type; @@ -268,7 +268,7 @@ typedef qreal ( *strfun_type3 ) ( const QString &, qreal, qreal ); typedef int ( *identfun_type ) ( const QString &sExpr, int *nPos, qreal *fVal ); /** @brief Callback used for variable creation factory functions. */ -typedef qreal* ( *facfun_type ) ( const QString &, void* ); +typedef qreal ( *facfun_type ) ( const QString &, void* ); } // end of namespace #endif diff --git a/src/libs/qmuparser/qmuparsertest.cpp b/src/libs/qmuparser/qmuparsertest.cpp index cce7cdfc2..fc237c0a6 100644 --- a/src/libs/qmuparser/qmuparsertest.cpp +++ b/src/libs/qmuparser/qmuparsertest.cpp @@ -103,9 +103,9 @@ int QmuParserTester::TestInterface() try { - p.DefineVar ( "a", &afVal[0] ); - p.DefineVar ( "b", &afVal[1] ); - p.DefineVar ( "c", &afVal[2] ); + p.DefineVar ( "a", afVal[0] ); + p.DefineVar ( "b", afVal[1] ); + p.DefineVar ( "c", afVal[2] ); p.SetExpr ( "a+b+c" ); p.Eval(); } @@ -280,21 +280,21 @@ int QmuParserTester::TestNames() // variable names qreal a; p.ClearConst(); - PARSER_THROWCHECK ( Var, false, "123abc", &a ) - PARSER_THROWCHECK ( Var, false, "9a", &a ) - PARSER_THROWCHECK ( Var, false, "0a", &a ) - PARSER_THROWCHECK ( Var, false, "+a", &a ) - PARSER_THROWCHECK ( Var, false, "-a", &a ) - PARSER_THROWCHECK ( Var, false, "?a", &a ) - PARSER_THROWCHECK ( Var, false, "!a", &a ) - PARSER_THROWCHECK ( Var, false, "a+", &a ) - PARSER_THROWCHECK ( Var, false, "a-", &a ) - PARSER_THROWCHECK ( Var, false, "a*", &a ) - PARSER_THROWCHECK ( Var, false, "a?", &a ) - PARSER_THROWCHECK ( Var, true, "a", &a ) - PARSER_THROWCHECK ( Var, true, "a_min", &a ) - PARSER_THROWCHECK ( Var, true, "a_min0", &a ) - PARSER_THROWCHECK ( Var, true, "a_min9", &a ) + PARSER_THROWCHECK ( Var, false, "123abc", a ) + PARSER_THROWCHECK ( Var, false, "9a", a ) + PARSER_THROWCHECK ( Var, false, "0a", a ) + PARSER_THROWCHECK ( Var, false, "+a", a ) + PARSER_THROWCHECK ( Var, false, "-a", a ) + PARSER_THROWCHECK ( Var, false, "?a", a ) + PARSER_THROWCHECK ( Var, false, "!a", a ) + PARSER_THROWCHECK ( Var, false, "a+", a ) + PARSER_THROWCHECK ( Var, false, "a-", a ) + PARSER_THROWCHECK ( Var, false, "a*", a ) + PARSER_THROWCHECK ( Var, false, "a?", a ) + PARSER_THROWCHECK ( Var, true, "a", a ) + PARSER_THROWCHECK ( Var, true, "a_min", a ) + PARSER_THROWCHECK ( Var, true, "a_min0", a ) + PARSER_THROWCHECK ( Var, true, "a_min9", a ) PARSER_THROWCHECK ( Var, false, "a_min9", 0 ) // Postfix operators // fail @@ -449,11 +449,11 @@ int QmuParserTester::TestVarConst() int idx; qmu::QmuParser p; qreal vVarVal[] = { 1, 2, 3, 4, 5}; - p.DefineVar ( "a", &vVarVal[0] ); - p.DefineVar ( "b", &vVarVal[1] ); - p.DefineVar ( "c", &vVarVal[2] ); - p.DefineVar ( "d", &vVarVal[3] ); - p.DefineVar ( "e", &vVarVal[4] ); + p.DefineVar ( "a", vVarVal[0] ); + p.DefineVar ( "b", vVarVal[1] ); + p.DefineVar ( "c", vVarVal[2] ); + p.DefineVar ( "d", vVarVal[3] ); + p.DefineVar ( "e", vVarVal[4] ); // Test lookup of defined variables // 4 used variables @@ -475,7 +475,7 @@ int QmuParserTester::TestVarConst() qmu::varmap_type::const_iterator item = UsedVar.begin(); for ( idx = 0; item != UsedVar.end(); ++item ) { - if ( &vVarVal[idx++] != item->second ) + if ( vVarVal[idx++] != item->second ) { throw false; } @@ -516,7 +516,7 @@ int QmuParserTester::TestVarConst() item = UsedVar.begin(); for ( idx = 0; item != UsedVar.end(); ++item ) { - if ( &vVarVal[idx++] != item->second ) + if ( vVarVal[idx++] != item->second ) { throw false; } @@ -1118,9 +1118,9 @@ int QmuParserTester::ThrowTest ( const QString &a_str, int a_iErrc, bool a_bFail qreal fVal[] = {1, 1, 1}; QmuParser p; - p.DefineVar ( "a", &fVal[0] ); - p.DefineVar ( "b", &fVal[1] ); - p.DefineVar ( "c", &fVal[2] ); + p.DefineVar ( "a", fVal[0] ); + p.DefineVar ( "b", fVal[1] ); + p.DefineVar ( "c", fVal[2] ); p.DefinePostfixOprt ( "{m}", Milli ); p.DefinePostfixOprt ( "m", Milli ); p.DefineFun ( "ping", Ping ); @@ -1173,7 +1173,7 @@ int QmuParserTester::EqnTestWithVarChange ( const QString &a_str, double a_fVar1 // variable qreal var = 0; - p.DefineVar ( "a", &var ); + p.DefineVar ( "a", var ); p.SetExpr ( a_str ); var = a_fVar1; @@ -1242,11 +1242,11 @@ int QmuParserTester::EqnTest ( const QString &a_str, double a_fRes, bool a_fPass p1->DefineConst ( "const2", 3 ); // variables qreal vVarVal[] = { 1, 2, 3, -2}; - p1->DefineVar ( "a", &vVarVal[0] ); - p1->DefineVar ( "aa", &vVarVal[1] ); - p1->DefineVar ( "b", &vVarVal[1] ); - p1->DefineVar ( "c", &vVarVal[2] ); - p1->DefineVar ( "d", &vVarVal[3] ); + p1->DefineVar ( "a", vVarVal[0] ); + p1->DefineVar ( "aa", vVarVal[1] ); + p1->DefineVar ( "b", vVarVal[1] ); + p1->DefineVar ( "c", vVarVal[2] ); + p1->DefineVar ( "d", vVarVal[3] ); // custom value ident functions p1->AddValIdent ( &QmuParserTester::IsHexVal ); diff --git a/src/libs/qmuparser/qmuparsertoken.h b/src/libs/qmuparser/qmuparsertoken.h index 5dd720766..921b9dcba 100644 --- a/src/libs/qmuparser/qmuparsertoken.h +++ b/src/libs/qmuparser/qmuparsertoken.h @@ -196,13 +196,13 @@ public: * Member variables not necessary for variable tokens will be invalidated. * @throw nothrow */ - QmuParserToken& SetVar ( TBase *a_pVar, const TString &a_strTok ) + QmuParserToken& SetVar ( TBase a_pVar, const TString &a_strTok ) { m_iCode = cmVAR; m_iType = tpDBL; m_strTok = a_strTok; m_iIdx = -1; - m_pTok = reinterpret_cast ( a_pVar ); + m_pTok = a_pVar; m_pCallback.reset ( 0 ); return *this; } @@ -357,7 +357,7 @@ public: case cmVAL: return m_fVal; case cmVAR: - return * ( reinterpret_cast(m_pTok) ); + return m_pTok; case cmLE: case cmGE: case cmNEQ: @@ -404,14 +404,14 @@ public: * Valid only if m_iType==CmdVar. * @throw QmuParserError if token is no variable token. */ - TBase* GetVar() const + TBase GetVar() const { if ( m_iCode != cmVAR ) { throw QmuParserError ( ecINTERNAL_ERROR ); } - return reinterpret_cast( m_pTok ); + return m_pTok; } //------------------------------------------------------------------------------ @@ -449,7 +449,7 @@ public: private: ECmdCode m_iCode; ///< Type of the token; The token type is a constant of type #ECmdCode. ETypeCode m_iType; - void *m_pTok; ///< Stores Token pointer; not applicable for all tokens + TBase m_pTok; ///< Stores Token pointer; not applicable for all tokens int m_iIdx; ///< An otional index to an external buffer storing the token data TString m_strTok; ///< Token string TString m_strVal; ///< Value for string variables diff --git a/src/libs/qmuparser/qmuparsertokenreader.cpp b/src/libs/qmuparser/qmuparsertokenreader.cpp index e2d869c9b..e082bd7a8 100644 --- a/src/libs/qmuparser/qmuparsertokenreader.cpp +++ b/src/libs/qmuparser/qmuparsertokenreader.cpp @@ -976,7 +976,7 @@ bool QmuParserTokenReader::IsUndefVarTok ( token_type &a_Tok ) Q_DECL_NOEXCEPT // If a factory is available implicitely create new variables if ( m_pFactory ) { - qreal *fVar = m_pFactory ( strTok, m_pFactoryData ); + qreal fVar = m_pFactory ( strTok, m_pFactoryData ); a_Tok.SetVar ( fVar, strTok ); // Do not use m_pParser->DefineVar( strTok, fVar ); @@ -990,7 +990,7 @@ bool QmuParserTokenReader::IsUndefVarTok ( token_type &a_Tok ) Q_DECL_NOEXCEPT } else { - a_Tok.SetVar ( &m_fZero, strTok ); + a_Tok.SetVar ( m_fZero, strTok ); m_UsedVar[strTok] = 0; // Add variable to used-var-list }