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