#include "sslidebar.h"

#include <QDebug>

#include <QMouseEvent>

#include <QWheelEvent>

SSlideBar::SSlideBar(QWidget *parent) : QScrollBar(parent)
{
    m_bPressed = false;
    m_bListUp = false;

    connect(&m_TimerPressed, SIGNAL(timeout()), this, SLOT(PressedTimer()));
}

SSlideBar::SSlideBar(Qt::Orientation orient, QWidget *parent) : QScrollBar(orient, parent)
{

}

SSlideBar::~SSlideBar()
{

}


bool SSlideBar::event(QEvent* pEvent)
{
    bool bRet = QScrollBar::event(pEvent);

    //qDebug() << pEvent->type();


    return bRet;
}

void SSlideBar::triggerAction(SliderAction action)
{

}


void SSlideBar::mousePressEvent(QMouseEvent *pEvent)
{

    grabMouse();


    int nY = pEvent->y();
    int nX = pEvent->x();

    QSize nSize = size();

    if(m_nSlideType==SLIDE_VERTICAL)
    {
        if(nY < nSize.height()-18 && nY>18)
        {
            QScrollBar::mousePressEvent(pEvent);
            m_bPressed = true;
        }
        else
        {
            m_bPressed = false;
            if(nY<18)
            {
                m_bListUp = true;
            }
            else if(nY>nSize.height()-18)
            {
                m_bListUp = false;
            }

            //qDebug() << "mousePressEvent";

            //PressedTimer();

            m_TimerPressed.start(100);
        }
    }
    else if(m_nSlideType==SLIDE_HORIZONTAL)
    {
        if(nX < nSize.width()-18 && nX>18)
        {
            QScrollBar::mousePressEvent(pEvent);
            m_bPressed = true;
        }
        else
        {
            m_bPressed = false;
            if(nX<18)
            {
                m_bListLeft = true;
            }
            else if(nX>nSize.width()-18)
            {
                m_bListLeft = false;
            }
        }

        //qDebug() << "mousePressEvent";

        //PressedTimer();

        m_TimerPressed.start(100);
    }



}

void SSlideBar::mouseReleaseEvent(QMouseEvent *pEvent)
{
    releaseMouse();

    m_TimerPressed.stop();


    int nY = pEvent->y();
    int nX = pEvent->x();

    QSize nSize = size();

    if(m_nSlideType==SLIDE_VERTICAL)
    {
        if(nY < nSize.height()-18 && nY>18)
        {
            QScrollBar::mouseReleaseEvent(pEvent);
        }
        else if(nY<18)
        {

        }
        else if(nY>nSize.height()-18)
        {

        }
    }
    else if(m_nSlideType==SLIDE_HORIZONTAL)
    {
        if(nX < nSize.width()-18 && nX>18)
        {
            QScrollBar::mouseReleaseEvent(pEvent);
        }
        else if(nX<18)
        {
            SingleUp();
        }
        else if(nX>nSize.height()-18)
        {
            SingleDown();
        }
    }



    m_bPressed = false;





    qDebug() << "Released";
}

void SSlideBar::mouseMoveEvent(QMouseEvent *pEvent)
{
    if(m_bPressed==true)
    {
        QScrollBar::mouseMoveEvent(pEvent);
    }
}

void SSlideBar::PressedTimer()
{
    //qDebug() << "PressedTimer();";
    //m_TimerPressed.stop();

    MoveScroll();

    //m_TimerPressed.start(100);
}

void SSlideBar::MoveScroll()
{

    if(m_bListUp==true)
    {
        if(m_nSlideType==SLIDE_HORIZONTAL)
        {

        }
        else if(m_nSlideType==SLIDE_VERTICAL)
        {
            SingleUp();
        }

    }
    else
    {
        if(m_nSlideType==SLIDE_HORIZONTAL)
        {

        }
        else if(m_nSlideType==SLIDE_VERTICAL)
        {
            SingleDown();
        }

    }




    if(m_bListUp==true)
    {
        SingleUp();
    }
    else
    {
        SingleDown();
    }
}

void SSlideBar::wheelEvent(QWheelEvent *pEvent)
{
    QScrollBar::wheelEvent(pEvent);
}

void SSlideBar::SetSlideType(SLIDEBAR_TYPE nType)
{
    m_nSlideType = nType;
}
