[qet] [1884] Reworked the DiagramContext class to sort custom variables.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1884
Author:   xavier
Date:     2012-06-29 07:21:46 +0200 (Fri, 29 Jun 2012)
Log Message:
-----------
Reworked the DiagramContext class to sort custom variables.

Modified Paths:
--------------
    trunk/sources/diagramcontext.cpp
    trunk/sources/diagramcontext.h
    trunk/sources/titleblockpropertieswidget.cpp
    trunk/sources/titleblocktemplate.cpp

Modified: trunk/sources/diagramcontext.cpp
===================================================================
--- trunk/sources/diagramcontext.cpp	2012-06-29 05:21:43 UTC (rev 1883)
+++ trunk/sources/diagramcontext.cpp	2012-06-29 05:21:46 UTC (rev 1884)
@@ -21,8 +21,18 @@
 /**
 	@return a list containing all the keys in the context object.
 */
-QList<QString> DiagramContext::keys() const {
-	return(content_.keys());
+QList<QString> DiagramContext::keys(DiagramContext::KeyOrder order) const {
+	if (order == None) {
+		return content_.keys();
+	} else {
+		QList<QString> keys_list = content_.keys();
+		if (order == Alphabetical) {
+			qSort(keys_list);
+		} else {
+			qSort(keys_list.begin(), keys_list.end(), DiagramContext::stringLongerThan);
+		}
+		return(keys_list);
+	}
 }
 
 /**
@@ -72,6 +82,13 @@
 }
 
 /**
+	@return True if \a a is longer than \a b, false otherwise.
+*/
+bool DiagramContext::stringLongerThan(const QString &a, const QString &b) {
+	return (a.length() > b.length());
+}
+
+/**
 	@param key a key string
 	@return true if that key is acceptable, false otherwise
 */

Modified: trunk/sources/diagramcontext.h
===================================================================
--- trunk/sources/diagramcontext.h	2012-06-29 05:21:43 UTC (rev 1883)
+++ trunk/sources/diagramcontext.h	2012-06-29 05:21:46 UTC (rev 1884)
@@ -27,7 +27,12 @@
 */
 class DiagramContext {
 	public:
-	QList<QString> keys() const;
+	enum KeyOrder {
+		None,
+		Alphabetical,
+		DecreasingLength
+	};
+	QList<QString> keys(KeyOrder = None) const;
 	bool contains(const QString &) const;
 	const QVariant operator[](const QString &) const;
 	bool addValue(const QString &, const QVariant &);
@@ -38,6 +43,7 @@
 	static QString validKeyRegExp();
 	
 	private:
+	static bool stringLongerThan(const QString &, const QString &);
 	bool keyIsAcceptable(const QString &) const;
 	/// Diagram context data (key/value pairs)
 	QHash<QString, QVariant> content_;

Modified: trunk/sources/titleblockpropertieswidget.cpp
===================================================================
--- trunk/sources/titleblockpropertieswidget.cpp	2012-06-29 05:21:43 UTC (rev 1883)
+++ trunk/sources/titleblockpropertieswidget.cpp	2012-06-29 05:21:46 UTC (rev 1884)
@@ -145,7 +145,7 @@
 void TitleBlockPropertiesWidget::setDiagramContext(const DiagramContext &context) {
 	clearDiagramContext();
 	int i = 0;
-	foreach (QString key, context.keys()) {
+	foreach (QString key, context.keys(DiagramContext::Alphabetical)) {
 		additional_fields_table -> setItem(i, 0, new QTableWidgetItem(key));
 		additional_fields_table -> setItem(i, 1, new QTableWidgetItem(context[key].toString()));
 		++ i;

Modified: trunk/sources/titleblocktemplate.cpp
===================================================================
--- trunk/sources/titleblocktemplate.cpp	2012-06-29 05:21:43 UTC (rev 1883)
+++ trunk/sources/titleblocktemplate.cpp	2012-06-29 05:21:46 UTC (rev 1884)
@@ -1316,13 +1316,13 @@
 }
 
 /**
- @param string A text containing 0 to n variables, e.g. "%var" or "%{var}"
- @param diagram_context Diagram context to use to interprete variables
- @return the provided string with variables replaced by the values from the diagram context
+	@param string A text containing 0 to n variables, e.g. "%var" or "%{var}"
+	@param diagram_context Diagram context to use to interprete variables
+	@return the provided string with variables replaced by the values from the diagram context
 */
 QString TitleBlockTemplate::interpreteVariables(const QString &string, const DiagramContext &diagram_context) const {
 	QString interpreted_string = string;
-	foreach (QString key, diagram_context.keys()) {
+	foreach (QString key, diagram_context.keys(DiagramContext::DecreasingLength)) {
 		interpreted_string.replace("%{" + key + "}", diagram_context[key].toString());
 		interpreted_string.replace("%" + key,        diagram_context[key].toString());
 	}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/