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