#include "formlistwidget.h"
#include "ui_formlistwidget.h"
#include <QWheelEvent>

#include "mainwindow.h"

FormListWidget::FormListWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormListWidget)
{
    ui->setupUi(this);

    m_nCurrentRowIndex = -1;

    m_pListWidget = ui->tableWidget;
    m_pHorizontalScrollBarWidget = ui->frame_horizontalScrollBar;
    m_pVerticalScrollBarWidget = ui->frame_verticalScrollBar;
    m_pHorizontalScrollBar = ui->horizontalScrollBar;
    m_pVerticalScrollBar = ui->verticalScrollBar;

    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    connect(this, SIGNAL(ChangeMenu(int)), pMainWindow, SLOT(ChangeMenu(int)));


    ui->tableWidget->setGeometry(0, 0, 1750, 640-25);
    ui->tableWidget->Init();
    ui->tableWidget->SetData();

    m_pVerticalScrollBar->SetSlideType(SSlideBar::SLIDE_VERTICAL);
    m_pVerticalScrollBar->setMinimum(0);
    m_pVerticalScrollBar->setMaximum(99);

    m_pHorizontalScrollBar->SetSlideType(SSlideBar::SLIDE_HORIZONTAL);
    m_pHorizontalScrollBar->setMinimum(0);
    m_pHorizontalScrollBar->setMaximum(599);

    ui->tableWidget->SetSlideBarVertical(m_pVerticalScrollBar, ui->frame_verticalScrollBar);

    ui->tableWidget->SetSlideBarHorizontal(m_pHorizontalScrollBar, ui->frame_horizontalScrollBar);


    connect(m_pVerticalScrollBar, SIGNAL(valueChanged(int)), ui->tableWidget, SLOT(valueChangedScrollBarVertical(int)));
    connect(m_pHorizontalScrollBar, SIGNAL(valueChanged(int)), ui->tableWidget, SLOT(valueChangedScrollBarHorizontal(int)));

    connect(ui->tableWidget, SIGNAL(SetSlideValue(int)), m_pVerticalScrollBar, SLOT(setValue(int)));

    connect(ui->tableWidget->GetHeaderType1(), SIGNAL(Resort(int, int)), ui->tableWidget, SLOT(Resort(int, int)));

    connect(m_pVerticalScrollBar, SIGNAL(actionTriggered(int)), this, SLOT(actionTriggered(int)));
    connect(m_pVerticalScrollBar, SIGNAL(SingleDown()), ui->tableWidget, SLOT(SingleDown()));
    connect(m_pVerticalScrollBar, SIGNAL(SingleUp()), ui->tableWidget, SLOT(SingleUp()));


    connect(m_pHorizontalScrollBar, SIGNAL(actionTriggered(int)), this, SLOT(actionTriggered(int)));



    //m_pVerticalScrollBar->setGeometry(1750-25, 60, 25, 555);


    ui->frame_verticalScrollBar->setGeometry(1750-26, 60, 25, 555);
    m_pVerticalScrollBar->setGeometry(0, 0, 25, 555);

    ui->frame_horizontalScrollBar->setGeometry(0, 640-26, 1750, 25);
    m_pHorizontalScrollBar->setGeometry(0, 0, 1750-1, 25);






    Init();
}

FormListWidget::~FormListWidget()
{
    ui->tableWidget->Clear();
    //MainWindow* pMainWindow = MainWindow::GetMainWindow();
    //disconnect(this, SIGNAL(ChangeMenu(int)), pMainWindow, SLOT(ChangeMenu(int)));
    delete ui;
}

void FormListWidget::actionTriggered(int action)
{

}


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

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

    int nFontSize = 24;
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        ui->tableWidget->setGeometry(0, 0, nSize.width(), nSize.height()-20);

        ui->frame_verticalScrollBar->setGeometry(nSize.width()-20, 50, 20, nSize.height()-20-50);
        m_pVerticalScrollBar->setGeometry(0, 0, 20, nSize.height()-20-50);

        ui->frame_horizontalScrollBar->setGeometry(0, nSize.height()-20, 1750, 20);
        m_pHorizontalScrollBar->setGeometry(0, 0, nSize.width()-1, 20);


    }
    else if(nDisplayType==SDISPLAY_1920X1080)
    {
        ui->tableWidget->setGeometry(0, 0, nSize.width(), nSize.height()-25);

        ui->frame_verticalScrollBar->setGeometry(nSize.width()-26, 60, 25, nSize.height()-25-60);
        m_pVerticalScrollBar->setGeometry(0, 0, 25, nSize.height()-25-60);

        ui->frame_horizontalScrollBar->setGeometry(0, nSize.height()-26, 1750, 25);
        m_pHorizontalScrollBar->setGeometry(0, 0, nSize.width()-1, 25);
    }

    //m_pListWidget->resize(nSize);


}

void FormListWidget::keyPressEvent(QKeyEvent *event)
{

}

void FormListWidget::keyReleaseEvent(QKeyEvent *event)
{

}

void FormListWidget::wheelEvent(QWheelEvent *event)
{

}

void FormListWidget::on_tableWidget_itemDoubleClicked(QTableWidgetItem *item)
{
    //CellDoubleClicked(item->row(), 0);
    //return;

    int nRow = item->row();
    if(m_nCurrentRowIndex!=nRow)
    {
        CellDoubleClicked(nRow, 0);
        m_nCurrentRowIndex = nRow;
    }

    /*
    m_nCurrentRowIndex = item->row();
    CommonData* pCommonData = MainWindow::GetCommonData();
    pCommonData->SetCurrentHistoryStudy(m_nCurrentRowIndex);

    ExecuteMenu();
    */

}

void FormListWidget::on_tableWidget_itemSelectionChanged()
{
    //m_nCurrentRowIndex = ui->tableWidget->currentRow();
}

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

void FormListWidget::ChangeHeader()
{
    ui->tableWidget->SetResizeEnableHeader(false);
    ui->tableWidget->ChangeHeader();
    ui->tableWidget->SetResizeEnableHeader(true);
}

int FormListWidget::GetCurrentRowIndex()
{
    return m_nCurrentRowIndex;
}

void FormListWidget::on_tableWidget_cellDoubleClicked(int row, int column)
{
    //if(m_nCurrentRowIndex!=row)
    {
        CellDoubleClicked(row, column);
        m_nCurrentRowIndex = row;
    }
}

void FormListWidget::CellDoubleClicked(int nRow, int nColumn)
{
    if(m_bDoubleClicked==false)
    {

    }
    m_bDoubleClicked = true;
}

void FormListWidget::Init()
{
    m_bDoubleClicked = false;
    m_nCurrentPos = 0;
    m_nCurrentRowIndex = -1;

    m_pHorizontalScrollBarWidget->hide();
}

int FormListWidget::GetSelectIndexWorklist(int nRow)
{
    return ui->tableWidget->GetSelectIndexWorklist(nRow);
}

void FormListWidget::on_tableWidget_cellClicked(int row, int column)
{
    int a=0;
}

void FormListWidget::on_tableWidget_itemClicked(QTableWidgetItem *item)
{
    int a=0;

    int nRow = item->row();
    if(m_nCurrentRowIndex!=nRow)
    {
        //CellDoubleClicked(nRow, 0);
        m_nCurrentRowIndex = nRow;
    }
    else
    {
        CellDoubleClicked(nRow, 0);
    }
}
