From 47d75bf5a3f58ce582e5715a9d5ba6e89a38b033 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 8 Aug 2009 15:18:43 +0100
Subject: Plumbing in place for roster group expansion. \nDoesn't work.


diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index 67afd14..18b28d2 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -20,6 +20,7 @@ QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent) {
 	setIndentation(0);
 	setRootIsDecorated(true);
 	connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&)));
+	connect(model_, SIGNAL(itemExpanded(const QModelIndex&, bool)), this, SLOT(handleItemExpanded(const QModelIndex&, bool)));
 }
 
 QtTreeWidget::~QtTreeWidget() {
@@ -38,6 +39,10 @@ void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
 	}
 }
 
+void QtTreeWidget::handleItemExpanded(const QModelIndex& index, bool expanded) {
+	setExpanded(index, expanded);
+}
+
 void QtTreeWidget::drawBranches(QPainter*, const QRect&, const QModelIndex&) const {
 }
 
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index bb8f618..8c76d60 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -22,6 +22,7 @@ class QtTreeWidget : public QTreeView, public TreeWidget {
 		QtTreeWidgetItem* getRoot();
 	private slots:
 		void handleItemActivated(const QModelIndex&);
+		void handleItemExpanded(const QModelIndex&, bool expanded);
 	private:
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
 		RosterModel* model_;
diff --git a/Swift/QtUI/Roster/QtTreeWidgetFactory.h b/Swift/QtUI/Roster/QtTreeWidgetFactory.h
index 2ca1e21..db20044 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetFactory.h
+++ b/Swift/QtUI/Roster/QtTreeWidgetFactory.h
@@ -33,6 +33,7 @@ class QtTreeWidgetFactory : public TreeWidgetFactory {
 			QtTreeWidgetItem* newItem = new QtTreeWidgetItem(qtItem);
 			//qtItem->setItemWidget(newItem, 0, newItem->getCollapsedRosterWidget());
 			qtItem->addChild(newItem);
+			newItem->setExpanded(true);
 			return newItem;
 		}
 };
diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp b/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
index aa1836c..1b77b26 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidgetItem.cpp
@@ -8,6 +8,7 @@ namespace Swift {
 QtTreeWidgetItem::QtTreeWidgetItem(QtTreeWidgetItem* parentItem) : QObject(), textColor_(0,0,0), backgroundColor_(255,255,255) {
 	parent_ = parentItem;
 	shown_ = true;
+	expanded_ = true;
 }
 
 void QtTreeWidgetItem::setText(const String& text) {
@@ -38,17 +39,18 @@ void QtTreeWidgetItem::setBackgroundColor(unsigned long color) {
 }
 
 void QtTreeWidgetItem::setExpanded(bool b) {
-	//treeWidget()->setItemExpanded(this, b);
+	expanded_ = true;
+	emit changed(this);
 }
 
 void QtTreeWidgetItem::hide() {
 	shown_ = false;
-	emit changed();
+	emit changed(this);
 }
 
 void QtTreeWidgetItem::show() {
 	shown_ = true;
-	emit changed();
+	emit changed(this);
 }
 
 bool QtTreeWidgetItem::isShown() {
@@ -77,18 +79,18 @@ QtTreeWidgetItem* QtTreeWidgetItem::getParentItem() {
 
 void QtTreeWidgetItem::addChild(QtTreeWidgetItem* child) {
 	children_.append(child);
-	connect(child, SIGNAL(changed()), this, SLOT(handleChanged()));
-	handleChanged();
+	connect(child, SIGNAL(changed(QtTreeWidgetItem*)), this, SLOT(handleChanged(QtTreeWidgetItem*)));
+	handleChanged(child);
 }
 
-void QtTreeWidgetItem::handleChanged() {
+void QtTreeWidgetItem::handleChanged(QtTreeWidgetItem* child) {
 	shownChildren_.clear();
 	for (int i = 0; i < children_.size(); i++) {
 		if (children_[i]->isShown()) {
 			shownChildren_.append(children_[i]);
 		}
 	}
-	emit changed();
+	emit changed(child);
 }
 
 int QtTreeWidgetItem::rowCount() {
@@ -106,6 +108,8 @@ int QtTreeWidgetItem::row() {
 
 QtTreeWidgetItem* QtTreeWidgetItem::getItem(int row) {
 	//qDebug() << "Returning row " << row << " from item " << displayName_;
+	Q_ASSERT(row >= 0);
+	Q_ASSERT(row < rowCount());
 	return shownChildren_[row];
 }
 
@@ -125,4 +129,8 @@ bool QtTreeWidgetItem::isContact() {
 	return children_.size() == 0;
 }
 
+bool QtTreeWidgetItem::isExpanded() {
+	return expanded_;
+}
+
 }
\ No newline at end of file
diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.h b/Swift/QtUI/Roster/QtTreeWidgetItem.h
index 0a32940..73da92f 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetItem.h
+++ b/Swift/QtUI/Roster/QtTreeWidgetItem.h
@@ -43,14 +43,15 @@ class QtTreeWidgetItem : public QObject, public TreeWidgetItem {
 			void show();
 			bool isShown();
 			bool isContact();
+			bool isExpanded();
 
 			QWidget* getCollapsedRosterWidget();
 			QWidget* getExpandedRosterWidget();
 		
 		signals:
-			void changed();
+			void changed(QtTreeWidgetItem*);
 		private slots:
-			void handleChanged();
+			void handleChanged(QtTreeWidgetItem* item);
 		private:
 			QList<QtTreeWidgetItem*> children_;
 			QList<QtTreeWidgetItem*> shownChildren_;
@@ -61,6 +62,7 @@ class QtTreeWidgetItem : public QObject, public TreeWidgetItem {
 			QColor backgroundColor_;
 			QVariant avatar_;
 			bool shown_;
+			bool expanded_;
 };
 
 }
diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp
index f322792..30d8135 100644
--- a/Swift/QtUI/Roster/RosterModel.cpp
+++ b/Swift/QtUI/Roster/RosterModel.cpp
@@ -11,16 +11,23 @@ RosterModel::~RosterModel() {
 
 void RosterModel::setRoot(QtTreeWidgetItem* root) {
 	tree_ = root;
-	connect(tree_, SIGNAL(changed()), this, SLOT(handleItemChanged()));
+	connect(tree_, SIGNAL(changed(QtTreeWidgetItem*)), this, SLOT(handleItemChanged(QtTreeWidgetItem*)));
 }
 							  
 
-void RosterModel::handleItemChanged() {
-	//FIXME: This is just a lazy hack to cause the view to refresh until it works.
-	// Then I'll replace it with the proper implementation.
-	//printf("Changed\n");
+void RosterModel::handleItemChanged(QtTreeWidgetItem* item) {
+	if (!item->isShown()) {
+		return;
+	}
+	//these two lines should be redundant, but...
 	reset();
 	emit layoutChanged();
+	//These lines don't seem to be enough
+	Q_ASSERT(item);
+	QModelIndex modelIndex = index(item);
+	emit itemExpanded(modelIndex, item->isExpanded());
+	emit dataChanged(modelIndex, modelIndex);
+	
 }
 
 int RosterModel::columnCount(const QModelIndex& parent) const {
@@ -40,6 +47,19 @@ QModelIndex RosterModel::index(int row, int column, const QModelIndex& parent) c
 	return row < parentItem->rowCount() ? createIndex(row, column, parentItem->getItem(row)) : QModelIndex();
 }
 
+QModelIndex RosterModel::index(QtTreeWidgetItem* item) const {
+	QtTreeWidgetItem* parentItem = item->getParentItem();
+	Q_ASSERT(parentItem);
+	QModelIndex parentIndex = parent(item);
+	return index(item->row(), 0, parentIndex);
+}
+
+QModelIndex RosterModel::parent(QtTreeWidgetItem* item) const {
+	QtTreeWidgetItem* parentItem = item->getParentItem();
+	return parentItem == tree_ ? QModelIndex() : index(parentItem->row(), 0, parent(parentItem));
+}
+
+
 QModelIndex RosterModel::parent(const QModelIndex& index) const {
 	if (!index.isValid()) {
 		return QModelIndex();
diff --git a/Swift/QtUI/Roster/RosterModel.h b/Swift/QtUI/Roster/RosterModel.h
index e75bc2f..c1d998c 100644
--- a/Swift/QtUI/Roster/RosterModel.h
+++ b/Swift/QtUI/Roster/RosterModel.h
@@ -15,10 +15,14 @@ public:
 	int columnCount(const QModelIndex& parent = QModelIndex()) const;
 	QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
 	QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
+	QModelIndex index(QtTreeWidgetItem* item) const;
 	QModelIndex parent(const QModelIndex& index) const;
+	QModelIndex parent(QtTreeWidgetItem* item) const;
 	int rowCount(const QModelIndex& parent = QModelIndex()) const;
+signals:
+	void itemExpanded(const QModelIndex& item, bool expanded);
 private slots:
-	void handleItemChanged();
+	void handleItemChanged(QtTreeWidgetItem* item);
 private:
 	QtTreeWidgetItem* tree_;
 };
diff --git a/Swift/QtUI/Roster/main.cpp b/Swift/QtUI/Roster/main.cpp
index 60ee592..660dce9 100644
--- a/Swift/QtUI/Roster/main.cpp
+++ b/Swift/QtUI/Roster/main.cpp
@@ -15,6 +15,7 @@ int main(int argc, char *argv[])
 
 	Swift::QtTreeWidgetFactory treeWidgetFactory;
 	Swift::QtTreeWidget* tree = dynamic_cast<Swift::QtTreeWidget*>(treeWidgetFactory.createTreeWidget());
+	tree->show();
 	for (int i = 0; i < 500; i++) {
 		Swift::QtTreeWidgetItem* group = dynamic_cast<Swift::QtTreeWidgetItem*>(treeWidgetFactory.createTreeWidgetItem(tree));
 		group->setText("People");
@@ -26,8 +27,9 @@ int main(int argc, char *argv[])
 		item1->setText("Remko");
 		item2->setText("Kevin");
 		item3->setText("Cath");
+		item4->setText("KimTypo");
 		item4->setText("Kim");
 	}
-	tree->show();
+	
     return app.exec();
 }
\ No newline at end of file
-- 
cgit v0.10.2-6-g49f6