view ui/certificatetabledelegate.cpp @ 347:dde533ba4fcc

Added table item delegate for certificates.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 10 Apr 2014 14:13:34 +0200
parents
children c9315b24b055
line wrap: on
line source
#include <QtWidgets>
#include <QComboBox>

#include "certificatetabledelegate.h"

void CertificateTableDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();

    if (index.column() == 0) {
        if (option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());
        }
        bool value = index.data(Qt::DisplayRole).toBool();
        QIcon icon = value ? QIcon(":/img/list-add.png"):QIcon(":/img/list-remove.png") ;
        icon.paint(painter, option.rect, Qt::AlignVCenter|Qt::AlignHCenter);
    }
    else {
        QStyledItemDelegate::paint(painter, option, index);
    }
    painter->restore();
}

QWidget *CertificateTableDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() == 0) {
        // Draw a combobox in the first column for install/remove certificate
        // selection.
        return drawComboBox(parent, option, index);
    }
    else {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

QWidget *CertificateTableDelegate::drawComboBox(QWidget *parent,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    // Create a combobox and add two items for install/remove.
    QComboBox *comboBox = new QComboBox(parent);
    comboBox->addItem(QIcon(":/img/list-add.png"), QString(""), QVariant("true"));
    comboBox->addItem(QIcon(":/img/list-remove.png"), QString(""), QVariant("false"));
    return comboBox;
}

void CertificateTableDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
{
    if (index.column() != 0) {
        return;
    }
    // Get the current value from the model.
    QString value = index.data(Qt::DisplayRole).toString();

    qDebug() << "model value is: " + index.data(Qt::DisplayRole).toString();
    // Find the index in comboxbox items and set the current index.
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    int ndx = comboBox->findData(value, Qt::UserRole);
    comboBox->setCurrentIndex(ndx);
}

void CertificateTableDelegate::setModelData(QWidget *editor,
    QAbstractItemModel *model, const QModelIndex &index) const
{
    if (index.column() != 0) {
        return;
    }
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    bool value = comboBox->currentData().toBool();
    model->setData(index, value, Qt::EditRole);
}

void CertificateTableDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() != 0) {
        return;
    }
    editor->setGeometry(option.rect);
}

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