#include "pondere.h"
#include <QDebug>
#include <QComboBox>

VariantModel::VariantModel(QObject * parent)
{

}

VariantModel::~VariantModel(){}

QModelIndex VariantModel::index ( int row, int column, const QModelIndex & parent  ) const
{
     if (!parent.child(row,column).isValid())
     {
     	  return createIndex(row, column);
         }
     else
     {
     	return parent.child(row,column);
         }
     
}

QModelIndex VariantModel::parent ( const QModelIndex & index  ) const
{
            return QModelIndex();
}

int VariantModel::rowCount(const QModelIndex &parent ) const {
     Q_UNUSED(parent);
     return m_itemsVector.size();
}

int VariantModel::columnCount(const QModelIndex &parent ) const {
     Q_UNUSED(parent);
     return int(COL_COUNT);
}

bool VariantModel::setData ( const QModelIndex & index, const QVariant & value, int role  )
{
    int row = index.row();
    int column = index.column();
    QVector<QVariant> list;
    list = m_itemsVector.at(row);
    switch(column){
        case ACT :
            list.replace(ACT,value);
            break;
        case MODIFIER :
            list.replace(MODIFIER,value);
            break;
        default :
            break;    
        }
    m_itemsVector.replace(row,list);
    Q_EMIT dataChanged(index, index);
    return true;       
}

QVariant VariantModel::data(const QModelIndex &index, int role ) const
 {
            
            QVariant data;
            if (!index.isValid()) {
                     qWarning() << __FILE__ << QString::number(__LINE__) << "index not valid" ;
                return QVariant();
                }
                
            
            if (role==Qt::EditRole || role==Qt::DisplayRole) {
               int row = index.row();
               const QVector<QVariant> & valuesListByRow = m_itemsVector.at(row);
               data = valuesListByRow[index.column()];
            }
            return data;
 }

 bool VariantModel::insertRows( int position, int count, const QModelIndex & parent  )
 {
            beginInsertRows(parent, position, position+count-1);
            for (int row=0; row < count; row++) {
                
                QVector<QVariant> list;
                for (int j = 0; j < 2; j += 1)
                {
            	    list << QVariant(0);
            	    }
                m_itemsVector . append(list);
                }
            endInsertRows();
            return true;
            
        }


Qt::ItemFlags VariantModel::flags(const QModelIndex &index) const
{
   return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}


SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
     : QItemDelegate(parent)
 {
 }

 QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
     const QStyleOptionViewItem &/* option */,
     const QModelIndex &/* index */) const
 {
     QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
     editor->setMinimum(0.0);
     editor->setMaximum(1.0);
     editor->setSingleStep(0.1);

     return editor;
 }

 void SpinBoxDelegate::setEditorData(QWidget *editor,
                                     const QModelIndex &index) const
 {
     double value = index.model()->data(index, Qt::DisplayRole).toDouble();

     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
     spinBox->setValue(value);
 }

 void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
 {
     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
     spinBox->interpretText();
     double value = spinBox->value();

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

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

PondereActes::PondereActes(QWidget * parent,QString datas)
{
    setupUi(this);
    QString dataLine;
    QStringList listOfDatas;
    qDebug() << __FILE__ << QString::number(__LINE__) << " datas  =" << datas ;
    m_model = new VariantModel(this);
    QStringList listOfLines = datas.split("\n");
    if (listOfLines.size()>1)
    {
    	  dataLine = listOfLines[1];
    	  qDebug() << __FILE__ << QString::number(__LINE__) << " dataLine =" << dataLine ;
        }
    if (dataLine.contains("+"))
    {
    	  listOfDatas = dataLine.split("+");
    	  listOfDatas.removeAll("");
        }
    tableView->setItemDelegateForColumn(MODIFIER,new SpinBoxDelegate);
    for (int row = 0; row < listOfDatas.size(); row += 1)
    {
    	QString act = listOfDatas[row];
    	if (!act.isEmpty())
    	{
    	    QVariant half = QVariant(0.5);
    	    qDebug() << __FILE__ << QString::number(__LINE__) << " act =" << act ;
    	    m_model->insertRows(row,1,QModelIndex());
    	    m_model->setData(m_model->index(row,ACT),act,Qt::EditRole);
    	    m_model->setData(m_model->index(row,MODIFIER),half,Qt::EditRole);
    	    m_model->submit();  
    	    }
    	
    	}
    tableView->setModel(m_model);
}

PondereActes::~PondereActes(){}

QString PondereActes::getModifiedValues()
{
    QString resume;
    for (int row = 0; row < m_model->rowCount(); row += 1)
    {
    	  resume += m_model->data(m_model->index(row,ACT),Qt::DisplayRole).toString()
    	            +"*"+
    	            m_model->data(m_model->index(row,MODIFIER),Qt::DisplayRole).toString();
    	  if (row < m_model->rowCount() -1)
    	  {
    	  	  resume += "+";
    	      }
    	            
        }
    return resume;
}

