| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | - | |||||||||||||
| 3 | - | |||||||||||||
| 4 | - | |||||||||||||
| 5 | - | |||||||||||||
| 6 | - | |||||||||||||
| 7 | class QStyledItemDelegatePrivate : public QAbstractItemDelegatePrivate | - | ||||||||||||
| 8 | { | - | ||||||||||||
| 9 | inline QStyledItemDelegate* q_func() { return static_cast<QStyledItemDelegate *>(q_ptr); } inline const QStyledItemDelegate* q_func() const { return static_cast<const QStyledItemDelegate *>(q_ptr); } friend class QStyledItemDelegate; | - | ||||||||||||
| 10 | - | |||||||||||||
| 11 | public: | - | ||||||||||||
| 12 | QStyledItemDelegatePrivate() : factory(0) { } never executed: end of block | 0 | ||||||||||||
| 13 | - | |||||||||||||
| 14 | static const QWidget *widget(const QStyleOptionViewItem &option) | - | ||||||||||||
| 15 | { | - | ||||||||||||
| 16 | return never executed: option.widget;return option.widget;never executed: return option.widget; | 0 | ||||||||||||
| 17 | } | - | ||||||||||||
| 18 | - | |||||||||||||
| 19 | const QItemEditorFactory *editorFactory() const | - | ||||||||||||
| 20 | { | - | ||||||||||||
| 21 | return never executed: factoryreturn factory ? factory : QItemEditorFactory::defaultFactory();
never executed: return factory ? factory : QItemEditorFactory::defaultFactory(); | 0 | ||||||||||||
| 22 | } | - | ||||||||||||
| 23 | - | |||||||||||||
| 24 | QItemEditorFactory *factory; | - | ||||||||||||
| 25 | }; | - | ||||||||||||
| 26 | QStyledItemDelegate::QStyledItemDelegate(QObject *parent) | - | ||||||||||||
| 27 | : QAbstractItemDelegate(*new QStyledItemDelegatePrivate(), parent) | - | ||||||||||||
| 28 | { | - | ||||||||||||
| 29 | } never executed: end of block | 0 | ||||||||||||
| 30 | - | |||||||||||||
| 31 | - | |||||||||||||
| 32 | - | |||||||||||||
| 33 | - | |||||||||||||
| 34 | QStyledItemDelegate::~QStyledItemDelegate() | - | ||||||||||||
| 35 | { | - | ||||||||||||
| 36 | } | - | ||||||||||||
| 37 | QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale& locale) const | - | ||||||||||||
| 38 | { | - | ||||||||||||
| 39 | return never executed: d_func()->textForRole(Qt::DisplayRole, value, locale);return d_func()->textForRole(Qt::DisplayRole, value, locale);never executed: return d_func()->textForRole(Qt::DisplayRole, value, locale); | 0 | ||||||||||||
| 40 | } | - | ||||||||||||
| 41 | void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option, | - | ||||||||||||
| 42 | const QModelIndex &index) const | - | ||||||||||||
| 43 | { | - | ||||||||||||
| 44 | QVariant value = index.data(Qt::FontRole); | - | ||||||||||||
| 45 | if (value.isValid()
| 0 | ||||||||||||
| 46 | option->font = qvariant_cast<QFont>(value).resolve(option->font); | - | ||||||||||||
| 47 | option->fontMetrics = QFontMetrics(option->font); | - | ||||||||||||
| 48 | } never executed: end of block | 0 | ||||||||||||
| 49 | - | |||||||||||||
| 50 | value = index.data(Qt::TextAlignmentRole); | - | ||||||||||||
| 51 | if (value.isValid()
| 0 | ||||||||||||
| 52 | option->displayAlignment = Qt::Alignment(value.toInt()); never executed: option->displayAlignment = Qt::Alignment(value.toInt()); | 0 | ||||||||||||
| 53 | - | |||||||||||||
| 54 | value = index.data(Qt::ForegroundRole); | - | ||||||||||||
| 55 | if (value.canConvert<QBrush>()
| 0 | ||||||||||||
| 56 | option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); never executed: option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); | 0 | ||||||||||||
| 57 | - | |||||||||||||
| 58 | option->index = index; | - | ||||||||||||
| 59 | value = index.data(Qt::CheckStateRole); | - | ||||||||||||
| 60 | if (value.isValid()
| 0 | ||||||||||||
| 61 | option->features |= QStyleOptionViewItem::HasCheckIndicator; | - | ||||||||||||
| 62 | option->checkState = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||
| 63 | } never executed: end of block | 0 | ||||||||||||
| 64 | - | |||||||||||||
| 65 | value = index.data(Qt::DecorationRole); | - | ||||||||||||
| 66 | if (value.isValid()
| 0 | ||||||||||||
| 67 | option->features |= QStyleOptionViewItem::HasDecoration; | - | ||||||||||||
| 68 | switch (value.type()) { | - | ||||||||||||
| 69 | case never executed: QVariant::Icon:case QVariant::Icon:never executed: {case QVariant::Icon: | 0 | ||||||||||||
| 70 | option->icon = qvariant_cast<QIcon>(value); | - | ||||||||||||
| 71 | QIcon::Mode mode; | - | ||||||||||||
| 72 | if (!(option->state & QStyle::State_Enabled)
| 0 | ||||||||||||
| 73 | mode = QIcon::Disabled; never executed: mode = QIcon::Disabled; | 0 | ||||||||||||
| 74 | else if (option->state & QStyle::State_Selected
| 0 | ||||||||||||
| 75 | mode = QIcon::Selected; never executed: mode = QIcon::Selected; | 0 | ||||||||||||
| 76 | else | - | ||||||||||||
| 77 | mode = QIcon::Normal; never executed: mode = QIcon::Normal; | 0 | ||||||||||||
| 78 | QIcon::State state = option->state & QStyle::State_Open
| 0 | ||||||||||||
| 79 | QSize actualSize = option->icon.actualSize(option->decorationSize, mode, state); | - | ||||||||||||
| 80 | - | |||||||||||||
| 81 | option->decorationSize = QSize(qMin(option->decorationSize.width(), actualSize.width()), | - | ||||||||||||
| 82 | qMin(option->decorationSize.height(), actualSize.height())); | - | ||||||||||||
| 83 | break; never executed: break; | 0 | ||||||||||||
| 84 | } | - | ||||||||||||
| 85 | case never executed: QVariant::Color:case QVariant::Color:never executed: {case QVariant::Color: | 0 | ||||||||||||
| 86 | QPixmap pixmap(option->decorationSize); | - | ||||||||||||
| 87 | pixmap.fill(qvariant_cast<QColor>(value)); | - | ||||||||||||
| 88 | option->icon = QIcon(pixmap); | - | ||||||||||||
| 89 | break; never executed: break; | 0 | ||||||||||||
| 90 | } | - | ||||||||||||
| 91 | case never executed: QVariant::Image:case QVariant::Image:never executed: {case QVariant::Image: | 0 | ||||||||||||
| 92 | QImage image = qvariant_cast<QImage>(value); | - | ||||||||||||
| 93 | option->icon = QIcon(QPixmap::fromImage(image)); | - | ||||||||||||
| 94 | option->decorationSize = image.size() / image.devicePixelRatio(); | - | ||||||||||||
| 95 | break; never executed: break; | 0 | ||||||||||||
| 96 | } | - | ||||||||||||
| 97 | case never executed: QVariant::Pixmap:case QVariant::Pixmap:never executed: {case QVariant::Pixmap: | 0 | ||||||||||||
| 98 | QPixmap pixmap = qvariant_cast<QPixmap>(value); | - | ||||||||||||
| 99 | option->icon = QIcon(pixmap); | - | ||||||||||||
| 100 | option->decorationSize = pixmap.size() / pixmap.devicePixelRatio(); | - | ||||||||||||
| 101 | break; never executed: break; | 0 | ||||||||||||
| 102 | } | - | ||||||||||||
| 103 | default never executed: :default:never executed: default: | 0 | ||||||||||||
| 104 | break; never executed: break; | 0 | ||||||||||||
| 105 | } | - | ||||||||||||
| 106 | } | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | value = index.data(Qt::DisplayRole); | - | ||||||||||||
| 109 | if (value.isValid()
| 0 | ||||||||||||
| 110 | option->features |= QStyleOptionViewItem::HasDisplay; | - | ||||||||||||
| 111 | option->text = displayText(value, option->locale); | - | ||||||||||||
| 112 | } never executed: end of block | 0 | ||||||||||||
| 113 | - | |||||||||||||
| 114 | option->backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole)); | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | - | |||||||||||||
| 117 | option->styleObject = 0; | - | ||||||||||||
| 118 | } never executed: end of block | 0 | ||||||||||||
| 119 | void QStyledItemDelegate::paint(QPainter *painter, | - | ||||||||||||
| 120 | const QStyleOptionViewItem &option, const QModelIndex &index) const | - | ||||||||||||
| 121 | { | - | ||||||||||||
| 122 | ((!(index.isValid())) ? qt_assert("index.isValid()",__FILE__,372) : qt_noop()); | - | ||||||||||||
| 123 | - | |||||||||||||
| 124 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 125 | initStyleOption(&opt, index); | - | ||||||||||||
| 126 | - | |||||||||||||
| 127 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 128 | QStyle *style = widget
| 0 | ||||||||||||
| 129 | style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); | - | ||||||||||||
| 130 | } never executed: end of block | 0 | ||||||||||||
| 131 | QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, | - | ||||||||||||
| 132 | const QModelIndex &index) const | - | ||||||||||||
| 133 | { | - | ||||||||||||
| 134 | QVariant value = index.data(Qt::SizeHintRole); | - | ||||||||||||
| 135 | if (value.isValid()
| 0 | ||||||||||||
| 136 | return never executed: qvariant_cast<QSize>(value);return qvariant_cast<QSize>(value);never executed: return qvariant_cast<QSize>(value); | 0 | ||||||||||||
| 137 | - | |||||||||||||
| 138 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 139 | initStyleOption(&opt, index); | - | ||||||||||||
| 140 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 141 | QStyle *style = widget
| 0 | ||||||||||||
| 142 | return never executed: style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget);return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget);never executed: return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); | 0 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | QWidget *QStyledItemDelegate::createEditor(QWidget *parent, | - | ||||||||||||
| 145 | const QStyleOptionViewItem &, | - | ||||||||||||
| 146 | const QModelIndex &index) const | - | ||||||||||||
| 147 | { | - | ||||||||||||
| 148 | const QStyledItemDelegatePrivate * const d = d_func(); | - | ||||||||||||
| 149 | if (!index.isValid()
| 0 | ||||||||||||
| 150 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||
| 151 | return never executed: d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent);return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent);never executed: return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent); | 0 | ||||||||||||
| 152 | } | - | ||||||||||||
| 153 | void QStyledItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const | - | ||||||||||||
| 154 | { | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | - | |||||||||||||
| 157 | - | |||||||||||||
| 158 | - | |||||||||||||
| 159 | QVariant v = index.data(Qt::EditRole); | - | ||||||||||||
| 160 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | if (!n.isEmpty()
| 0 | ||||||||||||
| 163 | if (!v.isValid()
| 0 | ||||||||||||
| 164 | v = QVariant(editor->property(n).userType(), (const void *)0); never executed: v = QVariant(editor->property(n).userType(), (const void *)0); | 0 | ||||||||||||
| 165 | editor->setProperty(n, v); | - | ||||||||||||
| 166 | } never executed: end of block | 0 | ||||||||||||
| 167 | - | |||||||||||||
| 168 | } never executed: end of block | 0 | ||||||||||||
| 169 | void QStyledItemDelegate::setModelData(QWidget *editor, | - | ||||||||||||
| 170 | QAbstractItemModel *model, | - | ||||||||||||
| 171 | const QModelIndex &index) const | - | ||||||||||||
| 172 | { | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | - | |||||||||||||
| 175 | - | |||||||||||||
| 176 | - | |||||||||||||
| 177 | - | |||||||||||||
| 178 | const QStyledItemDelegatePrivate * const d = d_func(); | - | ||||||||||||
| 179 | ((!(model)) ? qt_assert("model",__FILE__,469) : qt_noop()); | - | ||||||||||||
| 180 | ((!(editor)) ? qt_assert("editor",__FILE__,470) : qt_noop()); | - | ||||||||||||
| 181 | QByteArray n = editor->metaObject()->userProperty().name(); | - | ||||||||||||
| 182 | if (n.isEmpty()
| 0 | ||||||||||||
| 183 | n = d->editorFactory()->valuePropertyName( never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||
| 184 | model->data(index, Qt::EditRole).userType()); never executed: n = d->editorFactory()->valuePropertyName( model->data(index, Qt::EditRole).userType()); | 0 | ||||||||||||
| 185 | if (!n.isEmpty()
| 0 | ||||||||||||
| 186 | model->setData(index, editor->property(n), Qt::EditRole); never executed: model->setData(index, editor->property(n), Qt::EditRole); | 0 | ||||||||||||
| 187 | - | |||||||||||||
| 188 | } never executed: end of block | 0 | ||||||||||||
| 189 | - | |||||||||||||
| 190 | - | |||||||||||||
| 191 | - | |||||||||||||
| 192 | - | |||||||||||||
| 193 | - | |||||||||||||
| 194 | void QStyledItemDelegate::updateEditorGeometry(QWidget *editor, | - | ||||||||||||
| 195 | const QStyleOptionViewItem &option, | - | ||||||||||||
| 196 | const QModelIndex &index) const | - | ||||||||||||
| 197 | { | - | ||||||||||||
| 198 | if (!editor
| 0 | ||||||||||||
| 199 | return; never executed: return; | 0 | ||||||||||||
| 200 | ((!(index.isValid())) ? qt_assert("index.isValid()",__FILE__,490) : qt_noop()); | - | ||||||||||||
| 201 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | QStyleOptionViewItem opt = option; | - | ||||||||||||
| 204 | initStyleOption(&opt, index); | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | - | |||||||||||||
| 207 | - | |||||||||||||
| 208 | - | |||||||||||||
| 209 | if (qobject_cast<QExpandingLineEdit*>(editor)
| 0 | ||||||||||||
| 210 | opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, editor); never executed: opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, editor); | 0 | ||||||||||||
| 211 | else | - | ||||||||||||
| 212 | - | |||||||||||||
| 213 | opt.showDecorationSelected = true; never executed: opt.showDecorationSelected = true; | 0 | ||||||||||||
| 214 | - | |||||||||||||
| 215 | QStyle *style = widget
| 0 | ||||||||||||
| 216 | QRect geom = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget); | - | ||||||||||||
| 217 | if ( editor->layoutDirection() == Qt::RightToLeft
| 0 | ||||||||||||
| 218 | const int delta = qSmartMinSize(editor).width() - geom.width(); | - | ||||||||||||
| 219 | if (delta > 0
| 0 | ||||||||||||
| 220 | - | |||||||||||||
| 221 | geom.adjust(-delta, 0, 0, 0); | - | ||||||||||||
| 222 | } never executed: end of block | 0 | ||||||||||||
| 223 | } never executed: end of block | 0 | ||||||||||||
| 224 | - | |||||||||||||
| 225 | editor->setGeometry(geom); | - | ||||||||||||
| 226 | } never executed: end of block | 0 | ||||||||||||
| 227 | - | |||||||||||||
| 228 | - | |||||||||||||
| 229 | - | |||||||||||||
| 230 | - | |||||||||||||
| 231 | - | |||||||||||||
| 232 | - | |||||||||||||
| 233 | - | |||||||||||||
| 234 | QItemEditorFactory *QStyledItemDelegate::itemEditorFactory() const | - | ||||||||||||
| 235 | { | - | ||||||||||||
| 236 | const QStyledItemDelegatePrivate * const d = d_func(); | - | ||||||||||||
| 237 | return never executed: d->factory;return d->factory;never executed: return d->factory; | 0 | ||||||||||||
| 238 | } | - | ||||||||||||
| 239 | void QStyledItemDelegate::setItemEditorFactory(QItemEditorFactory *factory) | - | ||||||||||||
| 240 | { | - | ||||||||||||
| 241 | QStyledItemDelegatePrivate * const d = d_func(); | - | ||||||||||||
| 242 | d->factory = factory; | - | ||||||||||||
| 243 | } never executed: end of block | 0 | ||||||||||||
| 244 | bool QStyledItemDelegate::eventFilter(QObject *object, QEvent *event) | - | ||||||||||||
| 245 | { | - | ||||||||||||
| 246 | QStyledItemDelegatePrivate * const d = d_func(); | - | ||||||||||||
| 247 | return never executed: d->editorEventFilter(object, event);return d->editorEventFilter(object, event);never executed: return d->editorEventFilter(object, event); | 0 | ||||||||||||
| 248 | } | - | ||||||||||||
| 249 | - | |||||||||||||
| 250 | - | |||||||||||||
| 251 | - | |||||||||||||
| 252 | - | |||||||||||||
| 253 | bool QStyledItemDelegate::editorEvent(QEvent *event, | - | ||||||||||||
| 254 | QAbstractItemModel *model, | - | ||||||||||||
| 255 | const QStyleOptionViewItem &option, | - | ||||||||||||
| 256 | const QModelIndex &index) | - | ||||||||||||
| 257 | { | - | ||||||||||||
| 258 | ((!(event)) ? qt_assert("event",__FILE__,588) : qt_noop()); | - | ||||||||||||
| 259 | ((!(model)) ? qt_assert("model",__FILE__,589) : qt_noop()); | - | ||||||||||||
| 260 | - | |||||||||||||
| 261 | - | |||||||||||||
| 262 | Qt::ItemFlags flags = model->flags(index); | - | ||||||||||||
| 263 | if (!(flags & Qt::ItemIsUserCheckable)
| 0 | ||||||||||||
| 264 | || !(flags & Qt::ItemIsEnabled)
| 0 | ||||||||||||
| 265 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 266 | - | |||||||||||||
| 267 | - | |||||||||||||
| 268 | QVariant value = index.data(Qt::CheckStateRole); | - | ||||||||||||
| 269 | if (!value.isValid()
| 0 | ||||||||||||
| 270 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 271 | - | |||||||||||||
| 272 | const QWidget *widget = QStyledItemDelegatePrivate::widget(option); | - | ||||||||||||
| 273 | QStyle *style = widget
| 0 | ||||||||||||
| 274 | - | |||||||||||||
| 275 | - | |||||||||||||
| 276 | if ((
| 0 | ||||||||||||
| 277 | || (
| 0 | ||||||||||||
| 278 | || (
| 0 | ||||||||||||
| 279 | QStyleOptionViewItem viewOpt(option); | - | ||||||||||||
| 280 | initStyleOption(&viewOpt, index); | - | ||||||||||||
| 281 | QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, widget); | - | ||||||||||||
| 282 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | - | ||||||||||||
| 283 | if (me->button() != Qt::LeftButton
| 0 | ||||||||||||
| 284 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 285 | - | |||||||||||||
| 286 | if ((
| 0 | ||||||||||||
| 287 | || (
| 0 | ||||||||||||
| 288 | return never executed: true;return true;never executed: return true; | 0 | ||||||||||||
| 289 | - | |||||||||||||
| 290 | } never executed: else if (event->type() == QEvent::KeyPressend of block
| 0 | ||||||||||||
| 291 | if (static_cast<
| 0 | ||||||||||||
| 292 | && static_cast<
| 0 | ||||||||||||
| 293 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 294 | } never executed: else {end of block | 0 | ||||||||||||
| 295 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 296 | } | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); | - | ||||||||||||
| 299 | if (flags & Qt::ItemIsUserTristate
| 0 | ||||||||||||
| 300 | state = ((Qt::CheckState)((state + 1) % 3)); never executed: state = ((Qt::CheckState)((state + 1) % 3)); | 0 | ||||||||||||
| 301 | else | - | ||||||||||||
| 302 | state = (
never executed: state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked; | 0 | ||||||||||||
| 303 | return never executed: model->setData(index, state, Qt::CheckStateRole);return model->setData(index, state, Qt::CheckStateRole);never executed: return model->setData(index, state, Qt::CheckStateRole); | 0 | ||||||||||||
| 304 | } | - | ||||||||||||
| 305 | - | |||||||||||||
| 306 | - | |||||||||||||
| 307 | - | |||||||||||||
| Switch to Source code | Preprocessed file |