void ProjectWidgetItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const { if (index.isValid() && !index.parent().isValid()) { QModelIndex modifiedIndex = index.sibling(index.row(), ProjectWidget::TAG_COLUMN); QStyleOptionViewItemV4 modifiedOption(option); modifiedOption.rect.setX(option.decorationSize.width()); int width = option.fontMetrics.width(modifiedIndex.data().toString()) + option.decorationSize.width(); QPaintEngine* paintEngine = painter->paintEngine(); if (paintEngine != 0) { QPaintDevice* paintDevice = paintEngine->paintDevice(); width = paintDevice->width(); } modifiedOption.rect.setWidth(width); QItemDelegate::paint(painter, modifiedOption, modifiedIndex); return; } QItemDelegate::paint(painter, option, index); }
/** Draws the column 2 (AutoUpdate) of ScriptRepositoryView. * * This function is called every time the ScriptRepository needs to *draw the widget for the AutoUpdate of the file/folder inside the *ScriptRepository. Instead of displaying the strings 'true' and *'false' it will draw a checkbox that 'hoppefully' will better *indicate to the user the condition of the entry as well as *encourage him to act. The action will be dealt with at the *editorEvent. * * When this method is called, it will get the index in order to *retrieve the information about the state of the entry *(folder/file). * * * @param painter: Required to draw the widget * @param option: Provided by the framework and has information *displaying the widget. * @param index: Identifies the entry inside the RepoModel * (indirectly the file / folder). */ void ScriptRepositoryView::CheckBoxDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) return; if (painter->device() == 0) return; QStyleOptionViewItemV4 modifiedOption(option); QPoint p = modifiedOption.rect.center(); QSize curr = modifiedOption.rect.size(); int min_value = (int)((curr.width() < curr.height()) ? curr.width() : curr.height() * .8); // make the checkbox a square in the center of the cell modifiedOption.rect.setSize(QSize(min_value, min_value)); modifiedOption.rect.moveCenter(p); // get the current state of this entry QString state = index.model()->data(index, Qt::DisplayRole).toString(); if (state == "true") modifiedOption.state |= QStyle::State_On; else if (state == "false") modifiedOption.state |= QStyle::State_Off; else return; // draw it QApplication::style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &modifiedOption, painter); }
void BooleanDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QVariant value=index.data(); if (!value.isValid() || qVariantCanConvert<bool>(value)) { bool boolVal = value.isValid() ? value.toBool() : defaultValue; QStyle *style=qApp->style(); QRect checkBoxRect=style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); int chkWidth=checkBoxRect.width(); int chkHeight=checkBoxRect.height(); int centerX=option.rect.left() + qMax(option.rect.width()/2-chkWidth/2, 0); int centerY=option.rect.top() + qMax(option.rect.height()/2-chkHeight/2, 0); QStyleOptionViewItem modifiedOption(option); modifiedOption.rect.moveTo(centerX, centerY); modifiedOption.rect.setSize(QSize(chkWidth, chkHeight)); if((option.state & QStyle::State_Enabled) != QStyle::State_Enabled){ painter->fillRect(option.rect, qApp->palette().background()); } painter->drawPixmap(modifiedOption.rect, boolVal ? checkedPixmap : uncheckedPixmap); /*QStyle *style=qApp->style(); QRect checkBoxRect=style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); int chkWidth=checkBoxRect.width(); int chkHeight=checkBoxRect.height(); int centerX=option.rect.left() + qMax(option.rect.width()/2-chkWidth/2, 0); int centerY=option.rect.top() + qMax(option.rect.height()/2-chkHeight/2, 0); QStyleOptionViewItem modifiedOption(option); modifiedOption.rect.moveTo(centerX, centerY); modifiedOption.rect.setSize(QSize(chkWidth, chkHeight)); if(boolVal){ modifiedOption.state |= QStyle::State_On; modifiedOption.state &= ~QStyle::State_Off; }else{ modifiedOption.state &= ~QStyle::State_On; modifiedOption.state |= QStyle::State_Off; } if((option.state & QStyle::State_Enabled) != QStyle::State_Enabled){ painter->fillRect(option.rect, qApp->palette().background()); } style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &modifiedOption, painter); */ }else{ QStyledItemDelegate::paint(painter, option, index); } }