painthelper.cpp 6.9 KB

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