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
;
21 class DOLPHIN_EXPORT KStandardItemListWidgetInformant
: public KItemListWidgetInformant
24 KStandardItemListWidgetInformant();
25 ~KStandardItemListWidgetInformant() override
;
27 void calculateItemSizeHints(QVector
<std::pair
<qreal
/* height */, bool /* isElided */>> &logicalHeightHints
,
28 qreal
&logicalWidthHint
,
29 const KItemListView
*view
) const override
;
31 qreal
preferredRoleColumnWidth(const QByteArray
&role
, int index
, const KItemListView
*view
) const override
;
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.
41 virtual QString
itemText(int index
, const KItemListView
*view
) const;
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.
48 virtual bool itemIsLink(int index
, const KItemListView
*view
) const;
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.
55 virtual QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
58 * @return A font based on baseFont which is customized for symlinks.
60 virtual QFont
customizedFontForLinks(const QFont
&baseFont
) const;
62 void calculateIconsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
63 void calculateCompactLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
64 void calculateDetailsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const;
66 friend class KStandardItemListWidget
; // Accesses roleText()
70 * @brief ItemList widget implementation for KStandardItemListView and KStandardItemModel.
72 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
77 enum Layout
{ IconsLayout
, CompactLayout
, DetailsLayout
};
79 KStandardItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
80 ~KStandardItemListWidget() override
;
82 void setLayout(Layout layout
);
83 Layout
layout() const;
85 void setHighlightEntireRow(bool highlightEntireRow
);
86 bool highlightEntireRow() const;
88 void setSupportsItemExpanding(bool supportsItemExpanding
);
89 bool supportsItemExpanding() const;
91 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
93 QRectF
iconRect() const override
;
94 QRectF
textRect() const override
;
95 QRectF
textFocusRect() const override
;
96 QRectF
selectionRect() const override
;
97 QRectF
expansionToggleRect() const override
;
98 QRectF
selectionToggleRect() const override
;
99 QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
101 static KItemListWidgetInformant
*createInformant();
105 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
106 * soon as the item need to gets repainted.
108 void invalidateCache();
111 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
112 * soon as the item needs to get repainted.
114 void invalidateIconCache();
117 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
118 * The default implementation is empty.
120 virtual void refreshCache();
123 * @return True if the give role should be right aligned when showing it inside a column.
124 * Per default false is returned.
126 virtual bool isRoleRightAligned(const QByteArray
&role
) const;
129 * @return True if the item should be visually marked as hidden item. Per default
132 virtual bool isHidden() const;
135 * @return A font based on baseFont which is customized according to the data shown in the widget.
137 virtual QFont
customizedFont(const QFont
&baseFont
) const;
139 virtual QPalette::ColorRole
normalTextColorRole() const;
141 void setTextColor(const QColor
&color
);
142 QColor
textColor(const QWidget
&widget
) const;
144 void setOverlay(const QPixmap
&overlay
);
145 QPixmap
overlay() const;
148 * @see KStandardItemListWidgetInformant::roleText().
150 QString
roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const;
154 * Select the text without MIME-type extension
155 * This is file-item-specific and should be moved
156 * into KFileItemListWidget.
158 * Inherited classes can define, if the MIME-type extension
159 * should be selected or not.
161 * @return Selection length (with or without MIME-type extension)
163 virtual int selectionLength(const QString
&text
) const;
165 void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>()) override
;
166 void visibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
) override
;
167 void columnWidthChanged(const QByteArray
&role
, qreal current
, qreal previous
) override
;
168 void sidePaddingChanged(qreal width
) override
;
169 void styleOptionChanged(const KItemListStyleOption
¤t
, const KItemListStyleOption
&previous
) override
;
170 void hoveredChanged(bool hovered
) override
;
171 void selectedChanged(bool selected
) override
;
172 void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
) override
;
173 void editedRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
) override
;
174 void iconSizeChanged(int current
, int previous
) override
;
175 void resizeEvent(QGraphicsSceneResizeEvent
*event
) override
;
176 void showEvent(QShowEvent
*event
) override
;
177 void hideEvent(QHideEvent
*event
) override
;
178 bool event(QEvent
*event
) override
;
182 QStaticText staticText
;
184 void updateAdditionalInfoTextColor();
187 void finishRoleEditing();
190 void slotCutItemsChanged();
191 void slotRoleEditingCanceled(const QByteArray
&role
, const QVariant
&value
);
192 void slotRoleEditingFinished(const QByteArray
&role
, const QVariant
&value
);
195 void triggerCacheRefreshing();
196 void updateExpansionArea();
197 void updatePixmapCache();
199 void updateTextsCache();
200 void updateIconsLayoutTextCache();
201 void updateCompactLayoutTextCache();
202 void updateDetailsLayoutTextCache();
204 void drawPixmap(QPainter
*painter
, const QPixmap
&pixmap
);
205 void drawSiblingsInformation(QPainter
*painter
);
207 QRectF
roleEditingRect(const QByteArray
&role
) const;
209 QString
elideRightKeepExtension(const QString
&text
, int elidingWidth
) const;
212 * Closes the role editor and returns the focus back
213 * to the KItemListContainer.
215 void closeRoleEditor();
217 QPixmap
pixmapForIcon(const QString
&name
, const QStringList
&overlays
, int size
, QIcon::Mode mode
) const;
220 * @return Preferred size of the rating-image based on the given
221 * style-option. The height of the font is taken as
224 static QSizeF
preferredRatingSize(const KItemListStyleOption
&option
);
227 * @return Horizontal padding in pixels that is added to the required width of
228 * a column to display the content.
230 static qreal
columnPadding(const KItemListStyleOption
&option
);
233 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
238 QFont m_customizedFont
;
239 QFontMetrics m_customizedFontMetrics
;
241 bool m_highlightEntireRow
;
242 bool m_supportsItemExpanding
;
246 QSet
<QByteArray
> m_dirtyContentRoles
;
251 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
253 qreal m_columnWidthSum
;
254 QRectF m_iconRect
; // Cache for KItemListWidget::iconRect()
255 QPixmap m_hoverPixmap
; // Cache for modified m_pixmap when hovering the item
259 QList
<QByteArray
> m_sortedVisibleRoles
;
261 QRectF m_expansionArea
;
263 QColor m_customTextColor
;
264 QColor m_additionalInfoTextColor
;
269 KItemListRoleEditor
*m_roleEditor
;
270 KItemListRoleEditor
*m_oldRoleEditor
;
272 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
273 // share a common layout calculation