#include "locolorselector.h"
#include "./ColorDialog/ColorDialog.h"
LoColorSelector::LoColorSelector(QWidget *parent) :
    QPushButton(parent),
    m_text(""),
    m_color(Qt::transparent)
{ init(); }

LoColorSelector::LoColorSelector(const QString &text, const QColor &color, QWidget *parent):
    QPushButton(parent),
    m_text(text),
    m_color(color)
{ init(); }

void LoColorSelector::init()
{
    setText(m_text);
    setColor(m_color);
    connect(this, SIGNAL(clicked()), this, SLOT(onColorChanged()));
}

void LoColorSelector::setColor(const QColor &color)
{
    QFont ft = font();
    QString sColor;
    if(color.alpha() == 0) {
        ft.setStrikeOut(true);
    } else {
        ft.setStrikeOut(false);
    }
    sColor = QString("background-color: rgba(%1, %2, %3, %4);color: rgba(%5, %6, %7, %8);").
                     arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha()).arg(255-color.red()).arg(255-color.green()).arg(255-color.blue()).arg(255);
    setStyleSheet(sColor);
//    QString strFontColor;
//    strFontColor = QString("color: rgba(%1, %2, %3, %4);").
//                     arg(255-color.red()).arg(255-color.green()).arg(255-color.blue()).arg(255);
//    setStyleSheet(strFontColor);

    setFont(ft);
    m_color = color;
}

void LoColorSelector::onColorChanged()
{
    bool bAlpha=true;
  //  if(this->text()=="T")
   //     bAlpha=false;
    ColorDialog *m_pColorDlg = new ColorDialog(m_color,bAlpha,this);
    connect(m_pColorDlg, SIGNAL(accepted()), this, SLOT(onAccepted()));
    m_pColorDlg->showDialog();
    if(m_pColorDlg->m_buttonRole==m_pColorDlg->Yes)
    {
        QColor color=m_pColorDlg->getRgba();
       // m_color.setAlpha(255);
       // QColor color = ColorDialog::getColor();//::getColor(m_color, nullptr, tr("Select Color"), QColorDialog::ShowAlphaChannel);
        if(color.isValid()) {
            setColor(color);

            emit sColorChanged(color);
        }
    }

}
