]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistheaderwidget.h
a522fa3a2806bda5640cf72d6eb91df1cd2e54cc
[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 setSidePadding(qreal width);
54 qreal sidePadding() 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, qreal currentWidth, qreal previousWidth);
66
67 void sidePaddingChanged(qreal width);
68
69 /**
70 * Is emitted if the user has released the mouse button after adjusting the
71 * width of a visible role.
72 */
73 void columnWidthChangeFinished(const QByteArray &role, qreal currentWidth);
74
75 /**
76 * Is emitted if the position of the column has been changed.
77 */
78 void columnMoved(const QByteArray &role, int currentIndex, int previousIndex);
79
80 /**
81 * Is emitted if the user has changed the sort order by clicking on a
82 * header item. The sort order of the model has already been adjusted to
83 * the current sort order. Note that no signal will be emitted if the
84 * sort order of the model has been changed without user interaction.
85 */
86 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
87
88 /**
89 * Is emitted if the user has changed the sort role by clicking on a
90 * header item. The sort role of the model has already been adjusted to
91 * the current sort role. Note that no signal will be emitted if the
92 * sort role of the model has been changed without user interaction.
93 */
94 void sortRoleChanged(const QByteArray &current, const QByteArray &previous);
95
96 void columnUnHovered(int columnIndex);
97 void columnHovered(int columnIndex);
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 enum PaddingGrip {
114 Leading,
115 Trailing,
116 };
117
118 void paintRole(QPainter *painter, const QByteArray &role, const QRectF &rect, int orderIndex, QWidget *widget = nullptr) const;
119
120 void updatePressedRoleIndex(const QPointF &pos);
121 void updateHoveredIndex(const QPointF &pos);
122 int roleIndexAt(const QPointF &pos) const;
123 bool isAboveRoleGrip(const QPointF &pos, int roleIndex) const;
124 bool isAbovePaddingGrip(const QPointF &pos, PaddingGrip paddingGrip) const;
125
126 /**
127 * Creates a pixmap of the role with the index \a roleIndex that is shown
128 * during moving a role.
129 */
130 QPixmap createRolePixmap(int roleIndex) const;
131
132 /**
133 * @return Target index of the currently moving visible role based on the current
134 * state of m_movingRole.
135 */
136 int targetOfMovingRole() const;
137
138 /**
139 * @return x-position of the left border of the role \a role.
140 */
141 qreal roleXPosition(const QByteArray &role) const;
142
143 private:
144 enum RoleOperation { NoRoleOperation, ResizeRoleOperation, ResizePaddingColumnOperation, MoveRoleOperation };
145
146 bool m_automaticColumnResizing;
147 KItemModelBase *m_model;
148 qreal m_offset;
149 qreal m_sidePadding;
150 QList<QByteArray> m_columns;
151 QHash<QByteArray, qreal> m_columnWidths;
152 QHash<QByteArray, qreal> m_preferredColumnWidths;
153
154 int m_hoveredIndex;
155 int m_pressedRoleIndex;
156 RoleOperation m_roleOperation;
157 QPointF m_pressedMousePos;
158
159 struct MovingRole {
160 QPixmap pixmap;
161 int x;
162 int xDec;
163 int index;
164 };
165 MovingRole m_movingRole;
166 };
167
168 #endif