| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "arthurstyle.h" |
| 5 |
#include "arthurwidgets.h" |
| 6 |
#include <QLayout> |
| 7 |
#include <QPainter> |
| 8 |
#include <QPainterPath> |
| 9 |
#include <QPixmapCache> |
| 10 |
#include <QRadioButton> |
| 11 |
#include <QString> |
| 12 |
#include <QStyleOption> |
| 13 |
#include <QtDebug> |
| 14 |
|
| 15 |
QPixmap cached(const QString &img) |
| 16 |
{ |
| 17 |
QPixmap pm; |
| 18 |
if (QPixmapCache::find(img, &pm)) |
| 19 |
return pm; |
| 20 |
|
| 21 |
pm = QPixmap::fromImage(QImage(img), Qt::OrderedDither | Qt::OrderedAlphaDither); |
| 22 |
if (pm.isNull()) |
| 23 |
return QPixmap(); |
| 24 |
|
| 25 |
QPixmapCache::insert(img, pm); |
| 26 |
return pm; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
ArthurStyle::ArthurStyle() |
| 31 |
: QCommonStyle() |
| 32 |
{ |
| 33 |
Q_INIT_RESOURCE(shared); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
void ArthurStyle::drawHoverRect(QPainter *painter, const QRect &r) const |
| 38 |
{ |
| 39 |
qreal h = r.height(); |
| 40 |
qreal h2 = r.height() / qreal(2); |
| 41 |
QPainterPath path; |
| 42 |
path.addRect(r.x() + h2, r.y() + 0, r.width() - h2 * 2, r.height()); |
| 43 |
path.addEllipse(r.x(), r.y(), h, h); |
| 44 |
path.addEllipse(r.x() + r.width() - h, r.y(), h, h); |
| 45 |
path.setFillRule(Qt::WindingFill); |
| 46 |
painter->setPen(Qt::NoPen); |
| 47 |
painter->setBrush(QColor(191, 215, 191)); |
| 48 |
painter->setRenderHint(QPainter::Antialiasing); |
| 49 |
painter->drawPath(path); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void ArthurStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, |
| 54 |
QPainter *painter, const QWidget *widget) const |
| 55 |
{ |
| 56 |
|
| 57 |
Q_ASSERT(option); |
| 58 |
switch (element) { |
| 59 |
case PE_FrameFocusRect: |
| 60 |
break; |
| 61 |
|
| 62 |
case PE_IndicatorRadioButton: |
| 63 |
if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) { |
| 64 |
bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver); |
| 65 |
painter->save(); |
| 66 |
QPixmap radio; |
| 67 |
if (hover) |
| 68 |
drawHoverRect(painter, widget->rect()); |
| 69 |
|
| 70 |
if (button->state & State_Sunken) |
| 71 |
radio = cached(":res/images/radiobutton-on.png"); |
| 72 |
else if (button->state & State_On) |
| 73 |
radio = cached(":res/images/radiobutton_on.png"); |
| 74 |
else |
| 75 |
radio = cached(":res/images/radiobutton_off.png"); |
| 76 |
painter->drawPixmap(button->rect.topLeft(), radio); |
| 77 |
|
| 78 |
painter->restore(); |
| 79 |
} |
| 80 |
break; |
| 81 |
|
| 82 |
case PE_PanelButtonCommand: |
| 83 |
if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) { |
| 84 |
bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver); |
| 85 |
|
| 86 |
painter->save(); |
| 87 |
const QPushButton *pushButton = qobject_cast<const QPushButton *>(widget); |
| 88 |
Q_ASSERT(pushButton); |
| 89 |
QWidget *parent = pushButton->parentWidget(); |
| 90 |
if (parent && qobject_cast<QGroupBox *>(parent)) { |
| 91 |
QLinearGradient lg(0, 0, 0, parent->height()); |
| 92 |
lg.setColorAt(0, QColor(224,224,224)); |
| 93 |
lg.setColorAt(1, QColor(255,255,255)); |
| 94 |
painter->setPen(Qt::NoPen); |
| 95 |
painter->setBrush(lg); |
| 96 |
painter->setBrushOrigin(-widget->mapToParent(QPoint(0,0))); |
| 97 |
painter->drawRect(button->rect); |
| 98 |
painter->setBrushOrigin(0,0); |
| 99 |
} |
| 100 |
|
| 101 |
bool down = (button->state & State_Sunken) || (button->state & State_On); |
| 102 |
|
| 103 |
QPixmap left, right, mid; |
| 104 |
if (down) { |
| 105 |
left = cached(":res/images/button_pressed_cap_left.png"); |
| 106 |
right = cached(":res/images/button_pressed_cap_right.png"); |
| 107 |
mid = cached(":res/images/button_pressed_stretch.png"); |
| 108 |
} else { |
| 109 |
left = cached(":res/images/button_normal_cap_left.png"); |
| 110 |
right = cached(":res/images/button_normal_cap_right.png"); |
| 111 |
mid = cached(":res/images/button_normal_stretch.png"); |
| 112 |
} |
| 113 |
painter->drawPixmap(button->rect.topLeft(), left); |
| 114 |
painter->drawTiledPixmap(QRect(button->rect.x() + left.width(), |
| 115 |
button->rect.y(), |
| 116 |
button->rect.width() - left.width() - right.width(), |
| 117 |
left.height()), |
| 118 |
mid); |
| 119 |
painter->drawPixmap(button->rect.x() + button->rect.width() - right.width(), |
| 120 |
button->rect.y(), |
| 121 |
right); |
| 122 |
if (hover) |
| 123 |
painter->fillRect(widget->rect().adjusted(3,5,-3,-5), QColor(31,127,31,63)); |
| 124 |
painter->restore(); |
| 125 |
} |
| 126 |
break; |
| 127 |
|
| 128 |
case PE_FrameGroupBox: |
| 129 |
if (const QStyleOptionFrame *group |
| 130 |
= qstyleoption_cast<const QStyleOptionFrame *>(option)) { |
| 131 |
const QRect &r = group->rect; |
| 132 |
|
| 133 |
painter->save(); |
| 134 |
int radius = 14; |
| 135 |
int radius2 = radius*2; |
| 136 |
QPainterPath clipPath; |
| 137 |
clipPath.moveTo(radius, 0); |
| 138 |
clipPath.arcTo(r.right() - radius2, 0, radius2, radius2, 90, -90); |
| 139 |
clipPath.arcTo(r.right() - radius2, r.bottom() - radius2, radius2, radius2, 0, -90); |
| 140 |
clipPath.arcTo(r.left(), r.bottom() - radius2, radius2, radius2, 270, -90); |
| 141 |
clipPath.arcTo(r.left(), r.top(), radius2, radius2, 180, -90); |
| 142 |
painter->setClipPath(clipPath); |
| 143 |
QPixmap titleStretch = cached(":res/images/title_stretch.png"); |
| 144 |
QPixmap topLeft = cached(":res/images/groupframe_topleft.png"); |
| 145 |
QPixmap topRight = cached(":res/images/groupframe_topright.png"); |
| 146 |
QPixmap bottomLeft = cached(":res/images/groupframe_bottom_left.png"); |
| 147 |
QPixmap bottomRight = cached(":res/images/groupframe_bottom_right.png"); |
| 148 |
QPixmap leftStretch = cached(":res/images/groupframe_left_stretch.png"); |
| 149 |
QPixmap topStretch = cached(":res/images/groupframe_top_stretch.png"); |
| 150 |
QPixmap rightStretch = cached(":res/images/groupframe_right_stretch.png"); |
| 151 |
QPixmap bottomStretch = cached(":res/images/groupframe_bottom_stretch.png"); |
| 152 |
QLinearGradient lg(0, 0, 0, r.height()); |
| 153 |
lg.setColorAt(0, QColor(224,224,224)); |
| 154 |
lg.setColorAt(1, QColor(255,255,255)); |
| 155 |
painter->setPen(Qt::NoPen); |
| 156 |
painter->setBrush(lg); |
| 157 |
painter->drawRect(r.adjusted(0, titleStretch.height()/2, 0, 0)); |
| 158 |
painter->setClipping(false); |
| 159 |
|
| 160 |
int topFrameOffset = titleStretch.height()/2 - 2; |
| 161 |
painter->drawPixmap(r.topLeft() + QPoint(0, topFrameOffset), topLeft); |
| 162 |
painter->drawPixmap(r.topRight() - QPoint(topRight.width()-1, 0) |
| 163 |
+ QPoint(0, topFrameOffset), topRight); |
| 164 |
painter->drawPixmap(r.bottomLeft() - QPoint(0, bottomLeft.height()-1), bottomLeft); |
| 165 |
painter->drawPixmap(r.bottomRight() - QPoint(bottomRight.width()-1, |
| 166 |
bottomRight.height()-1), bottomRight); |
| 167 |
|
| 168 |
QRect left = r; |
| 169 |
left.setY(r.y() + topLeft.height() + topFrameOffset); |
| 170 |
left.setWidth(leftStretch.width()); |
| 171 |
left.setHeight(r.height() - topLeft.height() - bottomLeft.height() - topFrameOffset); |
| 172 |
painter->drawTiledPixmap(left, leftStretch); |
| 173 |
|
| 174 |
QRect top = r; |
| 175 |
top.setX(r.x() + topLeft.width()); |
| 176 |
top.setY(r.y() + topFrameOffset); |
| 177 |
top.setWidth(r.width() - topLeft.width() - topRight.width()); |
| 178 |
top.setHeight(topLeft.height()); |
| 179 |
painter->drawTiledPixmap(top, topStretch); |
| 180 |
|
| 181 |
QRect right = r; |
| 182 |
right.setX(r.right() - rightStretch.width()+1); |
| 183 |
right.setY(r.y() + topRight.height() + topFrameOffset); |
| 184 |
right.setWidth(rightStretch.width()); |
| 185 |
right.setHeight(r.height() - topRight.height() |
| 186 |
- bottomRight.height() - topFrameOffset); |
| 187 |
painter->drawTiledPixmap(right, rightStretch); |
| 188 |
|
| 189 |
QRect bottom = r; |
| 190 |
bottom.setX(r.x() + bottomLeft.width()); |
| 191 |
bottom.setY(r.bottom() - bottomStretch.height()+1); |
| 192 |
bottom.setWidth(r.width() - bottomLeft.width() - bottomRight.width()); |
| 193 |
bottom.setHeight(bottomLeft.height()); |
| 194 |
painter->drawTiledPixmap(bottom, bottomStretch); |
| 195 |
painter->restore(); |
| 196 |
} |
| 197 |
break; |
| 198 |
|
| 199 |
default: |
| 200 |
QCommonStyle::drawPrimitive(element, option, painter, widget); |
| 201 |
break; |
| 202 |
} |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
void ArthurStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, |
| 208 |
QPainter *painter, const QWidget *widget) const |
| 209 |
{ |
| 210 |
switch (control) { |
| 211 |
case CC_Slider: |
| 212 |
if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option)) { |
| 213 |
QRect groove = subControlRect(CC_Slider, option, SC_SliderGroove, widget); |
| 214 |
QRect handle = subControlRect(CC_Slider, option, SC_SliderHandle, widget); |
| 215 |
|
| 216 |
painter->save(); |
| 217 |
|
| 218 |
bool hover = (slider->state & State_Enabled) && (slider->state & State_MouseOver); |
| 219 |
if (hover) { |
| 220 |
QRect moderated = widget->rect().adjusted(0, 4, 0, -4); |
| 221 |
drawHoverRect(painter, moderated); |
| 222 |
} |
| 223 |
|
| 224 |
if ((option->subControls & SC_SliderGroove) && groove.isValid()) { |
| 225 |
QPixmap grv = cached(":res/images/slider_bar.png"); |
| 226 |
painter->drawPixmap(QRect(groove.x() + 5, groove.y(), |
| 227 |
groove.width() - 10, grv.height()), |
| 228 |
grv); |
| 229 |
} |
| 230 |
if ((option->subControls & SC_SliderHandle) && handle.isValid()) { |
| 231 |
QPixmap hndl = cached(":res/images/slider_thumb_on.png"); |
| 232 |
painter->drawPixmap(handle.topLeft(), hndl); |
| 233 |
} |
| 234 |
|
| 235 |
painter->restore(); |
| 236 |
} |
| 237 |
break; |
| 238 |
case CC_GroupBox: |
| 239 |
if (const QStyleOptionGroupBox *groupBox |
| 240 |
= qstyleoption_cast<const QStyleOptionGroupBox *>(option)) { |
| 241 |
QStyleOptionGroupBox groupBoxCopy(*groupBox); |
| 242 |
groupBoxCopy.subControls &= ~SC_GroupBoxLabel; |
| 243 |
QCommonStyle::drawComplexControl(control, &groupBoxCopy, painter, widget); |
| 244 |
|
| 245 |
if (groupBox->subControls & SC_GroupBoxLabel) { |
| 246 |
const QRect &r = groupBox->rect; |
| 247 |
QPixmap titleLeft = cached(":res/images/title_cap_left.png"); |
| 248 |
QPixmap titleRight = cached(":res/images/title_cap_right.png"); |
| 249 |
QPixmap titleStretch = cached(":res/images/title_stretch.png"); |
| 250 |
int txt_width = groupBox->fontMetrics.horizontalAdvance(groupBox->text) + 20; |
| 251 |
painter->drawPixmap(r.center().x() - txt_width/2, 0, titleLeft); |
| 252 |
QRect tileRect = subControlRect(control, groupBox, SC_GroupBoxLabel, widget); |
| 253 |
painter->drawTiledPixmap(tileRect, titleStretch); |
| 254 |
painter->drawPixmap(tileRect.x() + tileRect.width(), 0, titleRight); |
| 255 |
int opacity = 31; |
| 256 |
painter->setPen(QColor(0, 0, 0, opacity)); |
| 257 |
painter->drawText(tileRect.translated(0, 1), |
| 258 |
Qt::AlignVCenter | Qt::AlignHCenter, groupBox->text); |
| 259 |
painter->drawText(tileRect.translated(2, 1), |
| 260 |
Qt::AlignVCenter | Qt::AlignHCenter, groupBox->text); |
| 261 |
painter->setPen(QColor(0, 0, 0, opacity * 2)); |
| 262 |
painter->drawText(tileRect.translated(1, 1), |
| 263 |
Qt::AlignVCenter | Qt::AlignHCenter, groupBox->text); |
| 264 |
painter->setPen(Qt::white); |
| 265 |
painter->drawText(tileRect, Qt::AlignVCenter | Qt::AlignHCenter, groupBox->text); |
| 266 |
} |
| 267 |
} |
| 268 |
break; |
| 269 |
default: |
| 270 |
QCommonStyle::drawComplexControl(control, option, painter, widget); |
| 271 |
break; |
| 272 |
} |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
void ArthurStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, |
| 277 |
QPainter *painter, const QWidget *widget) const |
| 278 |
{ |
| 279 |
switch (element) { |
| 280 |
case CE_RadioButtonLabel: |
| 281 |
if (const QStyleOptionButton *button |
| 282 |
= qstyleoption_cast<const QStyleOptionButton *>(option)) { |
| 283 |
|
| 284 |
if (button->text.isEmpty()) { |
| 285 |
QCommonStyle::drawControl(element, option, painter, widget); |
| 286 |
} else { |
| 287 |
painter->save(); |
| 288 |
painter->setPen(Qt::black); |
| 289 |
painter->drawText(button->rect, Qt::AlignVCenter, button->text); |
| 290 |
painter->restore(); |
| 291 |
} |
| 292 |
} |
| 293 |
break; |
| 294 |
case CE_PushButtonLabel: |
| 295 |
if (const QStyleOptionButton *button |
| 296 |
= qstyleoption_cast<const QStyleOptionButton *>(option)) { |
| 297 |
|
| 298 |
if (button->text.isEmpty()) { |
| 299 |
QCommonStyle::drawControl(element, option, painter, widget); |
| 300 |
} else { |
| 301 |
painter->save(); |
| 302 |
painter->setPen(Qt::black); |
| 303 |
painter->drawText(button->rect, Qt::AlignVCenter | Qt::AlignHCenter, button->text); |
| 304 |
painter->restore(); |
| 305 |
} |
| 306 |
} |
| 307 |
break; |
| 308 |
default: |
| 309 |
QCommonStyle::drawControl(element, option, painter, widget); |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
QRect ArthurStyle::subControlRect(ComplexControl control, const QStyleOptionComplex *option, |
| 315 |
SubControl subControl, const QWidget *widget) const |
| 316 |
{ |
| 317 |
QRect rect; |
| 318 |
|
| 319 |
switch (control) { |
| 320 |
default: |
| 321 |
rect = QCommonStyle::subControlRect(control, option, subControl, widget); |
| 322 |
break; |
| 323 |
case CC_GroupBox: |
| 324 |
if (const QStyleOptionGroupBox *group |
| 325 |
= qstyleoption_cast<const QStyleOptionGroupBox *>(option)) { |
| 326 |
switch (subControl) { |
| 327 |
default: |
| 328 |
rect = QCommonStyle::subControlRect(control, option, subControl, widget); |
| 329 |
break; |
| 330 |
case SC_GroupBoxContents: |
| 331 |
rect = QCommonStyle::subControlRect(control, option, subControl, widget); |
| 332 |
rect.adjust(0, -8, 0, 0); |
| 333 |
break; |
| 334 |
case SC_GroupBoxFrame: |
| 335 |
rect = group->rect; |
| 336 |
break; |
| 337 |
case SC_GroupBoxLabel: |
| 338 |
QPixmap titleLeft = cached(":res/images/title_cap_left.png"); |
| 339 |
QPixmap titleRight = cached(":res/images/title_cap_right.png"); |
| 340 |
QPixmap titleStretch = cached(":res/images/title_stretch.png"); |
| 341 |
int txt_width = group->fontMetrics.horizontalAdvance(group->text) + 20; |
| 342 |
rect = QRect(group->rect.center().x() - txt_width/2 + titleLeft.width(), 0, |
| 343 |
txt_width - titleLeft.width() - titleRight.width(), |
| 344 |
titleStretch.height()); |
| 345 |
break; |
| 346 |
} |
| 347 |
} |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
if (control == CC_Slider && subControl == SC_SliderHandle) { |
| 352 |
rect.setWidth(13); |
| 353 |
rect.setHeight(27); |
| 354 |
} else if (control == CC_Slider && subControl == SC_SliderGroove) { |
| 355 |
rect.setHeight(9); |
| 356 |
rect.moveTop(27/2 - 9/2); |
| 357 |
} |
| 358 |
return rect; |
| 359 |
} |
| 360 |
|
| 361 |
QSize ArthurStyle::sizeFromContents(ContentsType type, const QStyleOption *option, |
| 362 |
const QSize &size, const QWidget *widget) const |
| 363 |
{ |
| 364 |
QSize newSize = QCommonStyle::sizeFromContents(type, option, size, widget); |
| 365 |
|
| 366 |
|
| 367 |
switch (type) { |
| 368 |
case CT_RadioButton: |
| 369 |
newSize += QSize(20, 0); |
| 370 |
break; |
| 371 |
|
| 372 |
case CT_PushButton: |
| 373 |
newSize.setHeight(26); |
| 374 |
break; |
| 375 |
|
| 376 |
case CT_Slider: |
| 377 |
newSize.setHeight(27); |
| 378 |
break; |
| 379 |
|
| 380 |
default: |
| 381 |
break; |
| 382 |
} |
| 383 |
|
| 384 |
return newSize; |
| 385 |
} |
| 386 |
|
| 387 |
int ArthurStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QWidget *widget) const |
| 388 |
{ |
| 389 |
if (pm == PM_SliderLength) |
| 390 |
return 13; |
| 391 |
return QCommonStyle::pixelMetric(pm, opt, widget); |
| 392 |
} |
| 393 |
|
| 394 |
void ArthurStyle::polish(QWidget *widget) |
| 395 |
{ |
| 396 |
if (widget->layout() && qobject_cast<QGroupBox *>(widget)) { |
| 397 |
if (!widget->findChild<QGroupBox *>()) { |
| 398 |
widget->layout()->setSpacing(0); |
| 399 |
widget->layout()->setContentsMargins(12, 12, 12, 12); |
| 400 |
} else { |
| 401 |
widget->layout()->setContentsMargins(13, 13, 13, 13); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (qobject_cast<QPushButton *>(widget) |
| 406 |
|| qobject_cast<QRadioButton *>(widget) |
| 407 |
|| qobject_cast<QSlider *>(widget)) { |
| 408 |
widget->setAttribute(Qt::WA_Hover); |
| 409 |
} |
| 410 |
|
| 411 |
QPalette pal = widget->palette(); |
| 412 |
if (widget->isWindow()) { |
| 413 |
pal.setColor(QPalette::Window, QColor(241, 241, 241)); |
| 414 |
widget->setPalette(pal); |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
void ArthurStyle::unpolish(QWidget *widget) |
| 420 |
{ |
| 421 |
if (qobject_cast<QPushButton *>(widget) |
| 422 |
|| qobject_cast<QRadioButton *>(widget) |
| 423 |
|| qobject_cast<QSlider *>(widget)) { |
| 424 |
widget->setAttribute(Qt::WA_Hover, false); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void ArthurStyle::polish(QPalette &palette) |
| 429 |
{ |
| 430 |
palette.setColor(QPalette::Window, QColor(241, 241, 241)); |
| 431 |
} |
| 432 |
|
| 433 |
QRect ArthurStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const |
| 434 |
{ |
| 435 |
QRect r; |
| 436 |
switch(element) { |
| 437 |
case SE_RadioButtonClickRect: |
| 438 |
r = widget->rect(); |
| 439 |
break; |
| 440 |
case SE_RadioButtonContents: |
| 441 |
r = widget->rect().adjusted(20, 0, 0, 0); |
| 442 |
break; |
| 443 |
default: |
| 444 |
r = QCommonStyle::subElementRect(element, option, widget); |
| 445 |
break; |
| 446 |
} |
| 447 |
|
| 448 |
if (qobject_cast<const QRadioButton*>(widget)) |
| 449 |
r = r.adjusted(5, 0, -5, 0); |
| 450 |
|
| 451 |
return r; |
| 452 |
} |