]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Merge branch 'Applications/19.08'
[dolphin.git] / src / kitemviews / kitemlistwidget.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #ifndef KITEMLISTWIDGET_H
24 #define KITEMLISTWIDGET_H
25
26 #include "dolphin_export.h"
27 #include "kitemviews/kitemliststyleoption.h"
28
29 #include <QBitArray>
30 #include <QGraphicsWidget>
31 #include <QStyle>
32
33 class KItemListSelectionToggle;
34 class KItemListView;
35 class QPropertyAnimation;
36
37 /**
38 * @brief Provides information for creating an instance of KItemListWidget.
39 *
40 * KItemListView only creates KItemListWidget instances for the visible
41 * area. For calculating the required size of all items the expected
42 * size for the invisible items must be accessible. KItemListWidgetInformant
43 * provides this information.
44 */
45 class DOLPHIN_EXPORT KItemListWidgetInformant
46 {
47 public:
48 KItemListWidgetInformant();
49 virtual ~KItemListWidgetInformant();
50
51 virtual void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const = 0;
52
53 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
54 int index,
55 const KItemListView* view) const = 0;
56 };
57
58 /**
59 * @brief Widget that shows a visible item from the model.
60 *
61 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
62 * All properties are set by KItemListView, for each property there is a corresponding
63 * virtual protected method that allows to react on property changes.
64 */
65 class DOLPHIN_EXPORT KItemListWidget : public QGraphicsWidget
66 {
67 Q_OBJECT
68
69 public:
70 KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent);
71 ~KItemListWidget() override;
72
73 void setIndex(int index);
74 int index() const;
75
76 void setData(const QHash<QByteArray, QVariant>& data, const QSet<QByteArray>& roles = QSet<QByteArray>());
77 QHash<QByteArray, QVariant> data() const;
78
79 /**
80 * Draws the hover-rectangle if the item is hovered. Overwrite this method
81 * to show the data of the custom model provided by KItemListWidget::data().
82 * @reimp
83 */
84 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
85
86 void setVisibleRoles(const QList<QByteArray>& roles);
87 QList<QByteArray> visibleRoles() const;
88
89 /**
90 * Sets the width of a role that should be used if the alignment of the content
91 * should be done in columns.
92 */
93 void setColumnWidth(const QByteArray& role, qreal width);
94 qreal columnWidth(const QByteArray& role) const;
95
96 void setStyleOption(const KItemListStyleOption& option);
97 const KItemListStyleOption& styleOption() const;
98
99 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
100 // this by using the default mechanism.
101 void setSelected(bool selected);
102 bool isSelected() const;
103
104 void setCurrent(bool current);
105 bool isCurrent() const;
106
107 void setHovered(bool hovered);
108 bool isHovered() const;
109
110 void setHoverPosition(const QPointF& pos);
111
112 void setAlternateBackground(bool enable);
113 bool alternateBackground() const;
114
115 void setEnabledSelectionToggle(bool enabled);
116 bool enabledSelectionToggle() const;
117
118 /**
119 * Sets the sibling information for the item and all of its parents.
120 * The sibling information of the upper most parent is represented by
121 * the first bit, the sibling information of the item by the last bit.
122 * The sibling information is useful for drawing the branches in
123 * tree views.
124 */
125 void setSiblingsInformation(const QBitArray& siblings);
126 QBitArray siblingsInformation() const;
127
128 /**
129 * Allows the user to edit the role \a role. The signals
130 * roleEditingCanceled() or roleEditingFinished() will be
131 * emitted after editing. An ongoing editing gets canceled if
132 * the role is empty. Derived classes must implement
133 * editedRoleChanged().
134 */
135 void setEditedRole(const QByteArray& role);
136 QByteArray editedRole() const;
137
138 /**
139 * @return True if \a point is inside KItemListWidget::hoverRect(),
140 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
141 * or KItemListWidget::expansionToggleRect().
142 * @reimp
143 */
144 bool contains(const QPointF& point) const override;
145
146 /**
147 * @return Rectangle for the area that shows the icon.
148 */
149 virtual QRectF iconRect() const = 0;
150
151 /**
152 * @return Rectangle for the area that contains the text-properties.
153 */
154 virtual QRectF textRect() const = 0;
155
156 /**
157 * @return Focus rectangle for indicating the current item. Per default
158 * textRect() will be returned. Overwrite this method if textRect()
159 * provides a larger rectangle than the actual text (e.g. to
160 * be aligned with the iconRect()). The textFocusRect() may not be
161 * outside the boundaries of textRect().
162 */
163 virtual QRectF textFocusRect() const;
164
165 /**
166 * @return Rectangle around which a selection box should be drawn if the item is selected.
167 */
168 virtual QRectF selectionRect() const = 0;
169
170 /**
171 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
172 * Per default an empty rectangle is returned which means that no selection-toggle
173 * is available.
174 */
175 virtual QRectF selectionToggleRect() const;
176
177 /**
178 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
179 * Per default an empty rectangle is returned which means that no opening of sub-trees
180 * is supported.
181 */
182 virtual QRectF expansionToggleRect() const;
183
184 /**
185 * @return Pixmap that is used when dragging an item. Per default the current state of the
186 * widget is returned as pixmap.
187 */
188 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr);
189
190 signals:
191 void roleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
192 void roleEditingFinished(int index, const QByteArray& role, const QVariant& value);
193
194 protected:
195 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
196 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
197 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
198 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
199 virtual void currentChanged(bool current);
200 virtual void selectedChanged(bool selected);
201 virtual void hoveredChanged(bool hovered);
202 virtual void alternateBackgroundChanged(bool enabled);
203 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
204 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
205 void resizeEvent(QGraphicsSceneResizeEvent* event) override;
206
207 /**
208 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
209 * this opacity value should be respected.
210 */
211 qreal hoverOpacity() const;
212
213 const KItemListWidgetInformant* informant() const;
214
215 private slots:
216 void slotHoverAnimationFinished();
217
218 private:
219 void initializeSelectionToggle();
220 void setHoverOpacity(qreal opacity);
221 void clearHoverCache();
222 void drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState);
223
224 private:
225 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
226
227 KItemListWidgetInformant* m_informant;
228 int m_index;
229 bool m_selected;
230 bool m_current;
231 bool m_hovered;
232 bool m_alternateBackground;
233 bool m_enabledSelectionToggle;
234 QHash<QByteArray, QVariant> m_data;
235 QList<QByteArray> m_visibleRoles;
236 QHash<QByteArray, qreal> m_columnWidths;
237 KItemListStyleOption m_styleOption;
238 QBitArray m_siblingsInfo;
239
240 qreal m_hoverOpacity;
241 mutable QPixmap* m_hoverCache;
242 QPropertyAnimation* m_hoverAnimation;
243
244 KItemListSelectionToggle* m_selectionToggle;
245
246 QByteArray m_editedRole;
247 };
248
249 inline const KItemListWidgetInformant* KItemListWidget::informant() const
250 {
251 return m_informant;
252 }
253
254 #endif
255
256