]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.h
Calculate all item size hints at once.
[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<QSizeF>& sizeHints, 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 String representation of the role \a role. The representation of
58 * a role might depend on other roles, so the values of all roles
59 * are passed as parameter.
60 */
61 virtual QString roleText(const QByteArray& role,
62 const QHash<QByteArray, QVariant>& values) const;
63
64 void calculateIconsLayoutItemSizeHints(QVector<QSizeF>& sizeHints, const KItemListView* view) const;
65 void calculateCompactLayoutItemSizeHints(QVector<QSizeF>& sizeHints, const KItemListView* view) const;
66 void calculateDetailsLayoutItemSizeHints(QVector<QSizeF>& sizeHints, const KItemListView* view) const;
67
68 friend class KStandardItemListWidget; // Accesses roleText()
69 };
70
71 /**
72 * @brief Itemlist widget implementation for KStandardItemView and KStandardItemModel.
73 */
74 class LIBDOLPHINPRIVATE_EXPORT KStandardItemListWidget : public KItemListWidget
75 {
76 Q_OBJECT
77
78 public:
79 enum Layout
80 {
81 IconsLayout,
82 CompactLayout,
83 DetailsLayout
84 };
85
86 KStandardItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent);
87 virtual ~KStandardItemListWidget();
88
89 void setLayout(Layout layout);
90 Layout layout() const;
91
92 void setSupportsItemExpanding(bool supportsItemExpanding);
93 bool supportsItemExpanding() const;
94
95 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
96
97 virtual QRectF iconRect() const;
98 virtual QRectF textRect() const;
99 virtual QRectF textFocusRect() const;
100 virtual QRectF expansionToggleRect() const;
101 virtual QRectF selectionToggleRect() const;
102 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
103
104 static KItemListWidgetInformant* createInformant();
105
106 protected:
107 /**
108 * Invalidates the cache which results in calling KStandardItemListWidget::refreshCache() as
109 * soon as the item need to gets repainted.
110 */
111 void invalidateCache();
112
113 /**
114 * Is called if the cache got invalidated by KStandardItemListWidget::invalidateCache().
115 * The default implementation is empty.
116 */
117 virtual void refreshCache();
118
119 /**
120 * @return True if the give role should be right aligned when showing it inside a column.
121 * Per default false is returned.
122 */
123 virtual bool isRoleRightAligned(const QByteArray& role) const;
124
125 /**
126 * @return True if the item should be visually marked as hidden item. Per default
127 * false is returned.
128 */
129 virtual bool isHidden() const;
130
131 /**
132 * @return A font based on baseFont which is customized according to the data shown in the widget.
133 */
134 virtual QFont customizedFont(const QFont& baseFont) const;
135
136 virtual QPalette::ColorRole normalTextColorRole() const;
137
138 void setTextColor(const QColor& color);
139 QColor textColor() const;
140
141 void setOverlay(const QPixmap& overlay);
142 QPixmap overlay() const;
143
144 /**
145 * @see KStandardItemListWidgetInformant::roleText().
146 */
147 QString roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values) const;
148
149 /**
150 * Fixes:
151 * Select the text without MIME-type extension
152 * This is file-item-specific and should be moved
153 * into KFileItemListWidget.
154 *
155 * Inherited classes can define, if the MIME-type extension
156 * should be selected or not.
157 *
158 * @return Selection length (with or without MIME-type extension)
159 */
160 virtual int selectionLength(const QString& text) const;
161
162 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
163 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
164 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
165 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
166 virtual void hoveredChanged(bool hovered);
167 virtual void selectedChanged(bool selected);
168 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
169 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
170 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
171 virtual void showEvent(QShowEvent* event);
172 virtual void hideEvent(QHideEvent* event);
173
174 private slots:
175 void slotCutItemsChanged();
176 void slotRoleEditingCanceled(const QByteArray& role, const QVariant& value);
177 void slotRoleEditingFinished(const QByteArray& role, const QVariant& value);
178
179 private:
180 void triggerCacheRefreshing();
181 void updateExpansionArea();
182 void updatePixmapCache();
183
184 void updateTextsCache();
185 void updateIconsLayoutTextCache();
186 void updateCompactLayoutTextCache();
187 void updateDetailsLayoutTextCache();
188
189 void updateAdditionalInfoTextColor();
190
191 void drawPixmap(QPainter* painter, const QPixmap& pixmap);
192 void drawSiblingsInformation(QPainter* painter);
193
194 QRectF roleEditingRect(const QByteArray &role) const;
195
196 /**
197 * Closes the role editor and returns the focus back
198 * to the KItemListContainer.
199 */
200 void closeRoleEditor();
201
202 static QPixmap pixmapForIcon(const QString& name, const QStringList& overlays, int size);
203
204 /**
205 * @return Preferred size of the rating-image based on the given
206 * style-option. The height of the font is taken as
207 * reference.
208 */
209 static QSizeF preferredRatingSize(const KItemListStyleOption& option);
210
211 /**
212 * @return Horizontal padding in pixels that is added to the required width of
213 * a column to display the content.
214 */
215 static qreal columnPadding(const KItemListStyleOption& option);
216
217 private:
218 bool m_isCut;
219 bool m_isHidden;
220 QFont m_customizedFont;
221 QFontMetrics m_customizedFontMetrics;
222 bool m_isExpandable;
223 bool m_supportsItemExpanding;
224
225 bool m_dirtyLayout;
226 bool m_dirtyContent;
227 QSet<QByteArray> m_dirtyContentRoles;
228
229 Layout m_layout;
230 QPointF m_pixmapPos;
231 QPixmap m_pixmap;
232 QSize m_scaledPixmapSize;
233
234 QRectF m_iconRect; // Cache for KItemListWidget::iconRect()
235 QPixmap m_hoverPixmap; // Cache for modified m_pixmap when hovering the item
236
237 struct TextInfo
238 {
239 QPointF pos;
240 QStaticText staticText;
241 };
242 QHash<QByteArray, TextInfo*> m_textInfo;
243
244 QRectF m_textRect;
245
246 QList<QByteArray> m_sortedVisibleRoles;
247
248 QRectF m_expansionArea;
249
250 QColor m_customTextColor;
251 QColor m_additionalInfoTextColor;
252
253 QPixmap m_overlay;
254 QPixmap m_rating;
255
256 KItemListRoleEditor* m_roleEditor;
257 KItemListRoleEditor* m_oldRoleEditor;
258
259 friend class KStandardItemListWidgetInformant; // Accesses private static methods to be able to
260 // share a common layout calculation
261 };
262
263 #endif
264
265