View Issue Details

IDProjectCategoryView StatusLast Update
0015940ScribusGeneralpublic2019-12-08 21:24
Reporterale Assigned Toale  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Product Version1.5.6.svn 
Fixed in Version1.5.6.svn 
Summary0015940: Duplication of names: add getUniqueName in util.cpp with the new "name (#)" pattern
Descriptionscribus currently often use "copy of ..."
in a previous (resolved) ticked for the styles it has been explained why adding a numeric suffix feels better.

i wanted to clone jean's getUniqueName() from styles/styleset.h into util.h and i discovered that util.h already contains a getUniqueName() with a "flexible" pattern.

the current "util.h" function does not seem to be very popular, because the code i was looking at (plugins/fileloader/scribus150format/scribus150format.cpp) implements its own "Copy of" logic.

i suggest that:

- all duplication of names should use the pattern "name (#)"
- if possible, all duplication of names should use the function defined in utils.h
- if possible, remove the template implementation in styles/styleset.h in favor of the one in utils.h

a patch for introducing getUniqueName() in util.h will follow very soon...
TagsNo tags attached.
PatchYes

Activities

ale

2019-11-13 10:41

manager   ~0047063

it's a template function accepting any type that implements contains() on the list.

i've most of the inline comments to the doc of the function itself, leaving only the inline comments about the regexp.

the first usage will follow right away for the duplication of master pages...

ale

2019-11-13 10:41

manager  

util-getuniquename.diff (1,392 bytes)   
diff --git a/scribus/util.h b/scribus/util.h
index 6e4529fd4..f36de9d7b 100644
--- a/scribus/util.h
+++ b/scribus/util.h
@@ -154,6 +154,45 @@ void    SCRIBUS_API getDashArray(int dashtype, double linewidth, QVector<float>
 bool SCRIBUS_API convertOldTable(ScribusDoc *m_Doc, PageItem* gItem, QList<PageItem*> &gpL, QStack< QList<PageItem*> > *groupStackT = nullptr, QList<PageItem *> *target = nullptr);
 
 void SCRIBUS_API setWidgetBoldFont(QWidget* w, bool wantBold);
+/*!
+ *\brief
+ * check if name exists in list
+ * if it exists then return "name (#)", where # counts up from
+ * existing "name (#)"s (starting from 2)
+ * \retval the current name or a new unique name
+*/
+template<class STRINGLIST>
+QString getUniqueName(const QString& name, const STRINGLIST& list)
+{
+	if (!list.contains(name))
+		return name;
+
+	QString newName(name);
+
+	QString prefix(newName);
+	int suffixNum = 1;
+
+	// capture the name and the index, if any
+	// fred (5)
+	// ^^^^  ^   (where ^ means captured)
+	static QRegExp rx("^(.*)\\s+\\((\\d+)\\)$");
+	if (rx.indexIn(newName) != -1)
+	{
+		QStringList matches = rx.capturedTexts();
+		prefix = matches[1];
+		suffixNum = matches[2].toInt();
+	}
+
+	do
+	{
+		suffixNum++;
+		newName = prefix + " (" + QString::number(suffixNum) + ")";
+	}
+	while (list.contains(newName));
+
+	return newName;
+}
+
 /*!
  *\brief
  * check if name exists in list
util-getuniquename.diff (1,392 bytes)   

Issue History

Date Modified Username Field Change
2019-11-13 10:09 ale New Issue
2019-11-13 10:09 ale Assigned To => ale
2019-11-13 10:09 ale Status new => assigned
2019-11-13 10:41 ale Note Added: 0047063
2019-11-13 10:41 ale File Added: util-getuniquename.diff
2019-11-13 10:43 ale Summary duplication of names: add getUniqueName in util.cpp with the new "name (#)" pattern => [PATCH] duplication of names: add getUniqueName in util.cpp with the new "name (#)" pattern
2019-11-13 10:43 ale Patch No => Yes
2019-11-13 22:24 cbradney Status assigned => resolved
2019-11-13 22:24 cbradney Fixed in Version => 1.5.6.svn
2019-11-13 22:24 cbradney Resolution open => fixed
2019-11-13 22:27 jghali Summary [PATCH] duplication of names: add getUniqueName in util.cpp with the new "name (#)" pattern => Duplication of names: add getUniqueName in util.cpp with the new "name (#)" pattern
2019-12-08 21:24 cbradney Status resolved => closed