diff --git a/src/Mod/Spreadsheet/App/Cell.cpp b/src/Mod/Spreadsheet/App/Cell.cpp index 07e7123c40..500b01a5c2 100644 --- a/src/Mod/Spreadsheet/App/Cell.cpp +++ b/src/Mod/Spreadsheet/App/Cell.cpp @@ -25,6 +25,8 @@ #ifndef _PreComp_ #endif +#include + #include #include #include "Cell.h" @@ -32,6 +34,8 @@ #include #include #include +#include +#include #include #include #include @@ -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(prop); + qFormatted = QString::fromUtf8(stringProp->getValue()); + + } else if (prop->isDerivedFrom(App::PropertyQuantity::getClassTypeId())) { + double rawVal = static_cast(prop)->getValue(); + const App::PropertyQuantity * floatProp = static_cast(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(prop)->getValue(); + } else { + rawVal = static_cast(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; +} + diff --git a/src/Mod/Spreadsheet/App/Cell.h b/src/Mod/Spreadsheet/App/Cell.h index 0a5dee8109..e8bdb1e669 100644 --- a/src/Mod/Spreadsheet/App/Cell.h +++ b/src/Mod/Spreadsheet/App/Cell.h @@ -122,6 +122,8 @@ public: App::CellAddress getAddress() const { return address; } + std::string getFormattedQuantity(void); + /* Alignment */ static const int ALIGNMENT_LEFT; static const int ALIGNMENT_HCENTER; diff --git a/src/Mod/TechDraw/App/DrawViewSpreadsheet.cpp b/src/Mod/TechDraw/App/DrawViewSpreadsheet.cpp index e4bdab3e1e..69c29e3259 100644 --- a/src/Mod/TechDraw/App/DrawViewSpreadsheet.cpp +++ b/src/Mod/TechDraw/App/DrawViewSpreadsheet.cpp @@ -257,14 +257,15 @@ std::string DrawViewSpreadsheet::getSheetImage(void) cellwidth = sheet->getColumnWidth(address.col()); cellheight = sheet->getRowHeight(address.row()); celltext = ""; + Spreadsheet::Cell* cell = sheet->getCell(address); // get the text App::Property* prop = sheet->getPropertyByName(address.toString().c_str()); std::stringstream field; if (prop != 0) { - if (prop->isDerivedFrom((App::PropertyQuantity::getClassTypeId()))) - field << static_cast(prop)->getValue(); - else if (prop->isDerivedFrom((App::PropertyFloat::getClassTypeId()))) - field << static_cast(prop)->getValue(); + if (prop->isDerivedFrom((App::PropertyQuantity::getClassTypeId()))) { + field << cell->getFormattedQuantity(); + } else if (prop->isDerivedFrom((App::PropertyFloat::getClassTypeId()))) + field << cell->getFormattedQuantity(); else if (prop->isDerivedFrom((App::PropertyString::getClassTypeId()))) field << static_cast(prop)->getValue(); else @@ -276,7 +277,6 @@ std::string DrawViewSpreadsheet::getSheetImage(void) std::string bcolor = "none"; std::string fcolor = c.asCSSString(); std::string textstyle = ""; - Spreadsheet::Cell* cell = sheet->getCell(address); if (cell) { App::Color f,b; std::set st;