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 "libmythbase/compat.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) const { 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  auto *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  auto *qim = dynamic_cast<MythQtImage *>(im);
140 
141  QMutexLocker locker(&m_imageDeleteLock);
142  if (qim && qim->GetPixmap())
143  {
144  m_imageDeleteList.push_back(qim->GetPixmap());
145  qim->SetPixmap(nullptr);
146  }
147 }
MythQtPainter::DrawImage
void DrawImage(QRect r, MythImage *im, QRect src, int alpha) override
Definition: mythpainter_qt.cpp:112
MythQtImage::NeedsRegen
bool NeedsRegen(void) const
Definition: mythpainter_qt.cpp:25
MythQtImage
Definition: mythpainter_qt.cpp:15
MythQtImage::SetChanged
void SetChanged(bool change=true) override
Definition: mythpainter_qt.cpp:33
mythpainter_qt.h
MythQtPainter
Definition: mythpainter_qt.h:13
MythQtPainter::GetFormatImagePriv
MythImage * GetFormatImagePriv(void) override
Creates a reference counted image, call DecrRef() to delete.
Definition: mythpainter_qt.cpp:132
MythQtImage::RegeneratePixmap
void RegeneratePixmap(void)
Definition: mythpainter_qt.cpp:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythQtPainter::DeleteFormatImagePriv
void DeleteFormatImagePriv(MythImage *im) override
Definition: mythpainter_qt.cpp:137
MythQtPainter::m_painter
QPainter * m_painter
Definition: mythpainter_qt.h:42
mythfontproperties.h
mythlogging.h
MythQtPainter::~MythQtPainter
~MythQtPainter() override
Definition: mythpainter_qt.cpp:55
MythQtImage::m_pixmap
QPixmap * m_pixmap
Definition: mythpainter_qt.cpp:29
hardwareprofile.config.p
p
Definition: config.py:33
compat.h
MythQtPainter::Begin
void Begin(QPaintDevice *parent) override
Definition: mythpainter_qt.cpp:72
MythImage::SetChanged
virtual void SetChanged(bool change=true)
Definition: mythimage.h:50
MythQtImage::SetPixmap
void SetPixmap(QPixmap *p)
Definition: mythpainter_qt.cpp:23
MythQtPainter::m_imageDeleteLock
QMutex m_imageDeleteLock
Definition: mythpainter_qt.h:46
MythPainter::End
virtual void End()
Definition: mythpainter.h:55
MythQtPainter::m_imageDeleteList
std::list< QPixmap * > m_imageDeleteList
Definition: mythpainter_qt.h:45
MythImage
Definition: mythimage.h:36
MythQtImage::MythQtImage
MythQtImage(MythQtPainter *parent)
Definition: mythpainter_qt.cpp:18
MythQtPainter::End
void End() override
Definition: mythpainter_qt.cpp:89
MythQtPainter::m_clipRegion
QRegion m_clipRegion
Definition: mythpainter_qt.h:43
MythPainter::Teardown
virtual void Teardown(void)
Definition: mythpainter.cpp:28
mythmainwindow.h
MythQtPainter::DeletePixmaps
void DeletePixmaps(void)
Definition: mythpainter_qt.cpp:61
MythQtImage::m_bRegenPixmap
bool m_bRegenPixmap
Definition: mythpainter_qt.cpp:30
MythPainter::Begin
virtual void Begin(QPaintDevice *)
Definition: mythpainter.h:54
MythQtPainter::SetClipRect
void SetClipRect(QRect clipRect) override
Definition: mythpainter_qt.cpp:97
MythQtImage::GetPixmap
QPixmap * GetPixmap(void)
Definition: mythpainter_qt.cpp:22