Skip to content

Commit

Permalink
Experimental Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartmnt committed Aug 22, 2022
1 parent bb82ad0 commit 64beb2e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
80 changes: 45 additions & 35 deletions src/mumble/UserView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
#include <QtGui/QPainter>
#include <QtWidgets/QWhatsThis>

// These will be updated later when the font size
// of the tree view item is known
int UserDelegate::FLAG_TOTAL_DIMENSION = 0;
int UserDelegate::FLAG_ICON_DIMENSION = 0;

UserDelegate::UserDelegate(QObject *p) : QStyledItemDelegate(p) {
}

void UserDelegate::adjustFlag(int flagTotalDimension, int flagIconPadding, int flagIconDimension) {
m_flagTotalDimension = flagTotalDimension;
m_flagIconPadding = flagIconPadding;
m_flagIconDimension = flagIconDimension;
}

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);
Expand Down Expand Up @@ -60,17 +61,11 @@ void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
}
#endif

// Calculate the icon size for status icons based on font size
// This should automaticially adjust size when the user has
// display scaling enabled
FLAG_TOTAL_DIMENSION = o.fontMetrics.height();
FLAG_ICON_DIMENSION = FLAG_TOTAL_DIMENSION - (4 * FLAG_ICON_PADDING);

// 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_TOTAL_DIMENSION * ql.count(), 0);
o.rect = option.rect.adjusted(0, 0, -m_flagTotalDimension * ql.count(), 0);

// draw icon
QRect decorationRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &o, o.widget);
Expand All @@ -83,13 +78,13 @@ void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
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_TOTAL_DIMENSION), option.rect.y(),
ql.size() * FLAG_TOTAL_DIMENSION, option.rect.height());
QRect ps = QRect(option.rect.right() - (ql.size() * m_flagTotalDimension), option.rect.y(),
ql.size() * m_flagTotalDimension, 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_TOTAL_DIMENSION + FLAG_ICON_PADDING, FLAG_ICON_PADDING);
r.setSize(QSize(m_flagIconDimension, m_flagIconDimension));
r.translate(i * m_flagTotalDimension + m_flagIconPadding, m_flagIconPadding);
QRect p = QStyle::alignedRect(option.direction, option.decorationAlignment, r.size(), r);
qvariant_cast< QIcon >(ql[i]).paint(painter, p, option.decorationAlignment, iconMode, QIcon::On);
}
Expand All @@ -104,7 +99,7 @@ bool UserDelegate::helpEvent(QHelpEvent *evt, QAbstractItemView *view, const QSt
const QModelIndex firstColumnIdx = index.sibling(index.row(), 1);
QVariant data = m->data(firstColumnIdx);
QList< QVariant > flagList = data.toList();
const int offset = flagList.size() * -FLAG_TOTAL_DIMENSION;
const int offset = flagList.size() * -m_flagTotalDimension;
const int firstFlagPos = option.rect.topRight().x() + offset;

if (evt->pos().x() >= firstFlagPos) {
Expand All @@ -115,11 +110,26 @@ bool UserDelegate::helpEvent(QHelpEvent *evt, QAbstractItemView *view, const QSt
}

UserView::UserView(QWidget *p) : QTreeView(p) {
setItemDelegate(new UserDelegate(this));
// Calculate the icon size for status icons based on font size
// This should automaticially adjust size when the user has
// display scaling enabled
m_userDelegate = new UserDelegate(this);
adjustFlag();
setItemDelegate(m_userDelegate);

QTimer::singleShot(0, [this]() { adjustFlag(); });

connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(nodeActivated(const QModelIndex &)));
}

void UserView::adjustFlag() {
m_flagTotalDimension = QFontMetrics(font()).height();
int flagIconPadding = 1;
int flagIconDimension = m_flagTotalDimension - (4 * flagIconPadding);
m_userDelegate->adjustFlag(m_flagTotalDimension, flagIconPadding, flagIconDimension);
viewport()->update();
}

/**
* This implementation contains a special handler to display
* custom what's this entries for items. All other events are
Expand Down Expand Up @@ -153,53 +163,53 @@ void UserView::mouseReleaseEvent(QMouseEvent *evt) {
// 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
// FLAG_TOTAL_DIMENSION once.
int flagDimension = UserDelegate::FLAG_TOTAL_DIMENSION;
int commentFlagPxOffset = -flagDimension;
// m_flagTotalDimension once.
int commentFlagPxOffset = -m_flagTotalDimension;
bool hasComment = false;

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

if (clientUser->bLocalIgnore)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bRecording)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bPrioritySpeaker)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bMute)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bSuppress)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bSelfMute)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bLocalMute)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bSelfDeaf)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->bDeaf)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (!clientUser->qsFriendName.isEmpty())
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
if (clientUser->iId >= 0)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;

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

if (channel->bFiltered)
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;

if (channel->hasEnterRestrictions) {
commentFlagPxOffset -= flagDimension;
commentFlagPxOffset -= m_flagTotalDimension;
}
}

if (hasComment) {
QRect r = visualRect(idx);
const int commentFlagPxPos = r.topRight().x() + commentFlagPxOffset;

if ((clickPosition.x() >= commentFlagPxPos) && (clickPosition.x() <= (commentFlagPxPos + flagDimension))) {
if ((clickPosition.x() >= commentFlagPxPos)
&& (clickPosition.x() <= (commentFlagPxPos + m_flagTotalDimension))) {
// Clicked comment icon
QString str = userModel->data(idx, Qt::ToolTipRole).toString();
if (str.isEmpty()) {
Expand Down
13 changes: 9 additions & 4 deletions src/mumble/UserView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class UserDelegate : public QStyledItemDelegate {
Q_OBJECT
Q_DISABLE_COPY(UserDelegate)

int m_flagTotalDimension;
int m_flagIconPadding;
int m_flagIconDimension;

public:
UserDelegate(QObject *parent);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;

const static int FLAG_ICON_PADDING = 1;
static int FLAG_TOTAL_DIMENSION;
static int FLAG_ICON_DIMENSION;
void adjustFlag(int flagTotalDimension, int flagIconPadding, int flagIconDimension);

public slots:
bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option,
Expand All @@ -35,6 +36,10 @@ class UserView : public QTreeView {
Q_OBJECT
Q_DISABLE_COPY(UserView)

int m_flagTotalDimension;
UserDelegate *m_userDelegate;
void adjustFlag();

protected:
void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *) Q_DECL_OVERRIDE;
Expand Down

0 comments on commit 64beb2e

Please sign in to comment.