void ItemViewButtons::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { bool mouseIsOver = false; if (painter != NULL && painter->device() != NULL && painter->device()->devType() == QInternal::Widget) { QWidget* w = static_cast<QWidget*>(painter->device()); if (w != NULL) { QPoint mousePos = QCursor::pos(); QPoint wMousePos = w->mapFromGlobal(mousePos); mouseIsOver = option.rect.contains(wMousePos); } } // QStyledItemDelegate::paint(painter, option, index); if (this->itemView()->isEnabled()) { QStyleOptionViewItemV4 optionForBtn(option); optionForBtn.rect = this->itemView()->visualRect(d->modelIndexForButtonDisplay(index)); const int btnIndex = this->buttonAtModelIndex(index); Private::ButtonInfo* btnInfo = d->mutableButtonInfo(btnIndex); if (btnInfo == NULL) return; // Check if button can be displayed if (btnInfo->itemDisplayModes.testFlag(DisplayWhenItemSelected) && !option.state.testFlag(QStyle::State_Selected)) { // painter->fillRect(optionForBtn.rect, optionForBtn.backgroundBrush); return; } if (btnInfo->itemDisplayModes.testFlag(DisplayPermanent)) { d->paintButton(btnInfo, painter, optionForBtn); } else if (btnInfo->itemDisplayModes.testFlag(DisplayOnDetection)) { if (mouseIsOver) d->paintButton(btnInfo, painter, optionForBtn); else painter->fillRect(optionForBtn.rect, optionForBtn.backgroundBrush); } } else { d->resetButtonUnderMouseState(); // d->setAllIsOverButtonState(false); } }
void ViewItemButtonMechanism::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) { bool mouseIsOver = false; if (painter != 0) { QWidget* w = dynamic_cast<QWidget*>(painter->device()); if (w != 0) { QPoint mousePos = QCursor::pos(); QPoint wMousePos = w->mapFromGlobal(mousePos); mouseIsOver = option.rect.contains(wMousePos); } } // QStyledItemDelegate::paint(painter, option, index); if (this->itemView()->isEnabled()) { QStyleOptionViewItemV4 optionForBtn(option); optionForBtn.rect = this->itemView()->visualRect(d->modelIndexForButtonDisplay(index)); const int btnIndex = this->buttonAtModelIndex(index); ViewItemButtonMechanismPrivate::ButtonInfo* btnInfo = d->mutableButtonInfo(btnIndex); const int dispMode = btnInfo != 0 ? btnInfo->itemDisplayMode : -1; switch (dispMode) { case DisplayPermanent: { d->paintButton(btnInfo, painter, optionForBtn); break; } case DisplayOnDetection: { if (mouseIsOver) d->paintButton(btnInfo, painter, optionForBtn); else painter->fillRect(optionForBtn.rect, optionForBtn.backgroundBrush); break; } default: break; } } else { d->resetButtonUnderMouseState(); // d->setAllIsOverButtonState(false); } }