[TD][SS]Fix 4131 SS formatting in TD View

This commit is contained in:
wandererfan
2019-09-19 11:11:07 -04:00
committed by WandererFan
parent 1f4fb612dd
commit b1b1c4f0df
3 changed files with 60 additions and 5 deletions

View File

@@ -25,6 +25,8 @@
#ifndef _PreComp_
#endif
#include <QLocale>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "Cell.h"
@@ -32,6 +34,8 @@
#include <boost/tokenizer.hpp>
#include <Base/Reader.h>
#include <Base/Quantity.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Base/Writer.h>
#include <Base/Console.h>
#include <App/Expression.h>
@@ -960,4 +964,53 @@ App::Color Cell::decodeColor(const std::string & color, const App::Color & defau
return defaultColor;
}
//roughly based on Spreadsheet/Gui/SheetModel.cpp
std::string Cell::getFormattedQuantity(void)
{
std::string result;
QString qFormatted;
App::CellAddress thisCell = getAddress();
Property* prop = owner->sheet()->getPropertyByName(thisCell.toString().c_str());
if (prop->isDerivedFrom(App::PropertyString::getClassTypeId())) {
const App::PropertyString * stringProp = static_cast<const App::PropertyString*>(prop);
qFormatted = QString::fromUtf8(stringProp->getValue());
} else if (prop->isDerivedFrom(App::PropertyQuantity::getClassTypeId())) {
double rawVal = static_cast<App::PropertyQuantity*>(prop)->getValue();
const App::PropertyQuantity * floatProp = static_cast<const App::PropertyQuantity*>(prop);
DisplayUnit du;
bool hasDisplayUnit = getDisplayUnit(du);
double duScale = du.scaler;
const Base::Unit& computedUnit = floatProp->getUnit();
qFormatted = QLocale::system().toString(rawVal,'f',Base::UnitsApi::getDecimals());
if (hasDisplayUnit) {
if (computedUnit.isEmpty() || computedUnit == du.unit) {
QString number =
QLocale::system().toString(rawVal / duScale,'f',Base::UnitsApi::getDecimals());
qFormatted = number + Base::Tools::fromStdString(" " + displayUnit.stringRep);
}
}
} else if (prop->isDerivedFrom(App::PropertyFloat::getClassTypeId()) ||
prop->isDerivedFrom(App::PropertyInteger::getClassTypeId())) {
double rawVal;
if(prop->isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
rawVal = static_cast<const App::PropertyFloat*>(prop)->getValue();
} else {
rawVal = static_cast<const App::PropertyInteger*>(prop)->getValue();
}
DisplayUnit du;
bool hasDisplayUnit = getDisplayUnit(du);
double duScale = du.scaler;
qFormatted = QLocale::system().toString(rawVal,'f',Base::UnitsApi::getDecimals());
if (hasDisplayUnit) {
QString number = QLocale::system().toString(rawVal / duScale, 'f',Base::UnitsApi::getDecimals());
qFormatted = number + Base::Tools::fromStdString(" " + displayUnit.stringRep);
}
}
result = Base::Tools::toStdString(qFormatted);
return result;
}