| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | #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();}
 |