[qet] [2029] Element editor: revamped ElementScene::zItems() to fulfil the needs of ElementScene::managePrimitivesGroups()

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


Revision: 2029
Author:   xavier
Date:     2013-02-08 23:05:20 +0100 (Fri, 08 Feb 2013)
Log Message:
-----------
Element editor: revamped ElementScene::zItems() to fulfil the needs of ElementScene::managePrimitivesGroups()

Modified Paths:
--------------
    trunk/sources/editor/editorcommands.cpp
    trunk/sources/editor/elementscene.cpp
    trunk/sources/editor/elementscene.h
    trunk/sources/editor/qetelementeditor.cpp

Modified: trunk/sources/editor/editorcommands.cpp
===================================================================
--- trunk/sources/editor/editorcommands.cpp	2013-02-08 22:05:18 UTC (rev 2028)
+++ trunk/sources/editor/editorcommands.cpp	2013-02-08 22:05:20 UTC (rev 2029)
@@ -291,7 +291,7 @@
 		if (!part -> zValue()) {
 			// the added part has no specific zValue already defined, we put it
 			// above existing items (but still under terminals)
-			QList<QGraphicsItem *> existing_items = editor_scene_ -> zItems();
+			QList<QGraphicsItem *> existing_items = editor_scene_ -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
 			qreal z = existing_items.count() ? existing_items.last() -> zValue() + 1 : 1;
 			part -> setZValue(z);
 		}
@@ -522,8 +522,8 @@
 	ElementEditionCommand(elmt, 0, parent),
 	option(o)
 {
-	// recupere les parties de l'elements, sauf les bornes
-	QList<QGraphicsItem *> items_list = editor_scene_ -> zItems();
+	// retrieve all primitives but terminals
+	QList<QGraphicsItem *> items_list = editor_scene_ -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
 	
 	// prend un snapshot des zValues
 	foreach(QGraphicsItem *qgi, items_list) undo_hash.insert(qgi, qgi -> zValue());

Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp	2013-02-08 22:05:18 UTC (rev 2028)
+++ trunk/sources/editor/elementscene.cpp	2013-02-08 22:05:20 UTC (rev 2029)
@@ -487,7 +487,7 @@
 	
 	QDomElement description = xml_document.createElement("description");
 	// description de l'element
-	foreach(QGraphicsItem *qgi, zItems(true)) {
+	foreach(QGraphicsItem *qgi, zItems()) {
 		// si l'export ne concerne que la selection, on ignore les parties non selectionnees
 		if (!all_parts && !qgi -> isSelected()) continue;
 		if (CustomElementPart *ce = dynamic_cast<CustomElementPart *>(qgi)) {
@@ -961,30 +961,56 @@
 	@param include_terminals true pour inclure les bornes, false sinon
 	@return les parties de l'element ordonnes par zValue croissante
 */
-QList<QGraphicsItem *> ElementScene::zItems(bool include_terminals) const {
-	// recupere les elements
+QList<QGraphicsItem *> ElementScene::zItems(ItemOptions options) const {
+	// handle dummy request, i.e. when neither Selected nor NonSelected are set
+	if (!(options & ElementScene::Selected) && !(options & ElementScene::NonSelected)) {
+		return(QList<QGraphicsItem *>());
+	}
+	
+	// retrieve all items
 	QList<QGraphicsItem *> all_items_list(items());
+	QMutableListIterator<QGraphicsItem *> i(all_items_list);
 	
-	// enleve les bornes
+	// remove unrequired items
+	if ((options & ElementScene::SelectedOrNot) != ElementScene::SelectedOrNot) {
+		bool keep_selected = options & ElementScene::Selected;
+		while (i.hasNext()) {
+			if (i.next() -> isSelected() != keep_selected) {
+				i.remove();
+			}
+		}
+	}
+	
 	QList<QGraphicsItem *> terminals;
-	foreach(QGraphicsItem *qgi, all_items_list) {
+	QList<QGraphicsItem *> helpers;
+	for (i.toFront(); i.hasNext(); ) {
+		i.next();
+		QGraphicsItem *qgi = i.value();
 		if (
 			qgi -> type() == ElementPrimitiveDecorator::Type ||
 			qgi -> type() == QGraphicsRectItem::Type
 		) {
-			all_items_list.removeAt(all_items_list.indexOf(qgi));
+			i.remove();
+			helpers << qgi;
 		}
 		else if (qgraphicsitem_cast<PartTerminal *>(qgi)) {
-			all_items_list.removeAt(all_items_list.indexOf(qgi));
+			i.remove();
 			terminals << qgi;
 		}
 	}
 	
 	// ordonne les parties par leur zValue
-	qSort(all_items_list.begin(), all_items_list.end(), ElementScene::zValueLessThan);
+	if (options & SortByZValue) {
+		qSort(all_items_list.begin(), all_items_list.end(), ElementScene::zValueLessThan);
+	}
 	
 	// rajoute eventuellement les bornes
-	if (include_terminals) all_items_list += terminals;
+	if (options & ElementScene::IncludeTerminals) {
+		all_items_list += terminals;
+	}
+	if (options & ElementScene::IncludeHelperItems) {
+		all_items_list += helpers;
+	}
 	return(all_items_list);
 }
 
@@ -993,7 +1019,7 @@
 */
 ElementContent ElementScene::selectedContent() const {
 	ElementContent content;
-	foreach(QGraphicsItem *qgi, zItems(true)) {
+	foreach(QGraphicsItem *qgi, zItems()) {
 		if (qgi -> isSelected()) content << qgi;
 	}
 	return(content);
@@ -1260,6 +1286,7 @@
 	represents the current selection.
 */
 void ElementScene::managePrimitivesGroups() {
+	// this function is not supposed to be reentrant
 	if (!decorator_lock_ -> tryLock()) return;
 	
 	if (!decorator_) {
@@ -1269,18 +1296,8 @@
 		decorator_ -> hide();
 	}
 	
-	QList<QGraphicsItem *> selected_items;
-	foreach (QGraphicsItem *item, items()) {
-		if (item -> type() == ElementPrimitiveDecorator::Type) continue;
-		if (item -> type() == QGraphicsRectItem::Type) continue;
-		if (item -> isSelected()) {
-			selected_items << item;
-		}
-	}
-	/// TODO export the above code to a proper method
-	
-	
 	// should we hide the decorator?
+	QList<QGraphicsItem *> selected_items = zItems(ElementScene::Selected | ElementScene::IncludeTerminals);
 	if (!selected_items.count()) {
 		decorator_ -> hide();
 	} else {

Modified: trunk/sources/editor/elementscene.h
===================================================================
--- trunk/sources/editor/elementscene.h	2013-02-08 22:05:18 UTC (rev 2028)
+++ trunk/sources/editor/elementscene.h	2013-02-08 22:05:20 UTC (rev 2029)
@@ -42,7 +42,17 @@
 	Q_OBJECT
 	
 	// enum
+	public:
 	enum Behavior { Normal, Line, Rectangle, Circle, Ellipse, Polygon, Text, Terminal, Arc, TextField, PasteArea };
+	enum ItemOption {
+		SortByZValue = 1,
+		IncludeTerminals = 2,
+		IncludeHelperItems = 4,
+		Selected = 8,
+		NonSelected = 16,
+		SelectedOrNot = 24
+	};
+	Q_DECLARE_FLAGS(ItemOptions, ItemOption);
 	
 	// constructors, destructor
 	public:
@@ -130,7 +140,7 @@
 	virtual void fromXml(const QDomDocument &, const QPointF & = QPointF(), bool = true, ElementContent * = 0);
 	virtual void reset();
 	virtual QList<CustomElementPart *> primitives() const;
-	virtual QList<QGraphicsItem *> zItems(bool = false) const;
+	virtual QList<QGraphicsItem *> zItems(ItemOptions options = ItemOptions(SortByZValue | IncludeTerminals | SelectedOrNot)) const;
 	virtual ElementContent selectedContent() const;
 	virtual void getPasteArea(const QRectF &);
 	QRectF borderRect() const;
@@ -209,6 +219,8 @@
 	void pasteAreaDefined(const QRectF &);
 };
 
+Q_DECLARE_OPERATORS_FOR_FLAGS(ElementScene::ItemOptions)
+
 /**
 	@param wid the new width for the currently edited element
 */

Modified: trunk/sources/editor/qetelementeditor.cpp
===================================================================
--- trunk/sources/editor/qetelementeditor.cpp	2013-02-08 22:05:18 UTC (rev 2028)
+++ trunk/sources/editor/qetelementeditor.cpp	2013-02-08 22:05:20 UTC (rev 2029)
@@ -1145,7 +1145,7 @@
 void QETElementEditor::slot_createPartsList() {
 	parts_list -> blockSignals(true);
 	parts_list -> clear();
-	QList<QGraphicsItem *> qgis = ce_scene -> zItems(true);
+	QList<QGraphicsItem *> qgis = ce_scene -> zItems();
 	
 	// on ne construit plus la liste a partir de 200 primitives
 	// c'est ingerable : la maj de la liste prend trop de temps et le resultat
@@ -1179,7 +1179,7 @@
 	} else if (items_count <= QET_MAX_PARTS_IN_ELEMENT_EDITOR_LIST) {
 		parts_list -> blockSignals(true);
 		int i = 0;
-		QList<QGraphicsItem *> items = ce_scene -> zItems(true);
+		QList<QGraphicsItem *> items = ce_scene -> zItems();
 		for (int j = items.count() - 1 ; j >= 0 ; -- j) {
 			QGraphicsItem *qgi = items[j];
 			QListWidgetItem *qlwi = parts_list -> item(i);


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