Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qohosstyle_p.h"
5#include <QtGui/qpainter.h>
6#include <QtGui/qpainterpath.h>
7#include <QtWidgets/qcheckbox.h>
8#include <QtWidgets/qcombobox.h>
9#include <QtWidgets/qgroupbox.h>
10#include <QtWidgets/qlineedit.h>
11#include <QtWidgets/qlistview.h>
12#include <QtWidgets/qmdisubwindow.h>
13#include <QtWidgets/qpushbutton.h>
14#include <QtWidgets/qradiobutton.h>
15#include <QtWidgets/qscrollbar.h>
16#include <QtWidgets/qspinbox.h>
17#include <QtWidgets/qstyleditemdelegate.h>
18#include <QtWidgets/qstyleoption.h>
19#include <QtWidgets/qtoolbutton.h>
20#include <QtWidgets/qtreeview.h>
21#include "qpa/qplatformtheme.h"
22#include "private/qguiapplication_p.h"
23#include "private/qstylehelper_p.h"
24#include <algorithm>
25
26namespace {
27
28constexpr int checkBoxOrRadioButtonIndicatorSize = 24;
29constexpr int checkBoxOrRadioButtonIndicatorStrokeWidth = 1;
30constexpr int checkBoxOrRadioButtonIndicatorGap = 2;
31constexpr int checkBoxOrRadioButtonHoverSize =
32 checkBoxOrRadioButtonIndicatorSize + (2 * checkBoxOrRadioButtonIndicatorGap);
33constexpr int checkBoxOrRadioButtonFocusStrokeWidth = 2;
34constexpr int checkBoxOrRadioButtonSize =
35 checkBoxOrRadioButtonIndicatorSize + (2 * checkBoxOrRadioButtonIndicatorGap)
36 + (2 * checkBoxOrRadioButtonFocusStrokeWidth);
37constexpr int radioButtonIndicatorStrokeWidth = 6;
38
39constexpr int textCursorWidth = 1;
40
41constexpr int itemViewItemHorizontalMargin = 16;
42constexpr int itemViewItemVerticalMargin = 8;
43constexpr int itemViewItemWithDecorationVerticalMargin = 10;
44constexpr int itemViewItemSpacing = 8;
45constexpr int itemViewSeparatorHeight = 5;
46
47constexpr int focusFrameWidth = 2;
48constexpr int focusFrameCornerRadius = 16;
49
50constexpr int headerMargin = 16;
51constexpr int headerDefaultSectionSizeVertical = 48;
52
53constexpr double lineEditCornerRadius = 32.0;
54constexpr int lineEditHorizontalMargin = 16;
55constexpr int lineEditVerticalMargin = 9;
56
57constexpr int menuBarItemSpacing = 16;
58constexpr int menuBarVerticalMargin = 8;
59constexpr int menuBarHorizontalMargin = 8;
60
61constexpr int menuItemVerticalMargin = 8;
62constexpr int menuItemCornerRadius = 4;
63constexpr int menuItemHoverHorizontalMargin = 4;
64
65constexpr int pushButtonHorizontalMargin = 16;
66constexpr int pushButtonVerticalMargin = 8;
67constexpr int pushButtonCornerRadius = 8;
68constexpr int pushButtonFocusFrameGap = 2;
69constexpr int pushButtonFocusFrameWidth = 2;
70
71constexpr int progressBarMinHeight = 24;
72constexpr int progressBarGrooveHeight = 4;
73constexpr int progressBarCornerRadius = 2;
74
75constexpr int shadowXSRadius = 22;
76
77constexpr int sliderThickness = 40;
78constexpr int sliderGrooveHeight = 4;
79constexpr int sliderCornerRadius = 2;
80constexpr int sliderThumbSize = 16;
81constexpr int sliderHoverSize = 4;
82constexpr int sliderFocusStrokeWidth = 2;
83
84constexpr int smallIconSize = 12;
85
86constexpr int splitterHandleWidth = 6;
87constexpr int splitterWidth = 12;
88
89constexpr int titleBarHeight = 46;
90
91constexpr int toolButtonCornerRadius = 4;
92constexpr int toolButtonFocusFrameWidth = 2;
93
94constexpr int toolTipCornerRadius = 8;
95constexpr int toolTipFrameWidth = 8;
96constexpr int toolTipBackgroundAlpha = 204;
97
98constexpr int spinBoxFrameHeight = 32;
99constexpr int spinBoxButtonBoxWidth = 32;
100constexpr int spinBoxButtonBoxHeight = 16;
101constexpr int spinBoxEditHorizontalMargin = 4;
102constexpr double spinBoxFrameRadius = 12.0;
103constexpr int spinBoxFrameStroke = 2;
104
105constexpr int maxScrollBarSize = 9;
106constexpr int thinScrollBarSize = 3;
107constexpr int thickScrollBarSize = 6;
108
109constexpr int tabBarTabRadius = 8;
110constexpr int tabBarTabFrameWidth = 2;
111constexpr int tabBarTabUnderLineWidth = 2;
112
113constexpr int groupBoxBottomPadding = 8;
114constexpr int groupBoxFrameCornerRadius = 8;
115constexpr int groupBoxHorizontalPadding = 12;
116constexpr int groupBoxTitleTextFontSize = 20;
117constexpr int groupBoxTitleToContentSpacing = 8;
118constexpr int groupBoxTopPadding = 24;
119
120constexpr int comboBoxVerticalMargin = 4;
121constexpr int comboBoxFrameRadius = 8;
122constexpr int comboBoxFrameLeftRightPadding = 16;
123constexpr int comboBoxFocusFrameWidth = 2;
124
125constexpr int tabWidgetFrameWidth = 1;
126
127constexpr double ohos_id_alpha_disabled = 0.4;
128constexpr double ohos_id_alpha_content_tertiary = 0.4;
129
130QColor makeInactiveOrDisabledFromColor(const QColor &color)
131{
132 auto disabledColor = color;
133 disabledColor.setAlphaF(ohos_id_alpha_disabled);
134
135 return disabledColor;
136}
137
138template<typename T>
139bool qobjectIsInstanceOf(const QObject *obj)
140{
141 return qobject_cast<const T *>(obj) != nullptr;
142}
143
144template<typename T>
145bool qStyleOptionIs(const QStyleOption *option)
146{
147 return qstyleoption_cast<const T *>(option) != nullptr;
148}
149
150bool isItemViewSeparator(const QModelIndex &index)
151{
152 return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator");
153}
154
155bool isFirstItem(const QModelIndex &index)
156{
157 return index.isValid() && index.row() == 0 && index.column() == 0;
158}
159
160bool isLastItem(const QModelIndex &index)
161{
162 return index.isValid()
163 && index.row() == index.model()->rowCount(index.parent()) - 1
164 && index.column() == index.model()->columnCount(index.parent()) - 1;
165}
166
167bool isHoverable(const QWidget *widget)
168{
169 static bool (* const hoverTypeCheckFuncs[])(const QObject *obj) = {
170 &qobjectIsInstanceOf<QCheckBox>,
171 &qobjectIsInstanceOf<QPushButton>,
172 &qobjectIsInstanceOf<QRadioButton>,
173 &qobjectIsInstanceOf<QSlider>,
174 &qobjectIsInstanceOf<QToolButton>,
175 &qobjectIsInstanceOf<QComboBox>,
176 &qobjectIsInstanceOf<QScrollBar>,
177 };
178
179 return std::any_of(
180 std::begin(hoverTypeCheckFuncs), std::end(hoverTypeCheckFuncs),
181 [&](const auto &typeCheckFunc) {
182 return typeCheckFunc(widget);
183 });
184}
185
186QRect adjusted(const QRect &rect, int growth)
187{
188 return rect.adjusted(-growth, -growth, growth, growth);
189}
190
191QColor getSunkenOrHoverColor(bool isSunken, const QPalette &palette)
192{
193 return isSunken
194 ? palette.color(QPalette::Active, QPalette::Dark)
195 : palette.color(QPalette::Active, QPalette::Light);
196}
197
198void paintOnStackTop(QPainter &painter, const std::function<void(QPainter &painter)> &paint)
199{
200 painter.save();
201 paint(painter);
202 painter.restore();
203}
204
205void drawLine(QPainter &painter, const QPen &pen, const QPoint &aPoint, const QPoint &bPoint)
206{
207 paintOnStackTop(painter, [&](QPainter &painter) {
208 painter.setRenderHints(QPainter::Antialiasing);
209 painter.setPen(pen);
210 painter.drawLine(aPoint, bPoint);
211 });
212}
213
214void drawEllipse(QPainter &painter, const QPen &pen, const QBrush &brush, const QRect &rect)
215{
216 painter.save();
217 painter.setRenderHints(QPainter::Antialiasing);
218 painter.setPen(pen);
219 painter.setBrush(brush);
220 painter.drawEllipse(rect);
221 painter.restore();
222}
223
224void drawRect(QPainter &painter, const QPen &pen, const QBrush &brush, const QRect &rect)
225{
226 paintOnStackTop(painter, [&](QPainter &painter) {
227 painter.setRenderHints(QPainter::Antialiasing);
228 painter.setPen(pen);
229 painter.setBrush(brush);
230 painter.drawRect(rect);
231 });
232}
233
234void drawRoundedRect(
235 QPainter &painter, const QPen &pen, const QBrush &brush, const QRect &rect, qreal xRadius, qreal yRadius)
236{
237 paintOnStackTop(painter, [&](QPainter &painter) {
238 painter.setRenderHints(QPainter::Antialiasing);
239 painter.setPen(pen);
240 painter.setBrush(brush);
241 painter.drawRoundedRect(rect, xRadius, yRadius);
242 });
243}
244
245QPainterPath roundedPathInRect(
246 const QRect &rect, qreal topLeftCornerRadius, qreal topRightCornerRadius, qreal bottomLeftCornerRadius,
247 qreal bottomRightCornerRadius)
248{
249 QPainterPath path;
250
251 const auto bottom = rect.y() + rect.height();
252 const auto right = rect.x() + rect.width();
253
254 const auto topLeftDiameter = 2 * topLeftCornerRadius;
255 path.moveTo(rect.x() + topLeftCornerRadius, rect.y());
256 path.arcTo(rect.x(), rect.y(), topLeftDiameter, topLeftDiameter, 90.0, 90.0);
257
258 const auto bottomLeftDiameter = 2 * bottomLeftCornerRadius;
259 path.lineTo(rect.x(), bottom - bottomLeftCornerRadius);
260 path.arcTo(rect.x(), bottom - bottomLeftDiameter, bottomLeftDiameter, bottomLeftDiameter, 180.0, 90.0);
261
262 const auto bottomRightDiameter = 2 * bottomRightCornerRadius;
263 path.lineTo(right - bottomRightCornerRadius, bottom);
264 path.arcTo(
265 right - bottomRightDiameter, bottom - bottomRightDiameter, bottomRightDiameter, bottomRightDiameter, 270.0,
266 90.0);
267
268 const auto topRightDiameter = 2 * topRightCornerRadius;
269 path.lineTo(right, rect.y() + topRightCornerRadius);
270 path.arcTo(right - topRightDiameter, rect.y(), topRightDiameter, topRightDiameter, 360.0, 90.0);
271
272 path.closeSubpath();
273
274 return path;
275}
276
277void drawPath(QPainter &painter, const QPen &pen, const QBrush &brush, const QPainterPath& path)
278{
279 paintOnStackTop(painter, [&](QPainter &painter) {
280 painter.setRenderHints(QPainter::Antialiasing);
281 painter.setPen(pen);
282 painter.setBrush(brush);
283 painter.drawPath(path);
284 });
285}
286
287void drawRoundedRect(
288 QPainter &painter, const QPen &pen, const QBrush brush, const QRect &rect, qreal topLeftCornerRadius,
289 qreal topRightCornerRadius, qreal bottomLeftCornerRadius, qreal bottomRightCornerRadius)
290{
291 drawPath(
292 painter, pen, brush, roundedPathInRect(rect, topLeftCornerRadius, topRightCornerRadius, bottomLeftCornerRadius,
293 bottomRightCornerRadius));
294}
295
296void drawPolyline(
297 QPainter &painter, const QPen &pen, const QBrush &brush, const std::vector<QPointF> &polygonPoints)
298{
299 paintOnStackTop(painter, [&](auto &p) {
300 p.setRenderHint(QPainter::Antialiasing);
301 p.setPen(pen);
302 p.setBrush(brush);
303 p.drawPolyline(polygonPoints.data(), polygonPoints.size());
304 });
305}
306
307void drawCheckBoxOrRadioButtonIndicator(QPainter &painter, const QStyleOption &option, bool isCheckBox)
308{
309 const auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
310 const auto isOn = option.state.testFlag(QStyle::State_On);
311 const auto isMouseOver = option.state.testFlag(QStyle::State_MouseOver);
312 const auto isSunken = option.state.testFlag(QStyle::State_Sunken);
313
314 painter.save();
315 painter.setRenderHints(QPainter::Antialiasing);
316
317 painter.setOpacity(isEnabled ? 1.0 : ohos_id_alpha_disabled);
318
319 QRect hoverRect(0, 0, checkBoxOrRadioButtonHoverSize, checkBoxOrRadioButtonHoverSize);
320 hoverRect.moveCenter(option.rect.center());
321 if (isMouseOver || isSunken)
322 drawEllipse(painter, Qt::NoPen, getSunkenOrHoverColor(isSunken, option.palette), hoverRect);
323
324 QRect indicatorRect(0, 0, checkBoxOrRadioButtonIndicatorSize, checkBoxOrRadioButtonIndicatorSize);
325 indicatorRect.moveCenter(option.rect.center());
326
327 auto activeColor = option.palette.color(QPalette::Active, QPalette::Button);
328 if (isOn) {
329 if (isCheckBox) {
330 drawEllipse(painter, Qt::NoPen, activeColor, indicatorRect);
331
332 QPixmap svgPixmap(indicatorRect.size());
333 if (svgPixmap.load(QString::fromStdString(":/resources/ic_gallery_material_select_checkbo.svg"))) {
334 painter.drawPixmap(indicatorRect.topLeft(), svgPixmap);
335 }
336 } else {
337 drawEllipse(
338 painter, QPen(activeColor, radioButtonIndicatorStrokeWidth), Qt::NoBrush,
339 adjusted(indicatorRect, -radioButtonIndicatorStrokeWidth / 2));
340 }
341 } else {
342 drawEllipse(
343 painter, QPen(option.palette.color(QPalette::Inactive, QPalette::Button),
344 checkBoxOrRadioButtonIndicatorStrokeWidth), option.palette.color(QPalette::Inactive, QPalette::Base),
345 adjusted(indicatorRect, -checkBoxOrRadioButtonIndicatorStrokeWidth));
346 }
347
348 painter.restore();
349}
350
351void drawLineEditBackground(QPainter &painter, const QStyleOption &option)
352{
353 auto radius = std::min(
354 std::min(option.rect.size().width(), option.rect.size().height()) * 0.5, lineEditCornerRadius);
355 drawRoundedRect(painter, Qt::NoPen, option.palette.brush(QPalette::Base), option.rect, radius, radius);
356}
357
358QBrush getPushButtonBackgroundBrush(const QStyleOptionButton &option)
359{
360 const auto isDefaultButton = option.features.testFlag(QStyleOptionButton::DefaultButton);
361 const auto isAutoDefaultButton = option.features.testFlag(QStyleOptionButton::AutoDefaultButton);
362 const auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
363 const auto isOn = option.state.testFlag(QStyle::State_On);
364
365 const auto defaultButtonBackgroundColor = isEnabled
366 ? option.palette.color(QPalette::Active, QPalette::AlternateBase)
367 : makeInactiveOrDisabledFromColor(option.palette.color(QPalette::Active, QPalette::AlternateBase));
368
369 return isOn
370 ? isDefaultButton || isAutoDefaultButton
371 ? defaultButtonBackgroundColor
372 : option.palette.color(QPalette::Active, QPalette::Dark)
373 : isDefaultButton
374 ? defaultButtonBackgroundColor
375 : option.palette.brush(
376 isEnabled
377 ? QPalette::Active
378 : QPalette::Inactive,
379 QPalette::Button);
380}
381
382QColor getPushButtonTextColor(const QStyleOptionButton &option)
383{
384 const auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
385 const auto isDefaultButton = option.features.testFlag(QStyleOptionButton::DefaultButton);
386 const auto isOn = option.state.testFlag(QStyle::State_On);
387 const QPalette *themePalette =
388 QGuiApplicationPrivate::platformTheme()->palette(QPlatformTheme::Palette::ButtonPalette);
389
390 return isDefaultButton || isOn
391 ? themePalette->color(QPalette::Active, QPalette::BrightText)
392 : themePalette->color(
393 isEnabled
394 ? QPalette::Active
395 : QPalette::Disabled,
396 QPalette::ButtonText);
397}
398
399void drawPushButtonBackground(QPainter &painter, const QStyleOptionButton &option)
400{
401 paintOnStackTop(painter, [&](QPainter &painter) {
402 const auto isMouseOver = option.state.testFlag(QStyle::State_MouseOver);
403 const auto isSunken = option.state.testFlag(QStyle::State_Sunken);
404
405 painter.setRenderHints(QPainter::Antialiasing);
406 painter.setPen(Qt::NoPen);
407
408 auto adjustment = pushButtonFocusFrameGap + pushButtonFocusFrameWidth;
409 auto rect = option.rect.adjusted(adjustment, adjustment, -adjustment, -adjustment);
410
411 painter.setBrush(getPushButtonBackgroundBrush(option));
412 painter.drawRoundedRect(rect, pushButtonCornerRadius, pushButtonCornerRadius);
413
414 if (isMouseOver || isSunken) {
415 painter.setBrush(getSunkenOrHoverColor(isSunken, option.palette));
416 painter.drawRoundedRect(rect, pushButtonCornerRadius, pushButtonCornerRadius);
417 }
418 });
419}
420
421void drawPushButtonFocus(QPainter &painter, const QStyleOption &option)
422{
423 paintOnStackTop(painter, [&](QPainter &painter) {
424 painter.setRenderHints(QPainter::Antialiasing);
425 painter.setBrush(Qt::NoBrush);
426 painter.setPen(QPen(option.palette.color(QPalette::Active, QPalette::Highlight), pushButtonFocusFrameWidth));
427 auto cornerRadius = pushButtonCornerRadius + pushButtonFocusFrameGap;
428 painter.drawRoundedRect(option.rect, cornerRadius, cornerRadius);
429 });
430}
431
432QRect getProgressBarGrooveRect(const QStyleOptionProgressBar &option)
433{
434 auto rect = option.rect;
435 const bool horizontal = option.state & QStyle::State_Horizontal;
436 if (horizontal) {
437 rect.setHeight(progressBarGrooveHeight);
438 rect.moveTop(option.rect.center().y());
439 } else {
440 rect.setWidth(progressBarGrooveHeight);
441 rect.moveRight(option.rect.center().x());
442 }
443
444 return rect;
445}
446
447void drawProgressBarContent(QPainter &painter, const QStyleOptionProgressBar &option)
448{
449 auto rect = getProgressBarGrooveRect(option);
450 const double fraction = static_cast<double>(option.progress - option.minimum) / (option.maximum - option.minimum);
451 const bool horizontal = option.state & QStyle::State_Horizontal;
452 if (horizontal) {
453 rect.setWidth(fraction * rect.width());
454 rect.moveLeft(option.invertedAppearance ? option.rect.width() - rect.width() : 0);
455 } else {
456 rect.setHeight(fraction * rect.height());
457 rect.moveTop(option.invertedAppearance ? 0 : option.rect.height() - rect.height());
458 }
459
460 drawRoundedRect(
461 painter, Qt::NoPen, option.palette.brush(QPalette::Highlight), rect, progressBarCornerRadius,
462 progressBarCornerRadius);
463}
464
465void drawSliderGroove(QPainter &painter, const QStyleOptionSlider &option)
466{
467 drawRoundedRect(
468 painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::Mid),
469 option.rect, sliderCornerRadius, sliderCornerRadius);
470
471 QRect fillRect;
472 if (option.orientation == Qt::Horizontal) {
473 const auto sliderPosition = QStyle::sliderPositionFromValue(
474 option.minimum, option.maximum, option.sliderPosition, option.rect.width(), option.upsideDown);
475 fillRect = QRect(option.rect.topLeft(), QSize(sliderPosition, option.rect.height()));
476 } else {
477 const auto sliderPosition = QStyle::sliderPositionFromValue(
478 option.minimum, option.maximum, option.sliderPosition, option.rect.height(), option.upsideDown);
479 fillRect = QRect(
480 option.rect.x(), option.rect.y() + sliderPosition, option.rect.width(), option.rect.height() - sliderPosition);
481 }
482 drawRoundedRect(
483 painter, Qt::NoPen, option.palette.brush(QPalette::Highlight), fillRect, sliderCornerRadius,
484 sliderCornerRadius);
485}
486
487void drawSliderThumb(QPainter &painter, const QStyleOptionSlider &option)
488{
489 QRect shadowRect(0, 0, shadowXSRadius, shadowXSRadius);
490 shadowRect.moveCenter(option.rect.center());
491
492 QRadialGradient radialGrad(shadowRect.center(), shadowXSRadius * 1.0);
493 radialGrad.setColorAt(0.0, Qt::transparent);
494 radialGrad.setColorAt(1.0, option.palette.color(QPalette::Active, QPalette::Shadow));
495
496 drawEllipse(painter, Qt::NoPen, QBrush(radialGrad), shadowRect);
497
498 if (option.state.testFlag(QStyle::State_MouseOver))
499 drawEllipse(painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::Light), option.rect);
500
501 auto adjustedRect = option.rect.adjusted(sliderHoverSize, sliderHoverSize, -sliderHoverSize, -sliderHoverSize);
502 drawEllipse(painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::AlternateBase), adjustedRect);
503
504 if (option.state.testFlag(QStyle::State_Sunken))
505 drawEllipse(painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::Dark), adjustedRect);
506
507 if (option.state.testFlag(QStyle::State_HasFocus)) {
508 drawEllipse(
509 painter, QPen(option.palette.color(QPalette::Active, QPalette::Highlight), sliderFocusStrokeWidth),
510 Qt::NoBrush, adjusted(option.rect, -1));
511 }
512}
513
514void drawToolButton(QPainter &painter, const QStyleOption &option)
515{
516 const auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
517 const auto isMouseOver = option.state.testFlag(QStyle::State_MouseOver);
518 const auto isSunken = option.state.testFlag(QStyle::State_Sunken);
519
520 drawRoundedRect(
521 painter, Qt::NoPen, option.palette.brush(isEnabled ? QPalette::Active : QPalette::Disabled, QPalette::Button),
522 option.rect, toolButtonCornerRadius, toolButtonCornerRadius);
523
524 if (isMouseOver || isSunken) {
525 drawRoundedRect(
526 painter, Qt::NoPen, getSunkenOrHoverColor(isSunken, option.palette), option.rect, toolButtonCornerRadius,
527 toolButtonCornerRadius);
528 }
529}
530
531void drawToolBarbackground(QPainter &painter, const QStyleOption &option)
532{
533 paintOnStackTop(painter, [&](QPainter &painter) {
534 painter.setRenderHints(QPainter::Antialiasing);
535 painter.fillRect(option.rect, option.palette.brush(QPalette::Active, QPalette::Window));
536 });
537}
538
539void drawMenuBarBarbackground(QPainter &painter, const QStyleOption &option)
540{
541 paintOnStackTop(painter, [&](QPainter &painter) {
542 painter.setRenderHints(QPainter::Antialiasing);
543 painter.fillRect(option.rect, option.palette.brush(QPalette::Active, QPalette::Window));
544 });
545}
546
547void drawListItemSeparator(QPainter &painter, const QStyleOption &option)
548{
549 QPoint center = option.rect.center();
550 drawLine(
551 painter, option.palette.color(QPalette::Active, QPalette::Mid),
552 QPoint(option.rect.left(), center.y()), QPoint(option.rect.right(), center.y()));
553}
554
555void drawMenuItem(QPainter &painter, const QStyleOptionMenuItem &option, int checkMarkSize, int iconSize)
556{
557 const auto contentRect = option.rect.adjusted(itemViewItemHorizontalMargin, 0, -itemViewItemHorizontalMargin, 0);
558 if (option.menuItemType == QStyleOptionMenuItem::Separator) {
559 auto copyOption = option;
560 copyOption.rect = contentRect;
561 drawListItemSeparator(painter, copyOption);
562 } else {
563 const bool isSelected = option.state.testFlag(QStyle::State_Selected);
564 const bool isSunken = option.state.testFlag(QStyle::State_Sunken);
565
566 if (isSelected || isSunken) {
567 drawRoundedRect(
568 painter, Qt::NoPen, getSunkenOrHoverColor(isSunken, option.palette),
569 option.rect.adjusted(menuItemHoverHorizontalMargin, 0, -menuItemHoverHorizontalMargin, 0),
570 menuItemCornerRadius, menuItemCornerRadius);
571 }
572
573 const bool isCheckable = option.checkType != QStyleOptionMenuItem::NotCheckable;
574 QRect checkMarkRect;
575 if (isCheckable) {
576 checkMarkRect = contentRect;
577 checkMarkRect.setWidth(checkMarkSize);
578
579 auto copyOption = option;
580 copyOption.rect = checkMarkRect;
581 if (option.checked)
582 copyOption.state |= QStyle::State_On;
583
584 drawCheckBoxOrRadioButtonIndicator(painter, copyOption, true);
585 }
586
587 const bool hasIcon = !option.icon.isNull();
588 QRect iconRect;
589 if (hasIcon) {
590 iconRect = QRect(contentRect.x(), contentRect.center().y() - iconSize * 0.5, iconSize, iconSize);
591 iconRect.moveLeft(checkMarkRect.right() + itemViewItemHorizontalMargin);
592
593 option.icon.paint(
594 &painter, iconRect, Qt::AlignCenter,
595 option.state.testFlag(QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled,
596 option.checked ? QIcon::On : QIcon::Off);
597 }
598
599 QRect textRect = contentRect;
600 if (isCheckable)
601 textRect.setLeft(checkMarkRect.right() + itemViewItemHorizontalMargin);
602 if (hasIcon)
603 textRect.setLeft(iconRect.right() + itemViewItemHorizontalMargin);
604 paintOnStackTop(painter, [&](QPainter &painter) {
605 painter.setPen(option.palette.color(QPalette::Text));
606 painter.drawText(
607 textRect, (Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic), option.text);
608 });
609 }
610}
611
612void drawMenuTearOff(QPainter &painter, const QStyleOption &option)
613{
614 const auto pointY = option.rect.y() + option.rect.height() / 2;
615 drawLine(
616 painter, QPen(option.palette.color(QPalette::Text), 1, Qt::DashLine), QPoint(option.rect.x(), pointY),
617 QPoint(option.rect.x() + option.rect.width(), pointY)
618 );
619}
620
621void drawSplitter(QPainter &painter, const QStyleOption &option)
622{
623 constexpr int splitterAvailableSizeDivider = 12;
624 const bool isHorizontal = option.state.testFlag(QStyle::State_Horizontal);
625
626 if (option.rect.width() > 1 && option.rect.height() > 1) {
627 const auto handleSize = isHorizontal
628 ? QSize(splitterHandleWidth, option.rect.height() / splitterAvailableSizeDivider)
629 : QSize(option.rect.width() / splitterAvailableSizeDivider, splitterHandleWidth);
630 const auto handleRect = QRect(
631 QPoint(
632 (option.rect.width() - handleSize.width()) * 0.5,
633 (option.rect.height() - handleSize.height()) * 0.5),
634 handleSize);
635 const auto cornerRadius = std::min(handleSize.width(), handleSize.height()) * 0.5;
636 drawRoundedRect(
637 painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::Mid),
638 handleRect, cornerRadius, cornerRadius);
639 } else {
640 auto endPoint = isHorizontal ? option.rect.bottomLeft() : option.rect.topRight();
641 drawLine(painter, option.palette.dark().color(), option.rect.topLeft(), endPoint);
642 }
643}
644
645void drawSpinBoxFrame(
646 QPainter &painter, const QStyleOptionSpinBox &option, const QRect &frameRect, int frameWidth)
647{
648 auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
649
650 QPen pen =
651 option.frame && option.subControls.testFlag(QStyle::SC_SpinBoxFrame)
652 ? QPen(option.palette.color(QPalette::Active, QPalette::Mid), frameWidth)
653 : Qt::NoPen;
654
655 paintOnStackTop(painter, [&](auto &p) {
656 p.setOpacity(isEnabled ? 1.0 : ohos_id_alpha_disabled);
657 drawRoundedRect(
658 p, pen, option.palette.window(), frameRect, spinBoxFrameRadius, spinBoxFrameRadius);
659 });
660}
661
662void drawSpinBoxButton(QPainter &painter, const QStyleOptionSpinBox &option, int penWidth)
663{
664 auto rect = option.rect;
665 auto isEnabled = option.state.testFlag(QStyle::State_Enabled);
666 auto isPressed = option.state.testFlag(QStyle::State_Sunken);
667
668 QPen pen(option.palette.color(QPalette::Active, QPalette::Mid), penWidth);
669 QBrush brush(option.palette.window());
670
671 if (isPressed)
672 brush = QBrush(option.palette.highlight());
673
674 qreal rectX = rect.x();
675 qreal rectY = rect.y();
676 qreal rectWidth = rect.width();
677 qreal rectHeight = rect.height();
678 qreal cornerRadius = static_cast<qreal>(spinBoxFrameRadius);
679
680 QPainterPath path;
681 path.moveTo(rectX, rectY);
682 path.lineTo(rectX, rectY + rectHeight);
683 if (option.subControls == QStyle::SC_SpinBoxDown) {
684 path.lineTo(rectX + rectWidth - cornerRadius, rectY + rectHeight);
685 path.arcTo(
686 rectX + rectWidth - 2 * cornerRadius, rectY + rectHeight - 2 * cornerRadius,
687 2 * cornerRadius, 2 * cornerRadius, 270, 90);
688 path.lineTo(rectX + rectWidth, rectY);
689 } else {
690 path.lineTo(rectX + rectWidth, rectY + rectHeight);
691 path.lineTo(rectX + rectWidth, rectY + cornerRadius);
692 path.arcTo(rectX + rectWidth - 2 * cornerRadius, rectY, 2 * cornerRadius, 2 * cornerRadius, 0, 90);
693 }
694 path.closeSubpath();
695
696 paintOnStackTop(painter, [&](auto &p) {
697 p.setOpacity(isEnabled ? 1.0 : ohos_id_alpha_disabled);
698 drawPath(p, pen, brush, path);
699 });
700}
701
702void drawScrollBarSlider(QPainter &painter, const QStyleOptionSlider &option)
703{
704 int scrollBarRadius =
705 option.orientation == Qt::Vertical
706 ? option.rect.width() * 0.5
707 : option.rect.height() * 0.5;
708
709 paintOnStackTop(painter, [&](QPainter &painter) {
710 painter.setOpacity(ohos_id_alpha_content_tertiary);
711 drawRoundedRect(
712 painter, Qt::NoPen, option.palette.color(QPalette::Active, QPalette::Midlight),
713 option.rect, scrollBarRadius, scrollBarRadius);
714 });
715}
716
717Qt::Orientation tabBarShapeToOrientation(QTabBar::Shape shape)
718{
719 switch (shape) {
720 case QTabBar::RoundedNorth:
721 case QTabBar::RoundedSouth:
722 case QTabBar::TriangularNorth:
723 case QTabBar::TriangularSouth:
724 return Qt::Horizontal;
725 case QTabBar::RoundedWest:
726 case QTabBar::TriangularWest:
727 case QTabBar::RoundedEast:
728 case QTabBar::TriangularEast:
729 return Qt::Vertical;
730 }
731}
732
733void drawTabBarTabShape(QPainter &painter, const QStyleOptionTab &option)
734{
735 auto isSelected = option.state.testFlag(QStyle::State_Selected);
736 auto isFocused = option.state.testFlag(QStyle::State_HasFocus);
737 QRect rect(option.rect);
738 adjusted(rect, -tabBarTabFrameWidth - 1);
739
740 QPoint underLineStart, underLineEnd;
741 if (tabBarShapeToOrientation(option.shape) == Qt::Horizontal) {
742 int lineY = rect.y() + rect.height() - tabBarTabUnderLineWidth;
743 underLineStart = QPoint(rect.x() + tabBarTabRadius, lineY);
744 underLineEnd = QPoint(rect.x() + rect.width() - tabBarTabRadius, lineY);
745 } else {
746 int lineX = rect.x();
747
748 if (option.shape == QTabBar::RoundedWest || option.shape == QTabBar::TriangularWest)
749 lineX += rect.width() - tabBarTabUnderLineWidth;
750 else
751 lineX += tabBarTabUnderLineWidth;
752
753 underLineStart = QPoint(lineX, rect.y() + tabBarTabRadius);
754 underLineEnd = QPoint(lineX, rect.y() + rect.height() - tabBarTabRadius);
755 }
756
757 drawRoundedRect(painter, Qt::NoPen, option.palette.window(), rect, tabBarTabRadius, tabBarTabRadius);
758 if (!isFocused && isSelected)
759 drawLine(painter, QPen(option.palette.highlight(), tabBarTabUnderLineWidth), underLineStart, underLineEnd);
760}
761
762void drawComboBoxFrame(QPainter &painter, const QStyleOptionComboBox &option)
763{
764 const auto isMouseOver = option.state.testFlag(QStyle::State_MouseOver);
765 const auto isOpened = option.state.testFlag(QStyle::State_On);
766 const auto isFocused = option.state.testFlag(QStyle::State_HasFocus);
767
768 auto focusedPen = QPen(
769 option.palette.color(QPalette::Active, QPalette::AlternateBase),
770 comboBoxFocusFrameWidth);
771
772 drawRoundedRect(
773 painter, isFocused ? focusedPen : Qt::NoPen, option.palette.brush(QPalette::Window), option.rect,
774 comboBoxFrameRadius, comboBoxFrameRadius);
775
776 if (isMouseOver || isOpened) {
777 auto pen = isOpened ? focusedPen : Qt::NoPen;
778 auto brush = getSunkenOrHoverColor(isOpened, option.palette);
779 drawRoundedRect(painter, pen, brush, option.rect, comboBoxFrameRadius, comboBoxFrameRadius);
780 }
781}
782
783void drawComboBoxListViewItem(QPainter &painter, const QStyleOptionViewItem &option, const QBrush &brush)
784{
785 if (isFirstItem(option.index)) {
786 drawRoundedRect(
787 painter, Qt::NoPen, brush, option.rect, focusFrameCornerRadius, focusFrameCornerRadius, 0, 0);
788 } else if (isLastItem(option.index)) {
789 drawRoundedRect(
790 painter, Qt::NoPen, brush, option.rect, 0, 0, focusFrameCornerRadius, focusFrameCornerRadius);
791 } else {
792 drawRect(painter, Qt::NoPen, brush, option.rect);
793 }
794}
795
796void drawTreeViewHighlightItem(QPainter &painter, const QStyleOptionViewItem &option, const QBrush &brush)
797{
798 switch (option.viewItemPosition) {
799 case QStyleOptionViewItem::OnlyOne:
800 drawRoundedRect(
801 painter, Qt::NoPen, brush, option.rect, focusFrameCornerRadius, focusFrameCornerRadius);
802 break;
803 case QStyleOptionViewItem::Beginning:
804 drawRoundedRect(
805 painter, Qt::NoPen, brush, option.rect, focusFrameCornerRadius, 0, focusFrameCornerRadius, 0);
806 break;
807 case QStyleOptionViewItem::Invalid:
808 case QStyleOptionViewItem::Middle:
809 drawRect(painter, Qt::NoPen, brush, option.rect);
810 break;
811 case QStyleOptionViewItem::End:
812 drawRoundedRect(
813 painter, Qt::NoPen, brush, option.rect, 0, focusFrameCornerRadius, 0, focusFrameCornerRadius);
814 break;
815 }
816}
817
818void drawPanelItemViewItem(QPainter &painter, const QStyleOptionViewItem &option, bool isComboBoxListViewItem)
819{
820 if (isItemViewSeparator(option.index)) {
821 auto copyOption = option;
822 copyOption.rect = option.rect.adjusted(itemViewItemHorizontalMargin, 0, -itemViewItemHorizontalMargin, 0);
823 drawListItemSeparator(painter, copyOption);
824 } else {
825 auto isSelected = option.state.testFlag(QStyle::State_Selected);
826 auto isMouseOver = option.state.testFlag(QStyle::State_MouseOver);
827
828 auto colorGroup = option.state.testFlag(QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled;
829 if (colorGroup == QPalette::Normal && !option.state.testFlag(QStyle::State_Active))
830 colorGroup = QPalette::Inactive;
831
832 const QBrush brush =
833 isSelected
834 ? option.palette.brush(colorGroup, QPalette::Highlight)
835 : isMouseOver
836 ? option.palette.color(QPalette::Active, QPalette::Light)
837 : option.backgroundBrush.style() == Qt::NoBrush
838 ? Qt::NoBrush
839 : option.backgroundBrush;
840
841 if (isComboBoxListViewItem)
842 drawComboBoxListViewItem(painter, option, brush);
843 else
844 drawTreeViewHighlightItem(painter, option, brush);
845 }
846}
847
848void drawHeaderSection(QPainter &painter, const QStyleOptionHeader &option)
849{
850 QRect rect =
851 option.position == QStyleOptionHeader::OnlyOneSection
852 ? option.rect
853 : option.orientation == Qt::Horizontal
854 ? option.rect.adjusted(-1, 0, 0, 0)
855 : option.rect.adjusted(0, -1, 0, 0);
856
857 drawRect(painter, QPen(Qt::black), option.palette.brush(QPalette::Button), rect);
858}
859
860void drawWindowBackgroundAndFrame(
861 QPainter &painter, const QStyleOptionFrame &option, QFlags<Qt::WindowState> state, int titleBarHeight)
862{
863 const auto offset = option.lineWidth * 0.5;
864 const auto windowFrameColor =
865 state.testFlag(Qt::WindowActive)
866 ? option.palette.color(QPalette::Active, QPalette::Base)
867 : option.palette.color(QPalette::Inactive, QPalette::Base);
868 if (state.testFlag(Qt::WindowMinimized)) {
869 drawRoundedRect(
870 painter, QPen(windowFrameColor, option.lineWidth), Qt::NoBrush,
871 option.rect.adjusted(offset, offset, -offset, -offset), focusFrameCornerRadius, focusFrameCornerRadius, 0,
872 0);
873 drawRect(
874 painter, Qt::NoPen, option.palette.brush(QPalette::Window),
875 option.rect.adjusted(option.lineWidth, titleBarHeight, -option.lineWidth, 0));
876 } else {
877 drawRoundedRect(
878 painter, Qt::NoPen, windowFrameColor, option.rect.adjusted(0, titleBarHeight, 0, 0), 0, 0,
879 focusFrameCornerRadius, focusFrameCornerRadius);
880 drawRoundedRect(
881 painter, Qt::NoPen, option.palette.brush(QPalette::Window),
882 option.rect.adjusted(option.lineWidth, titleBarHeight, -option.lineWidth, -option.lineWidth),
883 focusFrameCornerRadius, focusFrameCornerRadius);
884 drawRoundedRect(
885 painter, QPen(windowFrameColor, option.lineWidth), Qt::NoBrush,
886 option.rect.adjusted(offset, offset, -offset, -offset), focusFrameCornerRadius, focusFrameCornerRadius);
887 }
888}
889
890void drawToolTip(QPainter &painter, const QStyleOptionFrame &option)
891{
892 drawRoundedRect(
893 painter, option.palette.color(QPalette::Active, QPalette::Base),
894 option.palette.toolTipBase(), option.rect, toolTipCornerRadius,
895 toolTipCornerRadius);
896}
897
898QRect titleBarSubControlRect(const QStyleOptionTitleBar &option, QCommonStyle::SubControl subControl)
899{
900 const bool isMinimized = (option.titleBarState & Qt::WindowMinimized) != 0;
901 const bool isMaximized = (option.titleBarState & Qt::WindowMaximized) != 0;
902
903 const bool hasTitle = option.titleBarFlags.testFlag(Qt::WindowTitleHint);
904 const bool hasSystemMenuButton = option.titleBarFlags.testFlag(Qt::WindowSystemMenuHint);
905 const bool hasShadeButton = option.titleBarFlags.testFlag(Qt::WindowShadeButtonHint);
906 const bool hasContextHelpButton = option.titleBarFlags.testFlag(Qt::WindowContextHelpButtonHint);
907 const bool hasMinimizeButton = option.titleBarFlags.testFlag(Qt::WindowMinimizeButtonHint);
908 const bool hasMaximizeButton = option.titleBarFlags.testFlag(Qt::WindowMaximizeButtonHint);
909
910 const auto controlSize = QSize(30, 30);
911 const int titleBarHorizontalMargin = 25;
912 const int titleBarControlVerticalMargin = 15;
913
914 const auto titleBarContentRect = QRect(
915 option.rect.x() + titleBarHorizontalMargin, (option.rect.height() - controlSize.height()) / 2,
916 option.rect.width() - 2 * titleBarHorizontalMargin, controlSize.height());
917 const auto titleBarContentRectRight = titleBarContentRect.x() + titleBarContentRect.width();
918 const auto delta = controlSize.width() + titleBarControlVerticalMargin;
919
920 QRect result;
921 switch (subControl) {
922 case QCommonStyle::SC_TitleBarSysMenu:
923 if (hasSystemMenuButton)
924 result = QRect(titleBarContentRect.topLeft(), controlSize);
925 break;
926 case QCommonStyle::SC_TitleBarLabel:
927 if (hasTitle) {
928 result = titleBarContentRect;
929 if (hasSystemMenuButton)
930 result.adjust(delta, 0, -delta, 0);
931 if (hasMinimizeButton)
932 result.adjust(0, 0, -delta, 0);
933 if (hasMaximizeButton)
934 result.adjust(0, 0, -delta, 0);
935 if (hasShadeButton)
936 result.adjust(0, 0, -delta, 0);
937 if (hasContextHelpButton)
938 result.adjust(0, 0, -delta, 0);
939 }
940 break;
941 case QCommonStyle::SC_TitleBarMaxButton:
942 if (!isMaximized && hasMaximizeButton) {
943 result = QRect(
944 QPoint(titleBarContentRectRight - 3 * delta + titleBarControlVerticalMargin,
945 titleBarContentRect.y()), controlSize);
946 }
947 break;
948 case QCommonStyle::SC_TitleBarNormalButton:
949 if (isMinimized && hasMinimizeButton) {
950 result = QRect(
951 QPoint(titleBarContentRectRight - 2 * delta + titleBarControlVerticalMargin,
952 titleBarContentRect.y()), controlSize);
953 } else if (isMaximized && hasMaximizeButton) {
954 result = QRect(
955 QPoint(titleBarContentRectRight - 3 * delta + titleBarControlVerticalMargin,
956 titleBarContentRect.y()), controlSize);
957 }
958 break;
959 case QCommonStyle::SC_TitleBarMinButton:
960 if (!isMinimized && hasMinimizeButton) {
961 result = QRect(
962 QPoint(titleBarContentRectRight - 2 * delta + titleBarControlVerticalMargin,
963 titleBarContentRect.y()), controlSize);
964 }
965 break;
966 case QCommonStyle::SC_TitleBarCloseButton:
967 result = QRect(
968 QPoint(titleBarContentRectRight - controlSize.width(), titleBarContentRect.y()), controlSize);
969 break;
970 case QCommonStyle::SC_TitleBarShadeButton:
971 case QCommonStyle::SC_TitleBarUnshadeButton:
972 result = QRect(
973 QPoint(titleBarContentRectRight - 4 * delta + titleBarControlVerticalMargin,
974 titleBarContentRect.y()), controlSize);
975 break;
976 case QCommonStyle::SC_TitleBarContextHelpButton:
977 result = QRect(
978 QPoint(titleBarContentRectRight - (hasShadeButton ? 5 : 4) * delta + titleBarControlVerticalMargin,
979 titleBarContentRect.y()), controlSize);
980 break;
981 default:
982 break;
983 }
984
985 return result;
986}
987
988}
989
990QOhosStyle::QOhosStyle()
991 : QCommonStyle()
992{
993}
994
995void QOhosStyle::drawPrimitive(
996 PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
997{
998 switch (element) {
999 case PE_Frame: {
1000 if (widget != nullptr && qobjectIsInstanceOf<QComboBox>(widget->parent()))
1001 break;
1002 QCommonStyle::drawPrimitive(element, option, painter, widget);
1003 break;
1004 }
1005 case PE_FrameWindow:
1006 if (qStyleOptionIs<QStyleOptionFrame>(option)) {
1007 drawWindowBackgroundAndFrame(
1008 *painter, *qstyleoption_cast<const QStyleOptionFrame *>(option), widget->windowState(),
1009 pixelMetric(PM_TitleBarHeight, option, widget));
1010 }
1011 break;
1012 case PE_FrameFocusRect:
1013 if (const QStyleOptionFocusRect *fropt = qstyleoption_cast<const QStyleOptionFocusRect *>(option)) {
1014 if (qobjectIsInstanceOf<QPushButton>(widget)) {
1015 drawPushButtonFocus(*painter, *option);
1016 } else if (qobjectIsInstanceOf<QToolButton>(widget)) {
1017 const auto menuButtonIndicatorSize =
1018 qobject_cast<const QToolButton *>(widget)->popupMode() == QToolButton::MenuButtonPopup
1019 ? pixelMetric(PM_MenuButtonIndicator, option, widget)
1020 : 0;
1021 drawRoundedRect(
1022 *painter,
1023 QPen(option->palette.color(QPalette::Active, QPalette::Highlight), toolButtonFocusFrameWidth),
1024 Qt::NoBrush, option->rect.adjusted(-1, -1, 1 + menuButtonIndicatorSize, 1),
1025 toolButtonCornerRadius, toolButtonCornerRadius);
1026 } else if (qobjectIsInstanceOf<QCheckBox>(widget) || qobjectIsInstanceOf<QRadioButton>(widget)) {
1027 drawEllipse(
1028 *painter, QPen(option->palette.color(QPalette::Active, QPalette::Button),
1029 checkBoxOrRadioButtonFocusStrokeWidth), Qt::NoBrush, option->rect);
1030 } else if (qobjectIsInstanceOf<QTabBar>(widget)) {
1031 drawRoundedRect(
1032 *painter, QPen(option->palette.highlight(), tabBarTabFrameWidth), Qt::NoBrush,
1033 option->rect, tabBarTabRadius, tabBarTabRadius);
1034 } else {
1035 drawRoundedRect(
1036 *painter, QPen(option->palette.color(QPalette::Active, QPalette::Highlight), focusFrameWidth),
1037 Qt::NoBrush, option->rect, focusFrameCornerRadius, focusFrameCornerRadius);
1038 }
1039 }
1040 break;
1041 case PE_PanelButtonCommand:
1042 if (qStyleOptionIs<QStyleOptionButton>(option))
1043 drawPushButtonBackground(*painter, *qstyleoption_cast<const QStyleOptionButton *>(option));
1044 break;
1045 case PE_PanelButtonTool:
1046 if (qobjectIsInstanceOf<QMdiSubWindow>(widget)) {
1047 const QPalette *toolButtonThemePalette =
1048 QGuiApplicationPrivate::platformTheme()->palette(QPlatformTheme::Palette::ToolButtonPalette);
1049 const auto panelButtonColor =
1050 toolButtonThemePalette->color(QPalette::Active, QPalette::AlternateBase);
1051 drawEllipse(
1052 *painter, Qt::NoPen,
1053 widget->windowState().testFlag(Qt::WindowActive)
1054 ? panelButtonColor
1055 : makeInactiveOrDisabledFromColor(panelButtonColor),
1056 option->rect);
1057 } else {
1058 const QToolButton *toolButton = qobject_cast<const QToolButton *>(widget);
1059 const auto menuButtonIndicatorSize =
1060 toolButton != nullptr && toolButton->popupMode() == QToolButton::MenuButtonPopup
1061 ? pixelMetric(PM_MenuButtonIndicator, option, widget)
1062 : 0;
1063 auto optionCopy = *option;
1064 optionCopy.rect.setWidth(option->rect.width() + menuButtonIndicatorSize);
1065 drawToolButton(*painter, optionCopy);
1066 }
1067 break;
1068 case PE_PanelMenuBar:
1069 drawMenuBarBarbackground(*painter, *option);
1070 break;
1071 case PE_IndicatorCheckBox:
1072 drawCheckBoxOrRadioButtonIndicator(*painter, *option, true);
1073 break;
1074 case PE_IndicatorRadioButton:
1075 drawCheckBoxOrRadioButtonIndicator(*painter, *option, false);
1076 break;
1077 case PE_PanelLineEdit: {
1078 // HACK: Q(Abstrace)SpinBox contains a QLineEdit inside, which is painted separately from
1079 // the QSpinBox itself. We will be drawing the whole QSpinBox in the drawComplexControl.
1080 if (widget != nullptr && qobjectIsInstanceOf<QAbstractSpinBox>(widget->parentWidget()))
1081 break;
1082
1083 // HACK: QComboBox in editable mode presents line edit in the center. This line edit is drawn
1084 // above QComboBox. There are two issues here: QComboBox has different base color than line edit.
1085 // Second, line edit and QComboBox has the same size - so drawing a line edit overlays a frame border.
1086 // To fix above, do not draw line edit background.
1087 if (widget != nullptr && qobjectIsInstanceOf<QComboBox>(widget->parentWidget()))
1088 break;
1089
1090 // HACK: while editing Tree View/Widget item - line edit component is presented above it. If the line edit has
1091 // some opacity in background content below it (item content) is visible. This causes an effect that original
1092 // text is still visible while editing. To fix it, override background (base) color and use solid color value.
1093 auto optionCopy = *option;
1094 if (widget != nullptr && qstrcmp(widget->metaObject()->className(), "QExpandingLineEdit") == 0) {
1095 optionCopy.palette.setColor(
1096 QPalette::Base,
1097 option->palette.color(QPalette::Active, QPalette::AlternateBase));
1098 }
1099
1100 drawLineEditBackground(*painter, optionCopy);
1101 break;
1102 }
1103 case PE_PanelButtonBevel:
1104 if (qStyleOptionIs<QStyleOptionSpinBox>(option)) {
1105 drawSpinBoxButton(
1106 *painter, *qstyleoption_cast<const QStyleOptionSpinBox *>(option),
1107 pixelMetric(PM_SpinBoxFrameWidth, option, widget));
1108 }
1109 break;
1110 case PE_IndicatorSpinDown:
1111 case PE_IndicatorSpinUp: {
1112 // HACK: QCommonStyle adjusts the indicator left as follows: copy.rect.adjust(3, 0, -4, 0);
1113 // However, Ohos requires 4 pixels of horizontal margin and 2 pixels of vertical margin
1114 QRect indicatorRect = option->rect.adjusted(1, 2, 0, -2);
1115 constexpr int arrowWidth = 2;
1116 auto x = indicatorRect.x();
1117 auto y = indicatorRect.y();
1118 auto width = indicatorRect.width();
1119 auto height = indicatorRect.height();
1120 auto verticalSpacing = 4;
1121 auto horizontalSpacing = 7;
1122
1123 qreal x1 = x + horizontalSpacing;
1124 qreal x2 = x + width * 0.5;
1125 qreal x3 = x + width - horizontalSpacing;
1126
1127 qreal y1, y2;
1128 if (element == PE_IndicatorSpinDown) {
1129 y1 = y + verticalSpacing;
1130 y2 = y + height - verticalSpacing;
1131 } else {
1132 y1 = y + height - verticalSpacing;
1133 y2 = y + verticalSpacing;
1134 }
1135 auto y3 = y1;
1136
1137 QPen indicatorPen(option->palette.color(QPalette::Active, QPalette::Text), arrowWidth);
1138 indicatorPen.setCapStyle(Qt::RoundCap);
1139 indicatorPen.setJoinStyle(Qt::RoundJoin);
1140
1141 paintOnStackTop(*painter, [&](auto &p) {
1142 p.setOpacity(option->state.testFlag(State_Enabled) ? 1.0 : ohos_id_alpha_disabled);
1143 p.setRenderHint(QPainter::Antialiasing);
1144 p.setPen(indicatorPen);
1145 p.setBrush(Qt::NoBrush);
1146 p.drawLine(x1, y1, x2, y2);
1147 p.drawLine(x2, y2, x3, y3);
1148 });
1149 break;
1150 }
1151 case PE_FrameGroupBox: {
1152 const auto frame = qstyleoption_cast<const QStyleOptionFrame *>(option);
1153 if (frame != nullptr) {
1154 QPen pen;
1155 pen.setColor(frame->palette.alternateBase().color());
1156 pen.setStyle(
1157 frame->features.testFlag(QStyleOptionFrame::FrameFeature::Flat)
1158 ? Qt::NoPen : Qt::SolidLine);
1159
1160 painter->setClipRegion(frame->rect);
1161 drawRoundedRect(
1162 *painter, pen, option->palette.window(), frame->rect, groupBoxFrameCornerRadius,
1163 groupBoxFrameCornerRadius);
1164 }
1165 break;
1166 }
1167 case PE_IndicatorArrowUp:
1168 case PE_IndicatorArrowDown:
1169 case PE_IndicatorArrowLeft:
1170 case PE_IndicatorArrowRight: {
1171 QRect availableRect;
1172 int maxArrowSize = 0;
1173 if (qobjectIsInstanceOf<QToolButton>(widget) || qobjectIsInstanceOf<QPushButton>(widget)
1174 || qobjectIsInstanceOf<QComboBox>(widget)) {
1175 const auto menuButtonIndicatorSize = pixelMetric(PM_MenuButtonIndicator, option, widget);
1176 availableRect = QRect(
1177 widget->size().width() - menuButtonIndicatorSize, 0, menuButtonIndicatorSize, widget->size().height());
1178 maxArrowSize = std::min(availableRect.width(), availableRect.height()) * 0.5;
1179 } else {
1180 availableRect = option->rect;
1181 maxArrowSize = std::min(availableRect.width(), availableRect.height());
1182 }
1183
1184 const auto arrowWidth = 2;
1185 auto arrowRect = QRect(0, 0, maxArrowSize, maxArrowSize * 0.5);
1186 arrowRect.moveCenter(availableRect.center());
1187 arrowRect = adjusted(arrowRect, -arrowWidth * 0.5);
1188
1189 const std::vector<QPointF> polygonPoints = {
1190 QPointF(arrowRect.x(), arrowRect.y()),
1191 QPointF(arrowRect.x() + arrowRect.width() * 0.5, arrowRect.y() + arrowRect.height()),
1192 QPointF(arrowRect.x() + arrowRect.width(), arrowRect.y())
1193 };
1194 const auto isEnabled = option->state.testFlag(QStyle::State_Enabled);
1195
1196 paintOnStackTop(*painter, [&](auto &p) {
1197 p.translate(option->rect.center());
1198 switch (element) {
1199 default:
1200 case PE_IndicatorArrowDown:
1201 break;
1202 case PE_IndicatorArrowUp:
1203 p.rotate(180);
1204 break;
1205 case PE_IndicatorArrowLeft:
1206 p.rotate(90);
1207 break;
1208 case PE_IndicatorArrowRight:
1209 p.rotate(-90);
1210 break;
1211 }
1212 p.translate(-option->rect.center());
1213
1214 p.setOpacity(isEnabled ? 1.0 : ohos_id_alpha_disabled);
1215 drawPolyline(
1216 p,
1217 QPen(
1218 option->palette.color(QPalette::Active, QPalette::WindowText),
1219 arrowWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin), Qt::NoBrush,
1220 polygonPoints);
1221 });
1222 break;
1223 }
1224 case PE_IndicatorButtonDropDown: {
1225 if (qobjectIsInstanceOf<QToolButton>(widget))
1226 break;
1227 QCommonStyle::drawPrimitive(element, option, painter, widget);
1228 break;
1229 }
1230 case PE_PanelItemViewItem:
1231 if (qStyleOptionIs<QStyleOptionViewItem>(option)) {
1232 drawPanelItemViewItem(
1233 *painter, *qstyleoption_cast<const QStyleOptionViewItem *>(option),
1234 widget != nullptr && qstrcmp(widget->metaObject()->className(), "QComboBoxListView") == 0);
1235 }
1236 break;
1237 case PE_FrameTabWidget:
1238 if (qStyleOptionIs<QStyleOptionTabWidgetFrame>(option)) {
1239 QRect rect = option->rect;
1240
1241 switch (qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(option)->shape) {
1242 case QTabBar::RoundedNorth:
1243 case QTabBar::TriangularNorth:
1244 rect.adjust(0, tabWidgetFrameWidth, 0, 0);
1245 break;
1246 case QTabBar::RoundedWest:
1247 case QTabBar::TriangularWest:
1248 rect.adjust(tabWidgetFrameWidth, 0, 0, 0);
1249 break;
1250 case QTabBar::RoundedSouth:
1251 case QTabBar::TriangularSouth:
1252 rect.adjust(0, 0, 0, -(tabWidgetFrameWidth * 2));
1253 break;
1254 case QTabBar::RoundedEast:
1255 case QTabBar::TriangularEast:
1256 rect.adjust(0, 0, -(tabWidgetFrameWidth * 2), 0);
1257 break;
1258 }
1259
1260 drawRect(
1261 *painter, QPen(option->palette.color(QPalette::Dark), tabWidgetFrameWidth),
1262 Qt::NoBrush, rect);
1263 }
1264 break;
1265 case PE_PanelTipLabel:
1266 if (qStyleOptionIs<QStyleOptionFrame>(option))
1267 drawToolTip(*painter, *qstyleoption_cast<const QStyleOptionFrame *>(option));
1268 break;
1269 default:
1270 QCommonStyle::drawPrimitive(element, option, painter, widget);
1271 break;
1272 }
1273}
1274
1275void QOhosStyle::drawControl(
1276 ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
1277{
1278 switch (element) {
1279 case CE_HeaderSection:
1280 if (qStyleOptionIs<QStyleOptionHeader>(option))
1281 drawHeaderSection(*painter, *qstyleoption_cast<const QStyleOptionHeader *>(option));
1282 break;
1283 case CE_MenuBarEmptyArea:
1284 break;
1285 case CE_MenuItem:
1286 if (qStyleOptionIs<QStyleOptionMenuItem>(option)) {
1287 const auto *menuItemStyleOption = qstyleoption_cast<const QStyleOptionMenuItem *>(option);
1288 drawMenuItem(
1289 *painter, *menuItemStyleOption, pixelMetric(PM_IndicatorWidth, option, widget),
1290 pixelMetric(PM_SmallIconSize, option, widget));
1291 if (menuItemStyleOption->menuItemType == QStyleOptionMenuItem::SubMenu) {
1292 const auto iconSize = pixelMetric(PM_SmallIconSize, option, widget);
1293 const auto menuItemRect = menuItemStyleOption->rect;
1294
1295 QStyleOptionMenuItem menuArrowStyleOption = *menuItemStyleOption;
1296 menuArrowStyleOption.rect = QRect(
1297 menuItemRect.right() - itemViewItemHorizontalMargin - iconSize,
1298 menuItemRect.top() + (menuItemRect.height() - iconSize) / 2, iconSize, iconSize);
1299 QColor arrowColor = menuItemStyleOption->palette.color(QPalette::Active, QPalette::Text);
1300 menuArrowStyleOption.palette.setColor(
1301 QPalette::Active, QPalette::WindowText, arrowColor);
1302 drawPrimitive(
1303 menuArrowStyleOption.direction == Qt::RightToLeft ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight,
1304 &menuArrowStyleOption, painter, widget);
1305 }
1306 }
1307 break;
1308 case CE_MenuTearoff:
1309 drawMenuTearOff(*painter, *option);
1310 break;
1311 case CE_ProgressBarGroove:
1312 if (qstyleoption_cast<const QStyleOptionProgressBar *>(option)) {
1313 drawRoundedRect(
1314 *painter, Qt::NoPen, option->palette.color(QPalette::Active, QPalette::Mid),
1315 getProgressBarGrooveRect(*qstyleoption_cast<const QStyleOptionProgressBar *>(option)),
1316 progressBarCornerRadius, progressBarCornerRadius);
1317 }
1318 break;
1319 case CE_ProgressBarContents:
1320 if (qstyleoption_cast<const QStyleOptionProgressBar *>(option))
1321 drawProgressBarContent(*painter, *qstyleoption_cast<const QStyleOptionProgressBar *>(option));
1322 break;
1323 case CE_ProgressBarLabel:
1324 break;
1325 case CE_Splitter:
1326 drawSplitter(*painter, *option);
1327 break;
1328 case CE_ToolBar:
1329 drawToolBarbackground(*painter, *option);
1330 break;
1331 case CE_ScrollBarSlider:
1332 if (qStyleOptionIs<QStyleOptionSlider>(option))
1333 drawScrollBarSlider(*painter, *qstyleoption_cast<const QStyleOptionSlider *>(option));
1334 break;
1335 case CE_TabBarTabShape: {
1336 auto tabOption = qstyleoption_cast<const QStyleOptionTab *>(option);
1337 if (tabOption != nullptr)
1338 drawTabBarTabShape(*painter, *tabOption);
1339 break;
1340 }
1341 case CE_TabBarTabLabel: {
1342 auto tabOption = qstyleoption_cast<const QStyleOptionTab *>(option);
1343 if (tabOption != nullptr) {
1344 auto copy = *tabOption;
1345 if (!option->state.testFlag(State_Selected))
1346 copy.palette.setCurrentColorGroup(QPalette::Inactive);
1347 QCommonStyle::drawControl(element, &copy, painter, widget);
1348 }
1349 break;
1350 }
1351 case CE_PushButtonLabel: {
1352 if (qStyleOptionIs<QStyleOptionButton>(option)) {
1353 auto buttonOption = qstyleoption_cast<const QStyleOptionButton *>(option);
1354
1355 auto buttonOptionCopy = *buttonOption;
1356 buttonOptionCopy.palette.setColor(QPalette::ButtonText, getPushButtonTextColor(*buttonOption));
1357
1358 QCommonStyle::drawControl(element, &buttonOptionCopy, painter, widget);
1359 }
1360 break;
1361 }
1362 case CE_DockWidgetTitle: {
1363 const auto *dockWidgetOption = qstyleoption_cast<const QStyleOptionDockWidget *>(option);
1364 if (dockWidgetOption == nullptr)
1365 break;
1366 auto dockWidgetOptionCopy = *dockWidgetOption;
1367 if (!dockWidgetOptionCopy.title.isEmpty()) {
1368 QRect titleRect = subElementRect(SE_DockWidgetTitleBarText, option, widget);
1369 if (dockWidgetOptionCopy.verticalTitleBar) {
1370 QRect rect = dockWidgetOptionCopy.rect;
1371 QRect verticalRect = rect.transposed();
1372 titleRect = QRect(
1373 verticalRect.left() + rect.bottom() - titleRect.bottom(),
1374 verticalRect.top() + titleRect.left() - rect.left(),
1375 titleRect.height(),
1376 titleRect.width());
1377 }
1378 dockWidgetOptionCopy.title = painter->fontMetrics().elidedText(
1379 dockWidgetOptionCopy.title, Qt::ElideRight, titleRect.width());
1380 }
1381 QCommonStyle::drawControl(element, &dockWidgetOptionCopy, painter, widget);
1382 break;
1383 }
1384 default:
1385 QCommonStyle::drawControl(element, option, painter, widget);
1386 }
1387}
1388
1389void QOhosStyle::drawComplexControl(
1390 ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
1391{
1392 switch (control) {
1393 case CC_Slider: {
1394 const auto sliderStyleOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
1395 if (sliderStyleOption != nullptr) {
1396 if (option->subControls.testFlag(SC_SliderGroove)) {
1397 auto sliderStyleOptionCopy = *sliderStyleOption;
1398 sliderStyleOptionCopy.rect = subControlRect(control, option, SC_SliderGroove, widget);
1399 drawSliderGroove(*painter, sliderStyleOptionCopy);
1400 }
1401 if (option->subControls.testFlag(SC_SliderHandle)) {
1402 auto sliderStyleOptionCopy = *sliderStyleOption;
1403 sliderStyleOptionCopy.rect = subControlRect(control, option, SC_SliderHandle, widget);
1404 drawSliderThumb(*painter, sliderStyleOptionCopy);
1405 }
1406 }
1407 break;
1408 }
1409 case CC_SpinBox:
1410 if (qStyleOptionIs<QStyleOptionSpinBox>(option)) {
1411 auto spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
1412 auto frameRect = subControlRect(control, spinBox, SC_SpinBoxFrame, widget);
1413 auto frameWidth = pixelMetric(PM_SpinBoxFrameWidth, spinBox, widget);
1414 drawSpinBoxFrame(*painter, *spinBox, frameRect, frameWidth);
1415
1416 auto copy = *spinBox;
1417 copy.frame = false;
1418
1419 QCommonStyle::drawComplexControl(control, &copy, painter, widget);
1420 }
1421 break;
1422 case CC_ScrollBar: {
1423 const auto scrollBarStyleOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
1424 if (scrollBarStyleOption != nullptr) {
1425 if (option->subControls.testFlag(SC_ScrollBarSlider)) {
1426 auto scrollBarStyleOptionCopy = *scrollBarStyleOption;
1427 scrollBarStyleOptionCopy.rect = subControlRect(control, option, SC_ScrollBarSlider, widget);
1428 drawControl(CE_ScrollBarSlider, &scrollBarStyleOptionCopy, painter, widget);
1429 }
1430 }
1431 break;
1432 }
1433 case CC_GroupBox: {
1434 const auto groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(option);
1435 if (groupBox != nullptr) {
1436 auto groupBoxCopy = *groupBox;
1437 groupBoxCopy.textAlignment |= Qt::AlignVCenter;
1438
1439 const auto colorGroup = option->state.testFlag(QStyle::State_Enabled)
1440 ? QPalette::Active
1441 : QPalette::Disabled;
1442 if (!groupBoxCopy.palette.isBrushSet(colorGroup, QPalette::WindowText))
1443 groupBoxCopy.textColor = groupBoxCopy.palette.color(QPalette::Text);
1444
1445 QCommonStyle::drawComplexControl(control, &groupBoxCopy, painter, widget);
1446 }
1447 break;
1448 }
1449 case CC_ComboBox: {
1450 if (qStyleOptionIs<QStyleOptionComboBox>(option)) {
1451 const auto comboBoxStyleOption = qstyleoption_cast<const QStyleOptionComboBox *>(option);
1452 if (option->subControls.testFlag(SC_ComboBoxFrame)) {
1453 auto comboBoxStyleOptionCopy = *comboBoxStyleOption;
1454 comboBoxStyleOptionCopy.rect = subControlRect(control, option, SC_ComboBoxFrame, widget);
1455 drawComboBoxFrame(*painter, comboBoxStyleOptionCopy);
1456 }
1457
1458 if (option->subControls.testFlag(SC_ComboBoxArrow)) {
1459 auto optionCopy = *option;
1460 auto comboBoxOptions = static_cast<QStyleOption *>(&optionCopy);
1461 comboBoxOptions->rect = subControlRect(control, option, SC_ComboBoxArrow, widget);
1462 drawPrimitive(PE_IndicatorArrowDown, comboBoxOptions, painter, widget);
1463 }
1464 } else {
1465 QCommonStyle::drawComplexControl(control, option, painter, widget);
1466 }
1467 break;
1468 }
1469 default:
1470 QCommonStyle::drawComplexControl(control, option, painter, widget);
1471 }
1472}
1473
1474QRect QOhosStyle::subControlRect(
1475 ComplexControl control, const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const
1476{
1477 switch (control) {
1478 case CC_Slider:
1479 if (qStyleOptionIs<QStyleOptionSlider>(option)) {
1480 const auto sliderStyleOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
1481 switch (subControl) {
1482 case SC_SliderGroove: {
1483 const auto sliderLengthOffset = pixelMetric(PM_SliderLength, option, widget) * 0.5;
1484 auto grooveRect = QCommonStyle::subControlRect(control, option, subControl, widget);
1485
1486 if (sliderStyleOption->orientation == Qt::Horizontal) {
1487 grooveRect.adjust(sliderLengthOffset, 0, -sliderLengthOffset, 0);
1488 grooveRect.setHeight(sliderGrooveHeight);
1489 grooveRect.moveTop(option->rect.center().y() - sliderGrooveHeight * 0.5);
1490 } else {
1491 grooveRect.adjust(0, sliderLengthOffset, 0, -sliderLengthOffset);
1492 grooveRect.setWidth(sliderGrooveHeight);
1493 grooveRect.moveLeft(option->rect.center().x() - sliderGrooveHeight * 0.5);
1494 }
1495
1496 return grooveRect;
1497 }
1498 case SC_SliderHandle: {
1499 auto handleRect = QCommonStyle::subControlRect(control, option, subControl, widget);
1500 if (sliderStyleOption->orientation == Qt::Horizontal)
1501 handleRect.moveTop(option->rect.center().y() - handleRect.height() * 0.5);
1502 else
1503 handleRect.moveLeft(option->rect.center().x() - handleRect.width() * 0.5);
1504 return handleRect;
1505 }
1506 default:
1507 return QCommonStyle::subControlRect(control, option, subControl, widget);
1508 }
1509 } else {
1510 return QCommonStyle::subControlRect(control, option, subControl, widget);
1511 }
1512 case CC_SpinBox:
1513 if (qStyleOptionIs<QStyleOptionSpinBox>(option)) {
1514 const auto spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
1515 auto frameWidth = spinBox->frame ? pixelMetric(PM_SpinBoxFrameWidth, option, widget) : 0;
1516
1517 switch (subControl) {
1518 case SC_SpinBoxFrame: {
1519 auto frameRect = spinBox->rect;
1520 frameRect.setHeight(spinBoxFrameHeight);
1521 frameRect.moveCenter(spinBox->rect.center());
1522 return visualRect(spinBox->direction, spinBox->rect, frameRect);
1523 }
1524 case SC_SpinBoxDown:
1525 case SC_SpinBoxUp: {
1526 auto frameRect = subControlRect(CC_SpinBox, option, SC_SpinBoxFrame, widget);
1527 if (spinBox->buttonSymbols == QAbstractSpinBox::NoButtons)
1528 return QRect();
1529 auto buttonRect = QRect(
1530 frameRect.left() + frameRect.width() - spinBoxButtonBoxWidth,
1531 frameRect.top() + (frameRect.height() - spinBoxFrameHeight) * 0.5,
1532 spinBoxButtonBoxWidth, spinBoxButtonBoxHeight);
1533 if (subControl == SC_SpinBoxDown)
1534 buttonRect.translate(0, spinBoxButtonBoxHeight);
1535 return visualRect(spinBox->direction, frameRect, buttonRect);
1536 }
1537 case SC_SpinBoxEditField: {
1538 auto editRect = adjusted(spinBox->rect, -frameWidth);
1539 if (spinBox->buttonSymbols != QAbstractSpinBox::NoButtons) {
1540 editRect.setWidth(spinBox->rect.width() - frameWidth - spinBoxButtonBoxWidth);
1541 editRect = visualRect(spinBox->direction, spinBox->rect, editRect);
1542 }
1543 return editRect;
1544 }
1545 default:
1546 return QCommonStyle::subControlRect(control, option, subControl, widget);
1547 }
1548 } else {
1549 return QCommonStyle::subControlRect(control, option, subControl, widget);
1550 }
1551 case CC_ScrollBar:
1552 if (qStyleOptionIs<QStyleOptionSlider>(option)) {
1553 auto scrollBoxStyleOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
1554 auto defaultUpButtonRect = QCommonStyle::subControlRect(control, option, SC_ScrollBarSubLine, widget);
1555 auto defaultDownButtonRect = QCommonStyle::subControlRect(control, option, SC_ScrollBarAddLine, widget);
1556 switch (subControl) {
1557 case SC_ScrollBarAddLine:
1558 case SC_ScrollBarSubLine:
1559 return QRect();
1560 case SC_ScrollBarSubPage: {
1561 auto subPageRect = QCommonStyle::subControlRect(control, option, subControl, widget);
1562 if (scrollBoxStyleOption->orientation == Qt::Vertical)
1563 subPageRect.moveTop(subPageRect.top() - defaultDownButtonRect.height());
1564 else
1565 subPageRect.moveLeft(subPageRect.left() - defaultDownButtonRect.width());
1566 return subPageRect;
1567 }
1568 case SC_ScrollBarAddPage: {
1569 auto addPageRect = QCommonStyle::subControlRect(control, option, subControl, widget);
1570 if (scrollBoxStyleOption->orientation == Qt::Vertical)
1571 addPageRect.moveTop(addPageRect.top() + defaultUpButtonRect.height());
1572 else
1573 addPageRect.moveLeft(addPageRect.left() + defaultUpButtonRect.width());
1574 return addPageRect;
1575 }
1576 case SC_ScrollBarSlider: {
1577 const bool activeScrollBar =
1578 option->state.testFlag(QCommonStyle::State_Enabled)
1579 && option->state.testFlag(QCommonStyle::State_MouseOver);
1580 auto scrollBarExtent = activeScrollBar ? thickScrollBarSize : thinScrollBarSize;
1581
1582 auto sliderRect = QCommonStyle::subControlRect(control, option, subControl, widget);
1583 if (scrollBoxStyleOption->orientation == Qt::Vertical) {
1584 sliderRect.setWidth(scrollBarExtent);
1585 sliderRect.setHeight(sliderRect.height() + defaultUpButtonRect.height() + defaultDownButtonRect.height());
1586 sliderRect.moveTop(sliderRect.top() - defaultUpButtonRect.height());
1587 sliderRect.moveLeft((option->rect.width() - sliderRect.width()) * 0.5);
1588 } else {
1589 sliderRect.setHeight(scrollBarExtent);
1590 sliderRect.setWidth(sliderRect.width() + defaultUpButtonRect.width() + defaultDownButtonRect.width());
1591 sliderRect.moveTop((option->rect.height() - sliderRect.height()) * 0.5);
1592 sliderRect.moveLeft(sliderRect.left() - defaultUpButtonRect.width());
1593 }
1594 return sliderRect;
1595 }
1596 default:
1597 return QCommonStyle::subControlRect(control, option, subControl, widget);
1598 }
1599 } else {
1600 return QCommonStyle::subControlRect(control, option, subControl, widget);
1601 }
1602 case CC_GroupBox:
1603 if (qStyleOptionIs<QStyleOptionGroupBox>(option)) {
1604 auto groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(option);
1605 auto labelRect =
1606 QCommonStyle::subControlRect(control, option, SC_GroupBoxLabel, widget);
1607 if (groupBox->text.isEmpty()) {
1608 labelRect.moveTop(groupBox->rect.top() + groupBoxTopPadding);
1609 labelRect.moveBottom(labelRect.top());
1610 } else {
1611 labelRect.moveTop(groupBox->rect.top() + groupBoxTopPadding + groupBoxTitleTextFontSize / 2);
1612 }
1613
1614 switch (subControl) {
1615 case SC_GroupBoxFrame:
1616 return QCommonStyle::subControlRect(control, option, subControl, widget);
1617 case SC_GroupBoxLabel: {
1618 return labelRect;
1619 }
1620 case SC_GroupBoxCheckBox: {
1621 auto checkBoxRect =
1622 QCommonStyle::subControlRect(control, option, subControl, widget);
1623 checkBoxRect.moveTop(labelRect.center().y() - checkBoxRect.height() / 2);
1624 return checkBoxRect;
1625 }
1626 case SC_GroupBoxContents: {
1627 auto contentsRect =
1628 QCommonStyle::subControlRect(control, option, SC_GroupBoxContents, widget);
1629 contentsRect.setTop(labelRect.bottom() + (groupBox->text.isEmpty() ? 0 : groupBoxTitleToContentSpacing));
1630 contentsRect.setBottom(groupBox->rect.bottom() - groupBoxBottomPadding);
1631 contentsRect.adjust(groupBoxHorizontalPadding, 0, -groupBoxHorizontalPadding, 0);
1632 return contentsRect;
1633 }
1634 default:
1635 return QCommonStyle::subControlRect(control, option, subControl, widget);
1636 }
1637 } else {
1638 return QCommonStyle::subControlRect(control, option, subControl, widget);
1639 }
1640 case CC_ComboBox:
1641 if (qStyleOptionIs<QStyleOptionComboBox>(option)) {
1642 auto comboBoxStyleOption = qstyleoption_cast<const QStyleOptionComboBox *>(option);
1643 const auto arrowSize = pixelMetric(PM_MenuButtonIndicator, option, widget);
1644 switch (subControl) {
1645 case SC_ComboBoxFrame: {
1646 return adjusted(comboBoxStyleOption->rect, -comboBoxFocusFrameWidth);;
1647 }
1648 case SC_ComboBoxEditField: {
1649 return comboBoxStyleOption->rect.adjusted(
1650 comboBoxFrameLeftRightPadding, comboBoxVerticalMargin,
1651 -arrowSize - comboBoxFrameLeftRightPadding, -comboBoxVerticalMargin);
1652 }
1653 case SC_ComboBoxArrow: {
1654 auto arrowIconRect = comboBoxStyleOption->rect;
1655 arrowIconRect.setWidth(arrowSize);
1656 arrowIconRect.moveRight(comboBoxStyleOption->rect.width());
1657 return arrowIconRect;
1658 }
1659 default:
1660 return QCommonStyle::subControlRect(control, option, subControl, widget);
1661 }
1662 } else {
1663 return QCommonStyle::subControlRect(control, option, subControl, widget);
1664 }
1665 case CC_TitleBar:
1666 if (qStyleOptionIs<QStyleOptionTitleBar>(option)) {
1667 auto titleBarOption = qstyleoption_cast<const QStyleOptionTitleBar *>(option);
1668 return visualRect(
1669 titleBarOption->direction, titleBarOption->rect, titleBarSubControlRect(*titleBarOption, subControl));
1670 } else {
1671 return QCommonStyle::subControlRect(control, option, subControl, widget);
1672 }
1673 default:
1674 return QCommonStyle::subControlRect(control, option, subControl, widget);
1675 }
1676}
1677
1678QRect QOhosStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
1679{
1680 switch (element) {
1681 case SE_PushButtonContents:
1682 return option->rect.adjusted(pushButtonHorizontalMargin, 0, -pushButtonHorizontalMargin, 0);
1683 case SE_ItemViewItemFocusRect:
1684 return option->rect.adjusted(focusFrameWidth, focusFrameWidth, -focusFrameWidth, -focusFrameWidth);
1685 case SE_ProgressBarGroove:
1686 case SE_ProgressBarContents:
1687 return option->rect;
1688 case SE_ProgressBarLabel:
1689 return QRect();
1690 case SE_LineEditContents: {
1691 int horizontalMargin =
1692 widget != nullptr && qobjectIsInstanceOf<QAbstractSpinBox>(widget->parent())
1693 ? spinBoxEditHorizontalMargin
1694 : lineEditHorizontalMargin;
1695 return option->rect.adjusted(
1696 horizontalMargin, lineEditVerticalMargin, -horizontalMargin, -lineEditVerticalMargin);
1697 }
1698 case SE_CheckBoxFocusRect:
1699 case SE_RadioButtonFocusRect:
1700 return QRect(
1701 checkBoxOrRadioButtonFocusStrokeWidth, (option->rect.height() - checkBoxOrRadioButtonHoverSize) * 0.5,
1702 checkBoxOrRadioButtonHoverSize, checkBoxOrRadioButtonHoverSize);
1703 case SE_CheckBoxClickRect:
1704 return subElementRect(SE_CheckBoxIndicator, option, widget)
1705 | subElementRect(SE_CheckBoxContents, option, widget);
1706 case SE_RadioButtonClickRect:
1707 return subElementRect(SE_RadioButtonIndicator, option, widget)
1708 | subElementRect(SE_RadioButtonContents, option, widget);
1709 default:
1710 return QCommonStyle::subElementRect(element, option, widget);;
1711 }
1712}
1713
1714QSize QOhosStyle::sizeFromContents(
1715 ContentsType contents, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const
1716{
1717 switch (contents) {
1718 case CT_ItemViewItem: {
1719 const auto viewItemStyleOption = qstyleoption_cast<const QStyleOptionViewItem *>(option);
1720 if (viewItemStyleOption != nullptr) {
1721 if (isItemViewSeparator(viewItemStyleOption->index)) {
1722 return QSize(option->rect.width(), itemViewSeparatorHeight);
1723 } else {
1724 auto verticalMargin = viewItemStyleOption->features.testFlag(QStyleOptionViewItem::HasDecoration)
1725 ? itemViewItemWithDecorationVerticalMargin
1726 : itemViewItemVerticalMargin;
1727 return QCommonStyle::sizeFromContents(contents, option, contentsSize, widget)
1728 + QSize(0, 2 * verticalMargin);
1729 }
1730 } else {
1731 return QCommonStyle::sizeFromContents(contents, option, contentsSize, widget);
1732 }
1733 }
1734 case CT_PushButton: {
1735 auto sizeAlignment = (2 * pushButtonFocusFrameGap) + (2 * pushButtonFocusFrameWidth);
1736 return contentsSize + QSize(
1737 (pushButtonHorizontalMargin * 2) + sizeAlignment, (pushButtonVerticalMargin * 2) + sizeAlignment);
1738 }
1739 case CT_ComboBox: {
1740 const auto arrowSize = pixelMetric(PM_MenuButtonIndicator, option, widget);
1741 auto comboBoxSize = qStyleOptionIs<QStyleOptionComboBox>(option)
1742 && qstyleoption_cast<const QStyleOptionComboBox *>(option)->editable
1743 ? contentsSize + QSize(2 * lineEditHorizontalMargin, 0)
1744 : contentsSize;
1745 const auto frameWidthCompensation = 2 * comboBoxFocusFrameWidth;
1746 comboBoxSize.rwidth() += (arrowSize + frameWidthCompensation + 2 * comboBoxFrameLeftRightPadding);
1747 comboBoxSize.rheight() += (frameWidthCompensation + 2 * comboBoxVerticalMargin + 2 * lineEditVerticalMargin);
1748 return comboBoxSize;
1749 }
1750 case CT_ProgressBar: {
1751 const auto progressBarStyleOption = qstyleoption_cast<const QStyleOptionProgressBar *>(option);
1752 if (progressBarStyleOption != nullptr) {
1753 const bool horizontal = progressBarStyleOption->state & QStyle::State_Horizontal;
1754 if (horizontal)
1755 return QSize(contentsSize.width(), std::max(contentsSize.height(), progressBarMinHeight));
1756 else
1757 return QSize(std::max(contentsSize.height(), progressBarMinHeight), contentsSize.height());
1758 } else {
1759 return QCommonStyle::sizeFromContents(contents, option, contentsSize, widget);
1760 }
1761 }
1762 case CT_MenuItem: {
1763 const auto defaultSize = QCommonStyle::sizeFromContents(contents, option, contentsSize, widget);
1764 const auto *menuItemStyleOption = qstyleoption_cast<const QStyleOptionMenuItem *>(option);
1765 if (menuItemStyleOption != nullptr) {
1766 if (menuItemStyleOption->menuItemType == QStyleOptionMenuItem::Separator) {
1767 return QSize(defaultSize.width(), itemViewSeparatorHeight);
1768 } else {
1769 const auto menuVMargin = pixelMetric(PM_MenuVMargin, option, widget);
1770 const auto arrowWidth = menuItemStyleOption->menuItemType == QStyleOptionMenuItem::SubMenu
1771 ? menuVMargin + pixelMetric(PM_SmallIconSize, option, widget)
1772 : 0;
1773 const auto checkboxWidth = menuItemStyleOption->checkType != QStyleOptionMenuItem::NotCheckable
1774 ? menuVMargin + pixelMetric(PM_IndicatorWidth, option, widget)
1775 : 0;
1776
1777 return defaultSize
1778 + QSize(2 * itemViewItemHorizontalMargin, 2 * menuVMargin)
1779 + QSize(arrowWidth, 0)
1780 + QSize(checkboxWidth, 0);
1781 }
1782 } else {
1783 return defaultSize;
1784 }
1785 }
1786 case CT_LineEdit: {
1787 int horizontalMargin;
1788 if (widget != nullptr && qobjectIsInstanceOf<QAbstractSpinBox>(widget->parent()))
1789 horizontalMargin = spinBoxEditHorizontalMargin;
1790 else
1791 horizontalMargin = lineEditHorizontalMargin;
1792 return contentsSize + QSize(horizontalMargin * 2, lineEditVerticalMargin * 2);
1793 }
1794 case CT_SpinBox: {
1795 const auto spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
1796 if (spinBox != nullptr) {
1797 int buttonWidth;
1798 if (spinBox->subControls.testFlag(QStyle::SC_SpinBoxUp)
1799 && spinBox->subControls.testFlag(QStyle::SC_SpinBoxDown)) {
1800 buttonWidth = subControlRect(CC_SpinBox, spinBox, SC_SpinBoxDown, widget).width();
1801 } else {
1802 buttonWidth = 0;
1803 }
1804 const int frameWidth
1805 = spinBox->frame ? pixelMetric(PM_SpinBoxFrameWidth, option, widget) : 0;
1806
1807 return QSize(contentsSize.width() + buttonWidth + 2 * frameWidth + spinBoxEditHorizontalMargin * 2,
1808 std::max(contentsSize.height() + 2 * frameWidth, spinBoxFrameHeight));
1809 }
1810
1811 return contentsSize;
1812 }
1813 default:
1814 return QCommonStyle::sizeFromContents(contents, option, contentsSize, widget);
1815 }
1816}
1817
1818int QOhosStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
1819{
1820 switch (metric) {
1821 case PM_ButtonMargin:
1822 case PM_ButtonDefaultIndicator:
1823 case PM_ButtonShiftHorizontal:
1824 case PM_ButtonShiftVertical:
1825 return 0;
1826 case PM_IndicatorWidth:
1827 case PM_ExclusiveIndicatorWidth:
1828 return checkBoxOrRadioButtonSize;
1829 case PM_IndicatorHeight:
1830 case PM_ExclusiveIndicatorHeight:
1831 return checkBoxOrRadioButtonSize;
1832 case PM_FocusFrameHMargin:
1833 return qStyleOptionIs<QStyleOptionViewItem>(option)
1834 ? itemViewItemSpacing
1835 : QCommonStyle::pixelMetric(metric, option, widget);
1836 case PM_TextCursorWidth:
1837 return textCursorWidth;
1838 case PM_DefaultFrameWidth:
1839 if (qobjectIsInstanceOf<QLineEdit>(widget) || qobjectIsInstanceOf<QPushButton>(widget))
1840 return 0;
1841 else
1842 return QCommonStyle::pixelMetric(metric, option, widget);
1843 case PM_MenuBarPanelWidth:
1844 return 0;
1845 case PM_MenuBarItemSpacing:
1846 return menuBarItemSpacing;
1847 case PM_MenuBarVMargin:
1848 return menuBarVerticalMargin;
1849 case PM_MenuBarHMargin:
1850 return menuBarHorizontalMargin;
1851 case PM_MenuPanelWidth:
1852 return 0;
1853 case PM_MenuHMargin:
1854 return 0;
1855 case PM_MenuVMargin:
1856 return menuItemVerticalMargin;
1857 case PM_ProgressBarChunkWidth:
1858 return 0;
1859 case PM_SliderThickness:
1860 return sliderThickness;
1861 case PM_SliderLength:
1862 case PM_SliderControlThickness:
1863 return sliderThumbSize + 2 * sliderHoverSize;
1864 case PM_SmallIconSize:
1865 return qStyleOptionIs<QStyleOptionMenuItem>(option) || qStyleOptionIs<QStyleOptionComboBox>(option)
1866 ? smallIconSize
1867 : QCommonStyle::pixelMetric(metric, option, widget);
1868 case PM_SplitterWidth:
1869 return splitterWidth;
1870 case PM_SpinBoxFrameWidth:
1871 return spinBoxFrameStroke;
1872 case PM_ScrollBarExtent:
1873 return maxScrollBarSize;
1874 case PM_TabBarTabShiftVertical:
1875 case PM_TabBarBaseOverlap:
1876 return 0;
1877 case PM_TabBarTabHSpace:
1878 return 2 * tabBarTabRadius + 2 * tabBarTabFrameWidth;
1879 case PM_TitleBarHeight:
1880 return titleBarHeight;
1881 case PM_HeaderMargin:
1882 return headerMargin;
1883 case PM_HeaderDefaultSectionSizeVertical:
1884 return headerDefaultSectionSizeVertical;
1885 case PM_ToolTipLabelFrameWidth:
1886 return toolTipFrameWidth;
1887 case PM_MenuButtonIndicator:
1888 return 2 * QCommonStyle::pixelMetric(metric, option, widget);
1889 default:
1890 return QCommonStyle::pixelMetric(metric, option, widget);
1891 }
1892}
1893
1894int QOhosStyle::styleHint(
1895 StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *hintReturn) const
1896{
1897 switch (hint) {
1898 case SH_Slider_AbsoluteSetButtons:
1899 return Qt::LeftButton | Qt::MiddleButton;
1900 case SH_Slider_PageSetButtons:
1901 return 0;
1902 case SH_ItemView_ShowDecorationSelected:
1903 return qobjectIsInstanceOf<QTreeView>(widget) ? 0 : 1;
1904 case SH_ToolTipLabel_Opacity:
1905 return toolTipBackgroundAlpha;
1906 case SH_Menu_MouseTracking:
1907 case SH_MenuBar_MouseTracking:
1908 return 1;
1909 default:
1910 return QCommonStyle::styleHint(hint, option, widget, hintReturn);
1911 }
1912}
1913
1914void QOhosStyle::polish(QWidget *widget)
1915{
1916 QCommonStyle::polish(widget);
1917
1918 if (isHoverable(widget))
1919 widget->setAttribute(Qt::WA_Hover);
1920
1921 if (qobjectIsInstanceOf<QPushButton>(widget)) {
1922 if (qobject_cast<QPushButton *>(widget)->isDefault()) {
1923 QPalette palette = widget->palette();
1924 auto textDisabledColor = palette.color(QPalette::Active, QPalette::Base);
1925 textDisabledColor.setAlphaF(ohos_id_alpha_disabled);
1926 palette.setColor(QPalette::All, QPalette::ButtonText, palette.color(QPalette::Active, QPalette::Base));
1927 palette.setColor(QPalette::Disabled, QPalette::ButtonText, textDisabledColor);
1928 widget->setPalette(palette);
1929 }
1930 } else if (qobjectIsInstanceOf<QScrollBar>(widget)) {
1931 widget->setAttribute(Qt::WA_NoSystemBackground);
1932 widget->setAttribute(Qt::WA_OpaquePaintEvent, false);
1933 } else if (qobjectIsInstanceOf<QGroupBox>(widget)) {
1934 QFont font;
1935 font.setPointSize(groupBoxTitleTextFontSize);
1936 font.setBold(true);
1937 widget->setFont(font);
1938
1939 auto groupBox = qobject_cast<QGroupBox *>(widget);
1940 groupBox->setAlignment(Qt::AlignHCenter);
1941 } else if (qobjectIsInstanceOf<QListView>(widget)) {
1942 qobject_cast<QListView *>(widget)->viewport()->setAttribute(Qt::WA_Hover);
1943 } else if (qobjectIsInstanceOf<QComboBox>(widget)) {
1944 auto comboBox = qobject_cast<QComboBox *>(widget);
1945 comboBox->setItemDelegate(new QStyledItemDelegate(comboBox->view()));
1946 } else if (qobjectIsInstanceOf<QMdiSubWindow>(widget)) {
1947 widget->setAttribute(Qt::WA_TranslucentBackground);
1948 widget->setAutoFillBackground(false);
1949
1950 auto palette = widget->palette();
1951 palette.setColor(QPalette::Highlight, palette.color(QPalette::Active, QPalette::Base));
1952 palette.setColor(QPalette::Inactive, QPalette::Dark, palette.color(QPalette::Inactive, QPalette::Base));
1953 palette.setColor(
1954 QPalette::Inactive, QPalette::Window,
1955 makeInactiveOrDisabledFromColor(palette.color(QPalette::Active, QPalette::AlternateBase)));
1956 palette.setColor(QPalette::HighlightedText, Qt::black);
1957 widget->setPalette(palette);
1958 }
1959}
1960
1961void QOhosStyle::unpolish(QWidget *widget)
1962{
1963 if (isHoverable(widget)) {
1964 widget->setAttribute(Qt::WA_Hover, false);
1965 } else if (qobjectIsInstanceOf<QScrollBar>(widget)) {
1966 widget->setAttribute(Qt::WA_NoSystemBackground, false);
1967 widget->setAttribute(Qt::WA_OpaquePaintEvent);
1968 } else if (qobjectIsInstanceOf<QListView>(widget)) {
1969 qobject_cast<QListView *>(widget)->viewport()->setAttribute(Qt::WA_Hover, false);
1970 }
1971 QCommonStyle::unpolish(widget);
1972}
1973
1974QPalette QOhosStyle::standardPalette() const
1975{
1976 return *QGuiApplicationPrivate::platformTheme()->palette();
1977}