aboutsummaryrefslogtreecommitdiff
path: root/src/animationloader.cpp
diff options
context:
space:
mode:
authorSalanto <62221668+Salanto@users.noreply.github.com>2024-05-24 04:54:48 +0200
committerGitHub <noreply@github.com>2024-05-24 04:54:48 +0200
commit4c56a25463d15cf12e21fe512a598bee91b3363d (patch)
treeb0478364cd4d267c97334164aa876b41c1a841f9 /src/animationloader.cpp
parent4b0f7e4d806c79313e493a3c58818e995af25847 (diff)
parenteb024cb93131cddba8ec1a6094abde8bf1f4eaf3 (diff)
Merge pull request #966 from AttorneyOnline/coolslide-rebased
[Feature] Courtroom slides + major AOLayer overhaul
Diffstat (limited to 'src/animationloader.cpp')
-rw-r--r--src/animationloader.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/animationloader.cpp b/src/animationloader.cpp
new file mode 100644
index 00000000..5f57cbe5
--- /dev/null
+++ b/src/animationloader.cpp
@@ -0,0 +1,105 @@
+#include "animationloader.h"
+
+#include <QMutexLocker>
+#include <QtConcurrent/QtConcurrent>
+
+namespace kal
+{
+AnimationLoader::AnimationLoader(QThreadPool *threadPool)
+ : m_thread_pool(threadPool)
+{}
+
+AnimationLoader::~AnimationLoader()
+{
+ stopLoading();
+}
+
+QString AnimationLoader::loadedFileName() const
+{
+ return m_file_name;
+}
+
+void AnimationLoader::load(const QString &fileName)
+{
+ if (m_file_name == fileName)
+ {
+ return;
+ }
+ stopLoading();
+ m_file_name = fileName;
+ QImageReader *reader = new QImageReader;
+ reader->setFileName(fileName);
+ m_size = reader->size();
+ m_frames.clear();
+ m_frame_count = reader->imageCount();
+ m_loop_count = reader->loopCount();
+ m_exit_task = false;
+ m_task = QtConcurrent::run(m_thread_pool, [this, reader]() { populateVector(reader); });
+}
+
+void AnimationLoader::stopLoading()
+{
+ m_exit_task = true;
+ if (m_task.isRunning())
+ {
+ m_task.waitForFinished();
+ }
+}
+
+QSize AnimationLoader::size()
+{
+ return m_size;
+}
+
+int AnimationLoader::frameCount()
+{
+ return m_frame_count;
+}
+
+AnimationFrame AnimationLoader::frame(int frameNumber)
+{
+ if (m_frame_count <= 0)
+ {
+ return AnimationFrame();
+ }
+
+ m_task_lock.lock();
+ while (m_frames.size() < frameNumber + 1)
+ {
+#ifdef DEBUG_MOVIE
+ qDebug().noquote() << "Waiting for frame" << frameNumber << QString("(file: %1, frame count: %2)").arg(m_file_name).arg(m_frame_count);
+#endif
+ m_task_signal.wait(&m_task_lock);
+ }
+
+ AnimationFrame frame = qAsConst(m_frames)[frameNumber];
+ m_task_lock.unlock();
+
+ return frame;
+}
+
+int AnimationLoader::loopCount()
+{
+ return m_loop_count;
+}
+
+void AnimationLoader::populateVector(QImageReader *reader)
+{
+ int loaded_frame_count = 0;
+ int frame_count = reader->imageCount();
+ while (!m_exit_task && loaded_frame_count < frame_count)
+ {
+ {
+ QMutexLocker locker(&m_task_lock);
+ AnimationFrame frame;
+ frame.texture = QPixmap::fromImage(reader->read());
+ frame.duration = reader->nextImageDelay();
+ m_frames.append(frame);
+ ++loaded_frame_count;
+ }
+ m_task_signal.wakeAll();
+ }
+
+ delete reader;
+}
+} // namespace kal