From 26ac55fc087fb49abbdd164e125e41207e66f9fd Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 9 Jul 2011 19:54:55 +0100
Subject: Update QtChatList for recent backend changes


diff --git a/Swift/QtUI/ChatList/ChatListDelegate.cpp b/Swift/QtUI/ChatList/ChatListDelegate.cpp
index b2bfe0a..520b40e 100644
--- a/Swift/QtUI/ChatList/ChatListDelegate.cpp
+++ b/Swift/QtUI/ChatList/ChatListDelegate.cpp
@@ -30,7 +30,7 @@ QSize ChatListDelegate::sizeHint(const QStyleOptionViewItem& option, const QMode
 		return mucSizeHint(option, index);
 	}
 	else if (item && dynamic_cast<ChatListRecentItem*>(item)) {
-		return recentSizeHint(option, index);
+		return common_.contactSizeHint(option, index);
 	}
 	else if (item && dynamic_cast<ChatListGroupItem*>(item)) {
 		return groupDelegate_->sizeHint(option, index);
@@ -98,34 +98,17 @@ void ChatListDelegate::paintMUC(QPainter* painter, const QStyleOptionViewItem& o
 }
 
 void ChatListDelegate::paintRecent(QPainter* painter, const QStyleOptionViewItem& option, ChatListRecentItem* item) const {
-	painter->save();
-	QRect fullRegion(option.rect);
-	if ( option.state & QStyle::State_Selected ) {
-		painter->fillRect(fullRegion, option.palette.highlight());
-		painter->setPen(option.palette.highlightedText().color());
-	} else {
-		QColor nameColor = item->data(Qt::TextColorRole).value<QColor>();
-		painter->setPen(QPen(nameColor));
+	QColor nameColor = item->data(Qt::TextColorRole).value<QColor>();
+	QString avatarPath;
+	if (item->data(ChatListRecentItem::AvatarRole).isValid() && !item->data(ChatListRecentItem::AvatarRole).value<QString>().isNull()) {
+		QString avatarPath = item->data(ChatListRecentItem::AvatarRole).value<QString>();
 	}
-
-	QFontMetrics nameMetrics(common_.nameFont);
-	painter->setFont(common_.nameFont);
-	int extraFontWidth = nameMetrics.width("H");
-	int leftOffset = common_.horizontalMargin * 2 + extraFontWidth / 2;
-	QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0, 0));
-
-	int nameHeight = nameMetrics.height() + common_.verticalMargin;
-	QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0));
-
-	DelegateCommons::drawElidedText(painter, nameRegion, item->data(Qt::DisplayRole).toString());
-
-	painter->setFont(common_.detailFont);
-	painter->setPen(QPen(QColor(160,160,160)));
-
-	QRect detailRegion(textRegion.adjusted(0, nameHeight, 0, 0));
-	DelegateCommons::drawElidedText(painter, detailRegion, item->data(ChatListRecentItem::DetailTextRole).toString());
-
-	painter->restore();
+	QIcon presenceIcon = item->data(ChatListRecentItem::PresenceIconRole).isValid() && !item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>().isNull()
+			? item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>()
+			: QIcon(":/icons/offline.png");
+	QString name = item->data(Qt::DisplayRole).toString();
+	QString statusText = item->data(ChatListRecentItem::DetailTextRole).toString();
+	common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText);
 }
 
 }
diff --git a/Swift/QtUI/ChatList/ChatListRecentItem.cpp b/Swift/QtUI/ChatList/ChatListRecentItem.cpp
index 38f9a5e..e7c9599 100644
--- a/Swift/QtUI/ChatList/ChatListRecentItem.cpp
+++ b/Swift/QtUI/ChatList/ChatListRecentItem.cpp
@@ -24,11 +24,24 @@ QVariant ChatListRecentItem::data(int role) const {
 			/*case Qt::TextColorRole: return textColor_;
 		case Qt::BackgroundColorRole: return backgroundColor_;
 		case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant();
-		case StatusTextRole: return statusText_;
-		case AvatarRole: return avatar_;
-		case PresenceIconRole: return getPresenceIcon();*/
+		case StatusTextRole: return statusText_;*/
+		case AvatarRole: return chat_.avatarPath.string().c_str();
+		case PresenceIconRole: return getPresenceIcon();
 		default: return QVariant();
 	}
 }
 
+QIcon ChatListRecentItem::getPresenceIcon() const {
+	QString iconString;
+	switch (chat_.statusType) {
+	 	case StatusShow::Online: iconString = "online";break;
+	 	case StatusShow::Away: iconString = "away";break;
+	 	case StatusShow::XA: iconString = "away";break;
+	 	case StatusShow::FFC: iconString = "online";break;
+	 	case StatusShow::DND: iconString = "dnd";break;
+	 	case StatusShow::None: iconString = "offline";break;
+	}
+	return QIcon(":/icons/" + iconString + ".png");
+}
+
 }
diff --git a/Swift/QtUI/ChatList/ChatListRecentItem.h b/Swift/QtUI/ChatList/ChatListRecentItem.h
index f88de77..4e7bc3e 100644
--- a/Swift/QtUI/ChatList/ChatListRecentItem.h
+++ b/Swift/QtUI/ChatList/ChatListRecentItem.h
@@ -7,6 +7,7 @@
 #pragma once
 
 #include <QList>
+#include <QIcon>
 
 #include <boost/shared_ptr.hpp>
 
@@ -19,15 +20,16 @@ namespace Swift {
 	class ChatListRecentItem : public ChatListItem {
 		public:
 			enum RecentItemRoles {
-				DetailTextRole = Qt::UserRole/*,
+				DetailTextRole = Qt::UserRole,
 				AvatarRole = Qt::UserRole + 1,
-				PresenceIconRole = Qt::UserRole + 2,
+				PresenceIconRole = Qt::UserRole + 2/*,
 				StatusShowTypeRole = Qt::UserRole + 3*/
 			};
 			ChatListRecentItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent);
 			const ChatListWindow::Chat& getChat() const;
 			QVariant data(int role) const;
 		private:
+			QIcon getPresenceIcon() const;
 			ChatListWindow::Chat chat_;
 	};
 }
diff --git a/Swift/QtUI/Roster/DelegateCommons.cpp b/Swift/QtUI/Roster/DelegateCommons.cpp
index 164b80f..1e02086 100644
--- a/Swift/QtUI/Roster/DelegateCommons.cpp
+++ b/Swift/QtUI/Roster/DelegateCommons.cpp
@@ -6,6 +6,9 @@
 
 #include "DelegateCommons.h"
 
+#include <QtScaledAvatarCache.h>
+#include <QFileInfo>
+
 namespace Swift {
 
 
@@ -14,10 +17,76 @@ void DelegateCommons::drawElidedText(QPainter* painter, const QRect& region, con
 	painter->drawText(region, Qt::AlignTop, adjustedText);
 }
 
+void DelegateCommons::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QColor& nameColor, const QString& avatarPath, const QIcon& presenceIcon, const QString& name, const QString& statusText) const {
+	painter->save();
+	QRect fullRegion(option.rect);
+	if ( option.state & QStyle::State_Selected ) {
+		painter->fillRect(fullRegion, option.palette.highlight());
+		painter->setPen(option.palette.highlightedText().color());
+	} else {
+		painter->setPen(QPen(nameColor));
+	}
+
+	QRect presenceIconRegion(QPoint(farLeftMargin, fullRegion.top()), QSize(presenceIconWidth, fullRegion.height() - verticalMargin));
+
+	int calculatedAvatarSize = presenceIconRegion.height();
+	//This overlaps the presenceIcon, so must be painted first
+	QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth / 2, presenceIconRegion.top()), QSize(calculatedAvatarSize, calculatedAvatarSize));
+
+	QPixmap avatarPixmap;
+	if (!avatarPath.isEmpty()) {
+		QString scaledAvatarPath = QtScaledAvatarCache(avatarRegion.height()).getScaledAvatarPath(avatarPath);
+		if (QFileInfo(scaledAvatarPath).exists()) {
+			avatarPixmap.load(scaledAvatarPath);
+		}
+	}
+	if (avatarPixmap.isNull()) {
+		avatarPixmap = QPixmap(":/icons/avatar.png").scaled(avatarRegion.height(), avatarRegion.width(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
+	}
+
+	painter->drawPixmap(avatarRegion.topLeft() + QPoint(((avatarRegion.width() - avatarPixmap.width()) / 2), (avatarRegion.height() - avatarPixmap.height()) / 2), avatarPixmap);
+
+	//Paint the presence icon over the top of the avatar
+	presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter);
+
+	QFontMetrics nameMetrics(nameFont);
+	painter->setFont(nameFont);
+	int extraFontWidth = nameMetrics.width("H");
+	int leftOffset = avatarRegion.right() + horizontalMargin * 2 + extraFontWidth / 2;
+	QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0/*-leftOffset*/, 0));
+
+	int nameHeight = nameMetrics.height() + verticalMargin;
+	QRect nameRegion(textRegion.adjusted(0, verticalMargin, 0, 0));
+
+	DelegateCommons::drawElidedText(painter, nameRegion, name);
+
+
+	painter->setFont(detailFont);
+	painter->setPen(QPen(QColor(160,160,160)));
+
+	QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0));
+	DelegateCommons::drawElidedText(painter, statusTextRegion, statusText);
+
+	painter->restore();
+}
+
+QSize DelegateCommons::contactSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
+	int heightByAvatar = avatarSize + verticalMargin * 2;
+	QFontMetrics nameMetrics(nameFont);
+	QFontMetrics statusMetrics(detailFont);
+	int sizeByText = 2 * verticalMargin + nameMetrics.height() + statusMetrics.height();
+	//Doesn't work, yay! FIXME: why?
+	//QSize size = (option.state & QStyle::State_Selected) ? QSize(150, 80) : QSize(150, avatarSize_ + margin_ * 2);
+	//qDebug() << "Returning size" << size;
+	return QSize(150, sizeByText > heightByAvatar ? sizeByText : heightByAvatar);
+}
 
 const int DelegateCommons::horizontalMargin = 2;
 const int DelegateCommons::verticalMargin = 2;
 const int DelegateCommons::farLeftMargin = 2;
+const int DelegateCommons::avatarSize = 20;
+const int DelegateCommons::presenceIconHeight = 16;
+const int DelegateCommons::presenceIconWidth = 16;
 
 
 
diff --git a/Swift/QtUI/Roster/DelegateCommons.h b/Swift/QtUI/Roster/DelegateCommons.h
index 9213ef5..c79d64b 100644
--- a/Swift/QtUI/Roster/DelegateCommons.h
+++ b/Swift/QtUI/Roster/DelegateCommons.h
@@ -11,6 +11,8 @@
 #include <QPainter>
 #include <QRect>
 #include <QString>
+#include <QIcon>
+#include <QStyleOptionViewItem>
 
 namespace Swift {
 	class DelegateCommons {
@@ -23,11 +25,17 @@ namespace Swift {
 
 			static void drawElidedText(QPainter* painter, const QRect& region, const QString& text);
 
+			QSize contactSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
+			void paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QColor& nameColor, const QString& avatarPath, const QIcon& presenceIcon, const QString& name, const QString& statusText) const;
+
 			int detailFontSizeDrop;
 			QFont nameFont;
 			QFont detailFont;
 			static const int horizontalMargin;
 			static const int verticalMargin;
 			static const int farLeftMargin;			
+			static const int avatarSize;
+			static const int presenceIconHeight;
+			static const int presenceIconWidth;
 	};
 }
diff --git a/Swift/QtUI/Roster/RosterDelegate.cpp b/Swift/QtUI/Roster/RosterDelegate.cpp
index aaa6236..6914fc3 100644
--- a/Swift/QtUI/Roster/RosterDelegate.cpp
+++ b/Swift/QtUI/Roster/RosterDelegate.cpp
@@ -12,7 +12,6 @@
 #include <QBrush>
 #include <QFontMetrics>
 #include <QPainterPath>
-#include <QFileInfo>
 #include <QPolygon>
 #include <qdebug.h>
 #include <QBitmap>
@@ -22,7 +21,6 @@
 
 #include "QtTreeWidget.h"
 #include "RosterModel.h"
-#include "QtScaledAvatarCache.h"
 
 namespace Swift {
 
@@ -43,15 +41,8 @@ QSize RosterDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelI
 	return contactSizeHint(option, index);
 }
 
-QSize RosterDelegate::contactSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const {
-	int heightByAvatar = avatarSize_ + common_.verticalMargin * 2;
-	QFontMetrics nameMetrics(common_.nameFont);
-	QFontMetrics statusMetrics(common_.detailFont);
-	int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height();
-	//Doesn't work, yay! FIXME: why?
-	//QSize size = (option.state & QStyle::State_Selected) ? QSize(150, 80) : QSize(150, avatarSize_ + margin_ * 2);
-	//qDebug() << "Returning size" << size;
-	return QSize(150, sizeByText > heightByAvatar ? sizeByText : heightByAvatar);
+QSize RosterDelegate::contactSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const {
+	return common_.contactSizeHint(option, index);
 }
 
 void RosterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
@@ -70,70 +61,18 @@ void RosterDelegate::paintGroup(QPainter* painter, const QStyleOptionViewItem& o
 }
 
 void RosterDelegate::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
-	//qDebug() << "painting" << index.data(Qt::DisplayRole).toString();
-	painter->save();
-	//QStyledItemDelegate::paint(painter, option, index);
-	//initStyleOption(option, index);
-	QRect fullRegion(option.rect);
-	if ( option.state & QStyle::State_Selected ) {
-		painter->fillRect(fullRegion, option.palette.highlight());
-		painter->setPen(option.palette.highlightedText().color());
-	} else {
-		QColor nameColor = index.data(Qt::TextColorRole).value<QColor>();
-		painter->setPen(QPen(nameColor));
-	}
-	
-	QRect presenceIconRegion(QPoint(common_.farLeftMargin, fullRegion.top()), QSize(presenceIconWidth_, fullRegion.height() - common_.verticalMargin));
-	
-	int calculatedAvatarSize = presenceIconRegion.height();
-	//This overlaps the presenceIcon, so must be painted first
-	QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth_ / 2, presenceIconRegion.top()), QSize(calculatedAvatarSize, calculatedAvatarSize));
-
-	QPixmap avatarPixmap;
+	QColor nameColor = index.data(Qt::TextColorRole).value<QColor>();
+	QString avatarPath;
 	if (index.data(AvatarRole).isValid() && !index.data(AvatarRole).value<QString>().isNull()) {
 		QString avatarPath = index.data(AvatarRole).value<QString>();
-		QString scaledAvatarPath = QtScaledAvatarCache(avatarRegion.height()).getScaledAvatarPath(avatarPath);
-		if (QFileInfo(scaledAvatarPath).exists()) {
-			avatarPixmap.load(scaledAvatarPath);
-		}
 	}
-	if (avatarPixmap.isNull()) {
-		avatarPixmap = QPixmap(":/icons/avatar.png").scaled(avatarRegion.height(), avatarRegion.width(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
-	}
-
-	painter->drawPixmap(avatarRegion.topLeft() + QPoint(((avatarRegion.width() - avatarPixmap.width()) / 2), (avatarRegion.height() - avatarPixmap.height()) / 2), avatarPixmap);
-
-	//Paint the presence icon over the top of the avatar
 	QIcon presenceIcon = index.data(PresenceIconRole).isValid() && !index.data(PresenceIconRole).value<QIcon>().isNull()
-		? index.data(PresenceIconRole).value<QIcon>()
-		: QIcon(":/icons/offline.png");
-	presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter);
-	
-	QFontMetrics nameMetrics(common_.nameFont);
-	painter->setFont(common_.nameFont);
-	int extraFontWidth = nameMetrics.width("H");
-	int leftOffset = avatarRegion.right() + common_.horizontalMargin * 2 + extraFontWidth / 2;
-	QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0/*-leftOffset*/, 0));
-	
-	int nameHeight = nameMetrics.height() + common_.verticalMargin;
-	QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0));
-	
-	DelegateCommons::drawElidedText(painter, nameRegion, index.data(Qt::DisplayRole).toString());
-	
-	
-	painter->setFont(common_.detailFont);
-	painter->setPen(QPen(QColor(160,160,160)));
-	
-	QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0));
-	DelegateCommons::drawElidedText(painter, statusTextRegion, index.data(StatusTextRole).toString());
-	
-	painter->restore();
+			? index.data(PresenceIconRole).value<QIcon>()
+			: QIcon(":/icons/offline.png");
+	QString name = index.data(Qt::DisplayRole).toString();
+	QString statusText = index.data(StatusTextRole).toString();
+	common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText);
 }
 
-
-const int RosterDelegate::avatarSize_ = 20;
-const int RosterDelegate::presenceIconHeight_ = 16;
-const int RosterDelegate::presenceIconWidth_ = 16;
-
 }
 
diff --git a/Swift/QtUI/Roster/RosterDelegate.h b/Swift/QtUI/Roster/RosterDelegate.h
index e6a16f2..253c11a 100644
--- a/Swift/QtUI/Roster/RosterDelegate.h
+++ b/Swift/QtUI/Roster/RosterDelegate.h
@@ -28,9 +28,5 @@ namespace Swift {
 		DelegateCommons common_;
 		GroupItemDelegate* groupDelegate_;
 		QtTreeWidget* tree_;
-		static const int avatarSize_;
-		static const int presenceIconHeight_;
-		static const int presenceIconWidth_;
-
 	};
 }
-- 
cgit v0.10.2-6-g49f6