MythTV  master
mythpainter_qimage.cpp
Go to the documentation of this file.
1 // QT headers
2 #include <QPainter>
3 #include <QImage>
4 
5 // MythUI headers
6 #include "mythpainter_qimage.h"
7 #include "mythmainwindow.h"
8 
9 // MythDB headers
10 #include "compat.h"
11 #include "mythlogging.h"
12 
14 {
15  Teardown();
16 }
17 
18 void MythQImagePainter::Begin(QPaintDevice *parent)
19 {
20  if (!parent)
21  {
22  LOG(VB_GENERAL, LOG_ERR,
23  "FATAL ERROR: No parent widget defined for QT Painter, bailing");
24  return;
25  }
26 
27  MythPainter::Begin(parent);
28  m_painter = new QPainter(parent);
29  m_copy = true;
30  m_paintedRegion = QRegion();
31  m_painter->setCompositionMode(QPainter::CompositionMode_Source);
32  m_clipRegion = QRegion();
33  SetClipRect(QRect());
34 }
35 
36 void MythQImagePainter::CheckPaintMode(const QRect &area)
37 {
38  if (!m_painter)
39  return;
40 
41  bool intersects;
42 
43  if (m_paintedRegion.isEmpty())
44  {
45  intersects = false;
46  m_paintedRegion = QRegion(area);
47  }
48  else
49  {
50  intersects = m_paintedRegion.intersects(area);
51  m_paintedRegion = m_paintedRegion.united(area);
52  }
53 
54  if (intersects && m_copy)
55  {
56  m_copy = false;
57  m_painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
58  }
59  else if (!intersects && !m_copy)
60  {
61  m_copy = true;
62  m_painter->setCompositionMode(QPainter::CompositionMode_Source);
63  }
64 }
65 
67 {
68  if (!m_painter)
69  return;
70 
71  m_painter->end();
72  delete m_painter;
73 
75 }
76 
77 void MythQImagePainter::SetClipRect(const QRect &clipRect)
78 {
79  if (!m_painter)
80  return;
81 
82  if (!clipRect.isEmpty())
83  {
84  m_painter->setClipping(true);
85  if (m_clipRegion.isEmpty())
86  m_clipRegion = QRegion(clipRect);
87  else
88  m_clipRegion = m_clipRegion.united(clipRect);
89  m_painter->setClipRegion(m_clipRegion);
90  }
91  else
92  m_painter->setClipping(false);
93 }
94 
95 void MythQImagePainter::SetClipRegion(const QRegion &region)
96 {
97  if (!m_painter)
98  return;
99 
100  if (!region.isEmpty())
101  {
102  m_painter->setClipping(true);
103  m_clipRegion = region;
104  m_painter->setClipRegion(m_clipRegion);
105  }
106  else
107  m_painter->setClipping(false);
108 }
109 
110 void MythQImagePainter::Clear(QPaintDevice *device, const QRegion &region)
111 {
112  if (!device || region.isEmpty())
113  return;
114 
115  QImage *dev = dynamic_cast<QImage*>(device);
116  if (!dev)
117  return;
118 
119  int img_width = dev->size().width();
120  int img_height = dev->size().height();
121 
122 #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
123  QVector<QRect> rects = region.rects();
124  for (int i = 0; i < rects.size(); i++)
125  {
126  const QRect& r = rects[i];
127 #else
128  for (const QRect& r : region)
129  {
130 #endif
131  if (r.top() > img_height || r.left() > img_width)
132  continue;
133 
134  int bottom = std::min(r.top() + r.height(), img_height);
135  int bwidth = std::min(r.left() + r.width(), img_width);
136  bwidth = (bwidth - r.left()) << 2;
137 
138  for (int row = r.top(); row < bottom; row++)
139  memset(dev->scanLine(row) + (r.left() << 2), 0, bwidth);
140  }
141 }
142 
143 void MythQImagePainter::DrawImage(const QRect &r, MythImage *im,
144  const QRect &src, int alpha)
145 {
146  if (!m_painter)
147  {
148  LOG(VB_GENERAL, LOG_ERR,
149  "FATAL ERROR: DrawImage called with no painter");
150  return;
151  }
152 
153  (void)alpha;
154 
155  CheckPaintMode(QRect(r.topLeft(), src.size()));
156  m_painter->setOpacity(static_cast<float>(alpha) / 255.0F);
157  m_painter->drawImage(r.topLeft(), (QImage)(*im), src);
158  m_painter->setOpacity(1.0);
159 }
160 
161 void MythQImagePainter::DrawText(const QRect &r, const QString &msg,
162  int flags, const MythFontProperties &font,
163  int alpha, const QRect &boundRect)
164 {
165  MythPainter::DrawText(r, msg, flags, font, alpha, boundRect);
166 }
167 
168 void MythQImagePainter::DrawRect(const QRect &area, const QBrush &fillBrush,
169  const QPen &linePen, int alpha)
170 {
171  MythPainter::DrawRect(area, fillBrush, linePen, alpha);
172 }
173 
174 void MythQImagePainter::DrawRoundRect(const QRect &area, int cornerRadius,
175  const QBrush &fillBrush,
176  const QPen &linePen, int alpha)
177 {
178  MythPainter::DrawRoundRect(area, cornerRadius, fillBrush, linePen, alpha);
179 }
180 
181 void MythQImagePainter::DrawEllipse(const QRect &area, const QBrush &fillBrush,
182  const QPen &linePen, int alpha)
183 {
184  MythPainter::DrawEllipse(area, fillBrush, linePen, alpha);
185 }
void SetClipRect(const QRect &clipRect) override
void CheckPaintMode(const QRect &area)
virtual void Teardown(void)
Definition: mythpainter.cpp:29
void DrawEllipse(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
virtual void DrawEllipse(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha)
void DrawImage(const QRect &r, MythImage *im, const QRect &src, int alpha) override
void DrawRoundRect(const QRect &area, int cornerRadius, const QBrush &fillBrush, const QPen &linePen, int alpha) override
virtual void DrawRoundRect(const QRect &area, int cornerRadius, const QBrush &fillBrush, const QPen &linePen, int alpha)
unsigned char r
Definition: ParseText.cpp:329
virtual void End()
Definition: mythpainter.h:51
void Clear(QPaintDevice *device, const QRegion &region) override
virtual void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha)
virtual void DrawText(const QRect &r, const QString &msg, int flags, const MythFontProperties &font, int alpha, const QRect &boundRect)
Definition: mythpainter.cpp:78
void DrawText(const QRect &r, const QString &msg, int flags, const MythFontProperties &font, int alpha, const QRect &boundRect) override
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
virtual void Begin(QPaintDevice *parent)
Definition: mythpainter.h:50
void Begin(QPaintDevice *parent) override
void SetClipRegion(const QRegion &region) override