MythTV  master
mythpainter_qt.cpp
Go to the documentation of this file.
1 
2 // QT headers
3 #include <QPainter>
4 #include <QPixmap>
5 
6 // MythUI headers
7 #include "mythpainter_qt.h"
8 #include "mythfontproperties.h"
9 #include "mythmainwindow.h"
10 
11 // MythDB headers
12 #include "compat.h"
13 #include "mythlogging.h"
14 
15 class MythQtImage : public MythImage
16 {
17  public:
18  explicit MythQtImage(MythQtPainter *parent) :
19  MythImage(parent, "MythQtImage") { }
20 
21  void SetChanged(bool change = true) override; // MythImage
22  QPixmap *GetPixmap(void) { return m_Pixmap; }
23  void SetPixmap(QPixmap *p) { m_Pixmap = p; }
24 
25  bool NeedsRegen(void) { return m_bRegenPixmap; }
26  void RegeneratePixmap(void);
27 
28  protected:
29  QPixmap *m_Pixmap {nullptr};
30  bool m_bRegenPixmap {false};
31 };
32 
33 void MythQtImage::SetChanged(bool change)
34 {
35  if (change)
36  m_bRegenPixmap = true;
37 
38  MythImage::SetChanged(change);
39 }
40 
42 {
43  // We allocate the pixmap here so it is done in the UI
44  // thread since QPixmap uses non-reentrant X calls.
45  if (!m_Pixmap)
46  m_Pixmap = new QPixmap;
47 
48  if (m_Pixmap)
49  {
50  *m_Pixmap = QPixmap::fromImage(*((QImage *)this));
51  m_bRegenPixmap = false;
52  }
53 }
54 
56 {
57  Teardown();
58  DeletePixmaps();
59 }
60 
62 {
63  QMutexLocker locker(&m_imageDeleteLock);
64  while (!m_imageDeleteList.empty())
65  {
66  QPixmap *pm = m_imageDeleteList.front();
67  m_imageDeleteList.pop_front();
68  delete pm;
69  }
70 }
71 
72 void MythQtPainter::Begin(QPaintDevice *parent)
73 {
74  if (!parent)
75  {
76  LOG(VB_GENERAL, LOG_ERR,
77  "FATAL ERROR: No parent widget defined for QT Painter, bailing");
78  return;
79  }
80 
81  MythPainter::Begin(parent);
82 
83  m_painter = new QPainter(parent);
84  m_clipRegion = QRegion(QRect(0, 0, 0, 0));
85 
86  DeletePixmaps();
87 }
88 
90 {
91  m_painter->end();
92  delete m_painter;
93 
95 }
96 
97 void MythQtPainter::SetClipRect(const QRect &clipRect)
98 {
99  m_painter->setClipRect(clipRect);
100  if (!clipRect.isEmpty())
101  {
102  m_painter->setClipping(true);
103  if (m_clipRegion.isEmpty())
104  m_clipRegion = QRegion(clipRect);
105  else
106  m_clipRegion = m_clipRegion.united(clipRect);
107  }
108  else
109  m_painter->setClipping(false);
110 }
111 
112 void MythQtPainter::DrawImage(const QRect &r, MythImage *im,
113  const QRect &src, int alpha)
114 {
115  if (!m_painter)
116  {
117  LOG(VB_GENERAL, LOG_ERR,
118  "FATAL ERROR: DrawImage called with no painter");
119  return;
120  }
121 
122  MythQtImage *qim = reinterpret_cast<MythQtImage *>(im);
123 
124  if (qim->NeedsRegen())
125  qim->RegeneratePixmap();
126 
127  m_painter->setOpacity(static_cast<float>(alpha) / 255.0F);
128  m_painter->drawPixmap(r.topLeft(), *(qim->GetPixmap()), src);
129  m_painter->setOpacity(1.0);
130 }
131 
133 {
134  return new MythQtImage(this);
135 }
136 
138 {
139  MythQtImage *qim = static_cast<MythQtImage *>(im);
140 
141  QMutexLocker locker(&m_imageDeleteLock);
142  if (qim->GetPixmap())
143  {
144  m_imageDeleteList.push_back(qim->GetPixmap());
145  qim->SetPixmap(nullptr);
146  }
147 }
virtual void Teardown(void)
Definition: mythpainter.cpp:29
QRegion m_clipRegion
QMutex m_imageDeleteLock
QPainter * m_painter
virtual void SetChanged(bool change=true)
Definition: mythimage.h:45
void End() override
std::list< QPixmap * > m_imageDeleteList
bool NeedsRegen(void)
unsigned char r
Definition: ParseText.cpp:329
virtual void End()
Definition: mythpainter.h:51
MythQtImage(MythQtPainter *parent)
void Begin(QPaintDevice *parent) override
MythImage * GetFormatImagePriv(void) override
Creates a reference counted image, call DecrRef() to delete.
void SetClipRect(const QRect &clipRect) override
void DeletePixmaps(void)
void DrawImage(const QRect &r, MythImage *im, const QRect &src, int alpha) override
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void SetPixmap(QPixmap *p)
void SetChanged(bool change=true) override
QPixmap * GetPixmap(void)
void DeleteFormatImagePriv(MythImage *im) override
void RegeneratePixmap(void)
virtual void Begin(QPaintDevice *parent)
Definition: mythpainter.h:50
QPixmap * m_Pixmap