#include "cellsmonitorlabelvoltagedelegate.h"
#include <QCheckBox>
#include <QApplication>
#include <QPainter>
#include <QLineEdit>


LabelVoltageDelegate::LabelVoltageDelegate(QObject *parent)
    : QStyledItemDelegate(parent) //: QItemDelegate(parent)
{
}

QWidget *LabelVoltageDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option,
    const QModelIndex & index ) const
{
    QLineEdit *editor = new QLineEdit(parent);

    return editor;
}

void LabelVoltageDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();

    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    lineEdit->setText(value);
}

void LabelVoltageDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    QString value = lineEdit->text();

    model->setData(index, value, Qt::EditRole);
}

void LabelVoltageDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

void LabelVoltageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //Получаем данные
    auto data = index.model()->data(index, Qt::DisplayRole).toString();

    //Создаем стиль CheckBox
    QStyleOptionButton checkboxstyle;
    //Выбрано или не выбрано
    QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);

    //Центрирование
    checkboxstyle.rect = option.rect;
    checkboxstyle.rect.setLeft(option.rect.x() +
                               option.rect.width()/2 - checkbox_rect.width()/2);

    painter->save();

    if(!data.isEmpty())
    {

    LineWithText r;

    int centerX = option.rect.width()/2 - m_circleSize/2;
    int centerY = option.rect.height()/2 - m_circleSize/2;
    QRectF rectL(
            option.rect.x(),
            option.rect.y() + option.rect.height()/2 ,
            option.rect.width(),
            0
            );

    QRectF rectT( option.rect );
    //QString res = QString::number(data,'f',1);

    r.draw
    (
        painter,
        rectL,
        //QColor("#009352"), // green
        QColor("#bfc5d7"),
        11,
        rectT,
        QString("%1 V").arg(data)
    );

    }
    else {
        RectClass r;
        QRectF rectT( option.rect );
        r.draw
        (
            painter,
            rectT
        );
    }

    painter->restore();
}

