Go to the documentation of this file.
13 #include <QCoreApplication>
15 #include <QDomDocument>
18 #include <QImageReader>
19 #include <QReadWriteLock>
38 #define LOC QString("MythUIImage(0x%1): ").arg((uint64_t)this,0,16)
124 LOG(VB_GUI | VB_FILE, LOG_DEBUG,
125 QString(
"ImageLoader::PreLoad(%1), this "
126 "file is already being loaded by this same MythUIImage "
127 "in another thread.").arg(cacheKey));
152 QString extension =
filename.section(
'.', -1);
153 return !
filename.startsWith(
"myth://") &&
154 (extension ==
"gif" ||
155 extension ==
"apng" ||
172 s_Attrib +=
"reflected";
175 s_Attrib +=
"greyscale";
179 s_Attrib +=
"orientation";
195 imagelabel = QString(
"%1-%2-%3x%4.png")
200 imagelabel.replace(
'/',
'-');
202 imagelabel.replace(
':',
'-');
220 if (!
PreLoad(cacheKey, parent))
229 bool bResize =
false;
230 bool bFoundInCache =
false;
258 LOG(VB_GUI | VB_FILE, LOG_INFO,
259 QString(
"ImageLoader::LoadImage(%1) Found in cache, "
261 .arg(cacheKey).arg(cnt));
270 bFoundInCache =
true;
274 LOG(VB_GUI | VB_FILE, LOG_INFO,
275 QString(
"ImageLoader::LoadImage(%1) NOT Found in cache. "
276 "Loading Directly").arg(cacheKey));
282 ok = image->
Load(imageReader);
293 if (image && image->isNull())
295 LOG(VB_GUI | VB_FILE, LOG_INFO,
296 QString(
"ImageLoader::LoadImage(%1) Image is NULL")
303 if (image && !bFoundInCache)
326 if (wmult != 1.0F || hmult != 1.0F)
328 w = image->size().width() * wmult;
329 h = image->size().height() * hmult;
345 if (wmult != 1.0F || hmult != 1.0F)
347 int width = newMaskImage->size().width() * wmult;
348 int height = newMaskImage->size().height() * hmult;
349 newMaskImage->
Resize(QSize(width, height));
358 QRect imageArea = image->rect();
365 if (maskArea.width() > imageArea.width())
366 x = (maskArea.width() - imageArea.width()) / 2;
368 if (maskArea.height() > imageArea.height())
369 y = (maskArea.height() - imageArea.height()) / 2;
372 imageArea.translate(x, y);
375 image->setAlphaChannel(mask.convertToFormat(QImage::Format_Alpha8));
401 QString frameFilename;
407 while (imageReader->canRead() && !aborted)
409 frameFilename =
filename.arg(imageCount);
415 aborted, imageReader);
420 images->append(
AnimationFrame(im, std::chrono::milliseconds(imageReader->nextImageDelay())));
443 int number,
bool aborted)
482 (QEvent::Type) QEvent::registerEventType();
500 bool aborted =
false;
513 if (frames && frames->count() > 1)
518 QCoreApplication::postEvent(
m_parent, le);
533 QCoreApplication::postEvent(
m_parent, le);
561 int low,
int high, std::chrono::milliseconds delay,
624 QHash<int, MythImage *>::iterator it =
m_images.begin();
734 for (std::chrono::milliseconds delay : qAsConst(delays))
765 if (!forceSize.isNull())
767 int w = (forceSize.width() <= 0) ? img->width() : forceSize.width();
768 int h = (forceSize.height() <= 0) ? img->height() : forceSize.height();
819 for (
auto *im : qAsConst(*images))
832 if (!forceSize.isNull())
834 int w = (forceSize.width() <= 0) ? im->width() : forceSize.width();
835 int h = (forceSize.height() <= 0) ? im->height() : forceSize.height();
858 aSize = aSize.expandedTo(im->size());
878 QVector<std::chrono::milliseconds> delays;
879 QVector<MythImage *> images;
881 for (
const auto & frame : qAsConst(frames))
883 images.append(frame.first);
884 delays.append(frame.second);
891 if (
m_delay < 0ms && !delays.empty())
980 if (bFilename.isEmpty())
989 if (qEnvironmentVariableIsSet(
"DISABLETHREADEDMYTHUIIMAGE"))
990 allowLoadInBackground =
false;
1000 bool complete =
true;
1009 bFilename.contains(
"%1"))
1028 bool do_background_load =
false;
1029 if (allowLoadInBackground)
1037 do_background_load =
true;
1040 if (do_background_load)
1043 LOG(VB_GUI | VB_FILE, LOG_DEBUG,
LOC +
1044 QString(
"Load(), spawning thread to load '%1'").arg(
filename));
1048 imProps, bFilename, i,
1054 if (!isAnimation && !
GetMythUI()->IsImageInCache(imagelabel))
1058 LOG(VB_GUI | VB_FILE, LOG_DEBUG,
LOC +
1059 QString(
"Load(), loading '%1' in foreground").arg(
filename));
1060 bool aborted =
false;
1072 LOG(VB_GUI, LOG_DEBUG, QString(
"Aborted loading animated"
1073 "image %1 in foreground")
1093 LOG(VB_GUI, LOG_DEBUG, QString(
"Aborted loading animated"
1094 "image %1 in foreground")
1104 rect.setSize(image->size());
1130 complete &= !do_background_load;
1154 abs(
m_lastDisplay.msecsTo(QTime::currentTime())) > delay.count())
1208 int alphaMod, QRect clipRect)
1234 area.translate(xoffset, yoffset);
1251 QRect currentImageArea = currentImage->rect();
1254 area.setSize(area.size().expandedTo(currentImage->size()));
1261 if (area.width() > visibleImage.width())
1262 x = area.width() / 2 + visibleImage.topLeft().x();
1264 if (area.height() > visibleImage.height())
1265 y = area.height() / 2 + visibleImage.topLeft().y();
1267 if ((x > 0 || y > 0))
1268 area.translate(x, y);
1276 srcRect = currentImageArea;
1278 p->SetClipRect(clipRect);
1279 p->DrawImage(area, currentImage, srcRect, alpha);
1291 const QString &
filename, QDomElement &element,
bool showWarnings)
1295 if (element.tagName() ==
"filename")
1308 else if (element.tagName() ==
"filepattern")
1312 QString
tmp = element.attribute(
"low");
1317 tmp = element.attribute(
"high");
1322 tmp = element.attribute(
"cycle",
"start");
1324 if (
tmp ==
"reverse")
1327 else if (element.tagName() ==
"area")
1332 else if (element.tagName() ==
"preserveaspect")
1334 else if (element.tagName() ==
"crop")
1336 else if (element.tagName() ==
"delay")
1340 if (value.contains(
","))
1342 QVector<std::chrono::milliseconds> delays;
1343 QStringList tokens = value.split(
",");
1344 for (
const auto & token : qAsConst(tokens))
1346 if (token.isEmpty())
1348 if (!delays.empty())
1349 delays.append(delays[delays.size()-1]);
1355 delays.append(std::chrono::milliseconds(token.toInt()));
1359 if (!delays.empty())
1367 m_delay = std::chrono::milliseconds(value.toInt());
1370 else if (element.tagName() ==
"reflection")
1373 QString
tmp = element.attribute(
"axis");
1377 if (
tmp.toLower() ==
"horizontal")
1383 tmp = element.attribute(
"shear");
1388 tmp = element.attribute(
"scale");
1393 tmp = element.attribute(
"length");
1398 tmp = element.attribute(
"spacing");
1403 else if (element.tagName() ==
"mask")
1408 else if (element.tagName() ==
"grayscale" ||
1409 element.tagName() ==
"greyscale")
1435 LOG(VB_GENERAL, LOG_ERR,
1436 QString(
"'%1' (%2) ERROR, bad parsing '%3' (%4)")
1539 if (!le || le->GetParent() !=
this)
1543 int number = le->GetNumber();
1544 QString
filename = le->GetFilename();
1546 bool aborted = le->GetAbortState();
1559 if (aborted || (le->GetBasefile() != propFilename))
1562 LOG(VB_GUI, LOG_DEBUG, QString(
"Aborted loading image %1")
1568 if (animationFrames)
1570 for (
const auto & frame : qAsConst(*animationFrames))
1577 delete animationFrames;
1580 else if (animationFrames)
1584 delete animationFrames;
1600 rect.setSize(image->size());
1644 if (!imageDir.exists())
1650 QStringList imageTypes;
1652 QList< QByteArray > exts = QImageReader::supportedImageFormats();
1653 for (
const auto & ext : qAsConst(exts))
1655 imageTypes.append(QString(
"*.").append(ext));
1658 imageDir.setNameFilters(imageTypes);
1669 std::random_device rd;
1670 std::mt19937 g(rd());
1684 std::random_device rd;
1685 std::mt19937 g(rd());
friend class MythUIImagePrivate
void SetDelays(const QVector< std::chrono::milliseconds > &delays)
Sets the delays between each image in an animation.
ImageCacheMode m_cacheMode
void SetMaskImage(MythImage *image)
bool GetAbortState() const
Image widget, displays a single image or multiple images in sequence.
ImageLoadThread(MythUIImage *parent, MythPainter *painter, const ImageProperties &imProps, QString basefile, int number, ImageCacheMode mode)
bool Load(MythImageReader *reader)
QHash< int, MythImage * > m_images
QReadWriteLock m_updateLock
MythImage * GetImage() const
virtual MythRect GetFullArea(void) const
int CalcAlpha(int alphamod) const
QRect toQRect(void) const
void Reflect(ReflectAxis axis, int shear, int scale, int length, int spacing=0)
MythUIImage(const QString &filepattern, int low, int high, std::chrono::milliseconds delay, MythUIType *parent, const QString &name)
bool Load(bool allowLoadInBackground=true, bool forceStat=false)
Load the image(s), wraps ImageLoader::LoadImage()
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
static QHash< QString, const MythUIImage * > m_loadingImages
static void PostLoad(const QString &cacheKey)
QHash< int, std::chrono::milliseconds > m_delays
static QMutex m_loadingImagesLock
static bool VERBOSE_LEVEL_CHECK(uint64_t mask, LogLevel_t level)
virtual MythPainter * GetPainter(void)
ReflectAxis m_reflectAxis
static QString GenImageLabel(const ImageProperties &imProps)
Generates a unique identifying string for this image which is used as a key in the image cache.
ImageLoadEvent(const MythUIImage *parent, AnimationFrames *frames, QString basefile, QString filename, bool aborted)
void customEvent(QEvent *event) override
void SetOrientation(int orientation)
Saves the exif orientation value of the first image in the widget.
virtual void SetArea(const MythRect &rect)
QString GetBasefile() const
QImage GetMaskImageSubset(QRect imageArea)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
AnimationFrames * GetAnimationFrames() const
void LoadNow(void) override
Cause images in this and child widgets to be loaded.
void SetFilepattern(const QString &filepattern, int low, int high)
Must be followed by a call to Load() to load the image.
void Clear(void)
Remove all images from the widget.
void Reset(void) override
Reset the image back to the default defined in the theme.
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
virtual void Pulse(void)
Pulse is called 70 times a second to trigger a single frame of an animation.
QVector< AnimationFrame > AnimationFrames
static QWaitCondition m_loadingImagesCond
bool m_showingRandomImage
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
void setIsOriented(bool oriented)
std::chrono::milliseconds m_delay
void SetCropRect(int x, int y, int width, int height)
Crop the image using the given rectangle, useful for removing unsightly edges from imported images or...
QString m_maskImageFilename
static bool PreLoad(const QString &cacheKey, const MythUIImage *uitype)
ImageProperties & operator=(const ImageProperties &other)
void SetSize(int width, int height)
Set the size of the widget.
QString GetXMLLocation(void) const
virtual void SetMinArea(const MythRect &rect)
Set the minimum area based on the given size.
void DependChanged(bool isDefault)
~MythUIImagePrivate()=default
void SetAnimationFrames(const AnimationFrames &frames)
AnimationFrames * m_images
static QString getFirstText(QDomElement &element)
int DecrRef(void) override
Decrements reference count and deletes on 0.
void CalculateArea(QRect parentArea)
friend class ImageLoadThread
void setIsReflected(bool reflected)
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void Copy(const ImageProperties &other)
void SetDelay(std::chrono::milliseconds delayms)
Set the delay between each image in an animation.
MythImage * LoadCacheImage(QString File, const QString &Label, MythPainter *Painter, ImageCacheMode cacheMode=kCacheNormal)
static MythRect parseRect(const QString &text, bool normalize=true)
virtual void SetChanged(bool change=true)
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
void SetMaskImageFilename(const QString &filename)
static bool SupportsAnimation(const QString &filename)
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
const MythUIImage * m_parent
virtual void Reset(void)
Reset the widget to it's original state, should not reset changes made by the theme.
static AnimationFrames * LoadAnimatedImage(MythPainter *painter, const ImageProperties &imProps, ImageCacheMode cacheMode, const MythUIImage *parent, bool &aborted)
The base class on which all widgets and screens are based.
void Orientation(int orientation)
Changes the orientation angle of the image according to the exif rotation values.
bool IsDeferredLoading(bool recurse=false) const
void SetImageCount(int low, int high)
Set the integer range for an animated image pattern.
ImageProperties m_imageProperties
QString GetFilename() const
int IncrRef(void) override
Increments reference count.
void SetImages(QVector< MythImage * > *images)
Should not be used unless absolutely necessary since it bypasses the image caching and threaded loade...
QRect GetMaskImageRect(void)
virtual void LoadNow(void)
Cause images in this and child widgets to be loaded.
ImageProperties()=default
virtual void SetSize(QSize size)
void ForceSize(QSize size)
Force the dimensions of the widget and image to the given size.
MythUIImagePrivate(MythUIImage *p)
MythMainWindow * GetMythMainWindow(void)
void FindRandomImage(void)
QString GetMaskImageFilename()
void Pulse(void) override
Pulse is called 70 times a second to trigger a single frame of an animation.
ImageLoadEvent(const MythUIImage *parent, MythImage *image, QString basefile, QString filename, int number, bool aborted)
void Resize(QSize newSize, bool preserveAspect=false)
void SetImage(MythImage *img)
Should not be used unless absolutely necessary since it bypasses the image caching and threaded loade...
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
ImageProperties m_imageProperties
void SetFilename(const QString &filename)
Must be followed by a call to Load() to load the image.
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
MythUIHelper * GetMythUI()
void GetScalingFactors(float &Horizontal, float &Vertical) const
AnimationCycle m_animationCycle
static bool parseBool(const QString &text)
QRect GetExtent(QSize size) const
static MythImage * LoadImage(MythPainter *painter, ImageProperties imProps, ImageCacheMode cacheMode, const MythUIImage *parent, bool &aborted, MythImageReader *imageReader=nullptr)
void start(QRunnable *runnable, const QString &debugName, int priority=0)
virtual void Finalize(void)
Perform any post-xml parsing initialisation tasks.
MThreadPool * GetImageThreadPool()
const MythUIImage * GetParent() const
MythImage * CacheImage(const QString &URL, MythImage *Image, bool NoDisk=false)
QString GetFileName(void) const
QPair< MythImage *, std::chrono::milliseconds > AnimationFrame
void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect) override