#include "wordartwidget.h"

#include <QPainter>
#include <QPainterPath>
#include <QFont>
#include <QLinearGradient>
#include <QPen>
#include <QStyleOption>

WordArtWidget::WordArtWidget(QWidget* parent)
    : QWidget(parent)
{
    m_iFontSize = 40;
    m_sOutColor = "#EB2E50";
    m_sInColor = "";
}

void WordArtWidget::setText(const QString &text)
{
    m_sText = text;
    this->update();
}

void WordArtWidget::setOutColor(const QString &outColor)
{
    m_sOutColor = outColor;
}

void WordArtWidget::setInColor(const QString &inColor)
{
    m_sInColor = inColor;
}

void WordArtWidget::setFontSize(int size)
{
    m_iFontSize = size;
}

void WordArtWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);

    // 防止样式表设置无效
    QStyleOption opt;
    opt.init(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);

    // 设置艺术字颜色
    QPen pen;
    pen.setWidth(2);
    pen.setColor(QColor(m_sOutColor));

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(pen);

    QLinearGradient linearGrad;    
    if(m_sInColor.isEmpty())
        linearGrad.setColorAt(0, Qt::transparent);     // 字体透明 只有边框
    else
        linearGrad.setColorAt(0, QColor(m_sInColor));  // 字体利用特定颜色来填充

    QFont font;
    font.setPointSize(m_iFontSize);
    font.setBold(true);

    QPainterPath textPath;
    QRect painterRect = rect();

    textPath.addText(0, painterRect.height() - m_iFontSize / 2, font, m_sText);
    painter.setBrush(linearGrad);
    painter.drawPath(textPath);
}
