From c33b178e3cd54e4365a3d42b3dfe4d2fb6761e3b Mon Sep 17 00:00:00 2001
From: Roman Telezhynskyi <kroluku@gmail.com>
Date: Wed, 30 Aug 2017 09:32:39 +0300
Subject: [PATCH] Template recognizes new placeholders.

--HG--
branch : feature
---
 .../valentina/dialogs/dialogpatternmaterials.cpp  |  4 ++--
 src/libs/vlayout/vtextmanager.cpp                 | 14 ++++++++++++++
 src/libs/vpatterndb/vtranslatevars.cpp            | 11 +++++++++--
 src/libs/vpatterndb/vtranslatevars.h              |  2 +-
 .../vtools/dialogs/support/dialogeditlabel.cpp    | 15 +++++++++++++++
 5 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/src/app/valentina/dialogs/dialogpatternmaterials.cpp b/src/app/valentina/dialogs/dialogpatternmaterials.cpp
index c757d9518..6970ba2cf 100644
--- a/src/app/valentina/dialogs/dialogpatternmaterials.cpp
+++ b/src/app/valentina/dialogs/dialogpatternmaterials.cpp
@@ -145,8 +145,8 @@ void DialogPatternMaterials::SetPatternMaterials(const QMap<int, QString> &list)
 
     for (int i = 0; i < userMaterialPlaceholdersQuantity; ++i)
     {
-        QTableWidgetItem *item = new QTableWidgetItem(per + qApp->TrVars()->PlaceholderToUser(pl_userMaterial) +
-                                                      QString::number(i + 1) + per);
+        const QString translated = qApp->TrVars()->PlaceholderToUser(pl_userMaterial + QString::number(i + 1));
+        QTableWidgetItem *item = new QTableWidgetItem(per + translated + per);
         item->setFlags(item->flags() ^ Qt::ItemIsEditable);
         item->setTextAlignment(Qt::AlignLeft);
 
diff --git a/src/libs/vlayout/vtextmanager.cpp b/src/libs/vlayout/vtextmanager.cpp
index f16c9a717..afb02cd9e 100644
--- a/src/libs/vlayout/vtextmanager.cpp
+++ b/src/libs/vlayout/vtextmanager.cpp
@@ -102,6 +102,20 @@ QMap<QString, QString> PreparePlaceholders(const VAbstractPattern *doc)
     placeholders.insert(pl_height, curHeight);
     placeholders.insert(pl_mExt, mExt);
 
+    const QMap<int, QString> materials = doc->GetPatternMaterials();
+    for (int i = 0; i < userMaterialPlaceholdersQuantity; ++i)
+    {
+        const QString number = QString::number(i + 1);
+
+        QString value;
+        if (materials.contains(i + 1))
+        {
+            value = materials.value(i + 1);
+        }
+
+        placeholders.insert(pl_userMaterial + number, value);
+    }
+
     // Piece tags
     placeholders.insert(pl_pLetter, "");
     placeholders.insert(pl_pAnnotation, "");
diff --git a/src/libs/vpatterndb/vtranslatevars.cpp b/src/libs/vpatterndb/vtranslatevars.cpp
index ba9bf0814..49df3568c 100644
--- a/src/libs/vpatterndb/vtranslatevars.cpp
+++ b/src/libs/vpatterndb/vtranslatevars.cpp
@@ -688,11 +688,18 @@ QString VTranslateVars::InternalVarToUser(const QString &var) const
 }
 
 //---------------------------------------------------------------------------------------------------------------------
-QString VTranslateVars::PlaceholderToUser(const QString &var) const
+QString VTranslateVars::PlaceholderToUser(QString var) const
 {
+    QString number;
+    if (var.startsWith(pl_userMaterial) && var.length() > pl_userMaterial.length())
+    {
+        number = var.right(var.length() - pl_userMaterial.length());
+        var = pl_userMaterial;
+    }
+
     if (placeholders.contains(var))
     {
-        return placeholders.value(var).translate();
+        return placeholders.value(var).translate() + number;
     }
 
     return var;
diff --git a/src/libs/vpatterndb/vtranslatevars.h b/src/libs/vpatterndb/vtranslatevars.h
index 1ac8a7c68..0596c0f58 100644
--- a/src/libs/vpatterndb/vtranslatevars.h
+++ b/src/libs/vpatterndb/vtranslatevars.h
@@ -46,7 +46,7 @@ public:
     bool VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const;
 
     QString InternalVarToUser(const QString &var) const;
-    QString PlaceholderToUser(const QString &var) const;
+    QString PlaceholderToUser(QString var) const;
 
     QString PlaceholderToUserText(QString text) const;
     QString PlaceholderFromUserText(QString text) const;
diff --git a/src/libs/vtools/dialogs/support/dialogeditlabel.cpp b/src/libs/vtools/dialogs/support/dialogeditlabel.cpp
index 332e444f7..ed7a34a7b 100644
--- a/src/libs/vtools/dialogs/support/dialogeditlabel.cpp
+++ b/src/libs/vtools/dialogs/support/dialogeditlabel.cpp
@@ -506,6 +506,21 @@ void DialogEditLabel::InitPlaceholders()
     m_placeholders.insert(pl_height, qMakePair(tr("Height"), curHeight));
     m_placeholders.insert(pl_mExt, qMakePair(tr("Measurments extension"), mExt));
 
+    const QString materialDescription = tr("User material");
+    const QMap<int, QString> materials = m_doc->GetPatternMaterials();
+    for (int i = 0; i < userMaterialPlaceholdersQuantity; ++i)
+    {
+        const QString number = QString::number(i + 1);
+
+        QString value;
+        if (materials.contains(i + 1))
+        {
+            value = materials.value(i + 1);
+        }
+
+        m_placeholders.insert(pl_userMaterial + number, qMakePair(materialDescription + number, value));
+    }
+
     // Piece tags
     m_placeholders.insert(pl_pLetter, qMakePair(tr("Piece letter"), QString("")));
     m_placeholders.insert(pl_pAnnotation, qMakePair(tr("Piece annotation"), QString("")));