From cd15947f47d68c015bf73883b51e266ad322c483 Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 24 Jul 2017 17:18:46 +0100 Subject: [PATCH] Do our own sizing of the controls we're painting --- qrenderdoc/Styles/RDStyle/RDStyle.cpp | 118 ++++++++++++++++++++++++++ qrenderdoc/Styles/RDStyle/RDStyle.h | 2 + 2 files changed, 120 insertions(+) diff --git a/qrenderdoc/Styles/RDStyle/RDStyle.cpp b/qrenderdoc/Styles/RDStyle/RDStyle.cpp index 7a68dbaaa..0b24095fb 100644 --- a/qrenderdoc/Styles/RDStyle/RDStyle.cpp +++ b/qrenderdoc/Styles/RDStyle/RDStyle.cpp @@ -32,6 +32,18 @@ #include #include "Code/QRDUtils.h" +namespace Constants +{ +static const int ButtonMargin = 6; +static const int ButtonBorder = 1; + +static const int HighlightBorder = 2; + +static const int CheckWidth = 14; +static const int CheckHeight = 14; +static const int CheckMargin = 3; +}; + RDStyle::RDStyle(ColorScheme scheme) : RDTweakedNativeStyle() { m_Scheme = scheme; @@ -140,14 +152,120 @@ void RDStyle::polish(QPalette &pal) } } +QRect RDStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, + const QWidget *widget) const +{ + if(cc == QStyle::CC_ToolButton) + { + int indicatorWidth = proxy()->pixelMetric(PM_MenuButtonIndicator, opt, widget); + + QRect ret = opt->rect; + + const QStyleOptionToolButton *toolbutton = qstyleoption_cast(opt); + + // return the normal rect if there's no menu + if(!(toolbutton->subControls & SC_ToolButtonMenu) && + !(toolbutton->features & QStyleOptionToolButton::HasMenu)) + { + return ret; + } + + if(sc == QStyle::SC_ToolButton) + { + ret.setRight(ret.right() - indicatorWidth); + } + else if(sc == QStyle::SC_ToolButtonMenu) + { + ret.setLeft(ret.right() - indicatorWidth); + } + + return ret; + } + return RDTweakedNativeStyle::subControlRect(cc, opt, sc, widget); +} + QRect RDStyle::subElementRect(SubElement element, const QStyleOption *opt, const QWidget *widget) const { + if(element == QStyle::SE_PushButtonContents || element == QStyle::SE_PushButtonFocusRect) + { + const int border = Constants::ButtonBorder; + return opt->rect.adjusted(border, border, -2 * border, -2 * border); + } + else if(element == QStyle::SE_RadioButtonFocusRect || element == QStyle::SE_CheckBoxFocusRect) + { + return opt->rect; + } + else if(element == QStyle::SE_RadioButtonIndicator || element == QStyle::SE_CheckBoxIndicator) + { + QRect ret = opt->rect; + + ret.setWidth(Constants::CheckWidth); + + int extra = ret.height() - Constants::CheckHeight; + + ret.setTop((ret.height() - Constants::CheckHeight) / 2); + ret.setHeight(Constants::CheckHeight); + + return ret; + } + else if(element == QStyle::SE_RadioButtonContents || element == QStyle::SE_CheckBoxContents) + { + QRect ret = opt->rect; + + ret.setLeft(ret.left() + Constants::CheckWidth + Constants::CheckMargin); + + return ret; + } + return RDTweakedNativeStyle::subElementRect(element, opt, widget); } QSize RDStyle::sizeFromContents(ContentsType type, const QStyleOption *opt, const QSize &size, const QWidget *widget) const { + if(type == CT_PushButton || type == CT_ToolButton) + { + const QStyleOptionButton *button = qstyleoption_cast(opt); + + QSize ret = size; + + // only for pushbuttons with text, ensure a minimum size + if(type == CT_PushButton && button && !button->text.isEmpty()) + { + ret.setWidth(qMax(50, ret.width())); + ret.setHeight(qMax(15, ret.height())); + } + + // add margin and border + ret.setHeight(ret.height() + Constants::ButtonMargin + Constants::ButtonBorder * 2); + ret.setWidth(ret.width() + Constants::ButtonMargin + Constants::ButtonBorder * 2); + + return ret; + } + else if(type == CT_CheckBox || type == CT_RadioButton) + { + const QStyleOptionButton *button = qstyleoption_cast(opt); + + QSize ret = size; + + // set minimum height for check/radio + ret.setHeight(qMax(ret.height(), Constants::CheckHeight) + Constants::HighlightBorder); + + // add width for the check/radio and a gap before the text/icon + ret.setWidth(Constants::CheckWidth + Constants::CheckMargin + ret.width()); + + return ret; + } + else if(type == CT_LineEdit) + { + QSize ret = size; + + ret.setWidth(Constants::ButtonBorder * 2 + ret.width()); + ret.setHeight(Constants::ButtonBorder * 2 + ret.height()); + + return ret; + } + return RDTweakedNativeStyle::sizeFromContents(type, opt, size, widget); } diff --git a/qrenderdoc/Styles/RDStyle/RDStyle.h b/qrenderdoc/Styles/RDStyle/RDStyle.h index 3b43b68f9..d150a2837 100644 --- a/qrenderdoc/Styles/RDStyle/RDStyle.h +++ b/qrenderdoc/Styles/RDStyle/RDStyle.h @@ -43,6 +43,8 @@ public: void polish(QPalette &pal) override; + QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, + const QWidget *widget = Q_NULLPTR) const override; QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const override; QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size,