]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
Build with QT_NO_KEYWORDS
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.h
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef KSTANDARDITEMLISTWIDGET_H
8 #define KSTANDARDITEMLISTWIDGET_H
9
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemlistwidget.h"
12
13 #include <QPixmap>
14 #include <QPointF>
15 #include <QStaticText>
16
17 class KItemListRoleEditor;
18 class KItemListStyleOption;
19 class KItemListView;
20
21 class DOLPHIN_EXPORT KStandardItemListWidgetInformant : public KItemListWidgetInformant
22 {
23 public:
24 KStandardItemListWidgetInformant();
25 ~KStandardItemListWidgetInformant() override;
26
27 void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const override;
28
29 qreal preferredRoleColumnWidth(const QByteArray& role,
30 int index,
31 const KItemListView* view) const override;
32 protected:
33 /**
34 * @return The value of the "text" role. The default implementation returns
35 * view->model()->data(index)["text"]. If a derived class can
36 * prevent the (possibly expensive) construction of the
37 * QHash<QByteArray, QVariant> returned by KItemModelBase::data(int),
38 * it can reimplement this function.
39 */
40 virtual QString itemText(int index, const KItemListView* view) const;
41
42 /**
43 * @return The value of the "isLink" role. The default implementation returns false.
44 * The derived class should reimplement this function, when information about
45 * links is available and in usage.
46 */
47 virtual bool itemIsLink(int index, const KItemListView* view) const;
48
49 /**
50 * @return String representation of the role \a role. The representation of
51 * a role might depend on other roles, so the values of all roles
52 * are passed as parameter.
53 */
54 virtual QString roleText(const QByteArray& role,
55 const QHash<QByteArray, QVariant>& values) const;
56
57 /**
58 * @return A font based on baseFont which is customized for symlinks.
59 */
60 virtual QFont customizedFontForLinks(const QFont& baseFont) const;
61
62 void calculateIconsLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const;
63 void calculateCompactLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const;
64 void calculateDetailsLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const;
65
66 friend class KStandardItemListWidget; // Accesses roleText()
67 };
68
69 /**
70 * @brief Itemlist widget implementation for KStandardItemListView and KStandardItemModel.
71 */
72 class DOLPHIN_EXPORT KStandardItemListWidget : public KItemListWidget
73 {
74 Q_OBJECT
75
76 public:
77 enum Layout
78 {
79 IconsLayout,
80 CompactLayout,
81 DetailsLayout
82 };
83
84 KStandardItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent);
85 ~KStandardItemListWidget() override;
86
87 void setLayout(Layout layout);
88 Layout layout() const;
89
90 void setSupportsItemExpanding(bool supportsItemExpanding);
91 bool supportsItemExpanding() const;
92
93 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
94
95 QRectF iconRect() const override;
96 QRectF textRect() const override;
97 QRectF textFocusRect() const override;
98 QRectF selectionRect() const override;
99 QRectF expansionToggleRect() const override;
100 QRectF selectionToggleRect() const override;
101 QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
102
103 static KItemListWidgetInformant* createInformant();
104
105 protected:
106 /**
107 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
108 * soon as the item need to gets repainted.
109 */
110 void invalidateCache();
111
112 /**
113 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
114 * The default implementation is empty.
115 */
116 virtual void refreshCache();
117
118 /**
119 * @return True if the give role should be right aligned when showing it inside a column.
120 * Per default false is returned.
121 */
122 virtual bool isRoleRightAligned(const QByteArray& role) const;
123
124 /**
125 * @return True if the item should be visually marked as hidden item. Per default
126 * false is returned.
127 */
128 virtual bool isHidden() const;
129
130 /**
131 * @return A font based on baseFont which is customized according to the data shown in the widget.
132 */
133 virtual QFont customizedFont(const QFont& baseFont) const;
134
135 virtual QPalette::ColorRole normalTextColorRole() const;
136
137 void setTextColor(const QColor& color);
138 QColor textColor() const;
139
140 void setOverlay(const QPixmap& overlay);
141 QPixmap overlay() const;
142
143 /**
144 * @see KStandardItemListWidgetInformant::roleText().
145 */
146 QString roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values) const;
147
148 /**
149 * Fixes:
150 * Select the text without MIME-type extension
151 * This is file-item-specific and should be moved
152 * into KFileItemListWidget.
153 *
154 * Inherited classes can define, if the MIME-type extension
155 * should be selected or not.
156 *
157 * @return Selection length (with or without MIME-type extension)
158 */
159 virtual int selectionLength(const QString& text) const;
160
161 void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>()) override;
162 void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous) override;
163 void columnWidthChanged(const QByteArray& role, qreal current, qreal previous) override;
164 void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous) override;
165 void hoveredChanged(bool hovered) override;
166 void selectedChanged(bool selected) override;
167 void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous) override;
168 void editedRoleChanged(const QByteArray& current, const QByteArray& previous) override;
169 void resizeEvent(QGraphicsSceneResizeEvent* event) override;
170 void showEvent(QShowEvent* event) override;
171 void hideEvent(QHideEvent* event) override;
172 bool event(QEvent *event) override;
173
174 struct TextInfo
175 {
176 QPointF pos;
177 QStaticText staticText;
178 };
179
180 public Q_SLOTS:
181 void finishRoleEditing();
182
183 private Q_SLOTS:
184 void slotCutItemsChanged();
185 void slotRoleEditingCanceled(const QByteArray& role, const QVariant& value);
186 void slotRoleEditingFinished(const QByteArray& role, const QVariant& value);
187
188 private:
189 void triggerCacheRefreshing();
190 void updateExpansionArea();
191 void updatePixmapCache();
192
193 void updateTextsCache();
194 void updateIconsLayoutTextCache();
195 void updateCompactLayoutTextCache();
196 void updateDetailsLayoutTextCache();
197
198 void updateAdditionalInfoTextColor();
199
200 void drawPixmap(QPainter* painter, const QPixmap& pixmap);
201 void drawSiblingsInformation(QPainter* painter);
202
203 QRectF roleEditingRect(const QByteArray &role) const;
204
205 QString elideRightKeepExtension(const QString &text, int elidingWidth) const;
206
207 /**
208 * Closes the role editor and returns the focus back
209 * to the KItemListContainer.
210 */
211 void closeRoleEditor();
212
213 static QPixmap pixmapForIcon(const QString& name, const QStringList& overlays, int size, QIcon::Mode mode);
214
215 /**
216 * @return Preferred size of the rating-image based on the given
217 * style-option. The height of the font is taken as
218 * reference.
219 */
220 static QSizeF preferredRatingSize(const KItemListStyleOption& option);
221
222 /**
223 * @return Horizontal padding in pixels that is added to the required width of
224 * a column to display the content.
225 */
226 static qreal columnPadding(const KItemListStyleOption& option);
227
228 protected:
229 QHash<QByteArray, TextInfo*> m_textInfo; // PlacesItemListWidget needs to access this
230
231 private:
232 bool m_isCut;
233 bool m_isHidden;
234 QFont m_customizedFont;
235 QFontMetrics m_customizedFontMetrics;
236 bool m_isExpandable;
237 bool m_supportsItemExpanding;
238
239 bool m_dirtyLayout;
240 bool m_dirtyContent;
241 QSet<QByteArray> m_dirtyContentRoles;
242
243 Layout m_layout;
244 QPointF m_pixmapPos;
245 QPixmap m_pixmap;
246 QSize m_scaledPixmapSize; //Size of the pixmap in device independent pixels
247
248 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
249 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
250
251 QRectF m_textRect;
252
253 QList<QByteArray> m_sortedVisibleRoles;
254
255 QRectF m_expansionArea;
256
257 QColor m_customTextColor;
258 QColor m_additionalInfoTextColor;
259
260 QPixmap m_overlay;
261 QPixmap m_rating;
262
263 KItemListRoleEditor* m_roleEditor;
264 KItemListRoleEditor* m_oldRoleEditor;
265
266 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
267 // share a common layout calculation
268 };
269
270 #endif