Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/itemview.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //______________________________________________________________ | ||||
6 | QSize Style::itemViewItemSizeFromContents(const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const | ||||
7 | { | ||||
8 | // call base class | ||||
9 | const QSize size(ParentStyleClass::sizeFromContents(CT_ItemViewItem, option, contentsSize, widget)); | ||||
10 | return expandSize(size, Metrics::ItemView_ItemMarginWidth); | ||||
11 | } | ||||
12 | | ||||
13 | //___________________________________________________________________________________ | ||||
14 | bool Style::drawPanelItemViewItemPrimitive(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
15 | { | ||||
16 | // cast option and check | ||||
17 | #if BREEZE_USE_KDE4 | ||||
18 | const auto viewItemOption = qstyleoption_cast<const QStyleOptionViewItemV4 *>(option); | ||||
19 | #else | ||||
20 | const auto viewItemOption = qstyleoption_cast<const QStyleOptionViewItem *>(option); | ||||
21 | #endif | ||||
22 | if (!viewItemOption) | ||||
23 | return false; | ||||
24 | | ||||
25 | // try cast widget | ||||
26 | const auto abstractItemView = qobject_cast<const QAbstractItemView *>(widget); | ||||
27 | | ||||
28 | // store palette and rect | ||||
29 | const auto &palette(option->palette); | ||||
30 | auto rect(option->rect); | ||||
31 | | ||||
32 | // store flags | ||||
33 | const State &state(option->state); | ||||
34 | const bool mouseOver((state & State_MouseOver) && (!abstractItemView || abstractItemView->selectionMode() != QAbstractItemView::NoSelection)); | ||||
35 | const bool selected(state & State_Selected); | ||||
36 | const bool enabled(state & State_Enabled); | ||||
37 | const bool active(state & State_Active); | ||||
38 | | ||||
39 | const bool hasCustomBackground = viewItemOption->backgroundBrush.style() != Qt::NoBrush && !(state & State_Selected); | ||||
40 | const bool hasSolidBackground = !hasCustomBackground || viewItemOption->backgroundBrush.style() == Qt::SolidPattern; | ||||
41 | #if BREEZE_USE_KDE4 | ||||
42 | const bool hasAlternateBackground(viewItemOption->features & QStyleOptionViewItemV2::Alternate); | ||||
43 | #else | ||||
44 | const bool hasAlternateBackground(viewItemOption->features & QStyleOptionViewItem::Alternate); | ||||
45 | #endif | ||||
46 | | ||||
47 | // do nothing if no background is to be rendered | ||||
48 | if (!(mouseOver || selected || hasCustomBackground || hasAlternateBackground)) { | ||||
49 | return true; | ||||
50 | } | ||||
51 | | ||||
52 | // define color group | ||||
53 | QPalette::ColorGroup colorGroup; | ||||
54 | if (enabled) | ||||
55 | colorGroup = active ? QPalette::Active : QPalette::Inactive; | ||||
56 | else | ||||
57 | colorGroup = QPalette::Disabled; | ||||
58 | | ||||
59 | // render alternate background | ||||
60 | if (hasAlternateBackground) { | ||||
61 | painter->setPen(Qt::NoPen); | ||||
62 | painter->setBrush(palette.brush(colorGroup, QPalette::AlternateBase)); | ||||
63 | painter->drawRect(rect); | ||||
64 | } | ||||
65 | | ||||
66 | // stop here if no highlight is needed | ||||
67 | if (!(mouseOver || selected || hasCustomBackground)) { | ||||
68 | return true; | ||||
69 | } | ||||
70 | | ||||
71 | // render custom background | ||||
72 | if (hasCustomBackground && !hasSolidBackground) { | ||||
73 | painter->setBrushOrigin(viewItemOption->rect.topLeft()); | ||||
74 | painter->setBrush(viewItemOption->backgroundBrush); | ||||
75 | painter->setPen(Qt::NoPen); | ||||
76 | painter->drawRect(viewItemOption->rect); | ||||
77 | return true; | ||||
78 | } | ||||
79 | | ||||
80 | // render selection | ||||
81 | // define color | ||||
82 | QColor color; | ||||
83 | if (hasCustomBackground && hasSolidBackground) | ||||
84 | color = viewItemOption->backgroundBrush.color(); | ||||
85 | else | ||||
86 | color = palette.color(colorGroup, QPalette::Highlight); | ||||
87 | | ||||
88 | // change color to implement mouse over | ||||
89 | if (mouseOver && !hasCustomBackground) { | ||||
90 | if (!selected) | ||||
91 | color.setAlphaF(0.2); | ||||
92 | else | ||||
93 | color = color.lighter(110); | ||||
94 | } | ||||
95 | | ||||
96 | // render | ||||
97 | _helper->renderSelection(painter, rect, color); | ||||
98 | | ||||
99 | return true; | ||||
100 | } | ||||
101 | | ||||
102 | //______________________________________________________________________________ | ||||
103 | const QAbstractItemView *Style::itemViewParent(const QWidget *widget) const | ||||
104 | { | ||||
105 | const QAbstractItemView *itemView(nullptr); | ||||
106 | | ||||
107 | // check widget directly | ||||
108 | if ((itemView = qobject_cast<const QAbstractItemView *>(widget))) | ||||
109 | return itemView; | ||||
110 | | ||||
111 | // check widget grand-parent | ||||
112 | else if (widget && widget->parentWidget() && (itemView = qobject_cast<const QAbstractItemView *>(widget->parentWidget()->parentWidget())) && itemView->viewport() == widget->parentWidget()) { | ||||
113 | return itemView; | ||||
114 | } | ||||
115 | | ||||
116 | // return null otherwise | ||||
117 | else | ||||
118 | return nullptr; | ||||
119 | } | ||||
120 | | ||||
121 | //____________________________________________________________________ | ||||
122 | bool Style::isSelectedItem(const QWidget *widget, const QPoint &localPosition) const | ||||
123 | { | ||||
124 | // get relevant itemview parent and check | ||||
125 | const QAbstractItemView *itemView(itemViewParent(widget)); | ||||
126 | if (!(itemView && itemView->hasFocus() && itemView->selectionModel())) | ||||
127 | return false; | ||||
128 | | ||||
129 | #if QT_VERSION >= 0x050000 | ||||
130 | QPoint position = widget->mapTo(itemView, localPosition); | ||||
131 | #else | ||||
132 | // qt4 misses a const for mapTo argument, although nothing is actually changed to the passed widget | ||||
133 | QPoint position = widget->mapTo(const_cast<QAbstractItemView *>(itemView), localPosition); | ||||
134 | #endif | ||||
135 | | ||||
136 | // get matching QModelIndex and check | ||||
137 | const QModelIndex index(itemView->indexAt(position)); | ||||
138 | if (!index.isValid()) | ||||
139 | return false; | ||||
140 | | ||||
141 | // check whether index is selected | ||||
142 | return itemView->selectionModel()->isSelected(index); | ||||
143 | } | ||||
144 | | ||||
145 | //___________________________________________________________________________________ | ||||
146 | bool Style::drawIndicatorBranchPrimitive(const QStyleOption *option, QPainter *painter, const QWidget *) const | ||||
147 | { | ||||
148 | // copy rect and palette | ||||
149 | const auto &rect(option->rect); | ||||
150 | const auto &palette(option->palette); | ||||
151 | | ||||
152 | // state | ||||
153 | const State &state(option->state); | ||||
154 | const bool reverseLayout(option->direction == Qt::RightToLeft); | ||||
155 | | ||||
156 | // draw expander | ||||
157 | int expanderAdjust = 0; | ||||
158 | if (state & State_Children) { | ||||
159 | // state | ||||
160 | const bool expanderOpen(state & State_Open); | ||||
161 | const bool enabled(state & State_Enabled); | ||||
162 | const bool mouseOver(enabled && (state & State_MouseOver)); | ||||
163 | | ||||
164 | // expander rect | ||||
165 | int expanderSize = qMin(rect.width(), rect.height()); | ||||
166 | expanderSize = qMin(expanderSize, int(Metrics::ItemView_ArrowSize)); | ||||
167 | expanderAdjust = expanderSize / 2 + 1; | ||||
168 | const auto arrowRect = centerRect(rect, expanderSize, expanderSize); | ||||
169 | | ||||
170 | // get orientation from option | ||||
171 | ArrowOrientation orientation; | ||||
172 | if (expanderOpen) | ||||
173 | orientation = ArrowDown; | ||||
174 | else if (reverseLayout) | ||||
175 | orientation = ArrowLeft; | ||||
176 | else | ||||
177 | orientation = ArrowRight; | ||||
178 | | ||||
179 | // color | ||||
180 | const auto arrowColor(mouseOver ? _helper->hoverColor(palette) : _helper->arrowColor(palette, QPalette::Text)); | ||||
181 | | ||||
182 | // render | ||||
183 | _helper->renderArrow(painter, arrowRect, arrowColor, orientation); | ||||
184 | } | ||||
185 | | ||||
186 | // tree branches | ||||
187 | if (!StyleConfigData::viewDrawTreeBranchLines()) | ||||
188 | return true; | ||||
189 | | ||||
190 | const auto center(rect.center()); | ||||
191 | const auto lineColor(KColorUtils::mix(palette.color(QPalette::Base), palette.color(QPalette::Text), 0.25)); | ||||
192 | painter->setRenderHint(QPainter::Antialiasing, true); | ||||
193 | painter->translate(0.5, 0.5); | ||||
194 | painter->setPen(QPen(lineColor, 1)); | ||||
195 | if (state & (State_Item | State_Children | State_Sibling)) { | ||||
196 | const QLineF line(QPointF(center.x(), rect.top()), QPointF(center.x(), center.y() - expanderAdjust - 1)); | ||||
197 | painter->drawLine(line); | ||||
198 | } | ||||
199 | | ||||
200 | // The right/left (depending on direction) line gets drawn if we have an item | ||||
201 | if (state & State_Item) { | ||||
202 | const QLineF line = reverseLayout ? QLineF(QPointF(rect.left(), center.y()), QPointF(center.x() - expanderAdjust, center.y())) : QLineF(QPointF(center.x() + expanderAdjust, center.y()), QPointF(rect.right(), center.y())); | ||||
203 | painter->drawLine(line); | ||||
204 | } | ||||
205 | | ||||
206 | // The bottom if we have a sibling | ||||
207 | if (state & State_Sibling) { | ||||
208 | const QLineF line(QPointF(center.x(), center.y() + expanderAdjust), QPointF(center.x(), rect.bottom())); | ||||
209 | painter->drawLine(line); | ||||
210 | } | ||||
211 | | ||||
212 | return true; | ||||
213 | } | ||||
214 | | ||||
215 | } |