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