Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/groupbox.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //______________________________________________________________ | ||||
6 | QRect Style::groupBoxSubControlRect(const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const | ||||
7 | { | ||||
8 | QRect rect = option->rect; | ||||
9 | switch (subControl) { | ||||
10 | case SC_GroupBoxFrame: | ||||
11 | return rect; | ||||
12 | | ||||
13 | case SC_GroupBoxContents: { | ||||
14 | // cast option and check | ||||
15 | const auto groupBoxOption = qstyleoption_cast<const QStyleOptionGroupBox *>(option); | ||||
16 | if (!groupBoxOption) | ||||
17 | break; | ||||
18 | | ||||
19 | // take out frame width | ||||
20 | rect = insideMargin(rect, Metrics::Frame_FrameWidth); | ||||
21 | | ||||
22 | // get state | ||||
23 | const bool checkable(groupBoxOption->subControls & QStyle::SC_GroupBoxCheckBox); | ||||
24 | const bool emptyText(groupBoxOption->text.isEmpty()); | ||||
25 | | ||||
26 | // calculate title height | ||||
27 | int titleHeight(0); | ||||
28 | if (!emptyText) | ||||
29 | titleHeight = groupBoxOption->fontMetrics.height(); | ||||
30 | if (checkable) | ||||
31 | titleHeight = qMax(titleHeight, int(Metrics::CheckBox_Size)); | ||||
32 | | ||||
33 | // add margin | ||||
34 | if (titleHeight > 0) | ||||
35 | titleHeight += 2 * Metrics::GroupBox_TitleMarginWidth; | ||||
36 | | ||||
37 | rect.adjust(0, titleHeight, 0, 0); | ||||
38 | return rect; | ||||
39 | } | ||||
40 | | ||||
41 | case SC_GroupBoxCheckBox: | ||||
42 | case SC_GroupBoxLabel: { | ||||
43 | // cast option and check | ||||
44 | const auto groupBoxOption = qstyleoption_cast<const QStyleOptionGroupBox *>(option); | ||||
45 | if (!groupBoxOption) | ||||
46 | break; | ||||
47 | | ||||
48 | // take out frame width | ||||
49 | rect = insideMargin(rect, Metrics::Frame_FrameWidth); | ||||
50 | | ||||
51 | const bool emptyText(groupBoxOption->text.isEmpty()); | ||||
52 | const bool checkable(groupBoxOption->subControls & QStyle::SC_GroupBoxCheckBox); | ||||
53 | | ||||
54 | // calculate title height | ||||
55 | int titleHeight(0); | ||||
56 | int titleWidth(0); | ||||
57 | if (!emptyText) { | ||||
58 | const QFontMetrics fontMetrics = option->fontMetrics; | ||||
59 | titleHeight = qMax(titleHeight, fontMetrics.height()); | ||||
60 | titleWidth += fontMetrics.size(_mnemonics->textFlags(), groupBoxOption->text).width(); | ||||
61 | } | ||||
62 | | ||||
63 | if (checkable) { | ||||
64 | titleHeight = qMax(titleHeight, int(Metrics::CheckBox_Size)); | ||||
65 | titleWidth += Metrics::CheckBox_Size; | ||||
66 | if (!emptyText) | ||||
67 | titleWidth += Metrics::CheckBox_ItemSpacing; | ||||
68 | } | ||||
69 | | ||||
70 | // adjust height | ||||
71 | auto titleRect(rect); | ||||
72 | titleRect.setHeight(titleHeight); | ||||
73 | titleRect.translate(0, Metrics::GroupBox_TitleMarginWidth); | ||||
74 | | ||||
75 | // center | ||||
76 | titleRect = centerRect(titleRect, titleWidth, titleHeight); | ||||
77 | | ||||
78 | if (subControl == SC_GroupBoxCheckBox) { | ||||
79 | // vertical centering | ||||
80 | titleRect = centerRect(titleRect, titleWidth, Metrics::CheckBox_Size); | ||||
81 | | ||||
82 | // horizontal positioning | ||||
83 | const QRect subRect(titleRect.topLeft(), QSize(Metrics::CheckBox_Size, titleRect.height())); | ||||
84 | return visualRect(option->direction, titleRect, subRect); | ||||
85 | | ||||
86 | } else { | ||||
87 | // vertical centering | ||||
88 | QFontMetrics fontMetrics = option->fontMetrics; | ||||
89 | titleRect = centerRect(titleRect, titleWidth, fontMetrics.height()); | ||||
90 | | ||||
91 | // horizontal positioning | ||||
92 | auto subRect(titleRect); | ||||
93 | if (checkable) | ||||
94 | subRect.adjust(Metrics::CheckBox_Size + Metrics::CheckBox_ItemSpacing, 0, 0, 0); | ||||
95 | return visualRect(option->direction, titleRect, subRect); | ||||
96 | } | ||||
97 | } | ||||
98 | | ||||
99 | default: | ||||
100 | break; | ||||
101 | } | ||||
102 | | ||||
103 | return ParentStyleClass::subControlRect(CC_GroupBox, option, subControl, widget); | ||||
104 | } | ||||
105 | | ||||
106 | //______________________________________________________________ | ||||
107 | bool Style::drawFrameGroupBoxPrimitive(const QStyleOption *option, QPainter *painter, const QWidget *) const | ||||
108 | { | ||||
109 | // cast option and check | ||||
110 | const auto frameOption(qstyleoption_cast<const QStyleOptionFrame *>(option)); | ||||
111 | if (!frameOption) | ||||
112 | return true; | ||||
113 | | ||||
114 | // no frame for flat groupboxes | ||||
115 | #if BREEZE_USE_KDE4 | ||||
116 | QStyleOptionFrameV2 frameOption2(*frameOption); | ||||
117 | if (frameOption2.features & QStyleOptionFrameV2::Flat) | ||||
118 | return true; | ||||
119 | #else | ||||
120 | if (frameOption->features & QStyleOptionFrame::Flat) | ||||
121 | return true; | ||||
122 | #endif | ||||
123 | | ||||
124 | // normal frame | ||||
125 | const auto &palette(option->palette); | ||||
126 | const auto background(_helper->frameBackgroundColor(palette)); | ||||
127 | const auto outline(_helper->frameOutlineColor(palette)); | ||||
128 | | ||||
129 | /* | ||||
130 | * need to reset painter's clip region in order to paint behind textbox label | ||||
131 | * (was taken out in QCommonStyle) | ||||
132 | */ | ||||
133 | | ||||
134 | painter->setClipRegion(option->rect); | ||||
135 | _helper->renderFrame(painter, option->rect, background, outline); | ||||
136 | | ||||
137 | return true; | ||||
138 | } | ||||
139 | | ||||
140 | //______________________________________________________________ | ||||
141 | bool Style::drawGroupBoxComplexControl(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const | ||||
142 | { | ||||
143 | // base class method | ||||
144 | ParentStyleClass::drawComplexControl(CC_GroupBox, option, painter, widget); | ||||
145 | | ||||
146 | // cast option and check | ||||
147 | const auto groupBoxOption = qstyleoption_cast<const QStyleOptionGroupBox *>(option); | ||||
148 | if (!groupBoxOption) | ||||
149 | return true; | ||||
150 | | ||||
151 | // do nothing if either label is not selected or groupbox is empty | ||||
152 | if (!(option->subControls & QStyle::SC_GroupBoxLabel) || groupBoxOption->text.isEmpty()) { | ||||
153 | return true; | ||||
154 | } | ||||
155 | | ||||
156 | // store palette and rect | ||||
157 | const auto &palette(option->palette); | ||||
158 | | ||||
159 | // check focus state | ||||
160 | const State &state(option->state); | ||||
161 | const bool enabled(state & State_Enabled); | ||||
162 | const bool hasFocus(enabled && (option->state & State_HasFocus)); | ||||
163 | if (!hasFocus) | ||||
164 | return true; | ||||
165 | | ||||
166 | // alignment | ||||
167 | const int textFlags(groupBoxOption->textAlignment | _mnemonics->textFlags()); | ||||
168 | | ||||
169 | // update animation state | ||||
170 | _animations->widgetStateEngine().updateState(widget, AnimationFocus, hasFocus); | ||||
171 | const bool isFocusAnimated(_animations->widgetStateEngine().isAnimated(widget, AnimationFocus)); | ||||
172 | const qreal opacity(_animations->widgetStateEngine().opacity(widget, AnimationFocus)); | ||||
173 | | ||||
174 | // get relevant rect | ||||
175 | auto textRect = subControlRect(CC_GroupBox, option, SC_GroupBoxLabel, widget); | ||||
176 | textRect = option->fontMetrics.boundingRect(textRect, textFlags, groupBoxOption->text); | ||||
177 | | ||||
178 | // focus color | ||||
179 | QColor focusColor; | ||||
180 | if (isFocusAnimated) | ||||
181 | focusColor = _helper->alphaColor(_helper->focusColor(palette), opacity); | ||||
182 | else if (hasFocus) | ||||
183 | focusColor = _helper->focusColor(palette); | ||||
184 | | ||||
185 | // render focus | ||||
186 | _helper->renderFocusLine(painter, textRect, focusColor); | ||||
187 | | ||||
188 | return true; | ||||
189 | } | ||||
190 | | ||||
191 | } |