view ui/certificateitemdelegate.cpp @ 330:1e6d1eab8395

Fix NSS unit test for Windows and change how instructions are written This was supposed to fix the block on error. But it did not.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 08 Apr 2014 15:08:57 +0000
parents dd6a09d2699f
children 6cc124e79066
line wrap: on
line source
#include <QtWidgets>

#include "certificate.h"
#include "certificateitemdelegate.h"

void CertificateItemDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    // Save the current painter.
    painter->save();
    int status = index.data(Qt::UserRole + 1).toInt();
    if (status == 0) {
        // This status is not known, so draw the default item.
        QStyledItemDelegate::paint(painter, option, index);
        painter->restore();
        return;
    }

    if (status == Certificate::InstallNew) {
        //Set the icon and use bold and bigger font to highlight this item.
        QIcon *icon = new QIcon(":/img/list-add.png");
        QFont *font = new QFont();
        font->setBold(true);
        font->setPointSize(font->pointSize() + 1);
        drawItem(painter, option, index, icon, font);
    }
    else if (status == Certificate::InstallOld) {
        // Set the icon and use the default font for this item.
        QIcon *icon = new QIcon(":/img/list-add.png");
        QFont *font = new QFont();
        drawItem(painter, option, index, icon, font);
    }
    else if (status == Certificate::RemoveNew) {
        //Set the icon and use bold and bigger font to highlight this item.
        QIcon *icon = new QIcon(":/img/list-remove.png");
        QFont *font = new QFont();
        font->setBold(true);
        font->setPointSize(font->pointSize() + 1);
        drawItem(painter, option, index, icon, font);
    }
    else if (status == Certificate::RemoveOld) {
        // Set the icon and use the default font for this item.
        QIcon *icon = new QIcon(":/img/list-remove.png");
        QFont *font = new QFont();
        drawItem(painter, option, index, icon, font);
    }
    else {
        // Draw the default item.
        QStyledItemDelegate::paint(painter, option, index);
    }
    // Restore the painter to have an unmodified painter for the next draw
    // action.
    painter->restore();
    return;
}

void CertificateItemDelegate::drawItem(QPainter *painter,
    const QStyleOptionViewItem &option, const QModelIndex &index,
    QIcon *icon, QFont *font) const
{
    // Get temporary style option to draw a checkbox only.
    QStyleOptionViewItem opt = option;
    // Initialize the style options with the temporary option object.
    initStyleOption(&opt, index);
    // Clear all text to draw the checkbox only.
    opt.text.clear();

    // Draw highlighted background.
    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());
    }

    // Draw the checkbox control with the temporary options.
    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);

    // Draw the icon.
    int iconSpace = 25;
    if (!icon->isNull()) {
        QRect rect = option.rect.adjusted(25, 0, 0, 0);
        icon->paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft);
        iconSpace = 50;
    }

    // Draw the text using the given font.
    QString text = index.data().toString();
    QRect rect = option.rect.adjusted(iconSpace, 0, 0, 0);
    painter->setFont(*font);
    painter->drawText(rect.left(), rect.top(), rect.width(), rect.height(),
        Qt::AlignVCenter|Qt::AlignLeft, text, &rect);
}

http://wald.intevation.org/projects/trustbridge/