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