Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/menubar.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //______________________________________________________________ | ||||
6 | QSize Style::menuBarItemSizeFromContents(const QStyleOption *, const QSize &contentsSize, const QWidget *) const | ||||
7 | { | ||||
8 | return expandSize(contentsSize, Metrics::MenuBarItem_MarginWidth, Metrics::MenuBarItem_MarginHeight); | ||||
9 | } | ||||
10 | | ||||
11 | //___________________________________________________________________________________ | ||||
12 | bool Style::drawMenuBarItemControl(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
13 | { | ||||
14 | // cast option and check | ||||
15 | const auto menuItemOption = qstyleoption_cast<const QStyleOptionMenuItem *>(option); | ||||
16 | if (!menuItemOption) | ||||
17 | return true; | ||||
18 | | ||||
19 | // copy rect and palette | ||||
20 | const auto &rect(option->rect); | ||||
21 | const auto &palette(option->palette); | ||||
22 | | ||||
23 | // store state | ||||
24 | const State &state(option->state); | ||||
25 | const bool enabled(state & State_Enabled); | ||||
26 | const bool selected(enabled && (state & State_Selected)); | ||||
27 | const bool sunken(enabled && (state & State_Sunken)); | ||||
28 | const bool useStrongFocus(StyleConfigData::menuItemDrawStrongFocus()); | ||||
29 | | ||||
30 | // render hover and focus | ||||
31 | if (useStrongFocus && (selected || sunken)) { | ||||
32 | QColor outlineColor; | ||||
33 | if (sunken) | ||||
34 | outlineColor = _helper->focusColor(palette); | ||||
35 | else if (selected) | ||||
36 | outlineColor = _helper->hoverColor(palette); | ||||
37 | _helper->renderFocusRect(painter, rect, outlineColor); | ||||
38 | } | ||||
39 | | ||||
40 | /* | ||||
41 | check if item as an icon, in which case only the icon should be rendered | ||||
42 | consistently with comment in QMenuBarPrivate::calcActionRects | ||||
43 | */ | ||||
44 | if (!menuItemOption->icon.isNull()) { | ||||
45 | // icon size is forced to SmallIconSize | ||||
46 | const auto iconSize = pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget); | ||||
47 | const auto iconRect = centerRect(rect, iconSize, iconSize); | ||||
48 | | ||||
49 | // decide icon mode and state | ||||
50 | QIcon::Mode iconMode; | ||||
51 | QIcon::State iconState; | ||||
52 | if (!enabled) { | ||||
53 | iconMode = QIcon::Disabled; | ||||
54 | iconState = QIcon::Off; | ||||
55 | | ||||
56 | } else { | ||||
57 | if (useStrongFocus && sunken) | ||||
58 | iconMode = QIcon::Selected; | ||||
59 | else if (useStrongFocus && selected) | ||||
60 | iconMode = QIcon::Active; | ||||
61 | else | ||||
62 | iconMode = QIcon::Normal; | ||||
63 | | ||||
64 | iconState = sunken ? QIcon::On : QIcon::Off; | ||||
65 | } | ||||
66 | | ||||
67 | const auto pixmap = menuItemOption->icon.pixmap(iconSize, iconMode, iconState); | ||||
68 | drawItemPixmap(painter, iconRect, Qt::AlignCenter, pixmap); | ||||
69 | | ||||
70 | // render outline | ||||
71 | if (!useStrongFocus && (selected || sunken)) { | ||||
72 | QColor outlineColor; | ||||
73 | if (sunken) | ||||
74 | outlineColor = _helper->focusColor(palette); | ||||
75 | else if (selected) | ||||
76 | outlineColor = _helper->hoverColor(palette); | ||||
77 | | ||||
78 | _helper->renderFocusLine(painter, iconRect, outlineColor); | ||||
79 | } | ||||
80 | | ||||
81 | } else { | ||||
82 | // get text rect | ||||
83 | const int textFlags(Qt::AlignCenter | _mnemonics->textFlags()); | ||||
84 | const auto textRect = option->fontMetrics.boundingRect(rect, textFlags, menuItemOption->text); | ||||
85 | | ||||
86 | // render text | ||||
87 | const QPalette::ColorRole role = (useStrongFocus && sunken) ? QPalette::HighlightedText : QPalette::WindowText; | ||||
88 | drawItemText(painter, textRect, textFlags, palette, enabled, menuItemOption->text, role); | ||||
89 | | ||||
90 | // render outline | ||||
91 | if (!useStrongFocus && (selected || sunken)) { | ||||
92 | QColor outlineColor; | ||||
93 | if (sunken) | ||||
94 | outlineColor = _helper->focusColor(palette); | ||||
95 | else if (selected) | ||||
96 | outlineColor = _helper->hoverColor(palette); | ||||
97 | | ||||
98 | _helper->renderFocusLine(painter, textRect, outlineColor); | ||||
99 | } | ||||
100 | } | ||||
101 | | ||||
102 | return true; | ||||
103 | } | ||||
104 | | ||||
105 | } |