Handle non-square checkboxes : adjust to make them square

Extension of 7452e29cec
This commit is contained in:
Jake Turner
2023-07-30 14:51:18 +01:00
parent e0bb46979f
commit 75d7e06eb5
+117 -166
View File
@@ -1291,6 +1291,93 @@ void RDStyle::drawComplexControl(ComplexControl control, const QStyleOptionCompl
return RDTweakedNativeStyle::drawComplexControl(control, opt, p, widget);
}
static void drawX(const QStyleOption *opt, QPainter *p, const QRectF &rect)
{
QPainterPath checkpath;
QPolygonF poly;
// Left side:
//
// X
// | CheckCornerSize
// X
// \\ innerSizeX, innerSizeY
// \\ (width and height)
// CheckHeight X
// /
// /
// X
// | CheckCornerSize
// X
// Top side:
//
// X---X X----X
// \\ /
// \\ /
// X
Q_ASSERT(rect.height() == rect.width());
const float innerSize = float(rect.height() - Constants::CheckCornerSize * 2) / 2.0f;
const float totalSize = innerSize * 2 + Constants::CheckCornerSize * 2;
Q_ASSERT(totalSize == rect.height());
// left edge
QPointF pt = rect.topLeft();
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() + innerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() - innerSize);
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
// bottom edge
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
// right edge
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
// top edge
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
checkpath.addPolygon(poly);
p->fillPath(checkpath, opt->palette.brush(QPalette::ButtonText));
}
void RDStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *opt, QPainter *p,
const QWidget *widget) const
{
@@ -1406,6 +1493,20 @@ void RDStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *opt, Q
else if(element == QStyle::PE_IndicatorViewItemCheck || element == QStyle::PE_IndicatorCheckBox)
{
QRect rect = opt->rect;
int w = rect.width();
int h = rect.height();
if(w < h)
{
int padding = (h - w) / 2;
rect.setHeight(w);
rect.adjust(0, padding, 0, padding);
}
else if(h < w)
{
int padding = (w - h) / 2;
rect.setWidth(h);
rect.adjust(padding, 0, padding, 0);
}
QPen outlinePen(outlineBrush(opt->palette), 1.0);
@@ -1425,89 +1526,7 @@ void RDStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *opt, Q
if(opt->state & State_On)
{
QPainterPath checkpath;
QPolygonF poly;
// Left side:
//
// X
// | CheckCornerSize
// X
// \\ innerSize
// \\ (width and height)
// CheckHeight X
// /
// /
// X
// | CheckCornerSize
// X
// Top side:
//
// X---X X----X
// \\ /
// \\ /
// X
Q_ASSERT(rect.height() == rect.width());
const float innerSize = float(rect.height() - Constants::CheckCornerSize * 2) / 2.0f;
const float totalSize = innerSize * 2 + Constants::CheckCornerSize * 2;
Q_ASSERT(totalSize == rect.height());
// left edge
QPointF pt = rect.topLeft();
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() + innerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() - innerSize);
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
// bottom edge
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
// right edge
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
// top edge
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
checkpath.addPolygon(poly);
p->fillPath(checkpath, opt->palette.brush(QPalette::ButtonText));
drawX(opt, p, rect);
}
else if(opt->state & State_NoChange)
{
@@ -1625,6 +1644,20 @@ void RDStyle::drawControl(ControlElement control, const QStyleOption *opt, QPain
if(checkbox)
{
QRectF rect = proxy()->subElementRect(SE_CheckBoxIndicator, opt, widget).adjusted(1, 1, -1, -1);
float w = rect.width();
float h = rect.height();
if(w < h)
{
int padding = (h - w) * 0.5;
rect.setHeight(w);
rect.adjust(0, padding, 0, padding);
}
else if(h < w)
{
int padding = (w - h) * 0.5;
rect.setWidth(h);
rect.adjust(padding, 0, padding, 0);
}
QPen outlinePen(outlineBrush(opt->palette), 1.0);
@@ -1650,89 +1683,7 @@ void RDStyle::drawControl(ControlElement control, const QStyleOption *opt, QPain
if(opt->state & State_On)
{
QPainterPath checkpath;
QPolygonF poly;
// Left side:
//
// X
// | CheckCornerSize
// X
// \\ innerSize
// \\ (width and height)
// CheckHeight X
// /
// /
// X
// | CheckCornerSize
// X
// Top side:
//
// X---X X----X
// \\ /
// \\ /
// X
Q_ASSERT(rect.height() == rect.width());
const float innerSize = float(rect.height() - Constants::CheckCornerSize * 2) / 2.0f;
const float totalSize = innerSize * 2 + Constants::CheckCornerSize * 2;
Q_ASSERT(totalSize == rect.height());
// left edge
QPointF pt = rect.topLeft();
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() + innerSize);
poly << pt;
pt.setY(pt.y() + innerSize);
pt.setX(pt.x() - innerSize);
poly << pt;
pt.setY(pt.y() + Constants::CheckCornerSize);
poly << pt;
// bottom edge
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() + Constants::CheckCornerSize);
poly << pt;
// right edge
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() + innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setY(pt.y() - Constants::CheckCornerSize);
poly << pt;
// top edge
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() + innerSize);
poly << pt;
pt.setX(pt.x() - innerSize);
pt.setY(pt.y() - innerSize);
poly << pt;
pt.setX(pt.x() - Constants::CheckCornerSize);
poly << pt;
checkpath.addPolygon(poly);
p->fillPath(checkpath, opt->palette.brush(QPalette::ButtonText));
drawX(opt, p, rect);
}
else if(opt->state & State_NoChange)
{