1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #ifndef KSTANDARDITEMLISTWIDGET_H
21 #define KSTANDARDITEMLISTWIDGET_H
23 #include "dolphin_export.h"
24 #include "kitemviews/kitemlistwidget.h"
28 #include <QStaticText>
30 class KItemListRoleEditor
;
31 class KItemListStyleOption
;
34 class DOLPHIN_EXPORT KStandardItemListWidgetInformant
: public KItemListWidgetInformant
37 KStandardItemListWidgetInformant();
38 ~KStandardItemListWidgetInformant() override
;
40 void calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const override
;
42 qreal
preferredRoleColumnWidth(const QByteArray
& role
,
44 const KItemListView
* view
) const override
;
47 * @return The value of the "text" role. The default implementation returns
48 * view->model()->data(index)["text"]. If a derived class can
49 * prevent the (possibly expensive) construction of the
50 * QHash<QByteArray, QVariant> returned by KItemModelBase::data(int),
51 * it can reimplement this function.
53 virtual QString
itemText(int index
, const KItemListView
* view
) const;
56 * @return The value of the "isLink" role. The default implementation returns false.
57 * The derived class should reimplement this function, when information about
58 * links is available and in usage.
60 virtual bool itemIsLink(int index
, const KItemListView
* view
) const;
63 * @return String representation of the role \a role. The representation of
64 * a role might depend on other roles, so the values of all roles
65 * are passed as parameter.
67 virtual QString
roleText(const QByteArray
& role
,
68 const QHash
<QByteArray
, QVariant
>& values
) const;
71 * @return A font based on baseFont which is customized for symlinks.
73 virtual QFont
customizedFontForLinks(const QFont
& baseFont
) const;
75 void calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
76 void calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
77 void calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const;
79 friend class KStandardItemListWidget
; // Accesses roleText()
83 * @brief Itemlist widget implementation for KStandardItemListView and KStandardItemModel.
85 class DOLPHIN_EXPORT KStandardItemListWidget
: public KItemListWidget
97 KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
);
98 ~KStandardItemListWidget() override
;
100 void setLayout(Layout layout
);
101 Layout
layout() const;
103 void setSupportsItemExpanding(bool supportsItemExpanding
);
104 bool supportsItemExpanding() const;
106 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
108 QRectF
iconRect() const override
;
109 QRectF
textRect() const override
;
110 QRectF
textFocusRect() const override
;
111 QRectF
selectionRect() const override
;
112 QRectF
expansionToggleRect() const override
;
113 QRectF
selectionToggleRect() const override
;
114 QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
116 static KItemListWidgetInformant
* createInformant();
120 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
121 * soon as the item need to gets repainted.
123 void invalidateCache();
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;
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
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>()) override
;
175 void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
) override
;
176 void columnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
) override
;
177 void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
) override
;
178 void hoveredChanged(bool hovered
) override
;
179 void selectedChanged(bool selected
) override
;
180 void siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
) override
;
181 void editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
) override
;
182 void resizeEvent(QGraphicsSceneResizeEvent
* event
) override
;
183 void showEvent(QShowEvent
* event
) override
;
184 void hideEvent(QHideEvent
* event
) override
;
185 bool event(QEvent
*event
) override
;
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
);
238 QFont m_customizedFont
;
239 QFontMetrics m_customizedFontMetrics
;
241 bool m_supportsItemExpanding
;
245 QSet
<QByteArray
> m_dirtyContentRoles
;
250 QSize m_scaledPixmapSize
; //Size of the pixmap in device independent pixels
252 QRectF m_iconRect
; // Cache for KItemListWidget::iconRect()
253 QPixmap m_hoverPixmap
; // Cache for modified m_pixmap when hovering the item
258 QStaticText staticText
;
260 QHash
<QByteArray
, TextInfo
*> m_textInfo
;
264 QList
<QByteArray
> m_sortedVisibleRoles
;
266 QRectF m_expansionArea
;
268 QColor m_customTextColor
;
269 QColor m_additionalInfoTextColor
;
274 KItemListRoleEditor
* m_roleEditor
;
275 KItemListRoleEditor
* m_oldRoleEditor
;
277 friend class KStandardItemListWidgetInformant
; // Accesses private static methods to be able to
278 // share a common layout calculation