00001
00002
00003 #include <QPainter>
00004 #include <QPixmap>
00005
00006
00007 #include "mythpainter_qt.h"
00008 #include "mythfontproperties.h"
00009 #include "mythmainwindow.h"
00010
00011
00012 #include "compat.h"
00013 #include "mythlogging.h"
00014
00015 class MythQtImage : public MythImage
00016 {
00017 public:
00018 MythQtImage(MythPainter *parent) : MythImage(parent),
00019 m_Pixmap(NULL), m_bRegenPixmap(false) { }
00020 ~MythQtImage() { }
00021
00022 void SetChanged(bool change = true);
00023 QPixmap *GetPixmap(void) { return m_Pixmap; }
00024
00025 bool NeedsRegen(void) { return m_bRegenPixmap; }
00026 void RegeneratePixmap(void);
00027
00028 protected:
00029 QPixmap *m_Pixmap;
00030 bool m_bRegenPixmap;
00031 };
00032
00033 void MythQtImage::SetChanged(bool change)
00034 {
00035 if (change)
00036 m_bRegenPixmap = true;
00037
00038 MythImage::SetChanged(change);
00039 }
00040
00041 void MythQtImage::RegeneratePixmap(void)
00042 {
00043
00044
00045 if (!m_Pixmap)
00046 m_Pixmap = new QPixmap;
00047
00048 if (m_Pixmap)
00049 {
00050 *m_Pixmap = QPixmap::fromImage(*((QImage *)this));
00051 m_bRegenPixmap = false;
00052 }
00053 }
00054
00055 MythQtPainter::MythQtPainter() :
00056 MythPainter(),
00057 painter(0)
00058 {
00059 }
00060
00061 MythQtPainter::~MythQtPainter()
00062 {
00063 DeletePixmaps();
00064 }
00065
00066 void MythQtPainter::DeletePixmaps(void)
00067 {
00068 QMutexLocker locker(&m_imageDeleteLock);
00069 while (!m_imageDeleteList.empty())
00070 {
00071 QPixmap *pm = m_imageDeleteList.front();
00072 m_imageDeleteList.pop_front();
00073 delete pm;
00074 }
00075 }
00076
00077 void MythQtPainter::Begin(QPaintDevice *parent)
00078 {
00079 if (!parent)
00080 {
00081 LOG(VB_GENERAL, LOG_ERR,
00082 "FATAL ERROR: No parent widget defined for QT Painter, bailing");
00083 return;
00084 }
00085
00086 MythPainter::Begin(parent);
00087
00088 painter = new QPainter(parent);
00089 clipRegion = QRegion(QRect(0, 0, 0, 0));
00090
00091 DeletePixmaps();
00092 }
00093
00094 void MythQtPainter::End(void)
00095 {
00096 painter->end();
00097 delete painter;
00098
00099 MythPainter::End();
00100 }
00101
00102 void MythQtPainter::SetClipRect(const QRect &clipRect)
00103 {
00104 painter->setClipRect(clipRect);
00105 if (!clipRect.isEmpty())
00106 {
00107 painter->setClipping(true);
00108 if (clipRegion.isEmpty())
00109 clipRegion = QRegion(clipRect);
00110 else
00111 clipRegion = clipRegion.unite(clipRect);
00112 }
00113 else
00114 painter->setClipping(false);
00115 }
00116
00117 void MythQtPainter::DrawImage(const QRect &r, MythImage *im,
00118 const QRect &src, int alpha)
00119 {
00120 if (!painter)
00121 {
00122 LOG(VB_GENERAL, LOG_ERR,
00123 "FATAL ERROR: DrawImage called with no painter");
00124 return;
00125 }
00126
00127 MythQtImage *qim = reinterpret_cast<MythQtImage *>(im);
00128
00129 if (qim->NeedsRegen())
00130 qim->RegeneratePixmap();
00131
00132 painter->setOpacity(static_cast<float>(alpha) / 255.0);
00133 painter->drawPixmap(r.topLeft(), *(qim->GetPixmap()), src);
00134 painter->setOpacity(1.0);
00135 }
00136
00137 MythImage *MythQtPainter::GetFormatImagePriv()
00138 {
00139 return new MythQtImage(this);
00140 }
00141
00142 void MythQtPainter::DeleteFormatImagePriv(MythImage *im)
00143 {
00144 MythQtImage *qim = static_cast<MythQtImage *>(im);
00145
00146 QMutexLocker locker(&m_imageDeleteLock);
00147 if (qim->GetPixmap())
00148 m_imageDeleteList.push_back(qim->GetPixmap());
00149 }
00150