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"
16 #include <QStaticText>
18 class KItemListRoleEditor
;
19 class KItemListStyleOption
;
21 class QVariantAnimation
;
24 * @brief standard implementation of the ItemList widget informant for use with KStandardItemListView and KStandardItemModel.
26 * @see KItemListWidgetInformant
28 class DOLPHIN_EXPORT KStandardItemListWidgetInformant
: public KItemListWidgetInformant
31 KStandardItemListWidgetInformant();
32 ~KStandardItemListWidgetInformant() override
;
34 void calculateItemSizeHints(QVector
<std::pair
<qreal
/* height */, bool /* isElided */>> &logicalHeightHints
,
35 qreal
&logicalWidthHint
,
36 const KItemListView
*view
) const override
;
38 qreal
preferredRoleColumnWidth(const QByteArray
&role
, int index
, const KItemListView
*view
) const override
;
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.
48 virtual QString
itemText(int index
, const KItemListView
*view
) const;
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.
55 virtual bool itemIsLink(int index
, const KItemListView
*view
) const;
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.
62 virtual QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
65 * @return A font based on baseFont which is customized for symlinks.
67 virtual QFont
customizedFontForLinks(const QFont
&baseFont
) const;
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;
73 friend class KStandardItemListWidget
; // Accesses roleText()
77 * @brief standard implementation of an ItemList widget for KStandardItemListView and KStandardItemModel.
79 * @see KItemListWidget
81 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
86 enum Layout
{ IconsLayout
, CompactLayout
, DetailsLayout
};
88 KStandardItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
89 ~KStandardItemListWidget() override
;
91 void setLayout(Layout layout
);
93 void setHighlightEntireRow(bool highlightEntireRow
);
94 bool highlightEntireRow() const;
96 void setSupportsItemExpanding(bool supportsItemExpanding
);
97 bool supportsItemExpanding() const;
99 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
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
;
111 static KItemListWidgetInformant
*createInformant();
115 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
116 * soon as the item need to gets repainted.
118 void invalidateCache();
121 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
122 * soon as the item needs to get repainted.
124 void invalidateIconCache();
127 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
128 * The default implementation is empty.
130 virtual void refreshCache();
133 * @return True if the give role should be right aligned when showing it inside a column.
134 * Per default false is returned.
136 virtual bool isRoleRightAligned(const QByteArray
&role
) const;
139 * @return True if the item should be visually marked as hidden item. Per default
142 virtual bool isHidden() const;
145 * @return A font based on baseFont which is customized according to the data shown in the widget.
147 virtual QFont
customizedFont(const QFont
&baseFont
) const;
149 virtual QPalette::ColorRole
normalTextColorRole() const;
151 void setTextColor(const QColor
&color
);
152 QColor
textColor(const QWidget
&widget
) const;
154 void setOverlay(const QPixmap
&overlay
);
155 QPixmap
overlay() const;
158 * @see KStandardItemListWidgetInformant::roleText().
160 QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
162 static int numberOfUnicodeCharactersIn(const QString
&text
);
165 * @return Selection length (with or without MIME-type extension) in number of unicode characters, which might be different from number of QChars.
167 virtual int selectionLength(const QString
&text
) const;
169 void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>()) override
;
170 void visibleRolesChanged(const QList
<QByteArray
> ¤t
, 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
¤t
, const KItemListStyleOption
&previous
) override
;
174 void hoveredChanged(bool hovered
) override
;
175 void selectedChanged(bool selected
) override
;
176 void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
) override
;
177 void editedRoleChanged(const QByteArray
¤t
, 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
;
186 QStaticText staticText
;
188 void updateAdditionalInfoTextColor();
191 void finishRoleEditing();
194 void slotCutItemsChanged();
195 void slotRoleEditingCanceled(const QByteArray
&role
, const QVariant
&value
);
196 void slotRoleEditingFinished(const QByteArray
&role
, const QVariant
&value
);
199 void triggerCacheRefreshing();
200 void updateExpansionArea();
201 void updatePixmapCache();
203 void updateTextsCache();
204 void updateIconsLayoutTextCache();
205 void updateCompactLayoutTextCache();
206 void updateDetailsLayoutTextCache();
208 void drawPixmap(QPainter
*painter
, const QPixmap
&pixmap
);
209 void drawSiblingsInformation(QPainter
*painter
);
211 QRectF
roleEditingRect(const QByteArray
&role
) const;
213 QString
elideRightKeepExtension(const QString
&text
, int elidingWidth
) const;
216 * Escapes text for display purposes.
218 * Replaces '\n' with Unicode line break (U+21B5).
220 QString
escapeString(const QString
&text
) const;
223 * Closes the role editor and returns the focus back
224 * to the KItemListContainer.
226 void closeRoleEditor();
228 QPixmap
pixmapForIcon(const QString
&name
, const QStringList
&overlays
, int size
, QIcon::Mode mode
) const;
231 * @return Preferred size of the rating-image based on the given
232 * style-option. The height of the font is taken as
235 static QSizeF
preferredRatingSize(const KItemListStyleOption
&option
);
238 * @return Horizontal padding in pixels that is added to the required width of
239 * a column to display the content.
241 static qreal
columnPadding(const KItemListStyleOption
&option
);
243 /** @returns whether the usual icon should be shown or not. */
244 bool isIconControlledByActivateSoonAnimation() const;
247 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
252 QFont m_customizedFont
;
253 QFontMetrics m_customizedFontMetrics
;
255 bool m_highlightEntireRow
;
256 bool m_supportsItemExpanding
;
260 QSet
<QByteArray
> m_dirtyContentRoles
;
265 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
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
273 QList
<QByteArray
> m_sortedVisibleRoles
;
275 QRectF m_expansionArea
;
277 QColor m_customTextColor
;
278 QColor m_additionalInfoTextColor
;
283 KItemListRoleEditor
*m_roleEditor
;
284 KItemListRoleEditor
*m_oldRoleEditor
;
286 /** @see startActivateSoonAnimation() */
287 QPointer
<QVariantAnimation
> m_activateSoonAnimation
;
289 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
290 // share a common layout calculation