From c2bab7a2fac1b3f1ca11bc696771d32dcb537c7c Mon Sep 17 00:00:00 2001
From: bgbsww <120601209+bgbsww@users.noreply.github.com>
Date: Mon, 4 Dec 2023 14:04:53 -0500
Subject: [PATCH] Move the unit schema into Project Information and remove all
Project Unit System code (#11266)
* Add unit system to Project Information and store with document.
* Remove the project unit system
* Restore correct document activation signalling to fix test fail
* Remove commented out dead lines
* Restore ignore option for project unit schemas
* Whitespace fix
* Refresh after changing units
* Remove field label
* Property editor changes applied to unit system
---
src/App/Document.cpp | 13 ++++
src/App/Document.h | 2 +
src/Gui/Application.cpp | 18 +++---
src/Gui/DlgProjectInformation.ui | 49 ++++++++++-----
src/Gui/DlgProjectInformationImp.cpp | 11 ++++
src/Gui/Document.cpp | 44 +------------
src/Gui/Document.h | 6 --
src/Gui/MainWindow.cpp | 45 +++++++++++---
src/Gui/MainWindow.h | 4 ++
.../PreferencePages/DlgSettingsGeneral.cpp | 62 ++++---------------
src/Gui/PreferencePages/DlgSettingsGeneral.h | 1 -
src/Gui/PreferencePages/DlgSettingsGeneral.ui | 20 +-----
12 files changed, 125 insertions(+), 150 deletions(-)
diff --git a/src/App/Document.cpp b/src/App/Document.cpp
index 907fb58e57..7d96a12fbe 100644
--- a/src/App/Document.cpp
+++ b/src/App/Document.cpp
@@ -93,6 +93,7 @@ recompute path. Also, it enables more complicated dependencies beyond trees.
#include
#include
#include
+#include
#include "Document.h"
#include "private/DocumentP.h"
@@ -821,6 +822,18 @@ Document::Document(const char* documentName)
0,
Prop_None,
"Additional tag to save the name of the company");
+ ADD_PROPERTY_TYPE(UnitSystem, (""), 0, Prop_None, "Unit system to use in this project");
+ // Set up the possible enum values for the unit system
+ int num = static_cast(Base::UnitSystem::NumUnitSystemTypes);
+ std::vector enumValsAsVector;
+ for (int i = 0; i < num; i++) {
+ QString item = Base::UnitsApi::getDescription(static_cast(i));
+ enumValsAsVector.emplace_back(item.toStdString());
+ }
+ UnitSystem.setEnums(enumValsAsVector);
+ // Get the preferences/General unit system as the default for a new document
+ ParameterGrp::handle hGrpu = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units");
+ UnitSystem.setValue(hGrpu->GetInt("UserSchema", 0));
ADD_PROPERTY_TYPE(Comment, (""), 0, Prop_None, "Additional tag to save a comment");
ADD_PROPERTY_TYPE(Meta, (), 0, Prop_None, "Map with additional meta information");
ADD_PROPERTY_TYPE(Material, (), 0, Prop_None, "Map with material properties");
diff --git a/src/App/Document.h b/src/App/Document.h
index 1f89b74ec5..2250f5056e 100644
--- a/src/App/Document.h
+++ b/src/App/Document.h
@@ -93,6 +93,8 @@ public:
PropertyString LastModifiedDate;
/// company name UTF8(optional)
PropertyString Company;
+ /// Unit System
+ PropertyEnumeration UnitSystem;
/// long comment or description (UTF8 with line breaks)
PropertyString Comment;
/// Id e.g. Part number
diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp
index c1227a1771..52ec628915 100644
--- a/src/Gui/Application.cpp
+++ b/src/Gui/Application.cpp
@@ -933,19 +933,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));
+
+ // Update the application to show the unit change
+ ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
+ ("User parameter:BaseApp/Preferences/Units");
+ if( Doc.FileName.getValue()[0] != '\0' && ! hGrp->GetBool("IgnoreProjectSchema")) {
+ int userSchema = Doc.UnitSystem.getValue();
+ Base::UnitsApi::setSchema(static_cast(userSchema));
+ getMainWindow()->setUserSchema(userSchema);
+ Application::Instance->onUpdate();
}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/DlgProjectInformation.ui b/src/Gui/DlgProjectInformation.ui
index 38254cbcfe..7e2a892cba 100644
--- a/src/Gui/DlgProjectInformation.ui
+++ b/src/Gui/DlgProjectInformation.ui
@@ -137,6 +137,23 @@
-
+
+
+ Unit System:
+
+
+ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter
+
+
+
+ -
+
+
+ Unit system for this file
+
+
+
+ -
Created &by:
@@ -149,7 +166,7 @@
- -
+
-
@@ -159,7 +176,7 @@
- -
+
-
Creation &date:
@@ -172,7 +189,7 @@
- -
+
-
@@ -185,7 +202,7 @@
- -
+
-
&Last modified by:
@@ -198,7 +215,7 @@
- -
+
-
@@ -208,7 +225,7 @@
- -
+
-
Last &modification date:
@@ -221,7 +238,7 @@
- -
+
-
@@ -234,7 +251,7 @@
- -
+
-
Com&pany:
@@ -247,7 +264,7 @@
- -
+
-
@@ -257,7 +274,7 @@
- -
+
-
License information:
@@ -267,10 +284,10 @@
- -
+
-
- -
+
-
License URL
@@ -280,7 +297,7 @@
- -
+
-
-
@@ -294,7 +311,7 @@
- -
+
-
&Comment:
@@ -307,10 +324,10 @@
- -
+
-
- -
+
-
Qt::Vertical
diff --git a/src/Gui/DlgProjectInformationImp.cpp b/src/Gui/DlgProjectInformationImp.cpp
index 8f3de3e7a8..7505fb2ed4 100644
--- a/src/Gui/DlgProjectInformationImp.cpp
+++ b/src/Gui/DlgProjectInformationImp.cpp
@@ -30,10 +30,12 @@
#include
#include
+#include
#include "DlgProjectInformationImp.h"
#include "ui_DlgProjectInformation.h"
+#include "MainWindow.h"
#if 0 // needed for Qt's lupdate utility
qApp->translate("Gui::Dialog::DlgSettingsDocument", "All rights reserved");
@@ -76,6 +78,14 @@ DlgProjectInformationImp::DlgProjectInformationImp(App::Document* doc, QWidget*
ui->lineEditLastModDate->setText(QString::fromUtf8(doc->LastModifiedDate.getValue()));
ui->lineEditCompany->setText(QString::fromUtf8(doc->Company.getValue()));
+ // Load comboBox with unit systems
+ int num = static_cast(Base::UnitSystem::NumUnitSystemTypes);
+ for (int i = 0; i < num; i++) {
+ QString item = Base::UnitsApi::getDescription(static_cast(i));
+ ui->comboBox_unitSystem->addItem(item, i);
+ }
+ ui->comboBox_unitSystem->setCurrentIndex(doc->UnitSystem.getValue());
+
// load comboBox with license names
for (const auto& item : App::licenseItems) {
const char* name {item.at(App::posnOfFullName)};
@@ -130,6 +140,7 @@ void DlgProjectInformationImp::accept()
_doc->CreatedBy.setValue(ui->lineEditCreator->text().toUtf8());
_doc->LastModifiedBy.setValue(ui->lineEditCreator->text().toUtf8());
_doc->Company.setValue(ui->lineEditCompany->text().toUtf8());
+ getMainWindow()->setUserSchema(ui->comboBox_unitSystem->currentIndex());
QByteArray licenseName {ui->comboLicense->currentData().toByteArray()};
// Is this really necessary?
if (licenseName.isEmpty()) {
diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp
index 61136af948..b354a3827a 100644
--- a/src/Gui/Document.cpp
+++ b/src/Gui/Document.cpp
@@ -106,9 +106,6 @@ struct DocumentP
std::map _ViewProviderMapAnnotation;
std::list _redoViewProviders;
- int projectUnitSystem = -1;
- bool projectUnitSystemIgnore = false;
-
using Connection = boost::signals2::connection;
Connection connectNewObject;
Connection connectDelObject;
@@ -651,30 +648,6 @@ 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
//*****************************************************************************************************
@@ -1485,14 +1458,6 @@ void Document::RestoreDocFile(Base::Reader &reader)
Base::Console().Error("%s\n", e.what());
}
}
-
- if (localreader->readNextElement()) {
- if (strcmp(localreader->localName(), "ProjectUnitSystem") == 0) {
- d->projectUnitSystem = localreader->getAttributeAsInteger("US");
- d->projectUnitSystemIgnore = localreader->getAttributeAsInteger("ignore");
- localreader->readEndElement("Document");
- }
- }
}
reader.initLocalReader(localreader);
@@ -1615,14 +1580,6 @@ void Document::SaveDocFile (Base::Writer &writer) const
<< encodeAttribute(getCameraSettings()) << "\"/>\n";
writer.decInd(); // indentation for camera settings
- if (d->projectUnitSystem >= 0) {
- writer.incInd();
- writer.Stream() << writer.ind() << "projectUnitSystem << "\" ignore=\""
- << d->projectUnitSystemIgnore << "\"/>\n";
- writer.decInd();
- }
-
writer.Stream() << "" << std::endl;
}
@@ -2558,5 +2515,6 @@ void Document::slotChangePropertyEditor(const App::Document &doc, const App::Pro
if(getDocument() == &doc) {
FC_LOG(Prop.getFullName() << " editor changed");
setModified(true);
+ getMainWindow()->setUserSchema(doc.UnitSystem.getValue());
}
}
diff --git a/src/Gui/Document.h b/src/Gui/Document.h
index 8c2a6539ef..73df51510d 100644
--- a/src/Gui/Document.h
+++ b/src/Gui/Document.h
@@ -298,12 +298,6 @@ 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
Gui::DocumentPy *_pcDocPy;
diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp
index d72a3dd3a3..049f823232 100644
--- a/src/Gui/MainWindow.cpp
+++ b/src/Gui/MainWindow.cpp
@@ -100,7 +100,6 @@
#include "WorkbenchManager.h"
#include "Workbench.h"
-
#include "MergeDocuments.h"
#include "ViewProviderExtern.h"
@@ -181,11 +180,14 @@ public:
}
QObject::connect(actionGrp, &QActionGroup::triggered, this, [this](QAction* action) {
int userSchema = action->data().toInt();
- // Set and save the Unit System
- Base::UnitsApi::setSchema(static_cast(userSchema));
- getWindowParameter()->SetInt("UserSchema", userSchema);
- // Update the application to show the unit change
- Gui::Application::Instance->onUpdate();
+ setUserSchema(userSchema);
+ // Force PropertyEditor refresh until we find a better way. Q_EMIT something?
+ const auto views = getMainWindow()->findChildren();
+ for(auto view : views) {
+ bool show = view->showAll();
+ view->setShowAll(!show);
+ view->setShowAll(show);
+ }
} );
setMenu(menu);
retranslateUi();
@@ -216,10 +218,32 @@ public:
}
}
+ void setUserSchema(int userSchema)
+ {
+ App::Document* doc = App::GetApplication().getActiveDocument();
+ if ( doc != nullptr ) {
+ if (doc->UnitSystem.getValue() != userSchema )
+ doc->UnitSystem.setValue(userSchema);
+ } else
+ getWindowParameter()->SetInt("UserSchema", userSchema);
+
+ unitChanged();
+ Base::UnitsApi::setSchema(static_cast(userSchema));
+ // Update the main window to show the unit change
+ Gui::Application::Instance->onUpdate();
+ }
+
private:
void unitChanged()
{
+ ParameterGrp::handle hGrpu = App::GetApplication().GetParameterGroupByPath
+ ("User parameter:BaseApp/Preferences/Units");
+ bool ignore = hGrpu->GetBool("IgnoreProjectSchema", false);
+ App::Document* doc = App::GetApplication().getActiveDocument();
int userSchema = getWindowParameter()->GetInt("UserSchema", 0);
+ if ( doc != nullptr && ! ignore) {
+ userSchema = doc->UnitSystem.getValue();
+ }
auto actions = menu()->actions();
if(Q_UNLIKELY(userSchema < 0 || userSchema >= actions.size())) {
userSchema = 0;
@@ -242,7 +266,7 @@ private:
// Pimpl class
struct MainWindowP
{
- QPushButton* sizeLabel;
+ DimensionWidget* sizeLabel;
QLabel* actionLabel;
QTimer* actionTimer;
QTimer* statusTimer;
@@ -2430,6 +2454,13 @@ void MainWindow::setPaneText(int i, QString text)
}
}
+
+void MainWindow::setUserSchema(int userSchema)
+{
+ d->sizeLabel->setUserSchema(userSchema);
+}
+
+
void MainWindow::customEvent(QEvent* e)
{
if (e->type() == QEvent::User) {
diff --git a/src/Gui/MainWindow.h b/src/Gui/MainWindow.h
index dce8325d6d..dc5aee647d 100644
--- a/src/Gui/MainWindow.h
+++ b/src/Gui/MainWindow.h
@@ -220,6 +220,10 @@ public Q_SLOTS:
* Sets text to the pane in the status bar.
*/
void setPaneText(int i, QString text);
+ /**
+ * Sets the userschema in the status bar
+ */
+ void setUserSchema(int userSchema);
/**
* Arranges all child windows in a tile pattern.
*/
diff --git a/src/Gui/PreferencePages/DlgSettingsGeneral.cpp b/src/Gui/PreferencePages/DlgSettingsGeneral.cpp
index c3f39a073b..5b0d3955c7 100644
--- a/src/Gui/PreferencePages/DlgSettingsGeneral.cpp
+++ b/src/Gui/PreferencePages/DlgSettingsGeneral.cpp
@@ -35,6 +35,7 @@
# include
#endif
+#include
#include
#include
@@ -98,7 +99,6 @@ DlgSettingsGeneral::DlgSettingsGeneral( QWidget* parent )
for (int i = 0; i < num; i++) {
QString item = 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
@@ -191,6 +191,7 @@ void DlgSettingsGeneral::saveSettings()
("User parameter:BaseApp/Preferences/Units");
hGrpu->SetInt("UserSchema", ui->comboBox_UnitSystem->currentIndex());
hGrpu->SetInt("Decimals", ui->spinBoxDecimals->value());
+ hGrpu->SetBool("IgnoreProjectSchema", ui->checkBox_projectUnitSystemIgnore->isChecked());
// Set actual value
Base::UnitsApi::setDecimals(ui->spinBoxDecimals->value());
@@ -208,22 +209,15 @@ void DlgSettingsGeneral::saveSettings()
Base::QuantityFormat::setDefaultDenominator(FracInch);
// Set and save the Unit System
- viewSystemIndex = ui->comboBox_UnitSystem->currentIndex();
- 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));
+ if ( ui->checkBox_projectUnitSystemIgnore->isChecked() ) {
+ viewSystemIndex = ui->comboBox_UnitSystem->currentIndex();
+ UnitsApi::setSchema(static_cast(viewSystemIndex));
+ } else {
+ App::Document* doc = App::GetApplication().getActiveDocument();
+ if ( doc != nullptr ) {
+ UnitsApi::setSchema(static_cast(doc->UnitSystem.getValue()));
+ }
}
- //
ui->SubstituteDecimal->onSave();
ui->UseLocaleFormatting->onSave();
@@ -268,6 +262,7 @@ void DlgSettingsGeneral::loadSettings()
("User parameter:BaseApp/Preferences/Units");
ui->comboBox_UnitSystem->setCurrentIndex(hGrpu->GetInt("UserSchema", 0));
ui->spinBoxDecimals->setValue(hGrpu->GetInt("Decimals", Base::UnitsApi::getDecimals()));
+ ui->checkBox_projectUnitSystemIgnore->setChecked(hGrpu->GetBool("IgnoreProjectSchema", false));
// Get the current user setting for the minimum fractional inch
FracInch = hGrpu->GetInt("FracInch", Base::QuantityFormat::getDefaultDenominator());
@@ -276,26 +271,6 @@ void DlgSettingsGeneral::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();
ui->UseLocaleFormatting->onRestore();
ui->RecentFiles->onRestore();
@@ -433,11 +408,9 @@ void DlgSettingsGeneral::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);
@@ -646,19 +619,6 @@ void DlgSettingsGeneral::onUnitSystemIndexChanged(int index)
}
}
-void DlgSettingsGeneral::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);
- }
-}
-
void DlgSettingsGeneral::onThemeChanged(int index) {
Q_UNUSED(index);
themeChanged = true;
diff --git a/src/Gui/PreferencePages/DlgSettingsGeneral.h b/src/Gui/PreferencePages/DlgSettingsGeneral.h
index ad55fc2fd1..05e5366a7a 100644
--- a/src/Gui/PreferencePages/DlgSettingsGeneral.h
+++ b/src/Gui/PreferencePages/DlgSettingsGeneral.h
@@ -72,7 +72,6 @@ protected Q_SLOTS:
public Q_SLOTS:
void onUnitSystemIndexChanged(int index);
- void on_checkBox_projectUnitSystemIgnore_stateChanged(int state);
private:
void saveDockWindowVisibility();
diff --git a/src/Gui/PreferencePages/DlgSettingsGeneral.ui b/src/Gui/PreferencePages/DlgSettingsGeneral.ui
index 2bf14ce7c2..7498c212dd 100644
--- a/src/Gui/PreferencePages/DlgSettingsGeneral.ui
+++ b/src/Gui/PreferencePages/DlgSettingsGeneral.ui
@@ -52,7 +52,7 @@
-
- Unit system:
+ Default Unit system:
@@ -87,27 +87,13 @@
- -
-
-
- Document unit system:
-
-
-
- -
-
-
- Unit system stored in the current document
-
-
-
- -
+
-
If enabled, document unit systems are ignored
- Ignore
+ Ignore project unit system and use default