From 030c2d8a448dbf17808b840df1d0379639867e99 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 29 Aug 2009 17:15:27 +0100
Subject: Show contact count on collapsed roster groups


diff --git a/Swift/QtUI/QtRosterHeader.cpp b/Swift/QtUI/QtRosterHeader.cpp
index 425b22d..2a01868 100644
--- a/Swift/QtUI/QtRosterHeader.cpp
+++ b/Swift/QtUI/QtRosterHeader.cpp
@@ -99,6 +99,9 @@ void QtRosterHeader::resizeNameLabel() {
 	QString escapedName = name_;
 	escapedName.replace("<","&lt;");
 	nameLabel_->setText("<b>" + escapedName + "</b>");
+	return;
+	//FIXME: Make this not an infinite loop, so it can be continued.
+	
 	int reductionCount = 0;
 	while (nameLabel_->sizeHint().width() + statusWidget_->width() + 30 > width()) {
 		//qDebug() << nameLabel_->sizeHint().width() << " " << statusWidget_->width() << " " << width();
diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index b678e0c..75863fc 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -3,13 +3,15 @@
 #include "Swiften/Base/Platform.h"
 #include "Swiften/Roster/OpenChatRosterAction.h"
 
+#include <qdebug.h>
+
 namespace Swift {
 
 QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent) {
 	treeRoot_ = new QtTreeWidgetItem(NULL);
 	model_ = new RosterModel();
 	model_->setRoot(treeRoot_);
-		setModel(model_);
+	setModel(model_);
 	delegate_ = new RosterDelegate();
 	setItemDelegate(delegate_);
 	setHeaderHidden(true);
@@ -20,7 +22,10 @@ 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)));
+	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&)));
 }
 
 QtTreeWidget::~QtTreeWidget() {
@@ -39,8 +44,46 @@ void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
 	}
 }
 
-void QtTreeWidget::handleItemExpanded(const QModelIndex& index, bool expanded) {
-	setExpanded(index, expanded);
+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::handleDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) {
+	Q_UNUSED(bottomRight);
+	//in our model, this is only thrown with topLeft == bottomRight
+	if (!topLeft.isValid()) {
+		return;
+	}
+	QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(topLeft.internalPointer());
+	if (qtItem) {
+		//qDebug() << "Item changed, passing expanded state to view: " << qtItem->isExpanded();
+		setExpanded(topLeft, qtItem->isExpanded());
+	}
+
 }
 
 void QtTreeWidget::drawBranches(QPainter*, const QRect&, const QModelIndex&) const {
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index 8c76d60..13b6d8e 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -22,7 +22,10 @@ class QtTreeWidget : public QTreeView, public TreeWidget {
 		QtTreeWidgetItem* getRoot();
 	private slots:
 		void handleItemActivated(const QModelIndex&);
-		void handleItemExpanded(const QModelIndex&, bool expanded);
+		void handleModelItemExpanded(const QModelIndex&, bool expanded);
+		void handleExpanded(const QModelIndex&);
+		void handleCollapsed(const QModelIndex&);
+		void handleDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
 	private:
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
 		RosterModel* model_;
diff --git a/Swift/QtUI/Roster/RosterDelegate.cpp b/Swift/QtUI/Roster/RosterDelegate.cpp
index e50052b..c14f170 100644
--- a/Swift/QtUI/Roster/RosterDelegate.cpp
+++ b/Swift/QtUI/Roster/RosterDelegate.cpp
@@ -78,50 +78,71 @@ void RosterDelegate::paintGroup(QPainter* painter, const QStyleOptionViewItem& o
 	painter->fillPath(fillPath, backgroundBrush);
 	painter->drawPath(linePath);
 	
-	QBrush triangleBrush(QColor(110, 110, 110));
-	QBrush triangleShadowBrush(QColor(47, 47, 47));
 	double triangleWidth = 9;
 	double triangleHeight = 5;
 	QtTreeWidgetItem* item = index.isValid() ? static_cast<QtTreeWidgetItem*>(index.internalPointer()) : NULL;
 	if (item) {
-		QPainterPath trianglePath;
-		QPainterPath triangleShadowPath;
-		QPolygonF triangle;
-		QPolygonF triangleShadow;
-		QPointF triangleTopLeft(region.left() + horizontalMargin_ + 1, region.top() + region.height() / 2 - triangleHeight / 2);
-		QPointF shadowOffset(0,-1);
-		QPointF trianglePoint2;
-		QPointF trianglePoint3;
-		
-		if (item->isExpanded()) {
-			triangleTopLeft += QPoint(0, 1);
-			trianglePoint2 = triangleTopLeft + QPointF(triangleWidth, 0);
-			trianglePoint3 = trianglePoint2 + QPointF(-1 * (triangleWidth / 2), triangleHeight);
-			//qDebug() << "Plotting expanded" << triangleTopLeft << ", " << trianglePoint2 << ", " << trianglePoint3;
-		} else {
-			trianglePoint2 = triangleTopLeft + QPointF(0, triangleWidth);
-			trianglePoint3 = trianglePoint2 + QPointF(triangleHeight, -1 * (triangleWidth / 2));
-			//qDebug() << "Plotting collapsed" << triangleTopLeft << ", " << trianglePoint2 << ", " << trianglePoint3;
-		}
-		triangle << triangleTopLeft << trianglePoint2 << trianglePoint3 << triangleTopLeft;
-		triangleShadow << triangleTopLeft + shadowOffset << trianglePoint2 + shadowOffset << trianglePoint3 + shadowOffset << triangleTopLeft + shadowOffset;
-		
-		trianglePath.addPolygon(triangle);
-		triangleShadowPath.addPolygon(triangleShadow);
-		painter->fillPath(triangleShadowPath, triangleShadowBrush);
-		painter->fillPath(trianglePath, triangleBrush);
+		paintExpansionTriangle(painter, region.adjusted(horizontalMargin_ +  1, 0, 0, 0), triangleWidth, triangleHeight, item->isExpanded());
 	}
 	
-	int textLeftOffset = 2 * horizontalMargin_ + 1 + triangleWidth;
+	int textLeftOffset = 3 * horizontalMargin_ + 1 + triangleWidth;
 	QFontMetrics fontMetrics(groupFont_);
 	int textTopOffset = (option.rect.height() - fontMetrics.height()) / 2;
-	QRect textRect = region.adjusted(textLeftOffset, textTopOffset, -1 * textLeftOffset, -1 * textTopOffset);
 	painter->setFont(groupFont_);
+	int contactCountWidth = 0;
+	QRect textRect = region.adjusted(textLeftOffset, textTopOffset, -1 * textLeftOffset, -1 * textTopOffset);
+
+	if (!item->isExpanded()) {
+		QFontMetrics groupMetrics(groupFont_);
+		int contactCount = item->rowCount();
+		QString countString = QString("%1").arg(contactCount);
+		contactCountWidth = groupMetrics.width(countString) + 2 * horizontalMargin_;
+		int offsetAmount = textRect.width() - contactCountWidth + horizontalMargin_;
+		QRect countRect = textRect.adjusted(offsetAmount, 0, 0/*-1 * offsetAmount*/, 0);
+		qDebug() << "Painting count string " << countString << " at " << countRect << " from offset " << offsetAmount;
+		paintShadowText(painter, countRect, countString);
+	}
+	QRect nameTextRect = textRect.adjusted(0, 0, contactCountWidth, 0);
+	paintShadowText(painter, nameTextRect, index.data(Qt::DisplayRole).toString());
+	painter->restore();
+}
+
+void RosterDelegate::paintExpansionTriangle(QPainter* painter, const QRect& region, int width, int height, bool expanded) const {
+	QBrush triangleBrush(QColor(110, 110, 110));
+	QBrush triangleShadowBrush(QColor(47, 47, 47));
+	QPainterPath trianglePath;
+	QPainterPath triangleShadowPath;
+	QPolygonF triangle;
+	QPolygonF triangleShadow;
+	QPointF triangleTopLeft(region.left(), region.top() + region.height() / 2 - height / 2);
+	QPointF shadowOffset(0,-1);
+	QPointF trianglePoint2;
+	QPointF trianglePoint3;
+	
+	if (expanded) {
+		triangleTopLeft += QPoint(0, 1);
+		trianglePoint2 = triangleTopLeft + QPointF(width, 0);
+		trianglePoint3 = trianglePoint2 + QPointF(-1 * (width / 2), height);
+		//qDebug() << "Plotting expanded" << triangleTopLeft << ", " << trianglePoint2 << ", " << trianglePoint3;
+	} else {
+		trianglePoint2 = triangleTopLeft + QPointF(0, width);
+		trianglePoint3 = trianglePoint2 + QPointF(height, -1 * (width / 2));
+		//qDebug() << "Plotting collapsed" << triangleTopLeft << ", " << trianglePoint2 << ", " << trianglePoint3;
+	}
+	triangle << triangleTopLeft << trianglePoint2 << trianglePoint3 << triangleTopLeft;
+	triangleShadow << triangleTopLeft + shadowOffset << trianglePoint2 + shadowOffset << trianglePoint3 + shadowOffset << triangleTopLeft + shadowOffset;
+	
+	trianglePath.addPolygon(triangle);
+	triangleShadowPath.addPolygon(triangleShadow);
+	painter->fillPath(triangleShadowPath, triangleShadowBrush);
+	painter->fillPath(trianglePath, triangleBrush);
+}
+
+void RosterDelegate::paintShadowText(QPainter* painter, const QRect& region, const QString& text) const {
 	painter->setPen(QPen(QColor(254, 254, 254)));
-	painter->drawText(textRect.adjusted(0, 1, 0, 0), Qt::AlignTop, index.data(Qt::DisplayRole).toString());
+	painter->drawText(region.adjusted(0, 1, 0, 0), Qt::AlignTop, text);
 	painter->setPen(QPen(QColor(115, 115, 115)));
-	painter->drawText(textRect, Qt::AlignTop, index.data(Qt::DisplayRole).toString());
-	painter->restore();
+	painter->drawText(region, Qt::AlignTop, text);
 }
 
 void RosterDelegate::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
diff --git a/Swift/QtUI/Roster/RosterDelegate.h b/Swift/QtUI/Roster/RosterDelegate.h
index 712af81..c96e2a8 100644
--- a/Swift/QtUI/Roster/RosterDelegate.h
+++ b/Swift/QtUI/Roster/RosterDelegate.h
@@ -16,6 +16,8 @@ namespace Swift {
 		QSize contactSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
 		void paintGroup(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; 
 		void paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; 
+		void paintShadowText(QPainter* painter, const QRect& region, const QString& text) const;
+		void paintExpansionTriangle(QPainter* painter, const QRect& region, int width, int height, bool expanded) const;
 		QFont nameFont_;
 		QFont statusFont_;
 		QFont groupFont_;
-- 
cgit v0.10.2-6-g49f6