Skip to content

Commit

Permalink
Fix #9132 - be more clever about displaying numbers in the diagrams (#…
Browse files Browse the repository at this point in the history
…9238)

- use the previous number as a hint on how to display the converted one
- if unit and displayUnit is one do not do any conversion
  • Loading branch information
adrpo committed Jul 15, 2022
1 parent 26b1dfa commit 2c88f03
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 7 additions & 5 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -32,6 +32,7 @@
* @author Adeel Asghar <adeel.asghar@liu.se>
*/

#include <iostream>
#include "TextAnnotation.h"
#include "Modeling/Commands.h"

Expand Down Expand Up @@ -559,7 +560,7 @@ void TextAnnotation::updateTextStringHelper(QRegExp regExp)
{
int pos = 0;
while ((pos = regExp.indexIn(mTextString, pos)) != -1) {
QString variable = regExp.cap(0).trimmed();
QString variable = regExp.cap(0).trimmed(); QString qs;
if ((!variable.isEmpty()) && (variable.compare("%%") != 0) && (variable.compare("%name") != 0) && (variable.compare("%class") != 0)) {
variable.remove("%");
variable = StringHandler::removeFirstLastCurlBrackets(variable);
Expand All @@ -585,23 +586,24 @@ void TextAnnotation::updateTextStringHelper(QRegExp regExp)
displayUnit = unit;
}
}
if (displayUnit.isEmpty() || unit.isEmpty()) {
mTextString.replace(pos, regExp.matchedLength(), textValue);
// do not do any conversion if unit or displayUnit is empty of if both are 1!
if (displayUnit.isEmpty() || unit.isEmpty() || (displayUnit.compare("1") == 0 && unit.compare("1") == 0)) {
qs = mTextString.replace(pos, regExp.matchedLength(), textValue);
pos += textValue.length();
} else {
QString textValueWithDisplayUnit;
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
OMCInterface::convertUnits_res convertUnit = pOMCProxy->convertUnits(unit, displayUnit);
if (convertUnit.unitsCompatible) {
qreal convertedValue = Utilities::convertUnit(textValue.toDouble(), convertUnit.offset, convertUnit.scaleFactor);
textValue = StringHandler::number(convertedValue);
textValue = StringHandler::number(convertedValue, textValue);
displayUnit = Utilities::convertUnitToSymbol(displayUnit);
textValueWithDisplayUnit = QString("%1 %2").arg(textValue, displayUnit);
} else {
unit = Utilities::convertUnitToSymbol(unit);
textValueWithDisplayUnit = QString("%1 %2").arg(textValue, unit);
}
mTextString.replace(pos, regExp.matchedLength(), textValueWithDisplayUnit);
qs = mTextString.replace(pos, regExp.matchedLength(), textValueWithDisplayUnit);
pos += textValueWithDisplayUnit.length();
}
} else { /* if the value of %\\W* is empty then remove the % sign. */
Expand Down
11 changes: 9 additions & 2 deletions OMEdit/OMEditLIB/Util/StringHandler.cpp
Expand Up @@ -1958,13 +1958,20 @@ QString StringHandler::insertClassAtPosition(QString parentClassText, QString ch
* \brief StringHandler::number
* Helper for QString::number with default precision of 16 instead of 6.
* \param value
* \param hint - default "" otherwise previous value to get a hint on how to format the new one
* \param format
* \param precision
* \return
*/
QString StringHandler::number(double value, char format, int precision)
QString StringHandler::number(double value, QString hint, char format, int precision)
{
return QString::number(value, format, precision);
// we have a hint, see if we can use it to display the number in a similar fashion
if (hint.contains("e", Qt::CaseInsensitive)) {
// we have an e in the hint, attempt to shorten the number!
return QString::number(value, format, QLocale::FloatingPointShortest);
} else {
return QString::number(value, format, precision);
}
}

static std::string cmt = "";
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Util/StringHandler.h
Expand Up @@ -170,7 +170,7 @@ class StringHandler : public QObject
static QString removeLeadingSpaces(QString contents);
static QString removeLine(QString text, QString lineToRemove);
static QString insertClassAtPosition(QString parentClassText, QString childClassText, int linePosition, int nestedLevel);
static QString number(double value, char format = 'g', int precision = 16);
static QString number(double value, QString hint = "", char format = 'g', int precision = 16);
static QString getModelicaComment(QString element);
static QString convertSemVertoReadableString(const QString &semver);
protected:
Expand Down

0 comments on commit 2c88f03

Please sign in to comment.