painthelper.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "painthelper.h"
  2. #include <QPainterPath>
  3. FontEx::FontEx(const QString &family, int pixelSize, bool bold) : QFont(family)
  4. {
  5. setPixelSize(pixelSize);
  6. setBold(bold);
  7. }
  8. FontEx::FontEx(FontEx::NotoSansType type, int pixelSize)
  9. {
  10. QString family = "黑体";
  11. if(type == NotoSansType::Normal) family = "思源黑体 CN Normal";
  12. if(type == NotoSansType::Medium) family = "思源黑体 CN Medium";
  13. if(type == NotoSansType::Regular) family = "思源黑体 CN Regular";
  14. if(type == NotoSansType::Bold) family = "思源黑体 CN Bold";
  15. if(type == NotoSansType::Light) family = "思源黑体 CN Light";
  16. setFamily(family);
  17. setPixelSize(pixelSize);
  18. }
  19. PainterEx::PainterEx(QPaintDevice *device): QPainter(device)
  20. {
  21. }
  22. void PainterEx::SetBrushOnly(const QColor &color)
  23. {
  24. setPen(Qt::transparent);
  25. setBrush(color);
  26. }
  27. void PainterEx::SetPenOnly(const QColor &color, qreal width, Qt::PenStyle style)
  28. {
  29. setPen(QPen(color, width, style));
  30. setBrush(Qt::transparent);
  31. }
  32. //------------------------------------------------------------------------
  33. //函 数:DrawCircle(QPointF center, double radius, const QColor &color = QColor())
  34. //
  35. //说 明:画圆的函数
  36. //
  37. //参 数:
  38. //[传入]center 中心点
  39. //[传入]radius 半径
  40. //
  41. //返 回 值:
  42. //------------------------------------------------------------------------
  43. void PainterEx::DrawCircle(QPointF center, double radius, const QColor &brush, const QPen &pen)
  44. {
  45. save();
  46. setRenderHint(QPainter::Antialiasing);
  47. setPen(pen);
  48. setBrush(brush.isValid()?brush:QColor(0,0,0,0));
  49. translate(center - QPointF(radius, radius));
  50. drawEllipse(QRectF(0, 0, radius*2, radius*2));
  51. restore();
  52. }
  53. //------------------------------------------------------------------------
  54. //函 数:DrawTextTwice(const QRectF &textRect, const QString &text, const QColor &color = QColor(), Qt::Alignment flags = Qt::AlignHCenter|Qt::AlignVCenter);
  55. //
  56. //说 明:思源黑体连续绘制两次, 否则会有点失真
  57. //
  58. //参 数:
  59. //[传入]textRect
  60. //[传入]text
  61. //[传入]color
  62. //[传入]flags
  63. //
  64. //返 回 值:
  65. //------------------------------------------------------------------------
  66. void PainterEx::DrawTextTwice(const QRectF &textRect, const QString &text, const QColor &color, Qt::Alignment flags)
  67. {
  68. save();
  69. if(color.isValid()) SetPenOnly(color);
  70. drawText(textRect, static_cast<int>(flags), text);
  71. drawText(textRect, static_cast<int>(flags), text);
  72. restore();
  73. }
  74. void PainterEx::DrawText(const QRectF &textRect, const QString &text, const QColor &color, Qt::Alignment flags)
  75. {
  76. save();
  77. if(color.isValid()) SetPenOnly(color);
  78. drawText(textRect, static_cast<int>(flags), text);
  79. restore();
  80. }
  81. void PainterEx::DrawRoundedRect(const QRectF &rect, int radius, const QColor &color, RoundedCorners flags)
  82. {
  83. // save();
  84. // setRenderHint(QPainter::Antialiasing);
  85. // if(color.isValid()) SetBrushOnly(color);
  86. // //先画4个圆
  87. // DrawCircle(QPointF(radius, radius), radius, color);
  88. // DrawCircle(QPointF(textRect.width() - radius, radius), radius, color);
  89. // DrawCircle(QPointF(radius, textRect.height() - radius), radius, color);
  90. // DrawCircle(QPointF(textRect.width() - radius, textRect.height() - radius), radius, color);
  91. // //再画2个矩形
  92. // drawRect(textRect.adjusted(0, radius, 0, -radius));
  93. // drawRect(textRect.adjusted(radius, 0, -radius, 0));
  94. // restore();
  95. save();
  96. setRenderHint(QPainter::Antialiasing);
  97. if(color.isValid()) SetBrushOnly(color);
  98. QRectF cornerRect(0, 0, radius*2, radius*2);
  99. //下面arcTo都是逆时针画90°弧
  100. QPainterPath roundPath; //圆角矩形
  101. roundPath.setFillRule(Qt::WindingFill);
  102. //起点
  103. QPointF topLeftPoint = flags.testFlag(RoundedCorner::TopLeft)?QPointF(radius, 0):QPointF(0, 0);
  104. QPointF bottomLeftPoint = flags.testFlag(RoundedCorner::BottomLeft)?QPointF(0, rect.height()-radius):QPointF(0, rect.height());
  105. QPointF bottomRightPoint = flags.testFlag(RoundedCorner::BottomRight)?QPointF(rect.width()-radius, rect.height()):QPointF(rect.width(), rect.height());
  106. QPointF topRightPoint = flags.testFlag(RoundedCorner::TopRight)?QPointF(rect.width(), radius):QPointF(rect.width(), 0);
  107. roundPath.moveTo(topLeftPoint);//左上角
  108. if(flags.testFlag(RoundedCorner::TopLeft)) roundPath.arcTo(cornerRect, 90, 90);
  109. roundPath.lineTo(bottomLeftPoint);//左下角
  110. if(flags.testFlag(RoundedCorner::BottomLeft)) roundPath.arcTo(cornerRect.translated(0, rect.height()-radius*2), 180, 90);
  111. roundPath.lineTo(bottomRightPoint);//右下角
  112. if(flags.testFlag(RoundedCorner::BottomRight)) roundPath.arcTo(cornerRect.translated(rect.width()-radius*2, rect.height()-radius*2), 270, 90);
  113. roundPath.lineTo(topRightPoint);//右上角
  114. if(flags.testFlag(RoundedCorner::TopRight)) roundPath.arcTo(cornerRect.translated(rect.width()-radius*2, 0), 360, 90);
  115. roundPath.closeSubpath();
  116. translate(rect.topLeft());
  117. drawPath(roundPath);
  118. restore();
  119. }
  120. void PainterEx::DrawPixmap(const QRectF &rect, const QPixmap &pixmap)
  121. {
  122. QRectF sourceRect(0.0, 0.0, pixmap.width(), pixmap.height());
  123. drawPixmap(rect, pixmap, sourceRect);
  124. }
  125. void PainterEx::DrawPixmap(const QRectF &rect, const QString &srcPath, int alpha)
  126. {
  127. save();
  128. QPixmap pixmap(srcPath);
  129. if(alpha != 255)
  130. {//将pixmap处理成半透明
  131. QPixmap temp(pixmap.size());
  132. temp.fill(Qt::transparent);
  133. QPainter p1(&temp);
  134. p1.setCompositionMode(QPainter::CompositionMode_Source);
  135. p1.drawPixmap(0, 0, pixmap);
  136. p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  137. p1.fillRect(temp.rect(), QColor(0, 0, 0, alpha));
  138. p1.end();
  139. pixmap = temp;
  140. }
  141. QRectF sourceRect(0.0, 0.0, pixmap.width(), pixmap.height());
  142. drawPixmap(rect, pixmap, sourceRect);
  143. restore();
  144. }
  145. void PainterEx::DrawBorder(const QRect &rect, const QPen &pen, RectBorders flags)
  146. {
  147. save();
  148. setPen(pen);
  149. if(flags.testFlag(RectBorder::RectBorderLeft)) drawLine(rect.topLeft(), rect.bottomLeft());
  150. if(flags.testFlag(RectBorder::RectBorderRight)) drawLine(rect.topRight(), rect.bottomRight());
  151. if(flags.testFlag(RectBorder::RectBorderTop)) drawLine(rect.topLeft(), rect.topRight());
  152. if(flags.testFlag(RectBorder::RectBorderBottom)) drawLine(rect.bottomLeft(), rect.bottomRight());
  153. restore();
  154. }
  155. void PainterEx::DrawTriangle(const QRect &rect, bool isUp, const QColor &brush, const QPen &pen)
  156. {
  157. save();
  158. setRenderHint(QPainter::Antialiasing);
  159. setPen(pen);
  160. setBrush(brush.isValid()?brush:QColor(0,0,0,0));
  161. QPainterPath roundPath;
  162. roundPath.setFillRule(Qt::WindingFill);
  163. roundPath.moveTo(isUp?rect.bottomLeft():rect.topLeft()); //底边-左
  164. roundPath.lineTo(isUp?rect.bottomRight():rect.topRight()); //底边-右
  165. roundPath.lineTo(QPoint(rect.center().x(), isUp?rect.top():rect.bottom()));//头
  166. roundPath.closeSubpath();
  167. drawPath(roundPath);
  168. restore();
  169. }