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
, qreal
& logicalWidthHint
, const KItemListView
* view
) const override
;
29 qreal
preferredRoleColumnWidth(const QByteArray
& role
,
31 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
,
56 const QHash
<QByteArray
, QVariant
>& values
) const;
59 * @return A font based on baseFont which is customized for symlinks.
61 virtual QFont
customizedFontForLinks(const QFont
& baseFont
) const;
63 void calculateIconsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
64 void calculateCompactLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
65 void calculateDetailsLayoutItemSizeHints(QVector
<std::pair
<qreal
, bool>>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
67 friend class KStandardItemListWidget
; // Accesses roleText()
71 * @brief ItemList widget implementation for KStandardItemListView and KStandardItemModel.
73 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
85 KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
);
86 ~KStandardItemListWidget() override
;
88 void setLayout(Layout layout
);
89 Layout
layout() const;
91 void setSupportsItemExpanding(bool supportsItemExpanding
);
92 bool supportsItemExpanding() const;
94 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
96 QRectF
iconRect() const override
;
97 QRectF
textRect() const override
;
98 QRectF
textFocusRect() const override
;
99 QRectF
selectionRect() const override
;
100 QRectF
expansionToggleRect() const override
;
101 QRectF
selectionToggleRect() const override
;
102 QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
104 static KItemListWidgetInformant
* createInformant();
108 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
109 * soon as the item need to gets repainted.
111 void invalidateCache();
114 * Invalidates the icon cache which results in calling KStandardItemListWidget::refreshCache() as
115 * soon as the item needs to get repainted.
117 void invalidateIconCache();
120 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
121 * The default implementation is empty.
123 virtual void refreshCache();
126 * @return True if the give role should be right aligned when showing it inside a column.
127 * Per default false is returned.
129 virtual bool isRoleRightAligned(const QByteArray
& role
) const;
132 * @return True if the item should be visually marked as hidden item. Per default
135 virtual bool isHidden() const;
138 * @return A font based on baseFont which is customized according to the data shown in the widget.
140 virtual QFont
customizedFont(const QFont
& baseFont
) const;
142 virtual QPalette::ColorRole
normalTextColorRole() const;
144 void setTextColor(const QColor
& color
);
145 QColor
textColor() const;
147 void setOverlay(const QPixmap
& overlay
);
148 QPixmap
overlay() const;
151 * @see KStandardItemListWidgetInformant::roleText().
153 QString
roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
) const;
157 * Select the text without MIME-type extension
158 * This is file-item-specific and should be moved
159 * into KFileItemListWidget.
161 * Inherited classes can define, if the MIME-type extension
162 * should be selected or not.
164 * @return Selection length (with or without MIME-type extension)
166 virtual int selectionLength(const QString
& text
) const;
168 void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>()) override
;
169 void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
) override
;
170 void columnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
) override
;
171 void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
) override
;
172 void hoveredChanged(bool hovered
) override
;
173 void selectedChanged(bool selected
) override
;
174 void siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
) override
;
175 void editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
) override
;
176 void resizeEvent(QGraphicsSceneResizeEvent
* event
) override
;
177 void showEvent(QShowEvent
* event
) override
;
178 void hideEvent(QHideEvent
* event
) override
;
179 bool event(QEvent
*event
) override
;
184 QStaticText staticText
;
188 void finishRoleEditing();
191 void slotCutItemsChanged();
192 void slotRoleEditingCanceled(const QByteArray
& role
, const QVariant
& value
);
193 void slotRoleEditingFinished(const QByteArray
& role
, const QVariant
& value
);
196 void triggerCacheRefreshing();
197 void updateExpansionArea();
198 void updatePixmapCache();
200 void updateTextsCache();
201 void updateIconsLayoutTextCache();
202 void updateCompactLayoutTextCache();
203 void updateDetailsLayoutTextCache();
205 void updateAdditionalInfoTextColor();
207 void drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
);
208 void drawSiblingsInformation(QPainter
* painter
);
210 QRectF
roleEditingRect(const QByteArray
&role
) const;
212 QString
elideRightKeepExtension(const QString
&text
, int elidingWidth
) const;
215 * Closes the role editor and returns the focus back
216 * to the KItemListContainer.
218 void closeRoleEditor();
220 static QPixmap
pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
, QIcon::Mode mode
);
223 * @return Preferred size of the rating-image based on the given
224 * style-option. The height of the font is taken as
227 static QSizeF
preferredRatingSize(const KItemListStyleOption
& option
);
230 * @return Horizontal padding in pixels that is added to the required width of
231 * a column to display the content.
233 static qreal
columnPadding(const KItemListStyleOption
& option
);
236 QHash
<QByteArray
, TextInfo
*> m_textInfo
; // PlacesItemListWidget needs to access this
241 QFont m_customizedFont
;
242 QFontMetrics m_customizedFontMetrics
;
244 bool m_supportsItemExpanding
;
248 QSet
<QByteArray
> m_dirtyContentRoles
;
253 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
255 QRectF m_iconRect
; // Cache for KItemListWidget::iconRect()
256 QPixmap m_hoverPixmap
; // Cache for modified m_pixmap when hovering the item
260 QList
<QByteArray
> m_sortedVisibleRoles
;
262 QRectF m_expansionArea
;
264 QColor m_customTextColor
;
265 QColor m_additionalInfoTextColor
;
270 KItemListRoleEditor
* m_roleEditor
;
271 KItemListRoleEditor
* m_oldRoleEditor
;
273 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
274 // share a common layout calculation