]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
4f7a9136e0c52ecbbf883453a79400870dcf4b26
[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 <libdolphin_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 LIBDOLPHINPRIVATE_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;
42
43 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
44 int index,
45 const KItemListView* view) const;
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 LIBDOLPHINPRIVATE_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);
108
109 virtual QRectF iconRect() const;
110 virtual QRectF textRect() const;
111 virtual QRectF textFocusRect() const;
112 virtual QRectF expansionToggleRect() const;
113 virtual QRectF selectionToggleRect() const;
114 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
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 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
175 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
176 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
177 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
178 virtual void hoveredChanged(bool hovered);
179 virtual void selectedChanged(bool selected);
180 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
181 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
182 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
183 virtual void showEvent(QShowEvent* event);
184 virtual void hideEvent(QHideEvent* event);
185
186 private slots:
187 void slotCutItemsChanged();
188 void slotRoleEditingCanceled(const QByteArray& role, const QVariant& value);
189 void slotRoleEditingFinished(const QByteArray& role, const QVariant& value);
190
191 private:
192 void triggerCacheRefreshing();
193 void updateExpansionArea();
194 void updatePixmapCache();
195
196 void updateTextsCache();
197 void updateIconsLayoutTextCache();
198 void updateCompactLayoutTextCache();
199 void updateDetailsLayoutTextCache();
200
201 void updateAdditionalInfoTextColor();
202
203 void drawPixmap(QPainter* painter, const QPixmap& pixmap);
204 void drawSiblingsInformation(QPainter* painter);
205
206 QRectF roleEditingRect(const QByteArray &role) const;
207
208 /**
209 * Closes the role editor and returns the focus back
210 * to the KItemListContainer.
211 */
212 void closeRoleEditor();
213
214 static QPixmap pixmapForIcon(const QString& name, const QStringList& overlays, int size);
215
216 /**
217 * @return Preferred size of the rating-image based on the given
218 * style-option. The height of the font is taken as
219 * reference.
220 */
221 static QSizeF preferredRatingSize(const KItemListStyleOption& option);
222
223 /**
224 * @return Horizontal padding in pixels that is added to the required width of
225 * a column to display the content.
226 */
227 static qreal columnPadding(const KItemListStyleOption& option);
228
229 private:
230 bool m_isCut;
231 bool m_isHidden;
232 QFont m_customizedFont;
233 QFontMetrics m_customizedFontMetrics;
234 bool m_isExpandable;
235 bool m_supportsItemExpanding;
236
237 bool m_dirtyLayout;
238 bool m_dirtyContent;
239 QSet<QByteArray> m_dirtyContentRoles;
240
241 Layout m_layout;
242 QPointF m_pixmapPos;
243 QPixmap m_pixmap;
244 QSize m_scaledPixmapSize;
245
246 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
247 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
248
249 struct TextInfo
250 {
251 QPointF pos;
252 QStaticText staticText;
253 };
254 QHash<QByteArray, TextInfo*> m_textInfo;
255
256 QRectF m_textRect;
257
258 QList<QByteArray> m_sortedVisibleRoles;
259
260 QRectF m_expansionArea;
261
262 QColor m_customTextColor;
263 QColor m_additionalInfoTextColor;
264
265 QPixmap m_overlay;
266 QPixmap m_rating;
267
268 KItemListRoleEditor* m_roleEditor;
269 KItemListRoleEditor* m_oldRoleEditor;
270
271 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
272 // share a common layout calculation
273 };
274
275 #endif
276
277