Skip to content

Commit

Permalink
MythUIImage: improve random image selection
Browse files Browse the repository at this point in the history
We now cache the list of available images and randomise that list. We then
iterate over the random list which makes sure we don't re-show an image
until every image has been shown once. We then re-randomise the list and
start over again.
  • Loading branch information
paul-h committed Dec 4, 2021
1 parent 8fdd00c commit b60551c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
69 changes: 49 additions & 20 deletions mythtv/libs/libmythui/mythuiimage.cpp
Expand Up @@ -1634,36 +1634,65 @@ void MythUIImage::customEvent(QEvent *event)

void MythUIImage::FindRandomImage(void)
{
QDir imageDir(m_imageDirectory);
QString randFile;

if (!imageDir.exists())
// find and save the list of available images
if (m_imageList.isEmpty())
{
QString themeDir = GetMythUI()->GetThemeDir() + '/';
imageDir.setPath(themeDir + m_imageDirectory);
}
QDir imageDir(m_imageDirectory);

QStringList imageTypes;
if (!imageDir.exists())
{
QString themeDir = GetMythUI()->GetThemeDir() + '/';
imageDir.setPath(themeDir + m_imageDirectory);
}

QList< QByteArray > exts = QImageReader::supportedImageFormats();
for (const auto & ext : qAsConst(exts))
{
imageTypes.append(QString("*.").append(ext));
}
QStringList imageTypes;

imageDir.setNameFilters(imageTypes);
QList< QByteArray > exts = QImageReader::supportedImageFormats();
for (const auto & ext : qAsConst(exts))
{
imageTypes.append(QString("*.").append(ext));
}

QStringList imageList = imageDir.entryList();
QString randFile;
imageDir.setNameFilters(imageTypes);

if (!imageList.empty())
m_imageList = imageDir.entryList();

if (m_imageList.empty())
{
m_origFilename = m_imageProperties.m_filename = randFile;
return;
}

// randomly shuffle the images
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(m_imageList.begin(), m_imageList.end(), g);
m_imageListIndex = 0;
randFile = QString("%1%2").arg(m_imageDirectory, m_imageList.at(m_imageListIndex));
}
else
{
// try to find a different image
do
if (!m_imageList.empty())
{
uint32_t rand = MythRandom() % static_cast<uint32_t>(imageList.size());
randFile = QString("%1%2").arg(m_imageDirectory, imageList.takeAt(static_cast<int>(rand)));
m_imageListIndex++;

} while (imageList.size() > 1 && randFile == m_origFilename);
// if we are at the last image in the list re-shuffle the list and start from the beginning
if (m_imageListIndex == m_imageList.size())
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(m_imageList.begin(), m_imageList.end(), g);
m_imageListIndex = 0;
}

// make sure we don't show the same image again in the unlikely event the re-shuffle shows the same image again
if (m_imageList.at(m_imageListIndex) == m_origFilename && m_imageList.size() > 1)
m_imageListIndex++;

randFile = QString("%1%2").arg(m_imageDirectory, m_imageList.at(m_imageListIndex));
}
}

m_origFilename = m_imageProperties.m_filename = randFile;
Expand Down
2 changes: 2 additions & 0 deletions mythtv/libs/libmythui/mythuiimage.h
Expand Up @@ -186,6 +186,8 @@ class MUI_PUBLIC MythUIImage : public MythUIType

bool m_showingRandomImage {false};
QString m_imageDirectory;
QStringList m_imageList;
int m_imageListIndex {0};

MythUIImagePrivate *d {nullptr}; // NOLINT(readability-identifier-naming)

Expand Down

0 comments on commit b60551c

Please sign in to comment.