Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qitemdelegate.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qitemdelegate.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #ifndef QT_NO_ITEMVIEWS | - | ||||||||||||||||||
37 | #include <qabstractitemmodel.h> | - | ||||||||||||||||||
38 | #include <qapplication.h> | - | ||||||||||||||||||
39 | #include <qbrush.h> | - | ||||||||||||||||||
40 | #include <qpainter.h> | - | ||||||||||||||||||
41 | #include <qpalette.h> | - | ||||||||||||||||||
42 | #include <qpoint.h> | - | ||||||||||||||||||
43 | #include <qrect.h> | - | ||||||||||||||||||
44 | #include <qsize.h> | - | ||||||||||||||||||
45 | #include <qstyle.h> | - | ||||||||||||||||||
46 | #include <qdatetime.h> | - | ||||||||||||||||||
47 | #include <qstyleoption.h> | - | ||||||||||||||||||
48 | #include <qevent.h> | - | ||||||||||||||||||
49 | #include <qpixmap.h> | - | ||||||||||||||||||
50 | #include <qbitmap.h> | - | ||||||||||||||||||
51 | #include <qpixmapcache.h> | - | ||||||||||||||||||
52 | #include <qitemeditorfactory.h> | - | ||||||||||||||||||
53 | #include <qmetaobject.h> | - | ||||||||||||||||||
54 | #include <qtextlayout.h> | - | ||||||||||||||||||
55 | #include <private/qabstractitemdelegate_p.h> | - | ||||||||||||||||||
56 | #include <private/qtextengine_p.h> | - | ||||||||||||||||||
57 | #include <qdebug.h> | - | ||||||||||||||||||
58 | #include <qlocale.h> | - | ||||||||||||||||||
59 | #include <qdialog.h> | - | ||||||||||||||||||
60 | #include <qmath.h> | - | ||||||||||||||||||
61 | - | |||||||||||||||||||
62 | #include <limits.h> | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | // keep in sync with QAbstractItemDelegate::helpEvent() | - | ||||||||||||||||||
65 | #ifndef DBL_DIG | - | ||||||||||||||||||
66 | # define DBL_DIG 10 | - | ||||||||||||||||||
67 | #endif | - | ||||||||||||||||||
68 | - | |||||||||||||||||||
69 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
70 | - | |||||||||||||||||||
71 | class QItemDelegatePrivate : public QAbstractItemDelegatePrivate | - | ||||||||||||||||||
72 | { | - | ||||||||||||||||||
73 | Q_DECLARE_PUBLIC(QItemDelegate) | - | ||||||||||||||||||
74 | - | |||||||||||||||||||
75 | public: | - | ||||||||||||||||||
76 | QItemDelegatePrivate() : f(0), clipPainting(true) {} never executed: end of block | 0 | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | inline const QItemEditorFactory *editorFactory() const | - | ||||||||||||||||||
79 | { return f ? f : QItemEditorFactory::defaultFactory(); } never executed: return f ? f : QItemEditorFactory::defaultFactory();
| 0 | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | inline QIcon::Mode iconMode(QStyle::State state) const | - | ||||||||||||||||||
82 | { | - | ||||||||||||||||||
83 | if (!(state & QStyle::State_Enabled)) return QIcon::Disabled; never executed: return QIcon::Disabled;
| 0 | ||||||||||||||||||
84 | if (state & QStyle::State_Selected) return QIcon::Selected; never executed: return QIcon::Selected;
| 0 | ||||||||||||||||||
85 | return QIcon::Normal; never executed: return QIcon::Normal; | 0 | ||||||||||||||||||
86 | } | - | ||||||||||||||||||
87 | - | |||||||||||||||||||
88 | inline QIcon::State iconState(QStyle::State state) const | - | ||||||||||||||||||
89 | { return state & QStyle::State_Open ? QIcon::On : QIcon::Off; } never executed: return state & QStyle::State_Open ? QIcon::On : QIcon::Off;
| 0 | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | inline static QString replaceNewLine(QString text) | - | ||||||||||||||||||
92 | { | - | ||||||||||||||||||
93 | const QChar nl = QLatin1Char('\n'); | - | ||||||||||||||||||
94 | for (int i = 0; i < text.count(); ++i)
| 0 | ||||||||||||||||||
95 | if (text.at(i) == nl)
| 0 | ||||||||||||||||||
96 | text[i] = QChar::LineSeparator; never executed: text[i] = QChar::LineSeparator; | 0 | ||||||||||||||||||
97 | return text; never executed: return text; | 0 | ||||||||||||||||||
98 | } | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | QString valueToText(const QVariant &value, const QStyleOptionViewItem &option) const; | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | QItemEditorFactory *f; | - | ||||||||||||||||||
103 | bool clipPainting; | - | ||||||||||||||||||
104 | - | |||||||||||||||||||
105 | QRect textLayoutBounds(const QStyleOptionViewItem &options) const; | - | ||||||||||||||||||
106 | QSizeF doTextLayout(int lineWidth) const; | - | ||||||||||||||||||
107 | mutable QTextLayout textLayout; | - | ||||||||||||||||||
108 | mutable QTextOption textOption; | - | ||||||||||||||||||
109 | - | |||||||||||||||||||
110 | const QWidget *widget(const QStyleOptionViewItem &option) const | - | ||||||||||||||||||
111 | { | - | ||||||||||||||||||
112 | return option.widget; never executed: return option.widget; | 0 | ||||||||||||||||||
113 | } | - | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | // ### temporary hack until we have QStandardItemDelegate | - | ||||||||||||||||||
116 | mutable struct Icon { | - | ||||||||||||||||||
117 | QIcon icon; | - | ||||||||||||||||||
118 | QIcon::Mode mode; | - | ||||||||||||||||||
119 | QIcon::State state; | - | ||||||||||||||||||
120 | } tmp; | - | ||||||||||||||||||
121 | }; | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | QRect QItemDelegatePrivate::textLayoutBounds(const QStyleOptionViewItem &option) const | - | ||||||||||||||||||
124 | { | - | ||||||||||||||||||
125 | QRect rect = option.rect; | - | ||||||||||||||||||
126 | const bool wrapText = option.features & QStyleOptionViewItem::WrapText; | - | ||||||||||||||||||
127 | switch (option.decorationPosition) { | - | ||||||||||||||||||
128 | case QStyleOptionViewItem::Left: never executed: case QStyleOptionViewItem::Left: | 0 | ||||||||||||||||||
129 | case QStyleOptionViewItem::Right: never executed: case QStyleOptionViewItem::Right: | 0 | ||||||||||||||||||
130 | rect.setWidth(wrapText && rect.isValid() ? rect.width() : (QFIXED_MAX)); | - | ||||||||||||||||||
131 | break; never executed: break; | 0 | ||||||||||||||||||
132 | case QStyleOptionViewItem::Top: never executed: case QStyleOptionViewItem::Top: | 0 | ||||||||||||||||||
133 | case QStyleOptionViewItem::Bottom: never executed: case QStyleOptionViewItem::Bottom: | 0 | ||||||||||||||||||
134 | rect.setWidth(wrapText ? option.decorationSize.width() : (QFIXED_MAX)); | - | ||||||||||||||||||
135 | break; never executed: break; | 0 | ||||||||||||||||||
136 | } | - | ||||||||||||||||||
137 | - | |||||||||||||||||||
138 | return rect; never executed: return rect; | 0 | ||||||||||||||||||
139 | } | - | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const | - | ||||||||||||||||||
142 | { | - | ||||||||||||||||||
143 | qreal height = 0; | - | ||||||||||||||||||
144 | qreal widthUsed = 0; | - | ||||||||||||||||||
145 | textLayout.beginLayout(); | - | ||||||||||||||||||
146 | while (true) { | - | ||||||||||||||||||
147 | QTextLine line = textLayout.createLine(); | - | ||||||||||||||||||
148 | if (!line.isValid())
| 0 | ||||||||||||||||||
149 | break; never executed: break; | 0 | ||||||||||||||||||
150 | line.setLineWidth(lineWidth); | - | ||||||||||||||||||
151 | line.setPosition(QPointF(0, height)); | - | ||||||||||||||||||
152 | height += line.height(); | - | ||||||||||||||||||
153 | widthUsed = qMax(widthUsed, line.naturalTextWidth()); | - | ||||||||||||||||||
154 | } never executed: end of block | 0 | ||||||||||||||||||
155 | textLayout.endLayout(); | - | ||||||||||||||||||
156 | return QSizeF(widthUsed, height); never executed: return QSizeF(widthUsed, height); | 0 | ||||||||||||||||||
157 | } | - | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | /*! | - | ||||||||||||||||||
160 | \class QItemDelegate | - | ||||||||||||||||||
161 | - | |||||||||||||||||||
162 | \brief The QItemDelegate class provides display and editing facilities for | - | ||||||||||||||||||
163 | data items from a model. | - | ||||||||||||||||||
164 | - | |||||||||||||||||||
165 | \ingroup model-view | - | ||||||||||||||||||
166 | \inmodule QtWidgets | - | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | QItemDelegate can be used to provide custom display features and editor | - | ||||||||||||||||||
169 | widgets for item views based on QAbstractItemView subclasses. Using a | - | ||||||||||||||||||
170 | delegate for this purpose allows the display and editing mechanisms to be | - | ||||||||||||||||||
171 | customized and developed independently from the model and view. | - | ||||||||||||||||||
172 | - | |||||||||||||||||||
173 | The QItemDelegate class is one of the \l{Model/View Classes} and | - | ||||||||||||||||||
174 | is part of Qt's \l{Model/View Programming}{model/view framework}. | - | ||||||||||||||||||
175 | Note that QStyledItemDelegate has taken over the job of drawing | - | ||||||||||||||||||
176 | Qt's item views. We recommend the use of QStyledItemDelegate when | - | ||||||||||||||||||
177 | creating new delegates. | - | ||||||||||||||||||
178 | - | |||||||||||||||||||
179 | When displaying items from a custom model in a standard view, it is | - | ||||||||||||||||||
180 | often sufficient to simply ensure that the model returns appropriate | - | ||||||||||||||||||
181 | data for each of the \l{Qt::ItemDataRole}{roles} that determine the | - | ||||||||||||||||||
182 | appearance of items in views. The default delegate used by Qt's | - | ||||||||||||||||||
183 | standard views uses this role information to display items in most | - | ||||||||||||||||||
184 | of the common forms expected by users. However, it is sometimes | - | ||||||||||||||||||
185 | necessary to have even more control over the appearance of items than | - | ||||||||||||||||||
186 | the default delegate can provide. | - | ||||||||||||||||||
187 | - | |||||||||||||||||||
188 | This class provides default implementations of the functions for | - | ||||||||||||||||||
189 | painting item data in a view and editing data from item models. | - | ||||||||||||||||||
190 | Default implementations of the paint() and sizeHint() virtual | - | ||||||||||||||||||
191 | functions, defined in QAbstractItemDelegate, are provided to | - | ||||||||||||||||||
192 | ensure that the delegate implements the correct basic behavior | - | ||||||||||||||||||
193 | expected by views. You can reimplement these functions in | - | ||||||||||||||||||
194 | subclasses to customize the appearance of items. | - | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | When editing data in an item view, QItemDelegate provides an | - | ||||||||||||||||||
197 | editor widget, which is a widget that is placed on top of the view | - | ||||||||||||||||||
198 | while editing takes place. Editors are created with a | - | ||||||||||||||||||
199 | QItemEditorFactory; a default static instance provided by | - | ||||||||||||||||||
200 | QItemEditorFactory is installed on all item delegates. You can set | - | ||||||||||||||||||
201 | a custom factory using setItemEditorFactory() or set a new default | - | ||||||||||||||||||
202 | factory with QItemEditorFactory::setDefaultFactory(). It is the | - | ||||||||||||||||||
203 | data stored in the item model with the Qt::EditRole that is edited. | - | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | Only the standard editing functions for widget-based delegates are | - | ||||||||||||||||||
206 | reimplemented here: | - | ||||||||||||||||||
207 | - | |||||||||||||||||||
208 | \list | - | ||||||||||||||||||
209 | \li createEditor() returns the widget used to change data from the model | - | ||||||||||||||||||
210 | and can be reimplemented to customize editing behavior. | - | ||||||||||||||||||
211 | \li setEditorData() provides the widget with data to manipulate. | - | ||||||||||||||||||
212 | \li updateEditorGeometry() ensures that the editor is displayed correctly | - | ||||||||||||||||||
213 | with respect to the item view. | - | ||||||||||||||||||
214 | \li setModelData() returns updated data to the model. | - | ||||||||||||||||||
215 | \endlist | - | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | The closeEditor() signal indicates that the user has completed editing the data, | - | ||||||||||||||||||
218 | and that the editor widget can be destroyed. | - | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | \section1 Standard Roles and Data Types | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | The default delegate used by the standard views supplied with Qt | - | ||||||||||||||||||
223 | associates each standard role (defined by Qt::ItemDataRole) with certain | - | ||||||||||||||||||
224 | data types. Models that return data in these types can influence the | - | ||||||||||||||||||
225 | appearance of the delegate as described in the following table. | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | \table | - | ||||||||||||||||||
228 | \header \li Role \li Accepted Types | - | ||||||||||||||||||
229 | \omit | - | ||||||||||||||||||
230 | \row \li \l Qt::AccessibleDescriptionRole \li QString | - | ||||||||||||||||||
231 | \row \li \l Qt::AccessibleTextRole \li QString | - | ||||||||||||||||||
232 | \endomit | - | ||||||||||||||||||
233 | \row \li \l Qt::BackgroundRole \li QBrush | - | ||||||||||||||||||
234 | \row \li \l Qt::BackgroundColorRole \li QColor (obsolete; use Qt::BackgroundRole instead) | - | ||||||||||||||||||
235 | \row \li \l Qt::CheckStateRole \li Qt::CheckState | - | ||||||||||||||||||
236 | \row \li \l Qt::DecorationRole \li QIcon, QPixmap and QColor | - | ||||||||||||||||||
237 | \row \li \l Qt::DisplayRole \li QString and types with a string representation | - | ||||||||||||||||||
238 | \row \li \l Qt::EditRole \li See QItemEditorFactory for details | - | ||||||||||||||||||
239 | \row \li \l Qt::FontRole \li QFont | - | ||||||||||||||||||
240 | \row \li \l Qt::SizeHintRole \li QSize | - | ||||||||||||||||||
241 | \omit | - | ||||||||||||||||||
242 | \row \li \l Qt::StatusTipRole \li | - | ||||||||||||||||||
243 | \endomit | - | ||||||||||||||||||
244 | \row \li \l Qt::TextAlignmentRole \li Qt::Alignment | - | ||||||||||||||||||
245 | \row \li \l Qt::ForegroundRole \li QBrush | - | ||||||||||||||||||
246 | \row \li \l Qt::TextColorRole \li QColor (obsolete; use Qt::ForegroundRole instead) | - | ||||||||||||||||||
247 | \omit | - | ||||||||||||||||||
248 | \row \li \l Qt::ToolTipRole | - | ||||||||||||||||||
249 | \row \li \l Qt::WhatsThisRole | - | ||||||||||||||||||
250 | \endomit | - | ||||||||||||||||||
251 | \endtable | - | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | If the default delegate does not allow the level of customization that | - | ||||||||||||||||||
254 | you need, either for display purposes or for editing data, it is possible to | - | ||||||||||||||||||
255 | subclass QItemDelegate to implement the desired behavior. | - | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | \section1 Subclassing | - | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | When subclassing QItemDelegate to create a delegate that displays items | - | ||||||||||||||||||
260 | using a custom renderer, it is important to ensure that the delegate can | - | ||||||||||||||||||
261 | render items suitably for all the required states; e.g. selected, | - | ||||||||||||||||||
262 | disabled, checked. The documentation for the paint() function contains | - | ||||||||||||||||||
263 | some hints to show how this can be achieved. | - | ||||||||||||||||||
264 | - | |||||||||||||||||||
265 | You can provide custom editors by using a QItemEditorFactory. The | - | ||||||||||||||||||
266 | \l{Color Editor Factory Example} shows how a custom editor can be | - | ||||||||||||||||||
267 | made available to delegates with the default item editor | - | ||||||||||||||||||
268 | factory. This way, there is no need to subclass QItemDelegate. An | - | ||||||||||||||||||
269 | alternative is to reimplement createEditor(), setEditorData(), | - | ||||||||||||||||||
270 | setModelData(), and updateEditorGeometry(). This process is | - | ||||||||||||||||||
271 | described in the \l{Spin Box Delegate Example}. | - | ||||||||||||||||||
272 | - | |||||||||||||||||||
273 | \section1 QStyledItemDelegate vs. QItemDelegate | - | ||||||||||||||||||
274 | - | |||||||||||||||||||
275 | Since Qt 4.4, there are two delegate classes: QItemDelegate and | - | ||||||||||||||||||
276 | QStyledItemDelegate. However, the default delegate is QStyledItemDelegate. | - | ||||||||||||||||||
277 | These two classes are independent alternatives to painting and providing | - | ||||||||||||||||||
278 | editors for items in views. The difference between them is that | - | ||||||||||||||||||
279 | QStyledItemDelegate uses the current style to paint its items. We therefore | - | ||||||||||||||||||
280 | recommend using QStyledItemDelegate as the base class when implementing | - | ||||||||||||||||||
281 | custom delegates or when working with Qt style sheets. The code required | - | ||||||||||||||||||
282 | for either class should be equal unless the custom delegate needs to use | - | ||||||||||||||||||
283 | the style for drawing. | - | ||||||||||||||||||
284 | - | |||||||||||||||||||
285 | \sa {Delegate Classes}, QStyledItemDelegate, QAbstractItemDelegate, | - | ||||||||||||||||||
286 | {Spin Box Delegate Example}, {Settings Editor Example}, | - | ||||||||||||||||||
287 | {Icons Example} | - | ||||||||||||||||||
288 | */ | - | ||||||||||||||||||
289 | - | |||||||||||||||||||
290 | /*! | - | ||||||||||||||||||
291 | Constructs an item delegate with the given \a parent. | - | ||||||||||||||||||
292 | */ | - | ||||||||||||||||||
293 | - | |||||||||||||||||||
294 | QItemDelegate::QItemDelegate(QObject *parent) | - | ||||||||||||||||||
295 | : QAbstractItemDelegate(*new QItemDelegatePrivate(), parent) | - | ||||||||||||||||||
296 | { | - | ||||||||||||||||||
297 | - | |||||||||||||||||||
298 | } never executed: end of block | 0 | ||||||||||||||||||
299 | - | |||||||||||||||||||
300 | /*! | - | ||||||||||||||||||
301 | Destroys the item delegate. | - | ||||||||||||||||||
302 | */ | - | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | QItemDelegate::~QItemDelegate() | - | ||||||||||||||||||
305 | { | - | ||||||||||||||||||
306 | } | - | ||||||||||||||||||
307 | - | |||||||||||||||||||
308 | /*! | - | ||||||||||||||||||
309 | \property QItemDelegate::clipping | - | ||||||||||||||||||
310 | \brief if the delegate should clip the paint events | - | ||||||||||||||||||
311 | \since 4.2 | - | ||||||||||||||||||
312 | - | |||||||||||||||||||
313 | This property will set the paint clip to the size of the item. | - | ||||||||||||||||||
314 | The default value is on. It is useful for cases such | - | ||||||||||||||||||
315 | as when images are larger than the size of the item. | - | ||||||||||||||||||
316 | */ | - | ||||||||||||||||||
317 | - | |||||||||||||||||||
318 | bool QItemDelegate::hasClipping() const | - | ||||||||||||||||||
319 | { | - | ||||||||||||||||||
320 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
321 | return d->clipPainting; never executed: return d->clipPainting; | 0 | ||||||||||||||||||
322 | } | - | ||||||||||||||||||
323 | - | |||||||||||||||||||
324 | void QItemDelegate::setClipping(bool clip) | - | ||||||||||||||||||
325 | { | - | ||||||||||||||||||
326 | Q_D(QItemDelegate); | - | ||||||||||||||||||
327 | d->clipPainting = clip; | - | ||||||||||||||||||
328 | } never executed: end of block | 0 | ||||||||||||||||||
329 | - | |||||||||||||||||||
330 | QString QItemDelegatePrivate::valueToText(const QVariant &value, const QStyleOptionViewItem &option) const | - | ||||||||||||||||||
331 | { | - | ||||||||||||||||||
332 | return textForRole(Qt::DisplayRole, value, option.locale, DBL_DIG); never executed: return textForRole(Qt::DisplayRole, value, option.locale, 10); | 0 | ||||||||||||||||||
333 | } | - | ||||||||||||||||||
334 | - | |||||||||||||||||||
335 | /*! | - | ||||||||||||||||||
336 | Renders the delegate using the given \a painter and style \a option for | - | ||||||||||||||||||
337 | the item specified by \a index. | - | ||||||||||||||||||
338 | - | |||||||||||||||||||
339 | When reimplementing this function in a subclass, you should update the area | - | ||||||||||||||||||
340 | held by the option's \l{QStyleOption::rect}{rect} variable, using the | - | ||||||||||||||||||
341 | option's \l{QStyleOption::state}{state} variable to determine the state of | - | ||||||||||||||||||
342 | the item to be displayed, and adjust the way it is painted accordingly. | - | ||||||||||||||||||
343 | - | |||||||||||||||||||
344 | For example, a selected item may need to be displayed differently to | - | ||||||||||||||||||
345 | unselected items, as shown in the following code: | - | ||||||||||||||||||
346 | - | |||||||||||||||||||
347 | \snippet itemviews/pixelator/pixeldelegate.cpp 2 | - | ||||||||||||||||||
348 | \dots | - | ||||||||||||||||||
349 | - | |||||||||||||||||||
350 | After painting, you should ensure that the painter is returned to its | - | ||||||||||||||||||
351 | the state it was supplied in when this function was called. For example, | - | ||||||||||||||||||
352 | it may be useful to call QPainter::save() before painting and | - | ||||||||||||||||||
353 | QPainter::restore() afterwards. | - | ||||||||||||||||||
354 | - | |||||||||||||||||||
355 | \sa QStyle::State | - | ||||||||||||||||||
356 | */ | - | ||||||||||||||||||
357 | void QItemDelegate::paint(QPainter *painter, | - | ||||||||||||||||||
358 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
359 | const QModelIndex &index) const | - | ||||||||||||||||||
360 | { | - | ||||||||||||||||||
361 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
362 | Q_ASSERT(index.isValid()); | - | ||||||||||||||||||
363 | - | |||||||||||||||||||
364 | QStyleOptionViewItem opt = setOptions(index, option); | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | // prepare | - | ||||||||||||||||||
367 | painter->save(); | - | ||||||||||||||||||
368 | if (d->clipPainting)
| 0 | ||||||||||||||||||
369 | painter->setClipRect(opt.rect); never executed: painter->setClipRect(opt.rect); | 0 | ||||||||||||||||||
370 | - | |||||||||||||||||||
371 | // get the data and the rectangles | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | QVariant value; | - | ||||||||||||||||||
374 | - | |||||||||||||||||||
375 | QPixmap pixmap; | - | ||||||||||||||||||
376 | QRect decorationRect; | - | ||||||||||||||||||
377 | value = index.data(Qt::DecorationRole); | - | ||||||||||||||||||
378 | if (value.isValid()) {
| 0 | ||||||||||||||||||
379 | // ### we need the pixmap to call the virtual function | - | ||||||||||||||||||
380 | pixmap = decoration(opt, value); | - | ||||||||||||||||||
381 | if (value.type() == QVariant::Icon) {
| 0 | ||||||||||||||||||
382 | d->tmp.icon = qvariant_cast<QIcon>(value); | - | ||||||||||||||||||
383 | d->tmp.mode = d->iconMode(option.state); | - | ||||||||||||||||||
384 | d->tmp.state = d->iconState(option.state); | - | ||||||||||||||||||
385 | const QSize size = d->tmp.icon.actualSize(option.decorationSize, | - | ||||||||||||||||||
386 | d->tmp.mode, d->tmp.state); | - | ||||||||||||||||||
387 | decorationRect = QRect(QPoint(0, 0), size); | - | ||||||||||||||||||
388 | } else { never executed: end of block | 0 | ||||||||||||||||||
389 | d->tmp.icon = QIcon(); | - | ||||||||||||||||||
390 | decorationRect = QRect(QPoint(0, 0), pixmap.size()); | - | ||||||||||||||||||
391 | } never executed: end of block | 0 | ||||||||||||||||||
392 | } else { | - | ||||||||||||||||||
393 | d->tmp.icon = QIcon(); | - | ||||||||||||||||||
394 | decorationRect = QRect(); | - | ||||||||||||||||||
395 | } never executed: end of block | 0 | ||||||||||||||||||
396 | - | |||||||||||||||||||
397 | QString text; | - | ||||||||||||||||||
398 | QRect displayRect; | - | ||||||||||||||||||
399 | value = index.data(Qt::DisplayRole); | - | ||||||||||||||||||
400 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||||||||
401 | text = d->valueToText(value, opt); | - | ||||||||||||||||||
402 | displayRect = textRectangle(painter, d->textLayoutBounds(opt), opt.font, text); | - | ||||||||||||||||||
403 | } never executed: end of block | 0 | ||||||||||||||||||
404 | - | |||||||||||||||||||
405 | QRect checkRect; | - | ||||||||||||||||||
406 | Qt::CheckState checkState = Qt::Unchecked; | - | ||||||||||||||||||
407 | value = index.data(Qt::CheckStateRole); | - | ||||||||||||||||||
408 | if (value.isValid()) {
| 0 | ||||||||||||||||||
409 | checkState = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||||||||
410 | checkRect = doCheck(opt, opt.rect, value); | - | ||||||||||||||||||
411 | } never executed: end of block | 0 | ||||||||||||||||||
412 | - | |||||||||||||||||||
413 | // do the layout | - | ||||||||||||||||||
414 | - | |||||||||||||||||||
415 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); | - | ||||||||||||||||||
416 | - | |||||||||||||||||||
417 | // draw the item | - | ||||||||||||||||||
418 | - | |||||||||||||||||||
419 | drawBackground(painter, opt, index); | - | ||||||||||||||||||
420 | drawCheck(painter, opt, checkRect, checkState); | - | ||||||||||||||||||
421 | drawDecoration(painter, opt, decorationRect, pixmap); | - | ||||||||||||||||||
422 | drawDisplay(painter, opt, displayRect, text); | - | ||||||||||||||||||
423 | drawFocus(painter, opt, displayRect); | - | ||||||||||||||||||
424 | - | |||||||||||||||||||
425 | // done | - | ||||||||||||||||||
426 | painter->restore(); | - | ||||||||||||||||||
427 | } never executed: end of block | 0 | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | /*! | - | ||||||||||||||||||
430 | Returns the size needed by the delegate to display the item | - | ||||||||||||||||||
431 | specified by \a index, taking into account the style information | - | ||||||||||||||||||
432 | provided by \a option. | - | ||||||||||||||||||
433 | - | |||||||||||||||||||
434 | When reimplementing this function, note that in case of text | - | ||||||||||||||||||
435 | items, QItemDelegate adds a margin (i.e. 2 * | - | ||||||||||||||||||
436 | QStyle::PM_FocusFrameHMargin) to the length of the text. | - | ||||||||||||||||||
437 | */ | - | ||||||||||||||||||
438 | - | |||||||||||||||||||
439 | QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
440 | const QModelIndex &index) const | - | ||||||||||||||||||
441 | { | - | ||||||||||||||||||
442 | QVariant value = index.data(Qt::SizeHintRole); | - | ||||||||||||||||||
443 | if (value.isValid())
| 0 | ||||||||||||||||||
444 | return qvariant_cast<QSize>(value); never executed: return qvariant_cast<QSize>(value); | 0 | ||||||||||||||||||
445 | QRect decorationRect = rect(option, index, Qt::DecorationRole); | - | ||||||||||||||||||
446 | QRect displayRect = rect(option, index, Qt::DisplayRole); | - | ||||||||||||||||||
447 | QRect checkRect = rect(option, index, Qt::CheckStateRole); | - | ||||||||||||||||||
448 | - | |||||||||||||||||||
449 | doLayout(option, &checkRect, &decorationRect, &displayRect, true); | - | ||||||||||||||||||
450 | - | |||||||||||||||||||
451 | return (decorationRect|displayRect|checkRect).size(); never executed: return (decorationRect|displayRect|checkRect).size(); | 0 | ||||||||||||||||||
452 | } | - | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | /*! | - | ||||||||||||||||||
455 | Returns the widget used to edit the item specified by \a index | - | ||||||||||||||||||
456 | for editing. The \a parent widget and style \a option are used to | - | ||||||||||||||||||
457 | control how the editor widget appears. | - | ||||||||||||||||||
458 | - | |||||||||||||||||||
459 | \sa QAbstractItemDelegate::createEditor() | - | ||||||||||||||||||
460 | */ | - | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | QWidget *QItemDelegate::createEditor(QWidget *parent, | - | ||||||||||||||||||
463 | const QStyleOptionViewItem &, | - | ||||||||||||||||||
464 | const QModelIndex &index) const | - | ||||||||||||||||||
465 | { | - | ||||||||||||||||||
466 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
467 | if (!index.isValid())
| 0 | ||||||||||||||||||
468 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
469 | const QItemEditorFactory *factory = d->f; | - | ||||||||||||||||||
470 | if (factory == 0)
| 0 | ||||||||||||||||||
471 | factory = QItemEditorFactory::defaultFactory(); never executed: factory = QItemEditorFactory::defaultFactory(); | 0 | ||||||||||||||||||
472 | QWidget *w = factory->createEditor(index.data(Qt::EditRole).userType(), parent); | - | ||||||||||||||||||
473 | if (w)
| 0 | ||||||||||||||||||
474 | w->setFocusPolicy(Qt::WheelFocus); never executed: w->setFocusPolicy(Qt::WheelFocus); | 0 | ||||||||||||||||||
475 | return w; never executed: return w; | 0 | ||||||||||||||||||
476 | } | - | ||||||||||||||||||
477 | - | |||||||||||||||||||
478 | /*! | - | ||||||||||||||||||
479 | Sets the data to be displayed and edited by the \a editor from the | - | ||||||||||||||||||
480 | data model item specified by the model \a index. | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | The default implementation stores the data in the \a editor | - | ||||||||||||||||||
483 | widget's \l {Qt's Property System} {user property}. | - | ||||||||||||||||||
484 | - | |||||||||||||||||||
485 | \sa QMetaProperty::isUser() | - | ||||||||||||||||||
486 | */ | - | ||||||||||||||||||
487 | - | |||||||||||||||||||
488 | void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const | - | ||||||||||||||||||
489 | { | - | ||||||||||||||||||
490 | #ifdef QT_NO_PROPERTIES | - | ||||||||||||||||||
491 | Q_UNUSED(editor); | - | ||||||||||||||||||
492 | Q_UNUSED(index); | - | ||||||||||||||||||
493 | #else | - | ||||||||||||||||||
494 | QVariant v = index.data(Qt::EditRole); | - | ||||||||||||||||||
495 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||||||||
496 | - | |||||||||||||||||||
497 | if (!n.isEmpty()) {
| 0 | ||||||||||||||||||
498 | if (!v.isValid())
| 0 | ||||||||||||||||||
499 | v = QVariant(editor->property(n).userType(), (const void *)0); never executed: v = QVariant(editor->property(n).userType(), (const void *)0); | 0 | ||||||||||||||||||
500 | editor->setProperty(n, v); | - | ||||||||||||||||||
501 | } never executed: end of block | 0 | ||||||||||||||||||
502 | #endif | - | ||||||||||||||||||
503 | } never executed: end of block | 0 | ||||||||||||||||||
504 | - | |||||||||||||||||||
505 | /*! | - | ||||||||||||||||||
506 | Gets data from the \a editor widget and stores it in the specified | - | ||||||||||||||||||
507 | \a model at the item \a index. | - | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | The default implementation gets the value to be stored in the data | - | ||||||||||||||||||
510 | model from the \a editor widget's \l {Qt's Property System} {user | - | ||||||||||||||||||
511 | property}. | - | ||||||||||||||||||
512 | - | |||||||||||||||||||
513 | \sa QMetaProperty::isUser() | - | ||||||||||||||||||
514 | */ | - | ||||||||||||||||||
515 | - | |||||||||||||||||||
516 | void QItemDelegate::setModelData(QWidget *editor, | - | ||||||||||||||||||
517 | QAbstractItemModel *model, | - | ||||||||||||||||||
518 | const QModelIndex &index) const | - | ||||||||||||||||||
519 | { | - | ||||||||||||||||||
520 | #ifdef QT_NO_PROPERTIES | - | ||||||||||||||||||
521 | Q_UNUSED(model); | - | ||||||||||||||||||
522 | Q_UNUSED(editor); | - | ||||||||||||||||||
523 | Q_UNUSED(index); | - | ||||||||||||||||||
524 | #else | - | ||||||||||||||||||
525 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
526 | Q_ASSERT(model); | - | ||||||||||||||||||
527 | Q_ASSERT(editor); | - | ||||||||||||||||||
528 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||||||||
529 | if (n.isEmpty())
| 0 | ||||||||||||||||||
530 | n = d->editorFactory()->valuePropertyName( never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||||||||
531 | model->data(index, Qt::EditRole).userType()); never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||||||||
532 | if (!n.isEmpty())
| 0 | ||||||||||||||||||
533 | model->setData(index, editor->property(n), Qt::EditRole); never executed: model->setData(index, editor->property(n), Qt::EditRole); | 0 | ||||||||||||||||||
534 | #endif | - | ||||||||||||||||||
535 | } never executed: end of block | 0 | ||||||||||||||||||
536 | - | |||||||||||||||||||
537 | /*! | - | ||||||||||||||||||
538 | Updates the \a editor for the item specified by \a index | - | ||||||||||||||||||
539 | according to the style \a option given. | - | ||||||||||||||||||
540 | */ | - | ||||||||||||||||||
541 | - | |||||||||||||||||||
542 | void QItemDelegate::updateEditorGeometry(QWidget *editor, | - | ||||||||||||||||||
543 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
544 | const QModelIndex &index) const | - | ||||||||||||||||||
545 | { | - | ||||||||||||||||||
546 | if (!editor)
| 0 | ||||||||||||||||||
547 | return; never executed: return; | 0 | ||||||||||||||||||
548 | Q_ASSERT(index.isValid()); | - | ||||||||||||||||||
549 | QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole)); | - | ||||||||||||||||||
550 | QString text = QItemDelegatePrivate::replaceNewLine(index.data(Qt::DisplayRole).toString()); | - | ||||||||||||||||||
551 | QRect pixmapRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect()); | - | ||||||||||||||||||
552 | QRect textRect = textRectangle(0, option.rect, option.font, text); | - | ||||||||||||||||||
553 | QRect checkRect = doCheck(option, textRect, index.data(Qt::CheckStateRole)); | - | ||||||||||||||||||
554 | QStyleOptionViewItem opt = option; | - | ||||||||||||||||||
555 | opt.showDecorationSelected = true; // let the editor take up all available space | - | ||||||||||||||||||
556 | doLayout(opt, &checkRect, &pixmapRect, &textRect, false); | - | ||||||||||||||||||
557 | editor->setGeometry(textRect); | - | ||||||||||||||||||
558 | } never executed: end of block | 0 | ||||||||||||||||||
559 | - | |||||||||||||||||||
560 | /*! | - | ||||||||||||||||||
561 | Returns the editor factory used by the item delegate. | - | ||||||||||||||||||
562 | If no editor factory is set, the function will return null. | - | ||||||||||||||||||
563 | - | |||||||||||||||||||
564 | \sa setItemEditorFactory() | - | ||||||||||||||||||
565 | */ | - | ||||||||||||||||||
566 | QItemEditorFactory *QItemDelegate::itemEditorFactory() const | - | ||||||||||||||||||
567 | { | - | ||||||||||||||||||
568 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
569 | return d->f; never executed: return d->f; | 0 | ||||||||||||||||||
570 | } | - | ||||||||||||||||||
571 | - | |||||||||||||||||||
572 | /*! | - | ||||||||||||||||||
573 | Sets the editor factory to be used by the item delegate to be the \a factory | - | ||||||||||||||||||
574 | specified. If no editor factory is set, the item delegate will use the | - | ||||||||||||||||||
575 | default editor factory. | - | ||||||||||||||||||
576 | - | |||||||||||||||||||
577 | \sa itemEditorFactory() | - | ||||||||||||||||||
578 | */ | - | ||||||||||||||||||
579 | void QItemDelegate::setItemEditorFactory(QItemEditorFactory *factory) | - | ||||||||||||||||||
580 | { | - | ||||||||||||||||||
581 | Q_D(QItemDelegate); | - | ||||||||||||||||||
582 | d->f = factory; | - | ||||||||||||||||||
583 | } never executed: end of block | 0 | ||||||||||||||||||
584 | - | |||||||||||||||||||
585 | /*! | - | ||||||||||||||||||
586 | Renders the item view \a text within the rectangle specified by \a rect | - | ||||||||||||||||||
587 | using the given \a painter and style \a option. | - | ||||||||||||||||||
588 | */ | - | ||||||||||||||||||
589 | - | |||||||||||||||||||
590 | void QItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, | - | ||||||||||||||||||
591 | const QRect &rect, const QString &text) const | - | ||||||||||||||||||
592 | { | - | ||||||||||||||||||
593 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
594 | - | |||||||||||||||||||
595 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
| 0 | ||||||||||||||||||
596 | ? QPalette::Normal : QPalette::Disabled; | - | ||||||||||||||||||
597 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
| 0 | ||||||||||||||||||
598 | cg = QPalette::Inactive; never executed: cg = QPalette::Inactive; | 0 | ||||||||||||||||||
599 | if (option.state & QStyle::State_Selected) {
| 0 | ||||||||||||||||||
600 | painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight)); | - | ||||||||||||||||||
601 | painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); | - | ||||||||||||||||||
602 | } else { never executed: end of block | 0 | ||||||||||||||||||
603 | painter->setPen(option.palette.color(cg, QPalette::Text)); | - | ||||||||||||||||||
604 | } never executed: end of block | 0 | ||||||||||||||||||
605 | - | |||||||||||||||||||
606 | if (text.isEmpty())
| 0 | ||||||||||||||||||
607 | return; never executed: return; | 0 | ||||||||||||||||||
608 | - | |||||||||||||||||||
609 | if (option.state & QStyle::State_Editing) {
| 0 | ||||||||||||||||||
610 | painter->save(); | - | ||||||||||||||||||
611 | painter->setPen(option.palette.color(cg, QPalette::Text)); | - | ||||||||||||||||||
612 | painter->drawRect(rect.adjusted(0, 0, -1, -1)); | - | ||||||||||||||||||
613 | painter->restore(); | - | ||||||||||||||||||
614 | } never executed: end of block | 0 | ||||||||||||||||||
615 | - | |||||||||||||||||||
616 | const QStyleOptionViewItem opt = option; | - | ||||||||||||||||||
617 | - | |||||||||||||||||||
618 | const QWidget *widget = d->widget(option); | - | ||||||||||||||||||
619 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||||||||
620 | const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1; | - | ||||||||||||||||||
621 | QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding | - | ||||||||||||||||||
622 | const bool wrapText = opt.features & QStyleOptionViewItem::WrapText; | - | ||||||||||||||||||
623 | d->textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap); | - | ||||||||||||||||||
624 | d->textOption.setTextDirection(option.direction); | - | ||||||||||||||||||
625 | d->textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment)); | - | ||||||||||||||||||
626 | d->textLayout.setTextOption(d->textOption); | - | ||||||||||||||||||
627 | d->textLayout.setFont(option.font); | - | ||||||||||||||||||
628 | d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text)); | - | ||||||||||||||||||
629 | - | |||||||||||||||||||
630 | QSizeF textLayoutSize = d->doTextLayout(textRect.width()); | - | ||||||||||||||||||
631 | - | |||||||||||||||||||
632 | if (textRect.width() < textLayoutSize.width()
| 0 | ||||||||||||||||||
633 | || textRect.height() < textLayoutSize.height()) {
| 0 | ||||||||||||||||||
634 | QString elided; | - | ||||||||||||||||||
635 | int start = 0; | - | ||||||||||||||||||
636 | int end = text.indexOf(QChar::LineSeparator, start); | - | ||||||||||||||||||
637 | if (end == -1) {
| 0 | ||||||||||||||||||
638 | elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width()); | - | ||||||||||||||||||
639 | } else { never executed: end of block | 0 | ||||||||||||||||||
640 | while (end != -1) {
| 0 | ||||||||||||||||||
641 | elided += option.fontMetrics.elidedText(text.mid(start, end - start), | - | ||||||||||||||||||
642 | option.textElideMode, textRect.width()); | - | ||||||||||||||||||
643 | elided += QChar::LineSeparator; | - | ||||||||||||||||||
644 | start = end + 1; | - | ||||||||||||||||||
645 | end = text.indexOf(QChar::LineSeparator, start); | - | ||||||||||||||||||
646 | } never executed: end of block | 0 | ||||||||||||||||||
647 | //let's add the last line (after the last QChar::LineSeparator) | - | ||||||||||||||||||
648 | elided += option.fontMetrics.elidedText(text.mid(start), | - | ||||||||||||||||||
649 | option.textElideMode, textRect.width()); | - | ||||||||||||||||||
650 | } never executed: end of block | 0 | ||||||||||||||||||
651 | d->textLayout.setText(elided); | - | ||||||||||||||||||
652 | textLayoutSize = d->doTextLayout(textRect.width()); | - | ||||||||||||||||||
653 | } never executed: end of block | 0 | ||||||||||||||||||
654 | - | |||||||||||||||||||
655 | const QSize layoutSize(textRect.width(), int(textLayoutSize.height())); | - | ||||||||||||||||||
656 | const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, | - | ||||||||||||||||||
657 | layoutSize, textRect); | - | ||||||||||||||||||
658 | // if we still overflow even after eliding the text, enable clipping | - | ||||||||||||||||||
659 | if (!hasClipping() && (textRect.width() < textLayoutSize.width()
| 0 | ||||||||||||||||||
660 | || textRect.height() < textLayoutSize.height())) {
| 0 | ||||||||||||||||||
661 | painter->save(); | - | ||||||||||||||||||
662 | painter->setClipRect(layoutRect); | - | ||||||||||||||||||
663 | d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect); | - | ||||||||||||||||||
664 | painter->restore(); | - | ||||||||||||||||||
665 | } else { never executed: end of block | 0 | ||||||||||||||||||
666 | d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect); | - | ||||||||||||||||||
667 | } never executed: end of block | 0 | ||||||||||||||||||
668 | } | - | ||||||||||||||||||
669 | - | |||||||||||||||||||
670 | /*! | - | ||||||||||||||||||
671 | Renders the decoration \a pixmap within the rectangle specified by | - | ||||||||||||||||||
672 | \a rect using the given \a painter and style \a option. | - | ||||||||||||||||||
673 | */ | - | ||||||||||||||||||
674 | void QItemDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, | - | ||||||||||||||||||
675 | const QRect &rect, const QPixmap &pixmap) const | - | ||||||||||||||||||
676 | { | - | ||||||||||||||||||
677 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
678 | // if we have an icon, we ignore the pixmap | - | ||||||||||||||||||
679 | if (!d->tmp.icon.isNull()) {
| 0 | ||||||||||||||||||
680 | d->tmp.icon.paint(painter, rect, option.decorationAlignment, | - | ||||||||||||||||||
681 | d->tmp.mode, d->tmp.state); | - | ||||||||||||||||||
682 | return; never executed: return; | 0 | ||||||||||||||||||
683 | } | - | ||||||||||||||||||
684 | - | |||||||||||||||||||
685 | if (pixmap.isNull() || !rect.isValid())
| 0 | ||||||||||||||||||
686 | return; never executed: return; | 0 | ||||||||||||||||||
687 | QPoint p = QStyle::alignedRect(option.direction, option.decorationAlignment, | - | ||||||||||||||||||
688 | pixmap.size(), rect).topLeft(); | - | ||||||||||||||||||
689 | if (option.state & QStyle::State_Selected) {
| 0 | ||||||||||||||||||
690 | QPixmap *pm = selected(pixmap, option.palette, option.state & QStyle::State_Enabled); | - | ||||||||||||||||||
691 | painter->drawPixmap(p, *pm); | - | ||||||||||||||||||
692 | } else { never executed: end of block | 0 | ||||||||||||||||||
693 | painter->drawPixmap(p, pixmap); | - | ||||||||||||||||||
694 | } never executed: end of block | 0 | ||||||||||||||||||
695 | } | - | ||||||||||||||||||
696 | - | |||||||||||||||||||
697 | /*! | - | ||||||||||||||||||
698 | Renders the region within the rectangle specified by \a rect, indicating | - | ||||||||||||||||||
699 | that it has the focus, using the given \a painter and style \a option. | - | ||||||||||||||||||
700 | */ | - | ||||||||||||||||||
701 | - | |||||||||||||||||||
702 | void QItemDelegate::drawFocus(QPainter *painter, | - | ||||||||||||||||||
703 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
704 | const QRect &rect) const | - | ||||||||||||||||||
705 | { | - | ||||||||||||||||||
706 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
707 | if ((option.state & QStyle::State_HasFocus) == 0 || !rect.isValid())
| 0 | ||||||||||||||||||
708 | return; never executed: return; | 0 | ||||||||||||||||||
709 | QStyleOptionFocusRect o; | - | ||||||||||||||||||
710 | o.QStyleOption::operator=(option); | - | ||||||||||||||||||
711 | o.rect = rect; | - | ||||||||||||||||||
712 | o.state |= QStyle::State_KeyboardFocusChange; | - | ||||||||||||||||||
713 | o.state |= QStyle::State_Item; | - | ||||||||||||||||||
714 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
| 0 | ||||||||||||||||||
715 | ? QPalette::Normal : QPalette::Disabled; | - | ||||||||||||||||||
716 | o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected) | - | ||||||||||||||||||
717 | ? QPalette::Highlight : QPalette::Window); | - | ||||||||||||||||||
718 | const QWidget *widget = d->widget(option); | - | ||||||||||||||||||
719 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||||||||
720 | style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, widget); | - | ||||||||||||||||||
721 | } never executed: end of block | 0 | ||||||||||||||||||
722 | - | |||||||||||||||||||
723 | /*! | - | ||||||||||||||||||
724 | Renders a check indicator within the rectangle specified by \a | - | ||||||||||||||||||
725 | rect, using the given \a painter and style \a option, using the | - | ||||||||||||||||||
726 | given \a state. | - | ||||||||||||||||||
727 | */ | - | ||||||||||||||||||
728 | - | |||||||||||||||||||
729 | void QItemDelegate::drawCheck(QPainter *painter, | - | ||||||||||||||||||
730 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
731 | const QRect &rect, Qt::CheckState state) const | - | ||||||||||||||||||
732 | { | - | ||||||||||||||||||
733 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
734 | if (!rect.isValid())
| 0 | ||||||||||||||||||
735 | return; never executed: return; | 0 | ||||||||||||||||||
736 | - | |||||||||||||||||||
737 | QStyleOptionViewItem opt(option); | - | ||||||||||||||||||
738 | opt.rect = rect; | - | ||||||||||||||||||
739 | opt.state = opt.state & ~QStyle::State_HasFocus; | - | ||||||||||||||||||
740 | - | |||||||||||||||||||
741 | switch (state) { | - | ||||||||||||||||||
742 | case Qt::Unchecked: never executed: case Qt::Unchecked: | 0 | ||||||||||||||||||
743 | opt.state |= QStyle::State_Off; | - | ||||||||||||||||||
744 | break; never executed: break; | 0 | ||||||||||||||||||
745 | case Qt::PartiallyChecked: never executed: case Qt::PartiallyChecked: | 0 | ||||||||||||||||||
746 | opt.state |= QStyle::State_NoChange; | - | ||||||||||||||||||
747 | break; never executed: break; | 0 | ||||||||||||||||||
748 | case Qt::Checked: never executed: case Qt::Checked: | 0 | ||||||||||||||||||
749 | opt.state |= QStyle::State_On; | - | ||||||||||||||||||
750 | break; never executed: break; | 0 | ||||||||||||||||||
751 | } | - | ||||||||||||||||||
752 | - | |||||||||||||||||||
753 | const QWidget *widget = d->widget(option); | - | ||||||||||||||||||
754 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||||||||
755 | style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget); | - | ||||||||||||||||||
756 | } never executed: end of block | 0 | ||||||||||||||||||
757 | - | |||||||||||||||||||
758 | /*! | - | ||||||||||||||||||
759 | \since 4.2 | - | ||||||||||||||||||
760 | - | |||||||||||||||||||
761 | Renders the item background for the given \a index, | - | ||||||||||||||||||
762 | using the given \a painter and style \a option. | - | ||||||||||||||||||
763 | */ | - | ||||||||||||||||||
764 | - | |||||||||||||||||||
765 | void QItemDelegate::drawBackground(QPainter *painter, | - | ||||||||||||||||||
766 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
767 | const QModelIndex &index) const | - | ||||||||||||||||||
768 | { | - | ||||||||||||||||||
769 | if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
| 0 | ||||||||||||||||||
770 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
| 0 | ||||||||||||||||||
771 | ? QPalette::Normal : QPalette::Disabled; | - | ||||||||||||||||||
772 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
| 0 | ||||||||||||||||||
773 | cg = QPalette::Inactive; never executed: cg = QPalette::Inactive; | 0 | ||||||||||||||||||
774 | - | |||||||||||||||||||
775 | painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); | - | ||||||||||||||||||
776 | } else { never executed: end of block | 0 | ||||||||||||||||||
777 | QVariant value = index.data(Qt::BackgroundRole); | - | ||||||||||||||||||
778 | if (value.canConvert<QBrush>()) {
| 0 | ||||||||||||||||||
779 | QPointF oldBO = painter->brushOrigin(); | - | ||||||||||||||||||
780 | painter->setBrushOrigin(option.rect.topLeft()); | - | ||||||||||||||||||
781 | painter->fillRect(option.rect, qvariant_cast<QBrush>(value)); | - | ||||||||||||||||||
782 | painter->setBrushOrigin(oldBO); | - | ||||||||||||||||||
783 | } never executed: end of block | 0 | ||||||||||||||||||
784 | } never executed: end of block | 0 | ||||||||||||||||||
785 | } | - | ||||||||||||||||||
786 | - | |||||||||||||||||||
787 | - | |||||||||||||||||||
788 | /*! | - | ||||||||||||||||||
789 | \internal | - | ||||||||||||||||||
790 | - | |||||||||||||||||||
791 | Code duplicated in QCommonStylePrivate::viewItemLayout | - | ||||||||||||||||||
792 | */ | - | ||||||||||||||||||
793 | - | |||||||||||||||||||
794 | void QItemDelegate::doLayout(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
795 | QRect *checkRect, QRect *pixmapRect, QRect *textRect, | - | ||||||||||||||||||
796 | bool hint) const | - | ||||||||||||||||||
797 | { | - | ||||||||||||||||||
798 | Q_ASSERT(checkRect && pixmapRect && textRect); | - | ||||||||||||||||||
799 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
800 | const QWidget *widget = d->widget(option); | - | ||||||||||||||||||
801 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||||||||
802 | const bool hasCheck = checkRect->isValid(); | - | ||||||||||||||||||
803 | const bool hasPixmap = pixmapRect->isValid(); | - | ||||||||||||||||||
804 | const bool hasText = textRect->isValid(); | - | ||||||||||||||||||
805 | const int textMargin = hasText ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
| 0 | ||||||||||||||||||
806 | const int pixmapMargin = hasPixmap ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
| 0 | ||||||||||||||||||
807 | const int checkMargin = hasCheck ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
| 0 | ||||||||||||||||||
808 | int x = option.rect.left(); | - | ||||||||||||||||||
809 | int y = option.rect.top(); | - | ||||||||||||||||||
810 | int w, h; | - | ||||||||||||||||||
811 | - | |||||||||||||||||||
812 | textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding | - | ||||||||||||||||||
813 | if (textRect->height() == 0 && (!hasPixmap || !hint)) {
| 0 | ||||||||||||||||||
814 | //if there is no text, we still want to have a decent height for the item sizeHint and the editor size | - | ||||||||||||||||||
815 | textRect->setHeight(option.fontMetrics.height()); | - | ||||||||||||||||||
816 | } never executed: end of block | 0 | ||||||||||||||||||
817 | - | |||||||||||||||||||
818 | QSize pm(0, 0); | - | ||||||||||||||||||
819 | if (hasPixmap) {
| 0 | ||||||||||||||||||
820 | pm = pixmapRect->size(); | - | ||||||||||||||||||
821 | pm.rwidth() += 2 * pixmapMargin; | - | ||||||||||||||||||
822 | } never executed: end of block | 0 | ||||||||||||||||||
823 | if (hint) {
| 0 | ||||||||||||||||||
824 | h = qMax(checkRect->height(), qMax(textRect->height(), pm.height())); | - | ||||||||||||||||||
825 | if (option.decorationPosition == QStyleOptionViewItem::Left
| 0 | ||||||||||||||||||
826 | || option.decorationPosition == QStyleOptionViewItem::Right) {
| 0 | ||||||||||||||||||
827 | w = textRect->width() + pm.width(); | - | ||||||||||||||||||
828 | } else { never executed: end of block | 0 | ||||||||||||||||||
829 | w = qMax(textRect->width(), pm.width()); | - | ||||||||||||||||||
830 | } never executed: end of block | 0 | ||||||||||||||||||
831 | } else { | - | ||||||||||||||||||
832 | w = option.rect.width(); | - | ||||||||||||||||||
833 | h = option.rect.height(); | - | ||||||||||||||||||
834 | } never executed: end of block | 0 | ||||||||||||||||||
835 | - | |||||||||||||||||||
836 | int cw = 0; | - | ||||||||||||||||||
837 | QRect check; | - | ||||||||||||||||||
838 | if (hasCheck) {
| 0 | ||||||||||||||||||
839 | cw = checkRect->width() + 2 * checkMargin; | - | ||||||||||||||||||
840 | if (hint) w += cw; never executed: w += cw;
| 0 | ||||||||||||||||||
841 | if (option.direction == Qt::RightToLeft) {
| 0 | ||||||||||||||||||
842 | check.setRect(x + w - cw, y, cw, h); | - | ||||||||||||||||||
843 | } else { never executed: end of block | 0 | ||||||||||||||||||
844 | check.setRect(x + checkMargin, y, cw, h); | - | ||||||||||||||||||
845 | } never executed: end of block | 0 | ||||||||||||||||||
846 | } | - | ||||||||||||||||||
847 | - | |||||||||||||||||||
848 | // at this point w should be the *total* width | - | ||||||||||||||||||
849 | - | |||||||||||||||||||
850 | QRect display; | - | ||||||||||||||||||
851 | QRect decoration; | - | ||||||||||||||||||
852 | switch (option.decorationPosition) { | - | ||||||||||||||||||
853 | case QStyleOptionViewItem::Top: { never executed: case QStyleOptionViewItem::Top: | 0 | ||||||||||||||||||
854 | if (hasPixmap)
| 0 | ||||||||||||||||||
855 | pm.setHeight(pm.height() + pixmapMargin); // add space never executed: pm.setHeight(pm.height() + pixmapMargin); | 0 | ||||||||||||||||||
856 | h = hint ? textRect->height() : h - pm.height();
| 0 | ||||||||||||||||||
857 | - | |||||||||||||||||||
858 | if (option.direction == Qt::RightToLeft) {
| 0 | ||||||||||||||||||
859 | decoration.setRect(x, y, w - cw, pm.height()); | - | ||||||||||||||||||
860 | display.setRect(x, y + pm.height(), w - cw, h); | - | ||||||||||||||||||
861 | } else { never executed: end of block | 0 | ||||||||||||||||||
862 | decoration.setRect(x + cw, y, w - cw, pm.height()); | - | ||||||||||||||||||
863 | display.setRect(x + cw, y + pm.height(), w - cw, h); | - | ||||||||||||||||||
864 | } never executed: end of block | 0 | ||||||||||||||||||
865 | break; } never executed: break; | 0 | ||||||||||||||||||
866 | case QStyleOptionViewItem::Bottom: { never executed: case QStyleOptionViewItem::Bottom: | 0 | ||||||||||||||||||
867 | if (hasText)
| 0 | ||||||||||||||||||
868 | textRect->setHeight(textRect->height() + textMargin); // add space never executed: textRect->setHeight(textRect->height() + textMargin); | 0 | ||||||||||||||||||
869 | h = hint ? textRect->height() + pm.height() : h;
| 0 | ||||||||||||||||||
870 | - | |||||||||||||||||||
871 | if (option.direction == Qt::RightToLeft) {
| 0 | ||||||||||||||||||
872 | display.setRect(x, y, w - cw, textRect->height()); | - | ||||||||||||||||||
873 | decoration.setRect(x, y + textRect->height(), w - cw, h - textRect->height()); | - | ||||||||||||||||||
874 | } else { never executed: end of block | 0 | ||||||||||||||||||
875 | display.setRect(x + cw, y, w - cw, textRect->height()); | - | ||||||||||||||||||
876 | decoration.setRect(x + cw, y + textRect->height(), w - cw, h - textRect->height()); | - | ||||||||||||||||||
877 | } never executed: end of block | 0 | ||||||||||||||||||
878 | break; } never executed: break; | 0 | ||||||||||||||||||
879 | case QStyleOptionViewItem::Left: { never executed: case QStyleOptionViewItem::Left: | 0 | ||||||||||||||||||
880 | if (option.direction == Qt::LeftToRight) {
| 0 | ||||||||||||||||||
881 | decoration.setRect(x + cw, y, pm.width(), h); | - | ||||||||||||||||||
882 | display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); | - | ||||||||||||||||||
883 | } else { never executed: end of block | 0 | ||||||||||||||||||
884 | display.setRect(x, y, w - pm.width() - cw, h); | - | ||||||||||||||||||
885 | decoration.setRect(display.right() + 1, y, pm.width(), h); | - | ||||||||||||||||||
886 | } never executed: end of block | 0 | ||||||||||||||||||
887 | break; } never executed: break; | 0 | ||||||||||||||||||
888 | case QStyleOptionViewItem::Right: { never executed: case QStyleOptionViewItem::Right: | 0 | ||||||||||||||||||
889 | if (option.direction == Qt::LeftToRight) {
| 0 | ||||||||||||||||||
890 | display.setRect(x + cw, y, w - pm.width() - cw, h); | - | ||||||||||||||||||
891 | decoration.setRect(display.right() + 1, y, pm.width(), h); | - | ||||||||||||||||||
892 | } else { never executed: end of block | 0 | ||||||||||||||||||
893 | decoration.setRect(x, y, pm.width(), h); | - | ||||||||||||||||||
894 | display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); | - | ||||||||||||||||||
895 | } never executed: end of block | 0 | ||||||||||||||||||
896 | break; } never executed: break; | 0 | ||||||||||||||||||
897 | default: never executed: default: | 0 | ||||||||||||||||||
898 | qWarning("doLayout: decoration position is invalid"); | - | ||||||||||||||||||
899 | decoration = *pixmapRect; | - | ||||||||||||||||||
900 | break; never executed: break; | 0 | ||||||||||||||||||
901 | } | - | ||||||||||||||||||
902 | - | |||||||||||||||||||
903 | if (!hint) { // we only need to do the internal layout if we are going to paint
| 0 | ||||||||||||||||||
904 | *checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, | - | ||||||||||||||||||
905 | checkRect->size(), check); | - | ||||||||||||||||||
906 | *pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment, | - | ||||||||||||||||||
907 | pixmapRect->size(), decoration); | - | ||||||||||||||||||
908 | // the text takes up all available space, unless the decoration is not shown as selected | - | ||||||||||||||||||
909 | if (option.showDecorationSelected)
| 0 | ||||||||||||||||||
910 | *textRect = display; never executed: *textRect = display; | 0 | ||||||||||||||||||
911 | else | - | ||||||||||||||||||
912 | *textRect = QStyle::alignedRect(option.direction, option.displayAlignment, never executed: *textRect = QStyle::alignedRect(option.direction, option.displayAlignment, textRect->size().boundedTo(display.size()), display); | 0 | ||||||||||||||||||
913 | textRect->size().boundedTo(display.size()), display); never executed: *textRect = QStyle::alignedRect(option.direction, option.displayAlignment, textRect->size().boundedTo(display.size()), display); | 0 | ||||||||||||||||||
914 | } else { | - | ||||||||||||||||||
915 | *checkRect = check; | - | ||||||||||||||||||
916 | *pixmapRect = decoration; | - | ||||||||||||||||||
917 | *textRect = display; | - | ||||||||||||||||||
918 | } never executed: end of block | 0 | ||||||||||||||||||
919 | } | - | ||||||||||||||||||
920 | - | |||||||||||||||||||
921 | /*! | - | ||||||||||||||||||
922 | \internal | - | ||||||||||||||||||
923 | - | |||||||||||||||||||
924 | Returns the pixmap used to decorate the root of the item view. | - | ||||||||||||||||||
925 | The style \a option controls the appearance of the root; the \a variant | - | ||||||||||||||||||
926 | refers to the data associated with an item. | - | ||||||||||||||||||
927 | */ | - | ||||||||||||||||||
928 | - | |||||||||||||||||||
929 | QPixmap QItemDelegate::decoration(const QStyleOptionViewItem &option, const QVariant &variant) const | - | ||||||||||||||||||
930 | { | - | ||||||||||||||||||
931 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
932 | switch (variant.type()) { | - | ||||||||||||||||||
933 | case QVariant::Icon: { never executed: case QVariant::Icon: | 0 | ||||||||||||||||||
934 | QIcon::Mode mode = d->iconMode(option.state); | - | ||||||||||||||||||
935 | QIcon::State state = d->iconState(option.state); | - | ||||||||||||||||||
936 | return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); } never executed: return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); | 0 | ||||||||||||||||||
937 | case QVariant::Color: { never executed: case QVariant::Color: | 0 | ||||||||||||||||||
938 | static QPixmap pixmap(option.decorationSize); | - | ||||||||||||||||||
939 | pixmap.fill(qvariant_cast<QColor>(variant)); | - | ||||||||||||||||||
940 | return pixmap; } never executed: return pixmap; | 0 | ||||||||||||||||||
941 | default: never executed: default: | 0 | ||||||||||||||||||
942 | break; never executed: break; | 0 | ||||||||||||||||||
943 | } | - | ||||||||||||||||||
944 | - | |||||||||||||||||||
945 | return qvariant_cast<QPixmap>(variant); never executed: return qvariant_cast<QPixmap>(variant); | 0 | ||||||||||||||||||
946 | } | - | ||||||||||||||||||
947 | - | |||||||||||||||||||
948 | // hacky but faster version of "QString::asprintf("%d-%d", i, enabled)" | - | ||||||||||||||||||
949 | static QString qPixmapSerial(quint64 i, bool enabled) | - | ||||||||||||||||||
950 | { | - | ||||||||||||||||||
951 | ushort arr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', ushort('0' + enabled) }; | - | ||||||||||||||||||
952 | ushort *ptr = &arr[16]; | - | ||||||||||||||||||
953 | - | |||||||||||||||||||
954 | while (i > 0) {
| 0 | ||||||||||||||||||
955 | // hey - it's our internal representation, so use the ascii character after '9' | - | ||||||||||||||||||
956 | // instead of 'a' for hex | - | ||||||||||||||||||
957 | *(--ptr) = '0' + i % 16; | - | ||||||||||||||||||
958 | i >>= 4; | - | ||||||||||||||||||
959 | } never executed: end of block | 0 | ||||||||||||||||||
960 | - | |||||||||||||||||||
961 | return QString((const QChar *)ptr, int(&arr[sizeof(arr) / sizeof(ushort)] - ptr)); never executed: return QString((const QChar *)ptr, int(&arr[sizeof(arr) / sizeof(ushort)] - ptr)); | 0 | ||||||||||||||||||
962 | } | - | ||||||||||||||||||
963 | - | |||||||||||||||||||
964 | /*! | - | ||||||||||||||||||
965 | \internal | - | ||||||||||||||||||
966 | Returns the selected version of the given \a pixmap using the given \a palette. | - | ||||||||||||||||||
967 | The \a enabled argument decides whether the normal or disabled highlight color of | - | ||||||||||||||||||
968 | the palette is used. | - | ||||||||||||||||||
969 | */ | - | ||||||||||||||||||
970 | QPixmap *QItemDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const | - | ||||||||||||||||||
971 | { | - | ||||||||||||||||||
972 | QString key = qPixmapSerial(pixmap.cacheKey(), enabled); | - | ||||||||||||||||||
973 | QPixmap *pm = QPixmapCache::find(key); | - | ||||||||||||||||||
974 | if (!pm) {
| 0 | ||||||||||||||||||
975 | QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied); | - | ||||||||||||||||||
976 | - | |||||||||||||||||||
977 | QColor color = palette.color(enabled ? QPalette::Normal : QPalette::Disabled, | - | ||||||||||||||||||
978 | QPalette::Highlight); | - | ||||||||||||||||||
979 | color.setAlphaF((qreal)0.3); | - | ||||||||||||||||||
980 | - | |||||||||||||||||||
981 | QPainter painter(&img); | - | ||||||||||||||||||
982 | painter.setCompositionMode(QPainter::CompositionMode_SourceAtop); | - | ||||||||||||||||||
983 | painter.fillRect(0, 0, img.width(), img.height(), color); | - | ||||||||||||||||||
984 | painter.end(); | - | ||||||||||||||||||
985 | - | |||||||||||||||||||
986 | QPixmap selected = QPixmap(QPixmap::fromImage(img)); | - | ||||||||||||||||||
987 | int n = (img.byteCount() >> 10) + 1; | - | ||||||||||||||||||
988 | if (QPixmapCache::cacheLimit() < n)
| 0 | ||||||||||||||||||
989 | QPixmapCache::setCacheLimit(n); never executed: QPixmapCache::setCacheLimit(n); | 0 | ||||||||||||||||||
990 | - | |||||||||||||||||||
991 | QPixmapCache::insert(key, selected); | - | ||||||||||||||||||
992 | pm = QPixmapCache::find(key); | - | ||||||||||||||||||
993 | } never executed: end of block | 0 | ||||||||||||||||||
994 | return pm; never executed: return pm; | 0 | ||||||||||||||||||
995 | } | - | ||||||||||||||||||
996 | - | |||||||||||||||||||
997 | /*! | - | ||||||||||||||||||
998 | \internal | - | ||||||||||||||||||
999 | */ | - | ||||||||||||||||||
1000 | - | |||||||||||||||||||
1001 | QRect QItemDelegate::rect(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
1002 | const QModelIndex &index, int role) const | - | ||||||||||||||||||
1003 | { | - | ||||||||||||||||||
1004 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
1005 | QVariant value = index.data(role); | - | ||||||||||||||||||
1006 | if (role == Qt::CheckStateRole)
| 0 | ||||||||||||||||||
1007 | return doCheck(option, option.rect, value); never executed: return doCheck(option, option.rect, value); | 0 | ||||||||||||||||||
1008 | if (value.isValid() && !value.isNull()) {
| 0 | ||||||||||||||||||
1009 | switch (value.type()) { | - | ||||||||||||||||||
1010 | case QVariant::Invalid: never executed: case QVariant::Invalid: | 0 | ||||||||||||||||||
1011 | break; never executed: break; | 0 | ||||||||||||||||||
1012 | case QVariant::Pixmap: { never executed: case QVariant::Pixmap: | 0 | ||||||||||||||||||
1013 | const QPixmap &pixmap = qvariant_cast<QPixmap>(value); | - | ||||||||||||||||||
1014 | return QRect(QPoint(0, 0), pixmap.size() / pixmap.devicePixelRatio() ); } never executed: return QRect(QPoint(0, 0), pixmap.size() / pixmap.devicePixelRatio() ); | 0 | ||||||||||||||||||
1015 | case QVariant::Image: { never executed: case QVariant::Image: | 0 | ||||||||||||||||||
1016 | const QImage &image = qvariant_cast<QImage>(value); | - | ||||||||||||||||||
1017 | return QRect(QPoint(0, 0), image.size() / image.devicePixelRatio() ); } never executed: return QRect(QPoint(0, 0), image.size() / image.devicePixelRatio() ); | 0 | ||||||||||||||||||
1018 | case QVariant::Icon: { never executed: case QVariant::Icon: | 0 | ||||||||||||||||||
1019 | QIcon::Mode mode = d->iconMode(option.state); | - | ||||||||||||||||||
1020 | QIcon::State state = d->iconState(option.state); | - | ||||||||||||||||||
1021 | QIcon icon = qvariant_cast<QIcon>(value); | - | ||||||||||||||||||
1022 | QSize size = icon.actualSize(option.decorationSize, mode, state); | - | ||||||||||||||||||
1023 | return QRect(QPoint(0, 0), size); } never executed: return QRect(QPoint(0, 0), size); | 0 | ||||||||||||||||||
1024 | case QVariant::Color: never executed: case QVariant::Color: | 0 | ||||||||||||||||||
1025 | return QRect(QPoint(0, 0), option.decorationSize); never executed: return QRect(QPoint(0, 0), option.decorationSize); | 0 | ||||||||||||||||||
1026 | case QVariant::String: never executed: case QVariant::String: | 0 | ||||||||||||||||||
1027 | default: { never executed: default: | 0 | ||||||||||||||||||
1028 | const QString text = d->valueToText(value, option); | - | ||||||||||||||||||
1029 | value = index.data(Qt::FontRole); | - | ||||||||||||||||||
1030 | QFont fnt = qvariant_cast<QFont>(value).resolve(option.font); | - | ||||||||||||||||||
1031 | return textRectangle(0, d->textLayoutBounds(option), fnt, text); } never executed: return textRectangle(0, d->textLayoutBounds(option), fnt, text); | 0 | ||||||||||||||||||
1032 | } | - | ||||||||||||||||||
1033 | } | - | ||||||||||||||||||
1034 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||||||||
1035 | } | - | ||||||||||||||||||
1036 | - | |||||||||||||||||||
1037 | /*! | - | ||||||||||||||||||
1038 | \internal | - | ||||||||||||||||||
1039 | */ | - | ||||||||||||||||||
1040 | QRect QItemDelegate::doCheck(const QStyleOptionViewItem &option, | - | ||||||||||||||||||
1041 | const QRect &bounding, const QVariant &value) const | - | ||||||||||||||||||
1042 | { | - | ||||||||||||||||||
1043 | if (value.isValid()) {
| 0 | ||||||||||||||||||
1044 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
1045 | QStyleOptionButton opt; | - | ||||||||||||||||||
1046 | opt.QStyleOption::operator=(option); | - | ||||||||||||||||||
1047 | opt.rect = bounding; | - | ||||||||||||||||||
1048 | const QWidget *widget = d->widget(option); // cast | - | ||||||||||||||||||
1049 | QStyle *style = widget ? widget->style() : QApplication::style();
| 0 | ||||||||||||||||||
1050 | return style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, widget); never executed: return style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, widget); | 0 | ||||||||||||||||||
1051 | } | - | ||||||||||||||||||
1052 | return QRect(); never executed: return QRect(); | 0 | ||||||||||||||||||
1053 | } | - | ||||||||||||||||||
1054 | - | |||||||||||||||||||
1055 | /*! | - | ||||||||||||||||||
1056 | \internal | - | ||||||||||||||||||
1057 | */ | - | ||||||||||||||||||
1058 | QRect QItemDelegate::textRectangle(QPainter * /*painter*/, const QRect &rect, | - | ||||||||||||||||||
1059 | const QFont &font, const QString &text) const | - | ||||||||||||||||||
1060 | { | - | ||||||||||||||||||
1061 | Q_D(const QItemDelegate); | - | ||||||||||||||||||
1062 | d->textOption.setWrapMode(QTextOption::WordWrap); | - | ||||||||||||||||||
1063 | d->textLayout.setTextOption(d->textOption); | - | ||||||||||||||||||
1064 | d->textLayout.setFont(font); | - | ||||||||||||||||||
1065 | d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text)); | - | ||||||||||||||||||
1066 | QSizeF fpSize = d->doTextLayout(rect.width()); | - | ||||||||||||||||||
1067 | const QSize size = QSize(qCeil(fpSize.width()), qCeil(fpSize.height())); | - | ||||||||||||||||||
1068 | // ###: textRectangle should take style option as argument | - | ||||||||||||||||||
1069 | const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; | - | ||||||||||||||||||
1070 | return QRect(0, 0, size.width() + 2 * textMargin, size.height()); never executed: return QRect(0, 0, size.width() + 2 * textMargin, size.height()); | 0 | ||||||||||||||||||
1071 | } | - | ||||||||||||||||||
1072 | - | |||||||||||||||||||
1073 | /*! | - | ||||||||||||||||||
1074 | \fn bool QItemDelegate::eventFilter(QObject *editor, QEvent *event) | - | ||||||||||||||||||
1075 | - | |||||||||||||||||||
1076 | Returns \c true if the given \a editor is a valid QWidget and the | - | ||||||||||||||||||
1077 | given \a event is handled; otherwise returns \c false. The following | - | ||||||||||||||||||
1078 | key press events are handled by default: | - | ||||||||||||||||||
1079 | - | |||||||||||||||||||
1080 | \list | - | ||||||||||||||||||
1081 | \li \uicontrol Tab | - | ||||||||||||||||||
1082 | \li \uicontrol Backtab | - | ||||||||||||||||||
1083 | \li \uicontrol Enter | - | ||||||||||||||||||
1084 | \li \uicontrol Return | - | ||||||||||||||||||
1085 | \li \uicontrol Esc | - | ||||||||||||||||||
1086 | \endlist | - | ||||||||||||||||||
1087 | - | |||||||||||||||||||
1088 | In the case of \uicontrol Tab, \uicontrol Backtab, \uicontrol Enter and \uicontrol Return | - | ||||||||||||||||||
1089 | key press events, the \a editor's data is comitted to the model | - | ||||||||||||||||||
1090 | and the editor is closed. If the \a event is a \uicontrol Tab key press | - | ||||||||||||||||||
1091 | the view will open an editor on the next item in the | - | ||||||||||||||||||
1092 | view. Likewise, if the \a event is a \uicontrol Backtab key press the | - | ||||||||||||||||||
1093 | view will open an editor on the \e previous item in the view. | - | ||||||||||||||||||
1094 | - | |||||||||||||||||||
1095 | If the event is a \uicontrol Esc key press event, the \a editor is | - | ||||||||||||||||||
1096 | closed \e without committing its data. | - | ||||||||||||||||||
1097 | - | |||||||||||||||||||
1098 | \sa commitData(), closeEditor() | - | ||||||||||||||||||
1099 | */ | - | ||||||||||||||||||
1100 | - | |||||||||||||||||||
1101 | bool QItemDelegate::eventFilter(QObject *object, QEvent *event) | - | ||||||||||||||||||
1102 | { | - | ||||||||||||||||||
1103 | Q_D(QItemDelegate); | - | ||||||||||||||||||
1104 | return d->editorEventFilter(object, event); never executed: return d->editorEventFilter(object, event); | 0 | ||||||||||||||||||
1105 | } | - | ||||||||||||||||||
1106 | - | |||||||||||||||||||
1107 | /*! | - | ||||||||||||||||||
1108 | \reimp | - | ||||||||||||||||||
1109 | */ | - | ||||||||||||||||||
1110 | - | |||||||||||||||||||
1111 | bool QItemDelegate::editorEvent(QEvent *event, | - | ||||||||||||||||||
1112 | QAbstractItemModel *model, | - | ||||||||||||||||||
1113 | const QStyleOptionViewItem &option, | - | ||||||||||||||||||
1114 | const QModelIndex &index) | - | ||||||||||||||||||
1115 | { | - | ||||||||||||||||||
1116 | Q_ASSERT(event); | - | ||||||||||||||||||
1117 | Q_ASSERT(model); | - | ||||||||||||||||||
1118 | - | |||||||||||||||||||
1119 | // make sure that the item is checkable | - | ||||||||||||||||||
1120 | Qt::ItemFlags flags = model->flags(index); | - | ||||||||||||||||||
1121 | if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
| 0 | ||||||||||||||||||
1122 | || !(flags & Qt::ItemIsEnabled))
| 0 | ||||||||||||||||||
1123 | return false; never executed: return false; | 0 | ||||||||||||||||||
1124 | - | |||||||||||||||||||
1125 | // make sure that we have a check state | - | ||||||||||||||||||
1126 | QVariant value = index.data(Qt::CheckStateRole); | - | ||||||||||||||||||
1127 | if (!value.isValid())
| 0 | ||||||||||||||||||
1128 | return false; never executed: return false; | 0 | ||||||||||||||||||
1129 | - | |||||||||||||||||||
1130 | // make sure that we have the right event type | - | ||||||||||||||||||
1131 | if ((event->type() == QEvent::MouseButtonRelease)
| 0 | ||||||||||||||||||
1132 | || (event->type() == QEvent::MouseButtonDblClick)
| 0 | ||||||||||||||||||
1133 | || (event->type() == QEvent::MouseButtonPress)) {
| 0 | ||||||||||||||||||
1134 | QRect checkRect = doCheck(option, option.rect, Qt::Checked); | - | ||||||||||||||||||
1135 | QRect emptyRect; | - | ||||||||||||||||||
1136 | doLayout(option, &checkRect, &emptyRect, &emptyRect, false); | - | ||||||||||||||||||
1137 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | - | ||||||||||||||||||
1138 | if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
| 0 | ||||||||||||||||||
1139 | return false; never executed: return false; | 0 | ||||||||||||||||||
1140 | - | |||||||||||||||||||
1141 | // eat the double click events inside the check rect | - | ||||||||||||||||||
1142 | if ((event->type() == QEvent::MouseButtonPress)
| 0 | ||||||||||||||||||
1143 | || (event->type() == QEvent::MouseButtonDblClick))
| 0 | ||||||||||||||||||
1144 | return true; never executed: return true; | 0 | ||||||||||||||||||
1145 | - | |||||||||||||||||||
1146 | } else if (event->type() == QEvent::KeyPress) { never executed: end of block
| 0 | ||||||||||||||||||
1147 | if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
| 0 | ||||||||||||||||||
1148 | && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
| 0 | ||||||||||||||||||
1149 | return false; never executed: return false; | 0 | ||||||||||||||||||
1150 | } else { never executed: end of block | 0 | ||||||||||||||||||
1151 | return false; never executed: return false; | 0 | ||||||||||||||||||
1152 | } | - | ||||||||||||||||||
1153 | - | |||||||||||||||||||
1154 | Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||||||||
1155 | if (flags & Qt::ItemIsUserTristate)
| 0 | ||||||||||||||||||
1156 | state = ((Qt::CheckState)((state + 1) % 3)); never executed: state = ((Qt::CheckState)((state + 1) % 3)); | 0 | ||||||||||||||||||
1157 | else | - | ||||||||||||||||||
1158 | state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked; never executed: state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
| 0 | ||||||||||||||||||
1159 | return model->setData(index, state, Qt::CheckStateRole); never executed: return model->setData(index, state, Qt::CheckStateRole); | 0 | ||||||||||||||||||
1160 | } | - | ||||||||||||||||||
1161 | - | |||||||||||||||||||
1162 | /*! | - | ||||||||||||||||||
1163 | \internal | - | ||||||||||||||||||
1164 | */ | - | ||||||||||||||||||
1165 | - | |||||||||||||||||||
1166 | QStyleOptionViewItem QItemDelegate::setOptions(const QModelIndex &index, | - | ||||||||||||||||||
1167 | const QStyleOptionViewItem &option) const | - | ||||||||||||||||||
1168 | { | - | ||||||||||||||||||
1169 | QStyleOptionViewItem opt = option; | - | ||||||||||||||||||
1170 | - | |||||||||||||||||||
1171 | // set font | - | ||||||||||||||||||
1172 | QVariant value = index.data(Qt::FontRole); | - | ||||||||||||||||||
1173 | if (value.isValid()){
| 0 | ||||||||||||||||||
1174 | opt.font = qvariant_cast<QFont>(value).resolve(opt.font); | - | ||||||||||||||||||
1175 | opt.fontMetrics = QFontMetrics(opt.font); | - | ||||||||||||||||||
1176 | } never executed: end of block | 0 | ||||||||||||||||||
1177 | - | |||||||||||||||||||
1178 | // set text alignment | - | ||||||||||||||||||
1179 | value = index.data(Qt::TextAlignmentRole); | - | ||||||||||||||||||
1180 | if (value.isValid())
| 0 | ||||||||||||||||||
1181 | opt.displayAlignment = Qt::Alignment(value.toInt()); never executed: opt.displayAlignment = Qt::Alignment(value.toInt()); | 0 | ||||||||||||||||||
1182 | - | |||||||||||||||||||
1183 | // set foreground brush | - | ||||||||||||||||||
1184 | value = index.data(Qt::ForegroundRole); | - | ||||||||||||||||||
1185 | if (value.canConvert<QBrush>())
| 0 | ||||||||||||||||||
1186 | opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); never executed: opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); | 0 | ||||||||||||||||||
1187 | - | |||||||||||||||||||
1188 | // disable style animations for checkboxes etc. within itemviews (QTBUG-30146) | - | ||||||||||||||||||
1189 | opt.styleObject = 0; | - | ||||||||||||||||||
1190 | - | |||||||||||||||||||
1191 | return opt; never executed: return opt; | 0 | ||||||||||||||||||
1192 | } | - | ||||||||||||||||||
1193 | - | |||||||||||||||||||
1194 | QT_END_NAMESPACE | - | ||||||||||||||||||
1195 | - | |||||||||||||||||||
1196 | #include "moc_qitemdelegate.cpp" | - | ||||||||||||||||||
1197 | - | |||||||||||||||||||
1198 | #endif // QT_NO_ITEMVIEWS | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |