#include "mybutton.h"

MyButton::MyButton(QWidget *parent):QPushButton(parent)
{
    labelImage = NULL;
    labelImage = new QLabel(this);

    labelName = NULL;
    labelName = new QLabel(this);

    labelValue = NULL;
    labelValue = new QLabel(this);
}

MyButton::~MyButton()
{
    if(labelImage != NULL)
    {
        delete labelImage;
    }

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

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

//设置顶图
void MyButton::setTopImage(QString imagePath,short int y)
{
    //label加载图片( 方法1)
    //QPixmap pix;
    //pix.load(imagePath);
    //labelImage->setPixmap(pix);

    labelName->hide();
    labelValue->hide();
    labelImage->show();

    labelImage->setGeometry(0,0,this->width(),this->height()+y);
    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;";
    labelImage->setStyleSheet(style);
}

void MyButton::setShortCutTopImage(QString imagePath, QPoint point)
{
    labelName->hide();
    labelValue->hide();
    labelImage->show();

    labelImage->setGeometry(point.x(),point.y(),this->width(),this->height());
    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;";
    labelImage->setStyleSheet(style);
}

void MyButton::setPreviewImage(QImage image)
{
    //label加载图片
    QPixmap pix = QPixmap::fromImage(image);
    labelImage->setPixmap(pix);
}

//设置条目左侧图标
void MyButton::setItemLeftIcon(QString imagePath)
{
    labelImage->move(0,0);
    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;";
    labelImage->setStyleSheet(style);
}

void MyButton::setLabelValueImage(QString imagePath)
{
    if(imagePath.length() <= 0)
    {
        QPixmap npix;
        labelValue->setPixmap(npix);
        return;
    }
    QImage image;
    image.load(imagePath);
    QPixmap pix = QPixmap::fromImage(image);
    labelValue->setPixmap(pix);

//    labelValue->setAlignment(Qt::AlignLeft);
//    labelValue->setAlignment(Qt::AlignBottom);
//    QString style = "background-color: rgba(255, 255, 255, 0);border:0px;image: url(" + imagePath + ");";
//    labelValue->setStyleSheet(style);
}

//设置底层图片(按下、抬起、失效)
void MyButton::setBottomImage(QString imagePath)
{
//第一种加载图片的方法
//    QPixmap pix;
//    pix.load(imageRelPath);
//    int w = this->width();
//    int h = this->height();
//    this->setIconSize(QSize(w,h));
//    this->setIcon(pix);

    //第二种加载图片的方法
    QString style = "QPushButton{outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + "_p0.png" + ");border:0px;}";
    style += "QPushButton:pressed{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}";
    style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}";
    style += "QPushButton:checked{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}";
    //style += "QPushButton:disabled{background-color: rgba(220, 220, 220, 50);}";
    this->setStyleSheet(style);
}

void MyButton::setWhiteBlueBottomStyle()
{
    //白蓝色渐变、灰蓝色渐变
    QString style = "QPushButton{color:black;background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(255, 255, 255), stop:1 rgb(215, 243, 254));border-radius:5px;}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(217, 222, 224), stop:1 rgb(191,213,223));}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(230, 230, 230), stop:1 rgb(182,182,182));}";
    this->setStyleSheet(style);
}

void MyButton::setBlueBorderBottomStyle()
{
    //蓝色渐变带边框
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(85, 211, 233), stop:1 rgb(28, 183, 210));border-bottom:1px solid rgb(30, 155, 177);border-radius:10px;}";
    style += "QPushButton:pressed{background-color: rgb(73, 181, 192);}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(230, 230, 230), stop:1 rgb(182,182,182));border-bottom:1px solid rgb(100,100,100);}";
    this->setStyleSheet(style);
}

void MyButton::setOrangeGradientBottomStyle(QString str)
{
    //橙色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(253, 231, 140), stop:1 rgb(240,119,12));border-radius:"+str+"px;color:rgb(255,255,255);}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(222, 196, 128), stop:1 rgb(213,125,54));color:rgb(255,255,255);}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(227, 227, 227), stop:1 rgb(177,177,177));color:rgb(255,255,255);}";
    this->setStyleSheet(style);
}

void MyButton::setBlueGradientBottomStyle()
{
    //蓝色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(79, 219, 254), stop:1 rgb(161,236,255));border-radius:4px;}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(63, 175, 203), stop:1 rgb(129,189,204));}";
    this->setStyleSheet(style);
}

void MyButton::setGreenGradientBottomStyle(QString str)
{
    //绿色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(139, 195, 163), stop:1 rgb(86,172,123));border-radius:"+str+"px;}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(105, 147, 123), stop:1 rgb(84,138,107));}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(227, 227, 227), stop:1 rgb(177,177,177));color:rgb(255,255,255);}";
    style += "QPushButton:checked{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(105, 147, 100), stop:1 rgb(84,138,90));}";
    this->setStyleSheet(style);
}

#if(0)
//设置控制动作界面底层图片(按下、抬起)，并设置文字前景色
void MyButton::setControlWidgetBtnBottomImage(QString imagePath, QString color)
{
    QString style = "QPushButton{outline: none;"+ color + "border-image: url(" + imagePath + "_p0.png" + ");border:0px;}";
    style += "QPushButton:pressed{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}";
    style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}";
    //style += "QPushButton:disabled{background-color: rgba(220, 220, 220, 50);}";
    this->setStyleSheet(style);
}
#endif

//设置主按钮底层图片
void MyButton::setCheckedBottomImage(QString imagePath)
{
    //第二种加载图片的方法
    QString style = "QPushButton{outline: none;border-image: url(" + imagePath + "_p0.png" + ");border:0px;}";
    style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}";
    style += "QPushButton:checked{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}";
    this->setStyleSheet(style);
}

void MyButton::setMainBtnBottomStyle()
{
    //蓝色渐变、灰色渐变、橙色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(16, 198, 255), stop:1 rgb(65, 107, 255));border-radius:10px;}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(234, 234, 234), stop:1 rgb(176,176,176));}";
    style += "QPushButton:checked{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(253, 225, 113), stop:1 rgb(240,119,12));}";
    this->setStyleSheet(style);
}

//设置按钮logo(按钮是没有点击事件的)
void MyButton::setUnPreBtnLogo(QString tStyle)
{
    labelName->hide();
    labelValue->hide();
    labelImage->hide();

    if(tStyle.length() <= 0)
    {
        return;
    }

    //橙色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(253, 231, 140), stop:1 rgb(240,119,12));border-radius:10px;border-image: url(" + tStyle + ");border:0px;outline: none;}";
    this->setStyleSheet(style);
}


//初始化label条目名称和条目值(参数设置)
void MyButton::initLeftNameRightValue(QRect rectName, QRect rectValue, int right)
{
    labelImage->hide();

    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);";
    style += "color: rgb(255, 255, 255);";
    labelName->setGeometry(rectName);
    labelName->setStyleSheet(style);
    labelName->setAlignment(Qt::AlignLeft | Qt::AlignBottom);

    labelValue->setGeometry(rectValue);
    labelValue->setStyleSheet(style);    
    if(right == 0)
    {
        labelValue->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
    }
    else
    {
        labelValue->setAlignment(Qt::AlignRight | Qt::AlignBottom);
    }

    QString style1 = "QPushButton{outline: none;border:0px;border-radius:7px;background-color:rgba(255, 255, 255, 0);}";
    style1 += "QPushButton:pressed{outline: none;background-color: rgba(255, 255, 255, 50);}";
    this->setStyleSheet(style1);
}

//初始化label图标显示和条目名称(文件导入)
void MyButton::initLeftImageRightName(QRect rectImage, QRect rectName)
{
    labelValue->hide();
    labelImage->show();

    labelImage->setGeometry(rectImage);
    labelImage->setAlignment(Qt::AlignHCenter);
    labelImage->setAlignment(Qt::AlignBottom);

    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);";
    style += "color: rgb(255, 255, 255);";

    labelName->setGeometry(rectName);
    labelName->setStyleSheet(style);
    labelName->setAlignment(Qt::AlignLeft);
    labelName->setAlignment(Qt::AlignBottom);

    QString style1 = "QPushButton{outline: none;border:0px;border-radius:7px;background-color:rgba(255, 255, 255, 0);}";
    style1 += "QPushButton:checked{outline: none;background-color: rgba(255, 255, 255, 50);}";
    this->setStyleSheet(style1);
}

void MyButton::initUpImageDownName(QRect rectImage, QRect rectName)
{
    labelValue->hide();
    labelImage->show();

    labelImage->setGeometry(rectImage);
    labelImage->setAlignment(Qt::AlignCenter);
    labelImage->setStyleSheet("outline: none;background-color: rgba(255, 255, 255, 0);");

    QString style = "outline: none;background-color: rgba(255, 255, 255, 0);";
    style += "color: rgb(255, 255, 255);";

    labelName->setGeometry(rectName);
    labelName->setStyleSheet(style);
    labelName->setAlignment(Qt::AlignLeft);
    labelName->setAlignment(Qt::AlignTop);
    labelName->setWordWrap(true);
}

void MyButton::initLabelValue(QRect rectValue)
{
    labelValue->setGeometry(rectValue);
    labelValue->setAlignment(Qt::AlignCenter);
}

void MyButton::setLabelNamePos(int pos)
{
    if(pos != 0)
    {
        labelName->setAlignment(Qt::AlignRight | Qt::AlignBottom);
    }
}

void MyButton::setLabelValuePos(int pos)
{
    if(pos != 0)
    {
        labelValue->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    }
}

//设置label字体
void MyButton::setLabelFont(QFont font)
{
    labelName->setFont(font);
    labelValue->setFont(font);
    //labelValue->show();
}

void MyButton::setLabelNameFont(QFont font)
{
    labelName->setFont(font);
}

void MyButton::setLabelValueFont(QFont font)
{
    labelValue->setFont(font);
}

//设置条目名称
void MyButton::setLabelName(QString name)
{
    labelName->setText(name);
    labelName->show();
}

void MyButton::setLabelNameStyle(QString style)
{
    labelName->setStyleSheet(style);
}

//设置条目值
void MyButton::setLabelValue(QString value)
{
    labelValue->setText(value);
    labelValue->show();
}

void MyButton::setLabelValueStyle(QString style)
{
    labelValue->setStyleSheet(style);
}

void MyButton::setLabelImage(QString style)
{
    labelImage->setStyleSheet(style);
}

void MyButton::setBtnBackColor(QString style)
{
    style += "outline: none;border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;";
    this->setStyleSheet(style);
}

void MyButton::setImageBackColor(QString style)
{
    style += "outline: none;border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;";
    labelImage->setStyleSheet(style);
}

void MyButton::setLabelImageChecked(QString style)
{
    style += "outline: none;border:4px groove rgb(52,52,52);border-style:inset;border-radius:5px;";
    labelImage->setStyleSheet(style);
}
