diff --git a/src/Mod/TechDraw/App/DrawTemplate.cpp b/src/Mod/TechDraw/App/DrawTemplate.cpp index 527b5a1c44..29de742c97 100644 --- a/src/Mod/TechDraw/App/DrawTemplate.cpp +++ b/src/Mod/TechDraw/App/DrawTemplate.cpp @@ -100,6 +100,32 @@ DrawPage* DrawTemplate::getParentPage() const return page; } +// Return the counts related to pages, namely collated page index and total page count +std::pair DrawTemplate::getPageNumbers() const +{ + std::vector pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); + std::vector pageNames; + for (auto page : pages) { + if (page->isAttachedToDocument() && + !page->testStatus(App::ObjectStatus::Remove)) { + pageNames.push_back(QString::fromUtf8(page->Label.getValue())); + } + } + QCollator collator; + std::sort(pageNames.begin(), pageNames.end(), collator); + + int pos = 0; + DrawPage *page = getParentPage(); + if (page) { + auto it = std::find(pageNames.begin(), pageNames.end(), QString::fromUtf8(page->Label.getValue())); + if (it != pageNames.end()) { + pos = it - pageNames.begin() + 1; + } + } + + return std::pair(pos, (int) pageNames.size()); +} + QString DrawTemplate::getAutofillValue(const QString &id) const { // author @@ -133,32 +159,23 @@ QString DrawTemplate::getAutofillValue(const QString &id) const } // sheet else if (id.compare(QString::fromUtf8(Autofill::Sheet)) == 0) { - std::vector pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); - std::vector pageNames; - for (auto page : pages) { - if (page->isAttachedToDocument() && - !page->testStatus(App::ObjectStatus::Remove)) { - pageNames.push_back(QString::fromUtf8(page->Label.getValue())); - } - } - QCollator collator; - std::sort(pageNames.begin(), pageNames.end(), collator); - - int pos = 0; - DrawPage *page = getParentPage(); - if (page) { - auto it = std::find(pageNames.begin(), pageNames.end(), QString::fromUtf8(page->Label.getValue())); - if (it != pageNames.end()) { - pos = it - pageNames.begin() + 1; - } - } - - return QString::asprintf("%d / %d", pos, (int) pageNames.size()); + std::pair pageNumbers = getPageNumbers(); + return QString::asprintf("%d / %d", pageNumbers.first, pageNumbers.second); } // title else if (id.compare(QString::fromUtf8(Autofill::Title)) == 0) { return QString::fromUtf8(getDocument()->Label.getValue()); } + // page number + else if (id.compare(QString::fromUtf8(Autofill::PageNumber)) == 0) { + std::pair pageNumbers = getPageNumbers(); + return QString::number(pageNumbers.first); + } + // page total + else if (id.compare(QString::fromUtf8(Autofill::PageCount)) == 0) { + std::pair pageNumbers = getPageNumbers(); + return QString::number(pageNumbers.second); + } return QString(); } diff --git a/src/Mod/TechDraw/App/DrawTemplate.h b/src/Mod/TechDraw/App/DrawTemplate.h index f2c6be5390..66d8904e27 100644 --- a/src/Mod/TechDraw/App/DrawTemplate.h +++ b/src/Mod/TechDraw/App/DrawTemplate.h @@ -56,6 +56,7 @@ public: virtual double getHeight() const; virtual DrawPage* getParentPage() const; + virtual std::pair getPageNumbers() const; virtual QString getAutofillValue(const QString &id) const; @@ -76,6 +77,8 @@ public: static constexpr const char *Scale = "scale"; static constexpr const char *Sheet = "sheet"; static constexpr const char *Title = "title"; + static constexpr const char *PageNumber = "page_number"; + static constexpr const char *PageCount = "page_count"; }; private: