From 3d6db73e834b745f2abadd04f8309d96f2b41b9c Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 26 Jul 2009 17:31:58 +0100
Subject: The new roster now renders (badly) the contents.


diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index d40169f..d37bb2a 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -5,10 +5,13 @@
 
 namespace Swift {
 
-QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent), RosterItem(NULL) {
-	model_ = new RosterModel(this);
+QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent) {
+	treeRoot_ = new QtTreeWidgetItem(NULL);
+	model_ = new RosterModel();
+	model_->setRoot(treeRoot_);
     setModel(model_);
-    //setWindowTitle("A roster");
+	//FIXME: just a dummy title.
+    setWindowTitle("A roster");
     //show();
 	setHeaderHidden(true);
 #ifdef SWIFT_PLATFORM_MACOSX
@@ -17,7 +20,15 @@ QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent), RosterItem(NULL
 	setAnimated(true);
 	setIndentation(0);
 	setRootIsDecorated(true);
-	connect(this, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(handleItemActivated(QTreeWidgetItem*, int)));
+	//connect(this, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(handleItemActivated(QTreeWidgetItem*, int)));
+}
+
+QtTreeWidget::~QtTreeWidget() {
+	delete model_;
+}
+
+QtTreeWidgetItem* QtTreeWidget::getRoot() {
+	return treeRoot_;
 }
 
 // void QtTreeWidget::handleItemActivated(QTreeWidgetItem* item, int column) {
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index f246e83..559f4e2 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -13,17 +13,19 @@
 
 namespace Swift {
 
-class QtTreeWidget : public QTreeView, public TreeWidget, public RosterItem {
+class QtTreeWidget : public QTreeView, public TreeWidget {
 	Q_OBJECT
 	public:
 		QtTreeWidget(QWidget* parent = 0);
+		~QtTreeWidget();
 		void show();
+		QtTreeWidgetItem* getRoot();
 	//private slots:
 	//	void handleItemActivated(QTreeWidgetItem*, int);
-
 	private:
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
 		RosterModel* model_;
+		QtTreeWidgetItem* treeRoot_;
 		
 };
 
diff --git a/Swift/QtUI/Roster/QtTreeWidgetFactory.h b/Swift/QtUI/Roster/QtTreeWidgetFactory.h
index 353f697..2ca1e21 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetFactory.h
+++ b/Swift/QtUI/Roster/QtTreeWidgetFactory.h
@@ -27,8 +27,9 @@ class QtTreeWidgetFactory : public TreeWidgetFactory {
 		}
 
 		TreeWidgetItem* createTreeWidgetItem(TreeWidget* item) {
-			QtTreeWidget* qtItem = dynamic_cast<QtTreeWidget*>(item);
-			assert(qtItem);
+			QtTreeWidget* treeItem = dynamic_cast<QtTreeWidget*>(item);
+			assert(treeItem);
+			QtTreeWidgetItem* qtItem = treeItem->getRoot();
 			QtTreeWidgetItem* newItem = new QtTreeWidgetItem(qtItem);
 			//qtItem->setItemWidget(newItem, 0, newItem->getCollapsedRosterWidget());
 			qtItem->addChild(newItem);
diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.h b/Swift/QtUI/Roster/QtTreeWidgetItem.h
index c35a840..2a72a83 100644
--- a/Swift/QtUI/Roster/QtTreeWidgetItem.h
+++ b/Swift/QtUI/Roster/QtTreeWidgetItem.h
@@ -23,8 +23,7 @@ class QtTreeWidgetItem : public TreeWidgetItem, public RosterItem {
 		}
 
 		void setText(const String& text) {
-			displayName_ = P2QSTRING(text);
-			// QTreeWidgetItem::setText(0, P2QSTRING(text));
+			setName(P2QSTRING(text));
 		}
 
 		void setTextColor(unsigned long color) {
diff --git a/Swift/QtUI/Roster/Roster.pro b/Swift/QtUI/Roster/Roster.pro
index c829d5c..086ef41 100644
--- a/Swift/QtUI/Roster/Roster.pro
+++ b/Swift/QtUI/Roster/Roster.pro
@@ -9,4 +9,8 @@ DEFINES += BOOST_SIGNALS_NAMESPACE=bsignals BOOST_ALL_NO_LIB
 exists(../config.pri) {
 	LIBS += ../../Controllers/Controllers.a ../../../Swiften/Swiften.a
 	include(../config.pri)
+}
+
+mac {
+	DEFINES += SWIFT_PLATFORM_MACOSX
 }
\ No newline at end of file
diff --git a/Swift/QtUI/Roster/RosterItem.cpp b/Swift/QtUI/Roster/RosterItem.cpp
index 14b4525..b39a47c 100644
--- a/Swift/QtUI/Roster/RosterItem.cpp
+++ b/Swift/QtUI/Roster/RosterItem.cpp
@@ -1,28 +1,57 @@
 #include "RosterItem.h"
 
+#include <qdebug.h>
+
 namespace Swift {
 	
-RosterItem::RosterItem(RosterItem* parent) {
+RosterItem::RosterItem(RosterItem* parent) : QObject() {
 	parent_ = parent;
 }
 
+RosterItem::~RosterItem() {
+	qDeleteAll(children_);
+}
+
 RosterItem* RosterItem::getParentItem() {
 	return parent_;
 }
 
 void RosterItem::addChild(RosterItem* child) {
+	printf("Boing\n");
 	children_.append(child);
+	connect(child, SIGNAL(changed()), this, SIGNAL(changed()));
+	emit changed();
 }
 
 int RosterItem::rowCount() {
+	qDebug() << "Returning size of " << children_.size() << " for item " << name_;
 	return children_.size();
 }
 
 int RosterItem::rowOf(RosterItem* item) {
 	return children_.indexOf(item);;
 }
-	
+
+int RosterItem::row() {
+	return parent_ ? parent_->rowOf(this) : 0;
+}
+
 RosterItem* RosterItem::getItem(int row) {
+	qDebug() << "Returning row " << row << " from item " << name_;
 	return children_[row];
 }
+
+QVariant RosterItem::data(int role) {
+	if (role != Qt::DisplayRole) {
+		return QVariant();
+	}
+	qDebug() << "Returning name " << name_ << " for role " << role;
+	return name_;
+}
+
+void RosterItem::setName(QString name) {
+	name_ = name;
+	qDebug() << "Name changed to " << name;
+	changed();
+}
 }
\ No newline at end of file
diff --git a/Swift/QtUI/Roster/RosterItem.h b/Swift/QtUI/Roster/RosterItem.h
index f7cd804..e0041d2 100644
--- a/Swift/QtUI/Roster/RosterItem.h
+++ b/Swift/QtUI/Roster/RosterItem.h
@@ -1,9 +1,10 @@
 #pragma once
 
 #include <QList>
-
+#include <QVariant>
 namespace Swift {
-	class RosterItem {
+	class RosterItem : public QObject {
+	Q_OBJECT
 	public:
 		RosterItem(RosterItem* parent);
 		~RosterItem();
@@ -11,10 +12,16 @@ namespace Swift {
 		RosterItem* getParentItem();
 		int rowCount();
 		int rowOf(RosterItem* item);
+		int row();
 		RosterItem* getItem(int row);
+		QVariant data(int role);
+		void setName(QString name);
+	signals:
+		void changed();
 	private:
 		QList<RosterItem*> children_;
 		RosterItem* parent_;
+		QString name_;
 	};
 	
 }
\ No newline at end of file
diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp
index 2c9a2cc..4c5954e 100644
--- a/Swift/QtUI/Roster/RosterModel.cpp
+++ b/Swift/QtUI/Roster/RosterModel.cpp
@@ -2,12 +2,25 @@
 
 namespace Swift {
 
-RosterModel::RosterModel(RosterItem* tree) {
-	tree_ = tree;
+RosterModel::RosterModel() {
 }
 
 RosterModel::~RosterModel() {
-	
+	delete tree_;
+}
+
+void RosterModel::setRoot(RosterItem* root) {
+	tree_ = root;
+	connect(tree_, SIGNAL(changed()), this, SLOT(handleItemChanged()));
+}
+							  
+
+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");
+	reset();
+	emit layoutChanged();
 }
 
 int RosterModel::columnCount(const QModelIndex& parent) const {
@@ -16,10 +29,8 @@ int RosterModel::columnCount(const QModelIndex& parent) const {
 }
 
 QVariant RosterModel::data(const QModelIndex& index, int role) const {
-	if (!index.isValid()) {
-		return QVariant();
-	}
-	return QVariant("bob");
+	RosterItem* item = index.isValid() ? static_cast<RosterItem*>(index.internalPointer()) : NULL;
+	return item ? item->data(role) : QVariant();
 }
 
 QModelIndex RosterModel::index(int row, int column, const QModelIndex& parent) const {
@@ -38,16 +49,12 @@ QModelIndex RosterModel::parent(const QModelIndex& index) const {
 	Q_ASSERT(item);
 
 	RosterItem* parentItem = item->getParentItem();
-	return parentItem == tree_ ? QModelIndex() : createIndex(parentItem->getParentItem()->rowOf(parentItem), 0, parentItem);
+	return parentItem == tree_ ? QModelIndex() : createIndex(parentItem->row(), 0, parentItem);
 
 }
 
 int RosterModel::rowCount(const QModelIndex& parent) const {
-	if (!parent.isValid()) {
-		return 0;
-	}
-	
-	RosterItem* item = static_cast<RosterItem*>(parent.internalPointer());
+	RosterItem* item = parent.isValid() ? static_cast<RosterItem*>(parent.internalPointer()) : tree_;
 	Q_ASSERT(item);
 	
 	return item->rowCount();
diff --git a/Swift/QtUI/Roster/RosterModel.h b/Swift/QtUI/Roster/RosterModel.h
index a958f0d..fbede32 100644
--- a/Swift/QtUI/Roster/RosterModel.h
+++ b/Swift/QtUI/Roster/RosterModel.h
@@ -7,14 +7,18 @@
 
 namespace Swift {
 class RosterModel : public QAbstractItemModel {
+Q_OBJECT
 public:
-	RosterModel(RosterItem* tree);
+	RosterModel();
 	~RosterModel();
+	void setRoot(RosterItem* tree);
 	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 parent(const QModelIndex& index) const;
 	int rowCount(const QModelIndex& parent = QModelIndex()) const;
+private slots:
+	void handleItemChanged();
 private:
 	RosterItem* tree_;
 };
-- 
cgit v0.10.2-6-g49f6