From c752d0cdf9a8a7574bd1895eecb06cd76455bdbb Mon Sep 17 00:00:00 2001 From: AgCaliva Date: Tue, 9 May 2023 19:28:17 -0300 Subject: [PATCH] Added per project Unit System feature --- src/Gui/Application.cpp | 15 +++++++++++ src/Gui/DlgGeneral.ui | 36 +++++++++++++++++++++++--- src/Gui/DlgGeneralImp.cpp | 53 ++++++++++++++++++++++++++++++++++++++- src/Gui/DlgGeneralImp.h | 1 + src/Gui/Document.cpp | 45 +++++++++++++++++++++++++++++++++ src/Gui/Document.h | 6 +++++ 6 files changed, 151 insertions(+), 5 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 4ba6c4b2d3..c7621de622 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -54,6 +54,8 @@ #include #include +#include + #include #include @@ -927,6 +929,19 @@ void Application::slotActiveDocument(const App::Document& Doc) Py::Module("FreeCADGui").setAttr(std::string("ActiveDocument"),Py::None()); } } + + //Set Unit System. + int projectUnitSystemIndex = doc->second->getProjectUnitSystem(); + int ignore = doc->second->getProjectUnitSystemIgnore(); + if( projectUnitSystemIndex >= 0 && !ignore ){//is valid + Base::UnitsApi::setSchema(static_cast(projectUnitSystemIndex)); + }else{// set up Unit system default + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath + ("User parameter:BaseApp/Preferences/Units"); + Base::UnitsApi::setSchema((Base::UnitSystem)hGrp->GetInt("UserSchema",0)); + Base::UnitsApi::setDecimals(hGrp->GetInt("Decimals", Base::UnitsApi::getDecimals())); + } + signalActiveDocument(*doc->second); updateActions(); } diff --git a/src/Gui/DlgGeneral.ui b/src/Gui/DlgGeneral.ui index 70a13f7c8d..ef0816048b 100644 --- a/src/Gui/DlgGeneral.ui +++ b/src/Gui/DlgGeneral.ui @@ -87,14 +87,42 @@ + + + + Project Unit system: + + + + + + + + Unit system used for display metrics stored in project + + + + + + + + If enabled, Unit System stored in project will be ignored. + + + Ignore + + + + + Minimum fractional inch: - + Minimum fractional inch to be displayed @@ -136,14 +164,14 @@ - + Number format: - + UseLocaleFormatting @@ -168,7 +196,7 @@ - + If enabled, numerical keypad decimal separator diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index 9edb20b4e1..65feac3df1 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -38,6 +38,8 @@ #include #include +#include + #include "DlgGeneralImp.h" #include "ui_DlgGeneral.h" #include "Action.h" @@ -50,6 +52,7 @@ #include "PreferencePackManager.h" #include "Language/Translator.h" + using namespace Gui::Dialog; namespace fs = boost::filesystem; using namespace Base; @@ -90,6 +93,7 @@ DlgGeneralImp::DlgGeneralImp( QWidget* parent ) for (int i = 0; i < num; i++) { QString item = qApp->translate("Gui::Dialog::DlgGeneralImp", Base::UnitsApi::getDescription(static_cast(i))); ui->comboBox_UnitSystem->addItem(item, i); + ui->comboBox_projectUnitSystem->addItem(item, i); } // Enable/disable the fractional inch option depending on system @@ -202,7 +206,21 @@ void DlgGeneralImp::saveSettings() // Set and save the Unit System viewSystemIndex = ui->comboBox_UnitSystem->currentIndex(); - UnitsApi::setSchema(static_cast(viewSystemIndex)); + auto activeDoc = Gui::Application::Instance->activeDocument(); + bool projectUnitSystemIgnore = ui->checkBox_projectUnitSystemIgnore->isChecked(); + if(activeDoc){ + activeDoc->setProjectUnitSystemIgnore( projectUnitSystemIgnore ); + if(!projectUnitSystemIgnore){ + int projectUnitSystemIndex = ui->comboBox_projectUnitSystem->currentIndex(); + activeDoc->setProjectUnitSystem( projectUnitSystemIndex ); + UnitsApi::setSchema(static_cast(projectUnitSystemIndex)); + }else{ + UnitsApi::setSchema(static_cast(viewSystemIndex)); + } + }else{ + UnitsApi::setSchema(static_cast(viewSystemIndex)); + } + // ui->SubstituteDecimal->onSave(); ui->UseLocaleFormatting->onSave(); @@ -268,6 +286,24 @@ void DlgGeneralImp::loadSettings() // handy little equation. cbIndex = std::log2(FracInch) - 1; ui->comboBox_FracInch->setCurrentIndex(cbIndex); + + + auto activeDoc = Gui::Application::Instance->activeDocument(); + if(activeDoc){ + int us = activeDoc->getProjectUnitSystem(); + if(us >= 0){//Valid unit system: + ui->comboBox_projectUnitSystem->setCurrentIndex( us ); + int pusIgnore = activeDoc->getProjectUnitSystemIgnore(); + ui->checkBox_projectUnitSystemIgnore->setChecked( pusIgnore ); + }else{ + ui->comboBox_projectUnitSystem->setCurrentIndex( 0 ); + ui->checkBox_projectUnitSystemIgnore->setChecked( false ); + } + }else{ + ui->checkBox_projectUnitSystemIgnore->setEnabled(false); + ui->comboBox_projectUnitSystem->setEnabled(false); + } + ui->SubstituteDecimal->onRestore(); @@ -403,9 +439,11 @@ void DlgGeneralImp::changeEvent(QEvent *event) if (event->type() == QEvent::LanguageChange) { int index = ui->UseLocaleFormatting->currentIndex(); int index2 = ui->comboBox_UnitSystem->currentIndex(); + int pusIndex = ui->comboBox_projectUnitSystem->currentIndex(); ui->retranslateUi(this); ui->UseLocaleFormatting->setCurrentIndex(index); ui->comboBox_UnitSystem->setCurrentIndex(index2); + ui->comboBox_projectUnitSystem->setCurrentIndex(pusIndex); } else { QWidget::changeEvent(event); @@ -561,4 +599,17 @@ void DlgGeneralImp::onUnitSystemIndexChanged(int index) } } +void DlgGeneralImp::on_checkBox_projectUnitSystemIgnore_stateChanged(int state) +{ + if (state < 0) + return; // happens when clearing the combo box in retranslateUi() + + // Enable/disable the projectUnitSystem if being ignored: + if(state == 2){//ignore + ui->comboBox_projectUnitSystem->setEnabled(false); + }else if(state == 0){ + ui->comboBox_projectUnitSystem->setEnabled(true); + } +} + #include "moc_DlgGeneralImp.cpp" diff --git a/src/Gui/DlgGeneralImp.h b/src/Gui/DlgGeneralImp.h index 4039012aeb..1d40b8f370 100644 --- a/src/Gui/DlgGeneralImp.h +++ b/src/Gui/DlgGeneralImp.h @@ -66,6 +66,7 @@ protected Q_SLOTS: public Q_SLOTS: void onUnitSystemIndexChanged(int index); + void on_checkBox_projectUnitSystemIgnore_stateChanged(int state); private: void setRecentFileSize(); diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index ed84eb045b..3bbfc42b70 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -105,6 +105,9 @@ struct DocumentP std::map _CoinMap; std::map _ViewProviderMapAnnotation; std::list _redoViewProviders; + + int projectUnitSystem=-1; + bool projectUnitSystemIgnore; using Connection = boost::signals2::connection; Connection connectNewObject; @@ -646,6 +649,30 @@ void Document::setPos(const char* name, const Base::Matrix4D& rclMtrx) } +void Document::setProjectUnitSystem(int pUS) +{ + if(pUS != d->projectUnitSystem && pUS >= 0){ + d->projectUnitSystem = pUS; + setModified(true); + } +} + +int Document::getProjectUnitSystem() const +{ + return d->projectUnitSystem; +} + +void Document::setProjectUnitSystemIgnore(bool ignore) +{ + d->projectUnitSystemIgnore = ignore; + setModified(true); +} + +bool Document::getProjectUnitSystemIgnore() const +{ + return d->projectUnitSystemIgnore; +} + //***************************************************************************************************** // Document //***************************************************************************************************** @@ -1453,6 +1480,15 @@ void Document::RestoreDocFile(Base::Reader &reader) Base::Console().Error("%s\n", e.what()); } } + try{ + localreader->readElement("ProjectUnitSystem"); + int US = localreader->getAttributeAsInteger("US"); + int ignore = localreader->getAttributeAsInteger("ignore"); + d->projectUnitSystem = US; + d->projectUnitSystemIgnore = ignore; + }catch (const Base::XMLParseException& e) { + Base::Console().Error("ProjectUnitSystem not found\n"); + } } localreader->readEndElement("Document"); @@ -1575,6 +1611,15 @@ void Document::SaveDocFile (Base::Writer &writer) const writer.Stream() << writer.ind() << "\n"; writer.decInd(); // indentation for camera settings + + if( d->projectUnitSystem >= 0 ){ + writer.incInd(); // indentation for ProjectUnitSystem + writer.Stream() << writer.ind() << "projectUnitSystem << "\" ignore=\"" + << d->projectUnitSystemIgnore << "\"/>\n"; + writer.decInd(); // indentation for ProjectUnitSystem + } + writer.Stream() << "" << std::endl; } diff --git a/src/Gui/Document.h b/src/Gui/Document.h index 73df51510d..f7fdad5ebd 100644 --- a/src/Gui/Document.h +++ b/src/Gui/Document.h @@ -297,6 +297,12 @@ public: const char *getCameraSettings() const; bool saveCameraSettings(const char *) const; + + void setProjectUnitSystem(int); + int getProjectUnitSystem() const; + + void setProjectUnitSystemIgnore(bool); + bool getProjectUnitSystemIgnore() const; protected: // pointer to the python class