From 6d64d22bdc18a8b4d3ef1ae7087f657de6c9abd9 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 16 Aug 2009 14:13:20 +0100
Subject: MeView collapsing.

Qt's peculiarities with which mouse events get passed where are evident here: clicking the toolbar collapses/expands, while if you're clicking the label, it has to be a doubleclick to collapse or expand.

diff --git a/Swift/QtUI/QtRosterHeader.cpp b/Swift/QtUI/QtRosterHeader.cpp
index 86e64ca..9b5410b 100644
--- a/Swift/QtUI/QtRosterHeader.cpp
+++ b/Swift/QtUI/QtRosterHeader.cpp
@@ -5,6 +5,7 @@
 #include <QIcon>
 #include <QSizePolicy>
 #include <qdebug.h>
+#include <QMouseEvent>
 
 #include "QtStatusWidget.h"
 
@@ -20,7 +21,7 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 
 	statusWidget_ = new QtStatusWidget(this);
 	toolBar_->addWidget(statusWidget_);
-	statusWidget_->resize(50, statusWidget_->height());
+	statusWidget_->setMaximumWidth(60);
 	connect(statusWidget_, SIGNAL(onChangeStatusRequest(StatusShow::Type)), this, SLOT(handleChangeStatusRequest(StatusShow::Type)));
 
 	nameLabel_ = new QLabel(this);
@@ -28,26 +29,54 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
 	toolBar_->addWidget(nameLabel_);
 	//nameLabel_->setMaximumWidth(width() - 5 - statusWidget_->width());
 		
-	QHBoxLayout* expandedLayout = new QHBoxLayout();
-	expandedLayout->setContentsMargins(5,5,5,5);
-	expandedLayout->setSpacing(11);
+	expandedLayout_ = new QHBoxLayout();
+	expandedLayout_->setContentsMargins(0,0,0,0);
+	expandedLayout_->setSpacing(0);
 	
 	avatarLabel_ = new QLabel(this);
 	setAvatar(":/icons/avatar.png");
-	expandedLayout->addWidget(avatarLabel_);
+	expandedLayout_->addWidget(avatarLabel_);
 	
 	statusEdit_ = new QTextEdit(this);
-	expandedLayout->addWidget(statusEdit_);
+	expandedLayout_->addWidget(statusEdit_);
 	statusEdit_->resize(statusEdit_->width(), 64);
 	statusEdit_->setAcceptRichText(false);
 	statusEdit_->setReadOnly(false);
 	setStatusText("");
 
-	vLayout->addLayout(expandedLayout);
+	vLayout->addLayout(expandedLayout_);
+	expanded_ = false;
+	avatarLabel_->hide();
+	statusEdit_->hide();
 
 	setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
 }
 
+void QtRosterHeader::mousePressEvent(QMouseEvent* event) {
+	if (nameLabel_->underMouse() || toolBar_->underMouse() && !statusWidget_->underMouse()) {
+		toggleExpanded();
+		event->accept();
+	} else {
+		event->ignore();
+	}
+	
+}
+
+void QtRosterHeader::toggleExpanded() {
+	expanded_ = !expanded_;
+	if (expanded_) {
+		expandedLayout_->setContentsMargins(5,5,5,5);
+		expandedLayout_->setSpacing(11);
+		avatarLabel_->show();
+		statusEdit_->show();
+	} else {
+		expandedLayout_->setContentsMargins(0,0,0,0);
+		expandedLayout_->setSpacing(0);
+		avatarLabel_->hide();
+		statusEdit_->hide();
+	}
+}
+
 void QtRosterHeader::handleChangeStatusRequest(StatusShow::Type type) {
 	Q_UNUSED(type);
 	emitStatus();
diff --git a/Swift/QtUI/QtRosterHeader.h b/Swift/QtUI/QtRosterHeader.h
index ae510ee..66c9333 100644
--- a/Swift/QtUI/QtRosterHeader.h
+++ b/Swift/QtUI/QtRosterHeader.h
@@ -10,6 +10,8 @@
 #include "Swiften/Base/String.h"
 #include "Swiften/Elements/StatusShow.h"
 
+class QHBoxLayout;
+
 namespace Swift {
 	class QtStatusWidget;
 	
@@ -28,13 +30,17 @@ namespace Swift {
 		void handleChangeStatusRequest(StatusShow::Type type);
 	protected:
 		virtual void resizeEvent(QResizeEvent* event);
+		virtual void mousePressEvent(QMouseEvent* event);
 	private:
 		void resizeNameLabel();
+		void toggleExpanded();
 		QString name_;
 		QLabel* avatarLabel_;
 		QLabel* nameLabel_;
 		QTextEdit* statusEdit_;
 		QToolBar* toolBar_;
 		QtStatusWidget* statusWidget_;
+		QHBoxLayout* expandedLayout_;
+		bool expanded_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6