From 2e49d1437a50571416fe510c24ff6cd0e29400d5 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 5 Nov 2015 13:07:51 +0200 Subject: [PATCH] For avoiding losing precision use double everywhere. --HG-- branch : develop --- src/app/valentina/core/vcmdexport.cpp | 14 ++--- src/app/valentina/core/vcmdexport.h | 6 +- .../dialogs/dialoglayoutsettings.cpp | 62 +++++++++++-------- .../valentina/dialogs/dialoglayoutsettings.h | 20 +++--- src/libs/vlayout/vlayoutgenerator.h | 2 +- src/libs/vmisc/vsettings.cpp | 24 +++---- src/libs/vmisc/vsettings.h | 12 ++-- 7 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/app/valentina/core/vcmdexport.cpp b/src/app/valentina/core/vcmdexport.cpp index b93685aee..4cb292887 100644 --- a/src/app/valentina/core/vcmdexport.cpp +++ b/src/app/valentina/core/vcmdexport.cpp @@ -82,17 +82,15 @@ VCommandLine::VCommandLine() : parser(), optionsUsed(InitOptionsUsed()), isGuiEn } //--------------------------------------------------------------------------------------------------------------------- -int VCommandLine::Lo2Px(const QString &src, const DialogLayoutSettings &converter) +qreal VCommandLine::Lo2Px(const QString &src, const DialogLayoutSettings &converter) { - //that is dirty-dirty hack ...eventually number is converted float <--> int 3 or 4 times including inside dialog ... - // that will loose precision for sure - return converter.LayoutToPixels(src.toFloat()); + return converter.LayoutToPixels(src.toDouble()); } //--------------------------------------------------------------------------------------------------------------------- -int VCommandLine::Pg2Px(const QString& src, const DialogLayoutSettings& converter) +qreal VCommandLine::Pg2Px(const QString& src, const DialogLayoutSettings& converter) { - return converter.PageToPixels(src.toFloat()); + return converter.PageToPixels(src.toDouble()); } //--------------------------------------------------------------------------------------------------------------------- @@ -320,12 +318,12 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const if (parser.isSet(*optionsUsed.value(LONG_OPTION_SHIFTLENGTH))) { - diag.SetShift(static_cast(Lo2Px(parser.value(*optionsUsed.value(LONG_OPTION_SHIFTLENGTH)), diag))); + diag.SetShift(Lo2Px(parser.value(*optionsUsed.value(LONG_OPTION_SHIFTLENGTH)), diag)); } if (parser.isSet(*optionsUsed.value(LONG_OPTION_GAPWIDTH))) { - diag.SetLayoutWidth(static_cast(Lo2Px(parser.value(*optionsUsed.value(LONG_OPTION_GAPWIDTH)), diag))); + diag.SetLayoutWidth(Lo2Px(parser.value(*optionsUsed.value(LONG_OPTION_GAPWIDTH)), diag)); } diag.SetAutoCrop(parser.isSet(*optionsUsed.value(LONG_OPTION_CROP))); diff --git a/src/app/valentina/core/vcmdexport.h b/src/app/valentina/core/vcmdexport.h index 1254fa38a..fe2cd8863 100644 --- a/src/app/valentina/core/vcmdexport.h +++ b/src/app/valentina/core/vcmdexport.h @@ -85,9 +85,9 @@ private: bool isGuiEnabled; friend class VApplication; - static int Lo2Px(const QString& src, const DialogLayoutSettings& converter); - static int Pg2Px(const QString& src, const DialogLayoutSettings& converter); - + static qreal Lo2Px(const QString& src, const DialogLayoutSettings& converter); + static qreal Pg2Px(const QString& src, const DialogLayoutSettings& converter); + static QMap InitOptionsUsed(); }; diff --git a/src/app/valentina/dialogs/dialoglayoutsettings.cpp b/src/app/valentina/dialogs/dialoglayoutsettings.cpp index 574faf658..98f00d337 100644 --- a/src/app/valentina/dialogs/dialoglayoutsettings.cpp +++ b/src/app/valentina/dialogs/dialoglayoutsettings.cpp @@ -112,49 +112,49 @@ DialogLayoutSettings::~DialogLayoutSettings() } //--------------------------------------------------------------------------------------------------------------------- -int DialogLayoutSettings::GetPaperHeight() const +qreal DialogLayoutSettings::GetPaperHeight() const { - return qFloor(UnitConvertor(ui->doubleSpinBoxPaperHeight->value(), oldPaperUnit, Unit::Px)); + return UnitConvertor(ui->doubleSpinBoxPaperHeight->value(), oldPaperUnit, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- -void DialogLayoutSettings::SetPaperHeight(int value) +void DialogLayoutSettings::SetPaperHeight(qreal value) { ui->doubleSpinBoxPaperHeight->setValue(UnitConvertor(value, Unit::Px, PaperUnit())); } //--------------------------------------------------------------------------------------------------------------------- -int DialogLayoutSettings::GetPaperWidth() const +qreal DialogLayoutSettings::GetPaperWidth() const { - return qFloor(UnitConvertor(ui->doubleSpinBoxPaperWidth->value(), oldPaperUnit, Unit::Px)); + return UnitConvertor(ui->doubleSpinBoxPaperWidth->value(), oldPaperUnit, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- -void DialogLayoutSettings::SetPaperWidth(int value) +void DialogLayoutSettings::SetPaperWidth(qreal value) { ui->doubleSpinBoxPaperWidth->setValue(UnitConvertor(value, Unit::Px, PaperUnit())); } //--------------------------------------------------------------------------------------------------------------------- -quint32 DialogLayoutSettings::GetShift() const +qreal DialogLayoutSettings::GetShift() const { - return static_cast(qFloor(UnitConvertor(ui->doubleSpinBoxShift->value(), oldLayoutUnit, Unit::Px))); + return UnitConvertor(ui->doubleSpinBoxShift->value(), oldLayoutUnit, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- -void DialogLayoutSettings::SetShift(quint32 value) +void DialogLayoutSettings::SetShift(qreal value) { ui->doubleSpinBoxShift->setValue(UnitConvertor(value, Unit::Px, LayoutUnit())); } //--------------------------------------------------------------------------------------------------------------------- -quint32 DialogLayoutSettings::GetLayoutWidth() const +qreal DialogLayoutSettings::GetLayoutWidth() const { - return static_cast(qFloor(UnitConvertor(ui->doubleSpinBoxLayoutWidth->value(), oldLayoutUnit, Unit::Px))); + return UnitConvertor(ui->doubleSpinBoxLayoutWidth->value(), oldLayoutUnit, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- -void DialogLayoutSettings::SetLayoutWidth(quint32 value) +void DialogLayoutSettings::SetLayoutWidth(qreal value) { ui->doubleSpinBoxLayoutWidth->setValue(UnitConvertor(value, Unit::Px, LayoutUnit())); } @@ -282,11 +282,15 @@ void DialogLayoutSettings::ConvertPaperSize() ui->doubleSpinBoxPaperWidth->setMaximum(FromPixel(QIMAGE_MAX, paperUnit)); ui->doubleSpinBoxPaperHeight->setMaximum(FromPixel(QIMAGE_MAX, paperUnit)); - ui->doubleSpinBoxPaperWidth->setValue(UnitConvertor(width, oldPaperUnit, paperUnit)); - ui->doubleSpinBoxPaperHeight->setValue(UnitConvertor(height, oldPaperUnit, paperUnit)); + const qreal newWidth = UnitConvertor(width, oldPaperUnit, paperUnit); + const qreal newHeight = UnitConvertor(height, oldPaperUnit, paperUnit); + oldPaperUnit = paperUnit; CorrectPaperDecimals(); MinimumPaperSize(); + + ui->doubleSpinBoxPaperWidth->setValue(newWidth); + ui->doubleSpinBoxPaperHeight->setValue(newHeight); } //--------------------------------------------------------------------------------------------------------------------- @@ -312,15 +316,15 @@ bool DialogLayoutSettings::SelectLayoutUnit(const QString &units) } //--------------------------------------------------------------------------------------------------------------------- -int DialogLayoutSettings::LayoutToPixels(qreal value) const +qreal DialogLayoutSettings::LayoutToPixels(qreal value) const { - return qFloor(UnitConvertor(value, LayoutUnit(), Unit::Px)); + return UnitConvertor(value, LayoutUnit(), Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- -int DialogLayoutSettings::PageToPixels(qreal value) const +qreal DialogLayoutSettings::PageToPixels(qreal value) const { - return qFloor(UnitConvertor(value, PaperUnit(), Unit::Px)); + return UnitConvertor(value, PaperUnit(), Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- @@ -340,11 +344,15 @@ void DialogLayoutSettings::ConvertLayoutSize() ui->doubleSpinBoxLayoutWidth->setMaximum(FromPixel(QIMAGE_MAX, unit)); ui->doubleSpinBoxShift->setMaximum(FromPixel(QIMAGE_MAX, unit)); - ui->doubleSpinBoxLayoutWidth->setValue(UnitConvertor(layoutWidth, oldLayoutUnit, unit)); - ui->doubleSpinBoxShift->setValue(UnitConvertor(shift, oldLayoutUnit, unit)); + const qreal newLayoutWidth = UnitConvertor(layoutWidth, oldLayoutUnit, unit); + const qreal newShift = UnitConvertor(shift, oldLayoutUnit, unit); + oldLayoutUnit = unit; CorrectLayoutDecimals(); MinimumLayoutSize(); + + ui->doubleSpinBoxLayoutWidth->setValue(newLayoutWidth); + ui->doubleSpinBoxShift->setValue(newShift); } //--------------------------------------------------------------------------------------------------------------------- @@ -404,9 +412,9 @@ void DialogLayoutSettings::DialogAccepted() SCASSERT(generator != nullptr) generator->SetLayoutWidth(GetLayoutWidth()); generator->SetCaseType(GetGroup()); - generator->SetPaperHeight(GetPaperHeight()); - generator->SetPaperWidth(GetPaperWidth()); - generator->SetShift(GetShift()); + generator->SetPaperHeight(qFloor(GetPaperHeight())); + generator->SetPaperWidth(qFloor(GetPaperWidth())); + generator->SetShift(qFloor(GetShift())); generator->SetRotate(GetRotate()); generator->SetRotationIncrease(GetIncrease()); generator->SetAutoCrop(GetAutoCrop()); @@ -636,8 +644,8 @@ void DialogLayoutSettings::CorrectPaperDecimals() ui->doubleSpinBoxPaperHeight->setDecimals(5); break; case Unit::Px: - ui->doubleSpinBoxPaperWidth->setDecimals(0); - ui->doubleSpinBoxPaperHeight->setDecimals(0); + ui->doubleSpinBoxPaperWidth->setDecimals(2); + ui->doubleSpinBoxPaperHeight->setDecimals(2); break; default: break; @@ -659,8 +667,8 @@ void DialogLayoutSettings::CorrectLayoutDecimals() ui->doubleSpinBoxShift->setDecimals(5); break; case Unit::Px: - ui->doubleSpinBoxLayoutWidth->setDecimals(0); - ui->doubleSpinBoxShift->setDecimals(0); + ui->doubleSpinBoxLayoutWidth->setDecimals(2); + ui->doubleSpinBoxShift->setDecimals(2); break; default: break; diff --git a/src/app/valentina/dialogs/dialoglayoutsettings.h b/src/app/valentina/dialogs/dialoglayoutsettings.h index 6130fe281..792a94ca7 100644 --- a/src/app/valentina/dialogs/dialoglayoutsettings.h +++ b/src/app/valentina/dialogs/dialoglayoutsettings.h @@ -62,17 +62,17 @@ public: explicit DialogLayoutSettings(VLayoutGenerator *generator, QWidget *parent = nullptr, bool disableSettings = false); ~DialogLayoutSettings(); - int GetPaperHeight() const; - void SetPaperHeight(int value); + qreal GetPaperHeight() const; + void SetPaperHeight(qreal value); - int GetPaperWidth() const; - void SetPaperWidth(int value); + qreal GetPaperWidth() const; + void SetPaperWidth(qreal value); - quint32 GetShift() const; - void SetShift(quint32 value); + qreal GetShift() const; + void SetShift(qreal value); - quint32 GetLayoutWidth() const; - void SetLayoutWidth(quint32 value); + qreal GetLayoutWidth() const; + void SetLayoutWidth(qreal value); Cases GetGroup() const; void SetGroup(const Cases &value); @@ -97,8 +97,8 @@ public: static QString MakeHelpTemplateList(); bool SelectPaperUnit(const QString& units); bool SelectLayoutUnit(const QString& units); - int LayoutToPixels(qreal value) const; - int PageToPixels(qreal value) const; + qreal LayoutToPixels(qreal value) const; + qreal PageToPixels(qreal value) const; static QString MakeGroupsHelp(); public slots: void ConvertPaperSize(); diff --git a/src/libs/vlayout/vlayoutgenerator.h b/src/libs/vlayout/vlayoutgenerator.h index 3332582e7..224f0d0db 100644 --- a/src/libs/vlayout/vlayoutgenerator.h +++ b/src/libs/vlayout/vlayoutgenerator.h @@ -59,7 +59,7 @@ public: void SetPaperWidth(int value); quint32 GetShift() const; - void SetShift(quint32 shift); + void SetShift(quint32 shift); void Generate(); diff --git a/src/libs/vmisc/vsettings.cpp b/src/libs/vmisc/vsettings.cpp index 3429974f8..b67fdf5e4 100644 --- a/src/libs/vmisc/vsettings.cpp +++ b/src/libs/vmisc/vsettings.cpp @@ -251,11 +251,11 @@ void VSettings::SetUserPassword(const QString &value) } //--------------------------------------------------------------------------------------------------------------------- -int VSettings::GetLayoutPaperHeight() const +qreal VSettings::GetLayoutPaperHeight() const { - const int def = qFloor(UnitConvertor(1189/*A0*/, Unit::Mm, Unit::Px)); + const qreal def = UnitConvertor(1189/*A0*/, Unit::Mm, Unit::Px); bool ok = false; - const int height = value(SettingLayoutPaperHeight, def).toInt(&ok); + const qreal height = value(SettingLayoutPaperHeight, def).toDouble(&ok); if (ok) { return height; @@ -267,17 +267,17 @@ int VSettings::GetLayoutPaperHeight() const } //--------------------------------------------------------------------------------------------------------------------- -void VSettings::SetLayoutPaperHeight(int value) +void VSettings::SetLayoutPaperHeight(qreal value) { setValue(SettingLayoutPaperHeight, value); } //--------------------------------------------------------------------------------------------------------------------- -int VSettings::GetLayoutPaperWidth() const +qreal VSettings::GetLayoutPaperWidth() const { - const int def = qFloor(UnitConvertor(841/*A0*/, Unit::Mm, Unit::Px)); + const qreal def = UnitConvertor(841/*A0*/, Unit::Mm, Unit::Px); bool ok = false; - const int width = value(SettingLayoutPaperWidth, def).toInt(&ok); + const qreal width = value(SettingLayoutPaperWidth, def).toDouble(&ok); if (ok) { return width; @@ -289,7 +289,7 @@ int VSettings::GetLayoutPaperWidth() const } //--------------------------------------------------------------------------------------------------------------------- -void VSettings::SetLayoutPaperWidth(int value) +void VSettings::SetLayoutPaperWidth(qreal value) { setValue(SettingLayoutPaperWidth, value); } @@ -311,9 +311,9 @@ quint32 VSettings::GetLayoutShift() const } //--------------------------------------------------------------------------------------------------------------------- -quint32 VSettings::GetDefLayoutShift() +qreal VSettings::GetDefLayoutShift() { - return static_cast(UnitConvertor(50, Unit::Mm, Unit::Px)); + return UnitConvertor(50, Unit::Mm, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- @@ -339,9 +339,9 @@ quint32 VSettings::GetLayoutWidth() const } //--------------------------------------------------------------------------------------------------------------------- -quint32 VSettings::GetDefLayoutWidth() +qreal VSettings::GetDefLayoutWidth() { - return static_cast(UnitConvertor(2.5, Unit::Mm, Unit::Px)); + return UnitConvertor(2.5, Unit::Mm, Unit::Px); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vmisc/vsettings.h b/src/libs/vmisc/vsettings.h index e5a04d3bb..fb06bcf98 100644 --- a/src/libs/vmisc/vsettings.h +++ b/src/libs/vmisc/vsettings.h @@ -83,18 +83,18 @@ public: QString GetUserPassword() const; void SetUserPassword(const QString &value); - int GetLayoutPaperHeight() const; - void SetLayoutPaperHeight(int value); + qreal GetLayoutPaperHeight() const; + void SetLayoutPaperHeight(qreal value); - int GetLayoutPaperWidth() const; - void SetLayoutPaperWidth(int value); + qreal GetLayoutPaperWidth() const; + void SetLayoutPaperWidth(qreal value); quint32 GetLayoutShift() const; - static quint32 GetDefLayoutShift(); + static qreal GetDefLayoutShift(); void SetLayoutShift(quint32 value); quint32 GetLayoutWidth() const; - static quint32 GetDefLayoutWidth(); + static qreal GetDefLayoutWidth(); void SetLayoutWidth(quint32 value); Cases GetLayoutGroup() const;