From 664bbe9afec9f5b9c1af9850866655cb1a3f2408 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 16 Jan 2023 16:40:50 +0200 Subject: [PATCH] New option. Default piece label template for new pieces. --- ChangeLog.txt | 1 + .../dialogs/dialogpatternproperties.cpp | 34 ++++++++++ .../dialogs/dialogpatternproperties.h | 2 + .../dialogs/dialogpatternproperties.ui | 52 +++++++++++++++- src/libs/ifc/schema/pattern/v0.9.2.xsd | 3 +- src/libs/ifc/xml/vabstractpattern.cpp | 36 ++++++++--- src/libs/ifc/xml/vabstractpattern.h | 5 +- .../vtools/tools/nodeDetails/vnodepoint.cpp | 10 ++- .../vtools/tools/nodeDetails/vnodepoint.h | 1 + src/libs/vtools/tools/vtoolseamallowance.cpp | 62 +++++++++++++++++++ src/libs/vtools/tools/vtoolseamallowance.h | 1 + 11 files changed, 194 insertions(+), 13 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d1da7844c..53ae48424 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -12,6 +12,7 @@ - New option Interactive tools. - Improve error messages while import CSV data. - [smart-pattern/valentina#189] Fix regression in dialog Known measurements. +- New option. Default piece label template for new pieces. # Valentina 0.7.52 September 12, 2022 - Fix crash when default locale is ru. diff --git a/src/app/valentina/dialogs/dialogpatternproperties.cpp b/src/app/valentina/dialogs/dialogpatternproperties.cpp index 3ff887012..1f786c81a 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.cpp +++ b/src/app/valentina/dialogs/dialogpatternproperties.cpp @@ -166,6 +166,11 @@ DialogPatternProperties::DialogPatternProperties(VPattern *doc, VContainer *pat //Initialization change value. Set to default value after initialization m_defaultChanged = false; m_securityChanged = false; + + connect(ui->pushButtonBrowsePieceLabelPath, &QPushButton::clicked, this, + &DialogPatternProperties::BrowseLabelPath); + ui->lineEditPieceLabelPath->setText(m_doc->GetDefaultPieceLabelPath()); + connect(ui->lineEditPieceLabelPath, &QLineEdit::textChanged, this, &DialogPatternProperties::LabelPathChanged); } //--------------------------------------------------------------------------------------------------------------------- @@ -235,6 +240,7 @@ void DialogPatternProperties::SaveDescription() m_doc->SetDescription(ui->plainTextEditDescription->document()->toPlainText()); m_doc->SetLabelPrefix(qvariant_cast(ui->comboBoxLabelLanguage->currentData())); m_doc->SetPassmarkLengthVariable(ui->lineEditPassmarkLength->text()); + m_doc->SetDefaultPieceLabelPath(ui->lineEditPieceLabelPath->text()); if (m_oldPassmarkLength != ui->lineEditPassmarkLength->text()) { @@ -421,3 +427,31 @@ void DialogPatternProperties::ShowImage() qCritical() << tr("Unable to open temp file"); } } + +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternProperties::BrowseLabelPath() +{ + QString path = ui->lineEditPieceLabelPath->text(); + if (path.isEmpty()) + { + path = VCommonSettings::PrepareLabelTemplates( + VAbstractApplication::VApp()->Settings()->GetPathLabelTemplate()); + } + + QString filters(tr("Label template") + QLatin1String("(*.xml)")); + + const QString filePath = QFileDialog::getOpenFileName(this, tr("Label template"), path, filters, nullptr, + VAbstractApplication::VApp()->NativeFileDialog()); + + ui->lineEditPieceLabelPath->setText(filePath); +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternProperties::LabelPathChanged(const QString &text) +{ + QPalette palette = ui->lineEditPieceLabelPath->palette(); + palette.setColor(ui->lineEditPieceLabelPath->foregroundRole(), + text.isEmpty() || QFileInfo::exists(text) ? Qt::black : Qt::red); + ui->lineEditPieceLabelPath->setPalette(palette); + m_descriptionChanged = true; +} diff --git a/src/app/valentina/dialogs/dialogpatternproperties.h b/src/app/valentina/dialogs/dialogpatternproperties.h index eda47a5db..0d4c5ce4e 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.h +++ b/src/app/valentina/dialogs/dialogpatternproperties.h @@ -63,6 +63,8 @@ private slots: void ChangeImage(); void SaveImage(); void ShowImage(); + void BrowseLabelPath(); + void LabelPathChanged(const QString &text); private: // cppcheck-suppress unknownMacro Q_DISABLE_COPY_MOVE(DialogPatternProperties) // NOLINT diff --git a/src/app/valentina/dialogs/dialogpatternproperties.ui b/src/app/valentina/dialogs/dialogpatternproperties.ui index e282095e7..a4b4d313e 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.ui +++ b/src/app/valentina/dialogs/dialogpatternproperties.ui @@ -6,8 +6,8 @@ 0 0 - 493 - 582 + 552 + 667 @@ -124,6 +124,54 @@ + + + + Default piece label template + + + + + + Path: + + + + + + + + 0 + 0 + + + + Path to default label template + + + true + + + + + + + + 0 + 0 + + + + Select path to label template + + + Browse… + + + + + + diff --git a/src/libs/ifc/schema/pattern/v0.9.2.xsd b/src/libs/ifc/schema/pattern/v0.9.2.xsd index 2c1b42b19..d2c07bd79 100644 --- a/src/libs/ifc/schema/pattern/v0.9.2.xsd +++ b/src/libs/ifc/schema/pattern/v0.9.2.xsd @@ -33,6 +33,7 @@ + @@ -81,7 +82,7 @@ - + diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 1e2710584..67e0bec52 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -106,6 +106,7 @@ const QString VAbstractPattern::TagNodes = QStringLiteral("nodes"); const QString VAbstractPattern::TagNode = QStringLiteral("node"); const QString VAbstractPattern::TagBackgroundImages = QStringLiteral("backgroudImages"); const QString VAbstractPattern::TagBackgroundImage = QStringLiteral("backgroudImage"); +const QString VAbstractPattern::TagPieceLabel = QStringLiteral("pieceLabel"); const QString VAbstractPattern::AttrName = QStringLiteral("name"); const QString VAbstractPattern::AttrVisible = QStringLiteral("visible"); @@ -1273,6 +1274,21 @@ void VAbstractPattern::SetFinalMeasurements(const QVector &me emit patternChanged(false); } +//--------------------------------------------------------------------------------------------------------------------- +QString VAbstractPattern::GetDefaultPieceLabelPath() const +{ + return UniqueTagText(TagPieceLabel); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPattern::SetDefaultPieceLabelPath(const QString &path) +{ + CheckTagExists(TagPieceLabel); + setTagText(TagPieceLabel, path); + modified = true; + emit patternChanged(false); +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractPattern::SetPatternWasChanged(bool changed) { @@ -1577,10 +1593,11 @@ auto VAbstractPattern::CheckTagExists(const QString &tag) -> QDomElement TagCustomerBirthDate, // 8 TagCustomerEmail, // 9 TagPatternLabel, // 10 - TagWatermark, // 11 - TagPatternMaterials, // 12 - TagFinalMeasurements, // 13 - TagBackgroundImages // 14 + TagPieceLabel, // 11 + TagWatermark, // 12 + TagPatternMaterials, // 13 + TagFinalMeasurements, // 14 + TagBackgroundImages // 15 }; switch (tags.indexOf(tag)) @@ -1615,16 +1632,19 @@ auto VAbstractPattern::CheckTagExists(const QString &tag) -> QDomElement case 10: // TagPatternLabel element = createElement(TagPatternLabel); break; - case 11: // TagWatermark + case 11: // TagPieceLabel + element = createElement(TagPieceLabel); + break; + case 12: // TagWatermark element = createElement(TagWatermark); break; - case 12: // TagPatternMaterials + case 13: // TagPatternMaterials element = createElement(TagPatternMaterials); break; - case 13: // TagFinalMeasurements + case 14: // TagFinalMeasurements element = createElement(TagFinalMeasurements); break; - case 14: // TagBackgroundImages + case 15: // TagBackgroundImages element = createElement(TagBackgroundImages); break; case 0: //TagUnit (Mandatory tag) diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index bc17b2418..e0ef6da10 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -44,7 +44,6 @@ #include "../vmisc/def.h" #include "vdomdocument.h" #include "vtoolrecord.h" -#include "../vlayout/vtextmanager.h" class QDomElement; class VPiecePath; @@ -197,6 +196,9 @@ public: QVector GetFinalMeasurements() const; void SetFinalMeasurements(const QVector &measurements); + QString GetDefaultPieceLabelPath() const; + void SetDefaultPieceLabelPath(const QString &path); + void SetPatternWasChanged(bool changed); bool GetPatternWasChanged() const; @@ -301,6 +303,7 @@ public: static const QString TagNode; static const QString TagBackgroundImages; static const QString TagBackgroundImage; + static const QString TagPieceLabel; static const QString AttrName; static const QString AttrVisible; diff --git a/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp b/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp index e83c77921..b559ce4f3 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp @@ -99,6 +99,7 @@ enum class ContextMenuOption : int InLayout, ForbidFlipping, ForceFlipping, + ResetLabelTemplate, Remove, TurnPoint, LAST_ONE_DO_NOT_USE @@ -351,6 +352,10 @@ QHash VNodePoint::InitContextMenu(QMenu *menu, vidtype pieceId, forceFlippingOption->setChecked(detail.IsForceFlipping()); contextMenu.insert(static_cast(ContextMenuOption::ForceFlipping), forceFlippingOption); + QAction *reseteLabelTemplateOption = menu->addAction(tr("Reset piece label template")); + reseteLabelTemplateOption->setEnabled(not doc->GetDefaultPieceLabelPath().isEmpty()); + contextMenu.insert(static_cast(ContextMenuOption::ResetLabelTemplate), reseteLabelTemplateOption); + QAction *actionRemove = menu->addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete")); referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true); contextMenu.insert(static_cast(ContextMenuOption::Remove), actionRemove); @@ -580,7 +585,7 @@ void VNodePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) ContextMenuOption selectedOption = static_cast( contextMenu.key(selectedAction, static_cast(ContextMenuOption::NoSelection))); - Q_STATIC_ASSERT_X(static_cast(ContextMenuOption::LAST_ONE_DO_NOT_USE) == 32, + Q_STATIC_ASSERT_X(static_cast(ContextMenuOption::LAST_ONE_DO_NOT_USE) == 33, "Not all options were handled."); QT_WARNING_PUSH @@ -603,6 +608,9 @@ QT_WARNING_DISABLE_GCC("-Wswitch-default") case ContextMenuOption::ForceFlipping: emit ToggleForceFlipping(selectedAction->isChecked()); break; + case ContextMenuOption::ResetLabelTemplate: + emit ResetPieceLabelTemplate(); + break; case ContextMenuOption::Remove: try { diff --git a/src/libs/vtools/tools/nodeDetails/vnodepoint.h b/src/libs/vtools/tools/nodeDetails/vnodepoint.h index 9fe5e91d0..c828f9e28 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodepoint.h +++ b/src/libs/vtools/tools/nodeDetails/vnodepoint.h @@ -70,6 +70,7 @@ signals: void TogglePassmark(quint32 id, bool toggle); void TogglePassmarkAngleType(quint32 id, PassmarkAngleType type); void TogglePassmarkLineType(quint32 id, PassmarkLineType type); + void ResetPieceLabelTemplate(); public slots: virtual void FullUpdateFromFile() override; void NameChangePosition(const QPointF &pos); diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index 1f6d7bb4c..94d1e99d6 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -41,6 +41,7 @@ #include "../vgeometry/vellipticalarc.h" #include "../ifc/xml/vpatternconverter.h" #include "../ifc/exception/vexceptionwrongid.h" +#include "../ifc/xml/vlabeltemplateconverter.h" #include "../undocommands/addpiece.h" #include "../undocommands/deletepiece.h" #include "../undocommands/movepiece.h" @@ -63,6 +64,7 @@ #include "tools/nodeDetails/vnodespline.h" #include "tools/nodeDetails/vnodesplinepath.h" #include "tools/nodeDetails/vtoolplacelabel.h" +#include "../vformat/vlabeltemplate.h" #include #include @@ -176,6 +178,29 @@ VToolSeamAllowance *VToolSeamAllowance::Create(const QPointer &dialo initData.parse = Document::FullParse; initData.typeCreation = Source::FromGui; + auto LoadLabelTemplate = [&initData](const QString &path) + { + if (not path.isEmpty()) + { + try + { + VLabelTemplate ltemplate; + ltemplate.setXMLContent(VLabelTemplateConverter(path).Convert()); + return ltemplate.ReadLines(); + } + catch (VException &e) + { + const QString errorMsg = QObject::tr("Piece '%1'. Unable to load default piece label template.\n%2\n%3") + .arg(initData.detail.GetName(), e.ErrorMessage(), e.DetailedInformation()); + VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) : + qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; + } + } + + return QVector(); + }; + + initData.detail.GetPieceLabelData().SetLabelTemplate(LoadLabelTemplate(doc->GetDefaultPieceLabelPath())); initData.detail.GetPath().SetNodes(PrepareNodes(initData.detail.GetPath(), scene, doc, data)); VToolSeamAllowance *piece = Create(initData); @@ -1225,6 +1250,9 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) forceFlippingOption->setCheckable(true); forceFlippingOption->setChecked(detail.IsForceFlipping()); + QAction *reseteLabelTemplateOption = menu.addAction(tr("Reset piece label template")); + reseteLabelTemplateOption->setEnabled(not doc->GetDefaultPieceLabelPath().isEmpty()); + QAction *actionRemove = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete")); actionRemove->setDisabled(_referens > 0); @@ -1245,6 +1273,10 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { ToggleForceFlipping(selectedAction->isChecked()); } + else if (selectedAction == reseteLabelTemplateOption) + { + ResetPieceLabelTemplate(); + } else if (selectedAction == actionRemove) { try @@ -1690,6 +1722,34 @@ void VToolSeamAllowance::TogglePassmarkLineType(quint32 id, PassmarkLineType typ } } +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::ResetPieceLabelTemplate() +{ + const VPiece oldDet = VAbstractTool::data.GetPiece(m_id); + VPiece newDet = oldDet; + + const QString path = doc->GetDefaultPieceLabelPath(); + if (not path.isEmpty()) + { + QVector lines; + try + { + VLabelTemplate ltemplate; + ltemplate.setXMLContent(VLabelTemplateConverter(path).Convert()); + lines = ltemplate.ReadLines(); + newDet.GetPieceLabelData().SetLabelTemplate(lines); + VAbstractApplication::VApp()->getUndoStack()->push(new SavePieceOptions(oldDet, newDet, doc, m_id)); + } + catch (VException &e) + { + const QString errorMsg = QObject::tr("Piece '%1'. Unable to load default piece label template.\n%2\n%3") + .arg(newDet.GetName(), e.ErrorMessage(), e.DetailedInformation()); + VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) : + qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; + } + } +} + //--------------------------------------------------------------------------------------------------------------------- VPieceItem::MoveTypes VToolSeamAllowance::FindLabelGeometry(const VPatternLabelData& labelData, const QVector &pins, qreal &rotationAngle, @@ -1928,6 +1988,8 @@ void VToolSeamAllowance::InitNode(const VPieceNode &node, VMainGraphicsScene *sc &VToolSeamAllowance::TogglePassmarkAngleType, Qt::UniqueConnection); connect(tool, &VNodePoint::TogglePassmarkLineType, parent, &VToolSeamAllowance::TogglePassmarkLineType, Qt::UniqueConnection); + connect(tool, &VNodePoint::ResetPieceLabelTemplate, parent, + &VToolSeamAllowance::ResetPieceLabelTemplate, Qt::UniqueConnection); tool->setParentItem(parent); tool->SetParentType(ParentType::Item); tool->SetExluded(node.IsExcluded()); diff --git a/src/libs/vtools/tools/vtoolseamallowance.h b/src/libs/vtools/tools/vtoolseamallowance.h index 3b076a246..18f565408 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.h +++ b/src/libs/vtools/tools/vtoolseamallowance.h @@ -173,6 +173,7 @@ private slots: void ToggleNodePointPassmark(quint32 id, bool toggle); void TogglePassmarkAngleType(quint32 id, PassmarkAngleType type); void TogglePassmarkLineType(quint32 id, PassmarkLineType type); + void ResetPieceLabelTemplate(); private: Q_DISABLE_COPY_MOVE(VToolSeamAllowance) // NOLINT