Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TD] automatically update dimension format #3260

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/Mod/TechDraw/Gui/DlgPrefsTechDraw3Imp.cpp
Expand Up @@ -29,6 +29,7 @@

#include <Base/Parameter.h>
#include <Base/Console.h>
#include <Base/UnitsApi.h>

#include "DrawGuiUtil.h"
#include "DlgPrefsTechDraw3Imp.h"
Expand All @@ -48,6 +49,11 @@ DlgPrefsTechDraw3Imp::DlgPrefsTechDraw3Imp( QWidget* parent )
plsb_ArrowSize->setMinimum(0);
pdsbBalloonKink->setUnit(Base::Unit::Length);
pdsbBalloonKink->setMinimum(0);

connect(cbGlobalDecimals, SIGNAL(toggled(bool)),
this, SLOT(onGlobalDecimalsChanged(bool)));
connect(sbAltDecimals, SIGNAL(valueChanged(int)),
this, SLOT(onAltDecimalsChanged(int)));
}

DlgPrefsTechDraw3Imp::~DlgPrefsTechDraw3Imp()
Expand Down Expand Up @@ -124,6 +130,58 @@ void DlgPrefsTechDraw3Imp::loadSettings()
pcbBalloonArrow->setCurrentIndex(prefBalloonArrow());
DrawGuiUtil::loadArrowBox(pcbArrow);
pcbArrow->setCurrentIndex(prefArrowStyle());

// check that if the sytem-wide decimals are used, the format uses the same decimals
// the format can be in the form "%.xf", where x is the number of decimals
// or in the form "%.g", that will cut decimals for integers, otherwise take the system-wide decimals
// get at first the setting (default is "%.xf", where x is the number of system-wide decimals)
Base::Reference<ParameterGrp> hGrpFormatSpec = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Dimensions");
std::string formatSpec = hGrpFormatSpec->GetASCII("formatSpec", "");
// get now if system-wiede is used or alternate
Base::Reference<ParameterGrp> hGrpUseGlobalDecimals = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Dimensions");
bool UseGlobalDecimals = hGrpUseGlobalDecimals->GetBool("UseGlobalDecimals", true);
// get if system-wide is used or alternate
Base::Reference<ParameterGrp> hGrpUseAltDecimals = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Dimensions");
int AltDecimals = hGrpUseAltDecimals->GetInt("AltDecimals", 2);

// if UseGlobalDecimals but the number after the dot is different, replace it
size_t dotPosition = formatSpec.find('.');
if (dotPosition == std::string::npos)
return;
std::string localDecimalsString;
localDecimalsString = formatSpec.at(dotPosition);
// check if localDecimalsString is actually a number and not e.g. 'g'
bool isDigit = false;
if (isdigit(formatSpec.at(dotPosition + 1)))
isDigit = true;
std::string systemDecimalsString = std::to_string(Base::UnitsApi::getDecimals());
std::string altDecimalsString = std::to_string(AltDecimals);
if (isDigit && UseGlobalDecimals && (localDecimalsString != systemDecimalsString))
{
formatSpec.replace(dotPosition + 1, 1, systemDecimalsString);
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}

// if use alternate decimals but the number after the dot is different, replace it
else if (isDigit && !UseGlobalDecimals && (localDecimalsString != altDecimalsString))
{
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
// if use alternate decimals but there is no number after the dot, add one
else if (!isDigit && !UseGlobalDecimals)
{
// we must change "%.g" to "%.xf"
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
formatSpec.append("f");
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
}

/**
Expand Down Expand Up @@ -159,6 +217,73 @@ int DlgPrefsTechDraw3Imp::prefArrowStyle(void) const
return style;
}

void DlgPrefsTechDraw3Imp::onGlobalDecimalsChanged(bool useGlobal)
{
std::string formatSpec = leformatSpec->text().toStdString();
// if UseGlobalDecimals but the number after the dot is different, replace it
size_t dotPosition = formatSpec.find('.');
if (dotPosition == std::string::npos)
return;
std::string localDecimalsString;
localDecimalsString = formatSpec.at(dotPosition);
// check if localDecimalsString is actually a number and not e.g. 'g'
bool isDigit = false;
if (isdigit(formatSpec.at(dotPosition + 1)))
isDigit = true;
std::string systemDecimalsString = std::to_string(Base::UnitsApi::getDecimals());
std::string altDecimalsString = std::to_string(sbAltDecimals->value());
// if UseGlobalDecimals but the number after the dot is different, replace it
if (isDigit && useGlobal && (localDecimalsString != systemDecimalsString))
{
formatSpec.replace(dotPosition + 1, 1, systemDecimalsString);
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
// if use alternate decimals but the number after the dot is different, replace it
else if (isDigit && !useGlobal && (localDecimalsString != altDecimalsString))
{
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
// if use alternate decimals but there is no number after the dot, add one
else if (!isDigit && !useGlobal)
{
// we must change "%.g" to "%.xf"
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
formatSpec.append("f");
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}

}

void DlgPrefsTechDraw3Imp::onAltDecimalsChanged(int AltDecimals)
{
std::string formatSpec = leformatSpec->text().toStdString();
// if UseGlobalDecimals but the number after the dot is different, replace it
size_t dotPosition = formatSpec.find('.');
if (dotPosition == std::string::npos)
return;
std::string localDecimalsString;
localDecimalsString = formatSpec.at(dotPosition);
// check if localDecimalsString is actually a number and not e.g. 'g'
bool isDigit = false;
if (isdigit(formatSpec.at(dotPosition + 1)))
isDigit = true;
std::string systemDecimalsString = std::to_string(Base::UnitsApi::getDecimals());
std::string altDecimalsString = std::to_string(AltDecimals);
// if use alternate decimals but the number after the dot is different, replace it
if (isDigit && !cbGlobalDecimals->isChecked() && (localDecimalsString != altDecimalsString))
{
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
// if use alternate decimals but there is no number after the dot, add one
else if (!isDigit && !cbGlobalDecimals->isChecked())
{
// we must change "%.g" to "%.xf"
formatSpec.replace(dotPosition + 1, 1, altDecimalsString);
formatSpec.append("f");
leformatSpec->setText(QString::fromLatin1(formatSpec.c_str()));
}
}

#include <Mod/TechDraw/Gui/moc_DlgPrefsTechDraw3Imp.cpp>
5 changes: 4 additions & 1 deletion src/Mod/TechDraw/Gui/DlgPrefsTechDraw3Imp.h
Expand Up @@ -39,6 +39,10 @@ class DlgPrefsTechDraw3Imp : public Gui::Dialog::PreferencePage, public Ui_DlgPr
DlgPrefsTechDraw3Imp( QWidget* parent = 0 );
~DlgPrefsTechDraw3Imp();

protected Q_SLOTS:
void onGlobalDecimalsChanged(bool);
void onAltDecimalsChanged(int);

protected:
void saveSettings();
void loadSettings();
Expand All @@ -47,7 +51,6 @@ class DlgPrefsTechDraw3Imp : public Gui::Dialog::PreferencePage, public Ui_DlgPr
int prefBalloonArrow(void) const;
int prefArrowStyle(void) const;


};

} // namespace TechDrawGui
Expand Down