#include "cctrlparamtable.h"

#include <QLineEdit>
#include "cctrlcombo.h"
#include <QHeaderView>
#include <QFont>
#include "gui.h"
#include "cctrllineedit.h"

CCtrlParamTable::CCtrlParamTable(QWidget *parent)
    : CMWidget(parent)
{
    m_pTable=new QTableWidget(this);
    m_pLayOut=new QGridLayout(this);
    m_pLayOut->addWidget(m_pTable);
    setLayout(m_pLayOut);
    memset(m_pVariant,0,M_Max_Table_ColoumX*M_Max_Table_RowY);

    m_iRowCntY=0;
    m_iColoumCntX=0;

    m_iRowCurY=0;
    m_iColoumCurX=0;

    connect(m_pTable,SIGNAL(currentCellChanged(int,int,int,int)),
            this,SLOT(TableCellChanged(int,int,int,int)));
    //    QFont font(M_FONT,M_FONT_SIZE);
    //    m_pTable->setFont(font);
    m_pTable->setEditTriggers(QTableWidget::NoEditTriggers);

    m_pTable->setAlternatingRowColors(true);
    m_pLayOut->setMargin(0);
    m_pLayOut->setSpacing(0);
    m_pLayOut->setSpacing(0);
}

void CCtrlParamTable::MParamInit_table(Variant **pParam,
                                       int32_t iColoumX,
                                       int32_t iRowY,
                                       const QStringList& nameList)
{
    m_iRowCntY=iRowY;
    m_iColoumCntX=iColoumX+1;
    m_pTable->setSortingEnabled(false);
    m_pTable->setColumnCount(m_iColoumCntX);
    m_pTable->setRowCount(m_iRowCntY);
    QStringList header;
    header<<QObject::trUtf8("name");
    for(int i=0;i<m_iColoumCntX;i++)
    {
        for(int j=0;j<m_iRowCntY;j++)
        {
            if(i<m_iColoumCntX-1)
            {
                Q_ASSERT(pParam[i+j*iColoumX]!=0);
                m_pVariant[i+j*iColoumX]=pParam[i+j*iColoumX];
            }

            QTableWidgetItem* pitem=new QTableWidgetItem();
            if(i==0)
            {
                QString s=nameList[j];
                s.replace(QString("_"), QString(" "));
                pitem->setText(s);
            }

            pitem->setToolTip("info");
            m_pTable->setItem(j,i,pitem);
        }
        header<<pParam[i]->m_sName;
    }
    m_pTable->setHorizontalHeaderLabels(header);
    QHeaderView * headview= m_pTable->horizontalHeader();
    headview->setDefaultSectionSize(110);
    int iCount=headview->count();
    headview->setSectionResizeMode(0,QHeaderView::ResizeToContents);
    for(int i=1;i<iCount;i++)
    {
        headview->setSectionResizeMode(i,QHeaderView::Interactive);
    }
}

void CCtrlParamTable::MFresh()
{
    m_pTable->setCurrentCell(-1,-1);

    for(int i=0;i<m_iColoumCntX-1;i++)
    {
        for(int j=0;j<m_iRowCntY;j++)
        {
            QTableWidgetItem* pitem=m_pTable->item(j,i+1);
            Q_ASSERT(pitem);
            Variant* pVariant=m_pVariant[i+j*(m_iColoumCntX-1)];
            QVariant& var=pVariant->m_vVar;
            pitem->setData(Qt::UserRole,var);
            if(var.type()!=QVariant::UInt)
            {
                if(var.type()==QVariant::Bool)
                {
                    if(var.toBool())
                    {
                        pitem->setText(QObject::trUtf8("yes"));
                    }
                    else
                    {
                        pitem->setText(QObject::trUtf8("no"));
                    }
                }
                else if(var.type()==QVariant::Double)
                {
                    pitem->setText(QString::number(var.toDouble()));
                }
                else
                {
                    pitem->setText(var.toString());
                }
            }
            else
            {
                if(pVariant->m_slRange.size())
                {
                    pitem->setText(pVariant->m_slRange[var.toUInt()]);
                }
            }
        }
    }
}

void CCtrlParamTable::MApply()
{
    m_pTable->setCurrentCell(-1,-1);
    for(int i=0;i<m_iColoumCntX-1;i++)
    {
        for(int j=0;j<m_iRowCntY;j++)
        {
            QTableWidgetItem* pitem=m_pTable->item(j,i+1);
            Variant* pVariant=m_pVariant[i+j*(m_iColoumCntX-1)];

            QVariant& var=pVariant->m_vVar;
            var= pitem->data(Qt::UserRole);
        }
    }
}

void CCtrlParamTable::MTimeFresh()
{

}

void CCtrlParamTable::TableCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{

    if(previousRow>=0&&previousRow<m_iRowCntY)
    {
        if(previousColumn>=1&&previousColumn<m_iColoumCntX)
        {
            QWidget *pWidgetPre=m_pTable->cellWidget(previousRow,previousColumn);
            if(pWidgetPre)
            {
                QTableWidgetItem* pitemPre=m_pTable->item(previousRow, previousColumn);
                QVariant var=pitemPre->data(Qt::UserRole);
                QVariant::Type type=var.type();
                if(type!=QVariant::UInt&&
                        type!=QVariant::Bool)
                {
                    QLineEdit* pLineEdit=(QLineEdit*)pWidgetPre;
                    QString text=pLineEdit->text();
                    pitemPre->setText(text);
                    switch (var.type()) {
                    case QVariant::Int:
                        var=text.toInt();
                        break;
                    case QVariant::Double:
                        var=text.toDouble();
                        break;
                    case QVariant::String:
                        var=text;
                        break;
                    default:
                        Q_ASSERT(0);
                        break;
                    }
                }
                else if(type==QVariant::Bool)
                {
                    CCtrlCombo* pCombo=(CCtrlCombo*)pWidgetPre;
                    QString text=pCombo->currentText();
                    pitemPre->setText(text);
                    var=pCombo->currentIndex()?true:false;
                }
                else
                {
                    CCtrlCombo* pCombo=(CCtrlCombo*)pWidgetPre;
                    QString text=pCombo->currentText();
                    pitemPre->setText(text);
                    var=uint(pCombo->currentIndex());
                }
                pitemPre->setData(Qt::UserRole,var);
            }
        }
    }
    m_pTable->removeCellWidget(previousRow, previousColumn);

    if(currentRow>=0&&currentRow<m_iRowCntY)
    {
        if(currentColumn>=1&&currentColumn<m_iColoumCntX)
        {
            QTableWidgetItem* pitemCur=m_pTable->item(currentRow, currentColumn);
            QVariant var=pitemCur->data(Qt::UserRole);
            QVariant::Type type=var.type();
            Variant* pVariant=m_pVariant[currentColumn-1+currentRow*(m_iColoumCntX-1)];

            if(!pVariant->m_bReadOnly || HasFactoryPermission())
            {
                if(type==QVariant::Double||
                        type==QVariant::Int)
                {
                    int iSize=pVariant->m_slRange.size();
                    if(iSize>=2)
                    {
                        switch(type)
                        {
                        case QVariant::Double:
                        {
                            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(pitemCur->text(),m_pTable);
                            pLineEdit->SetRange(pVariant->m_slRange.at(0).toDouble(),
                                                pVariant->m_slRange.at(1).toDouble(),
                                                true);
                            m_pTable->setCellWidget(currentRow, currentColumn,pLineEdit);
                            break;
                        }
                        case QVariant::Int:
                        {
                            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(pitemCur->text(),m_pTable);
                            pLineEdit->SetRange(pVariant->m_slRange.at(0).toDouble(),
                                                pVariant->m_slRange.at(1).toDouble(),
                                                false);
                            m_pTable->setCellWidget(currentRow, currentColumn,pLineEdit);
                            break;
                        }
                        }
                    }
                }
                else if(type==QVariant::String)
                {
                    CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(pitemCur->text(),m_pTable);
                    pLineEdit->m_bNumOnly = false;
                    m_pTable->setCellWidget(currentRow, currentColumn,pLineEdit);
                }
                else if(type==QVariant::Bool)
                {
                    CCtrlCombo* pCombo=new CCtrlCombo(this);
                    pCombo->addItems(QStringList()<<QObject::trUtf8("no")<<QObject::trUtf8("yes"));
                    pCombo->setCurrentIndex(var.toBool()?1:0);
                    m_pTable-> setCellWidget(currentRow, currentColumn,pCombo);
                }
                else
                {
                    CCtrlCombo* pCombo=new CCtrlCombo(this);

                    Variant* pVariant=m_pVariant[currentColumn-1+currentRow*(m_iColoumCntX-1)];
                    pCombo->addItems(pVariant->m_slRange);
                    pCombo->setCurrentIndex(var.toUInt());
                    m_pTable-> setCellWidget(currentRow, currentColumn,pCombo);
                }
            }
        }
    }
}



