Skip to content

Commit ad879b3

Browse files
author
ut003441
committed
fix: 修复字体字号为20的时候锁屏界面切换用户列表文字被遮挡的问题
监听字体字号改变信号,实时更新列表宽度 Log: Bug: linuxdeepin/developer-center#8873 Influence: 切换用户列表文字展示 fix: 修复字体字号为20的时候锁屏界面切换用户列表文字被遮挡的问题 监听字体字号改变信号,实时更新列表宽度 Log: Bug: linuxdeepin/developer-center#8873 Influence: 切换用户列表文字展示 fix: 修复字体字号为20的时候锁屏界面切换用户列表文字被遮挡的问题 监听字体字号改变信号,实时更新列表宽度 Log: Bug: linuxdeepin/developer-center#8873 Influence: 切换用户列表文字展示 fix: 修复字体字号为20的时候锁屏界面切换用户列表文字被遮挡的问题 监听字体字号改变信号,实时更新列表宽度 Log: Bug: linuxdeepin/developer-center#8873 Influence: 切换用户列表文字展示 fix: 修复字体字号为20的时候锁屏界面切换用户列表文字被遮挡的问题 监听字体字号改变信号,实时更新列表宽度 Log: Bug: linuxdeepin/developer-center#8873 Influence: 切换用户列表文字展示
1 parent 75981b6 commit ad879b3

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

src/widgets/useritemdelegate.cpp

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
DWIDGET_USE_NAMESPACE
1515

1616
const int ITEM_HEIGHT = 64;
17+
const int ITEM_SPACE = 10;
18+
const int LABEL_SPACE = 2;
1719
const int RADIUS_VALUE = 8;
1820
const QSize IMAGE_SIZE = QSize(48, 48);
1921

@@ -30,6 +32,8 @@ void UserItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
3032

3133
QPen pen;
3234
QRect rect = option.rect;
35+
// item间要留10空间的空白
36+
rect.setHeight(rect.height() - ITEM_SPACE);
3337

3438
if (option.state.testFlag(QStyle::State_Selected)) {
3539
// 鼠标悬停背景色
@@ -77,7 +81,16 @@ void UserItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
7781
QSize UserItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
7882
{
7983
Q_UNUSED(index)
80-
return QSize(option.rect.width(), ITEM_HEIGHT);
84+
85+
UserItemData userData = index.data(StaticUserDataRole).value<UserItemData>();
86+
int height = ITEM_SPACE + displayNameHeight() + LABEL_SPACE + userTypeHeight() + ITEM_SPACE;
87+
if (!userData.name.isEmpty()) {
88+
height += nameHeight() + LABEL_SPACE;
89+
}
90+
91+
height = height > ITEM_HEIGHT ? height : ITEM_HEIGHT;
92+
93+
return QSize(option.rect.width(), height);
8194
}
8295

8396
void UserItemDelegate::drawRoundImage(QPainter *thisPainter, const QRect &rect, const QString &path) const
@@ -86,10 +99,14 @@ void UserItemDelegate::drawRoundImage(QPainter *thisPainter, const QRect &rect,
8699
return;
87100

88101
// 设计图上常量
89-
const int margTop = 8;
102+
int margTop = 0;
90103
const int marginLeft = 8;
91104
const int imageRadius = 10;
92105

106+
if (rect.height() > IMAGE_SIZE.height()) {
107+
margTop = rect.height() / 2 - IMAGE_SIZE.height() / 2;
108+
}
109+
93110
QRect drawRect = QRect(rect.left() + marginLeft, rect.top() + margTop,
94111
IMAGE_SIZE.width(), IMAGE_SIZE.height());
95112
QPainterPath clipPath;
@@ -124,24 +141,24 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
124141
// 文字区域设计图常量
125142
const int leftMargin = 65; // 开始位置距离item最左边
126143
const int rightMargin = 48; // 结束位置距离item最右边
127-
const int topMargin = 6;
128-
const int displayNameAreaHeight = 29;
144+
const int topMargin = 0;
145+
129146
const int itemSpacing = 10;
130-
const int nameAreaHeight = 20;
131-
const int typeAreaHeight = 17;
132-
const int vItemSpacing = 2;
133147

134148
int textAreaWidth = rect.width() - leftMargin - rightMargin;
135149
QRect displayNameRect = QRect(rect.left() + leftMargin, rect.top() + topMargin,
136-
textAreaWidth, displayNameAreaHeight);
150+
textAreaWidth, displayNameHeight());
137151

138152
QFont font = DFontSizeManager::instance()->t4();
139153
font.setBold(true);
140154
painter->setFont(font);
141155

142156
// 绘制displayName, +1个像素是为了避免刚好width和文本宽度一致,最后文本还有省略号
143157
QString displayNameText = elidedText(userData.displayName, textAreaWidth + 1, font.pixelSize(), true);
144-
painter->drawText(displayNameRect, displayNameText);
158+
159+
QTextOption qTextOption;
160+
qTextOption.setAlignment(Qt::AlignmentFlag::AlignVCenter);
161+
painter->drawText(displayNameRect, displayNameText, qTextOption);
145162

146163
QFont typeFont = DFontSizeManager::instance()->t8();
147164
int userTypeAreaWidth = stringWidth(userData.userStrType, typeFont.pixelSize());
@@ -155,8 +172,8 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
155172

156173
QString nameText = elidedText(userData.name, textAreaWidth - itemSpacing - userTypeAreaWidth, font.pixelSize());
157174
nameWidth = stringWidth(nameText, font.pixelSize());
158-
QRect nameRect = QRect(displayNameRect.left(), displayNameRect.bottom() + vItemSpacing,
159-
textAreaWidth - itemSpacing - userTypeAreaWidth, nameAreaHeight);
175+
QRect nameRect = QRect(displayNameRect.left(), displayNameRect.bottom() + LABEL_SPACE,
176+
textAreaWidth - itemSpacing - userTypeAreaWidth, nameHeight());
160177

161178
painter->drawText(nameRect, nameText);
162179
}
@@ -166,8 +183,11 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
166183

167184
// 绘制userType, +1个像素为了避免字体大小不一样,绘制中心位置不一致
168185
int userTypeLeft = userData.name.isEmpty() ? displayNameRect.left() : displayNameRect.left() + nameWidth + itemSpacing;
169-
QRect userTypeRect = QRect(userTypeLeft, displayNameRect.bottom() + vItemSpacing + 1, userTypeAreaWidth + 1, typeAreaHeight);
170-
painter->drawText(userTypeRect, userData.userStrType);
186+
QRect userTypeRect = QRect(userTypeLeft, displayNameRect.bottom() + LABEL_SPACE + 1, userTypeAreaWidth + 1, userTypeHeight());
187+
qreal currentOpacity = painter->opacity();
188+
painter->setOpacity(0.7);
189+
painter->drawText(userTypeRect, userData.userStrType, qTextOption);
190+
painter->setOpacity(currentOpacity);
171191
}
172192

173193
void UserItemDelegate::drawCheckedState(QPainter *painter, const QRect &rect) const
@@ -218,3 +238,24 @@ QString UserItemDelegate::elidedText(const QString &originString, int width, int
218238
QFontMetrics fm(font);
219239
return fm.elidedText(originString, Qt::ElideRight, width);
220240
}
241+
242+
int UserItemDelegate::displayNameHeight()
243+
{
244+
QFont font = DFontSizeManager::instance()->t4();
245+
QFontMetrics fm(font);
246+
return fm.height();
247+
}
248+
249+
int UserItemDelegate::nameHeight()
250+
{
251+
QFont font = DFontSizeManager::instance()->t6();
252+
QFontMetrics fm(font);
253+
return fm.height();
254+
}
255+
256+
int UserItemDelegate::userTypeHeight()
257+
{
258+
QFont font = DFontSizeManager::instance()->t8();
259+
QFontMetrics fm(font);
260+
return fm.height();
261+
}

src/widgets/useritemdelegate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class UserItemDelegate : public QStyledItemDelegate
3131

3232
public:
3333
explicit UserItemDelegate(QObject *parent = nullptr);
34+
static int displayNameHeight();
35+
static int nameHeight();
36+
static int userTypeHeight();
3437

3538
protected:
3639
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;

src/widgets/userlistpopupwidget.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ void UserListPopupWidget::initUI()
179179
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
180180
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
181181
setEditTriggers(QAbstractItemView::NoEditTriggers);
182-
setViewportMargins(0, 0, 0, 0);
182+
setViewportMargins(5, 5, 5, 5);
183183
setMaximumHeight(MAX_HEIGHT);
184-
setSpacing(ITEM_SPACING);
184+
setSpacing(0);
185185

186186
setItemDelegate(m_userItemDelegate);
187187
setModel(m_userItemModel);
@@ -204,6 +204,10 @@ void UserListPopupWidget::initConnections()
204204
qInfo() << "request switch user id:" << data.userId << " displayName:" << data.displayName;
205205
Q_EMIT requestSwitchToUser(m_model->findUserByUid(data.userId));
206206
});
207+
208+
connect(qGuiApp, &QGuiApplication::fontChanged, this, [ this ] {
209+
updateViewHeight();
210+
});
207211
}
208212

209213
void UserListPopupWidget::loadUsers()
@@ -308,7 +312,7 @@ void UserListPopupWidget::updateViewWidth()
308312

309313
void UserListPopupWidget::updateViewHeight()
310314
{
311-
int height = m_userItemModel->rowCount() * ITEM_HEIGHT + (m_userItemModel->rowCount() * 2) * ITEM_SPACING;
315+
int height = m_userItemModel->rowCount() * getItemHeight();
312316
setFixedHeight(qMin(height, MAX_HEIGHT));
313317
}
314318

@@ -326,3 +330,10 @@ QString UserListPopupWidget::accountStrType(int accountType) const
326330
return "";
327331
}
328332
}
333+
334+
int UserListPopupWidget::getItemHeight()
335+
{
336+
int height = ITEM_SPACING * 2 + UserItemDelegate::displayNameHeight() + 2 + UserItemDelegate::userTypeHeight() + ITEM_SPACING * 2;
337+
338+
return height > ITEM_HEIGHT ? height : ITEM_HEIGHT;
339+
}

src/widgets/userlistpopupwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private slots:
5050
void updateViewWidth();
5151
void updateViewHeight();
5252
int calculateItemWidth();
53+
int getItemHeight();
5354
int stringWidth(const QString &str, int fontSize, bool isBold = false);
5455

5556
private:

0 commit comments

Comments
 (0)