From 1f48a865bc052b12c3a156426bd1ebdd5955c74e Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 14 May 2010 22:11:54 +0000
Subject: Start roster groups expanded.

Resolves: #343

diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index 97b055b..ba57421 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -8,6 +8,7 @@
 
 #include "Swiften/Base/Platform.h"
 #include "Swiften/Roster/ContactRosterItem.h"
+#include "Swiften/Roster/GroupRosterItem.h"
 #include "Swift/Controllers/UIEvents/UIEventStream.h"
 #include "Swift/Controllers/UIEvents/RequestChatUIEvent.h"
 
@@ -33,10 +34,10 @@ QtTreeWidget::QtTreeWidget(UIEventStream* eventStream, QWidget* parent) : QTreeV
 	setIndentation(0);
 	setRootIsDecorated(true);
 	connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&)));
-//	connect(model_, SIGNAL(itemExpanded(const QModelIndex&, bool)), this, SLOT(handleModelItemExpanded(const QModelIndex&, bool)));
+	connect(model_, SIGNAL(itemExpanded(const QModelIndex&, bool)), this, SLOT(handleModelItemExpanded(const QModelIndex&, bool)));
 //	connect(model_, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(handleDataChanged(const QModelIndex&, const QModelIndex&)));
-//	connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(handleExpanded(const QModelIndex&)));
-//	connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(handleCollapsed(const QModelIndex&)));
+	connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(handleExpanded(const QModelIndex&)));
+	connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(handleCollapsed(const QModelIndex&)));
 }
 
 QtTreeWidget::~QtTreeWidget() {
@@ -76,33 +77,33 @@ void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
 	}
 }
 
-//void QtTreeWidget::handleExpanded(const QModelIndex& index) {
-//	QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
-//	if (qtItem) {
-//		qtItem->setExpanded(true);
-//	}
-//}
-
-//void QtTreeWidget::handleCollapsed(const QModelIndex& index) {
-//	QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
-//	if (qtItem) {
-//		qtItem->setExpanded(false);
-//	}
-//}
-
-//void QtTreeWidget::handleModelItemExpanded(const QModelIndex& index, bool shouldExpand) {
-//	if (this->isExpanded(index) == shouldExpand) {
-//		return;
-//	}
-//	//setExpanded(index, shouldExpand);
-//	if (shouldExpand) {
-//		expand(index);
-//		emit expanded(index);
-//	} else {
-//		collapse(index);
-//		emit collapsed(index);
-//	}
-//}
+void QtTreeWidget::handleExpanded(const QModelIndex& index) {
+	GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
+	if (item) {
+		item->setExpanded(true);
+	}
+}
+
+void QtTreeWidget::handleCollapsed(const QModelIndex& index) {
+	GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
+	if (item) {
+		item->setExpanded(false);
+	}
+}
+
+void QtTreeWidget::handleModelItemExpanded(const QModelIndex& index, bool shouldExpand) {
+	if (this->isExpanded(index) == shouldExpand) {
+		return;
+	}
+	//setExpanded(index, shouldExpand);
+	if (shouldExpand) {
+		expand(index);
+		emit expanded(index);
+	} else {
+		collapse(index);
+		emit collapsed(index);
+	}
+}
 
 // void QtTreeWidget::handleDataChanged(const QModelIndex& topLeft, const QModelIndex& /*bottomRight*/) {
 // 	//in our model, this is only thrown with topLeft == bottomRight
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index c03f2e2..b45701e 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -28,9 +28,9 @@ class QtTreeWidget : public QTreeView{
 		void setRosterModel(Roster* roster);
 	private slots:
 		void handleItemActivated(const QModelIndex&);
-//		void handleModelItemExpanded(const QModelIndex&, bool expanded);
-//		void handleExpanded(const QModelIndex&);
-//		void handleCollapsed(const QModelIndex&);
+		void handleModelItemExpanded(const QModelIndex&, bool expanded);
+		void handleExpanded(const QModelIndex&);
+		void handleCollapsed(const QModelIndex&);
 //		void handleDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
 	protected:
 		void contextMenuEvent(QContextMenuEvent* event);
diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp
index d5b4002..00a032d 100644
--- a/Swift/QtUI/Roster/RosterModel.cpp
+++ b/Swift/QtUI/Roster/RosterModel.cpp
@@ -38,10 +38,15 @@ void RosterModel::setRoster(Roster* roster) {
 }
 
 void RosterModel::handleGroupAdded(GroupRosterItem* group) {
-	view_->setExpanded(index(group), true);
+	emit itemExpanded(index(group), group->isExpanded());
 }
 
-void RosterModel::handleChildrenChanged(GroupRosterItem* /*group*/) {
+void RosterModel::handleChildrenChanged(GroupRosterItem* group) {
+	foreach (RosterItem* item, group->getDisplayedChildren()) {
+		GroupRosterItem* child = dynamic_cast<GroupRosterItem*>(item);
+		if (!child) continue;
+		emit itemExpanded(index(child), child->isExpanded());
+	}
 	emit layoutChanged();
 }							  
 
@@ -49,7 +54,10 @@ void RosterModel::handleDataChanged(RosterItem* item) {
 	Q_ASSERT(item);
 	QModelIndex modelIndex = index(item);
 	if (modelIndex.isValid()) {
-		//emit itemExpanded(modelIndex, item->isExpanded());
+		GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
+		if (group) {
+			emit itemExpanded(modelIndex, group->isExpanded());
+		}
 		emit dataChanged(modelIndex, modelIndex);
 	}
 }
diff --git a/Swift/QtUI/Roster/RosterModel.h b/Swift/QtUI/Roster/RosterModel.h
index 17fdd3e..10aeccd 100644
--- a/Swift/QtUI/Roster/RosterModel.h
+++ b/Swift/QtUI/Roster/RosterModel.h
@@ -34,6 +34,8 @@ namespace Swift {
 			QModelIndex index(RosterItem* item) const;
 			QModelIndex parent(const QModelIndex& index) const;
 			int rowCount(const QModelIndex& parent = QModelIndex()) const;
+		signals:
+			void itemExpanded(const QModelIndex& item, bool expanded);
 		private:
 			void handleDataChanged(RosterItem* item);
 			void handleChildrenChanged(GroupRosterItem* item);
diff --git a/Swiften/Roster/GroupRosterItem.cpp b/Swiften/Roster/GroupRosterItem.cpp
index 2632444..7ce57d2 100644
--- a/Swiften/Roster/GroupRosterItem.cpp
+++ b/Swiften/Roster/GroupRosterItem.cpp
@@ -13,13 +13,21 @@
 namespace Swift {
 
 GroupRosterItem::GroupRosterItem(const String& name, GroupRosterItem* parent) : RosterItem(name, parent) {
-
+	expanded_ = true;
 }
 
 GroupRosterItem::~GroupRosterItem() {
 
 }
 
+bool GroupRosterItem::isExpanded() const {
+	return expanded_;
+}
+
+void GroupRosterItem::setExpanded(bool expanded) {
+	expanded_ = expanded;
+}
+
 const std::vector<RosterItem*>& GroupRosterItem::getChildren() const {
 	return children_;
 }
diff --git a/Swiften/Roster/GroupRosterItem.h b/Swiften/Roster/GroupRosterItem.h
index 0d230e2..aca2b05 100644
--- a/Swiften/Roster/GroupRosterItem.h
+++ b/Swiften/Roster/GroupRosterItem.h
@@ -25,11 +25,14 @@ class GroupRosterItem : public RosterItem {
 		void setDisplayed(RosterItem* item, bool displayed);
 		boost::signal<void ()> onChildrenChanged;
 		static bool itemLessThan(const RosterItem* left, const RosterItem* right);
+		void setExpanded(bool expanded);
+		bool isExpanded() const;
 	private:
 		void handleChildrenChanged(GroupRosterItem* group);
 		void handleDataChanged(RosterItem* item);
 		bool sortDisplayed();
 		String name_;
+		bool expanded_;
 		std::vector<RosterItem*> children_;
 		std::vector<RosterItem*> displayedChildren_;
 };
-- 
cgit v0.10.2-6-g49f6