Skip to content

Commit

Permalink
Handle non-square checkboxes : adjust to make them square
Browse files Browse the repository at this point in the history
Extension of 7452e29
  • Loading branch information
Zorro666 committed Jul 30, 2023
1 parent e0bb469 commit 75d7e06
Showing 1 changed file with 117 additions and 166 deletions.
283 changes: 117 additions & 166 deletions qrenderdoc/Styles/RDStyle/RDStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand Down

0 comments on commit 75d7e06

Please sign in to comment.