]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.h
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #ifndef KSTANDARDITEMLISTWIDGET_H
21 #define KSTANDARDITEMLISTWIDGET_H
22
23 #include "dolphin_export.h"
24 #include "kitemviews/kitemlistwidget.h"
25
26 #include <QPixmap>
27 #include <QPointF>
28 #include <QStaticText>
29
30 class KItemListRoleEditor;
31 class KItemListStyleOption;
32 class KItemListView;
33
34 class DOLPHIN_EXPORT KStandardItemListWidgetInformant : public KItemListWidgetInformant
35 {
36 public:
37 KStandardItemListWidgetInformant();
38 ~KStandardItemListWidgetInformant() override;
39
40 void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const override;
41
42 qreal preferredRoleColumnWidth(const QByteArray& role,
43 int index,
44 const KItemListView* view) const override;
45 protected:
46 /**
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.
52 */
53 virtual QString itemText(int index, const KItemListView* view) const;
54
55 /**
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.
59 */
60 virtual bool itemIsLink(int index, const KItemListView* view) const;
61
62 /**
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.
66 */
67 virtual QString roleText(const QByteArray& role,
68 const QHash<QByteArray, QVariant>& values) const;
69
70 /**
71 * @return A font based on baseFont which is customized for symlinks.
72 */
73 virtual QFont customizedFontForLinks(const QFont& baseFont) const;
74
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;
78
79 friend class KStandardItemListWidget; // Accesses roleText()
80 };
81
82 /**
83 * @brief Itemlist widget implementation for KStandardItemListView and KStandardItemModel.
84 */
85 class DOLPHIN_EXPORT KStandardItemListWidget : public KItemListWidget
86 {
87 Q_OBJECT
88
89 public:
90 enum Layout
91 {
92 IconsLayout,
93 CompactLayout,
94 DetailsLayout
95 };
96
97 KStandardItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent);
98 ~KStandardItemListWidget() override;
99
100 void setLayout(Layout layout);
101 Layout layout() const;
102
103 void setSupportsItemExpanding(bool supportsItemExpanding);
104 bool supportsItemExpanding() const;
105
106 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
107
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;
115
116 static KItemListWidgetInformant* createInformant();
117
118 protected:
119 /**
120 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
121 * soon as the item need to gets repainted.
122 */
123 void invalidateCache();
124
125 /**
126 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
127 * The default implementation is empty.
128 */
129 virtual void refreshCache();
130
131 /**
132 * @return True if the give role should be right aligned when showing it inside a column.
133 * Per default false is returned.
134 */
135 virtual bool isRoleRightAligned(const QByteArray& role) const;
136
137 /**
138 * @return True if the item should be visually marked as hidden item. Per default
139 * false is returned.
140 */
141 virtual bool isHidden() const;
142
143 /**
144 * @return A font based on baseFont which is customized according to the data shown in the widget.
145 */
146 virtual QFont customizedFont(const QFont& baseFont) const;
147
148 virtual QPalette::ColorRole normalTextColorRole() const;
149
150 void setTextColor(const QColor& color);
151 QColor textColor() const;
152
153 void setOverlay(const QPixmap& overlay);
154 QPixmap overlay() const;
155
156 /**
157 * @see KStandardItemListWidgetInformant::roleText().
158 */
159 QString roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values) const;
160
161 /**
162 * Fixes:
163 * Select the text without MIME-type extension
164 * This is file-item-specific and should be moved
165 * into KFileItemListWidget.
166 *
167 * Inherited classes can define, if the MIME-type extension
168 * should be selected or not.
169 *
170 * @return Selection length (with or without MIME-type extension)
171 */
172 virtual int selectionLength(const QString& text) const;
173
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;
186
187 public slots:
188 void finishRoleEditing();
189
190 private slots:
191 void slotCutItemsChanged();
192 void slotRoleEditingCanceled(const QByteArray& role, const QVariant& value);
193 void slotRoleEditingFinished(const QByteArray& role, const QVariant& value);
194
195 private:
196 void triggerCacheRefreshing();
197 void updateExpansionArea();
198 void updatePixmapCache();
199
200 void updateTextsCache();
201 void updateIconsLayoutTextCache();
202 void updateCompactLayoutTextCache();
203 void updateDetailsLayoutTextCache();
204
205 void updateAdditionalInfoTextColor();
206
207 void drawPixmap(QPainter* painter, const QPixmap& pixmap);
208 void drawSiblingsInformation(QPainter* painter);
209
210 QRectF roleEditingRect(const QByteArray &role) const;
211
212 QString elideRightKeepExtension(const QString &text, int elidingWidth) const;
213
214 /**
215 * Closes the role editor and returns the focus back
216 * to the KItemListContainer.
217 */
218 void closeRoleEditor();
219
220 static QPixmap pixmapForIcon(const QString& name, const QStringList& overlays, int size, QIcon::Mode mode);
221
222 /**
223 * @return Preferred size of the rating-image based on the given
224 * style-option. The height of the font is taken as
225 * reference.
226 */
227 static QSizeF preferredRatingSize(const KItemListStyleOption& option);
228
229 /**
230 * @return Horizontal padding in pixels that is added to the required width of
231 * a column to display the content.
232 */
233 static qreal columnPadding(const KItemListStyleOption& option);
234
235 private:
236 bool m_isCut;
237 bool m_isHidden;
238 QFont m_customizedFont;
239 QFontMetrics m_customizedFontMetrics;
240 bool m_isExpandable;
241 bool m_supportsItemExpanding;
242
243 bool m_dirtyLayout;
244 bool m_dirtyContent;
245 QSet<QByteArray> m_dirtyContentRoles;
246
247 Layout m_layout;
248 QPointF m_pixmapPos;
249 QPixmap m_pixmap;
250 QSize m_scaledPixmapSize; //Size of the pixmap in device independent pixels
251
252 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
253 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
254
255 struct TextInfo
256 {
257 QPointF pos;
258 QStaticText staticText;
259 };
260 QHash<QByteArray, TextInfo*> m_textInfo;
261
262 QRectF m_textRect;
263
264 QList<QByteArray> m_sortedVisibleRoles;
265
266 QRectF m_expansionArea;
267
268 QColor m_customTextColor;
269 QColor m_additionalInfoTextColor;
270
271 QPixmap m_overlay;
272 QPixmap m_rating;
273
274 KItemListRoleEditor* m_roleEditor;
275 KItemListRoleEditor* m_oldRoleEditor;
276
277 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
278 // share a common layout calculation
279 };
280
281 #endif