2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef KSTANDARDITEMLISTWIDGET_H
8 #define KSTANDARDITEMLISTWIDGET_H
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemlistwidget.h"
15 #include <QStaticText>
17 class KItemListRoleEditor
;
18 class KItemListStyleOption
;
22 * @brief standard implementation of the ItemList widget informant for use with KStandardItemListView and KStandardItemModel.
24 * @see KItemListWidgetInformant
26 class DOLPHIN_EXPORT KStandardItemListWidgetInformant
: public KItemListWidgetInformant
29 KStandardItemListWidgetInformant();
30 ~KStandardItemListWidgetInformant() override
;
32 void calculateItemSizeHints(QVector
<std::pair
<qreal
/* height */, bool /* isElided */>> &logicalHeightHints
,
33 qreal
&logicalWidthHint
,
34 const KItemListView
*view
) const override
;
36 qreal
preferredRoleColumnWidth(const QByteArray
&role
, int index
, const KItemListView
*view
) const override
;
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.
46 virtual QString
itemText(int index
, const KItemListView
*view
) const;
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.
53 virtual bool itemIsLink(int index
, const KItemListView
*view
) const;
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.
60 virtual QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
63 * @return A font based on baseFont which is customized for symlinks.
65 virtual QFont
customizedFontForLinks(const QFont
&baseFont
) const;
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;
71 friend class KStandardItemListWidget
; // Accesses roleText()
75 * @brief standard implementation of an ItemList widget for KStandardItemListView and KStandardItemModel.
77 * @see KItemListWidget
79 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
84 enum Layout
{ IconsLayout
, CompactLayout
, DetailsLayout
};
86 KStandardItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
87 ~KStandardItemListWidget() override
;
89 void setLayout(Layout layout
);
91 void setHighlightEntireRow(bool highlightEntireRow
);
92 bool highlightEntireRow() const;
94 void setSupportsItemExpanding(bool supportsItemExpanding
);
95 bool supportsItemExpanding() const;
97 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
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
;
107 static KItemListWidgetInformant
*createInformant();
111 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
112 * soon as the item need to gets repainted.
114 void invalidateCache();
117 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
118 * soon as the item needs to get repainted.
120 void invalidateIconCache();
123 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
124 * The default implementation is empty.
126 virtual void refreshCache();
129 * @return True if the give role should be right aligned when showing it inside a column.
130 * Per default false is returned.
132 virtual bool isRoleRightAligned(const QByteArray
&role
) const;
135 * @return True if the item should be visually marked as hidden item. Per default
138 virtual bool isHidden() const;
141 * @return A font based on baseFont which is customized according to the data shown in the widget.
143 virtual QFont
customizedFont(const QFont
&baseFont
) const;
145 virtual QPalette::ColorRole
normalTextColorRole() const;
147 void setTextColor(const QColor
&color
);
148 QColor
textColor(const QWidget
&widget
) const;
150 void setOverlay(const QPixmap
&overlay
);
151 QPixmap
overlay() const;
154 * @see KStandardItemListWidgetInformant::roleText().
156 QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
160 * Select the text without MIME-type extension
161 * This is file-item-specific and should be moved
162 * into KFileItemListWidget.
164 * Inherited classes can define, if the MIME-type extension
165 * should be selected or not.
167 * @return Selection length (with or without MIME-type extension)
169 virtual int selectionLength(const QString
&text
) const;
171 void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>()) override
;
172 void visibleRolesChanged(const QList
<QByteArray
> ¤t
, 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
¤t
, const KItemListStyleOption
&previous
) override
;
176 void hoveredChanged(bool hovered
) override
;
177 void selectedChanged(bool selected
) override
;
178 void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
) override
;
179 void editedRoleChanged(const QByteArray
¤t
, 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
;
188 QStaticText staticText
;
190 void updateAdditionalInfoTextColor();
193 void finishRoleEditing();
196 void slotCutItemsChanged();
197 void slotRoleEditingCanceled(const QByteArray
&role
, const QVariant
&value
);
198 void slotRoleEditingFinished(const QByteArray
&role
, const QVariant
&value
);
201 void triggerCacheRefreshing();
202 void updateExpansionArea();
203 void updatePixmapCache();
205 void updateTextsCache();
206 void updateIconsLayoutTextCache();
207 void updateCompactLayoutTextCache();
208 void updateDetailsLayoutTextCache();
210 void drawPixmap(QPainter
*painter
, const QPixmap
&pixmap
);
211 void drawSiblingsInformation(QPainter
*painter
);
213 QRectF
roleEditingRect(const QByteArray
&role
) const;
215 QString
elideRightKeepExtension(const QString
&text
, int elidingWidth
) const;
218 * Escapes text for display purposes.
220 * Replaces '\n' with Unicode line break (U+21B5).
222 QString
escapeString(const QString
&text
) const;
225 * Closes the role editor and returns the focus back
226 * to the KItemListContainer.
228 void closeRoleEditor();
230 QPixmap
pixmapForIcon(const QString
&name
, const QStringList
&overlays
, int size
, QIcon::Mode mode
) const;
233 * @return Preferred size of the rating-image based on the given
234 * style-option. The height of the font is taken as
237 static QSizeF
preferredRatingSize(const KItemListStyleOption
&option
);
240 * @return Horizontal padding in pixels that is added to the required width of
241 * a column to display the content.
243 static qreal
columnPadding(const KItemListStyleOption
&option
);
246 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
251 QFont m_customizedFont
;
252 QFontMetrics m_customizedFontMetrics
;
254 bool m_highlightEntireRow
;
255 bool m_supportsItemExpanding
;
259 QSet
<QByteArray
> m_dirtyContentRoles
;
264 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
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
272 QList
<QByteArray
> m_sortedVisibleRoles
;
274 QRectF m_expansionArea
;
276 QColor m_customTextColor
;
277 QColor m_additionalInfoTextColor
;
282 KItemListRoleEditor
*m_roleEditor
;
283 KItemListRoleEditor
*m_oldRoleEditor
;
285 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
286 // share a common layout calculation