From 86a064d24f39ef3fbda676c18166d31106bf3bad Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 20 Sep 2009 20:44:21 +0100
Subject: Sort the Qt roster.


diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp b/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
index 00a84f3..ae6d58d 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
@@ -2,6 +2,7 @@
 #include "Swift/QtUI/Roster/QtTreeWidget.h"
 
 #include <qdebug.h>
+#include <QtAlgorithms>
 
 namespace Swift {
 
@@ -119,13 +120,33 @@ void QtTreeWidgetItem::removeChild(QtTreeWidgetItem* child) {
 	handleChanged(this);
 }
 
+void bubbleSort(QList<QtTreeWidgetItem*>& list) {
+	for (int i = 0; i < list.size(); i++) {
+		for (int j = i + 1; j < list.size(); j++) {
+			if (*(list[j]) < *(list[j - 1])) {
+				QtTreeWidgetItem* lower = list[j];
+				list[j] = list[j - 1];
+				list[j - 1] = lower;
+			}
+		}
+	}
+}
+
 void QtTreeWidgetItem::handleChanged(QtTreeWidgetItem* child) {
+	/*We don't use the much faster qStableSort because it causes changed(child) and all sorts of nasty recursion*/
+	//qStableSort(children_.begin(), children_.end(), itemLessThan);
+	bubbleSort(children_);
 	shownChildren_.clear();
 	for (int i = 0; i < children_.size(); i++) {
 		if (children_[i]->isShown()) {
 			shownChildren_.append(children_[i]);
 		}
 	}
+
+	qDebug() << "List sorted, now:";
+	for (int i = 0; i < shownChildren_.size(); i++) {
+		qDebug() << (int)(shownChildren_[i]->getStatusShow()) << ":" << shownChildren_[i]->getName();
+ 	}
 	emit changed(child);
 }
 
@@ -179,7 +200,7 @@ QIcon QtTreeWidgetItem::getPresenceIcon() {
 	return QIcon(":/icons/" + iconString + ".png");
 }
 
-bool QtTreeWidgetItem::isContact() {
+bool QtTreeWidgetItem::isContact() const {
 	return children_.size() == 0;
 }
 
@@ -187,4 +208,22 @@ bool QtTreeWidgetItem::isExpanded() {
 	return expanded_;
 }
 
+bool QtTreeWidgetItem::operator<(const QtTreeWidgetItem& item) const {
+	if (isContact()) {
+		if (!item.isContact()) {
+			return false;
+		}
+		return getStatusShow() == item.getStatusShow() ? getName().toLower() < item.getName().toLower() : getStatusShow() < item.getStatusShow();
+	} else {
+		if (item.isContact()) {
+			return true;
+		}
+		return getName().toLower() < item.getName().toLower();
+	}
+}
+
+//bool itemLessThan(const QtTreeWidgetItem& left, const QtTreeWidgetItem& right) {
+//	return left < right;
+//}
+
 }
diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.h b/Swift/QtUI/Roster/QtTreeWidgetItem.h
index 7a459d5..8b69ae2 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetItem.h
+++ b/Swift/QtUI/Roster/QtTreeWidgetItem.h
@@ -48,12 +48,15 @@ class QtTreeWidgetItem : public QObject, public TreeWidgetItem {
 			void hide();
 			void show();
 			bool isShown();
-			bool isContact();
+			bool isContact() const;
 			bool isExpanded();
+			QString getName() const {return displayName_;};
+			StatusShow::Type getStatusShow() const {return statusShowType_;};
 
 			QWidget* getCollapsedRosterWidget();
 			QWidget* getExpandedRosterWidget();
-		
+			bool operator<(const QtTreeWidgetItem& item) const;
+			
 		signals:
 			void changed(QtTreeWidgetItem*);
 		private slots:
@@ -72,6 +75,8 @@ class QtTreeWidgetItem : public QObject, public TreeWidgetItem {
 			StatusShow::Type statusShowType_;
 };
 
+//bool itemLessThan(const QtTreeWidgetItem& left, const QtTreeWidgetItem& right);
+
 }
 #endif
 
-- 
cgit v0.10.2-6-g49f6