Skip to content

Commit a587514

Browse files
committed
FIX(ui): Use correct font size for scaling status icons
Previously (mumble-voip#5772), we implemented a fix to set the status icon size according to the user display scaling. The calculation is based upon font sizes. However, due to an oversight, the wrong font size was used as the base for this calculation. However, the problem was not visible, if by coincidence the correct font size was the same as the one used by accident. This commit changes the calculation of the icon size once again to use the correct font information. Fixes one part of mumble-voip#5817
1 parent 2a88501 commit a587514

2 files changed

Lines changed: 65 additions & 48 deletions

File tree

src/mumble/UserView.cpp

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
#include <QtGui/QPainter>
1919
#include <QtWidgets/QWhatsThis>
2020

21-
UserDelegate::UserDelegate(QObject *p, int flagTotalDimension, int flagIconPadding, int flagIconDimension)
22-
: QStyledItemDelegate(p) {
23-
m_flagTotalDimension = flagTotalDimension;
24-
m_flagIconDimension = flagIconDimension;
25-
m_flagIconPadding = flagIconPadding;
21+
UserDelegate::UserDelegate(QObject *p) : QStyledItemDelegate(p) {
22+
}
23+
24+
void UserDelegate::adjustIcons(int iconTotalDimension, int iconIconPadding, int iconIconDimension) {
25+
m_iconTotalDimension = iconTotalDimension;
26+
m_iconIconPadding = iconIconPadding;
27+
m_iconIconDimension = iconIconDimension;
2628
}
2729

2830
void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
@@ -62,8 +64,8 @@ void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
6264
// draw background
6365
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &o, painter, o.widget);
6466

65-
// resize rect to exclude the flag icons
66-
o.rect = option.rect.adjusted(0, 0, -m_flagTotalDimension * ql.count(), 0);
67+
// resize rect to exclude the icons
68+
o.rect = option.rect.adjusted(0, 0, -m_iconTotalDimension * ql.count(), 0);
6769

6870
// draw icon
6971
QRect decorationRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &o, o.widget);
@@ -75,14 +77,14 @@ void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
7577
painter->setFont(o.font);
7678
style->drawItemText(painter, textRect, o.displayAlignment, o.palette, true, itemText, colorRole);
7779

78-
// draw flag icons to original rect
79-
QRect ps = QRect(option.rect.right() - (ql.size() * m_flagTotalDimension), option.rect.y(),
80-
ql.size() * m_flagTotalDimension, option.rect.height());
80+
// draw icons to original rect
81+
QRect ps = QRect(option.rect.right() - (ql.size() * m_iconTotalDimension), option.rect.y(),
82+
ql.size() * m_iconTotalDimension, option.rect.height());
8183

8284
for (int i = 0; i < ql.size(); ++i) {
8385
QRect r = ps;
84-
r.setSize(QSize(m_flagIconDimension, m_flagIconDimension));
85-
r.translate(i * m_flagTotalDimension + m_flagIconPadding, m_flagIconPadding);
86+
r.setSize(QSize(m_iconIconDimension, m_iconIconDimension));
87+
r.translate(i * m_iconTotalDimension + m_iconIconPadding, m_iconIconPadding);
8688
QRect p = QStyle::alignedRect(option.direction, option.decorationAlignment, r.size(), r);
8789
qvariant_cast< QIcon >(ql[i]).paint(painter, p, option.decorationAlignment, iconMode, QIcon::On);
8890
}
@@ -96,27 +98,39 @@ bool UserDelegate::helpEvent(QHelpEvent *evt, QAbstractItemView *view, const QSt
9698
const QAbstractItemModel *m = index.model();
9799
const QModelIndex firstColumnIdx = index.sibling(index.row(), 1);
98100
QVariant data = m->data(firstColumnIdx);
99-
QList< QVariant > flagList = data.toList();
100-
const int offset = flagList.size() * -m_flagTotalDimension;
101-
const int firstFlagPos = option.rect.topRight().x() + offset;
101+
QList< QVariant > iconList = data.toList();
102+
const int offset = iconList.size() * -m_iconTotalDimension;
103+
const int firstIconPos = option.rect.topRight().x() + offset;
102104

103-
if (evt->pos().x() >= firstFlagPos) {
105+
if (evt->pos().x() >= firstIconPos) {
104106
return QStyledItemDelegate::helpEvent(evt, view, option, firstColumnIdx);
105107
}
106108
}
107109
return QStyledItemDelegate::helpEvent(evt, view, option, index);
108110
}
109111

110112
UserView::UserView(QWidget *p) : QTreeView(p) {
113+
m_userDelegate = new UserDelegate(this);
114+
adjustIcons();
115+
setItemDelegate(m_userDelegate);
116+
117+
// Because in Qt fonts take some time to initialize properly, we have to delay the call
118+
// to adjustIcons a bit in order to give the fonts the necessary time (so we can read out
119+
// the actual font details).
120+
QTimer::singleShot(0, [this]() { adjustIcons(); });
121+
122+
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(nodeActivated(const QModelIndex &)));
123+
}
124+
125+
void UserView::adjustIcons() {
111126
// Calculate the icon size for status icons based on font size
112127
// This should automaticially adjust size when the user has
113128
// display scaling enabled
114-
m_flagTotalDimension = QFontMetrics(p->font()).height();
115-
int flagIconPadding = 1;
116-
int flagIconDimension = m_flagTotalDimension - (4 * flagIconPadding);
117-
setItemDelegate(new UserDelegate(this, m_flagTotalDimension, flagIconPadding, flagIconDimension));
118-
119-
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(nodeActivated(const QModelIndex &)));
129+
m_iconTotalDimension = QFontMetrics(font()).height();
130+
int iconIconPadding = 1;
131+
int iconIconDimension = m_iconTotalDimension - (2 * iconIconPadding);
132+
m_userDelegate->adjustIcons(m_iconTotalDimension, iconIconPadding, iconIconDimension);
133+
viewport()->update();
120134
}
121135

122136
/**
@@ -136,7 +150,7 @@ bool UserView::event(QEvent *evt) {
136150

137151
/**
138152
* This function is used to create custom behaviour when clicking
139-
* on user/channel flags (e.Global::get(). showing the comment)
153+
* on user/channel icons (e.Global::get(). showing the comment)
140154
*/
141155
void UserView::mouseReleaseEvent(QMouseEvent *evt) {
142156
QPoint clickPosition = evt->pos();
@@ -150,55 +164,55 @@ void UserView::mouseReleaseEvent(QMouseEvent *evt) {
150164
// This is the x offset of the _beginning_ of the comment icon starting from the
151165
// right.
152166
// Thus if the comment icon is the last icon that is displayed, this is equal to
153-
// the negative width of a flag's width (which it is initialized to here). For
154-
// every flag that is displayed to the right of the comment flag, we have to subtract
155-
// m_flagTotalDimension once.
156-
int commentFlagPxOffset = -m_flagTotalDimension;
167+
// the negative width of a icon's width (which it is initialized to here). For
168+
// every icon that is displayed to the right of the comment icon, we have to subtract
169+
// m_iconTotalDimension once.
170+
int commentIconPxOffset = -m_iconTotalDimension;
157171
bool hasComment = false;
158172

159173
if (clientUser && !clientUser->qbaCommentHash.isEmpty()) {
160174
hasComment = true;
161175

162176
if (clientUser->bLocalIgnore)
163-
commentFlagPxOffset -= m_flagTotalDimension;
177+
commentIconPxOffset -= m_iconTotalDimension;
164178
if (clientUser->bRecording)
165-
commentFlagPxOffset -= m_flagTotalDimension;
179+
commentIconPxOffset -= m_iconTotalDimension;
166180
if (clientUser->bPrioritySpeaker)
167-
commentFlagPxOffset -= m_flagTotalDimension;
181+
commentIconPxOffset -= m_iconTotalDimension;
168182
if (clientUser->bMute)
169-
commentFlagPxOffset -= m_flagTotalDimension;
183+
commentIconPxOffset -= m_iconTotalDimension;
170184
if (clientUser->bSuppress)
171-
commentFlagPxOffset -= m_flagTotalDimension;
185+
commentIconPxOffset -= m_iconTotalDimension;
172186
if (clientUser->bSelfMute)
173-
commentFlagPxOffset -= m_flagTotalDimension;
187+
commentIconPxOffset -= m_iconTotalDimension;
174188
if (clientUser->bLocalMute)
175-
commentFlagPxOffset -= m_flagTotalDimension;
189+
commentIconPxOffset -= m_iconTotalDimension;
176190
if (clientUser->bSelfDeaf)
177-
commentFlagPxOffset -= m_flagTotalDimension;
191+
commentIconPxOffset -= m_iconTotalDimension;
178192
if (clientUser->bDeaf)
179-
commentFlagPxOffset -= m_flagTotalDimension;
193+
commentIconPxOffset -= m_iconTotalDimension;
180194
if (!clientUser->qsFriendName.isEmpty())
181-
commentFlagPxOffset -= m_flagTotalDimension;
195+
commentIconPxOffset -= m_iconTotalDimension;
182196
if (clientUser->iId >= 0)
183-
commentFlagPxOffset -= m_flagTotalDimension;
197+
commentIconPxOffset -= m_iconTotalDimension;
184198

185199
} else if (channel && !channel->qbaDescHash.isEmpty()) {
186200
hasComment = true;
187201

188202
if (channel->bFiltered)
189-
commentFlagPxOffset -= m_flagTotalDimension;
203+
commentIconPxOffset -= m_iconTotalDimension;
190204

191205
if (channel->hasEnterRestrictions) {
192-
commentFlagPxOffset -= m_flagTotalDimension;
206+
commentIconPxOffset -= m_iconTotalDimension;
193207
}
194208
}
195209

196210
if (hasComment) {
197211
QRect r = visualRect(idx);
198-
const int commentFlagPxPos = r.topRight().x() + commentFlagPxOffset;
212+
const int commentIconPxPos = r.topRight().x() + commentIconPxOffset;
199213

200-
if ((clickPosition.x() >= commentFlagPxPos)
201-
&& (clickPosition.x() <= (commentFlagPxPos + m_flagTotalDimension))) {
214+
if ((clickPosition.x() >= commentIconPxPos)
215+
&& (clickPosition.x() <= (commentIconPxPos + m_iconTotalDimension))) {
202216
// Clicked comment icon
203217
QString str = userModel->data(idx, Qt::ToolTipRole).toString();
204218
if (str.isEmpty()) {

src/mumble/UserView.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ class UserDelegate : public QStyledItemDelegate {
1717
Q_OBJECT
1818
Q_DISABLE_COPY(UserDelegate)
1919

20-
int m_flagTotalDimension;
21-
int m_flagIconPadding;
22-
int m_flagIconDimension;
20+
int m_iconTotalDimension;
21+
int m_iconIconPadding;
22+
int m_iconIconDimension;
2323

2424
public:
25-
UserDelegate(QObject *parent, int flagTotalDimension, int flagIconPadding, int flagIconDimension);
25+
UserDelegate(QObject *parent);
2626
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
27+
void adjustIcons(int iconTotalDimension, int iconIconPadding, int iconIconDimension);
2728

2829
public slots:
2930
bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option,
@@ -35,7 +36,9 @@ class UserView : public QTreeView {
3536
Q_OBJECT
3637
Q_DISABLE_COPY(UserView)
3738

38-
int m_flagTotalDimension;
39+
int m_iconTotalDimension;
40+
UserDelegate *m_userDelegate;
41+
void adjustIcons();
3942

4043
protected:
4144
void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE;

0 commit comments

Comments
 (0)