r22511 by jghali - remove some iterator ugliness by using c++11 auto type deduction

scribus-commit scribus-commit at lists.scribus.net
Tue May 1 22:54:37 UTC 2018


Author: jghali
Date: Tue May  1 22:54:37 2018
New Revision: 22511

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=22511
Log:
remove some iterator ugliness by using c++11 auto type deduction

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

Modified: trunk/Scribus/scribus/scribusdoc.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22511&path=/trunk/Scribus/scribus/scribusdoc.cpp
==============================================================================
--- trunk/Scribus/scribus/scribusdoc.cpp	(original)
+++ trunk/Scribus/scribus/scribusdoc.cpp	Tue May  1 22:54:37 2018
@@ -3612,25 +3612,26 @@
 }
 
 
-void ScribusDoc::orderedLayerList(QStringList* list)
-{
-	Q_ASSERT(list!=nullptr);
-	int layerCount=Layers.count();
-	if (layerCount != 0)
-	{
-		for (int i=0; i < layerCount; ++i)
-		{
-			ScLayers::iterator itend=Layers.end();
-			for (ScLayers::iterator it = Layers.begin(); it != itend; ++it)
-			{
-				if (layerCount-(*it).Level-1 == i)
-					list->append((*it).Name);
-			}
- 		}
+void ScribusDoc::orderedLayerList(QStringList* list) const
+{
+	Q_ASSERT(list != nullptr);
+
+	int layerCount = Layers.count();
+	if (layerCount == 0)
+		return;
+
+	for (int i=0; i < layerCount; ++i)
+	{
+		auto itend = Layers.cend();
+		for (auto it = Layers.cbegin(); it != itend; ++it)
+		{
+			if (layerCount - it->Level - 1 == i)
+				list->append(it->Name);
+		}
  	}
 }
 
-int ScribusDoc::firstLayerID()
+int ScribusDoc::firstLayerID() const
 {
 	QStringList newNames;
 	orderedLayerList(&newNames);
@@ -3701,18 +3702,17 @@
 	}
 }
 
-void ScribusDoc::getUsedColors(ColorList &colorsToUse, bool spot)
+void ScribusDoc::getUsedColors(ColorList &colorsToUse, bool spot) const
 {
 	bool found;
 	colorsToUse.clear();
-	colorsToUse.setDocument(this);
-	ColorList::Iterator it;
+	colorsToUse.setDocument(const_cast<ScribusDoc*>(this));
 
 	ResourceCollection resources;
 	this->getNamedResources(resources);
 	const QMap<QString, QString>& resColors = resources.colors();
 
-	for (it = PageColors.begin(); it != PageColors.end(); ++it)
+	for (auto it = PageColors.cbegin(); it != PageColors.cend(); ++it)
 	{
 		found = false;
 		// Tool preferences colors
@@ -3751,19 +3751,18 @@
 	}
 }
 
-bool ScribusDoc::lineStylesUseColor(const QString& colorName)
+bool ScribusDoc::lineStylesUseColor(const QString& colorName) const
 {
 	bool found = false;
-	QHash<QString,multiLine>::const_iterator itm, itmend;
 	multiLine::const_iterator its, itsend;
-	itmend = MLineStyles.constEnd();
-	for (itm = MLineStyles.constBegin(); itm != itmend && !found; ++itm)
+	auto itmend = MLineStyles.constEnd();
+	for (auto itm = MLineStyles.constBegin(); itm != itmend && !found; ++itm)
 	{
 		const multiLine& ml = itm.value();
 		itsend = ml.constEnd();
 		for (its = ml.constBegin(); its != itsend; ++its)
 		{
-			if ( its->Color == colorName )
+			if (its->Color == colorName)
 			{
 				found = true;
 				break;
@@ -3773,22 +3772,21 @@
 	return found;
 }
 
-void ScribusDoc::getUsedGradients(QHash<QString, VGradient> &gradients)
+void ScribusDoc::getUsedGradients(QHash<QString, VGradient> &gradients) const
 {
 	ResourceCollection resources;
 	this->getNamedResources(resources);
 	const QMap<QString, QString>& resGradients = resources.gradients();
-	QHash<QString, VGradient>::iterator it;
-	for (it = docGradients.begin(); it != docGradients.end(); ++it)
+	for (auto it = docGradients.cbegin(); it != docGradients.cend(); ++it)
 	{
 		if (resGradients.contains(it.key()))
-			gradients.insert(it.key(), docGradients[it.key()]);
-	}
-}
-
-bool ScribusDoc::addGradient(QString &name, VGradient &gradient)
-{
-	for (QHash<QString, VGradient>::Iterator it = docGradients.begin(); it != docGradients.end(); ++it)
+			gradients.insert(it.key(), it.value());
+	}
+}
+
+bool ScribusDoc::addGradient(QString &name, const VGradient &gradient)
+{
+	for (auto it = docGradients.cbegin(); it != docGradients.cend(); ++it)
  	{
 		if (it.value() == gradient)
 		{
@@ -3803,7 +3801,7 @@
 	return true;
 }
 
-void ScribusDoc::setGradients(QHash<QString, VGradient> &gradients)
+void ScribusDoc::setGradients(const QHash<QString, VGradient> &gradients)
 {
 	docGradients.clear();
 	docGradients = gradients;
@@ -3908,7 +3906,7 @@
 	return pattern;
 }
 
-void ScribusDoc::setPatterns(QHash<QString, ScPattern> &patterns)
+void ScribusDoc::setPatterns(const QHash<QString, ScPattern> &patterns)
 {
 	docPatterns.clear();
 	docPatterns = patterns;
@@ -4025,61 +4023,59 @@
 }
 
 
-QStringList ScribusDoc::getUsedPatternsSelection(Selection* customSelection)
+QStringList ScribusDoc::getUsedPatternsSelection(Selection* customSelection) const
 {
 	QStringList results;
 	int selectedItemCount = customSelection->count();
-	if (selectedItemCount != 0)
-	{
-		for (int i = 0; i < selectedItemCount; ++i)
-		{
-			PageItem *currItem = customSelection->itemAt(i);
-			QList<PageItem*> allItems;
-			if (currItem->isGroup())
-				allItems = currItem->getAllChildren();
-			else
-				allItems.append(currItem);
-			for (int j = 0; j < allItems.count(); j++)
-			{
-				currItem = allItems.at(j);
-				if ((currItem->GrType == 8) || (currItem->itemType() == PageItem::Symbol))
-				{
-					const QString& pat = currItem->pattern();
-					if (!pat.isEmpty() && !results.contains(pat))
-						results.append(currItem->pattern());
-				}
-				const QString& pat2 = currItem->strokePattern();
-				if (!pat2.isEmpty() && !results.contains(pat2))
-					results.append(currItem->strokePattern());
-				const QString& pat3 = currItem->patternMask();
-				if (!pat3.isEmpty() && !results.contains(pat3))
-					results.append(currItem->patternMask());
-			}
-			allItems.clear();
-		}
-		QStringList results2 = results;
-		for (int i = 0; i < results.count(); ++i)
-		{
-			QStringList pats = getUsedPatternsHelper(results[i], results2);
-			if (pats.isEmpty())
-				continue;
-			for (int j = 0; j < pats.count(); j++)
-			{
-				if (!results2.contains(pats[j]))
-					results2.append(pats[j]);
-			}
-		}
-		return results2;
-	}
-	else
+	if (selectedItemCount == 0)
 		return QStringList();
-}
-
-QStringList ScribusDoc::getUsedPatternsHelper(QString pattern, QStringList &results)
+
+	for (int i = 0; i < selectedItemCount; ++i)
+	{
+		PageItem *currItem = customSelection->itemAt(i);
+		QList<PageItem*> allItems;
+		if (currItem->isGroup())
+			allItems = currItem->getAllChildren();
+		else
+			allItems.append(currItem);
+		for (int j = 0; j < allItems.count(); j++)
+		{
+			currItem = allItems.at(j);
+			if ((currItem->GrType == 8) || (currItem->itemType() == PageItem::Symbol))
+			{
+				const QString& pat = currItem->pattern();
+				if (!pat.isEmpty() && !results.contains(pat))
+					results.append(currItem->pattern());
+			}
+			const QString& pat2 = currItem->strokePattern();
+			if (!pat2.isEmpty() && !results.contains(pat2))
+				results.append(currItem->strokePattern());
+			const QString& pat3 = currItem->patternMask();
+			if (!pat3.isEmpty() && !results.contains(pat3))
+				results.append(currItem->patternMask());
+		}
+		allItems.clear();
+	}
+	QStringList results2 = results;
+	for (int i = 0; i < results.count(); ++i)
+	{
+		QStringList pats = getUsedPatternsHelper(results[i], results2);
+		if (pats.isEmpty())
+			continue;
+		for (int j = 0; j < pats.count(); j++)
+		{
+			if (!results2.contains(pats[j]))
+				results2.append(pats[j]);
+		}
+	}
+	return results2;
+}
+
+QStringList ScribusDoc::getUsedPatternsHelper(QString pattern, QStringList &results) const
 {
 	if (!docPatterns.contains(pattern))
 		return QStringList();
-	ScPattern *pat = &docPatterns[pattern];
+	const ScPattern *pat = &docPatterns[pattern];
 	QStringList pats;
 	for (int i = 0; i < pat->items.count(); ++i)
 	{
@@ -4124,7 +4120,7 @@
 	return results;
 }
 
-QStringList ScribusDoc::getPatternDependencyList(QStringList used)
+QStringList ScribusDoc::getPatternDependencyList(QStringList used) const
 {
 	QStringList results;
 	QStringList pp;
@@ -4157,7 +4153,7 @@
 	return results;
 }
 
-QStringList ScribusDoc::getUsedSymbols()
+QStringList ScribusDoc::getUsedSymbols() const
 {
 	QList<PageItem*> allItems;
 	QStringList results;
@@ -4223,7 +4219,7 @@
 		}
 		allItems.clear();
 	}
-	for (QHash<QString, ScPattern>::Iterator it = docPatterns.begin(); it != docPatterns.end(); ++it)
+	for (auto it = docPatterns.cbegin(); it != docPatterns.cend(); ++it)
 	{
 		for (int i = 0; i < it.value().items.count(); ++i)
 		{
@@ -4249,9 +4245,9 @@
 	return results;
 }
 
-QStringList ScribusDoc::getUsedSymbolsHelper(QString pattern, QStringList &results)
-{
-	ScPattern *pat = &docPatterns[pattern];
+QStringList ScribusDoc::getUsedSymbolsHelper(QString pattern, QStringList &results) const
+{
+	const ScPattern *pat = &docPatterns[pattern];
 	QStringList pats;
 	for (int i = 0; i < pat->items.count(); ++i)
 	{
@@ -4635,23 +4631,24 @@
 }
 
 
-void ScribusDoc::getUsedProfiles(ProfilesL& usedProfiles)
+void ScribusDoc::getUsedProfiles(ProfilesL& usedProfiles) const
 {
 	PageItem* it = nullptr;
 	QStringList profileNames;
+	QList<PageItem*> allItems;
 	int counter = 0;
+
 	usedProfiles.clear();
 
-	QList<PageItem*> allItems;
 	profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultSolidColorRGBProfile);
 	profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultSolidColorCMYKProfile);
-	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultImageRGBProfile) < 0 )
+	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultImageRGBProfile) < 0)
 		profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultImageRGBProfile);
-	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultImageCMYKProfile) < 0 )
+	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultImageCMYKProfile) < 0)
 		profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultImageCMYKProfile);
-	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultMonitorProfile) < 0 )
+	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultMonitorProfile) < 0)
 		profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultMonitorProfile);
-	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultPrinterProfile) < 0 )
+	if (profileNames.indexOf(m_docPrefsData.colorPrefs.DCMSset.DefaultPrinterProfile) < 0)
 		profileNames.append(m_docPrefsData.colorPrefs.DCMSset.DefaultPrinterProfile);
 
 	if (profileNames.indexOf(m_docPrefsData.pdfPrefs.SolidProf) < 0)
@@ -4661,18 +4658,14 @@
 	if (profileNames.indexOf(m_docPrefsData.pdfPrefs.PrintProf) < 0)
 		profileNames.append(m_docPrefsData.pdfPrefs.PrintProf);
 	
+	const QList<PageItem*>* itemLists[] = { &MasterItems, &DocItems };
 	for (int lc = 0; lc < 2; ++lc)
 	{
-		if (lc == 0)
-			counter = MasterItems.count();
-		else if (lc == 1)
-			counter = DocItems.count();
+		const auto* pItemList = itemLists[lc];
+		counter = pItemList->count();
 		for (int d = 0; d < counter; ++d)
 		{
-			if (lc == 0)
-				it = MasterItems.at(d);
-			else if (lc == 1)
-				it = DocItems.at(d);
+			it = pItemList->at(d);
 			if (it->isGroup())
 				allItems = it->getAllChildren();
 			else
@@ -4687,7 +4680,8 @@
 			allItems.clear();
 		}
 	}
-	for (auto itf = FrameItems.begin(); itf != FrameItems.end(); ++itf)
+
+	for (auto itf = FrameItems.cbegin(); itf != FrameItems.cend(); ++itf)
 	{
 		PageItem *it = itf.value();
 		if (it->isGroup())
@@ -4704,7 +4698,7 @@
 		allItems.clear();
 	}
 
-	for (QStringList::Iterator pIter = profileNames.begin(); pIter != profileNames.end(); pIter++)
+	for (auto pIter = profileNames.cbegin(); pIter != profileNames.cend(); pIter++)
 	{
 		if (ScCore->InputProfiles.contains(*pIter))
 			usedProfiles[*pIter] = ScCore->InputProfiles[*pIter];

Modified: trunk/Scribus/scribus/scribusdoc.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22511&path=/trunk/Scribus/scribus/scribusdoc.h
==============================================================================
--- trunk/Scribus/scribus/scribusdoc.h	(original)
+++ trunk/Scribus/scribus/scribusdoc.h	Tue May  1 22:54:37 2018
@@ -501,16 +501,17 @@
 	 * @brief Return a list of the layers in their order
 	 * @param list QStringList to insert the layer names into
 	 */
-	void orderedLayerList(QStringList* list);
-
-	int firstLayerID();
+	void orderedLayerList(QStringList* list) const;
+
+	int firstLayerID() const;
+
 	//Items
 	bool deleteTaggedItems();
 
 	/*!
 		* @brief Builds a qmap of the icc profiles used within the document
 	 */
-	void getUsedProfiles(ProfilesL& usedProfiles);
+	void getUsedProfiles(ProfilesL& usedProfiles) const;
 	bool OpenCMSProfiles(ProfilesL InPo, ProfilesL InPoCMYK, ProfilesL MoPo, ProfilesL PrPo);
 	void CloseCMSProfiles();
 	void SetDefaultCMSParams();
@@ -675,26 +676,26 @@
 	/*!
 	* @brief Builds a qmap of the colours used within the document
 	*/
-	void getUsedColors(ColorList &colorsToUse, bool spot = false);
+	void getUsedColors(ColorList &colorsToUse, bool spot = false) const;
 	/*!
 	* @brief Return if a specific color is used by line styles
 	*/
-	bool lineStylesUseColor(const QString& colorName);
+	bool lineStylesUseColor(const QString& colorName) const;
 	/*!
 	* @brief Builds a qmap of the gradients used within the document
 	*/
-	void getUsedGradients(QHash<QString, VGradient> &Gradients);
+	void getUsedGradients(QHash<QString, VGradient> &Gradients) const;
 	/*!
 	* @brief Set the gradients for a document
 	*/
-	bool addGradient(QString &name, VGradient &gradient);
-	void setGradients(QHash<QString, VGradient> &gradients);
+	bool addGradient(QString &name, const VGradient &gradient);
+	void setGradients(const QHash<QString, VGradient> &gradients);
 	/*!
 	* @brief Set the patterns for a document
 	*/
 	bool addPattern(QString &name, ScPattern& pattern);
 	void removePattern(QString name);
-	void setPatterns(QHash<QString, ScPattern> &patterns);
+	void setPatterns(const QHash<QString, ScPattern> &patterns);
 	/*!
 	* @brief Check pattern with specified name and return it if valid
 	*/
@@ -703,14 +704,14 @@
 	* @brief Builds a QStringList of the patterns used within the document
 	*/
 	QStringList getUsedPatterns() const;
-	QStringList getUsedPatternsSelection(Selection* customSelection);
-	QStringList getUsedPatternsHelper(QString pattern, QStringList &results);
-	QStringList getPatternDependencyList(QStringList used);
+	QStringList getUsedPatternsSelection(Selection* customSelection) const;
+	QStringList getUsedPatternsHelper(QString pattern, QStringList &results) const;
+	QStringList getPatternDependencyList(QStringList used) const;
 	/*!
 	* @brief Builds a QStringList of the symbols used within the document
 	*/
-	QStringList getUsedSymbols();
-	QStringList getUsedSymbolsHelper(QString pattern, QStringList &results);
+	QStringList getUsedSymbols() const;
+	QStringList getUsedSymbolsHelper(QString pattern, QStringList &results) const;
 
 	/*!
 	* @brief Check if document use Acrobat Form Fields




More information about the scribus-commit mailing list