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