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
|
// Copyright 2009-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 "UserView.h"
#include "Channel.h"
#include "ClientUser.h"
#include "Log.h"
#include "MainWindow.h"
#include "ServerHandler.h"
#include "UserModel.h"
#include <QtGui/QDesktopServices>
#include <QtGui/QHelpEvent>
#include <QtGui/QPainter>
#include <QtWidgets/QWhatsThis>
// 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"
const int UserDelegate::FLAG_ICON_DIMENSION = 16;
const int UserDelegate::FLAG_ICON_PADDING = 1;
const int UserDelegate::FLAG_DIMENSION = 18;
UserDelegate::UserDelegate(QObject *p) : QStyledItemDelegate(p) {
}
void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
const QAbstractItemModel *m = index.model();
const QModelIndex idxc1 = index.sibling(index.row(), 1);
QVariant data = m->data(idxc1);
QList< QVariant > ql = data.toList();
// Allow a UserView's BackgroundRole to override the current theme's default color.
QVariant bg = index.data(Qt::BackgroundRole);
if (bg.isValid()) {
painter->fillRect(option.rect, bg.value< QBrush >());
}
painter->save();
QStyleOptionViewItem o = option;
initStyleOption(&o, index);
QStyle *style = o.widget->style();
QIcon::Mode iconMode = QIcon::Normal;
QPalette::ColorRole colorRole = ((o.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text);
#if defined(Q_OS_WIN)
// Qt's Vista Style has the wrong highlight color for treeview items
// We can't check for QStyleSheetStyle so we have to search the children list search for a QWindowsVistaStyle
QList< QObject * > hierarchy = style->findChildren< QObject * >();
hierarchy.insert(0, style);
foreach (QObject *obj, hierarchy) {
if (QString::fromUtf8(obj->metaObject()->className()) == QString::fromUtf8("QWindowsVistaStyle")) {
colorRole = QPalette::Text;
break;
}
}
#endif
// draw background
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &o, painter, o.widget);
// resize rect to exclude the flag icons
o.rect = option.rect.adjusted(0, 0, -FLAG_DIMENSION * ql.count(), 0);
// draw icon
QRect decorationRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &o, o.widget);
o.icon.paint(painter, decorationRect, o.decorationAlignment, iconMode, QIcon::On);
// draw text
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &o, o.widget);
QString itemText = o.fontMetrics.elidedText(o.text, o.textElideMode, textRect.width());
painter->setFont(o.font);
style->drawItemText(painter, textRect, o.displayAlignment, o.palette, true, itemText, colorRole);
// draw flag icons to original rect
QRect ps = QRect(option.rect.right() - (ql.size() * FLAG_DIMENSION), option.rect.y(), ql.size() * FLAG_DIMENSION,
option.rect.height());
for (int i = 0; i < ql.size(); ++i) {
QRect r = ps;
r.setSize(QSize(FLAG_ICON_DIMENSION, FLAG_ICON_DIMENSION));
r.translate(i * FLAG_DIMENSION + FLAG_ICON_PADDING, FLAG_ICON_PADDING);
QRect p = QStyle::alignedRect(option.direction, option.decorationAlignment, r.size(), r);
qvariant_cast< QIcon >(ql[i]).paint(painter, p, option.decorationAlignment, iconMode, QIcon::On);
}
painter->restore();
}
bool UserDelegate::helpEvent(QHelpEvent *evt, QAbstractItemView *view, const QStyleOptionViewItem &option,
const QModelIndex &index) {
if (index.isValid()) {
const QAbstractItemModel *m = index.model();
const QModelIndex firstColumnIdx = index.sibling(index.row(), 1);
QVariant data = m->data(firstColumnIdx);
QList< QVariant > flagList = data.toList();
const int offset = flagList.size() * -FLAG_DIMENSION;
const int firstFlagPos = option.rect.topRight().x() + offset;
if (evt->pos().x() >= firstFlagPos) {
return QStyledItemDelegate::helpEvent(evt, view, option, firstColumnIdx);
}
}
return QStyledItemDelegate::helpEvent(evt, view, option, index);
}
UserView::UserView(QWidget *p) : QTreeView(p) {
setItemDelegate(new UserDelegate(this));
qtSearch = new QTimer(this);
qtSearch->setInterval(QApplication::keyboardInputInterval());
qtSearch->setSingleShot(true);
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(nodeActivated(const QModelIndex &)));
connect(qtSearch, SIGNAL(timeout()), this, SLOT(selectSearchResult()));
}
/**
* This implementation contains a special handler to display
* custom what's this entries for items. All other events are
* passed on.
*/
bool UserView::event(QEvent *evt) {
if (evt->type() == QEvent::WhatsThisClicked) {
QWhatsThisClickedEvent *qwtce = static_cast< QWhatsThisClickedEvent * >(evt);
QDesktopServices::openUrl(qwtce->href());
evt->accept();
return true;
}
return QTreeView::event(evt);
}
/**
* This function is used to create custom behaviour when clicking
* on user/channel flags (e.g. showing the comment)
*/
void UserView::mouseReleaseEvent(QMouseEvent *evt) {
QPoint clickPosition = evt->pos();
QModelIndex idx = indexAt(clickPosition);
if ((evt->button() == Qt::LeftButton) && idx.isValid()) {
UserModel *userModel = qobject_cast< UserModel * >(model());
ClientUser *clientUser = userModel->getUser(idx);
Channel *channel = userModel->getChannel(idx);
// This is the x offset of the _beginning_ of the comment icon starting from the
// right.
// Thus if the comment icon is the last icon that is displayed, this is equal to
// the negative width of a flag's width (which it is initialized to here). For
// every flag that is displayed to the right of the comment flag, we have to subtract
// UserDelegate::FLAG_DIMENSION once.
int commentFlagPxOffset = -UserDelegate::FLAG_DIMENSION;
bool hasComment = false;
if (clientUser && !clientUser->qbaCommentHash.isEmpty()) {
hasComment = true;
if (clientUser->bLocalIgnore)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bRecording)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bPrioritySpeaker)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bMute)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bSuppress)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bSelfMute)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bLocalMute)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bSelfDeaf)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->bDeaf)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (!clientUser->qsFriendName.isEmpty())
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (clientUser->iId >= 0)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
} else if (channel && !channel->qbaDescHash.isEmpty()) {
hasComment = true;
if (channel->bFiltered)
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
if (channel->hasEnterRestrictions) {
commentFlagPxOffset -= UserDelegate::FLAG_DIMENSION;
}
}
if (hasComment) {
QRect r = visualRect(idx);
const int commentFlagPxPos = r.topRight().x() + commentFlagPxOffset;
if ((clickPosition.x() >= commentFlagPxPos)
&& (clickPosition.x() <= (commentFlagPxPos + UserDelegate::FLAG_DIMENSION))) {
// Clicked comment icon
QString str = userModel->data(idx, Qt::ToolTipRole).toString();
if (str.isEmpty()) {
userModel->bClicked = true;
} else {
QWhatsThis::showText(viewport()->mapToGlobal(r.bottomRight()), str, this);
userModel->seenComment(idx);
}
return;
}
}
}
QTreeView::mouseReleaseEvent(evt);
}
void UserView::keyPressEvent(QKeyEvent *ev) {
if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Enter)
UserView::nodeActivated(currentIndex());
QTreeView::keyPressEvent(ev);
}
void UserView::nodeActivated(const QModelIndex &idx) {
UserModel *um = static_cast< UserModel * >(model());
ClientUser *p = um->getUser(idx);
if (p) {
g.mw->openTextMessageDialog(p);
return;
}
Channel *c = um->getChannel(idx);
if (c) {
// if a channel is activated join it
g.sh->joinChannel(g.uiSession, c->iId);
}
}
/**
* This implementation provides a recursive realtime search over
* the whole channel tree. It also features delayed selection
* with with automatic expanding of folded channels.
*/
void UserView::keyboardSearch(const QString &search) {
if (qtSearch->isActive()) {
qpmiSearch = QPersistentModelIndex();
qtSearch->stop();
}
bool forceSkip = false;
if (tSearch.restart() > (QApplication::keyboardInputInterval() * 1000ULL)) {
qsSearch = QString();
forceSkip = true;
}
bool isBackspace = (search.length() == 1) && (search.at(0).row() == 0) && (search.at(0).cell() == 8);
if (isBackspace) {
if (!qsSearch.isEmpty())
qsSearch = qsSearch.left(qsSearch.length() - 1);
} else {
qsSearch += search;
}
// Try default search (which doesn't recurse non-expanded items) and see if it returns something "valid"
QTreeView::keyboardSearch(search);
QModelIndex start = currentIndex();
if (start.isValid() && model()->data(start, Qt::DisplayRole).toString().startsWith(qsSearch, Qt::CaseInsensitive))
return;
if (forceSkip && start.isValid())
start = indexBelow(start);
if (!start.isValid())
start = model()->index(0, 0, QModelIndex());
QModelIndexList qmil = model()->match(start, Qt::DisplayRole, qsSearch, 1,
Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap | Qt::MatchRecursive));
if (qmil.count() == 0)
qmil = model()->match(start, Qt::DisplayRole, qsSearch, 1,
Qt::MatchFlags(Qt::MatchContains | Qt::MatchWrap | Qt::MatchRecursive));
if (qmil.isEmpty())
return;
QModelIndex qmi = qmil.at(0);
QModelIndex p = qmi.parent();
bool visible = true;
while (visible && p.isValid()) {
visible = visible && isExpanded(p);
p = p.parent();
}
if (visible)
selectionModel()->setCurrentIndex(qmi, QItemSelectionModel::ClearAndSelect);
else {
qpmiSearch = qmi;
qtSearch->start();
}
}
void UserView::selectSearchResult() {
if (qpmiSearch.isValid()) {
selectionModel()->setCurrentIndex(qpmiSearch, QItemSelectionModel::ClearAndSelect);
}
qpmiSearch = QPersistentModelIndex();
}
bool channelHasUsers(const Channel *c) {
if (c->qlUsers.isEmpty() == false)
return true;
int i;
for (i = 0; i < c->qlChannels.count(); i++) {
if (channelHasUsers(c->qlChannels[i]))
return true;
}
return false;
}
static bool channelFiltered(const Channel *c) {
while (c) {
if (c->bFiltered)
return true;
c = c->cParent;
}
return false;
}
void UserView::updateChannel(const QModelIndex &idx) {
UserModel *um = static_cast< UserModel * >(model());
if (!idx.isValid()) {
return;
}
Channel *c = um->getChannel(idx);
for (int i = 0; idx.model()->index(i, 0, idx).isValid(); ++i) {
updateChannel(idx.model()->index(i, 0, idx));
}
if (c && idx.parent().isValid()) {
if (g.s.bFilterActive == false) {
setRowHidden(idx.row(), idx.parent(), false);
} else {
bool isChannelUserIsIn = false;
// Check whether user resides in this channel or a subchannel
if (g.uiSession != 0) {
const ClientUser *user = ClientUser::get(g.uiSession);
if (user) {
Channel *chan = user->cChannel;
while (chan) {
if (chan == c) {
isChannelUserIsIn = true;
break;
}
chan = chan->cParent;
}
}
}
if (channelFiltered(c) && !isChannelUserIsIn) {
setRowHidden(idx.row(), idx.parent(), true);
} else {
if (g.s.bFilterHidesEmptyChannels && !channelHasUsers(c)) {
setRowHidden(idx.row(), idx.parent(), true);
} else {
setRowHidden(idx.row(), idx.parent(), false);
}
}
}
}
}
void UserView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &) {
UserModel *um = static_cast< UserModel * >(model());
int nRowCount = um->rowCount();
int i;
for (i = 0; i < nRowCount; i++)
updateChannel(um->index(i, 0));
QTreeView::dataChanged(topLeft, bottomRight);
}
|