]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
Add drag-open animation
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.h
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef KSTANDARDITEMLISTWIDGET_H
8 #define KSTANDARDITEMLISTWIDGET_H
9
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemlistwidget.h"
12
13 #include <QPixmap>
14 #include <QPointF>
15 #include <QStaticText>
16
17 class KItemListRoleEditor;
18 class KItemListStyleOption;
19 class KItemListView;
20 class QVariantAnimation;
21
22 /**
23 * @brief standard implementation of the ItemList widget informant for use with KStandardItemListView and KStandardItemModel.
24 *
25 * @see KItemListWidgetInformant
26 */
27 class DOLPHIN_EXPORT KStandardItemListWidgetInformant : public KItemListWidgetInformant
28 {
29 public:
30 KStandardItemListWidgetInformant();
31 ~KStandardItemListWidgetInformant() override;
32
33 void calculateItemSizeHints(QVector<std::pair<qreal /* height */, bool /* isElided */>> &logicalHeightHints,
34 qreal &logicalWidthHint,
35 const KItemListView *view) const override;
36
37 qreal preferredRoleColumnWidth(const QByteArray &role, int index, const KItemListView *view) const override;
38
39 protected:
40 /**
41 * @return The value of the "text" role. The default implementation returns
42 * view->model()->data(index)["text"]. If a derived class can
43 * prevent the (possibly expensive) construction of the
44 * QHash<QByteArray, QVariant> returned by KItemModelBase::data(int),
45 * it can reimplement this function.
46 */
47 virtual QString itemText(int index, const KItemListView *view) const;
48
49 /**
50 * @return The value of the "isLink" role. The default implementation returns false.
51 * The derived class should reimplement this function, when information about
52 * links is available and in usage.
53 */
54 virtual bool itemIsLink(int index, const KItemListView *view) const;
55
56 /**
57 * @return String representation of the role \a role. The representation of
58 * a role might depend on other roles, so the values of all roles
59 * are passed as parameter.
60 */
61 virtual QString roleText(const QByteArray &role, const QHash<QByteArray, QVariant> &values) const;
62
63 /**
64 * @return A font based on baseFont which is customized for symlinks.
65 */
66 virtual QFont customizedFontForLinks(const QFont &baseFont) const;
67
68 void calculateIconsLayoutItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const;
69 void calculateCompactLayoutItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const;
70 void calculateDetailsLayoutItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const;
71
72 friend class KStandardItemListWidget; // Accesses roleText()
73 };
74
75 /**
76 * @brief standard implementation of an ItemList widget for KStandardItemListView and KStandardItemModel.
77 *
78 * @see KItemListWidget
79 */
80 class DOLPHIN_EXPORT KStandardItemListWidget : public KItemListWidget
81 {
82 Q_OBJECT
83
84 public:
85 enum Layout { IconsLayout, CompactLayout, DetailsLayout };
86
87 KStandardItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent);
88 ~KStandardItemListWidget() override;
89
90 void setLayout(Layout layout);
91
92 void setHighlightEntireRow(bool highlightEntireRow);
93 bool highlightEntireRow() const;
94
95 void setSupportsItemExpanding(bool supportsItemExpanding);
96 bool supportsItemExpanding() const;
97
98 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
99
100 QRectF iconRect() const override;
101 QRectF textRect() const override;
102 QRectF textFocusRect() const override;
103 QRectF selectionRect() const override;
104 QRectF expansionToggleRect() const override;
105 QRectF selectionToggleRect() const override;
106 QPixmap createDragPixmap(const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
107 /** @see KItemListWidget::startActivateSoonAnimation() */
108 void startActivateSoonAnimation(int timeUntilActivation) override;
109
110 static KItemListWidgetInformant *createInformant();
111
112 protected:
113 /**
114 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
115 * soon as the item need to gets repainted.
116 */
117 void invalidateCache();
118
119 /**
120 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
121 * soon as the item needs to get repainted.
122 */
123 void invalidateIconCache();
124
125 /**
126 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
127 * The default implementation is empty.
128 */
129 virtual void refreshCache();
130
131 /**
132 * @return True if the give role should be right aligned when showing it inside a column.
133 * Per default false is returned.
134 */
135 virtual bool isRoleRightAligned(const QByteArray &role) const;
136
137 /**
138 * @return True if the item should be visually marked as hidden item. Per default
139 * false is returned.
140 */
141 virtual bool isHidden() const;
142
143 /**
144 * @return A font based on baseFont which is customized according to the data shown in the widget.
145 */
146 virtual QFont customizedFont(const QFont &baseFont) const;
147
148 virtual QPalette::ColorRole normalTextColorRole() const;
149
150 void setTextColor(const QColor &color);
151 QColor textColor(const QWidget &widget) const;
152
153 void setOverlay(const QPixmap &overlay);
154 QPixmap overlay() const;
155
156 /**
157 * @see KStandardItemListWidgetInformant::roleText().
158 */
159 QString roleText(const QByteArray &role, const QHash<QByteArray, QVariant> &values) const;
160
161 /**
162 * Fixes:
163 * Select the text without MIME-type extension
164 * This is file-item-specific and should be moved
165 * into KFileItemListWidget.
166 *
167 * Inherited classes can define, if the MIME-type extension
168 * should be selected or not.
169 *
170 * @return Selection length (with or without MIME-type extension)
171 */
172 virtual int selectionLength(const QString &text) const;
173
174 void dataChanged(const QHash<QByteArray, QVariant> &current, const QSet<QByteArray> &roles = QSet<QByteArray>()) override;
175 void visibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous) override;
176 void columnWidthChanged(const QByteArray &role, qreal current, qreal previous) override;
177 void sidePaddingChanged(qreal width) override;
178 void styleOptionChanged(const KItemListStyleOption &current, const KItemListStyleOption &previous) override;
179 void hoveredChanged(bool hovered) override;
180 void selectedChanged(bool selected) override;
181 void siblingsInformationChanged(const QBitArray &current, const QBitArray &previous) override;
182 void editedRoleChanged(const QByteArray &current, const QByteArray &previous) override;
183 void iconSizeChanged(int current, int previous) override;
184 void resizeEvent(QGraphicsSceneResizeEvent *event) override;
185 void showEvent(QShowEvent *event) override;
186 void hideEvent(QHideEvent *event) override;
187 bool event(QEvent *event) override;
188
189 struct TextInfo {
190 QPointF pos;
191 QStaticText staticText;
192 };
193 void updateAdditionalInfoTextColor();
194
195 public Q_SLOTS:
196 void finishRoleEditing();
197
198 private Q_SLOTS:
199 void slotCutItemsChanged();
200 void slotRoleEditingCanceled(const QByteArray &role, const QVariant &value);
201 void slotRoleEditingFinished(const QByteArray &role, const QVariant &value);
202
203 private:
204 void triggerCacheRefreshing();
205 void updateExpansionArea();
206 void updatePixmapCache();
207
208 void updateTextsCache();
209 void updateIconsLayoutTextCache();
210 void updateCompactLayoutTextCache();
211 void updateDetailsLayoutTextCache();
212
213 void drawPixmap(QPainter *painter, const QPixmap &pixmap);
214 void drawSiblingsInformation(QPainter *painter);
215
216 QRectF roleEditingRect(const QByteArray &role) const;
217
218 QString elideRightKeepExtension(const QString &text, int elidingWidth) const;
219
220 /**
221 * Escapes text for display purposes.
222 *
223 * Replaces '\n' with Unicode line break (U+21B5).
224 */
225 QString escapeString(const QString &text) const;
226
227 /**
228 * Closes the role editor and returns the focus back
229 * to the KItemListContainer.
230 */
231 void closeRoleEditor();
232
233 QPixmap pixmapForIcon(const QString &name, const QStringList &overlays, int size, QIcon::Mode mode) const;
234
235 /**
236 * @return Preferred size of the rating-image based on the given
237 * style-option. The height of the font is taken as
238 * reference.
239 */
240 static QSizeF preferredRatingSize(const KItemListStyleOption &option);
241
242 /**
243 * @return Horizontal padding in pixels that is added to the required width of
244 * a column to display the content.
245 */
246 static qreal columnPadding(const KItemListStyleOption &option);
247
248 protected:
249 QHash<QByteArray, TextInfo *> m_textInfo; // PlacesItemListWidget needs to access this
250
251 private:
252 bool m_isCut;
253 bool m_isHidden;
254 QFont m_customizedFont;
255 QFontMetrics m_customizedFontMetrics;
256 bool m_isExpandable;
257 bool m_highlightEntireRow;
258 bool m_supportsItemExpanding;
259
260 bool m_dirtyLayout;
261 bool m_dirtyContent;
262 QSet<QByteArray> m_dirtyContentRoles;
263
264 Layout m_layout;
265 QPointF m_pixmapPos;
266 QPixmap m_pixmap;
267 QSize m_scaledPixmapSize; //Size of the pixmap in device independent pixels
268
269 qreal m_columnWidthSum;
270 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
271 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
272
273 QRectF m_textRect;
274
275 QList<QByteArray> m_sortedVisibleRoles;
276
277 QRectF m_expansionArea;
278
279 QColor m_customTextColor;
280 QColor m_additionalInfoTextColor;
281
282 QPixmap m_overlay;
283 QPixmap m_rating;
284
285 KItemListRoleEditor *m_roleEditor;
286 KItemListRoleEditor *m_oldRoleEditor;
287
288 /** @see startActivateSoonAnimation() */
289 QPointer<QVariantAnimation> m_activateSoonAnimation;
290
291 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
292 // share a common layout calculation
293 };
294
295 #endif