]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
Merge branch 'release/21.12'
[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 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;
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 leadingPaddingChanged(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 resizeEvent(QGraphicsSceneResizeEvent* event) override;
181 void showEvent(QShowEvent* event) override;
182 void hideEvent(QHideEvent* event) override;
183 bool event(QEvent *event) override;
184
185 struct TextInfo
186 {
187 QPointF pos;
188 QStaticText staticText;
189 };
190
191 public Q_SLOTS:
192 void finishRoleEditing();
193
194 private Q_SLOTS:
195 void slotCutItemsChanged();
196 void slotRoleEditingCanceled(const QByteArray& role, const QVariant& value);
197 void slotRoleEditingFinished(const QByteArray& role, const QVariant& value);
198
199 private:
200 void triggerCacheRefreshing();
201 void updateExpansionArea();
202 void updatePixmapCache();
203
204 void updateTextsCache();
205 void updateIconsLayoutTextCache();
206 void updateCompactLayoutTextCache();
207 void updateDetailsLayoutTextCache();
208
209 void updateAdditionalInfoTextColor();
210
211 void drawPixmap(QPainter* painter, const QPixmap& pixmap);
212 void drawSiblingsInformation(QPainter* painter);
213
214 QRectF roleEditingRect(const QByteArray &role) const;
215
216 QString elideRightKeepExtension(const QString &text, int elidingWidth) const;
217
218 /**
219 * Closes the role editor and returns the focus back
220 * to the KItemListContainer.
221 */
222 void closeRoleEditor();
223
224 static QPixmap pixmapForIcon(const QString& name, const QStringList& overlays, int size, QIcon::Mode mode);
225
226 /**
227 * @return Preferred size of the rating-image based on the given
228 * style-option. The height of the font is taken as
229 * reference.
230 */
231 static QSizeF preferredRatingSize(const KItemListStyleOption& option);
232
233 /**
234 * @return Horizontal padding in pixels that is added to the required width of
235 * a column to display the content.
236 */
237 static qreal columnPadding(const KItemListStyleOption& option);
238
239 protected:
240 QHash<QByteArray, TextInfo*> m_textInfo; // PlacesItemListWidget needs to access this
241
242 private:
243 bool m_isCut;
244 bool m_isHidden;
245 QFont m_customizedFont;
246 QFontMetrics m_customizedFontMetrics;
247 bool m_isExpandable;
248 bool m_highlightEntireRow;
249 bool m_supportsItemExpanding;
250
251 bool m_dirtyLayout;
252 bool m_dirtyContent;
253 QSet<QByteArray> m_dirtyContentRoles;
254
255 Layout m_layout;
256 QPointF m_pixmapPos;
257 QPixmap m_pixmap;
258 QSize m_scaledPixmapSize; //Size of the pixmap in device independent pixels
259
260 qreal m_columnWidthSum;
261 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
262 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
263
264 QRectF m_textRect;
265
266 QList<QByteArray> m_sortedVisibleRoles;
267
268 QRectF m_expansionArea;
269
270 QColor m_customTextColor;
271 QColor m_additionalInfoTextColor;
272
273 QPixmap m_overlay;
274 QPixmap m_rating;
275
276 KItemListRoleEditor* m_roleEditor;
277 KItemListRoleEditor* m_oldRoleEditor;
278
279 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
280 // share a common layout calculation
281 };
282
283 #endif