Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/header.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //___________________________________________________________________________________________________________________ | ||||
6 | QRect Style::headerArrowRect(const QStyleOption *option, const QWidget *) const | ||||
7 | { | ||||
8 | // cast option and check | ||||
9 | const auto headerOption(qstyleoption_cast<const QStyleOptionHeader *>(option)); | ||||
10 | if (!headerOption) | ||||
11 | return option->rect; | ||||
12 | | ||||
13 | // check if arrow is necessary | ||||
14 | if (headerOption->sortIndicator == QStyleOptionHeader::None) | ||||
15 | return QRect(); | ||||
16 | | ||||
17 | auto arrowRect(insideMargin(option->rect, Metrics::Header_MarginWidth)); | ||||
18 | arrowRect.setLeft(arrowRect.right() - Metrics::Header_ArrowSize + 1); | ||||
19 | | ||||
20 | return visualRect(option, arrowRect); | ||||
21 | } | ||||
22 | | ||||
23 | //___________________________________________________________________________________________________________________ | ||||
24 | QRect Style::headerLabelRect(const QStyleOption *option, const QWidget *) const | ||||
25 | { | ||||
26 | // cast option and check | ||||
27 | const auto headerOption(qstyleoption_cast<const QStyleOptionHeader *>(option)); | ||||
28 | if (!headerOption) | ||||
29 | return option->rect; | ||||
30 | | ||||
31 | // check if arrow is necessary | ||||
32 | auto labelRect(insideMargin(option->rect, Metrics::Header_MarginWidth, 0)); | ||||
33 | if (headerOption->sortIndicator == QStyleOptionHeader::None) | ||||
34 | return labelRect; | ||||
35 | | ||||
36 | labelRect.adjust(0, 0, -Metrics::Header_ArrowSize - Metrics::Header_ItemSpacing, 0); | ||||
37 | return visualRect(option, labelRect); | ||||
38 | } | ||||
39 | | ||||
40 | //______________________________________________________________ | ||||
41 | QSize Style::headerSectionSizeFromContents(const QStyleOption *option, const QSize &contentsSize, const QWidget *) const | ||||
42 | { | ||||
43 | // cast option and check | ||||
44 | const auto headerOption(qstyleoption_cast<const QStyleOptionHeader *>(option)); | ||||
45 | if (!headerOption) | ||||
46 | return contentsSize; | ||||
47 | | ||||
48 | // get text size | ||||
49 | const bool horizontal(headerOption->orientation == Qt::Horizontal); | ||||
50 | const bool hasText(!headerOption->text.isEmpty()); | ||||
51 | const bool hasIcon(!headerOption->icon.isNull()); | ||||
52 | | ||||
53 | const QSize textSize(hasText ? headerOption->fontMetrics.size(0, headerOption->text) : QSize()); | ||||
54 | const QSize iconSize(hasIcon ? QSize(22, 22) : QSize()); | ||||
55 | | ||||
56 | // contents width | ||||
57 | int contentsWidth(0); | ||||
58 | if (hasText) | ||||
59 | contentsWidth += textSize.width(); | ||||
60 | if (hasIcon) { | ||||
61 | contentsWidth += iconSize.width(); | ||||
62 | if (hasText) | ||||
63 | contentsWidth += Metrics::Header_ItemSpacing; | ||||
64 | } | ||||
65 | | ||||
66 | // contents height | ||||
67 | int contentsHeight(headerOption->fontMetrics.height()); | ||||
68 | if (hasIcon) | ||||
69 | contentsHeight = qMax(contentsHeight, iconSize.height()); | ||||
70 | | ||||
71 | if (horizontal && headerOption->sortIndicator != QStyleOptionHeader::None) { | ||||
72 | // also add space for sort indicator | ||||
73 | contentsWidth += Metrics::Header_ArrowSize + Metrics::Header_ItemSpacing; | ||||
74 | contentsHeight = qMax(contentsHeight, int(Metrics::Header_ArrowSize)); | ||||
75 | } | ||||
76 | | ||||
77 | // update contents size, add margins and return | ||||
78 | const QSize size(contentsSize.expandedTo(QSize(contentsWidth, contentsHeight))); | ||||
79 | return expandSize(size, Metrics::Header_MarginWidth); | ||||
80 | } | ||||
81 | | ||||
82 | //___________________________________________________________________________________ | ||||
83 | bool Style::drawIndicatorHeaderArrowPrimitive(const QStyleOption *option, QPainter *painter, const QWidget *) const | ||||
84 | { | ||||
85 | const auto headerOption(qstyleoption_cast<const QStyleOptionHeader *>(option)); | ||||
86 | const State &state(option->state); | ||||
87 | | ||||
88 | // arrow orientation | ||||
89 | ArrowOrientation orientation(ArrowNone); | ||||
90 | if (state & State_UpArrow || (headerOption && headerOption->sortIndicator == QStyleOptionHeader::SortUp)) | ||||
91 | orientation = ArrowUp; | ||||
92 | else if (state & State_DownArrow || (headerOption && headerOption->sortIndicator == QStyleOptionHeader::SortDown)) | ||||
93 | orientation = ArrowDown; | ||||
94 | if (orientation == ArrowNone) | ||||
95 | return true; | ||||
96 | | ||||
97 | // invert arrows if requested by (hidden) options | ||||
98 | if (StyleConfigData::viewInvertSortIndicator()) | ||||
99 | orientation = (orientation == ArrowUp) ? ArrowDown : ArrowUp; | ||||
100 | | ||||
101 | // state, rect and palette | ||||
102 | const auto &rect(option->rect); | ||||
103 | const auto &palette(option->palette); | ||||
104 | | ||||
105 | // define color and polygon for drawing arrow | ||||
106 | const auto color = _helper->arrowColor(palette, QPalette::ButtonText); | ||||
107 | | ||||
108 | // render | ||||
109 | _helper->renderArrow(painter, rect, color, orientation); | ||||
110 | | ||||
111 | return true; | ||||
112 | } | ||||
113 | | ||||
114 | //___________________________________________________________________________________ | ||||
115 | bool Style::drawHeaderSectionControl(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
116 | { | ||||
117 | const auto &rect(option->rect); | ||||
118 | const auto &palette(option->palette); | ||||
119 | const auto &state(option->state); | ||||
120 | const bool enabled(state & State_Enabled); | ||||
121 | const bool mouseOver(enabled && (state & State_MouseOver)); | ||||
122 | const bool sunken(enabled && (state & (State_On | State_Sunken))); | ||||
123 | | ||||
124 | const auto headerOption(qstyleoption_cast<const QStyleOptionHeader *>(option)); | ||||
125 | if (!headerOption) | ||||
126 | return true; | ||||
127 | | ||||
128 | const bool horizontal(headerOption->orientation == Qt::Horizontal); | ||||
129 | const bool isFirst(horizontal && (headerOption->position == QStyleOptionHeader::Beginning)); | ||||
130 | const bool isCorner(widget && widget->inherits("QTableCornerButton")); | ||||
131 | const bool reverseLayout(option->direction == Qt::RightToLeft); | ||||
132 | | ||||
133 | // update animation state | ||||
134 | _animations->headerViewEngine().updateState(widget, rect.topLeft(), mouseOver); | ||||
135 | const bool animated(enabled && _animations->headerViewEngine().isAnimated(widget, rect.topLeft())); | ||||
136 | const qreal opacity(_animations->headerViewEngine().opacity(widget, rect.topLeft())); | ||||
137 | | ||||
138 | // fill | ||||
139 | const auto &normal = palette.color(QPalette::Button); | ||||
140 | const auto focus(KColorUtils::mix(normal, _helper->focusColor(palette), 0.2)); | ||||
141 | const auto hover(KColorUtils::mix(normal, _helper->hoverColor(palette), 0.2)); | ||||
142 | | ||||
143 | QColor color; | ||||
144 | if (sunken) | ||||
145 | color = focus; | ||||
146 | else if (animated) | ||||
147 | color = KColorUtils::mix(normal, hover, opacity); | ||||
148 | else if (mouseOver) | ||||
149 | color = hover; | ||||
150 | else | ||||
151 | color = normal; | ||||
152 | | ||||
153 | painter->setRenderHint(QPainter::Antialiasing, false); | ||||
154 | painter->setBrush(color); | ||||
155 | painter->setPen(Qt::NoPen); | ||||
156 | painter->drawRect(rect); | ||||
157 | | ||||
158 | // outline | ||||
159 | painter->setBrush(Qt::NoBrush); | ||||
160 | painter->setPen(_helper->alphaColor(palette.color(QPalette::WindowText), 0.1)); | ||||
161 | | ||||
162 | if (isCorner) { | ||||
163 | if (reverseLayout) | ||||
164 | painter->drawPoint(rect.bottomLeft()); | ||||
165 | else | ||||
166 | painter->drawPoint(rect.bottomRight()); | ||||
167 | | ||||
168 | } else if (horizontal) { | ||||
169 | painter->drawLine(rect.bottomLeft(), rect.bottomRight()); | ||||
170 | | ||||
171 | } else { | ||||
172 | if (reverseLayout) | ||||
173 | painter->drawLine(rect.topLeft(), rect.bottomLeft()); | ||||
174 | else | ||||
175 | painter->drawLine(rect.topRight(), rect.bottomRight()); | ||||
176 | } | ||||
177 | | ||||
178 | // separators | ||||
179 | painter->setPen(_helper->alphaColor(palette.color(QPalette::WindowText), 0.2)); | ||||
180 | | ||||
181 | if (horizontal) { | ||||
182 | if (headerOption->section != 0 || isFirst) { | ||||
183 | if (reverseLayout) | ||||
184 | painter->drawLine(rect.topLeft(), rect.bottomLeft() - QPoint(0, 1)); | ||||
185 | else | ||||
186 | painter->drawLine(rect.topRight(), rect.bottomRight() - QPoint(0, 1)); | ||||
187 | } | ||||
188 | | ||||
189 | } else { | ||||
190 | if (reverseLayout) | ||||
191 | painter->drawLine(rect.bottomLeft() + QPoint(1, 0), rect.bottomRight()); | ||||
192 | else | ||||
193 | painter->drawLine(rect.bottomLeft(), rect.bottomRight() - QPoint(1, 0)); | ||||
194 | } | ||||
195 | | ||||
196 | return true; | ||||
197 | } | ||||
198 | | ||||
199 | //___________________________________________________________________________________ | ||||
200 | bool Style::drawHeaderEmptyAreaControl(const QStyleOption *option, QPainter *painter, const QWidget *) const | ||||
201 | { | ||||
202 | // use the same background as in drawHeaderPrimitive | ||||
203 | const auto &rect(option->rect); | ||||
204 | auto palette(option->palette); | ||||
205 | | ||||
206 | const bool horizontal(option->state & QStyle::State_Horizontal); | ||||
207 | const bool reverseLayout(option->direction == Qt::RightToLeft); | ||||
208 | | ||||
209 | // fill | ||||
210 | painter->setRenderHint(QPainter::Antialiasing, false); | ||||
211 | painter->setBrush(palette.color(QPalette::Button)); | ||||
212 | painter->setPen(Qt::NoPen); | ||||
213 | painter->drawRect(rect); | ||||
214 | | ||||
215 | // outline | ||||
216 | painter->setBrush(Qt::NoBrush); | ||||
217 | painter->setPen(_helper->alphaColor(palette.color(QPalette::ButtonText), 0.1)); | ||||
218 | | ||||
219 | if (horizontal) { | ||||
220 | painter->drawLine(rect.bottomLeft(), rect.bottomRight()); | ||||
221 | | ||||
222 | } else { | ||||
223 | if (reverseLayout) | ||||
224 | painter->drawLine(rect.topLeft(), rect.bottomLeft()); | ||||
225 | else | ||||
226 | painter->drawLine(rect.topRight(), rect.bottomRight()); | ||||
227 | } | ||||
228 | | ||||
229 | return true; | ||||
230 | } | ||||
231 | | ||||
232 | } |