[Qt] XRadioButton is drawn at original radio button location

This commit is contained in:
Satori 2020-09-23 17:28:39 +01:00
parent 18dfdce88e
commit 761f3a4735
1 changed files with 42 additions and 33 deletions

View File

@ -9,8 +9,8 @@ namespace ui {
namespace qt { namespace qt {
XRadioButton::XRadioButton(QWidget* parent) XRadioButton::XRadioButton(QWidget* parent)
: Themeable<QRadioButton>("XRadioButton", parent) { : Themeable<QRadioButton>("XRadioButton", parent) {
setFocusPolicy(Qt::TabFocus); // disable retaining focus through mouse click setFocusPolicy(Qt::TabFocus); // disable retaining focus through mouse click
} }
void XRadioButton::paintEvent(QPaintEvent* e) { void XRadioButton::paintEvent(QPaintEvent* e) {
@ -20,14 +20,48 @@ void XRadioButton::paintEvent(QPaintEvent* e) {
// create rect for indicator box // create rect for indicator box
// rect must start at 1 as the painter draws either side of start offset so // rect must start at 1 as the painter draws either side of start offset so
// starting at (0,0) would leave 2 sides cut off // starting at (0,0) would leave 2 sides cut off
QRectF indicator_box = QRectF(1, 1, 16, 16); QRectF indicator_rect = style()->proxy()->subElementRect(
QStyle::SE_RadioButtonIndicator, &option,
this);
QRectF indicator_box =
QRectF(indicator_rect.x() + 1.0, indicator_rect.y() + 0.0, 16, 16);
// get original rect for radio button label // get original rect for radio button label
QRect label_rect = style()->proxy()->subElementRect( QRect label_rect = style()->proxy()->subElementRect(
QStyle::SE_RadioButtonContents, &option, this); QStyle::SE_RadioButtonContents, &option,
this);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing);
QPen pen(border_color_);
if (hasFocus()) {
pen.setColor(focus_color_);
}
pen.setJoinStyle(Qt::MiterJoin);
painter.setPen(pen);
painter.drawEllipse(indicator_box);
// paint checked inner box if radio button is checked
if (isChecked()) {
painter.setPen(Qt::NoPen);
QBrush checked_brush = QBrush(checked_color_);
QRectF checked_rect =
QRectF(indicator_box.x() + (indicator_box.width() * 0.125),
indicator_box.y() + (indicator_box.height() * 0.125),
indicator_box.width() * 0.75, indicator_box.height() * 0.75);
painter.setBrush(checked_brush);
painter.drawEllipse(checked_rect);
painter.setPen(pen);
}
QFont font = this->font(); QFont font = this->font();
painter.setFont(font);
QFontMetrics metrics(font); QFontMetrics metrics(font);
QRect font_rect = metrics.boundingRect(text()); QRect font_rect = metrics.boundingRect(text());
@ -38,32 +72,7 @@ void XRadioButton::paintEvent(QPaintEvent* e) {
label_rect.translate(label_indent_, 0); label_rect.translate(label_indent_, 0);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing);
QPen pen(border_color_);
if (hasFocus()) {
pen.setColor(focus_color_);
}
painter.setFont(font);
pen.setJoinStyle(Qt::MiterJoin);
painter.setPen(pen);
painter.drawEllipse(indicator_box.center(), 8, 8);
painter.drawText(label_rect, text()); painter.drawText(label_rect, text());
// paint checked inner box if radio button is checked
if (isChecked()) {
painter.setPen(Qt::transparent);
QBrush checked_brush = QBrush(checked_color_);
QRectF checked_rect = QRectF(3, 3, 12, 12);
painter.setBrush(checked_brush);
painter.drawEllipse(checked_rect.center(), 6, 6);
}
} }
QSize XRadioButton::sizeHint() const { QSize XRadioButton::sizeHint() const {
@ -74,6 +83,6 @@ QSize XRadioButton::sizeHint() const {
return QRadioButton::sizeHint() + QSize(label_indent_, 0); return QRadioButton::sizeHint() + QSize(label_indent_, 0);
} }
} // namespace qt } // namespace qt
} // namespace ui } // namespace ui
} // namespace xe } // namespace xe