#include "painthelper.h"
#include <QPainterPath>

FontEx::FontEx(const QString &family, int pixelSize, bool bold) : QFont(family)
{
    setPixelSize(pixelSize);
    setBold(bold);
}

FontEx::FontEx(FontEx::NotoSansType type, int pixelSize)
{
    QString family = "黑体";
    if(type == NotoSansType::Normal) family = "思源黑体 CN Normal";
    if(type == NotoSansType::Medium) family = "思源黑体 CN Medium";
    if(type == NotoSansType::Regular) family = "思源黑体 CN Regular";
    if(type == NotoSansType::Bold) family = "思源黑体 CN Bold";
    if(type == NotoSansType::Light) family = "思源黑体 CN Light";
    setFamily(family);
    setPixelSize(pixelSize);
}

PainterEx::PainterEx(QPaintDevice *device): QPainter(device)
{
    
}


void PainterEx::SetBrushOnly(const QColor &color)
{
    setPen(Qt::transparent);
    setBrush(color);
}

void PainterEx::SetPenOnly(const QColor &color, qreal width, Qt::PenStyle style)
{
    setPen(QPen(color, width, style));
    setBrush(Qt::transparent);
}
//------------------------------------------------------------------------
//函    数:DrawCircle(QPointF center, double radius, const QColor &color = QColor())
//
//说    明:画圆的函数
//
//参    数:
//[传入]center 中心点
//[传入]radius 半径
//
//返 回 值:
//------------------------------------------------------------------------
void PainterEx::DrawCircle(QPointF center, double radius, const QColor &brush, const QPen &pen)
{
    save();
    setRenderHint(QPainter::Antialiasing);
    setPen(pen);
    setBrush(brush.isValid()?brush:QColor(0,0,0,0));
    translate(center - QPointF(radius, radius));
    drawEllipse(QRectF(0, 0, radius*2, radius*2));
    restore();
}
//------------------------------------------------------------------------
//函    数:DrawTextTwice(const QRectF &textRect, const QString &text, const QColor &color = QColor(), Qt::Alignment flags = Qt::AlignHCenter|Qt::AlignVCenter);
//
//说    明:思源黑体连续绘制两次, 否则会有点失真
//
//参    数:
//[传入]textRect 
//[传入]text 
//[传入]color 
//[传入]flags 
//
//返 回 值:
//------------------------------------------------------------------------
void PainterEx::DrawTextTwice(const QRectF &textRect, const QString &text, const QColor &color, Qt::Alignment flags)
{
    save();
    if(color.isValid()) SetPenOnly(color);
    drawText(textRect, static_cast<int>(flags), text);
    drawText(textRect, static_cast<int>(flags), text);
    restore();
}
void PainterEx::DrawText(const QRectF &textRect, const QString &text, const QColor &color, Qt::Alignment flags)
{
    save();
    if(color.isValid()) SetPenOnly(color);
    drawText(textRect, static_cast<int>(flags), text);
    restore();
}

void PainterEx::DrawRoundedRect(const QRectF &rect, int radius, const QColor &color, RoundedCorners flags)
{
//    save();
//    setRenderHint(QPainter::Antialiasing);
//    if(color.isValid()) SetBrushOnly(color);
//    //先画4个圆
//    DrawCircle(QPointF(radius, radius), radius, color);
//    DrawCircle(QPointF(textRect.width() - radius, radius), radius, color);
//    DrawCircle(QPointF(radius, textRect.height() - radius), radius, color);
//    DrawCircle(QPointF(textRect.width() - radius, textRect.height() - radius), radius, color);
//    //再画2个矩形
//    drawRect(textRect.adjusted(0, radius, 0, -radius));
//    drawRect(textRect.adjusted(radius, 0, -radius, 0));
//    restore();
    
    save();
    setRenderHint(QPainter::Antialiasing);
    if(color.isValid()) SetBrushOnly(color);
    QRectF cornerRect(0, 0, radius*2, radius*2);
    //下面arcTo都是逆时针画90°弧
    QPainterPath roundPath; //圆角矩形
    roundPath.setFillRule(Qt::WindingFill);
    
    //起点
    QPointF topLeftPoint = flags.testFlag(RoundedCorner::TopLeft)?QPointF(radius, 0):QPointF(0, 0);
    QPointF bottomLeftPoint = flags.testFlag(RoundedCorner::BottomLeft)?QPointF(0, rect.height()-radius):QPointF(0, rect.height());
    QPointF bottomRightPoint = flags.testFlag(RoundedCorner::BottomRight)?QPointF(rect.width()-radius, rect.height()):QPointF(rect.width(), rect.height());
    QPointF topRightPoint = flags.testFlag(RoundedCorner::TopRight)?QPointF(rect.width(), radius):QPointF(rect.width(), 0);
    
    roundPath.moveTo(topLeftPoint);//左上角
    if(flags.testFlag(RoundedCorner::TopLeft)) roundPath.arcTo(cornerRect, 90, 90);
    roundPath.lineTo(bottomLeftPoint);//左下角
    if(flags.testFlag(RoundedCorner::BottomLeft)) roundPath.arcTo(cornerRect.translated(0, rect.height()-radius*2), 180, 90);
    roundPath.lineTo(bottomRightPoint);//右下角
    if(flags.testFlag(RoundedCorner::BottomRight)) roundPath.arcTo(cornerRect.translated(rect.width()-radius*2, rect.height()-radius*2), 270, 90);
    roundPath.lineTo(topRightPoint);//右上角
    if(flags.testFlag(RoundedCorner::TopRight)) roundPath.arcTo(cornerRect.translated(rect.width()-radius*2, 0), 360, 90);
    roundPath.closeSubpath();
    
    translate(rect.topLeft());
    drawPath(roundPath);
    restore();
}

void PainterEx::DrawPixmap(const QRectF &rect, const QPixmap &pixmap)
{
    QRectF sourceRect(0.0, 0.0, pixmap.width(), pixmap.height());
    drawPixmap(rect, pixmap, sourceRect);
}

void PainterEx::DrawPixmap(const QRectF &rect, const QString &srcPath, int alpha)
{
    save();
    QPixmap pixmap(srcPath);
    if(alpha != 255)
    {//将pixmap处理成半透明
        QPixmap temp(pixmap.size());
        temp.fill(Qt::transparent);
        QPainter p1(&temp);
        p1.setCompositionMode(QPainter::CompositionMode_Source);
        p1.drawPixmap(0, 0, pixmap);
        p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        p1.fillRect(temp.rect(), QColor(0, 0, 0, alpha));
        p1.end();
        pixmap = temp;
    }
    QRectF sourceRect(0.0, 0.0, pixmap.width(), pixmap.height());
    drawPixmap(rect, pixmap, sourceRect);
    restore();
}

void PainterEx::DrawBorder(const QRect &rect, const QPen &pen, RectBorders flags)
{
    save();
    setPen(pen);
    if(flags.testFlag(RectBorder::RectBorderLeft)) drawLine(rect.topLeft(), rect.bottomLeft());
    if(flags.testFlag(RectBorder::RectBorderRight)) drawLine(rect.topRight(), rect.bottomRight());
    if(flags.testFlag(RectBorder::RectBorderTop)) drawLine(rect.topLeft(), rect.topRight());
    if(flags.testFlag(RectBorder::RectBorderBottom)) drawLine(rect.bottomLeft(), rect.bottomRight());
    restore();
}

void PainterEx::DrawTriangle(const QRect &rect, bool isUp, const QColor &brush, const QPen &pen)
{
    save();
    setRenderHint(QPainter::Antialiasing);
    setPen(pen);
    setBrush(brush.isValid()?brush:QColor(0,0,0,0));
    QPainterPath roundPath;
    roundPath.setFillRule(Qt::WindingFill);
    
    roundPath.moveTo(isUp?rect.bottomLeft():rect.topLeft()); //底边-左
    roundPath.lineTo(isUp?rect.bottomRight():rect.topRight()); //底边-右
    roundPath.lineTo(QPoint(rect.center().x(), isUp?rect.top():rect.bottom()));//头
    roundPath.closeSubpath();
    
    drawPath(roundPath);
    restore();
}