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