Skip to content

Commit 9ab8367

Browse files
deepin-ci-robot18202781743
authored andcommitted
sync: from linuxdeepin/qt5integration
Synchronize source files from linuxdeepin/qt5integration. Source-pull-request: linuxdeepin/qt5integration#216
1 parent ed8c1ae commit 9ab8367

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

styleplugins/chameleon/chameleonstyle.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
14791479
p->setRenderHint(QPainter::Antialiasing);
14801480
p->setPen(Qt::NoPen);
14811481
p->setBrush(Qt::NoBrush);
1482+
int menuButtonIndicatorMargin = 4;
14821483

14831484
if (toolbutton->state & (State_MouseOver | State_Sunken)) //hover状态 、press状态
14841485
p->setBrush(getBrush(toolbutton, DPalette::Button));
@@ -1489,7 +1490,7 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
14891490
(toolbutton->state & (QStyle::State_MouseOver | QStyle::State_Sunken))) {
14901491

14911492
// 绘制外层背景色
1492-
int menuButtonIndicatorMargin = 4;
1493+
14931494
auto btn = *toolbutton;
14941495
if (btn.state & (QStyle::State_MouseOver))
14951496
btn.state &= ~ QStyle::State_MouseOver;
@@ -1532,7 +1533,19 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
15321533
p->setPen(getColor(toolbutton, DPalette::ButtonText));
15331534
}
15341535

1535-
p->drawText(rect, alignment, toolbutton->text);
1536+
// 只显示文字且为PopupDelay模式且有菜单则文字和箭头整体居中显示(日历中的选择月份和年份的toolButton也遵循这个规则)
1537+
if ((toolbutton->features & QStyleOptionToolButton::HasMenu && toolbutton->features & QStyleOptionToolButton::PopupDelay && toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly)
1538+
|| w->property("_d_calendarToolBtn").toBool()) {
1539+
QFontMetrics metrics(toolbutton->font);
1540+
int fontWidth = metrics.horizontalAdvance(toolbutton->text);
1541+
int indicatorWidth = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, w);
1542+
int subRectWidth = fontWidth + indicatorWidth + menuButtonIndicatorMargin;
1543+
QRect subRect = QRect(rect.left() + (rect.width() - subRectWidth) / 2, rect.top(), subRectWidth, rect.height());
1544+
QRect textRect = QRect(subRect.topLeft(), QSize(fontWidth, rect.height()));
1545+
p->drawText(textRect, alignment, toolbutton->text);
1546+
} else {
1547+
p->drawText(rect, alignment, toolbutton->text);
1548+
}
15361549
} else { //只显示文字的情景 的 补集
15371550
QIcon icon;
15381551
QSize pmSize = toolbutton->iconSize;
@@ -1570,8 +1583,6 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
15701583
p->setBrush(getColor(toolbutton, DPalette::Button));
15711584
}
15721585

1573-
1574-
15751586
// pr为图标的大小
15761587
QRect pr = rect;
15771588
// tr为文字的大小
@@ -3316,23 +3327,30 @@ void ChameleonStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleO
33163327
newBtn.rect = QRect(ir.right() - mbi - menuButtonIndicatorMargin, (ir.height() - mbi) / 2, mbi, mbi);
33173328
newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
33183329

3319-
//DelayedPopup 模式,箭头右居中, 加一个日历 月按钮箭头居中
3320-
if (w && w->objectName() == "qt_calendar_monthbutton") {
3321-
newBtn.rect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() / 2, mbi - 4, mbi - 4);
3322-
newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
3330+
//仅文字,DelayedPopup 模式,文字和箭头整体居中
3331+
if (toolbutton->features & QStyleOptionToolButton::PopupDelay && toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
3332+
QFontMetrics metrics(toolbutton->font);
3333+
int fontWidth = metrics.horizontalAdvance(toolbutton->text);
3334+
int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
3335+
QRect subRect = QRect(tool.rect.left() + (tool.rect.width() - subRectWidth) / 2, tool.rect.top(), subRectWidth, tool.rect.height());
3336+
QRect indicatorRect = QRect(subRect.topLeft() + QPoint(fontWidth + menuButtonIndicatorMargin, (tool.rect.height() - mbi) / 2), QSize(mbi, mbi));
3337+
newBtn.rect = indicatorRect;
33233338
}
3339+
33243340
proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
33253341
}
33263342

3327-
//日历 年按钮 特制
3343+
//日历 年按钮 特制 文字和箭头整体居中
33283344
if (w && w->objectName() == "qt_calendar_yearbutton") {
3329-
QStyleOptionToolButton newBtn = *toolbutton;
3330-
int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, w);
3331-
QRect ir = toolbutton->rect;
3332-
3333-
newBtn.rect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() / 2, mbi - 4, mbi - 4);
3334-
newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
3335-
proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
3345+
QStyleOptionToolButton newBtn = *toolbutton;
3346+
int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, w);
3347+
QFontMetrics metrics(toolbutton->font);
3348+
int fontWidth = metrics.horizontalAdvance(toolbutton->text);
3349+
int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
3350+
QRect subRect = QRect(tool.rect.left() + (tool.rect.width() - subRectWidth) / 2, tool.rect.top(), subRectWidth, tool.rect.height());
3351+
QRect indicatorRect = QRect(subRect.topLeft() + QPoint(fontWidth + menuButtonIndicatorMargin, (tool.rect.height() - mbi) / 2), QSize(mbi, mbi));
3352+
newBtn.rect = indicatorRect;
3353+
proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
33363354
}
33373355
}
33383356
return;

0 commit comments

Comments
 (0)