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