summaryrefslogtreecommitdiffstats
blob: 705d8af1a0468514b40c32fefe4cb78f3fb4fa2d (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "QtNameWidget.h"

#include <QHBoxLayout>
#include <QMenu>
#include <QMouseEvent>
#include <QtDebug>

#include <Swift/QtUI/QtElidingLabel.h>
#include <Swift/QtUI/QtSettingsProvider.h>

namespace Swift {

QtNameWidget::QtNameWidget(QtSettingsProvider* settings, QWidget *parent) : QWidget(parent), settings(settings) {
	QHBoxLayout* mainLayout = new QHBoxLayout(this);
	mainLayout->setSpacing(0);
	mainLayout->setContentsMargins(0,0,0,0);

	mode = settings->getBoolSetting("showNickInRosterHeader", true) ? ShowNick : ShowJID;

	textLabel = new QtElidingLabel(this);
	QFont font = textLabel->font();
	font.setBold(true);
	textLabel->setFont(font);
	mainLayout->addWidget(textLabel);
}

void QtNameWidget::setNick(const QString& nick) {
	this->nick = nick;
	updateText();
}

void QtNameWidget::setJID(const QString& jid) {
	this->jid = jid;
	updateText();
}

void QtNameWidget::mousePressEvent(QMouseEvent* event) {
	QMenu menu;

	QAction* showAsNick = new QAction("Show nickname", this);
	showAsNick->setCheckable(true);
	if (mode == ShowNick) {
		showAsNick->setChecked(true);
	}
	menu.addAction(showAsNick);

	QAction* showAsJID = new QAction("Show ID", this);
	showAsJID->setCheckable(true);
	if (mode == ShowJID) {
		showAsJID->setChecked(true);
	}
	menu.addAction(showAsJID);

	QAction* result = menu.exec(event->globalPos());
	if (result == showAsJID) {
		mode = ShowJID;
	}
	else if (result == showAsNick) {
		mode = ShowNick;
	}
	settings->storeBool("showNickInRosterHeader", mode == ShowNick);
	updateText();
}

void QtNameWidget::updateText() {
	QString text;
	if (mode == ShowNick && !nick.isEmpty()) {
		text = nick;
	}
	else {
		text = jid;
	}
	text.replace("<","&lt;");
	textLabel->setText(text);
}

}