]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistheader.cpp
KItemListHeader: Allow resizing and change of the sorting
[dolphin.git] / src / kitemviews / kitemlistheader.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistheader_p.h"
21
22 #include "kitemmodelbase.h"
23
24 #include <QApplication>
25 #include <QGraphicsSceneHoverEvent>
26 #include <QPainter>
27 #include <QStyleOptionHeader>
28
29 #include <KDebug>
30
31 KItemListHeader::KItemListHeader(QGraphicsWidget* parent) :
32 QGraphicsWidget(parent),
33 m_model(0),
34 m_visibleRoles(),
35 m_visibleRolesWidths(),
36 m_hoveredRoleIndex(-1),
37 m_pressedRoleIndex(-1),
38 m_roleOperation(NoRoleOperation),
39 m_pressedMousePos()
40 {
41 setAcceptHoverEvents(true);
42
43 QStyleOptionHeader option;
44 const QSize headerSize = style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize());
45 resize(0, headerSize.height());
46 }
47
48 KItemListHeader::~KItemListHeader()
49 {
50 }
51
52 void KItemListHeader::setModel(KItemModelBase* model)
53 {
54 if (m_model == model) {
55 return;
56 }
57
58 if (m_model) {
59 disconnect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
60 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
61 disconnect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
62 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
63 }
64
65 m_model = model;
66
67 if (m_model) {
68 connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
69 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
70 connect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
71 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
72 }
73 }
74
75 KItemModelBase* KItemListHeader::model() const
76 {
77 return m_model;
78 }
79
80 void KItemListHeader::setVisibleRoles(const QList<QByteArray>& roles)
81 {
82 m_visibleRoles = roles;
83 update();
84 }
85
86 QList<QByteArray> KItemListHeader::visibleRoles() const
87 {
88 return m_visibleRoles;
89 }
90
91 void KItemListHeader::setVisibleRolesWidths(const QHash<QByteArray, qreal> rolesWidths)
92 {
93 m_visibleRolesWidths = rolesWidths;
94
95 // Assure that no width is smaller than the minimum allowed width
96 const qreal minWidth = minimumRoleWidth();
97 QMutableHashIterator<QByteArray, qreal> it(m_visibleRolesWidths);
98 while (it.hasNext()) {
99 it.next();
100 if (it.value() < minWidth) {
101 m_visibleRolesWidths.insert(it.key(), minWidth);
102 }
103 }
104
105 update();
106 }
107
108 QHash<QByteArray, qreal> KItemListHeader::visibleRolesWidths() const
109 {
110 return m_visibleRolesWidths;
111 }
112
113 void KItemListHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
114 {
115 Q_UNUSED(option);
116 Q_UNUSED(widget);
117
118 if (!m_model) {
119 return;
120 }
121
122 // Draw roles
123 painter->setFont(font());
124 painter->setPen(palette().text().color());
125
126 qreal x = 0;
127 int orderIndex = 0;
128 foreach (const QByteArray& role, m_visibleRoles) {
129 const qreal roleWidth = m_visibleRolesWidths.value(role);
130 const QRectF rect(x, 0, roleWidth, size().height());
131 paintRole(painter, role, rect, orderIndex);
132 x += roleWidth;
133 ++orderIndex;
134 }
135
136 // Draw background without roles
137 QStyleOption opt;
138 opt.init(widget);
139 opt.rect = QRect(x, 0, size().width() - x, size().height());
140 opt.state |= QStyle::State_Horizontal;
141 style()->drawControl(QStyle::CE_HeaderEmptyArea, &opt, painter);
142 }
143
144 void KItemListHeader::mousePressEvent(QGraphicsSceneMouseEvent* event)
145 {
146 event->accept();
147 updatePressedRoleIndex(event->pos());
148 m_pressedMousePos = event->pos();
149 m_roleOperation = isAboveRoleGrip(m_pressedMousePos, m_pressedRoleIndex) ?
150 ResizeRoleOperation : NoRoleOperation;
151 }
152
153 void KItemListHeader::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
154 {
155 QGraphicsWidget::mouseReleaseEvent(event);
156
157 if (m_pressedRoleIndex == -1) {
158 return;
159 }
160
161 if (m_roleOperation == NoRoleOperation) {
162 // Only a click has been done and no moving or resizing has been started
163 const QByteArray sortRole = m_model->sortRole();
164 const int sortRoleIndex = m_visibleRoles.indexOf(sortRole);
165 if (m_pressedRoleIndex == sortRoleIndex) {
166 // Toggle the sort order
167 const Qt::SortOrder toggled = (m_model->sortOrder() == Qt::AscendingOrder) ?
168 Qt::DescendingOrder : Qt::AscendingOrder;
169 m_model->setSortOrder(toggled);
170 } else {
171 // Change the sort role
172 const QByteArray sortRole = m_visibleRoles.at(m_pressedRoleIndex);
173 m_model->setSortRole(sortRole);
174 }
175 }
176
177 m_pressedRoleIndex = -1;
178 m_roleOperation = NoRoleOperation;
179 update();
180 }
181
182 void KItemListHeader::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
183 {
184 QGraphicsWidget::mouseMoveEvent(event);
185
186 if (m_roleOperation == ResizeRoleOperation) {
187 const QByteArray pressedRole = m_visibleRoles.at(m_pressedRoleIndex);
188
189 qreal roleWidth = m_visibleRolesWidths.value(pressedRole);
190 roleWidth += event->pos().x() - event->lastPos().x();
191 roleWidth = qMax(minimumRoleWidth(), roleWidth);
192
193 m_visibleRolesWidths.insert(pressedRole, roleWidth);
194 update();
195 } else if ((event->pos() - m_pressedMousePos).manhattanLength() >= QApplication::startDragDistance()) {
196 kDebug() << "Moving of role not supported yet";
197 m_roleOperation = MoveRoleOperation;
198 }
199 }
200
201 void KItemListHeader::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
202 {
203 QGraphicsWidget::hoverEnterEvent(event);
204 updateHoveredRoleIndex(event->pos());
205 }
206
207 void KItemListHeader::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
208 {
209 QGraphicsWidget::hoverLeaveEvent(event);
210 if (m_hoveredRoleIndex != -1) {
211 m_hoveredRoleIndex = -1;
212 update();
213 }
214 }
215
216 void KItemListHeader::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
217 {
218 QGraphicsWidget::hoverMoveEvent(event);
219
220 const QPointF& pos = event->pos();
221 updateHoveredRoleIndex(pos);
222 if (m_hoveredRoleIndex >= 0 && isAboveRoleGrip(pos, m_hoveredRoleIndex)) {
223 setCursor(Qt::SplitHCursor);
224 } else {
225 unsetCursor();
226 }
227 }
228
229 void KItemListHeader::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
230 {
231 Q_UNUSED(current);
232 Q_UNUSED(previous);
233 update();
234 }
235
236 void KItemListHeader::slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
237 {
238 Q_UNUSED(current);
239 Q_UNUSED(previous);
240 update();
241 }
242
243 void KItemListHeader::paintRole(QPainter* painter,
244 const QByteArray& role,
245 const QRectF& rect,
246 int orderIndex)
247 {
248 // The following code is based on the code from QHeaderView::paintSection().
249 // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
250 QStyleOptionHeader option;
251 option.section = orderIndex;
252 option.state = QStyle::State_None | QStyle::State_Raised | QStyle::State_Horizontal;
253 if (isEnabled()) {
254 option.state |= QStyle::State_Enabled;
255 }
256 if (window() && window()->isActiveWindow()) {
257 option.state |= QStyle::State_Active;
258 }
259 if (m_hoveredRoleIndex == orderIndex) {
260 option.state |= QStyle::State_MouseOver;
261 }
262 if (m_pressedRoleIndex == orderIndex) {
263 option.state |= QStyle::State_Sunken;
264 }
265 if (m_model->sortRole() == role) {
266 option.sortIndicator = (m_model->sortOrder() == Qt::AscendingOrder) ?
267 QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp;
268 }
269 option.rect = rect.toRect();
270
271 if (m_visibleRoles.count() == 1) {
272 option.position = QStyleOptionHeader::OnlyOneSection;
273 } else if (orderIndex == 0) {
274 option.position = QStyleOptionHeader::Beginning;
275 } else if (orderIndex == m_visibleRoles.count() - 1) {
276 option.position = QStyleOptionHeader::End;
277 } else {
278 option.position = QStyleOptionHeader::Middle;
279 }
280
281 option.orientation = Qt::Horizontal;
282 option.selectedPosition = QStyleOptionHeader::NotAdjacent;
283 option.text = m_model->roleDescription(role);
284
285 style()->drawControl(QStyle::CE_Header, &option, painter);
286 }
287
288 void KItemListHeader::updatePressedRoleIndex(const QPointF& pos)
289 {
290 const int pressedIndex = roleIndexAt(pos);
291 if (m_pressedRoleIndex != pressedIndex) {
292 m_pressedRoleIndex = pressedIndex;
293 update();
294 }
295 }
296
297 void KItemListHeader::updateHoveredRoleIndex(const QPointF& pos)
298 {
299 const int hoverIndex = roleIndexAt(pos);
300 if (m_hoveredRoleIndex != hoverIndex) {
301 m_hoveredRoleIndex = hoverIndex;
302 update();
303 }
304 }
305
306 int KItemListHeader::roleIndexAt(const QPointF& pos) const
307 {
308 int index = -1;
309
310 qreal x = 0;
311 foreach (const QByteArray& role, m_visibleRoles) {
312 ++index;
313 x += m_visibleRolesWidths.value(role);
314 if (pos.x() <= x) {
315 break;
316 }
317 }
318
319 return index;
320 }
321
322 bool KItemListHeader::isAboveRoleGrip(const QPointF& pos, int roleIndex) const
323 {
324 qreal x = 0;
325 for (int i = 0; i <= roleIndex; ++i) {
326 const QByteArray role = m_visibleRoles.at(i);
327 x += m_visibleRolesWidths.value(role);
328 }
329
330 const int grip = style()->pixelMetric(QStyle::PM_HeaderGripMargin);
331 return pos.x() >= (x - grip) && pos.x() <= x;
332 }
333
334 qreal KItemListHeader::minimumRoleWidth() const
335 {
336 QFontMetricsF fontMetrics(font());
337 return fontMetrics.averageCharWidth() * 5;
338 }
339
340 #include "kitemlistheader_p.moc"