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