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