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
;
20 class QVariantAnimation
;
23 * @brief standard implementation of the ItemList widget informant for use with KStandardItemListView and KStandardItemModel.
25 * @see KItemListWidgetInformant
27 class DOLPHIN_EXPORT KStandardItemListWidgetInformant
: public KItemListWidgetInformant
30 KStandardItemListWidgetInformant();
31 ~KStandardItemListWidgetInformant() override
;
33 void calculateItemSizeHints(QVector
<std::pair
<qreal
/* height */, bool /* isElided */>> &logicalHeightHints
,
34 qreal
&logicalWidthHint
,
35 const KItemListView
*view
) const override
;
37 qreal
preferredRoleColumnWidth(const QByteArray
&role
, int index
, const KItemListView
*view
) const override
;
41 * @return The value of the "text" role. The default implementation returns
42 * view->model()->data(index)["text"]. If a derived class can
43 * prevent the (possibly expensive) construction of the
44 * QHash<QByteArray, QVariant> returned by KItemModelBase::data(int),
45 * it can reimplement this function.
47 virtual QString
itemText(int index
, const KItemListView
*view
) const;
50 * @return The value of the "isLink" role. The default implementation returns false.
51 * The derived class should reimplement this function, when information about
52 * links is available and in usage.
54 virtual bool itemIsLink(int index
, const KItemListView
*view
) const;
57 * @return String representation of the role \a role. The representation of
58 * a role might depend on other roles, so the values of all roles
59 * are passed as parameter.
61 virtual QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
64 * @return A font based on baseFont which is customized for symlinks.
66 virtual QFont
customizedFontForLinks(const QFont
&baseFont
) const;
68 void calculateIconsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
69 void calculateCompactLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
70 void calculateDetailsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
72 friend class KStandardItemListWidget
; // Accesses roleText()
76 * @brief standard implementation of an ItemList widget for KStandardItemListView and KStandardItemModel.
78 * @see KItemListWidget
80 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
85 enum Layout
{ IconsLayout
, CompactLayout
, DetailsLayout
};
87 KStandardItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
88 ~KStandardItemListWidget() override
;
90 void setLayout(Layout layout
);
92 void setHighlightEntireRow(bool highlightEntireRow
);
93 bool highlightEntireRow() const;
95 void setSupportsItemExpanding(bool supportsItemExpanding
);
96 bool supportsItemExpanding() const;
98 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
100 QRectF
iconRect() const override
;
101 QRectF
textRect() const override
;
102 QRectF
textFocusRect() const override
;
103 QRectF
selectionRect() const override
;
104 QRectF
expansionToggleRect() const override
;
105 QRectF
selectionToggleRect() const override
;
106 QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
107 /** @see KItemListWidget::startActivateSoonAnimation() */
108 void startActivateSoonAnimation(int timeUntilActivation
) override
;
110 static KItemListWidgetInformant
*createInformant();
114 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
115 * soon as the item need to gets repainted.
117 void invalidateCache();
120 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
121 * soon as the item needs to get repainted.
123 void invalidateIconCache();
126 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
127 * The default implementation is empty.
129 virtual void refreshCache();
132 * @return True if the give role should be right aligned when showing it inside a column.
133 * Per default false is returned.
135 virtual bool isRoleRightAligned(const QByteArray
&role
) const;
138 * @return True if the item should be visually marked as hidden item. Per default
141 virtual bool isHidden() const;
144 * @return A font based on baseFont which is customized according to the data shown in the widget.
146 virtual QFont
customizedFont(const QFont
&baseFont
) const;
148 virtual QPalette::ColorRole
normalTextColorRole() const;
150 void setTextColor(const QColor
&color
);
151 QColor
textColor(const QWidget
&widget
) const;
153 void setOverlay(const QPixmap
&overlay
);
154 QPixmap
overlay() const;
157 * @see KStandardItemListWidgetInformant::roleText().
159 QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
163 * Select the text without MIME-type extension
164 * This is file-item-specific and should be moved
165 * into KFileItemListWidget.
167 * Inherited classes can define, if the MIME-type extension
168 * should be selected or not.
170 * @return Selection length (with or without MIME-type extension)
172 virtual int selectionLength(const QString
&text
) const;
174 void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>()) override
;
175 void visibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
) override
;
176 void columnWidthChanged(const QByteArray
&role
, qreal current
, qreal previous
) override
;
177 void sidePaddingChanged(qreal width
) override
;
178 void styleOptionChanged(const KItemListStyleOption
¤t
, const KItemListStyleOption
&previous
) override
;
179 void hoveredChanged(bool hovered
) override
;
180 void selectedChanged(bool selected
) override
;
181 void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
) override
;
182 void editedRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
) override
;
183 void iconSizeChanged(int current
, int previous
) override
;
184 void resizeEvent(QGraphicsSceneResizeEvent
*event
) override
;
185 void showEvent(QShowEvent
*event
) override
;
186 void hideEvent(QHideEvent
*event
) override
;
187 bool event(QEvent
*event
) override
;
191 QStaticText staticText
;
193 void updateAdditionalInfoTextColor();
196 void finishRoleEditing();
199 void slotCutItemsChanged();
200 void slotRoleEditingCanceled(const QByteArray
&role
, const QVariant
&value
);
201 void slotRoleEditingFinished(const QByteArray
&role
, const QVariant
&value
);
204 void triggerCacheRefreshing();
205 void updateExpansionArea();
206 void updatePixmapCache();
208 void updateTextsCache();
209 void updateIconsLayoutTextCache();
210 void updateCompactLayoutTextCache();
211 void updateDetailsLayoutTextCache();
213 void drawPixmap(QPainter
*painter
, const QPixmap
&pixmap
);
214 void drawSiblingsInformation(QPainter
*painter
);
216 QRectF
roleEditingRect(const QByteArray
&role
) const;
218 QString
elideRightKeepExtension(const QString
&text
, int elidingWidth
) const;
221 * Escapes text for display purposes.
223 * Replaces '\n' with Unicode line break (U+21B5).
225 QString
escapeString(const QString
&text
) const;
228 * Closes the role editor and returns the focus back
229 * to the KItemListContainer.
231 void closeRoleEditor();
233 QPixmap
pixmapForIcon(const QString
&name
, const QStringList
&overlays
, int size
, QIcon::Mode mode
) const;
236 * @return Preferred size of the rating-image based on the given
237 * style-option. The height of the font is taken as
240 static QSizeF
preferredRatingSize(const KItemListStyleOption
&option
);
243 * @return Horizontal padding in pixels that is added to the required width of
244 * a column to display the content.
246 static qreal
columnPadding(const KItemListStyleOption
&option
);
249 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
254 QFont m_customizedFont
;
255 QFontMetrics m_customizedFontMetrics
;
257 bool m_highlightEntireRow
;
258 bool m_supportsItemExpanding
;
262 QSet
<QByteArray
> m_dirtyContentRoles
;
267 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
269 qreal m_columnWidthSum
;
270 QRectF m_iconRect
; // Cache for KItemListWidget::iconRect()
271 QPixmap m_hoverPixmap
; // Cache for modified m_pixmap when hovering the item
275 QList
<QByteArray
> m_sortedVisibleRoles
;
277 QRectF m_expansionArea
;
279 QColor m_customTextColor
;
280 QColor m_additionalInfoTextColor
;
285 KItemListRoleEditor
*m_roleEditor
;
286 KItemListRoleEditor
*m_oldRoleEditor
;
288 /** @see startActivateSoonAnimation() */
289 QPointer
<QVariantAnimation
> m_activateSoonAnimation
;
291 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
292 // share a common layout calculation