📄 qitemdelegate.cpp
字号:
QPixmap QItemDelegate::decoration(const QStyleOptionViewItem &option, const QVariant &variant) const{ Q_D(const QItemDelegate); switch (variant.type()) { case QVariant::Icon: { QIcon::Mode mode = d->iconMode(option.state); QIcon::State state = d->iconState(option.state); return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); } case QVariant::Color: { static QPixmap pixmap(option.decorationSize); pixmap.fill(qvariant_cast<QColor>(variant)); return pixmap; } default: break; } return qvariant_cast<QPixmap>(variant);}// hacky but faster version of "QString::sprintf("%d-%d", i, enabled)"static QString qPixmapSerial(quint64 i, bool enabled){ ushort arr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', '0' + enabled }; ushort *ptr = &arr[16]; while (i > 0) { // hey - it's our internal representation, so use the ascii character after '9' // instead of 'a' for hex *(--ptr) = '0' + i % 16; i >>= 4; } return QString::fromUtf16(ptr, int(&arr[sizeof(arr) / sizeof(ushort)] - ptr));}/*! \internal Returns the selected version of the given \a pixmap using the given \a palette. The \a enabled argument decides whether the normal or disabled highlight color of the palette is used.*/QPixmap *QItemDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const{ QString key = qPixmapSerial(qt_pixmap_id(pixmap), enabled); QPixmap *pm = QPixmapCache::find(key); if (!pm) { QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied); QColor color = palette.color(enabled ? QPalette::Normal : QPalette::Disabled, QPalette::Highlight); color.setAlphaF(0.3); QPainter painter(&img); painter.setCompositionMode(QPainter::CompositionMode_SourceAtop); painter.fillRect(0, 0, img.width(), img.height(), color); painter.end(); QPixmap selected = QPixmap(QPixmap::fromImage(img)); int n = (img.numBytes() >> 10) + 1; if (QPixmapCache::cacheLimit() < n) QPixmapCache::setCacheLimit(n); QPixmapCache::insert(key, selected); pm = QPixmapCache::find(key); } return pm;}/*! \internal*/QRect QItemDelegate::rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const{ Q_D(const QItemDelegate); QVariant value = index.data(role); if (role == Qt::CheckStateRole) return check(option, option.rect, value); if (value.isValid()) { switch (value.type()) { case QVariant::Invalid: break; case QVariant::Pixmap: return QRect(QPoint(0, 0), qvariant_cast<QPixmap>(value).size()); case QVariant::Image: return QRect(QPoint(0, 0), qvariant_cast<QImage>(value).size()); case QVariant::Icon: { QIcon::Mode mode = d->iconMode(option.state); QIcon::State state = d->iconState(option.state); QIcon icon = qvariant_cast<QIcon>(value); QSize size = icon.actualSize(option.decorationSize, mode, state); return QRect(QPoint(0, 0), size); } case QVariant::Color: return QRect(QPoint(0, 0), option.decorationSize); case QVariant::String: default: { QString text = QItemDelegatePrivate::valueToText(value, option); value = index.data(Qt::FontRole); QFont fnt = qvariant_cast<QFont>(value).resolve(option.font); return textRectangle(0, d->textLayoutBounds(option), fnt, text); } } } return QRect();}/*! \internal Note that on Mac, if /usr/include/AssertMacros.h is included prior to QItemDelegate, and the application is building in debug mode, the check(assertion) will conflict with QItemDelegate::check. To avoid this problem, add #ifdef check #undef check #endif after including AssertMacros.h*/QRect QItemDelegate::check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &value) const{ if (value.isValid()) { Q_D(const QItemDelegate); QStyleOptionButton opt; opt.QStyleOption::operator=(option); opt.rect = bounding; const QWidget *widget = d->widget(option); // cast QStyle *style = widget ? widget->style() : QApplication::style(); return style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, widget); } return QRect();}/*! \internal*/QRect QItemDelegate::textRectangle(QPainter * /*painter*/, const QRect &rect, const QFont &font, const QString &text) const{ Q_D(const QItemDelegate); d->textOption.setWrapMode(QTextOption::WordWrap); d->textLayout.setTextOption(d->textOption); d->textLayout.setFont(font); d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text)); const QSize size = d->doTextLayout(rect.width()).toSize(); // ###: textRectangle should take style option as argument const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; return QRect(0, 0, size.width() + 2 * textMargin, size.height());}/*! \fn bool QItemDelegate::eventFilter(QObject *editor, QEvent *event) Returns true if the given \a editor is a valid QWidget and the given \a event is handled; otherwise returns false. The following key press events are handled by default: \list \o \gui Tab \o \gui Backtab \o \gui Enter \o \gui Return \o \gui Esc \endlist In the case of \gui Tab, \gui Backtab, \gui Enter and \gui Return key press events, the \a editor's data is comitted to the model and the editor is closed. If the \a event is a \gui Tab key press the view will open an editor on the next item in the view. Likewise, if the \a event is a \gui Backtab key press the view will open an editor on the \e previous item in the view. If the event is a \gui Esc key press event, the \a editor is closed \e without committing its data. \sa commitData(), closeEditor()*/bool QItemDelegate::eventFilter(QObject *object, QEvent *event){ QWidget *editor = ::qobject_cast<QWidget*>(object); if (!editor) return false; if (event->type() == QEvent::KeyPress) { switch (static_cast<QKeyEvent *>(event)->key()) { case Qt::Key_Tab: emit commitData(editor); emit closeEditor(editor, QAbstractItemDelegate::EditNextItem); return true; case Qt::Key_Backtab: emit commitData(editor); emit closeEditor(editor, QAbstractItemDelegate::EditPreviousItem); return true; case Qt::Key_Enter: case Qt::Key_Return: // We want the editor to be able to process the key press // before committing the data (e.g. so it can do // validation/fixup of the input). QMetaObject::invokeMethod(this, "_q_commitDataAndCloseEditor", Qt::QueuedConnection, Q_ARG(QWidget*, editor)); return false; case Qt::Key_Escape: // don't commit data emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache); break; default: return false; } if (editor->parentWidget()) editor->parentWidget()->setFocus(); return true; } else if (event->type() == QEvent::FocusOut) { if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) { QWidget *w = QApplication::focusWidget(); while (w) { // don't worry about focus changes internally in the editor if (w == editor) return false; w = w->parentWidget(); }#ifndef QT_NO_DRAGANDDROP // The window may lose focus during an drag operation. // i.e when dragging involves the taskbar on Windows. if (QDragManager::self() && QDragManager::self()->object != 0) return false;#endif // Opening a modal dialog will start a new eventloop // that will process the deleteLater event. if (QApplication::activeModalWidget() && !QApplication::activeModalWidget()->isAncestorOf(editor)) return false; emit commitData(editor); emit closeEditor(editor, NoHint); } } return false;}/*! \reimp*/bool QItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){ Q_ASSERT(event); Q_ASSERT(model); // make sure that the item is checkable Qt::ItemFlags flags = model->flags(index); if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled) || !(flags & Qt::ItemIsEnabled)) return false; // make sure that we have a check state QVariant value = index.data(Qt::CheckStateRole); if (!value.isValid()) return false; // make sure that we have the right event type if ((event->type() == QEvent::MouseButtonRelease) || (event->type() == QEvent::MouseButtonDblClick)) { QRect checkRect = check(option, option.rect, Qt::Checked); QRect emptyRect; doLayout(option, &checkRect, &emptyRect, &emptyRect, false); if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos())) return false; // eat the double click events inside the check rect if (event->type() == QEvent::MouseButtonDblClick) return true; } else if (event->type() == QEvent::KeyPress) { if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select) return false; } else { return false; } Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); return model->setData(index, state, Qt::CheckStateRole);}/*! \internal*/QStyleOptionViewItem QItemDelegate::setOptions(const QModelIndex &index, const QStyleOptionViewItem &option) const{ QStyleOptionViewItem opt = option; // set font QVariant value = index.data(Qt::FontRole); if (value.isValid()){ opt.font = qvariant_cast<QFont>(value).resolve(opt.font); opt.fontMetrics = QFontMetrics(opt.font); } // set text alignment value = index.data(Qt::TextAlignmentRole); if (value.isValid()) opt.displayAlignment = (Qt::Alignment)value.toInt(); // set foreground brush value = index.data(Qt::ForegroundRole); if (qVariantCanConvert<QBrush>(value)) opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value)); return opt;}#include "moc_qitemdelegate.cpp"#endif // QT_NO_ITEMVIEWS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -