SILICON
Loading...
Searching...
No Matches
siliconStyle.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include "siliconStyle.hpp"
18
19#include <algorithm>
20#include <cmath>
21
22#include <QAbstractScrollArea>
23#include <QAbstractSpinBox>
24#include <QApplication>
25#include <QComboBox>
26#include <QLabel>
27#include <QLineEdit>
28#include <QPainter>
29#include <QScopeGuard>
30#include <QStyleOption>
31#include <QTabBar>
32#include <QTabWidget>
33
34#include <ui/shell/icons.hpp>
35#include <ui/shell/theme.hpp>
36
37namespace SILICON::ui {
38namespace {
40 // Fixed widget metrics belong to the style. Light/dark themes only supply colors.
41 constexpr int borderWidth = 2;
42 constexpr int buttonRadius = 12;
43 constexpr int inputRadius = 8;
44 constexpr int menuRadius = 8;
45 constexpr int menuItemRadius = 6;
46 constexpr int indicatorRadius = 4;
47 constexpr int shadowOffset = 4;
48 constexpr int buttonLift = 2;
49 constexpr int controlHeight = 34;
50 constexpr int buttonHeight = 42;
51 constexpr int iconSize = 16;
52 constexpr int indicatorSize = 16;
53 constexpr int scrollBarThickness = 18;
54 constexpr int scrollBarMinimum = 30;
55 constexpr int scrollBarRadius = 7;
56 constexpr int menuArrowWidth = 16;
57 constexpr int tabHeight = 30;
58 constexpr int tabRadius = 4;
59 constexpr int spacingUnit = 4;
60 constexpr int controlHorizontal = 10;
61 constexpr int controlVertical = 6;
62 constexpr int buttonPaddingHorizontal = 18;
63 constexpr int buttonPaddingVertical = 10;
64 constexpr int menuVertical = 8;
65 constexpr int menuMargin = 4;
66 constexpr int menuItemMargin = 2;
67 constexpr int toolbarSpacing = 4;
68 constexpr int contentMargin = 9;
69 constexpr int inputButtonWidth = menuArrowWidth + spacingUnit * 2;
70
71 QRectF alignedRect(const QPainter* p, const QRectF& rect)
72 {
73 // Snap outer edges in device space, including fractional DPR and translations.
74 // Rotated/sheared controls keep their vector geometry instead of losing it to a
75 // bbox.
76 const auto transform = p->deviceTransform();
77 if (transform.type() > QTransform::TxScale || !transform.isInvertible())
78 return rect;
79 auto device = transform.mapRect(rect);
80 device.setCoords(std::round(device.left()), std::round(device.top()),
81 std::round(device.right()), std::round(device.bottom()));
82 return transform.inverted().mapRect(device);
83 }
84
85 void rounded(QPainter* p, const QRectF& rect, const QColor& color, qreal radius)
86 {
87 if (rect.isEmpty())
88 return;
89 radius = std::max(qreal(0), std::min({radius, rect.width() / 2, rect.height() / 2}));
90 p->setPen(Qt::NoPen);
91 p->setBrush(color);
92 p->drawRoundedRect(rect, radius, radius);
93 }
94
95 void panel(QPainter* p, QRectF rect, const QColor& fill, qreal radius, int width = 0,
96 int shadow = 0)
97 {
98 p->save();
99 const auto guard = qScopeGuard([p] { p->restore(); });
100 p->setRenderHint(QPainter::Antialiasing);
101 rect = alignedRect(p, rect);
102 const auto border = width ? ThemeEngine::getColor(ColorToken::Ink) : fill;
103 if (shadow > 0) {
104 rect.adjust(0, 0, -shadow, -shadow);
105 rounded(p, rect.translated(shadow, shadow), border, radius);
106 }
107 // Filled nested shapes keep outlines inside the allocated geometry at any DPR.
108 rounded(p, rect, border, radius);
109 rounded(p, rect.adjusted(width, width, -width, -width), fill,
110 std::max(qreal(0), radius - width));
111 }
112
113 // Remember only properties changed by polish(), so switching styles is reversible.
114 constexpr auto originalFontProperty = "_siliconOriginalFont";
115 constexpr auto installedFontProperty = "_siliconInstalledFont";
116 constexpr auto hoverProperty = "_siliconOriginalHover";
117
118 const QColor& foreground(QStyle::State state)
119 {
120 return ThemeEngine::getColor(state & QStyle::State_Enabled ? ColorToken::Ink
121 : ColorToken::Disabled);
122 }
123
124 bool hovered(QStyle::State state)
125 {
126 return (state & QStyle::State_Enabled) && (state & QStyle::State_MouseOver)
127 && !(state & QStyle::State_Sunken);
128 }
129
130 const QColor& buttonBackground(QStyle::State state)
131 {
132 if (!(state & QStyle::State_Enabled))
133 return ThemeEngine::getColor(ColorToken::Surface);
134 if (state & QStyle::State_On)
135 return ThemeEngine::getColor(ColorToken::Selected);
136 return ThemeEngine::getColor(hovered(state) ? ColorToken::Hover
137 : ColorToken::Surface);
138 }
139
140 const QColor& tabBackground(QStyle::State state)
141 {
142 if ((state & QStyle::State_Enabled) && (state & QStyle::State_Selected))
143 return ThemeEngine::getColor(ColorToken::Surface);
144 return ThemeEngine::getColor(hovered(state) ? ColorToken::Hover
145 : ColorToken::Background);
146 }
147
148 const QColor& scrollThumbColor(QStyle::State state)
149 {
150 if (!(state & QStyle::State_Enabled))
151 return ThemeEngine::getColor(ColorToken::Disabled);
152 return ThemeEngine::getColor(state & (QStyle::State_MouseOver | QStyle::State_Sunken)
153 ? ColorToken::Ink
154 : ColorToken::Grey);
155 }
156
157 QColor menuBackground(QStyle::State state, QColor fallback = Qt::transparent)
158 {
159 // Menus use selection for keyboard navigation and hover/press for pointer input.
160 constexpr auto highlightStates =
161 QStyle::State_Selected | QStyle::State_MouseOver | QStyle::State_Sunken;
162 if ((state & QStyle::State_Enabled) && (state & highlightStates))
163 return ThemeEngine::getColor(ColorToken::MenuHover);
164 return fallback;
165 }
166
167 bool hasClass(const QWidget* widget, const QString& name)
168 {
169 return widget && widget->property("class").toString().split(' ').contains(name);
170 }
171
172 void textPalette(QPalette& palette, const QColor& color)
173 {
174 for (const auto role : {QPalette::WindowText, QPalette::ButtonText, QPalette::Text})
175 palette.setColor(QPalette::All, role, color);
176 }
177
178 QRect inset(QRect rect, int horizontal, int vertical)
179 {
180 return rect.adjusted(horizontal, vertical, -horizontal, -vertical);
181 }
182
183 int buttonOffset(QStyle::State state)
184 {
185 if (!(state & QStyle::State_Enabled))
186 return 0;
187 if (state & QStyle::State_Sunken)
188 return buttonLift;
189 return state & QStyle::State_MouseOver ? -buttonLift : 0;
190 }
191
192 QRect buttonFaceRect(const QStyleOption& option)
193 {
194 // Painting and label layout share the same lifted/sunken face.
195 const int offset = buttonOffset(option.state);
196 return option.rect.adjusted(buttonLift, buttonLift, -shadowOffset, -shadowOffset)
197 .translated(offset, offset);
198 }
199
200 QColor separatorColor()
201 {
202 QColor color = ThemeEngine::getColor(ColorToken::Ink);
203 color.setAlphaF(0.14);
204 return color;
205 }
206
207 bool framelessLineEdit(const QStyleOption* option)
208 {
209 const auto* frame = qstyleoption_cast<const QStyleOptionFrame*>(option);
210 return frame && frame->lineWidth == 0;
211 }
212
213 void inputPanel(QPainter* p, const QRect& rect)
214 {
215 panel(p, rect, ThemeEngine::getColor(ColorToken::Surface), inputRadius, borderWidth);
216 }
217
218 int inputVerticalPadding(const QStyleOption& option, int border)
219 {
220 // Existing compact fields can be explicitly shorter than their size hint.
221 // Reduce padding before reducing the text's available height.
222 const int available =
223 std::max(0, option.rect.height() - option.fontMetrics.height() - 2 * border);
224 return std::min(controlVertical, available / 2);
225 }
226
227 struct InputGeometry {
228 QRect editField;
229 QRect buttons;
230 };
231
232 InputGeometry inputGeometry(const QStyleOption& option, bool frame, int buttonWidth)
233 {
234 const QRect r = option.rect;
235 const int border = frame ? borderWidth : 0;
236 const int vertical = inputVerticalPadding(option, border);
237 const QRect edit =
238 r.adjusted(border + controlHorizontal, border + vertical,
239 -buttonWidth - border - spacingUnit, -border - vertical);
240 const QRect buttons(r.right() + 1 - buttonWidth - border, r.top() + border,
241 buttonWidth, std::max(0, r.height() - border * 2));
242 // Lay out once left-to-right, then mirror both regions for RTL widgets.
243 return {.editField = QStyle::visualRect(option.direction, r, edit),
244 .buttons = QStyle::visualRect(option.direction, r, buttons)};
245 }
246
247 bool verticalTab(QTabBar::Shape shape)
248 {
249 return shape == QTabBar::RoundedWest || shape == QTabBar::RoundedEast
250 || shape == QTabBar::TriangularWest || shape == QTabBar::TriangularEast;
251 }
252
253 QStyle::State subControlState(const QStyleOptionComplex& option,
254 QStyle::SubControl control)
255 {
256 // The parent can be hovered/pressed while a different subcontrol is active.
257 auto state = option.state;
258 if (!(option.activeSubControls & control))
259 state &= ~(QStyle::State_MouseOver | QStyle::State_Sunken);
260 return state;
261 }
262
263 void drawSpinButton(const QStyle& style, QPainter* p, const QStyleOptionSpinBox& spin,
264 QStyle::SubControl control, const QRect& rect)
265 {
266 const bool up = control == QStyle::SC_SpinBoxUp;
267 QStyleOption button;
268 button.rect = rect;
269 button.state = subControlState(spin, control);
270 button.palette = spin.palette;
271 // Each direction has its own step limit, independent of the spinbox's state.
272 const auto step =
273 up ? QAbstractSpinBox::StepUpEnabled : QAbstractSpinBox::StepDownEnabled;
274 if (!(spin.stepEnabled & step))
275 button.state &= ~QStyle::State_Enabled;
276
277 if (button.state & (QStyle::State_MouseOver | QStyle::State_Sunken))
278 panel(p, rect, buttonBackground(button.state), indicatorRadius);
279 if (spin.buttonSymbols != QAbstractSpinBox::PlusMinus) {
280 if (!(button.state & QStyle::State_Enabled))
281 button.palette.setCurrentColorGroup(QPalette::Disabled);
282
283 style.drawPrimitive(
284 up ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, &button, p);
285 return;
286 }
287 p->setPen(QPen(foreground(button.state), borderWidth));
288 const auto center = rect.center();
289 p->drawLine(center - QPoint(spacingUnit, 0), center + QPoint(spacingUnit, 0));
290 if (up)
291 p->drawLine(center - QPoint(0, spacingUnit), center + QPoint(0, spacingUnit));
292 }
293
294 struct ScrollGeometry {
295 QRect groove;
296 QRect slider;
297 QRect subPage;
298 QRect addPage;
299 };
300
301 ScrollGeometry scrollGeometry(const QStyleOptionSlider& option)
302 {
303 const bool horizontal = option.orientation == Qt::Horizontal;
304 const QRect r = option.rect;
305 const int length = std::max(0, horizontal ? r.width() : r.height());
306 // Widen before subtraction and multiplication: Qt allows the full int range,
307 // and page/position products can overflow even when the range fits in an int.
308 const qint64 range = std::max(qint64(0), qint64(option.maximum) - option.minimum);
309 const qint64 page = std::max(0, option.pageStep);
310 const int thumb = range == 0 ? length
311 : std::clamp(int(page * length / (range + page)),
312 std::min(length, scrollBarMinimum), length);
313 const qint64 value =
314 std::clamp(qint64(option.sliderPosition) - option.minimum, qint64(0), range);
315 int offset = range == 0 ? 0 : int(value * (length - thumb) / range);
316
317 if (option.upsideDown)
318 offset = length - thumb - offset;
319 const auto section = [&](int start, int size) {
320 const QRect logical = horizontal ? QRect(r.x() + start, r.y(), size, r.height())
321 : QRect(r.x(), r.y() + start, r.width(), size);
322 return QStyle::visualRect(option.direction, r, logical);
323 };
324 const QRect before = section(0, offset);
325 const QRect after = section(offset + thumb, length - offset - thumb);
326 return {.groove = r,
327 .slider = section(offset, thumb),
328 .subPage = option.upsideDown ? after : before,
329 .addPage = option.upsideDown ? before : after};
330 }
331} // namespace
332
333SiliconStyle::SiliconStyle(QObject* parent) : QProxyStyle(QStringLiteral("Fusion"))
334{
335 setParent(parent);
336 setObjectName("SiliconStyle");
337}
338
340{
341 return ThemeEngine::current().palette();
342}
343
344// Override custom surfaces and geometry; delegate other elements to Fusion.
345// Each drawing entry point restores painter state, including on early returns.
346void SiliconStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option,
347 QPainter* p, const QWidget* widget) const
348{
349 if (!option || !p)
350 return;
351 p->save();
352 const auto guard = qScopeGuard([p] { p->restore(); });
353
354 switch (element) {
355 case PE_PanelButtonCommand: {
356 const bool pressed = (option->state & (State_Enabled | State_Sunken))
357 == (State_Enabled | State_Sunken);
358 const auto face = buttonFaceRect(*option);
359 const int shadow = pressed ? 0 : shadowOffset - buttonOffset(option->state);
360 panel(p, face.adjusted(0, 0, shadow, shadow), buttonBackground(option->state),
361 buttonRadius, borderWidth, shadow);
362 return;
363 }
364 case PE_PanelButtonTool: {
365 panel(p, option->rect, buttonBackground(option->state), indicatorRadius);
366 return;
367 }
368 case PE_PanelToolBar:
369 p->fillRect(option->rect, ThemeEngine::getColor(ColorToken::Background));
370 return;
371 case PE_IndicatorToolBarSeparator: {
372 QRect line = inset(option->rect, spacingUnit, spacingUnit);
373 if (option->state & State_Horizontal)
374 line = QRect(option->rect.center().x(), line.top(), 1, line.height());
375 else
376 line = QRect(line.left(), option->rect.center().y(), line.width(), 1);
377 p->fillRect(line, separatorColor());
378 return;
379 }
380 case PE_PanelLineEdit:
381 case PE_FrameLineEdit: {
382 // With lineWidth == 0 the edit is deliberately frameless (e.g. inside a spinbox).
383 if (framelessLineEdit(option)) {
384 if (element == PE_PanelLineEdit)
385 p->fillRect(option->rect, option->palette.brush(QPalette::Base));
386 return;
387 }
388
389 inputPanel(p, option->rect);
390 return;
391 }
392 case PE_Frame:
393 // Views and editors already sit inside docks/pages. Do not frame every
394 // nested viewport or draw another box when it receives keyboard focus.
395 if (qobject_cast<const QAbstractScrollArea*>(widget))
396 return;
397 break;
398 case PE_FrameTabWidget:
399 case PE_FrameTabBarBase:
400 case PE_FrameDockWidget:
401 case PE_FrameFocusRect: return;
402 case PE_PanelMenu:
403 panel(p, option->rect, ThemeEngine::getColor(ColorToken::SurfaceRaised), menuRadius,
404 borderWidth, shadowOffset);
405 return;
406 case PE_FrameMenu: return; // PE_PanelMenu draws the complete border and shadow.
407 case PE_IndicatorCheckBox:
408 case PE_IndicatorRadioButton:
409 case PE_IndicatorItemViewItemCheck: {
410 const bool radio = element == PE_IndicatorRadioButton;
411 const auto rect = QStyle::alignedRect(
412 option->direction, Qt::AlignCenter,
413 QSize(indicatorSize, indicatorSize).boundedTo(option->rect.size()),
414 option->rect);
415 panel(p, rect, buttonBackground(option->state),
416 radio ? rect.width() / 2.0 : indicatorRadius, borderWidth);
417 const QRectF r = QRectF(rect).adjusted(borderWidth + 2, borderWidth + 2,
418 -borderWidth - 2, -borderWidth - 2);
419 p->setRenderHint(QPainter::Antialiasing);
420 p->setPen(QPen(foreground(option->state), borderWidth, Qt::SolidLine, Qt::SquareCap,
421 Qt::MiterJoin));
422 if (option->state & State_NoChange) {
423 p->drawLine(QPointF(r.left(), r.center().y()),
424 QPointF(r.right(), r.center().y()));
425 return;
426 }
427 if (!(option->state & State_On))
428 return;
429 if (radio) {
430 p->setPen(Qt::NoPen);
431 p->setBrush(foreground(option->state));
432 p->drawEllipse(r);
433 return;
434 }
435 const QPointF points[]{QPointF(r.left(), r.center().y()),
436 QPointF(r.center().x() - 1, r.bottom()), r.topRight()};
437 p->drawPolyline(points, 3);
438 return;
439 }
440 case PE_IndicatorTabClose: {
441 if (option->state & State_MouseOver)
442 panel(p, option->rect, buttonBackground(option->state), indicatorRadius);
443 const auto r = inset(option->rect, spacingUnit, spacingUnit);
444 p->setRenderHint(QPainter::Antialiasing);
445 p->setPen(QPen(foreground(option->state), borderWidth));
446 p->drawLine(r.topLeft(), r.bottomRight());
447 p->drawLine(r.topRight(), r.bottomLeft());
448 return;
449 }
450 default: break;
451 }
452 QProxyStyle::drawPrimitive(element, option, p, widget);
453}
454
455void SiliconStyle::drawControl(ControlElement element, const QStyleOption* option,
456 QPainter* p, const QWidget* widget) const
457{
458 if (!option || !p)
459 return;
460 p->save();
461 const auto guard = qScopeGuard([p] { p->restore(); });
462
463 switch (element) {
464 case CE_PushButton: {
465 const auto* button = qstyleoption_cast<const QStyleOptionButton*>(option);
466 if (!button)
467 break;
468 // Resolve contents through this style so the label follows the bevel
469 // when the button lifts on hover or sinks on press.
470 drawControl(CE_PushButtonBevel, button, p, widget);
471 auto label = *button;
472 label.rect = subElementRect(SE_PushButtonContents, button, widget);
473 drawControl(CE_PushButtonLabel, &label, p, widget);
474 return;
475 }
476 case CE_RubberBand: {
477 if (!qstyleoption_cast<const QStyleOptionRubberBand*>(option))
478 break;
479 // The diagram marquee is a translucent selection surface. QCommonStyle's
480 // patterned rubber band obscures the circuit instead of tinting it.
481 auto fill = option->palette.color(QPalette::Active, QPalette::Highlight);
482 auto border = fill.darker(120);
483 fill.setAlpha(80);
484 border.setAlpha(180);
485 p->setRenderHint(QPainter::Antialiasing);
486 p->setPen(QPen(border, 1));
487 p->setBrush(fill);
488 p->drawRoundedRect(QRectF(option->rect).adjusted(0.5, 0.5, -0.5, -0.5), 1, 1);
489 return;
490 }
491 case CE_ShapedFrame:
492 if (qobject_cast<const QAbstractScrollArea*>(widget))
493 return;
494 break;
495 case CE_ToolBar:
496 case CE_MenuBarEmptyArea:
497 p->fillRect(option->rect, ThemeEngine::getColor(ColorToken::Background));
498 return;
499 case CE_HeaderSection:
500 case CE_HeaderEmptyArea: {
501 p->fillRect(option->rect, ThemeEngine::getColor(ColorToken::SurfaceRaised));
502 p->fillRect(
503 QRect(option->rect.left(), option->rect.bottom(), option->rect.width(), 1),
504 separatorColor());
505 return;
506 }
507 case CE_MenuItem: {
508 const auto* menu = qstyleoption_cast<const QStyleOptionMenuItem*>(option);
509 if (!menu)
510 break;
511 p->fillRect(menu->rect, ThemeEngine::getColor(ColorToken::SurfaceRaised));
512 if (menu->menuItemType != QStyleOptionMenuItem::Separator)
513 panel(p, inset(menu->rect, menuItemMargin, menuItemMargin),
514 menuBackground(menu->state), menuItemRadius);
515 auto item = *menu;
516 // Keep Fusion's text, icons, shortcuts and check indicators over our highlight.
517 item.palette.setColor(QPalette::All, QPalette::Window, Qt::transparent);
518 item.palette.setColor(QPalette::All, QPalette::Highlight, Qt::transparent);
519 QProxyStyle::drawControl(element, &item, p, widget);
520 return;
521 }
522 case CE_MenuBarItem: {
523 const auto* menu = qstyleoption_cast<const QStyleOptionMenuItem*>(option);
524 if (!menu)
525 break;
526 p->fillRect(option->rect, ThemeEngine::getColor(ColorToken::Background));
527 panel(p, option->rect,
528 menuBackground(option->state, ThemeEngine::getColor(ColorToken::Background)),
529 indicatorRadius);
530 auto label = *menu;
531 textPalette(label.palette, foreground(option->state));
532 // Fusion also paints a bevel here; only delegate the menu's label.
533 QCommonStyle::drawControl(element, &label, p, widget);
534 return;
535 }
536 case CE_TabBarTabShape: {
537 const auto* tab = qstyleoption_cast<const QStyleOptionTab*>(option);
538 if (!tab)
539 break;
540 panel(p, option->rect, tabBackground(option->state), tabRadius);
541 if (!(option->state & State_Selected))
542 return;
543 QRect line = option->rect;
544 switch (tab->shape) {
545 case QTabBar::RoundedSouth:
546 case QTabBar::TriangularSouth: line.setHeight(borderWidth); break;
547 case QTabBar::RoundedWest:
548 case QTabBar::TriangularWest: line.setLeft(line.right() - borderWidth + 1); break;
549 case QTabBar::RoundedEast:
550 case QTabBar::TriangularEast: line.setWidth(borderWidth); break;
551 default: line.setTop(line.bottom() - borderWidth + 1); break;
552 }
553 p->fillRect(line, foreground(option->state));
554 return;
555 }
556 default: break;
557 }
558 QProxyStyle::drawControl(element, option, p, widget);
559}
560
561void SiliconStyle::drawComplexControl(ComplexControl control,
562 const QStyleOptionComplex* option, QPainter* p,
563 const QWidget* widget) const
564{
565 if (!option || !p)
566 return;
567 p->save();
568 const auto guard = qScopeGuard([p] { p->restore(); });
569
570 switch (control) {
571 case CC_ComboBox: {
572 const auto* combo = qstyleoption_cast<const QStyleOptionComboBox*>(option);
573 if (!combo)
574 break;
575 if (combo->frame)
576 inputPanel(p, combo->rect);
577 if (combo->subControls & SC_ComboBoxArrow) {
578 QStyleOption indicator = *combo;
579 indicator.rect = subControlRect(control, combo, SC_ComboBoxArrow, widget);
580 drawPrimitive(PE_IndicatorArrowDown, &indicator, p, widget);
581 }
582 return;
583 }
584 case CC_SpinBox: {
585 const auto* spin = qstyleoption_cast<const QStyleOptionSpinBox*>(option);
586 if (!spin)
587 break;
588 if (spin->frame)
589 inputPanel(p, spin->rect);
590 if (spin->buttonSymbols == QAbstractSpinBox::NoButtons)
591 return;
592 for (const auto sc : {SC_SpinBoxUp, SC_SpinBoxDown}) {
593 if (!(spin->subControls & sc))
594 continue;
595 drawSpinButton(*this, p, *spin, sc, subControlRect(control, spin, sc, widget));
596 }
597 return;
598 }
599 case CC_ToolButton: {
600 const auto* tool = qstyleoption_cast<const QStyleOptionToolButton*>(option);
601 if (!tool)
602 break;
603 auto button = *tool;
604 if (!(button.state & State_AutoRaise) || button.state & State_MouseOver)
605 button.state |= State_Raised;
606 QProxyStyle::drawComplexControl(control, &button, p, widget);
607 return;
608 }
609 case CC_ScrollBar: {
610 const auto* scroll = qstyleoption_cast<const QStyleOptionSlider*>(option);
611 if (!scroll)
612 break;
613 const bool horizontal = scroll->orientation == Qt::Horizontal;
614 const auto r = scroll->rect;
615 p->fillRect(r, ThemeEngine::getColor(ColorToken::Background));
616 if (!(scroll->subControls & SC_ScrollBarSlider))
617 return;
618 const auto fill = scrollThumbColor(subControlState(*scroll, SC_ScrollBarSlider));
619 const auto handle = subControlRect(control, scroll, SC_ScrollBarSlider, widget);
620 panel(p, inset(handle, horizontal ? 2 : 3, horizontal ? 3 : 2), fill,
621 scrollBarRadius);
622 return;
623 }
624 default: break;
625 }
626 QProxyStyle::drawComplexControl(control, option, p, widget);
627}
628
629QRect SiliconStyle::subElementRect(SubElement element, const QStyleOption* option,
630 const QWidget* widget) const
631{
632 if (!option)
633 return {};
634
635 switch (element) {
636 case SE_PushButtonContents: {
637 return inset(buttonFaceRect(*option), buttonPaddingHorizontal + borderWidth,
638 buttonPaddingVertical + borderWidth);
639 }
640 case SE_LineEditContents:
641 if (framelessLineEdit(option))
642 return option->rect; // Embedded combo/spin edits already have parent padding.
643 return inset(option->rect, controlHorizontal + borderWidth,
644 inputVerticalPadding(*option, borderWidth) + borderWidth);
645 default: return QProxyStyle::subElementRect(element, option, widget);
646 }
647}
648
649QRect SiliconStyle::subControlRect(ComplexControl control,
650 const QStyleOptionComplex* option, SubControl sub,
651 const QWidget* widget) const
652{
653 if (!option)
654 return {};
655
656 const QRect r = option->rect;
657 switch (control) {
658 case CC_ComboBox: {
659 const auto* combo = qstyleoption_cast<const QStyleOptionComboBox*>(option);
660 if (!combo)
661 break;
662 const auto geometry = inputGeometry(*combo, combo->frame, inputButtonWidth);
663 switch (sub) {
664 case SC_ComboBoxArrow: return geometry.buttons;
665 case SC_ComboBoxEditField: return geometry.editField;
666 case SC_ComboBoxFrame:
667 case SC_ComboBoxListBoxPopup: return r;
668 default: return {};
669 }
670 }
671 case CC_SpinBox: {
672 const auto* spin = qstyleoption_cast<const QStyleOptionSpinBox*>(option);
673 if (!spin)
674 break;
675 const int buttons =
676 spin->buttonSymbols == QAbstractSpinBox::NoButtons ? 0 : inputButtonWidth;
677 const auto geometry = inputGeometry(*spin, spin->frame, buttons);
678 const QRect column = geometry.buttons;
679 switch (sub) {
680 case SC_SpinBoxFrame: return r;
681 case SC_SpinBoxEditField: return geometry.editField;
682 case SC_SpinBoxUp:
683 return QRect(column.x(), column.y(), buttons, column.height() / 2);
684 case SC_SpinBoxDown:
685 return QRect(column.x(), column.y() + column.height() / 2, buttons,
686 column.height() - column.height() / 2);
687 default: return {};
688 }
689 }
690 case CC_ScrollBar: {
691 const auto* scroll = qstyleoption_cast<const QStyleOptionSlider*>(option);
692 if (!scroll)
693 break;
694 const auto geometry = scrollGeometry(*scroll);
695 switch (sub) {
696 case SC_ScrollBarGroove: return geometry.groove;
697 case SC_ScrollBarSlider: return geometry.slider;
698 case SC_ScrollBarSubPage: return geometry.subPage;
699 case SC_ScrollBarAddPage: return geometry.addPage;
700 default: return {};
701 }
702 }
703 default: break;
704 }
705 return QProxyStyle::subControlRect(control, option, sub, widget);
706}
707
708QStyle::SubControl SiliconStyle::hitTestComplexControl(ComplexControl control,
709 const QStyleOptionComplex* option,
710 const QPoint& position,
711 const QWidget* widget) const
712{
713 if (!option || !option->rect.contains(position))
714 return SC_None;
715 // Hit testing must use the same geometry as painting.
716 const auto hit = QProxyStyle::hitTestComplexControl(control, option, position, widget);
717 // Qt tests the spinbox frame before the edit field. Give the embedded edit priority
718 // after Qt has ruled out the step buttons.
719 if (control == CC_SpinBox && hit == SC_SpinBoxFrame
720 && subControlRect(control, option, SC_SpinBoxEditField, widget).contains(position))
721 return SC_SpinBoxEditField;
722 return hit;
723}
724
725QSize SiliconStyle::sizeFromContents(ContentsType type, const QStyleOption* option,
726 const QSize& size, const QWidget* widget) const
727{
728 switch (type) {
729 case CT_CheckBox:
730 case CT_RadioButton:
731 return QCommonStyle::sizeFromContents(type, option, size, widget);
732 case CT_MenuBarItem: return size + QSize(controlHorizontal * 2, spacingUnit * 2);
733 case CT_PushButton: {
734 QSize result =
735 size
736 + QSize(2 * (buttonPaddingHorizontal + borderWidth) + shadowOffset + buttonLift,
737 2 * (buttonPaddingVertical + borderWidth) + shadowOffset + buttonLift);
738 if (const auto* button = qstyleoption_cast<const QStyleOptionButton*>(option);
739 button && button->features & QStyleOptionButton::HasMenu)
740 result.rwidth() += menuArrowWidth;
741 return result.expandedTo(QSize(0, buttonHeight));
742 }
743 case CT_ToolButton: {
744 QSize result =
745 size
746 + QSize(spacingUnit * 2 + borderWidth * 2, spacingUnit * 2 + borderWidth * 2);
747 if (const auto* tool = qstyleoption_cast<const QStyleOptionToolButton*>(option);
748 tool && tool->features & QStyleOptionToolButton::MenuButtonPopup)
749 result.rwidth() += menuArrowWidth;
750 return result;
751 }
752 case CT_LineEdit:
753 case CT_ComboBox:
754 case CT_SpinBox: {
755 // A spinbox uses its embedded edit's height as content size.
756 if (type == CT_LineEdit && framelessLineEdit(option))
757 return size;
758 QSize result = size
759 + QSize(2 * (controlHorizontal + borderWidth),
760 2 * (controlVertical + borderWidth));
761 if (type != CT_LineEdit)
762 result.rwidth() += inputButtonWidth;
763 return result.expandedTo(QSize(0, controlHeight));
764 }
765 case CT_MenuItem: {
766 const auto native = QProxyStyle::sizeFromContents(type, option, size, widget);
767 const auto* menu = qstyleoption_cast<const QStyleOptionMenuItem*>(option);
768 const bool separator =
769 menu && menu->menuItemType == QStyleOptionMenuItem::Separator;
770 return native
771 + QSize(spacingUnit * 2, separator ? spacingUnit * 2 : menuVertical * 2);
772 }
773 case CT_TabBarTab: {
774 QSize result = QProxyStyle::sizeFromContents(type, option, size, widget);
775 if (const auto* tab = qstyleoption_cast<const QStyleOptionTab*>(option);
776 tab && verticalTab(tab->shape))
777 result.setWidth(std::max(result.width(), tabHeight));
778 else
779 result.setHeight(std::max(result.height(), tabHeight));
780 return result;
781 }
782 default: break;
783 }
784 return QProxyStyle::sizeFromContents(type, option, size, widget);
785}
786
787int SiliconStyle::pixelMetric(PixelMetric metric, const QStyleOption* option,
788 const QWidget* widget) const
789{
790 switch (metric) {
791 case PM_DefaultFrameWidth:
792 if (widget && widget->windowType() == Qt::Popup
793 && qobject_cast<const QComboBox*>(widget->parentWidget()))
794 return borderWidth + shadowOffset + menuMargin;
795 if (qobject_cast<const QAbstractScrollArea*>(widget)
796 || qobject_cast<const QTabWidget*>(widget))
797 return 0;
798 return borderWidth;
799 case PM_MenuBarHMargin: return spacingUnit;
800 case PM_MenuBarVMargin: return spacingUnit / 2;
801 case PM_MenuBarItemSpacing: return spacingUnit;
802 case PM_DockWidgetFrameWidth: return 0;
803 case PM_DockWidgetTitleMargin:
804 case PM_HeaderMargin: return controlVertical;
805 case PM_SpinBoxFrameWidth: return borderWidth;
806 case PM_ButtonMargin: return buttonPaddingVertical;
807 case PM_MenuButtonIndicator: return menuArrowWidth;
808 case PM_ScrollBarExtent: return scrollBarThickness;
809 case PM_ScrollBarSliderMin: return scrollBarMinimum;
810 case PM_IndicatorWidth:
811 case PM_IndicatorHeight:
812 case PM_ExclusiveIndicatorWidth:
813 case PM_ExclusiveIndicatorHeight: return indicatorSize;
814 case PM_FocusFrameHMargin:
815 case PM_FocusFrameVMargin: return 0;
816 case PM_MenuPanelWidth: return borderWidth + shadowOffset;
817 case PM_MenuHMargin:
818 case PM_MenuVMargin: return menuMargin;
819 case PM_TabBarTabVSpace: return spacingUnit * 2;
820 case PM_TabBarTabShiftVertical: return 0;
821 case PM_TabCloseIndicatorWidth:
822 case PM_TabCloseIndicatorHeight: return iconSize;
823 case PM_ToolBarFrameWidth: return 1;
824 case PM_ToolBarItemSpacing:
825 case PM_ToolBarItemMargin: return toolbarSpacing;
826 case PM_LayoutLeftMargin:
827 case PM_LayoutTopMargin:
828 case PM_LayoutRightMargin:
829 case PM_LayoutBottomMargin: return contentMargin;
830 default: return QProxyStyle::pixelMetric(metric, option, widget);
831 }
832}
833
834int SiliconStyle::styleHint(StyleHint hint, const QStyleOption* option,
835 const QWidget* widget, QStyleHintReturn* result) const
836{
837 if (hint == SH_ComboBox_Popup)
838 return true;
839 if (hint == SH_RubberBand_Mask || hint == SH_UnderlineShortcut
840 || hint == SH_Widget_Animate)
841 return false;
842 return QProxyStyle::styleHint(hint, option, widget, result);
843}
844
845QIcon SiliconStyle::standardIcon(StandardPixmap pixmap, const QStyleOption* option,
846 const QWidget* widget) const
847{
848 switch (pixmap) {
849 case SP_TitleBarCloseButton:
850 case SP_DockWidgetCloseButton:
851 case SP_LineEditClearButton:
852 case SP_DialogCloseButton: return Icon("xmark");
853 case SP_DialogOkButton:
854 case SP_DialogApplyButton: return Icon("check");
855 case SP_MessageBoxInformation: return Icon("info");
856 case SP_MessageBoxCritical:
857 case SP_MessageBoxWarning: return Icon("xmark-circle");
858 default: return QProxyStyle::standardIcon(pixmap, option, widget);
859 }
860}
861
862void SiliconStyle::polish(QWidget* widget)
863{
864 if (!widget)
865 return;
866 if (!widget->property(hoverProperty).isValid())
867 widget->setProperty(hoverProperty, widget->testAttribute(Qt::WA_Hover));
868 QProxyStyle::polish(widget);
869 // Fusion enables hover on the remaining interactive controls.
870 if (qobject_cast<QLineEdit*>(widget) || qobject_cast<QTabBar*>(widget))
871 widget->setAttribute(Qt::WA_Hover);
872 // Explicit application fonts win; repeated polishing must not replace our backup.
873 if (widget->testAttribute(Qt::WA_SetFont)
874 || widget->property(installedFontProperty).isValid())
875 return;
876 QFont font;
877 if (qobject_cast<QLabel*>(widget)
878 && (widget->property("heading").toBool() || widget->objectName() == "titleLabel")) {
879 font = QFont("Chango");
880 font.setPixelSize(24);
881 font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
882 } else if (hasClass(widget, "mono")) {
883 font = QApplication::font("QPlainTextEdit");
884 } else {
885 return;
886 }
887 widget->setProperty(originalFontProperty, widget->font());
888 widget->setProperty(installedFontProperty, font);
889 widget->setFont(font);
890}
891
892void SiliconStyle::unpolish(QWidget* widget)
893{
894 if (!widget)
895 return;
896 const auto installed = widget->property(installedFontProperty);
897 if (installed.isValid()) {
898 const auto font = installed.value<QFont>();
899 const auto current = widget->font();
900 // Only undo our assignment. Preserve subsequent application changes, including zoom.
901 if (current.resolveMask() == font.resolveMask() && current == font.resolve(current)) {
902 widget->setFont(widget->property(originalFontProperty).value<QFont>());
903 widget->setAttribute(Qt::WA_SetFont, false);
904 }
905 widget->setProperty(originalFontProperty, {});
906 widget->setProperty(installedFontProperty, {});
907 }
908 QProxyStyle::unpolish(widget);
909 const auto hover = widget->property(hoverProperty);
910 if (hover.isValid()) {
911 widget->setAttribute(Qt::WA_Hover, hover.toBool());
912 widget->setProperty(hoverProperty, {});
913 }
914}
915
916} // namespace SILICON::ui
void polish(QWidget *) override
void unpolish(QWidget *) override
QRect subElementRect(SubElement, const QStyleOption *, const QWidget *=nullptr) const override
void drawComplexControl(ComplexControl, const QStyleOptionComplex *, QPainter *, const QWidget *=nullptr) const override
QSize sizeFromContents(ContentsType, const QStyleOption *, const QSize &, const QWidget *=nullptr) const override
int styleHint(StyleHint, const QStyleOption *=nullptr, const QWidget *=nullptr, QStyleHintReturn *=nullptr) const override
int pixelMetric(PixelMetric, const QStyleOption *=nullptr, const QWidget *=nullptr) const override
QIcon standardIcon(StandardPixmap, const QStyleOption *=nullptr, const QWidget *=nullptr) const override
SiliconStyle(QObject *parent=nullptr)
SubControl hitTestComplexControl(ComplexControl, const QStyleOptionComplex *, const QPoint &, const QWidget *=nullptr) const override
QPalette standardPalette() const override
QRect subControlRect(ComplexControl, const QStyleOptionComplex *, SubControl, const QWidget *=nullptr) const override
void drawControl(ControlElement, const QStyleOption *, QPainter *, const QWidget *=nullptr) const override
void drawPrimitive(PrimitiveElement, const QStyleOption *, QPainter *, const QWidget *=nullptr) const override
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
static const theme::Theme & current()
Definition theme.cpp:164
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
QPalette palette() const
Definition theme.cpp:101