Index: scribus/scribusdoc.h =================================================================== --- scribus/scribusdoc.h (revision 27773) +++ scribus/scribusdoc.h (working copy) @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1097,6 +1098,29 @@ */ void updateSectionPageNumbersToPages(); /** + * @brief Call when entering master-page editing (once per session). + * Snapshots the current set of master-page names and clears the + * touched-master tracking, both used by + * pageNeedsMasterPageReapply() to let the exit sequence skip pages + * whose master was never actually edited during this session. + */ + void startMasterPageEditSession(); + /** + * @brief Call once the exit sequence has finished consulting + * pageNeedsMasterPageReapply() for every page, so a later, unrelated + * change doesn't get evaluated against stale session data. + */ + void endMasterPageEditSession(); + /** + * @brief Whether pageIndex's assigned master page needs to be + * reapplied at the end of a master-page editing session - true if + * that specific master was genuinely edited during the session, or + * if any master was added/removed/renamed (a structural change, + * safest to reapply everything rather than track it precisely). + * Always true if startMasterPageEditSession() was never called. + */ + bool pageNeedsMasterPageReapply(uint pageIndex) const; + /** * @param otherPageIndex * @param location * @param count @@ -1410,6 +1434,17 @@ QList TempPages; /** \brief Mapping Master Page Name to Master Page numbers */ QMap MasterNames; + // Master-page names present when the current master-page editing + // session began (see startMasterPageEditSession()). Empty when no + // session is active or none has ever started. + QSet m_masterNamesAtSessionStart; + // Master-page names actually edited (had a real change fire while + // masterPageMode() was true) during the current session. + QSet m_touchedMasterPagesThisSession; + // True once startMasterPageEditSession() has been called for the + // current session, so pageNeedsMasterPageReapply() can safely + // default to "yes, reapply" if no session was ever started. + bool m_masterPageEditSessionActive {false}; /** \brief List of Objects */ //! Items is a pointer to the lists of items that exist in a document. In Normal mode, it points to DocItems. In Master Page mode it points to MasterItems. See ScribusDoc::assignPageModeLists() QList* Items {nullptr}; Index: scribus/scribusdoc.cpp =================================================================== --- scribus/scribusdoc.cpp (revision 27773) +++ scribus/scribusdoc.cpp (working copy) @@ -4847,6 +4847,42 @@ return m_docUnitRatio; } +void ScribusDoc::startMasterPageEditSession() +{ + m_masterNamesAtSessionStart.clear(); + for (auto it = MasterNames.cbegin(); it != MasterNames.cend(); ++it) + m_masterNamesAtSessionStart.insert(it.key()); + m_touchedMasterPagesThisSession.clear(); + m_masterPageEditSessionActive = true; +} + +void ScribusDoc::endMasterPageEditSession() +{ + m_masterPageEditSessionActive = false; + m_masterNamesAtSessionStart.clear(); + m_touchedMasterPagesThisSession.clear(); +} + +bool ScribusDoc::pageNeedsMasterPageReapply(uint pageIndex) const +{ + if (!m_masterPageEditSessionActive) + return true; + if (pageIndex >= static_cast(DocPages.count())) + return true; + + // If any master was added, removed, or renamed during the session, the + // name sets won't match - safest to reapply every page rather than try + // to reason precisely about what a rename or deletion affects. + QSet currentMasterNames; + for (auto it = MasterNames.cbegin(); it != MasterNames.cend(); ++it) + currentMasterNames.insert(it.key()); + if (currentMasterNames != m_masterNamesAtSessionStart) + return true; + + QString masterName = DocPages.at(static_cast(pageIndex))->masterPageName(); + return m_touchedMasterPagesThisSession.contains(masterName); +} + bool ScribusDoc::applyMasterPage(const QString& pageName, int pageNumber) { if (!MasterNames.contains(pageName)) @@ -13949,6 +13985,8 @@ void ScribusDoc::changed() { + if (m_masterPageEditSessionActive && m_masterPageMode && m_currentPage) + m_touchedMasterPagesThisSession.insert(m_currentPage->pageName()); setModified(true); // Do not emit docChanged signal() unnecessarily // Processing of that signal is slowwwwwww and Index: scribus/scribus.cpp =================================================================== --- scribus/scribus.cpp (revision 27773) +++ scribus/scribus.cpp (working copy) @@ -7758,7 +7758,11 @@ appModeHelper->setMasterPageEditMode(false, doc); int pageCount = doc->DocPages.count(); for (int i = 0; i < pageCount; ++i) - Apply_MasterPage(doc->DocPages.at(i)->masterPageName(), i, false); + { + if (doc->pageNeedsMasterPageReapply(i)) + Apply_MasterPage(doc->DocPages.at(i)->masterPageName(), i, false); + } + doc->endMasterPageEditSession(); pagePalette->endMasterPageMode(); if (m_pagePaletteWasClosed && pagePalette->isFloating()) Index: scribus/ui/pagepalette.cpp =================================================================== --- scribus/ui/pagepalette.cpp (revision 27773) +++ scribus/ui/pagepalette.cpp (working copy) @@ -174,10 +174,16 @@ void PagePalette::startMasterPageMode(const QString& masterPage) { ScribusDoc* doc = m_view->m_doc; - + bool mustDeselect = false; mustDeselect |= (!doc->masterPageMode()); mustDeselect |= (doc->masterPageMode() && doc->currentPage()->pageName() != masterPage); + // Only a genuine transition from normal mode into master-page mode + // starts a new tracking session - jumping directly to a different + // specific master while already in master-page mode (the second half + // of mustDeselect above) must not reset tracking mid-session. + if (!doc->masterPageMode()) + doc->startMasterPageEditSession(); if (mustDeselect) { // We must avoid deselecting directly if doc is in an edit mode,