From fd8d2f8a9d5f87df36c9077c7e1726fb492d2756 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 4 Feb 2022 11:01:52 +0200 Subject: [PATCH] Opacity option for a background image. --- .../core/vtooloptionspropertybrowser.cpp | 55 ++++- .../core/vtooloptionspropertybrowser.h | 4 + .../configpages/preferencespatternpage.cpp | 4 + .../configpages/preferencespatternpage.ui | 40 +++- src/app/valentina/mainwindow.cpp | 3 + src/libs/ifc/schema/pattern/v0.9.0.xsd | 1 + src/libs/ifc/xml/vabstractpattern.cpp | 8 + src/libs/ifc/xml/vabstractpattern.h | 1 + src/libs/ifc/xml/vbackgroundpatternimage.cpp | 13 ++ src/libs/ifc/xml/vbackgroundpatternimage.h | 4 + src/libs/vmisc/vvalentinasettings.cpp | 15 ++ src/libs/vmisc/vvalentinasettings.h | 3 + .../plugins/vnumberproperty.cpp | 198 +++++++++++------- .../plugins/vnumberproperty.h | 81 ++++--- .../backgroundimage/vbackgroundimageitem.cpp | 26 +++ .../backgroundimage/vbackgroundimageitem.h | 4 + .../backgroundimage/vbackgroundpixmapitem.cpp | 1 + .../backgroundimage/vbackgroundsvgitem.cpp | 1 + .../image/opaquebackgroundimage.cpp | 68 ++++++ .../image/opaquebackgroundimage.h | 51 +++++ src/libs/vtools/undocommands/undocommands.pri | 2 + 21 files changed, 470 insertions(+), 113 deletions(-) create mode 100644 src/libs/vtools/undocommands/image/opaquebackgroundimage.cpp create mode 100644 src/libs/vtools/undocommands/image/opaquebackgroundimage.h diff --git a/src/app/valentina/core/vtooloptionspropertybrowser.cpp b/src/app/valentina/core/vtooloptionspropertybrowser.cpp index 9faee2563..1b66edee2 100644 --- a/src/app/valentina/core/vtooloptionspropertybrowser.cpp +++ b/src/app/valentina/core/vtooloptionspropertybrowser.cpp @@ -37,6 +37,7 @@ #include "../vwidgets/vsimplepoint.h" #include "../vwidgets/vsimplecurve.h" #include "../vpropertyexplorer/vproperties.h" +#include "def.h" #include "vformulaproperty.h" #include "../vpatterndb/vformula.h" #include "../vgeometry/vcubicbezier.h" @@ -52,6 +53,7 @@ namespace { Q_GLOBAL_STATIC_WITH_ARGS(const QString, AttrHold, (QLatin1String("hold"))) Q_GLOBAL_STATIC_WITH_ARGS(const QString, AttrVisible, (QLatin1String("visible"))) +Q_GLOBAL_STATIC_WITH_ARGS(const QString, AttrOpacity, (QLatin1String("opacity"))) } //--------------------------------------------------------------------------------------------------------------------- @@ -753,6 +755,22 @@ void VToolOptionsPropertyBrowser::AddPropertyApproximationScale(const QString &p AddProperty(aScaleProperty, AttrAScale); } +//--------------------------------------------------------------------------------------------------------------------- +void VToolOptionsPropertyBrowser::AddPropertyOpacity(const QString &propertyName, int opacity) +{ + QMap settings + { + {QStringLiteral("Min"), 0}, + {QStringLiteral("Max"), 100}, + {QStringLiteral("Step"), 1}, + {QStringLiteral("Suffix"), QChar('%')} + }; + + auto *opacityProperty = new VPE::VIntegerProperty(propertyName, settings); + opacityProperty->setValue(opacity); + AddProperty(opacityProperty, *AttrOpacity); +} + //--------------------------------------------------------------------------------------------------------------------- template void VToolOptionsPropertyBrowser::SetName(VPE::VProperty *property) @@ -813,6 +831,26 @@ void VToolOptionsPropertyBrowser::SetVisible(VPE::VProperty *property) } } +//--------------------------------------------------------------------------------------------------------------------- +template +void VToolOptionsPropertyBrowser::SetOpacity(VPE::VProperty *property) +{ + if (auto *i = qgraphicsitem_cast(currentItem)) + { + const int opacity = property->data(VPE::VProperty::DPC_Data, Qt::DisplayRole).toInt(); + if (VFuzzyComparePossibleNulls(opacity, i->GetOpacity())) + { + return; + } + + i->SetOpacity(opacity/100.); + } + else + { + qWarning()<<"Can't cast item"; + } +} + //--------------------------------------------------------------------------------------------------------------------- template void VToolOptionsPropertyBrowser::SetPointName(VPE::VProperty *property) @@ -2612,7 +2650,7 @@ void VToolOptionsPropertyBrowser::ChangeDataBackgroundPixmapItem(VPE::VProperty { SCASSERT(property != nullptr) - const QString id = propertyToId[property]; + const QString id = propertyToId.value(property); switch (PropertiesList().indexOf(id)) { @@ -2625,6 +2663,9 @@ void VToolOptionsPropertyBrowser::ChangeDataBackgroundPixmapItem(VPE::VProperty case 66: // AttrVisible SetVisible(property); break; + case 67: // AttrOpacity + SetOpacity(property); + break; default: qWarning()<<"Unknown property type. id = "<(property); break; + case 67: // AttrOpacity + SetOpacity(property); + break; default: qWarning()<<"Unknown property type. id = "<IsHold(), *AttrHold); AddPropertyBool(tr("Visible:"), i->IsVisible(), *AttrVisible); + AddPropertyOpacity(tr("Opacity:"), static_cast(i->GetOpacity()*100)); } //--------------------------------------------------------------------------------------------------------------------- @@ -3246,6 +3291,7 @@ void VToolOptionsPropertyBrowser::ShowOptionsBackgroundSVGItem(QGraphicsItem *it AddPropertyObjectName(i, tr("Name:"), false); AddPropertyBool(tr("Hold:"), i->IsHold(), *AttrHold); AddPropertyBool(tr("Visible:"), i->IsVisible(), *AttrVisible); + AddPropertyOpacity(tr("Opacity:"), static_cast(i->GetOpacity()*100)); } //--------------------------------------------------------------------------------------------------------------------- @@ -4200,6 +4246,7 @@ void VToolOptionsPropertyBrowser::UpdateOptionsBackgroundPixmapItem() idToProperty.value(AttrName)->setValue(i->name()); idToProperty.value(*AttrHold)->setValue(i->IsHold()); idToProperty.value(*AttrVisible)->setValue(i->IsVisible()); + idToProperty.value(*AttrOpacity)->setValue(static_cast(i->GetOpacity()*100)); } //--------------------------------------------------------------------------------------------------------------------- @@ -4210,6 +4257,7 @@ void VToolOptionsPropertyBrowser::UpdateOptionsBackgroundSVGItem() idToProperty.value(AttrName)->setValue(i->name()); idToProperty.value(*AttrHold)->setValue(i->IsHold()); idToProperty.value(*AttrVisible)->setValue(i->IsVisible()); + idToProperty.value(*AttrOpacity)->setValue(static_cast(i->GetOpacity()*100)); } //--------------------------------------------------------------------------------------------------------------------- @@ -4282,7 +4330,8 @@ QStringList VToolOptionsPropertyBrowser::PropertiesList() const AttrAlias1, /* 63 */ AttrAlias2, /* 64 */ *AttrHold, /* 65 */ - *AttrVisible /* 66 */ + *AttrVisible, /* 66 */ + *AttrOpacity /* 67 */ }; return attr; } diff --git a/src/app/valentina/core/vtooloptionspropertybrowser.h b/src/app/valentina/core/vtooloptionspropertybrowser.h index 489800011..11641bbcb 100644 --- a/src/app/valentina/core/vtooloptionspropertybrowser.h +++ b/src/app/valentina/core/vtooloptionspropertybrowser.h @@ -75,6 +75,9 @@ private: template void SetVisible(VPE::VProperty *property); + template + void SetOpacity(VPE::VProperty *property); + template void SetPointName(VPE::VProperty *property); @@ -188,6 +191,7 @@ private: const QString &id); void AddPropertyApproximationScale(const QString &propertyName, qreal aScale); + void AddPropertyOpacity(const QString &propertyName, int opacity); void AddPropertyFormula(const QString &propertyName, const VFormula &formula, const QString &attrName); void AddPropertyParentPointName(const QString &pointName, const QString &propertyName, const QString &propertyAttribure); diff --git a/src/app/valentina/dialogs/configpages/preferencespatternpage.cpp b/src/app/valentina/dialogs/configpages/preferencespatternpage.cpp index 535c68be4..a0f4997a1 100644 --- a/src/app/valentina/dialogs/configpages/preferencespatternpage.cpp +++ b/src/app/valentina/dialogs/configpages/preferencespatternpage.cpp @@ -112,6 +112,8 @@ PreferencesPatternPage::PreferencesPatternPage(QWidget *parent) ui->checkBoxRemeberPatternMaterials->setChecked(settings->IsRememberPatternMaterials()); m_knownMaterials = settings->GetKnownMaterials(); + ui->spinBoxOpacity->setValue(settings->GetBackgroundImageDefOpacity()); + connect(ui->pushButtonKnownMaterials, &QPushButton::clicked, this, &PreferencesPatternPage::ManageKnownMaterials); } @@ -175,6 +177,8 @@ QStringList PreferencesPatternPage::Apply() settings->SetKnownMaterials(m_knownMaterials); settings->SetRememberPatternMaterials(ui->checkBoxRemeberPatternMaterials->isChecked()); + settings->SetBackgroundImageDefOpacity(ui->spinBoxOpacity->value()); + return preferences; } diff --git a/src/app/valentina/dialogs/configpages/preferencespatternpage.ui b/src/app/valentina/dialogs/configpages/preferencespatternpage.ui index cc02f7be9..ec971c310 100644 --- a/src/app/valentina/dialogs/configpages/preferencespatternpage.ui +++ b/src/app/valentina/dialogs/configpages/preferencespatternpage.ui @@ -6,8 +6,8 @@ 0 0 - 373 - 760 + 553 + 887 @@ -24,8 +24,8 @@ 0 0 - 353 - 740 + 533 + 867 @@ -381,6 +381,38 @@ This option will take an affect after restart. + + + + Background image + + + + + + Opacity: + + + + + + + Opacity value by default + + + % + + + 100 + + + 100 + + + + + + diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index f560136e3..461d9aa0e 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -1392,6 +1392,9 @@ void MainWindow::PlaceBackgroundImage(const QPointF &pos, const QString &fileNam VBackgroundPatternImage image = VBackgroundPatternImage::FromFile(fileName, dialog.BuiltIn()); image.SetName(dialog.Name()); + VValentinaSettings *settings = VAbstractValApplication::VApp()->ValentinaSettings(); + image.SetOpacity(settings->GetBackgroundImageDefOpacity()/100.); + QTransform m; m.translate(pos.x(), pos.y()); diff --git a/src/libs/ifc/schema/pattern/v0.9.0.xsd b/src/libs/ifc/schema/pattern/v0.9.0.xsd index db88fb7a9..e6ac4b6e1 100644 --- a/src/libs/ifc/schema/pattern/v0.9.0.xsd +++ b/src/libs/ifc/schema/pattern/v0.9.0.xsd @@ -74,6 +74,7 @@ + diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 903449644..78c0c70fb 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -52,6 +52,7 @@ #include "../vpatterndb/vcontainer.h" #include "../vpatterndb/vpiecenode.h" #include "../vtools/tools/vdatatool.h" +#include "def.h" #include "vpatternconverter.h" #include "vdomdocument.h" #include "vtoolrecord.h" @@ -60,6 +61,7 @@ #include "../vlayout/vtextmanager.h" #include "vpatternimage.h" #include "vbackgroundpatternimage.h" +#include "vvalentinasettings.h" class QDomElement; @@ -2156,6 +2158,10 @@ auto VAbstractPattern::GetBackgroundPatternImage(const QDomElement &element) con image.SetZValue(GetParametrUInt(element, AttrZValue, QChar('0'))); image.SetVisible(GetParametrBool(element, AttrVisible, trueStr)); + VValentinaSettings *settings = VAbstractValApplication::VApp()->ValentinaSettings(); + image.SetOpacity(GetParametrDouble(element, AttrOpacity, + QString::number(settings->GetBackgroundImageDefOpacity()/100.))); + QString matrix = GetParametrEmptyString(element, AttrTransform); image.SetMatrix(StringToTransfrom(matrix)); @@ -2221,6 +2227,8 @@ void VAbstractPattern::WriteBackgroundImage(QDomElement &element, const VBackgro SetAttributeOrRemoveIf(element, AttrHold, image.Hold(), [](bool hold) noexcept {return not hold;}); SetAttributeOrRemoveIf(element, AttrZValue, image.ZValue(), [](qreal z) noexcept {return qFuzzyIsNull(z);}); SetAttributeOrRemoveIf(element, AttrVisible, image.Visible(), [](bool visible) noexcept {return visible;}); + SetAttributeOrRemoveIf(element, AttrOpacity, image.Opacity(), + [](qreal o) noexcept {return VFuzzyComparePossibleNulls(o, 1);}); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index d4a2a9c5d..59b9e37df 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -400,6 +400,7 @@ signals: void BackgroundImageNameChanged(const QUuid &id); void BackgroundImagesZValueChanged(); void BackgroundImagePositionChanged(const QUuid &id); + void BackgroundImageOpacityChanged(const QUuid &id); public slots: virtual void LiteParseTree(const Document &parse)=0; diff --git a/src/libs/ifc/xml/vbackgroundpatternimage.cpp b/src/libs/ifc/xml/vbackgroundpatternimage.cpp index b3624b8f7..5e1f331f4 100644 --- a/src/libs/ifc/xml/vbackgroundpatternimage.cpp +++ b/src/libs/ifc/xml/vbackgroundpatternimage.cpp @@ -27,6 +27,7 @@ *************************************************************************/ #include "vbackgroundpatternimage.h" +#include "qglobal.h" #include "utils.h" #include "../vmisc/compatibility.h" @@ -310,3 +311,15 @@ void VBackgroundPatternImage::SetVisible(bool newVisible) { m_visible = newVisible; } + +//--------------------------------------------------------------------------------------------------------------------- +auto VBackgroundPatternImage::Opacity() const -> qreal +{ + return m_opacity; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VBackgroundPatternImage::SetOpacity(qreal newOpacity) +{ + m_opacity = qBound(0.0, newOpacity, 1.0); +} diff --git a/src/libs/ifc/xml/vbackgroundpatternimage.h b/src/libs/ifc/xml/vbackgroundpatternimage.h index 7f7897965..b0ce4ae5c 100644 --- a/src/libs/ifc/xml/vbackgroundpatternimage.h +++ b/src/libs/ifc/xml/vbackgroundpatternimage.h @@ -93,6 +93,9 @@ public: auto Visible() const -> bool; void SetVisible(bool newVisible); + auto Opacity() const -> qreal; + void SetOpacity(qreal newOpacity); + private: QUuid m_id{QUuid::createUuid()}; QString m_contentType{}; @@ -104,6 +107,7 @@ private: QTransform m_matrix{}; bool m_hold{false}; bool m_visible{true}; + qreal m_opacity{1.0}; }; #endif // VBACKGROUNDPATTERNIMAGE_H diff --git a/src/libs/vmisc/vvalentinasettings.cpp b/src/libs/vmisc/vvalentinasettings.cpp index e2f1052ab..26c8e5262 100644 --- a/src/libs/vmisc/vvalentinasettings.cpp +++ b/src/libs/vmisc/vvalentinasettings.cpp @@ -44,6 +44,7 @@ #include "../vmisc/def.h" #include "../vmisc/vmath.h" #include "../vlayout/vbank.h" +#include "qglobal.h" Q_DECLARE_METATYPE(QMarginsF) @@ -60,6 +61,8 @@ Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsLayout, (QLatin1String("pat Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternKnownMaterials, (QLatin1String("pattern/knownMaterials"))) Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternRememberMaterials, (QLatin1String("pattern/rememberMaterials"))) +Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternBackgroundImageDefOpacity, + (QLatin1String("pattern/backgroundImageDefOpacity"))) Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLayoutWidth, (QLatin1String("layout/width"))) Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLayoutSorting, (QLatin1String("layout/sorting"))) @@ -956,6 +959,18 @@ void VValentinaSettings::SetFinalMeasurementsSearchOptionMatchCase(bool value) setValue(*settingSearchOptionsFinalMeasurementsMatchCase, value); } +//--------------------------------------------------------------------------------------------------------------------- +int VValentinaSettings::GetBackgroundImageDefOpacity() const +{ + return value(*settingPatternBackgroundImageDefOpacity, 100).toInt(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VValentinaSettings::SetBackgroundImageDefOpacity(int value) +{ + setValue(*settingPatternBackgroundImageDefOpacity, qBound(0, value, 100)); +} + //--------------------------------------------------------------------------------------------------------------------- template T VValentinaSettings::GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const diff --git a/src/libs/vmisc/vvalentinasettings.h b/src/libs/vmisc/vvalentinasettings.h index 032dc29e8..557e7c891 100644 --- a/src/libs/vmisc/vvalentinasettings.h +++ b/src/libs/vmisc/vvalentinasettings.h @@ -234,6 +234,9 @@ public: auto GetFinalMeasurementsSearchOptionMatchCase() const ->bool; void SetFinalMeasurementsSearchOptionMatchCase(bool value); + auto GetBackgroundImageDefOpacity() const -> int; + void SetBackgroundImageDefOpacity(int value); + private: Q_DISABLE_COPY(VValentinaSettings) diff --git a/src/libs/vpropertyexplorer/plugins/vnumberproperty.cpp b/src/libs/vpropertyexplorer/plugins/vnumberproperty.cpp index fdb07d0d9..8b41112db 100644 --- a/src/libs/vpropertyexplorer/plugins/vnumberproperty.cpp +++ b/src/libs/vpropertyexplorer/plugins/vnumberproperty.cpp @@ -29,12 +29,27 @@ #include #include "../vproperty_p.h" +#include "qstringliteral.h" + +namespace +{ +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrMin, (QLatin1String("Min"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrMax, (QLatin1String("Max"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrInteger, (QLatin1String("integer"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrStep, (QLatin1String("Step"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrSuffix, (QLatin1String("Suffix"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrPrecision, (QLatin1String("Precision"))) // NOLINT +Q_GLOBAL_STATIC_WITH_ARGS(const QString, StrDouble, (QLatin1String("double"))) // NOLINT +} const int VPE::VIntegerProperty::StandardMin = -1000000; const int VPE::VIntegerProperty::StandardMax = 1000000; VPE::VIntegerProperty::VIntegerProperty(const QString& name, const QMap& settings) - : VProperty(name, QVariant::Int), minValue(StandardMin), maxValue(StandardMax), singleStep(1.0) + : VProperty(name, QVariant::Int), + m_minValue(StandardMin), + m_maxValue(StandardMax), + m_singleStep(1.0) { VProperty::setSettings(settings); VProperty::d_ptr->VariantValue.setValue(0); @@ -42,26 +57,27 @@ VPE::VIntegerProperty::VIntegerProperty(const QString& name, const QMapVariantValue.setValue(0); VProperty::d_ptr->VariantValue.convert(QVariant::Int); } //! Returns an editor widget, or NULL if it doesn't supply one -QWidget* VPE::VIntegerProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, - const QAbstractItemDelegate* delegate) +auto VPE::VIntegerProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate) -> QWidget* { Q_UNUSED(options) Q_UNUSED(delegate) - QSpinBox* tmpEditor = new QSpinBox(parent); + auto* tmpEditor = new QSpinBox(parent); tmpEditor->setLocale(parent->locale()); - tmpEditor->setMinimum(static_cast(minValue)); - tmpEditor->setMaximum(static_cast(maxValue)); - tmpEditor->setSingleStep(static_cast(singleStep)); + tmpEditor->setMinimum(static_cast(m_minValue)); + tmpEditor->setMaximum(static_cast(m_maxValue)); + tmpEditor->setSingleStep(static_cast(m_singleStep)); tmpEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); tmpEditor->setValue(VProperty::d_ptr->VariantValue.toInt()); + tmpEditor->setSuffix(m_suffix); connect(tmpEditor, QOverload::of(&QSpinBox::valueChanged), this, &VIntegerProperty::valueChanged); VProperty::d_ptr->editor = tmpEditor; @@ -69,62 +85,73 @@ QWidget* VPE::VIntegerProperty::createEditor(QWidget * parent, const QStyleOptio } //! Gets the data from the widget -QVariant VPE::VIntegerProperty::getEditorData(const QWidget *editor) const +auto VPE::VIntegerProperty::getEditorData(const QWidget *editor) const -> QVariant { - const QSpinBox* tmpEditor = qobject_cast(editor); + const auto* tmpEditor = qobject_cast(editor); if (tmpEditor) { return tmpEditor->value(); } - return QVariant(0); + return {0}; } void VPE::VIntegerProperty::setSetting(const QString& key, const QVariant& value) { - if (key == QLatin1String("Min")) + if (key == *StrMax) { - maxValue = value.toInt(); + m_maxValue = value.toInt(); } - else if (key == QLatin1String("Max")) + else if (key == *StrMin) { - minValue = value.toInt(); + m_minValue = value.toInt(); } - else if (key == QLatin1String("Step")) + else if (key == *StrStep) { - singleStep = value.toInt(); + m_singleStep = value.toInt(); + } + else if (key == *StrSuffix) + { + m_suffix = value.toString(); } } -QVariant VPE::VIntegerProperty::getSetting(const QString& key) const +auto VPE::VIntegerProperty::getSetting(const QString& key) const -> QVariant { - if (key == QLatin1String("Min")) + if (key == *StrMin) { - return minValue; + return m_minValue; } - if (key == QLatin1String("Max")) + + if (key == *StrMax) { - return maxValue; + return m_maxValue; } - if (key == QLatin1String("Step")) + + if (key == *StrStep) { - return singleStep; + return m_singleStep; } - else - return VProperty::getSetting(key); + + if (key == *StrSuffix) + { + return m_suffix; + } + + return VProperty::getSetting(key); } -QStringList VPE::VIntegerProperty::getSettingKeys() const +auto VPE::VIntegerProperty::getSettingKeys() const -> QStringList { - return (QStringList("Min") << "Max" << "Step"); + return {*StrMin, *StrMax, *StrStep, *StrSuffix}; } -QString VPE::VIntegerProperty::type() const +auto VPE::VIntegerProperty::type() const -> QString { - return "integer"; + return *StrInteger; } -VPE::VProperty* VPE::VIntegerProperty::clone(bool include_children, VProperty* container) const +auto VPE::VIntegerProperty::clone(bool include_children, VProperty* container) const -> VPE::VProperty* { return VProperty::clone(include_children, container ? container : new VIntegerProperty(getName())); } @@ -132,23 +159,31 @@ VPE::VProperty* VPE::VIntegerProperty::clone(bool include_children, VProperty* c void VPE::VIntegerProperty::valueChanged(int i) { Q_UNUSED(i) - UserChangeEvent *event = new UserChangeEvent(); - QCoreApplication::postEvent ( VProperty::d_ptr->editor, event ); + QCoreApplication::postEvent ( VProperty::d_ptr->editor, new UserChangeEvent() ); } +const int VPE::VDoubleProperty::StandardMin = -1000000; +const int VPE::VDoubleProperty::StandardMax = 1000000; const double VPE::VDoubleProperty::StandardPrecision = 5; VPE::VDoubleProperty::VDoubleProperty(const QString& name, const QMap& settings) - : VIntegerProperty(name), Precision(static_cast(StandardPrecision)) + : VProperty(name, QVariant::Double), + m_minValue(StandardMin), + m_maxValue(StandardMax), + m_singleStep(1.0), + m_precision(static_cast(StandardPrecision)) { VProperty::setSettings(settings); VProperty::d_ptr->VariantValue.setValue(0); VProperty::d_ptr->VariantValue.convert(QVariant::Double); - VProperty::d_ptr->PropertyVariantType = QVariant::Double; } VPE::VDoubleProperty::VDoubleProperty(const QString &name) - : VIntegerProperty(name), Precision(static_cast(StandardPrecision)) + : VProperty(name), + m_minValue(StandardMin), + m_maxValue(StandardMax), + m_singleStep(1.0), + m_precision(static_cast(StandardPrecision)) { VProperty::d_ptr->VariantValue.setValue(0); VProperty::d_ptr->VariantValue.convert(QVariant::Double); @@ -156,90 +191,109 @@ VPE::VDoubleProperty::VDoubleProperty(const QString &name) } //! Returns an editor widget, or NULL if it doesn't supply one -QWidget* VPE::VDoubleProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, - const QAbstractItemDelegate* delegate) +auto VPE::VDoubleProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate) -> QWidget* { Q_UNUSED(options) Q_UNUSED(delegate) - QDoubleSpinBox* tmpEditor = new QDoubleSpinBox(parent); + auto* tmpEditor = new QDoubleSpinBox(parent); tmpEditor->setLocale(parent->locale()); - tmpEditor->setMinimum(minValue); - tmpEditor->setMaximum(maxValue); - tmpEditor->setDecimals(Precision); - tmpEditor->setSingleStep(singleStep); + tmpEditor->setMinimum(m_minValue); + tmpEditor->setMaximum(m_maxValue); + tmpEditor->setDecimals(m_precision); + tmpEditor->setSingleStep(m_singleStep); + tmpEditor->setSuffix(m_suffix); tmpEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); tmpEditor->setValue(VProperty::d_ptr->VariantValue.toDouble()); - connect(tmpEditor, QOverload::of(&QDoubleSpinBox::valueChanged), this, &VIntegerProperty::valueChanged); + connect(tmpEditor, QOverload::of(&QDoubleSpinBox::valueChanged), this, &VDoubleProperty::valueChanged); VProperty::d_ptr->editor = tmpEditor; return VProperty::d_ptr->editor; } //! Gets the data from the widget -QVariant VPE::VDoubleProperty::getEditorData(const QWidget *editor) const +auto VPE::VDoubleProperty::getEditorData(const QWidget *editor) const -> QVariant { - const QDoubleSpinBox* tmpEditor = qobject_cast(editor); + const auto* tmpEditor = qobject_cast(editor); if (tmpEditor) { return tmpEditor->value(); } - return QVariant(0); + return {0}; } void VPE::VDoubleProperty::setSetting(const QString& key, const QVariant& value) { - if (key == QLatin1String("Min")) + if (key == *StrMin) { - minValue = value.toDouble(); + m_minValue = value.toDouble(); } - else if (key == QLatin1String("Max")) + else if (key == *StrMax) { - maxValue = value.toDouble(); + m_maxValue = value.toDouble(); } - else if (key == QLatin1String("Step")) + else if (key == *StrStep) { - singleStep = value.toDouble(); + m_singleStep = value.toDouble(); } - else if (key == QLatin1String("Precision")) + else if (key == *StrSuffix) { - Precision = value.toInt(); + m_suffix = value.toString(); + } + else if (key == *StrPrecision) + { + m_precision = value.toInt(); } } -QVariant VPE::VDoubleProperty::getSetting(const QString& key) const +auto VPE::VDoubleProperty::getSetting(const QString& key) const -> QVariant { - if (key == QLatin1String("Min")) + if (key == *StrMin) { - return minValue; + return m_minValue; } - if (key == QLatin1String("Max")) + + if (key == *StrMax) { - return maxValue; + return m_maxValue; } - if (key == QLatin1String("Step")) + + if (key == *StrStep) { - return singleStep; + return m_singleStep; } - if (key == QLatin1String("Precision")) + + if (key == *StrSuffix) { - return Precision; + return m_suffix; } - else - return VProperty::getSetting(key); + + if (key == *StrPrecision) + { + return m_precision; + } + + return VProperty::getSetting(key); } -QStringList VPE::VDoubleProperty::getSettingKeys() const +auto VPE::VDoubleProperty::getSettingKeys() const -> QStringList { - return (QStringList("Min") << "Max" << "Step" << "Precision"); + return {*StrMin, *StrMax, *StrStep, *StrSuffix, *StrPrecision}; } -QString VPE::VDoubleProperty::type() const +auto VPE::VDoubleProperty::type() const -> QString { - return "double"; + return *StrDouble; } -VPE::VProperty* VPE::VDoubleProperty::clone(bool include_children, VProperty* container) const +auto VPE::VDoubleProperty::clone(bool include_children, VProperty* container) const -> VPE::VProperty* { - return VIntegerProperty::clone(include_children, container ? container : new VDoubleProperty(getName())); + return VProperty::clone(include_children, container ? container : new VDoubleProperty(getName())); +} + +void VPE::VDoubleProperty::valueChanged(int i) +{ + Q_UNUSED(i) + QCoreApplication::postEvent ( VProperty::d_ptr->editor, new UserChangeEvent() ); } diff --git a/src/libs/vpropertyexplorer/plugins/vnumberproperty.h b/src/libs/vpropertyexplorer/plugins/vnumberproperty.h index 07ab4b4a1..3c0fdc5e2 100644 --- a/src/libs/vpropertyexplorer/plugins/vnumberproperty.h +++ b/src/libs/vpropertyexplorer/plugins/vnumberproperty.h @@ -21,14 +21,11 @@ #ifndef VNUMBERPROPERTY_H #define VNUMBERPROPERTY_H -#include -#include #include #include #include #include #include -#include #include #include @@ -44,7 +41,7 @@ QT_WARNING_DISABLE_GCC("-Wsuggest-final-types") //! Class for holding an integer property class VPROPERTYEXPLORERSHARED_EXPORT VIntegerProperty : public VProperty { - Q_OBJECT + Q_OBJECT // NOLINT public: VIntegerProperty(const QString& name, const QMap& settings); @@ -55,51 +52,55 @@ public: //! \options Render options //! \delegate A pointer to the QAbstractItemDelegate requesting the editor. This can be used to connect signals and //! slots. - virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& options, - const QAbstractItemDelegate* delegate) override; + auto createEditor(QWidget* parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate) -> QWidget* override; //! Gets the data from the widget - virtual QVariant getEditorData(const QWidget* editor) const override; + auto getEditorData(const QWidget* editor) const -> QVariant override; //! Sets the settings. Available settings: //! //! key: "Min" - value: Minimum number as integer //! key: "Max" - value: Maximum number as integer - virtual void setSetting(const QString& key, const QVariant& value) override; + //! key: "Step" - value: Increment step + //! key: "Suffix" - value: Editor's suffix + void setSetting(const QString& key, const QVariant& value) override; //! Get the settings. This function has to be implemented in a subclass in order to have an effect - virtual QVariant getSetting(const QString& key) const override; + auto getSetting(const QString& key) const -> QVariant override; //! Returns the list of keys of the property's settings - virtual QStringList getSettingKeys() const override; + auto getSettingKeys() const -> QStringList override; //! Returns a string containing the type of the property - virtual QString type() const override; + auto type() const -> QString override; //! Clones this property //! \param include_children Indicates whether to also clone the children //! \param container If a property is being passed here, no new VProperty is being created but instead it is tried //! to fill all the data into container. This can also be used when subclassing this function. //! \return Returns the newly created property (or container, if it was not NULL) - Q_REQUIRED_RESULT virtual VProperty* clone(bool include_children = true, - VProperty* container = nullptr) const override; + Q_REQUIRED_RESULT auto clone(bool include_children = true, + VProperty* container = nullptr) const -> VProperty* override; public slots: void valueChanged(int i); -protected: - double minValue, maxValue, singleStep; +private: + Q_DISABLE_COPY(VIntegerProperty) // NOLINT + + double m_minValue; + double m_maxValue; + double m_singleStep; + QString m_suffix{}; static const int StandardMin;// = -1000000; static const int StandardMax;// = 1000000; - -private: - Q_DISABLE_COPY(VIntegerProperty) }; //! Class for holding a double property -class VPROPERTYEXPLORERSHARED_EXPORT VDoubleProperty : public VIntegerProperty +class VPROPERTYEXPLORERSHARED_EXPORT VDoubleProperty : public VProperty { - Q_OBJECT + Q_OBJECT // NOLINT public: VDoubleProperty(const QString& name, const QMap& settings); @@ -110,46 +111,58 @@ public: //! \options Render options //! \delegate A pointer to the QAbstractItemDelegate requesting the editor. This can be used to connect signals and //! slots. - virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& options, - const QAbstractItemDelegate* delegate) override; + auto createEditor(QWidget* parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate) -> QWidget* override; //! Gets the data from the widget - virtual QVariant getEditorData(const QWidget* editor) const override; + auto getEditorData(const QWidget* editor) const -> QVariant override; //! Sets the settings. Available settings: //! //! key: "Min" - value: Minimum number as integer //! key: "Max" - value: Maximum number as integer - virtual void setSetting(const QString& key, const QVariant& value) override; + //! key: "Step" - value: Increment step + //! key: "Suffix" - value: Editor's suffix + //! key: "Precision" - value: Number of decimals after the decimal point + void setSetting(const QString& key, const QVariant& value) override; //! Get the settings. This function has to be implemented in a subclass in order to have an effect - virtual QVariant getSetting(const QString& key) const override; + auto getSetting(const QString& key) const -> QVariant override; //! Returns the list of keys of the property's settings - virtual QStringList getSettingKeys() const override; + auto getSettingKeys() const -> QStringList override; //! Returns a string containing the type of the property - virtual QString type() const override; + auto type() const -> QString override; //! Clones this property //! \param include_children Indicates whether to also clone the children //! \param container If a property is being passed here, no new VProperty is being created but instead it is tried //! to fill all the data into container. This can also be used when subclassing this function. //! \return Returns the newly created property (or container, if it was not NULL) - virtual VProperty* clone(bool include_children = true, VProperty* container = NULL) const override; + auto clone(bool include_children = true, VProperty* container = nullptr) const -> VProperty* override; -protected: - //! Number of decimals after the decimal point - int Precision; - - const static double StandardPrecision;// = 5; +public slots: + void valueChanged(int i); private: Q_DISABLE_COPY(VDoubleProperty) + + double m_minValue; + double m_maxValue; + double m_singleStep; + QString m_suffix{}; + + //! Number of decimals after the decimal point + int m_precision; + + static const int StandardMin;// = -1000000; + static const int StandardMax;// = 1000000; + const static double StandardPrecision;// = 5; }; QT_WARNING_POP -} +} // namespace VPE #endif // VNUMBERPROPERTY_H diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.cpp b/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.cpp index 1838eafc2..85e90cd1e 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.cpp +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.cpp @@ -38,6 +38,7 @@ #include "../../undocommands/image/renamebackgroundimage.h" #include "../../undocommands/image/hidebackgroundimage.h" #include "../../undocommands/image/resetbackgroundimage.h" +#include "../../undocommands/image/opaquebackgroundimage.h" #include "../toolsdef.h" #include @@ -69,6 +70,7 @@ VBackgroundImageItem::VBackgroundImageItem(const VBackgroundPatternImage &image, &VBackgroundImageItem::UpdateVisibilityState); connect(doc, &VAbstractPattern::BackgroundImagesZValueChanged, this, &VBackgroundImageItem::ZValueChanged); connect(doc, &VAbstractPattern::BackgroundImagePositionChanged, this, &VBackgroundImageItem::PositionChanged); + connect(doc, &VAbstractPattern::BackgroundImageOpacityChanged, this, &VBackgroundImageItem::OpacityChanged); InitImage(); } @@ -130,6 +132,18 @@ void VBackgroundImageItem::SetVisible(bool visible) VAbstractApplication::VApp()->getUndoStack()->push(new HideBackgroundImage(m_image.Id(), not visible, m_doc)); } +//--------------------------------------------------------------------------------------------------------------------- +auto VBackgroundImageItem::GetOpacity() const -> qreal +{ + return m_image.Opacity(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VBackgroundImageItem::SetOpacity(qreal opacity) +{ + VAbstractApplication::VApp()->getUndoStack()->push(new OpaqueBackgroundImage(m_image.Id(), opacity, m_doc)); +} + //--------------------------------------------------------------------------------------------------------------------- void VBackgroundImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { @@ -196,6 +210,18 @@ void VBackgroundImageItem::HoldChanged(QUuid id) UpdateHoldState(); } +//--------------------------------------------------------------------------------------------------------------------- +void VBackgroundImageItem::OpacityChanged(const QUuid &id) +{ + if (m_image.Id() != id) + { + return; + } + + m_image = m_doc->GetBackgroundImage(m_image.Id()); + update(); +} + //--------------------------------------------------------------------------------------------------------------------- void VBackgroundImageItem::VisibilityChanged(QUuid id) { diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.h b/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.h index a779ab9cb..964bb4d01 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.h +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundimageitem.h @@ -63,6 +63,9 @@ public: auto IsVisible() const -> bool; void SetVisible(bool visible); + auto GetOpacity() const -> qreal; + void SetOpacity(qreal opacity); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; signals: @@ -77,6 +80,7 @@ public slots: void PositionChanged(QUuid id); void ImageTransformationChanged(QUuid id); void HoldChanged(QUuid id); + void OpacityChanged(const QUuid &id); void VisibilityChanged(QUuid id); void NameChanged(QUuid id); void EnableSelection(bool enable); diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp b/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp index f3c3bcafb..fcf8c21a5 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp @@ -93,6 +93,7 @@ void VBackgroundPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsI painter->save(); painter->setTransform(Image().Matrix(), true); + painter->setOpacity(Image().Opacity()); painter->drawPixmap(QPointF(), Pixmap()); diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp b/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp index 3c4e0836d..b6c5b15db 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp @@ -67,6 +67,7 @@ void VBackgroundSVGItem::paint(QPainter *painter, const QStyleOptionGraphicsItem painter->save(); painter->setTransform(Image().Matrix(), true); + painter->setOpacity(Image().Opacity()); renderer->render(painter, QRectF(QPointF(0, 0), renderer->defaultSize())); diff --git a/src/libs/vtools/undocommands/image/opaquebackgroundimage.cpp b/src/libs/vtools/undocommands/image/opaquebackgroundimage.cpp new file mode 100644 index 000000000..ead305b1c --- /dev/null +++ b/src/libs/vtools/undocommands/image/opaquebackgroundimage.cpp @@ -0,0 +1,68 @@ +/************************************************************************ + ** + ** @file opaquebackgroundimage.cpp + ** @author Roman Telezhynskyi + ** @date 3 2, 2022 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2022 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#include "opaquebackgroundimage.h" +#include "../ifc/xml/vbackgroundpatternimage.h" + +//--------------------------------------------------------------------------------------------------------------------- +OpaqueBackgroundImage::OpaqueBackgroundImage(QUuid id, qreal opacity, VAbstractPattern *doc, QUndoCommand *parent) + : VUndoCommand(QDomElement(), doc, parent), + m_id(id), + m_opacity(opacity) +{ + setText(tr("change a background image opacity")); + + VBackgroundPatternImage image = doc->GetBackgroundImage(m_id); + + m_oldOpacity = image.Opacity(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void OpaqueBackgroundImage::undo() +{ + VBackgroundPatternImage image = doc->GetBackgroundImage(m_id); + + if (not image.IsNull()) + { + image.SetOpacity(m_oldOpacity); + doc->SaveBackgroundImage(image); + emit doc->BackgroundImageOpacityChanged(m_id); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void OpaqueBackgroundImage::redo() +{ + VBackgroundPatternImage image = doc->GetBackgroundImage(m_id); + + if (not image.IsNull()) + { + image.SetOpacity(m_opacity); + doc->SaveBackgroundImage(image); + emit doc->BackgroundImageOpacityChanged(m_id); + } +} diff --git a/src/libs/vtools/undocommands/image/opaquebackgroundimage.h b/src/libs/vtools/undocommands/image/opaquebackgroundimage.h new file mode 100644 index 000000000..27cd8c5bd --- /dev/null +++ b/src/libs/vtools/undocommands/image/opaquebackgroundimage.h @@ -0,0 +1,51 @@ +/************************************************************************ + ** + ** @file opaquebackgroundimage.h + ** @author Roman Telezhynskyi + ** @date 3 2, 2022 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2022 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#ifndef OPAQUEBACKGROUNDIMAGE_H +#define OPAQUEBACKGROUNDIMAGE_H + +#include "../vundocommand.h" + +#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0) +#include "../vmisc/defglobal.h" +#endif // QT_VERSION < QT_VERSION_CHECK(5, 13, 0) + +class OpaqueBackgroundImage : public VUndoCommand +{ +public: + OpaqueBackgroundImage(QUuid id, qreal opacity, VAbstractPattern *doc, QUndoCommand *parent = nullptr); + ~OpaqueBackgroundImage() override =default; + void undo() override; + void redo() override; +private: + Q_DISABLE_COPY_MOVE(OpaqueBackgroundImage) + QUuid m_id; + qreal m_opacity; + qreal m_oldOpacity{1.0}; +}; + +#endif // OPAQUEBACKGROUNDIMAGE_H diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index 98385852a..a5e39d1c9 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -11,6 +11,7 @@ HEADERS += \ $$PWD/image/holdallbackgroundimages.h \ $$PWD/image/holdbackgroundimage.h \ $$PWD/image/movebackgroundimage.h \ + $$PWD/image/opaquebackgroundimage.h \ $$PWD/image/renamebackgroundimage.h \ $$PWD/image/resetbackgroundimage.h \ $$PWD/image/rotatebackgroundimage.h \ @@ -50,6 +51,7 @@ SOURCES += \ $$PWD/image/holdallbackgroundimages.cpp \ $$PWD/image/holdbackgroundimage.cpp \ $$PWD/image/movebackgroundimage.cpp \ + $$PWD/image/opaquebackgroundimage.cpp \ $$PWD/image/renamebackgroundimage.cpp \ $$PWD/image/resetbackgroundimage.cpp \ $$PWD/image/rotatebackgroundimage.cpp \