]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistheaderwidget.h
8248fb7bb021bfba05084d26686be7990188b20f
[dolphin.git] / src / kitemviews / private / kitemlistheaderwidget.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 KITEMLISTHEADERWIDGET_H
21 #define KITEMLISTHEADERWIDGET_H
22
23 #include "dolphin_export.h"
24 #include <QGraphicsWidget>
25 #include <QHash>
26 #include <QList>
27
28 class KItemModelBase;
29
30 /**
31 * @brief Widget the implements the header for KItemListView showing the currently used roles.
32 *
33 * The widget is an internal API, the user of KItemListView may only access the
34 * class KItemListHeader.
35 */
36 class DOLPHIN_EXPORT KItemListHeaderWidget : public QGraphicsWidget
37 {
38 Q_OBJECT
39
40 public:
41 KItemListHeaderWidget(QGraphicsWidget* parent = 0);
42 ~KItemListHeaderWidget() override;
43
44 void setModel(KItemModelBase* model);
45 KItemModelBase* model() const;
46
47 void setAutomaticColumnResizing(bool automatic);
48 bool automaticColumnResizing() const;
49
50 void setColumns(const QList<QByteArray>& roles);
51 QList<QByteArray> columns() const;
52
53 void setColumnWidth(const QByteArray& role, qreal width);
54 qreal columnWidth(const QByteArray& role) const;
55
56 /**
57 * Sets the column-width that is required to show the role unclipped.
58 */
59 void setPreferredColumnWidth(const QByteArray& role, qreal width);
60 qreal preferredColumnWidth(const QByteArray& role) const;
61
62 void setOffset(qreal offset);
63 qreal offset() const;
64
65 qreal minimumColumnWidth() const;
66
67 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0) override;
68
69 signals:
70 /**
71 * Is emitted if the width of a visible role has been adjusted by the user with the mouse
72 * (no signal is emitted if KItemListHeader::setVisibleRoleWidth() is invoked).
73 */
74 void columnWidthChanged(const QByteArray& role,
75 qreal currentWidth,
76 qreal previousWidth);
77
78 /**
79 * Is emitted if the user has released the mouse button after adjusting the
80 * width of a visible role.
81 */
82 void columnWidthChangeFinished(const QByteArray& role,
83 qreal currentWidth);
84
85 /**
86 * Is emitted if the position of the column has been changed.
87 */
88 void columnMoved(const QByteArray& role, int currentIndex, int previousIndex);
89
90 /**
91 * Is emitted if the user has changed the sort order by clicking on a
92 * header item. The sort order of the model has already been adjusted to
93 * the current sort order. Note that no signal will be emitted if the
94 * sort order of the model has been changed without user interaction.
95 */
96 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
97
98 /**
99 * Is emitted if the user has changed the sort role by clicking on a
100 * header item. The sort role of the model has already been adjusted to
101 * the current sort role. Note that no signal will be emitted if the
102 * sort role of the model has been changed without user interaction.
103 */
104 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
105
106 protected:
107 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
108 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
109 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
110 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
111 void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
112 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
113 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
114
115 private slots:
116 void slotSortRoleChanged(const QByteArray& current, const QByteArray& previous);
117 void slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
118
119 private:
120 void paintRole(QPainter* painter,
121 const QByteArray& role,
122 const QRectF& rect,
123 int orderIndex,
124 QWidget* widget = 0) const;
125
126 void updatePressedRoleIndex(const QPointF& pos);
127 void updateHoveredRoleIndex(const QPointF& pos);
128 int roleIndexAt(const QPointF& pos) const;
129 bool isAboveRoleGrip(const QPointF& pos, int roleIndex) const;
130
131 /**
132 * Creates a pixmap of the role with the index \a roleIndex that is shown
133 * during moving a role.
134 */
135 QPixmap createRolePixmap(int roleIndex) const;
136
137 /**
138 * @return Target index of the currently moving visible role based on the current
139 * state of m_movingRole.
140 */
141 int targetOfMovingRole() const;
142
143 /**
144 * @return x-position of the left border of the role \a role.
145 */
146 qreal roleXPosition(const QByteArray& role) const;
147
148 private:
149 enum RoleOperation
150 {
151 NoRoleOperation,
152 ResizeRoleOperation,
153 MoveRoleOperation
154 };
155
156 bool m_automaticColumnResizing;
157 KItemModelBase* m_model;
158 qreal m_offset;
159 QList<QByteArray> m_columns;
160 QHash<QByteArray, qreal> m_columnWidths;
161 QHash<QByteArray, qreal> m_preferredColumnWidths;
162
163 int m_hoveredRoleIndex;
164 int m_pressedRoleIndex;
165 RoleOperation m_roleOperation;
166 QPointF m_pressedMousePos;
167
168 struct MovingRole
169 {
170 QPixmap pixmap;
171 int x;
172 int xDec;
173 int index;
174 } m_movingRole;
175 };
176
177 #endif
178
179