r23595 by jghali - Do not propose an already existing pattern name when sending an item to patterns or converting it to symbol

scribus-commit scribus-commit at lists.scribus.net
Sun Apr 12 17:40:01 UTC 2020


Author: jghali
Date: Sun Apr 12 17:40:01 2020
New Revision: 23595

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=23595
Log:
Do not propose an already existing pattern name when sending an item to patterns or converting it to symbol

Modified:
    trunk/Scribus/scribus/scribus.cpp
    trunk/Scribus/scribus/scribusdoc.cpp
    trunk/Scribus/scribus/scribusdoc.h

Modified: trunk/Scribus/scribus/scribus.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=23595&path=/trunk/Scribus/scribus/scribus.cpp
==============================================================================
--- trunk/Scribus/scribus/scribus.cpp	(original)
+++ trunk/Scribus/scribus/scribus.cpp	Sun Apr 12 17:40:01 2020
@@ -9154,8 +9154,11 @@
 {
 	if (!HaveDoc)
 		return;
-	QString patternName = "Pattern_"+doc->m_Selection->itemAt(0)->itemName();
+
+	QString patternName("Pattern_" + doc->m_Selection->itemAt(0)->itemName());
 	patternName = patternName.trimmed().simplified().replace(" ", "_");
+	patternName = doc->getUniquePatternName(patternName);
+
 	bool savedAlignGrid = doc->SnapGrid;
 	bool savedAlignGuides = doc->SnapGuides;
 	bool savedAlignElement = doc->SnapElement;
@@ -9299,8 +9302,11 @@
 		return;
 	if (doc->m_Selection->isEmpty())
 		return;
-	QString patternName("Pattern_"+doc->m_Selection->itemAt(0)->itemName());
+
+	QString patternName("Pattern_" + doc->m_Selection->itemAt(0)->itemName());
 	patternName = patternName.trimmed().simplified().replace(" ", "_");
+	patternName = doc->getUniquePatternName(patternName);
+
 	Query dia(this, "tt", 1, tr("&Name:"), tr("New Entry"));
 	dia.setEditText(patternName, true);
 	patternsDependingOnThis.clear();

Modified: trunk/Scribus/scribus/scribusdoc.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=23595&path=/trunk/Scribus/scribus/scribusdoc.cpp
==============================================================================
--- trunk/Scribus/scribus/scribusdoc.cpp	(original)
+++ trunk/Scribus/scribus/scribusdoc.cpp	Sun Apr 12 17:40:01 2020
@@ -36,6 +36,7 @@
 #include <QPixmap>
 #include <QPointer>
 #include <QProgressBar>
+#include <QRegExp>
 #include <QtAlgorithms>
 #include <QTime>
 //#include <qtconcurrentmap.h>
@@ -3747,6 +3748,42 @@
 	docPatterns = patterns;
 }
 
+QString ScribusDoc::getUniquePatternName(const QString& originalName) const
+{
+	if (!docPatterns.contains(originalName))
+		return originalName;
+
+	QString newName(originalName);
+
+	// Search the string for (number) at the end and capture
+	// both the number and the text leading up to it sans brackets.
+	//     Copy of fred (5)
+	//     ^^^^^^^^^^^^  ^   (where ^ means captured)
+	QRegExp rx("^(.*)\\s+\\((\\d+)\\)$");
+	int numMatches = rx.lastIndexIn(originalName);
+	// Add a (number) suffix to the end of the name. We start at the
+	// old suffix's value if there was one, or at 2 if there was not.
+	int suffixNum = 1;
+	QString prefix(newName);
+	if (numMatches != -1)
+	{
+		// Already had a suffix; use the name w/o suffix for prefix and
+		// grab the old suffix value as a starting point.
+		QStringList matches = rx.capturedTexts();
+		prefix = matches[1];
+		suffixNum = matches[2].toInt();
+	}
+	// Keep on incrementing the suffix 'till we find a free name
+	do
+	{
+		suffixNum ++;
+		newName = prefix + " (" + QString::number(suffixNum) + ")";
+	}
+	while (docPatterns.contains(newName));
+
+	return newName;
+}
+
 QStringList ScribusDoc::getUsedPatterns() const
 {
 	QList<PageItem*> allItems;

Modified: trunk/Scribus/scribus/scribusdoc.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=23595&path=/trunk/Scribus/scribus/scribusdoc.h
==============================================================================
--- trunk/Scribus/scribus/scribusdoc.h	(original)
+++ trunk/Scribus/scribus/scribusdoc.h	Sun Apr 12 17:40:01 2020
@@ -707,6 +707,10 @@
 	*/
 	ScPattern* checkedPattern(const QString &name);
 	/*!
+	* @brief Get a unique pattern name
+	*/
+	QString getUniquePatternName(const QString& originalName) const;
+	/*!
 	* @brief Builds a QStringList of the patterns used within the document
 	*/
 	QStringList getUsedPatterns() const;




More information about the scribus-commit mailing list