From ce05165f21b87a7c8602daf996dd530a2cc821a5 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 8 Jan 2022 19:37:24 +0100 Subject: [PATCH] App: replace three boolean of CellAddress::toString() with a bitmask of enums --- src/App/ObjectIdentifier.cpp | 2 +- src/App/Range.cpp | 14 ++++++++------ src/App/Range.h | 13 ++++++++++++- src/Mod/Spreadsheet/App/Sheet.cpp | 12 ++++++------ src/Mod/Spreadsheet/Gui/DlgSheetConf.cpp | 11 ++++++----- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/App/ObjectIdentifier.cpp b/src/App/ObjectIdentifier.cpp index 449208296b..e8e02e6299 100644 --- a/src/App/ObjectIdentifier.cpp +++ b/src/App/ObjectIdentifier.cpp @@ -307,7 +307,7 @@ bool ObjectIdentifier::verify(const App::Property &prop, bool silent) const { const std::string &name = components[result.propertyIndex].getName(); CellAddress addr; bool isAddress = addr.parseAbsoluteAddress(name.c_str()); - if((isAddress && addr.toString(true) != prop.getName()) || + if((isAddress && addr.toString(CellAddress::Cell::ShowRowColumn) != prop.getName()) || (!isAddress && name!=prop.getName())) { if(silent) return false; diff --git a/src/App/Range.cpp b/src/App/Range.cpp index 0e4f4651af..00c8fb8a27 100644 --- a/src/App/Range.cpp +++ b/src/App/Range.cpp @@ -234,15 +234,17 @@ App::CellAddress App::stringToAddress(const char * strAddress, bool silent) * @returns Address given as a string. */ -std::string App::CellAddress::toString(bool noAbsolute, bool r, bool c) const +std::string App::CellAddress::toString(Cell cell) const { std::stringstream s; - if(c) { - if(_absCol && !noAbsolute) + Base::Flags flags(cell); + if (flags.testFlag(Cell::ShowColumn)) { + if (_absCol && flags.testFlag(Cell::Absolute)) s << '$'; - if (col() < 26) + if (col() < 26) { s << (char)('A' + col()); + } else { int colnum = col() - 26; @@ -251,8 +253,8 @@ std::string App::CellAddress::toString(bool noAbsolute, bool r, bool c) const } } - if(r) { - if(_absRow && !noAbsolute) + if (flags.testFlag(Cell::ShowRow)) { + if (_absRow && flags.testFlag(Cell::Absolute)) s << '$'; s << (row() + 1); } diff --git a/src/App/Range.h b/src/App/Range.h index fd02734edc..9d339994d7 100644 --- a/src/App/Range.h +++ b/src/App/Range.h @@ -24,6 +24,7 @@ #define RANGE_H #include +#include #ifndef FC_GLOBAL_H #include #endif @@ -39,6 +40,14 @@ AppExport int validColumn(const std::string &colstr); AppExport int validRow(const std::string &rowstr); struct AppExport CellAddress { + // See call of ENABLE_BITMASK_OPERATORS + enum class Cell { + Absolute = 1, + ShowRow = 2, + ShowColumn = 4, + ShowRowColumn = ShowRow | ShowColumn, + ShowFull = Absolute | ShowRow | ShowColumn + }; CellAddress(int row = -1, int col = -1, bool absRow=false, bool absCol=false) : _row(row), _col(col), _absRow(absRow), _absCol(absCol) @@ -76,7 +85,7 @@ struct AppExport CellAddress { inline bool isAbsoluteCol() const { return _absCol; } - std::string toString(bool noAbsolute=false, bool row=true, bool col=true) const; + std::string toString(Cell = Cell::ShowFull) const; // Static members @@ -169,4 +178,6 @@ private: } +ENABLE_BITMASK_OPERATORS(App::CellAddress::Cell) + #endif // RANGE_H diff --git a/src/Mod/Spreadsheet/App/Sheet.cpp b/src/Mod/Spreadsheet/App/Sheet.cpp index 27e25130dc..1f14deb024 100644 --- a/src/Mod/Spreadsheet/App/Sheet.cpp +++ b/src/Mod/Spreadsheet/App/Sheet.cpp @@ -435,7 +435,7 @@ PyObject *Sheet::getPyObject(void) Property * Sheet::getProperty(CellAddress key) const { - return props.getDynamicPropertyByName(key.toString(true).c_str()); + return props.getDynamicPropertyByName(key.toString(CellAddress::Cell::ShowRowColumn).c_str()); } /** @@ -515,7 +515,7 @@ void Sheet::onSettingDocument() Property * Sheet::setFloatProperty(CellAddress key, double value) { - std::string name = key.toString(true); + std::string name = key.toString(CellAddress::Cell::ShowRowColumn); Property * prop = props.getDynamicPropertyByName(name.c_str()); PropertyFloat * floatProp; @@ -537,7 +537,7 @@ Property * Sheet::setFloatProperty(CellAddress key, double value) Property * Sheet::setIntegerProperty(CellAddress key, long value) { - std::string name = key.toString(true); + std::string name = key.toString(CellAddress::Cell::ShowRowColumn); Property * prop = props.getDynamicPropertyByName(name.c_str()); PropertyInteger * intProp; @@ -572,7 +572,7 @@ Property * Sheet::setIntegerProperty(CellAddress key, long value) Property * Sheet::setQuantityProperty(CellAddress key, double value, const Base::Unit & unit) { - std::string name = key.toString(true); + std::string name = key.toString(CellAddress::Cell::ShowRowColumn); Property * prop = props.getDynamicPropertyByName(name.c_str()); PropertySpreadsheetQuantity * quantityProp; @@ -607,7 +607,7 @@ Property * Sheet::setQuantityProperty(CellAddress key, double value, const Base: Property * Sheet::setStringProperty(CellAddress key, const std::string & value) { - std::string name = key.toString(true); + std::string name = key.toString(CellAddress::Cell::ShowRowColumn); Property * prop = props.getDynamicPropertyByName(name.c_str()); PropertyString * stringProp = freecad_dynamic_cast(prop); @@ -627,7 +627,7 @@ Property * Sheet::setStringProperty(CellAddress key, const std::string & value) Property * Sheet::setObjectProperty(CellAddress key, Py::Object object) { - std::string name = key.toString(true); + std::string name = key.toString(CellAddress::Cell::ShowRowColumn); Property * prop = props.getDynamicPropertyByName(name.c_str()); PropertyPythonObject * pyProp = freecad_dynamic_cast(prop); diff --git a/src/Mod/Spreadsheet/Gui/DlgSheetConf.cpp b/src/Mod/Spreadsheet/Gui/DlgSheetConf.cpp index 314183ec52..cc40d9b604 100644 --- a/src/Mod/Spreadsheet/Gui/DlgSheetConf.cpp +++ b/src/Mod/Spreadsheet/Gui/DlgSheetConf.cpp @@ -223,7 +223,7 @@ void DlgSheetConf::accept() // cell separately using a simpler expression can make it easy for us // to extract the name of the PropertyEnumeration for editing or unsetup. Gui::cmdAppObjectArgs(sheet, "set('%s', '=hiddenref(%s.String)')", - from.toString(true), prop->getFullName()); + from.toString(CellAddress::Cell::ShowRowColumn), prop->getFullName()); // Adjust the range to skip the first cell range = Range(from.row(),from.col()+1,to.row(),to.col()); @@ -232,9 +232,10 @@ void DlgSheetConf::accept() // PropertyEnumeration Gui::cmdAppObjectArgs(sheet, "setExpression('.cells.Bind.%s.%s', " "'tuple(.cells, <<%s>> + str(hiddenref(%s)+%d), <<%s>> + str(hiddenref(%s)+%d))')", - range.from().toString(true), range.to().toString(true), - range.from().toString(true,false,true), prop->getFullName(), from.row()+2, - range.to().toString(true,false,true), prop->getFullName(), from.row()+2); + range.from().toString(CellAddress::Cell::ShowRowColumn), + range.to().toString(CellAddress::Cell::ShowRowColumn), + range.from().toString(CellAddress::Cell::ShowColumn), prop->getFullName(), from.row()+2, + range.to().toString(CellAddress::Cell::ShowColumn), prop->getFullName(), from.row()+2); Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()"); Gui::Command::commitCommand(); @@ -272,7 +273,7 @@ void DlgSheetConf::onDiscard() { r.from().toString(), r.to().toString()); } - Gui::cmdAppObjectArgs(sheet, "clear('%s')", from.toString(true)); + Gui::cmdAppObjectArgs(sheet, "clear('%s')", from.toString(CellAddress::Cell::ShowRowColumn)); if(prop && prop->getName()) { auto obj = path.getDocumentObject();