summaryrefslogtreecommitdiffstats
blob: b8463b7b3baecbd2a1479fecfbe11e8efe9c1f47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "QtRosterHeader.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QIcon>
#include <QSizePolicy>
#include <qdebug.h>
#include <QMouseEvent>

#include "QtStatusWidget.h"

namespace Swift {
QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) {
	QVBoxLayout* vLayout = new QVBoxLayout();
	vLayout->setSpacing(0);
	vLayout->setContentsMargins(0,0,0,0);
	setLayout(vLayout);

	toolBar_ = new QToolBar(this);
	vLayout->addWidget(toolBar_);

	statusWidget_ = new QtStatusWidget(this);
	toolBar_->addWidget(statusWidget_);
	statusWidget_->setMaximumWidth(60);
	connect(statusWidget_, SIGNAL(onChangeStatusRequest(StatusShow::Type)), this, SLOT(handleChangeStatusRequest(StatusShow::Type)));

	nameLabel_ = new QLabel(this);
	setName("Me");
	toolBar_->addWidget(nameLabel_);
	//nameLabel_->setMaximumWidth(width() - 5 - statusWidget_->width());
		
	expandedLayout_ = new QHBoxLayout();
	expandedLayout_->setContentsMargins(0,0,0,0);
	expandedLayout_->setSpacing(0);
	
	avatarLabel_ = new QLabel(this);
	setAvatar(":/icons/avatar.png");
	expandedLayout_->addWidget(avatarLabel_);
	
	statusEdit_ = new QTextEdit(this);
	expandedLayout_->addWidget(statusEdit_);
	statusEdit_->resize(statusEdit_->width(), 64);
	statusEdit_->setAcceptRichText(false);
	statusEdit_->setReadOnly(false);
	setStatusText("");

	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();
}

void QtRosterHeader::emitStatus() {
	emit onChangeStatusRequest(statusWidget_->getSelectedStatusShow(), statusEdit_->toPlainText());
}

void QtRosterHeader::setStatusText(const QString& statusMessage) {
	statusEdit_->setText(statusMessage);
}

void QtRosterHeader::setName(const QString& name) {
	name_ = name;
	resizeNameLabel();
}

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();
		reductionCount++;
		QString reducedName = name_;
		reducedName.remove(name_.length() - reductionCount, reductionCount);
		reducedName.replace("<","&lt;");
		nameLabel_->setText("<b>" + reducedName +  + "...</b>");
	//	qDebug() << "Shrunk " << escapedName << " down to " << reducedName;
	}
	nameLabel_->setToolTip(name_);
}

void QtRosterHeader::resizeEvent(QResizeEvent* event) {
	QWidget::resizeEvent(event);
	resizeNameLabel();
}

void QtRosterHeader::setAvatar(const QString& path) {
	avatarLabel_->setPixmap(QIcon(path).pixmap(64, 64));
}

QSize QtRosterHeader::sizeHint() const {
	return minimumSizeHint();
}

}