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