Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The month button arrow in DCalendarWidget is out of range #216

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
p->setRenderHint(QPainter::Antialiasing);
p->setPen(Qt::NoPen);
p->setBrush(Qt::NoBrush);
int menuButtonIndicatorMargin = 4;

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

// 绘制外层背景色
int menuButtonIndicatorMargin = 4;

auto btn = *toolbutton;
if (btn.state & (QStyle::State_MouseOver))
btn.state &= ~ QStyle::State_MouseOver;
Expand Down Expand Up @@ -1532,7 +1533,19 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
p->setPen(getColor(toolbutton, DPalette::ButtonText));
}

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



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

//DelayedPopup 模式,箭头右居中, 加一个日历 月按钮箭头居中
if (w && w->objectName() == "qt_calendar_monthbutton") {
newBtn.rect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() / 2, mbi - 4, mbi - 4);
newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
//仅文字,DelayedPopup 模式,文字和箭头整体居中
if (toolbutton->features & QStyleOptionToolButton::PopupDelay && toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
QFontMetrics metrics(toolbutton->font);
int fontWidth = metrics.horizontalAdvance(toolbutton->text);
int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
QRect subRect = QRect(tool.rect.left() + (tool.rect.width() - subRectWidth) / 2, tool.rect.top(), subRectWidth, tool.rect.height());
QRect indicatorRect = QRect(subRect.topLeft() + QPoint(fontWidth + menuButtonIndicatorMargin, (tool.rect.height() - mbi) / 2), QSize(mbi, mbi));
newBtn.rect = indicatorRect;
}

proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
}

//日历 年按钮 特制
//日历 年按钮 特制 文字和箭头整体居中
if (w && w->objectName() == "qt_calendar_yearbutton") {
QStyleOptionToolButton newBtn = *toolbutton;
int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, w);
QRect ir = toolbutton->rect;

newBtn.rect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() / 2, mbi - 4, mbi - 4);
newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
QStyleOptionToolButton newBtn = *toolbutton;
int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, w);
QFontMetrics metrics(toolbutton->font);
int fontWidth = metrics.horizontalAdvance(toolbutton->text);
int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
QRect subRect = QRect(tool.rect.left() + (tool.rect.width() - subRectWidth) / 2, tool.rect.top(), subRectWidth, tool.rect.height());
QRect indicatorRect = QRect(subRect.topLeft() + QPoint(fontWidth + menuButtonIndicatorMargin, (tool.rect.height() - mbi) / 2), QSize(mbi, mbi));
newBtn.rect = indicatorRect;
proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, w);
}
}
return;
Expand Down
Loading