#include "formlistwidgetworklist.h"
#include "mainwindow.h"

FormListWidgetWorklist::FormListWidgetWorklist(QWidget *parent) : FormListWidget(parent)
{
    connect(m_pListWidget, SIGNAL(ListSingleDown()), this, SLOT(ListSingleDown()));
    connect(m_pListWidget, SIGNAL(ListSingleUp()), this, SLOT(ListSingleUp()));

    m_nMaxItemShow = 10;
    m_pListWidget->SetListType(SWIDGET_WORKLIST);
}

FormListWidgetWorklist::~FormListWidgetWorklist()
{
    disconnect(m_pListWidget, SIGNAL(ListSingleDown()), this, SLOT(ListSingleDown()));
    disconnect(m_pListWidget, SIGNAL(ListSingleUp()), this, SLOT(ListSingleUp()));

    m_pListWidget->Clear();
}

void FormListWidgetWorklist::ExecuteMenu()
{
    ChangeMenu(SMENU_CAPTURE);
}

void FormListWidgetWorklist::CellDoubleClicked(int nRow, int nColumn)
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<WorklistResponseData>* pListWorklist = pCommonData->GetWorklistData();

    if(m_bExecMenu==false)
    {
#ifdef _TEST_CODE

#else
        if(nRow<pListWorklist->size())
        {


            int nSelectRow = GetSelectIndexWorklist(nRow);

            if(nSelectRow<0)
            {
                nSelectRow = nRow;
            }

            WorklistResponseData rd = (*pListWorklist)[nSelectRow];

            pCommonData->ReleaseWorklistData();

            ACQUISITION_INFO* pAcquisitionInfo = pCommonData->GetAcquisitionInfo();
            pAcquisitionInfo->SetDataPatient(rd.m_Patient);
            pAcquisitionInfo->SetDataStudy(rd.m_Study);
            pAcquisitionInfo->SetDataSeries(rd.m_Series);
            pAcquisitionInfo->SetDataImage(rd.m_Image);
            pAcquisitionInfo->SetDataScheduledData(rd.m_ScheduledData);
            pCommonData->ReleaseAcquisitionInfo();

            m_bExecMenu = true;
            ExecuteMenu();

            return;
        }
#endif
    }
    else
    {
        pCommonData->ReleaseWorklistData();
    }
}

void FormListWidgetWorklist::Init()
{
    FormListWidget::Init();
    m_bExecMenu = false;
    m_pVerticalScrollBarWidget->hide();
    m_pHorizontalScrollBarWidget->hide();

    m_pListWidget->InitHeader();
    m_pListWidget->setCurrentCell(-1, -1);
    m_pListWidget->CheckHorizontalScroll();

    setFocus(Qt::OtherFocusReason);
    m_pListWidget->setFocus(Qt::OtherFocusReason);
}

void FormListWidgetWorklist::Clear()
{
    m_pListWidget->Clear();

    m_pListWidget->update();
}

void FormListWidgetWorklist::UpdateList()
{
    m_pListWidget->UpdateWorklist();

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

    int nSizeWorklist = pListWorklist->size();
    pCommonData->ReleaseWorklistData();

    if(nSizeWorklist>m_nMaxItemShow)
    {
        m_pVerticalScrollBar->setValue(0);
        m_pVerticalScrollBarWidget->show();
    }
    else
    {
        m_pVerticalScrollBarWidget->hide();
    }

    m_pListWidget->CheckHorizontalScroll();

}

void FormListWidgetWorklist::ListSingleDown()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    QScrollBar* pScrollBar = m_pListWidget->verticalScrollBar();
    float fMaxValue = 199.0f;
    float fMaxScrollBar = 0.0f;

    vector<HISTORY_STUDY*>* pListHistoryStudy = pCommonData->GetHistorySearchResult();
    int nHistoryCount = pListHistoryStudy->size();
    int nRemain = nHistoryCount - m_nMaxItemShow;

    float fRatio = (float)fMaxValue / (float)nRemain + 1.5f;

    int nValue = m_pVerticalScrollBar->value();
    nValue = nValue + fRatio;
    if(nValue > fMaxValue)
    {
        nValue = (int)fMaxValue;
    }

    m_pVerticalScrollBar->setValue(nValue);
}

void FormListWidgetWorklist::ListSingleUp()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    QScrollBar* pScrollBar = m_pListWidget->verticalScrollBar();
    float fMaxValue = 199.0f;
    float fMaxScrollBar = 0.0f;

    vector<HISTORY_STUDY*>* pListHistoryStudy = pCommonData->GetHistorySearchResult();
    int nHistoryCount = pListHistoryStudy->size();
    int nRemain = nHistoryCount - m_nMaxItemShow;

    float fRatio = (float)fMaxValue / (float)nRemain + 1.5f;

    int nValue = m_pVerticalScrollBar->value();
    nValue = nValue - fRatio;
    if(nValue < 0)
    {
        nValue = 0;
    }

    m_pVerticalScrollBar->setValue(nValue);
}


void FormListWidgetWorklist::keyReleaseEvent(QKeyEvent *event)
{
    SThreadImageSave* pThread = MainWindow::GetCommonData()->GetThreadCaptureImage();

    int nKey = event->key();

    if(nKey==Qt::Key_F10)
    {
        pThread->MissCapture();
    }
    else if(nKey==Qt::Key_F11)
    {
        pThread->MissCapture();
    }
}


void FormListWidgetWorklist::SetExecMenu(bool bExecMenu)
{
    m_bExecMenu = bExecMenu;
}
