Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TalkingUIEntry.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d9966cb06a37936ff0641c1b7b4c1947666acba (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2020-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "TalkingUIEntry.h"
#include "Channel.h"
#include "ClientUser.h"
#include "TalkingUI.h"
#include "TalkingUIContainer.h"
#include "UserModel.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QObject>
#include <QPainter>
#include <QPixmap>
#include <QVariant>
#include <QWidget>

// We define a global macro called 'g'. This can lead to issues when included code uses 'g' as a type or parameter name
// (like protobuf 3.7 does). As such, for now, we have to make this our last include.
#include "Global.h"

TalkingUIEntry::TalkingUIEntry(unsigned int associatedUserSession) : m_associatedUserSession(associatedUserSession) {
}

unsigned int TalkingUIEntry::getAssociatedUserSession() const {
	return m_associatedUserSession;
}

TalkingUIContainer *TalkingUIEntry::getContainer() {
	return m_container;
}

const TalkingUIContainer *TalkingUIEntry::getContainer() const {
	return m_container;
}

int TalkingUIEntry::compare(const TalkingUIEntry &other) const {
	if (getPriority() != other.getPriority()) {
		return static_cast< int >(getPriority()) - static_cast< int >(other.getPriority());
	}

	if (getType() != other.getType()) {
		return other.getType() > getType() ? -1 : 1;
	}

	if (other.getAssociatedUserSession() == getAssociatedUserSession()) {
		return 0;
	}

	return other.getAssociatedUserSession() > m_associatedUserSession ? -1 : 1;
}

void TalkingUIEntry::setPriority(EntryPriority priority) {
	m_priority = priority;
}

EntryPriority TalkingUIEntry::getPriority() const {
	return m_priority;
}

bool TalkingUIEntry::operator==(const TalkingUIEntry &other) const {
	return compare(other) == 0;
}

bool TalkingUIEntry::operator!=(const TalkingUIEntry &other) const {
	return compare(other) != 0;
}

bool TalkingUIEntry::operator<(const TalkingUIEntry &other) const {
	return compare(other) < 0;
}

bool TalkingUIEntry::operator<=(const TalkingUIEntry &other) const {
	return compare(other) <= 0;
}

bool TalkingUIEntry::operator>(const TalkingUIEntry &other) const {
	return compare(other) > 0;
}

bool TalkingUIEntry::operator>=(const TalkingUIEntry &other) const {
	return compare(other) >= 0;
}



static const int USER_CONTAINER_HORIZONTAL_MARGIN = 2;
static const int USER_CONTAINER_VERTICAL_MARGIN   = 3;


TalkingUIUser::TalkingUIUser(const ClientUser &user)
	: TalkingUIEntry(user.uiSession), m_backgroundWidget(new QWidget()),
	  m_backgroundWidgetStyleWrapper(m_backgroundWidget), m_name(user.qsName), m_timer() {
	// Create background widget and its layout that we'll use to place all other
	// components on
	m_backgroundWidget->setProperty("selected", false);
	QLayout *backgroundLayout = new QHBoxLayout();
	backgroundLayout->setContentsMargins(USER_CONTAINER_HORIZONTAL_MARGIN, USER_CONTAINER_VERTICAL_MARGIN,
										 USER_CONTAINER_HORIZONTAL_MARGIN, USER_CONTAINER_VERTICAL_MARGIN);
	m_backgroundWidget->setLayout(backgroundLayout);
	m_backgroundWidget->setAutoFillBackground(true);

	// Create the label we use to display the user's name
	m_nameLabel                 = new QLabel(m_backgroundWidget);
	const QString displayString = UserModel::createDisplayString(user, false, nullptr);
	setDisplayString(displayString);

	// Create the label we'll use to display the user's TalkingIcon
	m_talkingIcon = new QLabel(m_backgroundWidget);
	m_talkingIcon->setAlignment(Qt::AlignCenter);
	m_talkingIcon->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

	// Create the label we'll use to display any status icons for the user
	m_statusIcons = new QLabel(m_backgroundWidget);
	m_statusIcons->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	m_statusIcons->hide(); // hide by default
	// Uncomment the line below if you want to debug the icons
	// m_statusIcons->setStyleSheet(QLatin1String("border: 1px solid black;"));


	// By default we assume that there are no status icons to display and thus we only add
	// the label for the TalkingIcon and the name
	backgroundLayout->addWidget(m_talkingIcon);
	backgroundLayout->addWidget(m_nameLabel);

	// init size for talking icon to FontSize
	m_iconSize = QFontMetrics(m_backgroundWidget->font()).height();

	updateTalkingIcon();

	// If the timer runs out, we remove this entry from its container (if any)
	// and thereby this will cause its deletion (smart-pointer goes out of scope)
	QObject::connect(&m_timer, &QTimer::timeout, [this]() {
		if (getContainer()) {
			// We let the TalkingUI handle the removing in order for it to do the necessary
			// housekeeping (e.g. making sure there is no active selection for this user).
			getContainer()->m_talkingUI.removeUser(getAssociatedUserSession());
		}
	});
}

TalkingUIUser::~TalkingUIUser() {
	// Note that we have to use deleteLater instead of directly deleting the objects since
	// the latter can easily cause segmentation faults due to Qt's automatic memory
	// management model (parent-child-hierarchy)
	m_backgroundWidget->deleteLater();
	m_talkingIcon->deleteLater();
	m_nameLabel->deleteLater();
	m_statusIcons->deleteLater();
}

const QIcon &TalkingUIUser::getTalkingIcon(Settings::TalkState talkState) const {
	static const QIcon s_talkingIcon      = QIcon(QLatin1String("skin:talking_on.svg"));
	static const QIcon s_mutedTalkingIcon = QIcon(QLatin1String("skin:talking_muted.svg"));
	static const QIcon s_passiveIcon      = QIcon(QLatin1String("skin:talking_off.svg"));
	static const QIcon s_shoutingIcon     = QIcon(QLatin1String("skin:talking_alt.svg"));
	static const QIcon s_whisperingIcon   = QIcon(QLatin1String("skin:talking_whisper.svg"));

	switch (talkState) {
		case Settings::MutedTalking:
			return s_mutedTalkingIcon;
		case Settings::Talking:
			return s_talkingIcon;
		case Settings::Whispering:
			return s_whisperingIcon;
		case Settings::Shouting:
			return s_shoutingIcon;
		case Settings::Passive:
			return s_passiveIcon;
	}

	// switch above should cover all possible TalkStates
	// Return s_passiveIcon as a fallback
	qCritical("TalkingUIUser: Encountered invalid TalkState");
	return s_passiveIcon;
}

QWidget *TalkingUIUser::getWidget() {
	return m_backgroundWidget;
}

const QWidget *TalkingUIUser::getWidget() const {
	return m_backgroundWidget;
}

MultiStyleWidgetWrapper &TalkingUIUser::getStylableWidget() {
	return m_backgroundWidgetStyleWrapper;
}

EntryType TalkingUIUser::getType() const {
	return EntryType::USER;
}

QString TalkingUIUser::getName() const {
	return m_name;
}

int TalkingUIUser::compare(const TalkingUIEntry &other) const {
	if (other.getType() != getType()) {
		return static_cast< int >(other.getType()) - static_cast< int >(getType());
	}

	const TalkingUIUser &otherUser = static_cast< const TalkingUIUser & >(other);

	if (getPriority() != otherUser.getPriority()) {
		return getPriority() < other.getPriority() ? 1 : -1;
	}

	int res = getName().localeAwareCompare(otherUser.getName());

	if (res == 0) {
		// Make sure that we only consider users the same, if they have the same ID and
		// not simply because they happen to have the same name
		res = otherUser.getAssociatedUserSession() - getAssociatedUserSession();
	}

	return res;
}

void TalkingUIUser::updateTalkingIcon() {
	m_talkingIcon->setPixmap(
		getTalkingIcon(m_talkingState).pixmap(QSize(m_iconSize, m_iconSize), QIcon::Normal, QIcon::On));
}

void TalkingUIUser::setTalkingState(Settings::TalkState talkState) {
	if (m_talkingState == talkState) {
		return;
	}

	m_talkingState = talkState;

	switch (talkState) {
		case Settings::Passive:
			if (m_restrictLifetime) {
				// Start timer as soon as the user has become passive
				m_timer.start();
			}
			break;
		case Settings::Talking:
		case Settings::Whispering:
		case Settings::Shouting:
		case Settings::MutedTalking:
			// Stop timer as the user is no longer inactive
			m_timer.stop();
	}

	updateTalkingIcon();
}

void TalkingUIUser::setIconSize(int size) {
	m_iconSize = size;

	updateTalkingIcon();
}

void TalkingUIUser::setDisplayString(const QString &displayString) {
	if (g.uiSession == getAssociatedUserSession()) {
		// Display own name in bold
		m_nameLabel->setText(QString::fromLatin1("<b>%1</b>").arg(displayString));
	} else {
		m_nameLabel->setText(displayString);
	}
}

void TalkingUIUser::setLifeTime(unsigned int time) {
	m_timer.setInterval(time);
}

void TalkingUIUser::restrictLifetime(bool restrict) {
	if (restrict && !m_timer.isActive()) {
		// Start timer
		m_timer.start();
	} else if (!restrict && !m_timer.isActive()) {
		// Stop timer
		m_timer.stop();
	}

	m_restrictLifetime = restrict;
}

void TalkingUIUser::setStatus(UserStatus status) {
	static const QIcon s_muteIcon      = QIcon(QLatin1String("skin:muted_server.svg"));
	static const QIcon s_deafIcon      = QIcon(QLatin1String("skin:deafened_server.svg"));
	static const QIcon s_localMuteIcon = QIcon(QLatin1String("skin:muted_local.svg"));
	static const QIcon s_selfMuteIcon  = QIcon(QLatin1String("skin:muted_self.svg"));
	static const QIcon s_selfDeafIcon  = QIcon(QLatin1String("skin:deafened_self.svg"));


	std::vector< std::reference_wrapper< const QIcon > > icons;

	if (status.muted) {
		icons.push_back(s_muteIcon);
	}
	if (status.selfMuted) {
		icons.push_back(s_selfMuteIcon);
	}
	if (status.localMuted) {
		icons.push_back(s_localMuteIcon);
	}
	if (status.deafened) {
		icons.push_back(s_deafIcon);
	}
	if (status.selfDeafened) {
		icons.push_back(s_selfDeafIcon);
	}

	if (icons.size() == 0) {
		// No status icons to be shown -> hide the widget
		m_statusIcons->hide();
		m_backgroundWidget->layout()->removeWidget(m_statusIcons);
	} else {
		// Create a Pixmap that'll hold all icons
		const QSize size(m_iconSize * static_cast< int >(icons.size()), m_iconSize);
		QPixmap pixmap(size);
		pixmap.fill(Qt::transparent);

		// Draw the icons to the Pixmap
		QPainter painter(&pixmap);
		for (int i = 0; i < static_cast< int >(icons.size()); i++) {
			painter.drawPixmap(i * m_iconSize, 0,
							   icons[i].get().pixmap(QSize(m_iconSize, m_iconSize), QIcon::Normal, QIcon::On));
		}

		m_statusIcons->setPixmap(pixmap);

		if (m_backgroundWidget->layout()->indexOf(m_statusIcons) < 0) {
			// Add the status icons into the scene
			m_statusIcons->show();
			m_backgroundWidget->layout()->addWidget(m_statusIcons);
		}
	}
}



TalkingUIChannelListener::TalkingUIChannelListener(const ClientUser &user, const Channel &channel)
	: TalkingUIEntry(user.uiSession), m_backgroundWidget(new QWidget()),
	  m_backgroundWidgetStyleWrapper(m_backgroundWidget), m_channelID(channel.iId), m_name(user.qsName) {
	// Create background widget and its layout that we'll use to place all other
	// components on
	m_backgroundWidget->setProperty("selected", false);
	QLayout *backgroundLayout = new QHBoxLayout();
	backgroundLayout->setContentsMargins(USER_CONTAINER_HORIZONTAL_MARGIN, USER_CONTAINER_VERTICAL_MARGIN,
										 USER_CONTAINER_HORIZONTAL_MARGIN, USER_CONTAINER_VERTICAL_MARGIN);
	m_backgroundWidget->setLayout(backgroundLayout);
	m_backgroundWidget->setAutoFillBackground(true);


	// Create the label we use to display the icon
	m_icon = new QLabel(m_backgroundWidget);
	m_icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

	int iconSize = QFontMetrics(m_backgroundWidget->font()).height();
	// setIconSize will not only set the size but also the icon itself
	setIconSize(iconSize);

	// Create the label we use to display the user's name
	m_nameLabel                 = new QLabel(m_backgroundWidget);
	const QString displayString = UserModel::createDisplayString(user, true, &channel);
	setDisplayString(displayString);


	backgroundLayout->addWidget(m_icon);
	backgroundLayout->addWidget(m_nameLabel);
}

TalkingUIChannelListener::~TalkingUIChannelListener() {
	m_backgroundWidget->deleteLater();
	m_icon->deleteLater();
	m_nameLabel->deleteLater();
}

EntryType TalkingUIChannelListener::getType() const {
	return EntryType::LISTENER;
}

QWidget *TalkingUIChannelListener::getWidget() {
	return m_backgroundWidget;
}

const QWidget *TalkingUIChannelListener::getWidget() const {
	return m_backgroundWidget;
}

MultiStyleWidgetWrapper &TalkingUIChannelListener::getStylableWidget() {
	return m_backgroundWidgetStyleWrapper;
}

void TalkingUIChannelListener::setIconSize(int size) {
	static const QIcon s_earIcon = QIcon(QLatin1String("skin:ear.svg"));

	m_icon->setPixmap(s_earIcon.pixmap(QSize(size, size), QIcon::Normal, QIcon::On));
}

void TalkingUIChannelListener::setDisplayString(const QString &displayString) {
	m_nameLabel->setText(QString::fromLatin1("<i>%1</i>").arg(displayString));
}

int TalkingUIChannelListener::getAssociatedChannelID() const {
	return m_channelID;
}