OpenCoverage

qitemdelegate.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9