#include "slistwidget.h"

#include <QTableWidgetItem>

#include <QScrollBar>

#include "headertype1.h"

#include "mainwindow.h"

#include <QKeyEvent>
#include <QResizeEvent>

#include <QDebug>

SListWidget::SListWidget(QWidget *parent):QTableWidget(parent)
{
    //QTableView::setGridStyle(Qt::SolidLine);
    //Init();

    m_pSlideBarVertical = NULL;
    m_pSlideBarHorizontal = NULL;
    m_pSlideBarVerticalWidget = NULL;
    m_pSlideBarHorizontalWidget = NULL;

    m_nCurrentPos = 0;
    m_nTopDisplayIndex = 0;
    m_CurrentSingle = SINGLE_NONE;
    m_nMaxItemShow = 11;
    m_nRowHeight = 56;

    horizontalScrollBar()->hide();
    connect(&m_TimerScrollHorizontal, SIGNAL(timeout()), this, SLOT(ScrollUpdateHorizontal()));
}

SListWidget::~SListWidget()
{
    disconnect(&m_TimerScrollHorizontal, SIGNAL(timeout()), this, SLOT(ScrollUpdateHorizontal()));

    Clear();

    if(m_pHeaderType1!=NULL)
    {
        delete m_pHeaderType1;
        m_pHeaderType1 = NULL;
    }
}

void SListWidget::Init()
{
    QStringList tableHeader;
    tableHeader << "Name" << "Phone Number";

    setHorizontalHeaderLabels(tableHeader);

    setAlternatingRowColors(true);
    setStyleSheet("QTableWidget {alternate-background-color: #f9f9f9; background-color: #eeeff0; }");

    m_pHeaderType1 = new HeaderType1(Qt::Horizontal, this);
    setHorizontalHeader(m_pHeaderType1);

    connect(m_pHeaderType1, SIGNAL(sectionResized(int, int, int)), this, SLOT(sectionResizedHorizontal(int , int , int)));
    connect(m_pHeaderType1, SIGNAL(sectionHandleDoubleClicked(int)), this, SLOT(sectionHandleDoubleClickedHorizontal(int)));

    //MainWindow* pMainWindow = MainWindow::GetMainWindow();
    //connect(m_pHeaderType1, SIGNAL(Resort(int, int)), pMainWindow, SLOT(Test1(int, int)));

    {
        //://image/Arrow_top_2.png
        setSortingEnabled(false);
        m_pHeaderType1->setSortIndicatorShown(true);
        //pHeaderType1->setSortIndicator(0, Qt::DescendingOrder);
        //pHeaderType1->setSortIndicator(1, Qt::DescendingOrder);
        /*
        pHeaderType1->setStyleSheet(
                    "QTableWidget {background-color: transparent; border: none; outline: none;}"
                    "QHeaderView { border:none; border-bottom: 3px solid rgb(0, 160, 230); background: rgb(57, 58, 60); min-height: 60px;}"

                    "QHeaderView::section:horizontal { border:none; border-right: 1px solid white; color: white; background: transparent ; padding-left: 10px; }"
                    "QHeaderView::section:horizontal:hover { background: rgb(0, 160, 230);}"
                    "QHeaderView::section:horizontal:pressed { background: rgb(0, 180, 255);}"
                    "QHeaderView::up-arrow { width: 23px; height: 12px; padding-right: 25px; image: url(://image/Arrow_top_2.png); subcontrol-position: center right; }"
                    "QHeaderView::down-arrow { width: 23px; height: 12px; padding-right: 25px; image: url(://image/Arrow_under_2.png); subcontrol-position: center right; }");
                    */
    }



    this->setEditTriggers(NoEditTriggers);

    setGridStyle(Qt::SolidLine);
    //setSelectionMode(SelectionMode::SingleSelection);
    setSelectionMode(SelectionMode::ExtendedSelection);
    setSelectionBehavior(SelectionBehavior::SelectRows);
}


void SListWidget::SetData()
{
    //setColumnCount(2);



    int nData = 20;

    QTableWidgetItem* pItem = NULL;
    //setRowCount(30);
    setRowCount(nData);

    this->verticalHeader()->hide();
    //QScrollBar * pScrollVertical = this->verticalScrollBar();
    //QScrollBar * pScrollHorizontal = this->horizontalScrollBar();

    //ChangeHeader();

    //insertRow(0);

    Clear();

    int i=0;
    int j=0;



    for(i=0 ; i<nData ; i++)
    {
        setRowHeight(i, m_nRowHeight);
        RowData nRowData;
        /*
        bool bShow = true;
        if(bShow==true)
        {
            switch(i)
            {
            case 12:
                nRowData.push_back("2021/09/23");
                nRowData.push_back("2021/09/23 10:13:12");
                nRowData.push_back("12345678");
                nRowData.push_back("TEST1234");
                nRowData.push_back("2000/01/12");
                nRowData.push_back("80Kg");
                nRowData.push_back("Male");

            break;
            case 0:
                nRowData.push_back("2021/09/23");
                nRowData.push_back("2021/09/23 10:14:10");
                nRowData.push_back("PID234");
                nRowData.push_back("테스트234");
                nRowData.push_back("1999/11/12");
                nRowData.push_back("55Kg");
                nRowData.push_back("Female");
            break;
            case 1:
                nRowData.push_back("2021/09/23");
                nRowData.push_back("2021/09/23 10:15:02");
                nRowData.push_back("PID345");
                nRowData.push_back("테스트345");
                nRowData.push_back("1997/08/12");
                nRowData.push_back("");
                nRowData.push_back("Female");
            break;
            case 2:
                nRowData.push_back("2021/09/23");
                nRowData.push_back("2021/09/23 11:15:02");
                nRowData.push_back("PID567");
                nRowData.push_back("테스트567");
                nRowData.push_back("2002/01/23");
                nRowData.push_back("59Kg");
                nRowData.push_back("Male");
            break;
            case 4:

            break;
            case 5:

            break;
            case 6:

            break;
            case 7:

            break;
            case 8:

            break;
            case 9:

            break;
            }
        }
        */

        m_ListData.insert(pair<int,RowData>(i, nRowData));
    }

    for(i=0 ; i<nData ; i++)
    {
        //setRowHeight(i, m_nRowHeight);
    }

    map<int, RowData>::iterator it;
    int nDisplayIndex = 0;
    for(it=m_ListData.begin() ; it!=m_ListData.end() ; ++it)
    {
        int nIndex = it->first;
        RowData nRowData = it->second;
        for(j=0 ; j<nRowData.size() ; j++)
        {
            QString strData = nRowData[j];
            pItem = new QTableWidgetItem(strData);
            pItem->setTextAlignment(Qt::AlignCenter);
            setItem(nDisplayIndex, j, pItem);

            m_ListItem.push_back(pItem);
        }

        nDisplayIndex++;
    }


    setCurrentCell(-1, -1);


    //QSize rectSize = size();
    //QSize rectVerticalScroll = pScrollVertical->size();
    //QSize rectHorizontalScroll = pScrollHorizontal->size();

    //setColumnWidth(0, rectSize.width()-200);
    //setColumnWidth(1, 200);

    j=0;

    m_TimerScrollHorizontal.start(10);


}



void SListWidget::Resort(int nIndex, int nOrder)
{

}


HeaderType1* SListWidget::GetHeaderType1()
{
    return m_pHeaderType1;
}


void SListWidget::SingleDown()
{
    ListSingleDown();
    return;

    if(m_nTopDisplayIndex<30-(m_nMaxItemShow-1))
    {
        qDebug() << "SingleDown m_nTopDisplayIndex: " << m_nTopDisplayIndex;
        if(m_CurrentSingle!=SINGLE_DOWN)
        {
            //m_nTopDisplayIndex += 11;
            m_CurrentSingle = SINGLE_DOWN;
        }

        int nTmpCurrentItemPos = m_nTopDisplayIndex + 1;

        int nSlideMax = 199;
        int nRange = (30-m_nMaxItemShow);
        int nValue = (((float)nTmpCurrentItemPos / (float)nRange) *(float)nSlideMax +0.5f);

        if(nValue>nSlideMax)
        {
            nValue = nSlideMax;
        }
        else
        {
            m_nTopDisplayIndex = nTmpCurrentItemPos;
        }

        SetSlideValue(nValue);
        qDebug() << "value: " << nValue;
    }

}

void SListWidget::SingleUp()
{
    ListSingleUp();
    return;
    //int nCurrentPos = m_pSlideBarVertical->value();
    qDebug() << "SingleUp m_nTopDisplayIndex: " << m_nTopDisplayIndex;
    if(m_nTopDisplayIndex>0 && m_nTopDisplayIndex<=30-(m_nMaxItemShow-1))
    {
        QTableWidgetItem * pItem = NULL;

        if(m_CurrentSingle!=SINGLE_UP)
        {
            m_CurrentSingle = SINGLE_UP;
        }

        m_nTopDisplayIndex--;

        int nSlideMax = 99;
        int nRange = (30-m_nMaxItemShow);
        int nValue = (((float)m_nTopDisplayIndex / (float)nRange) *(float)nSlideMax +0.5f);
        //m_nCurrentPos = nValue;
        SetSlideValue(nValue);
    }
}

void SListWidget::valueChangedScrollBarVertical(int value)
{
    /*
    int nRange = (30-m_nMaxItemShow);

    int nRow = (((float)value/(float)99.0) * (float)(nRange) + 0.5f);
    QTableWidgetItem * pItem = NULL;

    int nCurrentPos = m_pSlideBarVertical->value();

    qDebug() << "m_nCurrentPos: " << m_nCurrentPos << " pos: " << nCurrentPos << " nRow: " << nRow;


    if(m_nCurrentPos<value)
    {
        pItem = item(nRow+(m_nMaxItemShow-1), 0);
        m_nTopDisplayIndex = nRow;// + 10;
    }
    else
    {
        pItem = item(nRow, 0);
        m_nTopDisplayIndex = nRow;
    }



    m_pSlideBarVertical->setValue(nCurrentPos);

    scrollToItem(pItem);

    m_nCurrentPos = value;
    */
    QScrollBar* pScrollBar = verticalScrollBar();
    float fMaxValue = 199.0f;
    float fMaxScrollBar = 0.0f;

    if(pScrollBar!=NULL)
    {
        fMaxScrollBar = pScrollBar->maximum();
        int nScrollOffset = ((((float)value)/fMaxValue) * fMaxScrollBar + 0.5f);

        if(nScrollOffset>fMaxScrollBar)
        {
            nScrollOffset = fMaxScrollBar;
        }
        pScrollBar->hide();
        pScrollBar->setValue(nScrollOffset);
    }

}

void SListWidget::valueChangedScrollBarHorizontal(int value)
{
    float fMaxValue = 199.0f;
    float fMaxScrollBar = horizontalScrollBar()->maximum();
    int nScrollOffset = ((((float)value)/fMaxValue) * fMaxScrollBar + 0.5f);

    if(nScrollOffset>fMaxScrollBar)
    {
        nScrollOffset = fMaxScrollBar;
    }

    QScrollBar* pScrollBar = horizontalScrollBar();
    if(pScrollBar!=NULL)
    {
        pScrollBar->hide();

        pScrollBar->setValue(nScrollOffset);
    }

    //m_TimerScrollHorizontal.start(10);
}


void SListWidget::keyPressEvent(QKeyEvent *event)
{
    qDebug() << event->key();
    parent()->event(event);
}


void SListWidget::SetSlideBarVertical(SSlideBar* pSlideBar, QFrame* pFrame)
{
    m_pSlideBarVertical = pSlideBar;
    m_pSlideBarVerticalWidget = pFrame;
    m_pSlideBarVertical->SetSlideType(SSlideBar::SLIDE_VERTICAL);
    m_pSlideBarVertical->setRange(0, 199);
}

void SListWidget::SetSlideBarHorizontal(SSlideBar* pSlideBar, QFrame* pFrame)
{
    m_pSlideBarHorizontal = pSlideBar;
    m_pSlideBarHorizontalWidget = pFrame;
    m_pSlideBarHorizontal->SetSlideType(SSlideBar::SLIDE_HORIZONTAL);
    m_pSlideBarHorizontal->setRange(0, 199);
}


void SListWidget::wheelEvent(QWheelEvent *event)
{
    int nDelta = event->delta();
    if(nDelta<0)
    {
        SingleDown();
    }
    else if(nDelta>0)
    {
        SingleUp();
    }

    int nValue = m_pSlideBarVertical->value();

    //m_pSlideBarVertical->wheelEvent(event);
}

void SListWidget::ChangeHeader()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    //map<int, int>* pMapHistoryIndex = pCommonData->GetMapHistoryIndex();
    //pMapHistoryIndex->clear();

    STableHeader* pTableHeader = NULL;

    if(m_nListType==SWIDGET_WORKLIST)
    {
        pTableHeader = pCommonData->GetWorklistTableHeader();
    }
    else if(m_nListType==SWIDGET_HISTORY)
    {
        pTableHeader = pCommonData->GetHistoryTableHeader();
    }
    else
    {
        return;
    }

    vector<TableHeader*>* pListTableHeader = pTableHeader->GetListActive();

    if(pListTableHeader->size()==0)
    {
        return;
    }

    int i=0;
    QStringList tableHeader;
    for(i=0 ; i<pListTableHeader->size() ; i++)
    {
        TableHeader* pHeader = (*pListTableHeader)[i];
        tableHeader << pHeader->m_strText;

        //pMapHistoryIndex->insert(map<int, int>::value_type(i, i));

        setColumnWidth(i, pHeader->m_nWidth);
    }
    setHorizontalHeaderLabels(tableHeader);


    setColumnCount(pListTableHeader->size());
    m_pHeaderType1->ChangeHeader(tableHeader);

    for(i=0 ; i<tableHeader.size() ; i++)
    {
        //setColumnWidth(i, 150);
    }

    if(isActiveWindow()==false)
    {

    }

    if(m_pHeaderType1->isActiveWindow()==false)
    {

    }

    m_pHeaderType1->show();
    m_pHeaderType1->update();
    update();

}

void SListWidget::Clear()
{
    int i=0;
    int j=0;

    map<int, RowData>::iterator it;
    int nDisplayIndex = 0;
    for(it=m_ListData.begin() ; it!=m_ListData.end() ; ++it)
    {
        int nIndex = it->first;
        RowData nRowData = it->second;
        for(j=0 ; j<nRowData.size() ; j++)
        {
            QString strData = nRowData[j];            
        }

        nDisplayIndex++;
    }

    m_ListData.clear();

    for(i=0 ; i<m_ListItem.size() ; i++)
    {
        QTableWidgetItem* pItem = m_ListItem[i];
        SAFE_DELETE(pItem);
    }
    m_ListItem.clear();

    setCurrentCell(-1, -1);
}

void SListWidget::UpdateWorklist()
{
    setSortingEnabled(false);

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<WorklistResponseData>* pListWorklist = pCommonData->GetWorklistData();
    vector<TableHeader*>* pListActivate = pCommonData->GetWorklistTableHeader()->GetListActive();

    Clear();

    int nRow = m_nMaxItemShow;
    if(nRow<pListWorklist->size())
    {
        nRow = pListWorklist->size();
    }
    setRowCount(nRow);

    int nFontSize = 18;
    int nDisplayType = MainWindow::GetCommonData()->GetDisplayType();
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        nFontSize = 14;
    }

    int i=0;
    for(i=0 ; i<pListWorklist->size() ; i++)
    {
        WorklistResponseData& pWorklistData = (*pListWorklist)[i];
        int j=0;
        vector<TableHeader*>::iterator it;
        for(it=pListActivate->begin(), j=0 ; it!=pListActivate->end() ; ++it, j++)
        {
            TableHeader* pTableHeader = (*it);
            int nIndex = pTableHeader->m_nIndex;
            int nDefaultIndex = pTableHeader->m_nIndexDefault;
            QString strData = "";

            if(nDefaultIndex==SDI_PATIENT_ID)
            {
                strData = pWorklistData.m_Patient.strPatientID;
            }
            else if(nDefaultIndex==SDI_PATIENT_NAME)
            {
                strData = pWorklistData.m_Patient.strPatientName;
            }
            else if(nDefaultIndex==SDI_PATIENT_SEX)
            {
                strData = pWorklistData.m_Patient.strPatientSex;
            }
            else if(nDefaultIndex==SDI_PATIENT_WEIGHT)
            {
                strData = pWorklistData.m_Patient.strPatientWeight;
            }
            else if(nDefaultIndex==SDI_PATIENT_AGE)
            {
                strData = pWorklistData.m_Patient.strPatientAge;
            }
            else if(nDefaultIndex==SDI_PATIENT_LOCATION)
            {
                strData = pWorklistData.m_Patient.strPatientLocation;
            }
            else if(nDefaultIndex==SDI_PATIENT_BIRTH_DATE)
            {
                strData = pWorklistData.m_Patient.strPatientBirthDate;
                ChangeDateDisplayFormat(strData);
            }
            else if(nDefaultIndex==SDI_STUDY_DATE)
            {
                strData = pWorklistData.m_Study.strStudyDate;
                ChangeDateDisplayFormat(strData);
            }
            else if(nDefaultIndex==SDI_STUDY_TIME)
            {
                strData = pWorklistData.m_Study.strStudyTime;
                ChangeTimeDisplayFormat(strData);
            }
            else if(nDefaultIndex==SDI_STUDY_DESCRIPTION)
            {
                strData = pWorklistData.m_Study.strStudyDescription;
            }
            else if(nDefaultIndex==SDI_SERIES_OPERATOR)
            {
                strData = pWorklistData.m_Study.strReferringPhysiciansName;
            }
            else if(nDefaultIndex==SDI_STUDY_CREATE_DATE)
            {
                strData = "";
            }
            else if(nDefaultIndex==SDI_ACCESSION_NUMBER)
            {
                strData = pWorklistData.m_Study.strAccessionNumber;
            }
            else if(nDefaultIndex==SDI_STUDY_INSTANCE_UID)
            {
                strData = pWorklistData.m_Study.strStudyInstanceUID;
            }
            else if(nDefaultIndex==SDI_SERIES_BODYPART)
            {
                strData = pWorklistData.m_Series.strBodyPartExamined;
            }
            else if(nDefaultIndex==SDI_SERIES_MODALITY)
            {
                strData = pWorklistData.m_Series.strModality;
            }
            else if(nDefaultIndex==SDI_SERIES_DESCRIPTION)
            {
                strData = pWorklistData.m_Series.strSeriesDescription;
            }
            else if(nDefaultIndex==SDI_SERIES_INSTANCE_UID)
            {
                strData = pWorklistData.m_Series.strSeriesInstanceUID;
            }
            else if(nDefaultIndex==SDI_SCHEDULED_DATE)
            {
                strData = pWorklistData.m_ScheduledData.strScheduledProcedureStepStartDate;
                ChangeDateDisplayFormat(strData);
            }
            else if(nDefaultIndex==SDI_SCHEDULED_TIME)
            {
                strData = pWorklistData.m_ScheduledData.strScheduledProcedureStepStartTime;
                ChangeTimeDisplayFormat(strData);
            }
            else if(nDefaultIndex==SDI_SCHEDULED_PERFORMING_PHYSICIAN_NAME)
            {
                strData = pWorklistData.m_ScheduledData.strScheduledPerformingPhysicianName;
            }
            else if(nDefaultIndex==SDI_SCHEDULED_CODE_VALUE)
            {
                strData = pWorklistData.m_ScheduledData.strScheduledCodeValue;
            }
            else if(nDefaultIndex==SDI_INDEX)
            {
                strData = QString::number(i+1);
            }

            QTableWidgetItem* pItem = new QTableWidgetItem(strData);
            pItem->setTextAlignment(Qt::AlignCenter);
            QFont font("Roboto");
            font.setPixelSize(nFontSize);
            pItem->setFont(font);
            setItem(i, nIndex, pItem);

            m_ListItem.push_back(pItem);
        }
        setRowHeight(i, m_nRowHeight);
    }

    pCommonData->ReleaseWorklistData();

    setCurrentCell(-1, -1);
}

void SListWidget::UpdateHistoryList()
{
    setSortingEnabled(false);

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<HISTORY_STUDY*>* pListHistoryStudy = pCommonData->GetHistorySearchResult();
    vector<TableHeader*>* pListActivate = pCommonData->GetHistoryTableHeader()->GetListActive();

    Clear();

    /*
    //Study Date, Creation DateTime, Patient ID, Patient Name, Patient BirthDate, Patient Weight, PatientSex
    //8,          7,                 2,          3,            5,               , 6                    , 4
    map<int, int> mapData;
    mapData.insert(map<int, int>::value_type(0, 8));
    mapData.insert(map<int, int>::value_type(1, 7));
    mapData.insert(map<int, int>::value_type(2, 2));
    mapData.insert(map<int, int>::value_type(3, 3));
    mapData.insert(map<int, int>::value_type(4, 5));
    mapData.insert(map<int, int>::value_type(5, 6));
    mapData.insert(map<int, int>::value_type(6, 4));
    */

    int nRow = m_nMaxItemShow;
    if(nRow<pListHistoryStudy->size())
    {
        nRow = pListHistoryStudy->size();
    }
    setRowCount(nRow);

    int nFontSize = 18;
    int nDisplayType = MainWindow::GetCommonData()->GetDisplayType();
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        nFontSize = 14;
    }

    int i=0;
    for(i=0 ; i<pListHistoryStudy->size() ; i++)
    {
        HISTORY_STUDY* pHistoryStudy = (*pListHistoryStudy)[i];
        int j=0;
        vector<TableHeader*>::iterator it;
        int nDisplayIndex = 0;
        for(it=pListActivate->begin() ; it!=pListActivate->end() ; ++it)
        {
            TableHeader* pTableHeader = (*it);
            int nIndex = pTableHeader->m_nIndex;
            int nDefaultIndex = pTableHeader->m_nIndexDefault;
            QString strData = "";

            //pHistoryStudy->GetData(nDefaultIndex);


            //if(nIndex<pHistoryStudy->m_List.size())
            {
                strData = pHistoryStudy->GetData(nDefaultIndex);

                if(nDefaultIndex==SDI_INDEX)
                {


                    strData = pHistoryStudy->GetData(nDefaultIndex);

                }
                else if(nDefaultIndex==SDI_PATIENT_SEX)
                {
                }
                else if(nDefaultIndex==SDI_PATIENT_WEIGHT)
                {
                    strData += " Kg";
                }
                else if(nDefaultIndex==SDI_PATIENT_BIRTH_DATE)
                {
                    ChangeDateDisplayFormat(strData);
                }
                else if(nDefaultIndex==SDI_STUDY_DATE)
                {
                    ChangeDateDisplayFormat(strData);
                }
                else if(nDefaultIndex==SDI_STUDY_TIME)
                {
                    ChangeTimeDisplayFormat(strData);
                }
                else if(nDefaultIndex==SDI_SCHEDULED_DATE)
                {
                    ChangeDateDisplayFormat(strData);
                }
                else if(nDefaultIndex==SDI_SCHEDULED_TIME)
                {
                    ChangeTimeDisplayFormat(strData);
                }



/*
                QString* pData = pHistoryStudy->m_List[nDefaultIndex];
                strData = *pData;

                if(nDisplayIndex==6)
                {

                }
                else if(nDisplayIndex==5)
                {

                }
                */
            }

            QTableWidgetItem* pItem = new QTableWidgetItem(strData);
            pItem->setTextAlignment(Qt::AlignCenter);
            QFont font("Roboto");
            font.setPixelSize(nFontSize);
            pItem->setFont(font);
            setItem(i, nDisplayIndex, pItem);

            m_ListItem.push_back(pItem);


            nDisplayIndex++;
        }

        setRowHeight(i, m_nRowHeight);
    }

    setCurrentCell(-1, -1);
}

void SListWidget::resizeEvent(QResizeEvent *event)
{
    QSize nSize = size();

    int nDisplayType = MainWindow::GetCommonData()->GetDisplayType();

    int nFontSize = 24;
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        m_nMaxItemShow = 13;
        m_nRowHeight = 46;
    }
    else if(nDisplayType==SDISPLAY_1920X1080)
    {
        m_nMaxItemShow = 11;
        m_nRowHeight = 56;
    }

    SetData();


}

void SListWidget::sectionResizedHorizontal(int logicalIndex, int oldSize, int newSize)
{
    QSize nSize = m_pHeaderType1->size();

    int nWidth = m_pHeaderType1->GetDisplayWidth();

    if(nWidth > nSize.width()+10)
    {
        m_pSlideBarHorizontalWidget->show();
    }
    else
    {
        m_pSlideBarHorizontalWidget->hide();
    }
}

void SListWidget::ScrollUpdateHorizontal()
{
    QSize nSize = m_pHeaderType1->size();

    int nWidth = m_pHeaderType1->GetDisplayWidth();

    if(nWidth > nSize.width()+10)
    {
        m_pSlideBarHorizontalWidget->show();
    }
    else
    {
        m_pSlideBarHorizontalWidget->hide();
    }

    m_TimerScrollHorizontal.stop();
}

void SListWidget::sectionHandleDoubleClickedHorizontal(int logicalIndex)
{
    QSize nSize = m_pHeaderType1->size();

    int nWidth = m_pHeaderType1->GetDisplayWidth();

    m_TimerScrollHorizontal.start(100);
}

void SListWidget::SetMaximumVisibleRow(int nRow)
{
    m_nMaxItemShow = nRow;
}

void SListWidget::CheckHorizontalScroll()
{
    m_TimerScrollHorizontal.start(100);
}


void SListWidget::SetListType(int nType)
{
    m_nListType = nType;

    if(nType==SWIDGET_HISTORY)
    {
        setSelectionMode(SelectionMode::ExtendedSelection);
    }
    else if(nType==SWIDGET_WORKLIST)
    {
        setSelectionMode(SelectionMode::SingleSelection);
    }

    //


    m_pHeaderType1->SetListType(nType);
}

void SListWidget::ChangeDateDisplayFormat(QString& strData)
{
    if(strData.length()>0)
    {
        strData.insert(6, '/');
        strData.insert(4, '/');
    }
}

void SListWidget::ChangeTimeDisplayFormat(QString& strData)
{
    if(strData.length()>0)
    {
        strData.insert(4, ':');
        strData.insert(2, ':');
    }
}


void SListWidget::InitHeader()
{
    if(m_pHeaderType1!=NULL)
    {
        m_pHeaderType1->InitHeader();
    }
}


int SListWidget::GetSelectIndexWorklist(int nRow)
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<TableHeader*>* pListActivate = pCommonData->GetWorklistTableHeader()->GetListActive();

    int j=0;
    vector<TableHeader*>::iterator it;
    int nFindRowIndex = -1;
    for(it=pListActivate->begin(), j=0 ; it!=pListActivate->end() ; ++it, j++)
    {
        TableHeader* pTableHeader = (*it);
        int nIndex = pTableHeader->m_nIndex;
        int nDefaultIndex = pTableHeader->m_nIndexDefault;

        if(nDefaultIndex==SDI_INDEX)
        {
            QTableWidgetItem* pItem = item(nRow, j);
            QString strData = pItem->text();
            nFindRowIndex = strData.toInt();
            if(nFindRowIndex>0)
            {
                nFindRowIndex = nFindRowIndex - 1;
            }
            break;
        }
    }

    return nFindRowIndex;
}

void SListWidget::SetResizeEnableHeader(bool bResize)
{
    m_pHeaderType1->SetResizeEnable(bResize);
}
