]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistheader_p.h
Details view: Expand the name-column like in Dolphin 1.x
[dolphin.git] / src / kitemviews / kitemlistheader_p.h
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 #ifndef KITEMLISTHEADER_H
21 #define KITEMLISTHEADER_H
22
23 #include <libdolphin_export.h>
24 #include <QGraphicsWidget>
25 #include <QHash>
26 #include <QList>
27
28 class KItemModelBase;
29
30 /**
31 * @brief Header for KItemListView that shows the currently used roles.
32 */
33 class LIBDOLPHINPRIVATE_EXPORT KItemListHeader : public QGraphicsWidget
34 {
35 Q_OBJECT
36
37 public:
38 KItemListHeader(QGraphicsWidget* parent = 0);
39 virtual ~KItemListHeader();
40
41 void setModel(KItemModelBase* model);
42 KItemModelBase* model() const;
43
44 void setVisibleRoles(const QList<QByteArray>& roles);
45 QList<QByteArray> visibleRoles() const;
46
47 void setVisibleRolesWidths(const QHash<QByteArray, qreal>& rolesWidths);
48 QHash<QByteArray, qreal> visibleRolesWidths() const;
49
50 qreal minimumRoleWidth() const;
51
52 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
53
54 signals:
55 /**
56 * Is emitted if the width of a visible role has been adjusted by the user with the mouse
57 * (no signal is emitted if KItemListHeader::setVisibleRoles() is invoked).
58 */
59 void visibleRoleWidthChanged(const QByteArray& role,
60 qreal currentWidth,
61 qreal previousWidth);
62
63 /**
64 * Is emitted if the position of the visible role has been changed.
65 */
66 void visibleRoleMoved(const QByteArray& role, int currentIndex, int previousIndex);
67
68 /**
69 * Is emitted if the user has changed the sort order by clicking on a
70 * header item. The sort order of the model has already been adjusted to
71 * the current sort order. Note that no signal will be emitted if the
72 * sort order of the model has been changed without user interaction.
73 */
74 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
75
76 /**
77 * Is emitted if the user has changed the sort role by clicking on a
78 * header item. The sort role of the model has already been adjusted to
79 * the current sort role. Note that no signal will be emitted if the
80 * sort role of the model has been changed without user interaction.
81 */
82 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
83
84 protected:
85 virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
86 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
87 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
88 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
89 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
90 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent* event);
91
92 private slots:
93 void slotSortRoleChanged(const QByteArray& current, const QByteArray& previous);
94 void slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
95
96 private:
97 void paintRole(QPainter* painter,
98 const QByteArray& role,
99 const QRectF& rect,
100 int orderIndex,
101 QWidget* widget = 0) const;
102
103 void updatePressedRoleIndex(const QPointF& pos);
104 void updateHoveredRoleIndex(const QPointF& pos);
105 int roleIndexAt(const QPointF& pos) const;
106 bool isAboveRoleGrip(const QPointF& pos, int roleIndex) const;
107
108 /**
109 * Creates a pixmap of the role with the index \a roleIndex that is shown
110 * during moving a role.
111 */
112 QPixmap createRolePixmap(int roleIndex) const;
113
114 /**
115 * @return Target index of the currently moving visible role based on the current
116 * state of m_movingRole.
117 */
118 int targetOfMovingRole() const;
119
120 /**
121 * @return x-position of the left border of the role \a role.
122 */
123 qreal roleXPosition(const QByteArray& role) const;
124
125 private:
126 enum RoleOperation
127 {
128 NoRoleOperation,
129 ResizeRoleOperation,
130 MoveRoleOperation
131 };
132
133 KItemModelBase* m_model;
134 QList<QByteArray> m_visibleRoles;
135 QHash<QByteArray, qreal> m_visibleRolesWidths;
136
137 int m_hoveredRoleIndex;
138 int m_pressedRoleIndex;
139 RoleOperation m_roleOperation;
140 QPointF m_pressedMousePos;
141
142 struct MovingRole
143 {
144 QPixmap pixmap;
145 int x;
146 int xDec;
147 int index;
148 } m_movingRole;
149 };
150
151 #endif
152
153