#include "cctrlgraphbutton.h"

CCtrlGraphButton::CCtrlGraphButton()
{
    m_iBtnType=STR;
    m_rect=QRect(0,0,80,60);
    m_pressColor=Qt::green;
    m_releaseColor=Qt::gray;
    m_bChecked=false;
    m_iState=RELEASE;
    m_bBtnDirectY=false;
}

CCtrlGraphButton::CCtrlGraphButton(int iBtnType, bool bDirectY)
{
    m_iBtnType=iBtnType;
    m_rect=QRect(0,0,80,60);
    m_pressColor=Qt::green;
    m_releaseColor=Qt::gray;
    m_bChecked=false;
    m_iState=RELEASE;
    m_bBtnDirectY=bDirectY;
}

CCtrlGraphButton::CCtrlGraphButton(QString &str, bool bDirectY)
{
    m_iBtnType=STR;
    m_str=str;
    m_rect=QRect(0,0,80,60);
    m_pressColor=Qt::green;
    m_releaseColor=Qt::gray;
    m_bChecked=false;
    m_iState=RELEASE;
    m_bBtnDirectY=bDirectY;
}

void CCtrlGraphButton::DrawButton(QPainter &painter)
{
    QColor bkColor;
    switch(m_iState)
    {
        case PRESS:bkColor=m_pressColor;break;
        case RELEASE:
        bkColor=m_releaseColor;
        if(m_bChecked)
        {
            bkColor=m_pressColor;
        }
        break;
    }

    bkColor.setAlpha(150);
    painter.fillRect(m_rect,bkColor);
//    QPen pen=painter.pen();
//    painter.setPen(QPen(m_releaseColor,2));
//    painter.setPen(pen);
//    painter.drawRect(m_rect);

    QRect rect,rectLeft,rectRight,rectTop,rectBottom;
    rect=m_rect;
    rect.adjust(2,2,-2,-2);
    rectLeft=rect;
    rectLeft.setRight(rect.left()+rect.width()/2);
    rectRight=rect;
    rectRight.setLeft(rect.right()-rect.width()/2);
    rectTop=rect;
    rectTop.setBottom(rect.top()+rect.height()/2);
    rectBottom=rect;
    rectBottom.setTop(rect.bottom()-rect.height()/2);
    switch(m_iBtnType)
    {
    case STR:
        if(!m_bBtnDirectY)
        {
            painter.setPen(Qt::black);
            painter.drawText(rect, Qt::AlignCenter,
                             m_str);

        }
        else
        {
            painter.save();
            QRect rect1=QRect(-m_rect.width()/2,-m_rect.height()/2,
                              m_rect.width(),m_rect.height());
            rect1.adjust(-8,-8,8,8);
            painter.translate(m_rect.center());
            painter.rotate(90);
            painter.drawText(rect1, Qt::AlignCenter,
                             m_str);
            painter.restore();
        }
        break;
    case EXPAND_H:
    {
         DrawTriAngle(painter,rectLeft,TRI_LEFT);
         DrawTriAngle(painter,rectRight,TRI_RIGHT);
    }
        break;
    case REDUCE_H:
    {
        DrawTriAngle(painter,rectLeft,TRI_RIGHT);
         DrawTriAngle(painter,rectRight,TRI_LEFT);
    }
        break;
    case EXPAND_V:
    {
         DrawTriAngle(painter,rectTop,TRI_UP);
         DrawTriAngle(painter,rectBottom,TRI_DOWN);
    }
        break;
    case REDUCE_V:
    {
         DrawTriAngle(painter,rectTop,TRI_DOWN);
         DrawTriAngle(painter,rectBottom,TRI_UP);
    }
        break;
    case LEFT:
    {
        DrawTriAngle(painter,rect,TRI_LEFT);
    }
        break;
    case RIGHT:
    {
        DrawTriAngle(painter,rect,TRI_RIGHT);
    }
        break;
    case LEFT2:
    {
        DrawTriAngle(painter,rectLeft,TRI_LEFT);
        DrawTriAngle(painter,rectRight,TRI_LEFT);
    }
        break;
    case RIGHT2:
    {
        DrawTriAngle(painter,rectLeft,TRI_RIGHT);
        DrawTriAngle(painter,rectRight,TRI_RIGHT);
    }
        break;
    case UP:
    {
        DrawTriAngle(painter,rect,TRI_UP);
    }
        break;
    case DOWN:
    {
        DrawTriAngle(painter,rect,TRI_DOWN);
    }
        break;
    case UP2:
    {
        DrawTriAngle(painter,rectTop,TRI_UP);
        DrawTriAngle(painter,rectBottom,TRI_UP);
    }
        break;
    case DOWN2:
    {
        DrawTriAngle(painter,rectTop,TRI_DOWN);
        DrawTriAngle(painter,rectBottom,TRI_DOWN);
    }
        break;
    default:break;
    }
}

void CCtrlGraphButton::DrawTriAngle(QPainter &painter,
                                    const QRect &rect,
                                    int iType)
{
    QRect rect1=rect;
    rect1.adjust(2,2,-2,-2);
    switch(iType)
    {
    case TRI_UP :
    {
        QPoint ptUp=(rect1.topLeft()+rect1.topRight())/2;
        QPainterPath path(ptUp);
        path.lineTo(rect1.bottomLeft());
        path.lineTo(rect1.bottomRight());
        path.lineTo(ptUp);
        painter.fillPath(path,Qt::black);
    }
        break;
    case TRI_DOWN :
    {
        QPoint ptDown=(rect1.bottomLeft()+rect1.bottomRight())/2;
        QPainterPath path(ptDown);
        path.lineTo(rect1.topLeft());
        path.lineTo(rect1.topRight());
        path.lineTo(ptDown);
        painter.fillPath(path,Qt::black);
    }
        break;
    case TRI_LEFT :
    {
        QPoint ptLeft=(rect1.topLeft()+rect1.bottomLeft())/2;
        QPainterPath path(ptLeft);
        path.lineTo(rect1.topRight());
        path.lineTo(rect1.bottomRight());
        path.lineTo(ptLeft);
        painter.fillPath(path,Qt::black);
    }

        break;
    case TRI_RIGHT :
    {
        QPoint ptRight=(rect1.topRight()+rect1.bottomRight())/2;
        QPainterPath path(ptRight);
        path.lineTo(rect1.topLeft());
        path.lineTo(rect1.bottomLeft());
        path.lineTo(ptRight);
        painter.fillPath(path,Qt::black);
    }
        break;
    }
}
