00001
00002 #include <QPainter>
00003 #include <QImage>
00004
00005
00006 #include "mythpainter_qimage.h"
00007 #include "mythmainwindow.h"
00008
00009
00010 #include "compat.h"
00011 #include "mythlogging.h"
00012
00013 MythQImagePainter::MythQImagePainter() :
00014 MythPainter(), painter(NULL), copy(false)
00015 {
00016 }
00017
00018 MythQImagePainter::~MythQImagePainter()
00019 {
00020 ExpireImages();
00021 }
00022
00023 void MythQImagePainter::Begin(QPaintDevice *parent)
00024 {
00025 if (!parent)
00026 {
00027 LOG(VB_GENERAL, LOG_ERR,
00028 "FATAL ERROR: No parent widget defined for QT Painter, bailing");
00029 return;
00030 }
00031
00032 MythPainter::Begin(parent);
00033 painter = new QPainter(parent);
00034 copy = true;
00035 paintedRegion = QRegion();
00036 painter->setCompositionMode(QPainter::CompositionMode_Source);
00037 clipRegion = QRegion();
00038 SetClipRect(QRect());
00039 }
00040
00041 void MythQImagePainter::CheckPaintMode(const QRect &area)
00042 {
00043 if (!painter)
00044 return;
00045
00046 bool intersects;
00047
00048 if (paintedRegion.isEmpty())
00049 {
00050 intersects = false;
00051 paintedRegion = QRegion(area);
00052 }
00053 else
00054 {
00055 intersects = paintedRegion.intersects(area);
00056 paintedRegion = paintedRegion.united(area);
00057 }
00058
00059 if (intersects && copy)
00060 {
00061 copy = false;
00062 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
00063 }
00064 else if (!intersects && !copy)
00065 {
00066 copy = true;
00067 painter->setCompositionMode(QPainter::CompositionMode_Source);
00068 }
00069 }
00070
00071 void MythQImagePainter::End(void)
00072 {
00073 if (!painter)
00074 return;
00075
00076 painter->end();
00077 delete painter;
00078
00079 MythPainter::End();
00080 }
00081
00082 void MythQImagePainter::SetClipRect(const QRect &clipRect)
00083 {
00084 if (!painter)
00085 return;
00086
00087 if (!clipRect.isEmpty())
00088 {
00089 painter->setClipping(true);
00090 if (clipRegion.isEmpty())
00091 clipRegion = QRegion(clipRect);
00092 else
00093 clipRegion = clipRegion.united(clipRect);
00094 painter->setClipRegion(clipRegion);
00095 }
00096 else
00097 painter->setClipping(false);
00098 }
00099
00100 void MythQImagePainter::SetClipRegion(const QRegion ®ion)
00101 {
00102 if (!painter)
00103 return;
00104
00105 if (!region.isEmpty())
00106 {
00107 painter->setClipping(true);
00108 clipRegion = region;
00109 painter->setClipRegion(clipRegion);
00110 }
00111 else
00112 painter->setClipping(false);
00113 }
00114
00115 void MythQImagePainter::Clear(QPaintDevice *device, const QRegion ®ion)
00116 {
00117 if (!device || region.isEmpty())
00118 return;
00119
00120 QImage *dev = dynamic_cast<QImage*>(device);
00121 if (!dev)
00122 return;
00123
00124 int img_width = dev->size().width();
00125 int img_height = dev->size().height();
00126
00127 QVector<QRect> rects = region.rects();
00128 for (int i = 0; i < rects.size(); i++)
00129 {
00130 if (rects[i].top() > img_height || rects[i].left() > img_width)
00131 continue;
00132
00133 int bottom = std::min(rects[i].top() + rects[i].height(), img_height);
00134 int bwidth = std::min(rects[i].left() + rects[i].width(), img_width);
00135 bwidth = (bwidth - rects[i].left()) << 2;
00136
00137 for (int row = rects[i].top(); row < bottom; row++)
00138 memset(dev->scanLine(row) + (rects[i].left() << 2), 0, bwidth);
00139 }
00140 }
00141
00142 void MythQImagePainter::DrawImage(const QRect &r, MythImage *im,
00143 const QRect &src, int alpha)
00144 {
00145 if (!painter)
00146 {
00147 LOG(VB_GENERAL, LOG_ERR,
00148 "FATAL ERROR: DrawImage called with no painter");
00149 return;
00150 }
00151
00152 (void)alpha;
00153
00154 CheckPaintMode(QRect(r.topLeft(), src.size()));
00155 painter->setOpacity(static_cast<float>(alpha) / 255.0);
00156 painter->drawImage(r.topLeft(), (QImage)(*im), src);
00157 painter->setOpacity(1.0);
00158 }
00159
00160 void MythQImagePainter::DrawText(const QRect &r, const QString &msg,
00161 int flags, const MythFontProperties &font,
00162 int alpha, const QRect &boundRect)
00163 {
00164 MythPainter::DrawText(r, msg, flags, font, alpha, boundRect);
00165 }
00166
00167 void MythQImagePainter::DrawRect(const QRect &area, const QBrush &fillBrush,
00168 const QPen &linePen, int alpha)
00169 {
00170 MythPainter::DrawRect(area, fillBrush, linePen, alpha);
00171 }
00172
00173 void MythQImagePainter::DrawRoundRect(const QRect &area, int cornerRadius,
00174 const QBrush &fillBrush,
00175 const QPen &linePen, int alpha)
00176 {
00177 MythPainter::DrawRoundRect(area, cornerRadius, fillBrush, linePen, alpha);
00178 }
00179
00180 void MythQImagePainter::DrawEllipse(const QRect &area, const QBrush &fillBrush,
00181 const QPen &linePen, int alpha)
00182 {
00183 MythPainter::DrawEllipse(area, fillBrush, linePen, alpha);
00184 }