MythTV  master
gallerytransitions.cpp
Go to the documentation of this file.
1 #include "gallerytransitions.h"
2 
3 #include <utility>
4 
5 #include "mythcorecontext.h"
6 
7 #if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
8 #include <QRandomGenerator>
9 #endif
10 
11 #define LOC QString("Transition: ")
12 
13 
14 Transition::Transition(const QString& name)
15  : m_duration(gCoreContext->GetNumSetting("GalleryTransitionTime", 1000))
16 {
17  setObjectName(name);
18 }
19 
20 
28 {
29  // Guard against garbage
30  int value = (setting >= kNoTransition && setting < kLastTransSentinel)
31  ? setting : kNoTransition;
32 
33  // If chosen transition isn't viable for painter then use previous ones.
34  // First transition must always be useable by all painters
35  Transition *result = nullptr;
36  do
37  result = m_map.value(value--, nullptr);
38  while (value >= kNoTransition && !result);
39 
40  if (result)
41  return *result;
42 
43  LOG(VB_GENERAL, LOG_CRIT,
44  LOC + QString("No transitions found for setting %1").arg(setting));
45 
46  return m_immediate;
47 }
48 
49 
55 {
56  // Create all non-animated transitions to be used by Random
57  m_map.insert(kBlendTransition, new TransitionBlend());
58  m_map.insert(kSlideTransition, new TransitionSlide());
59 
60  // Create all animated transitions to be used by Random
61  if (includeAnimations)
62  {
63  m_map.insert(kTwistTransition, new TransitionTwist());
64  m_map.insert(kZoomTransition, new TransitionZoom());
65  m_map.insert(kSpinTransition, new TransitionSpin());
66  }
67 
68  // Create random set from those already defined
69  m_map.insert(kRandomTransition, new TransitionRandom(m_map.values()));
70 
71  // Create other transitions that aren't to be used by Random
72  m_map.insert(kNoTransition, new TransitionNone());
73 }
74 
75 
83 void Transition::Start(Slide &from, Slide &to,
84  bool forwards, float speed)
85 {
86  LOG(VB_FILE, LOG_DEBUG, LOC +
87  QString("Starting transition %1 -> %2 (forwards= %3, speed= %4)")
88  .arg(from.objectName(), to.objectName()).arg(forwards).arg(speed));
89 
90  m_old = &from;
91  m_new = &to;
92 
93  // When playing forwards next image replaces prev image
94  // When playing backwards prev image replaces next image
95  m_prev = forwards ? m_old : m_new;
96  m_next = forwards ? m_new : m_old;
97 
98  // Set up transition
99  Initialise();
100 
101  // Make new image visible
102  m_new->SetVisible(true);
103 }
104 
105 
110 {
111  // Hide old image before it is reset
112  m_old->SetVisible(false);
113 
114  // Undo transition effects
115  Finalise();
116 
117  LOG(VB_FILE, LOG_DEBUG, LOC +
118  QString("Finished transition to %1").arg(m_new->objectName()));
119 
120  emit finished();
121 }
122 
123 
132  bool forwards, float speed)
133 {
134  Transition::Start(from, to, forwards, speed);
135 
136  // Finish immediately
137  Finished();
138 }
139 
140 
147 GroupTransition::GroupTransition(GroupAnimation *animation, const QString& name)
148  : Transition(name), m_animation(animation)
149 {
150  // Complete transition when the group finishes
152 }
153 
154 
159 {
160  // Group owns animation
161  delete m_animation;
162 }
163 
164 
173  bool forwards, float speed)
174 {
175  // Clear previous transition
176  m_animation->Clear();
177 
178  Transition::Start(from, to, forwards, speed);
179 
180  // Start group
181  if (m_animation)
182  m_animation->Start(forwards, speed);
183 }
184 
185 
190 void GroupTransition::SetSpeed(float speed)
191 {
192  if (m_animation)
193  m_animation->SetSpeed(speed);
194 }
195 
196 
200 {
201  if (m_animation)
202  m_animation->Pulse();
203 }
204 
205 
210 {
211  // Fade out prev image
212  auto *oldPic = new Animation(m_prev, Animation::Alpha);
213  oldPic->Set(255, 0, m_duration, QEasingCurve::InOutQuad);
214  m_animation->Add(oldPic);
215 
216  // Fade in next image
217  auto *newPic = new Animation(m_next, Animation::Alpha);
218  newPic->Set(0, 255, m_duration, QEasingCurve::InOutQuad);
219  m_animation->Add(newPic);
220 
221  // Hide new image until it's needed
222  m_new->SetAlpha(0);
223 }
224 
225 
230 {
231  // Undo effects of the transition
232  m_old->SetAlpha(255);
233 }
234 
235 
240 {
241  // Reduce hzoom of left image to nothing
242  auto *oldPic = new Animation(m_prev, Animation::HorizontalZoom);
243  oldPic->Set(1.0, 0.0, m_duration / 2, QEasingCurve::InQuart);
244  m_animation->Add(oldPic);
245 
246  // Increase hzoom of right image from nothing to full
247  auto *newPic = new Animation(m_next, Animation::HorizontalZoom);
248  newPic->Set(0.0, 1.0, m_duration / 2, QEasingCurve::OutQuart);
249  m_animation->Add(newPic);
250 
251  // Hide new image until it's needed
253 }
254 
255 
260 {
261  // Undo effects of the transition
262  m_old->SetHorizontalZoom(1.0);
263 }
264 
265 
270 {
271  // Both images are same size
272  int width = m_old->GetArea().width();
273 
274  // Slide off to left
275  auto *oldPic = new Animation(m_prev, Animation::Position);
276  oldPic->Set(QPoint(0, 0), QPoint(-width, 0), m_duration, QEasingCurve::InOutQuart);
277  m_animation->Add(oldPic);
278 
279  // Slide in from right
280  auto *newPic = new Animation(m_next, Animation::Position);
281  newPic->Set(QPoint(width, 0), QPoint(0, 0), m_duration, QEasingCurve::InOutQuart);
282  m_animation->Add(newPic);
283 
284  // Hide new image until it's needed
285  m_new->SetPosition(width, 0);
286 }
287 
288 
293 {
294  // Undo effects of the transition
295  m_old->SetPosition(0,0);
296 }
297 
298 
303 {
304  // Both images are same size
305  int width = m_old->GetArea().width();
306 
307  // Zoom away to left
308  auto *oldZoom = new Animation(m_prev, Animation::Zoom);
309  oldZoom->Set(1.0, 0.0, m_duration, QEasingCurve::OutQuad);
310  m_animation->Add(oldZoom);
311 
312  auto *oldMove = new Animation(m_prev, Animation::Position);
313  oldMove->Set(QPoint(0, 0), QPoint(-width / 2, 0), m_duration,
314  QEasingCurve::InQuad);
315  m_animation->Add(oldMove);
316 
317  // Zoom in from right
318  auto *newZoom = new Animation(m_next, Animation::Zoom);
319  newZoom->Set(0.0, 1.0, m_duration, QEasingCurve::InQuad);
320  m_animation->Add(newZoom);
321 
322  auto *newMove = new Animation(m_next, Animation::Position);
323  newMove->Set(QPoint(width / 2, 0), QPoint(0, 0), m_duration,
324  QEasingCurve::OutQuad);
325  m_animation->Add(newMove);
326 
327  // Hide new image until it's needed
328  m_new->SetZoom(0.0);
329 }
330 
331 
337 {
338  // Undo effects of the transition
339  m_old->SetZoom(1.0);
340  m_old->SetPosition(0, 0);
341 }
342 
343 
348 {
349  // Set up blend
351 
352  // Add simultaneous spin
353  auto *an = new Animation(m_prev, Animation::Angle);
354  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
355  m_animation->Add(an);
356 
357  an = new Animation(m_next, Animation::Angle);
358  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
359  m_animation->Add(an);
360 
361  // Zoom prev away, then back
362  auto *seq = new SequentialAnimation();
363  m_animation->Add(seq);
364 
365  an = new Animation(m_prev, Animation::Zoom);
366  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
367  seq->Add(an);
368 
369  an = new Animation(m_prev, Animation::Zoom);
370  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
371  seq->Add(an);
372 
373  // Zoom next away, then back
374  seq = new SequentialAnimation();
375  m_animation->Add(seq);
376 
377  an = new Animation(m_next, Animation::Zoom);
378  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
379  seq->Add(an);
380 
381  an = new Animation(m_next, Animation::Zoom);
382  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
383  seq->Add(an);
384 }
385 
386 
392 {
393  // Undo blend
395 
396  // Undo effects of the transition
397  m_old->SetZoom(1.0);
398  m_old->SetAngle(0.0);
399 }
400 
401 
409 void TransitionRandom::Start(Slide &from, Slide &to, bool forwards, float speed)
410 {
411  // Select a random peer.
412 #if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
413  int rand = QRandomGenerator::global()->bounded(m_peers.size());
414 #else
415  // cppcheck-suppress qrandCalled
416  int rand = qrand() % m_peers.size();
417 #endif
418  m_current = m_peers[rand];
419  // Invoke peer
421  m_current->Start(from, to, forwards, speed);
422 }
423 
424 
429 {
430  // Clean up
431  disconnect(m_current, nullptr, nullptr, nullptr);
432  m_current = nullptr;
433  emit finished();
434 }
435 
436 
Slide
A specialised image for slideshows.
Definition: galleryslide.h:157
Slide::SetZoom
void SetZoom(float zoom)
Sets slide zoom.
Definition: galleryslide.cpp:472
Transition::Finished
virtual void Finished()
Transition has completed.
Definition: gallerytransitions.cpp:109
TransitionTwist::Initialise
void Initialise() override
Set up Twist transition.
Definition: gallerytransitions.cpp:239
GroupTransition::GroupTransition
GroupTransition(GroupAnimation *animation, const QString &name)
Create group transition.
Definition: gallerytransitions.cpp:147
Animation::HorizontalZoom
@ HorizontalZoom
Definition: galleryslide.h:59
TransitionRandom::m_current
Transition * m_current
Selected transition.
Definition: gallerytransitions.h:179
TransitionZoom::Finalise
void Finalise() override
Reset Zoom transition.
Definition: gallerytransitions.cpp:336
kTwistTransition
@ kTwistTransition
Definition: gallerytransitions.h:19
arg
arg(title).arg(filename).arg(doDelete))
TransitionRandom::m_peers
QList< Transition * > m_peers
Set of distinct transitions.
Definition: gallerytransitions.h:177
TransitionSpin::Finalise
void Finalise() override
Reset Spin transition.
Definition: gallerytransitions.cpp:391
Transition::Finalise
virtual void Finalise()
Definition: gallerytransitions.h:39
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
Transition::m_new
Slide * m_new
The new image that will replace the current one (whatever the transition direction)
Definition: gallerytransitions.h:55
Transition
Base class of an animated transition that can be accelerated & reversed.
Definition: gallerytransitions.h:28
TransitionSlide::Initialise
void Initialise() override
Set up Slide transition.
Definition: gallerytransitions.cpp:269
GroupTransition::m_animation
GroupAnimation * m_animation
Definition: gallerytransitions.h:97
Animation::Angle
@ Angle
Definition: galleryslide.h:59
TransitionBlend::Finalise
void Finalise() override
Reset Blend transition.
Definition: gallerytransitions.cpp:229
MythUIType::GetArea
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:870
TransitionZoom
Zooms from right, then away to left.
Definition: gallerytransitions.h:135
gallerytransitions.h
Provides transitions for slideshows.
MythUIType::SetAlpha
void SetAlpha(int newalpha)
Definition: mythuitype.cpp:931
TransitionTwist::Finalise
void Finalise() override
Reset Twist transition.
Definition: gallerytransitions.cpp:259
Transition::Transition
Transition(const QString &name)
Definition: gallerytransitions.cpp:14
TransitionSlide::Finalise
void Finalise() override
Reset Slide transition.
Definition: gallerytransitions.cpp:292
kSpinTransition
@ kSpinTransition
Definition: gallerytransitions.h:22
GroupAnimation
Abstract class for groups of animations.
Definition: galleryslide.h:91
GroupTransition::~GroupTransition
~GroupTransition() override
Destroy group transition.
Definition: gallerytransitions.cpp:158
Transition::Start
virtual void Start(Slide &from, Slide &to, bool forwards, float speed=1.0)
Start base transition.
Definition: gallerytransitions.cpp:83
MythUIType::SetPosition
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:518
TransitionNone::Start
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Switch images.
Definition: gallerytransitions.cpp:131
GroupAnimation::Start
void Start(bool forwards, float speed=1.0) override
Initialise & start base animation.
Definition: galleryslide.h:97
TransitionRandom::Finished
void Finished() override
Invoked when peer transition completes.
Definition: gallerytransitions.cpp:428
kBlendTransition
@ kBlendTransition
Definition: gallerytransitions.h:18
TransitionNone
Switches images instantly with no effects.
Definition: gallerytransitions.h:73
TransitionRandom
Invokes random transitions.
Definition: gallerytransitions.h:156
TransitionBlend::Initialise
void Initialise() override
Set up Blend transition.
Definition: gallerytransitions.cpp:209
Transition::m_duration
int m_duration
Seconds transition will last.
Definition: gallerytransitions.h:49
TransitionSpin
Images blend whilst rotating.
Definition: gallerytransitions.h:146
TransitionRandom::Start
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Starts a random transition from a set of its peer transitions,.
Definition: gallerytransitions.cpp:409
LOC
#define LOC
Definition: gallerytransitions.cpp:11
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
GroupTransition::Pulse
void Pulse() override
Update group transition.
Definition: gallerytransitions.cpp:199
kLastTransSentinel
@ kLastTransSentinel
Definition: gallerytransitions.h:23
TransitionSlide
Slide on from right, then off to left.
Definition: gallerytransitions.h:124
TransitionTwist
Image twists into the next.
Definition: gallerytransitions.h:113
SequentialAnimation
A group of animations to be played sequentially.
Definition: galleryslide.h:111
Animation::Zoom
@ Zoom
Definition: galleryslide.h:59
Animation
A single animation controlling alpha, zoom, rotation and position.
Definition: galleryslide.h:55
GroupAnimation::Pulse
void Pulse() override=0
Animation::Position
@ Position
Definition: galleryslide.h:59
Transition::m_next
Slide * m_next
The image occurring later in the slideshow sequence.
Definition: gallerytransitions.h:65
TransitionSpin::Initialise
void Initialise() override
Set up Spin transition.
Definition: gallerytransitions.cpp:347
mythcorecontext.h
GroupAnimation::SetSpeed
void SetSpeed(float speed) override
Definition: galleryslide.h:99
TransitionRegistry::m_immediate
TransitionNone m_immediate
A transition that updates instantly.
Definition: gallerytransitions.h:193
kZoomTransition
@ kZoomTransition
Definition: gallerytransitions.h:21
GroupAnimation::Add
virtual void Add(AbstractAnimation *child)
Add child animation to group.
Definition: galleryslide.cpp:120
GroupTransition::Start
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Start group transition.
Definition: gallerytransitions.cpp:172
MythUIType::SetVisible
virtual void SetVisible(bool visible)
Definition: mythuitype.cpp:1086
TransitionRegistry::TransitionRegistry
TransitionRegistry(bool includeAnimations)
Constructor.
Definition: gallerytransitions.cpp:54
TransitionRegistry::Select
Transition & Select(int setting)
Returns the requested transition or an alternative if it is not suitable for the painter.
Definition: gallerytransitions.cpp:27
GroupTransition::SetSpeed
void SetSpeed(float speed) override
Change transition speed.
Definition: gallerytransitions.cpp:190
kNoTransition
@ kNoTransition
Definition: gallerytransitions.h:16
MythUIType::SetHorizontalZoom
void SetHorizontalZoom(float zoom)
Definition: mythuitype.cpp:956
GroupAnimation::Clear
void Clear() override
Delete all child animations.
Definition: galleryslide.cpp:131
Transition::finished
void finished()
kSlideTransition
@ kSlideTransition
Definition: gallerytransitions.h:20
Transition::m_prev
Slide * m_prev
The image occurring earlier in the slideshow sequence.
Definition: gallerytransitions.h:63
MythUIType::SetAngle
void SetAngle(float angle)
Definition: mythuitype.cpp:968
Transition::m_old
Slide * m_old
The image currently displayed, which will be replaced (whatever the transition direction)
Definition: gallerytransitions.h:52
kRandomTransition
@ kRandomTransition
Definition: gallerytransitions.h:17
TransitionZoom::Initialise
void Initialise() override
Set up Zoom transition.
Definition: gallerytransitions.cpp:302
Animation::Alpha
@ Alpha
Definition: galleryslide.h:59
AbstractAnimation::finished
void finished()
Signals animation has finished.
TransitionRegistry::m_map
TransitionMap m_map
All possible transitions.
Definition: gallerytransitions.h:197
TransitionBlend
Image blends into the next by fading.
Definition: gallerytransitions.h:102
Transition::Initialise
virtual void Initialise()
Definition: gallerytransitions.h:38