📄 qitemdelegate.cpp
字号:
int y = option.rect.top(); int w, h; textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding QSize pm(0, 0); if (pixmapRect->isValid()) { pm = option.decorationSize; pm.rwidth() += 2 * textMargin; } if (hint) { h = qMax(checkRect->height(), qMax(textRect->height(), pm.height())); if (option.decorationPosition == QStyleOptionViewItem::Left || option.decorationPosition == QStyleOptionViewItem::Right) { w = textRect->width() + pm.width(); } else { w = qMax(textRect->width(), pm.width()); } } else { w = option.rect.width(); h = option.rect.height(); } int cw = 0; QRect check; if (checkRect->isValid()) { cw = checkRect->width() + 2 * textMargin; if (hint) w += cw; if (option.direction == Qt::RightToLeft) { check.setRect(x + w - cw, y, cw, h); } else { check.setRect(x, y, cw, h); } } // at this point w should be the *total* width QRect display; QRect decoration; switch (option.decorationPosition) { case QStyleOptionViewItem::Top: { if (!pm.isEmpty()) pm.setHeight(pm.height() + textMargin); // add space h = hint ? textRect->height() : h - pm.height(); if (option.direction == Qt::RightToLeft) { decoration.setRect(x, y, w - cw, pm.height()); display.setRect(x, y + pm.height(), w - cw, h); } else { decoration.setRect(x + cw, y, w - cw, pm.height()); display.setRect(x + cw, y + pm.height(), w - cw, h); } break; } case QStyleOptionViewItem::Bottom: { if (!textRect->isEmpty()) textRect->setHeight(textRect->height() + textMargin); // add space h = hint ? textRect->height() + pm.height() : h; if (option.direction == Qt::RightToLeft) { display.setRect(x, y, w - cw, textRect->height()); decoration.setRect(x, y + textRect->height(), w - cw, h - textRect->height()); } else { display.setRect(x + cw, y, w - cw, textRect->height()); decoration.setRect(x + cw, y + textRect->height(), w - cw, h - textRect->height()); } break; } case QStyleOptionViewItem::Left: { if (option.direction == Qt::LeftToRight) { decoration.setRect(x + cw, y, pm.width(), h); display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); } else { display.setRect(x, y, w - pm.width() - cw, h); decoration.setRect(display.right() + 1, y, pm.width(), h); } break; } case QStyleOptionViewItem::Right: { if (option.direction == Qt::LeftToRight) { display.setRect(x + cw, y, w - pm.width() - cw, h); decoration.setRect(display.right() + 1, y, pm.width(), h); } else { decoration.setRect(x, y, pm.width(), h); display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h); } break; } default: qWarning("doLayout: decoration position is invalid"); decoration = *pixmapRect; break; } if (!hint) { // we only need to do the internal layout if we are going to paint *checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, checkRect->size(), check); *pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment, pixmapRect->size(), decoration); // the text takes up all awailable space, unless the decoration is not shown as selected if (option.showDecorationSelected) *textRect = display; else *textRect = QStyle::alignedRect(option.direction, option.displayAlignment, textRect->size().boundedTo(display.size()), display); } else { *checkRect = check; *pixmapRect = decoration; *textRect = display; }}/*! \internal Returns the pixmap used to decorate the root of the item view. The style \a option controls the appearance of the root; the \a variant refers to the data associated with an item.*/QPixmap QItemDelegate::decoration(const QStyleOptionViewItem &option, const QVariant &variant) const{ switch (variant.type()) { case QVariant::Icon: return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, option.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled, option.state & QStyle::State_Open ? QIcon::On : QIcon::Off); 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*/QPixmap *QItemDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const{ QString key = qPixmapSerial(pixmap.serialNumber(), 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)); QPixmapCache::insert(key, selected); pm = QPixmapCache::find(key); } return pm;}/*! \internal*/QRect QItemDelegate::check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &value) const{ if (value.isValid()) { QStyleOptionButton opt; opt.QStyleOption::operator=(option); opt.rect = bounding; return QApplication::style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt); } return QRect();}/*! If the \a object is the current editor: if the \a event is an Esc key press the current edit is cancelled and ended, or if the \a event is an Enter or Return key press the current edit is accepted and ended. If editing is ended the event filter returns true to signify that it has handled the event; in all other cases it does nothing and returns false to signify that the event hasn't been handled. \sa 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: emit commitData(editor); emit closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); break; 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) { // dont 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 task bar on Windows. if (QDragManager::self() && QDragManager::self()->object != 0) return false;#endif 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 we have the right event type and that the item is checkable if ((event->type() != QEvent::MouseButtonRelease) || !(model->flags(index) & Qt::ItemIsUserCheckable)) return false; // check if the event happened in the right place QVariant value = index.data(Qt::CheckStateRole); QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignLeft | Qt::AlignVCenter, check(option, option.rect, value).size(), QRect(option.rect.x() + textMargin, option.rect.y(), option.rect.width(), option.rect.height())); if (checkRect.contains(static_cast<QMouseEvent*>(event)->pos())) { Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); return model->setData(index, (state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole); } return false;}#endif // QT_NO_ITEMVIEWS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -