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;
57 /** Configure whether the requested text should be optimized for viewing on a screen or for being read out aloud by a text-to-speech engine. */
58 enum class ForUsageAs
{ DisplayedText
, SpokenText
};
61 * @param role The role the text is being requested for.
62 * @param values The data of the item. All the data is passed because the text might depend on multiple data points.
63 * @param forUsageAs Whether the roleText should be optimized for displaying (i.e. kept somewhat short) or optimized for speaking e.g. by screen readers
64 * or text-to-speech in general (i.e. by prefering announcing a month as July instead of as the number 7).
65 * @return String representation of the role \a role. The representation of
66 * a role might depend on other roles, so the values of all roles
67 * are passed as parameter.
69 virtual QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
, ForUsageAs forUsageAs
= ForUsageAs::DisplayedText
) const;
72 * @return A font based on baseFont which is customized for symlinks.
74 virtual QFont
customizedFontForLinks(const QFont
&baseFont
) const;
76 void calculateIconsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
77 void calculateCompactLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
78 void calculateDetailsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
80 friend class KStandardItemListWidget
; // Accesses roleText()
84 * @brief standard implementation of an ItemList widget for KStandardItemListView and KStandardItemModel.
86 * @see KItemListWidget
88 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
93 enum Layout
{ IconsLayout
, CompactLayout
, DetailsLayout
};
95 KStandardItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
96 ~KStandardItemListWidget() override
;
98 void setLayout(Layout layout
);
100 void setHighlightEntireRow(bool highlightEntireRow
);
101 bool highlightEntireRow() const;
103 void setSupportsItemExpanding(bool supportsItemExpanding
);
104 bool supportsItemExpanding() const;
106 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
108 QRectF
textRect() const override
;
109 QRectF
textFocusRect() const override
;
110 QRectF
selectionRectFull() const override
;
111 QRectF
selectionRectCore() const override
;
112 QRectF
expansionToggleRect() const override
;
113 QRectF
selectionToggleRect() const override
;
114 QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
115 /** @see KItemListWidget::startActivateSoonAnimation() */
116 void startActivateSoonAnimation(int timeUntilActivation
) override
;
118 static KItemListWidgetInformant
*createInformant();
122 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
123 * soon as the item need to gets repainted.
125 void invalidateCache();
128 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
129 * soon as the item needs to get repainted.
131 void invalidateIconCache();
134 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
135 * The default implementation is empty.
137 virtual void refreshCache();
140 * @return True if the give role should be right aligned when showing it inside a column.
141 * Per default false is returned.
143 virtual bool isRoleRightAligned(const QByteArray
&role
) const;
146 * @return True if the item should be visually marked as hidden item. Per default
149 virtual bool isHidden() const;
152 * @return A font based on baseFont which is customized according to the data shown in the widget.
154 virtual QFont
customizedFont(const QFont
&baseFont
) const;
156 virtual QPalette::ColorRole
normalTextColorRole() const;
158 void setTextColor(const QColor
&color
);
159 QColor
textColor(const QWidget
&widget
) const;
161 void setOverlays(QHash
<Qt::Corner
, QString
> &overlay
);
162 QHash
<Qt::Corner
, QString
> overlays() const;
165 * @see KStandardItemListWidgetInformant::roleText().
167 QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
169 static int numberOfUnicodeCharactersIn(const QString
&text
);
172 * @return Selection length (with or without MIME-type extension) in number of unicode characters, which might be different from number of QChars.
174 virtual int selectionLength(const QString
&text
) const;
176 void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>()) override
;
177 void visibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
) override
;
178 void columnWidthChanged(const QByteArray
&role
, qreal current
, qreal previous
) override
;
179 void sidePaddingChanged(qreal leftPaddingWidth
, qreal rightPaddingWidth
) override
;
180 void styleOptionChanged(const KItemListStyleOption
¤t
, const KItemListStyleOption
&previous
) override
;
181 void hoveredChanged(bool hovered
) override
;
182 void selectedChanged(bool selected
) override
;
183 void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
) override
;
184 void editedRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
) override
;
185 void iconSizeChanged(int current
, int previous
) override
;
186 void resizeEvent(QGraphicsSceneResizeEvent
*event
) override
;
187 void showEvent(QShowEvent
*event
) override
;
188 void hideEvent(QHideEvent
*event
) override
;
189 bool event(QEvent
*event
) override
;
193 QStaticText staticText
;
195 void updateAdditionalInfoTextColor();
198 void finishRoleEditing();
201 void slotCutItemsChanged();
202 void slotRoleEditingCanceled(const QByteArray
&role
, const QVariant
&value
);
203 void slotRoleEditingFinished(const QByteArray
&role
, const QVariant
&value
);
206 void triggerCacheRefreshing();
207 void updateExpansionArea();
208 void updatePixmapCache();
210 void updateTextsCache();
211 void updateIconsLayoutTextCache();
212 void updateCompactLayoutTextCache();
213 void updateDetailsLayoutTextCache();
215 QPixmap
addOverlays(const QPixmap
&pixmap
,
216 const QHash
<Qt::Corner
, QString
> &overlays
,
218 qreal devicePixelRatioF
,
219 QIcon::Mode mode
= QIcon::Normal
) const;
221 void drawPixmap(QPainter
*painter
, const QPixmap
&pixmap
);
222 /** Draw the lines and arrows that visualize the expanded state and level of this row. */
223 void drawSiblingsInformation(QPainter
*painter
);
225 QRectF
roleEditingRect(const QByteArray
&role
) const;
227 QString
elideText(QString text
, qreal elidingWidth
) const;
230 * Escapes text for display purposes.
232 * Replaces '\n' with Unicode line break (U+21B5).
234 QString
escapeString(const QString
&text
) const;
237 * Closes the role editor and returns the focus back
238 * to the KItemListContainer.
240 void closeRoleEditor();
242 QPixmap
pixmapForIcon(const QString
&name
, const QSize
&size
, QIcon::Mode mode
) const;
245 * @return Preferred size of the rating-image based on the given
246 * style-option. The height of the font is taken as
249 static QSizeF
preferredRatingSize(const KItemListStyleOption
&option
);
252 * @return Horizontal padding in pixels that is added to the required width of
253 * a column to display the content.
255 static qreal
columnPadding(const KItemListStyleOption
&option
);
257 /** @returns whether the usual icon should be shown or not. */
258 bool isIconControlledByActivateSoonAnimation() const;
261 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
266 QFont m_customizedFont
;
267 QFontMetrics m_customizedFontMetrics
;
269 bool m_highlightEntireRow
;
270 bool m_supportsItemExpanding
;
274 QSet
<QByteArray
> m_dirtyContentRoles
;
279 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
281 qreal m_columnWidthSum
;
282 QRectF m_iconRect
; // Cache for KItemListWidget::iconRect()
286 QList
<QByteArray
> m_sortedVisibleRoles
;
288 QRectF m_expansionArea
;
290 QColor m_customTextColor
;
291 QColor m_additionalInfoTextColor
;
293 QHash
<Qt::Corner
, QString
> m_overlays
;
296 KItemListRoleEditor
*m_roleEditor
;
297 KItemListRoleEditor
*m_oldRoleEditor
;
299 /** @see startActivateSoonAnimation() */
300 QPointer
<QVariantAnimation
> m_activateSoonAnimation
;
302 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
303 // share a common layout calculation