1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
**/
#include "radioproxystyle.h"
#include <QStyleOptionComplex>
#include <QDebug>
#include <QBitmap>
#include <QPainter>
#include <QPixmap>
RadioProxystyle::RadioProxystyle(const QColor &color, QStyle *style) : mColor(color), QProxyStyle(style)
{
}
int RadioProxystyle::pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
{
switch (metric) {
case PM_ExclusiveIndicatorHeight:
return 24;
case PM_ExclusiveIndicatorWidth:
return 24;
default:
break;
}
return QProxyStyle::pixelMetric(metric, option, widget);
}
void RadioProxystyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
switch (element) {
case CE_RadioButton:
{
if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
QStyleOptionButton subopt = *button;
subopt.rect = proxy()->subElementRect(SE_RadioButtonIndicator, option, widget);
proxy()->drawPrimitive(PE_IndicatorRadioButton, &subopt, painter, widget);
subopt.rect = proxy()->subElementRect(SE_RadioButtonContents, option, widget);
proxy()->drawControl(CE_RadioButtonLabel, &subopt, painter, widget);
return;
}
break;
}
default:
{
}break;
}
QProxyStyle::drawControl(element, option, painter, widget);
}
void RadioProxystyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
switch (element) {
case PE_IndicatorRadioButton:
{
if (const QStyleOptionButton* radiobutton = qstyleoption_cast<const QStyleOptionButton*>(option)) {
QRectF rect = radiobutton->rect.adjusted(1, 1, -1, -1);
bool on = radiobutton->state & State_On;
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(mColor);
painter->setBrush(mColor);
painter->drawEllipse(rect);
if (on) {
QRectF childRect(rect.x(), rect.y(), (rect.width() / 2) - 2, (rect.height() / 2) - 2);
childRect.moveCenter(rect.center());
painter->setPen(Qt::NoPen);
painter->setBrush(radiobutton->palette.brush(QPalette::Active, QPalette::HighlightedText));
painter->drawEllipse(childRect);
}
painter->restore();
return;
}
break;
}
default:
{
}break;
}
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
QRect RadioProxystyle::subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const
{
switch (element) {
case SE_RadioButtonIndicator:
{
QRect rect;
int h = proxy()->pixelMetric(PM_ExclusiveIndicatorHeight, option, widget);
rect.setRect(option->rect.x(), option->rect.y() + ((option->rect.height() - h) / 2),
proxy()->pixelMetric(PM_ExclusiveIndicatorWidth, option, widget), h);
rect = visualRect(option->direction, option->rect, rect);
return rect;
}
case SE_RadioButtonClickRect:
{
return proxy()->subElementRect(SE_RadioButtonIndicator, option, widget);
}
default:
{
}break;
}
return QProxyStyle::subElementRect(element, option, widget);
}
QSize RadioProxystyle::sizeFromContents(QStyle::ContentsType element, const QStyleOption *option, const QSize &size, const QWidget *widget) const
{
QSize newSize = size;
switch (element) {
case CT_RadioButton:
{
if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
int w = proxy()->pixelMetric(PM_ExclusiveIndicatorWidth, option, widget);
int h = proxy()->pixelMetric(PM_ExclusiveIndicatorHeight, option, widget);
int spacing = proxy()->pixelMetric(PM_RadioButtonLabelSpacing, option, widget);
if (!button->icon.isNull())
spacing += 4;
newSize.setWidth(newSize.width() + w + spacing);
newSize.setHeight(qMax(qMax(newSize.height(), h), 36));
return newSize;
}
break;
}
default:
{
}break;
}
return QProxyStyle::sizeFromContents(element, option, size, widget);
}
RadioProxystyle::~RadioProxystyle()
{
}
|