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
This commit is contained in:
bgbsww
2023-12-04 14:04:53 -05:00
committed by GitHub
parent 0aa328622d
commit c2bab7a2fa
12 changed files with 125 additions and 150 deletions

View File

@@ -93,6 +93,7 @@ recompute path. Also, it enables more complicated dependencies beyond trees.
#include <Base/Uuid.h>
#include <Base/Sequencer.h>
#include <Base/Stream.h>
#include <Base/UnitsApi.h>
#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<int>(Base::UnitSystem::NumUnitSystemTypes);
std::vector<std::string> enumValsAsVector;
for (int i = 0; i < num; i++) {
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(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");

View File

@@ -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

View File

@@ -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<Base::UnitSystem>(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<Base::UnitSystem>(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();
}

View File

@@ -137,6 +137,23 @@
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="textLabelUnitSystem">
<property name="text">
<string>Unit System:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="comboBox_unitSystem">
<property name="toolTip">
<string>Unit system for this file</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="textLabelCreator">
<property name="text">
<string>Created &amp;by:</string>
@@ -149,7 +166,7 @@
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QLineEdit" name="lineEditCreator">
<property name="minimumSize">
<size>
@@ -159,7 +176,7 @@
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QLabel" name="textLabelCreateDate">
<property name="text">
<string>Creation &amp;date:</string>
@@ -172,7 +189,7 @@
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QLineEdit" name="lineEditDate">
<property name="minimumSize">
<size>
@@ -185,7 +202,7 @@
</property>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QLabel" name="textLabelLastMod">
<property name="text">
<string>&amp;Last modified by:</string>
@@ -198,7 +215,7 @@
</property>
</widget>
</item>
<item row="6" column="1">
<item row="7" column="1">
<widget class="QLineEdit" name="lineEditLastMod">
<property name="minimumSize">
<size>
@@ -208,7 +225,7 @@
</property>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QLabel" name="textLabelLastModDate">
<property name="text">
<string>Last &amp;modification date:</string>
@@ -221,7 +238,7 @@
</property>
</widget>
</item>
<item row="7" column="1">
<item row="8" column="1">
<widget class="QLineEdit" name="lineEditLastModDate">
<property name="minimumSize">
<size>
@@ -234,7 +251,7 @@
</property>
</widget>
</item>
<item row="8" column="0">
<item row="9" column="0">
<widget class="QLabel" name="textLabelCompany">
<property name="text">
<string>Com&amp;pany:</string>
@@ -247,7 +264,7 @@
</property>
</widget>
</item>
<item row="8" column="1">
<item row="9" column="1">
<widget class="QLineEdit" name="lineEditCompany">
<property name="minimumSize">
<size>
@@ -257,7 +274,7 @@
</property>
</widget>
</item>
<item row="9" column="0">
<item row="10" column="0">
<widget class="QLabel" name="textLabelLicense">
<property name="text">
<string>License information:</string>
@@ -267,10 +284,10 @@
</property>
</widget>
</item>
<item row="9" column="1">
<item row="10" column="1">
<widget class="QComboBox" name="comboLicense"/>
</item>
<item row="10" column="0">
<item row="11" column="0">
<widget class="QLabel" name="textLabelLicenseURL">
<property name="text">
<string>License URL</string>
@@ -280,7 +297,7 @@
</property>
</widget>
</item>
<item row="10" column="1">
<item row="11" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="lineEditLicenseURL"/>
@@ -294,7 +311,7 @@
</item>
</layout>
</item>
<item row="11" column="0">
<item row="12" column="0">
<widget class="QLabel" name="textLabelComment">
<property name="text">
<string>&amp;Comment:</string>
@@ -307,10 +324,10 @@
</property>
</widget>
</item>
<item row="11" column="1" rowspan="2">
<item row="12" column="1" rowspan="2">
<widget class="QTextEdit" name="textEditComment"/>
</item>
<item row="12" column="0">
<item row="13" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>

View File

@@ -30,10 +30,12 @@
#include <App/Document.h>
#include <App/License.h>
#include <Base/UnitsApi.h>
#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<int>(Base::UnitSystem::NumUnitSystemTypes);
for (int i = 0; i < num; i++) {
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(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()) {

View File

@@ -106,9 +106,6 @@ struct DocumentP
std::map<std::string,ViewProvider*> _ViewProviderMapAnnotation;
std::list<ViewProviderDocumentObject*> _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 US=\""
<< d->projectUnitSystem << "\" ignore=\""
<< d->projectUnitSystemIgnore << "\"/>\n";
writer.decInd();
}
writer.Stream() << "</Document>" << 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());
}
}

View File

@@ -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;

View File

@@ -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<Base::UnitSystem>(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<PropertyView*>();
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<Base::UnitSystem>(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) {

View File

@@ -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.
*/

View File

@@ -35,6 +35,7 @@
# include <boost/filesystem.hpp>
#endif
#include <App/Document.h>
#include <Base/Parameter.h>
#include <Base/UnitsApi.h>
@@ -98,7 +99,6 @@ DlgSettingsGeneral::DlgSettingsGeneral( QWidget* parent )
for (int i = 0; i < num; i++) {
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(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<UnitSystem>(projectUnitSystemIndex));
}else{
UnitsApi::setSchema(static_cast<UnitSystem>(viewSystemIndex));
}
}else{
UnitsApi::setSchema(static_cast<UnitSystem>(viewSystemIndex));
if ( ui->checkBox_projectUnitSystemIgnore->isChecked() ) {
viewSystemIndex = ui->comboBox_UnitSystem->currentIndex();
UnitsApi::setSchema(static_cast<UnitSystem>(viewSystemIndex));
} else {
App::Document* doc = App::GetApplication().getActiveDocument();
if ( doc != nullptr ) {
UnitsApi::setSchema(static_cast<UnitSystem>(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;

View File

@@ -72,7 +72,6 @@ protected Q_SLOTS:
public Q_SLOTS:
void onUnitSystemIndexChanged(int index);
void on_checkBox_projectUnitSystemIgnore_stateChanged(int state);
private:
void saveDockWindowVisibility();

View File

@@ -52,7 +52,7 @@
<item row="1" column="0">
<widget class="QLabel" name="unitSystemLabel">
<property name="text">
<string>Unit system:</string>
<string>Default Unit system:</string>
</property>
</widget>
</item>
@@ -87,27 +87,13 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Document unit system:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboBox_projectUnitSystem">
<property name="toolTip">
<string>Unit system stored in the current document</string>
</property>
</widget>
</item>
<item row="2" column="2">
<item row="2" column="0" colspan="3">
<widget class="QCheckBox" name="checkBox_projectUnitSystemIgnore">
<property name="toolTip">
<string>If enabled, document unit systems are ignored</string>
</property>
<property name="text">
<string>Ignore</string>
<string>Ignore project unit system and use default</string>
</property>
</widget>
</item>