rrenkert@266: #include rrenkert@266: rrenkert@266: #include "certificate.h" andre@372: #include "mainwindow.h" rrenkert@266: #include "certificateitemdelegate.h" rrenkert@266: rrenkert@266: void CertificateItemDelegate::paint(QPainter *painter, rrenkert@266: const QStyleOptionViewItem &option, const QModelIndex &index) const rrenkert@266: { rrenkert@266: // Save the current painter. rrenkert@266: painter->save(); andre@375: int status = index.data(StatusRole).toInt(); rrenkert@266: if (status == 0) { rrenkert@266: // This status is not known, so draw the default item. rrenkert@266: QStyledItemDelegate::paint(painter, option, index); rrenkert@266: painter->restore(); rrenkert@266: return; rrenkert@266: } rrenkert@266: rrenkert@266: if (status == Certificate::InstallNew) { rrenkert@266: //Set the icon and use bold and bigger font to highlight this item. rrenkert@266: QIcon *icon = new QIcon(":/img/list-add.png"); rrenkert@266: QFont *font = new QFont(); rrenkert@266: font->setBold(true); rrenkert@266: font->setPointSize(font->pointSize() + 1); rrenkert@266: drawItem(painter, option, index, icon, font); rrenkert@266: } rrenkert@266: else if (status == Certificate::InstallOld) { rrenkert@266: // Set the icon and use the default font for this item. rrenkert@266: QIcon *icon = new QIcon(":/img/list-add.png"); rrenkert@266: QFont *font = new QFont(); rrenkert@266: drawItem(painter, option, index, icon, font); rrenkert@266: } rrenkert@266: else if (status == Certificate::RemoveNew) { rrenkert@266: //Set the icon and use bold and bigger font to highlight this item. rrenkert@266: QIcon *icon = new QIcon(":/img/list-remove.png"); rrenkert@266: QFont *font = new QFont(); rrenkert@266: font->setBold(true); rrenkert@266: font->setPointSize(font->pointSize() + 1); rrenkert@266: drawItem(painter, option, index, icon, font); rrenkert@266: } rrenkert@266: else if (status == Certificate::RemoveOld) { rrenkert@266: // Set the icon and use the default font for this item. rrenkert@266: QIcon *icon = new QIcon(":/img/list-remove.png"); rrenkert@266: QFont *font = new QFont(); rrenkert@266: drawItem(painter, option, index, icon, font); rrenkert@266: } rrenkert@266: else { rrenkert@266: // Draw the default item. rrenkert@266: QStyledItemDelegate::paint(painter, option, index); rrenkert@266: } rrenkert@266: // Restore the painter to have an unmodified painter for the next draw rrenkert@266: // action. rrenkert@266: painter->restore(); rrenkert@266: return; rrenkert@266: } rrenkert@266: rrenkert@266: void CertificateItemDelegate::drawItem(QPainter *painter, rrenkert@266: const QStyleOptionViewItem &option, const QModelIndex &index, rrenkert@266: QIcon *icon, QFont *font) const rrenkert@266: { rrenkert@266: // Get temporary style option to draw a checkbox only. rrenkert@266: QStyleOptionViewItem opt = option; rrenkert@266: // Initialize the style options with the temporary option object. rrenkert@266: initStyleOption(&opt, index); rrenkert@266: // Clear all text to draw the checkbox only. rrenkert@266: opt.text.clear(); rrenkert@266: rrenkert@266: // Draw highlighted background. rrenkert@266: if (option.state & QStyle::State_Selected) { rrenkert@266: painter->fillRect(option.rect, option.palette.highlight()); rrenkert@266: } rrenkert@266: rrenkert@266: // Draw the checkbox control with the temporary options. rrenkert@266: QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); rrenkert@266: rrenkert@266: // Draw the icon. rrenkert@266: int iconSpace = 25; rrenkert@266: if (!icon->isNull()) { rrenkert@266: QRect rect = option.rect.adjusted(25, 0, 0, 0); rrenkert@266: icon->paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft); rrenkert@266: iconSpace = 50; rrenkert@266: } rrenkert@266: rrenkert@266: // Draw the text using the given font. rrenkert@266: QString text = index.data().toString(); rrenkert@266: QRect rect = option.rect.adjusted(iconSpace, 0, 0, 0); rrenkert@266: painter->setFont(*font); rrenkert@266: painter->drawText(rect.left(), rect.top(), rect.width(), rect.height(), rrenkert@266: Qt::AlignVCenter|Qt::AlignLeft, text, &rect); rrenkert@266: }