MythTV  master
gallerytransitions.cpp
Go to the documentation of this file.
1 #include "gallerytransitions.h"
2 
3 #include <utility>
4 
6 
7 #include <QRandomGenerator>
8 
9 #define LOC QString("Transition: ")
10 
11 
12 Transition::Transition(const QString& name)
13  : m_duration(gCoreContext->GetDurSetting<std::chrono::milliseconds>("GalleryTransitionTime", 1s))
14 {
15  setObjectName(name);
16 }
17 
18 
26 {
27  // Guard against garbage
28  int value = (setting >= kNoTransition && setting < kLastTransSentinel)
29  ? setting : kNoTransition;
30 
31  // If chosen transition isn't viable for painter then use previous ones.
32  // First transition must always be useable by all painters
33  Transition *result = nullptr;
34  do
35  result = m_map.value(value--, nullptr);
36  while (value >= kNoTransition && !result);
37 
38  if (result)
39  return *result;
40 
41  LOG(VB_GENERAL, LOG_CRIT,
42  LOC + QString("No transitions found for setting %1").arg(setting));
43 
44  return m_immediate;
45 }
46 
47 
53 {
54  // Create all non-animated transitions to be used by Random
55  m_map.insert(kBlendTransition, new TransitionBlend());
56  m_map.insert(kSlideTransition, new TransitionSlide());
57 
58  // Create all animated transitions to be used by Random
59  if (includeAnimations)
60  {
61  m_map.insert(kTwistTransition, new TransitionTwist());
62  m_map.insert(kZoomTransition, new TransitionZoom());
63  m_map.insert(kSpinTransition, new TransitionSpin());
64  }
65 
66  // Create random set from those already defined
67  m_map.insert(kRandomTransition, new TransitionRandom(m_map.values()));
68 
69  // Create other transitions that aren't to be used by Random
70  m_map.insert(kNoTransition, new TransitionNone());
71 }
72 
73 
81 void Transition::Start(Slide &from, Slide &to,
82  bool forwards, float speed)
83 {
84  LOG(VB_FILE, LOG_DEBUG, LOC +
85  QString("Starting transition %1 -> %2 (forwards= %3, speed= %4)")
86  .arg(from.objectName(), to.objectName()).arg(forwards).arg(speed));
87 
88  m_old = &from;
89  m_new = &to;
90 
91  // When playing forwards next image replaces prev image
92  // When playing backwards prev image replaces next image
93  m_prev = forwards ? m_old : m_new;
94  m_next = forwards ? m_new : m_old;
95 
96  // Set up transition
97  Initialise();
98 
99  // Make new image visible
100  m_new->SetVisible(true);
101 }
102 
103 
108 {
109  // Hide old image before it is reset
110  m_old->SetVisible(false);
111 
112  // Undo transition effects
113  Finalise();
114 
115  LOG(VB_FILE, LOG_DEBUG, LOC +
116  QString("Finished transition to %1").arg(m_new->objectName()));
117 
118  emit finished();
119 }
120 
121 
130  bool forwards, float speed)
131 {
132  Transition::Start(from, to, forwards, speed);
133 
134  // Finish immediately
135  Finished();
136 }
137 
138 
145 GroupTransition::GroupTransition(GroupAnimation *animation, const QString& name)
146  : Transition(name), m_animation(animation)
147 {
148  // Complete transition when the group finishes
150 }
151 
152 
157 {
158  // Group owns animation
159  delete m_animation;
160 }
161 
162 
171  bool forwards, float speed)
172 {
173  // Clear previous transition
174  m_animation->Clear();
175 
176  Transition::Start(from, to, forwards, speed);
177 
178  // Start group
179  m_animation->Start(forwards, speed);
180 }
181 
182 
187 void GroupTransition::SetSpeed(float speed)
188 {
189  if (m_animation)
190  m_animation->SetSpeed(speed);
191 }
192 
193 
197 {
198  if (m_animation)
199  m_animation->Pulse();
200 }
201 
202 
207 {
208  // Fade out prev image
209  auto *oldPic = new Animation(m_prev, Animation::Alpha);
210  oldPic->Set(255, 0, m_duration, QEasingCurve::InOutQuad);
211  m_animation->Add(oldPic);
212 
213  // Fade in next image
214  auto *newPic = new Animation(m_next, Animation::Alpha);
215  newPic->Set(0, 255, m_duration, QEasingCurve::InOutQuad);
216  m_animation->Add(newPic);
217 
218  // Hide new image until it's needed
219  m_new->SetAlpha(0);
220 }
221 
222 
227 {
228  // Undo effects of the transition
229  m_old->SetAlpha(255);
230 }
231 
232 
237 {
238  // Reduce hzoom of left image to nothing
239  auto *oldPic = new Animation(m_prev, Animation::HorizontalZoom);
240  oldPic->Set(1.0, 0.0, m_duration / 2, QEasingCurve::InQuart);
241  m_animation->Add(oldPic);
242 
243  // Increase hzoom of right image from nothing to full
244  auto *newPic = new Animation(m_next, Animation::HorizontalZoom);
245  newPic->Set(0.0, 1.0, m_duration / 2, QEasingCurve::OutQuart);
246  m_animation->Add(newPic);
247 
248  // Hide new image until it's needed
250 }
251 
252 
257 {
258  // Undo effects of the transition
259  m_old->SetHorizontalZoom(1.0);
260 }
261 
262 
267 {
268  // Both images are same size
269  int width = m_old->GetArea().width();
270 
271  // Slide off to left
272  auto *oldPic = new Animation(m_prev, Animation::Position);
273  oldPic->Set(QPoint(0, 0), QPoint(-width, 0), m_duration, QEasingCurve::InOutQuart);
274  m_animation->Add(oldPic);
275 
276  // Slide in from right
277  auto *newPic = new Animation(m_next, Animation::Position);
278  newPic->Set(QPoint(width, 0), QPoint(0, 0), m_duration, QEasingCurve::InOutQuart);
279  m_animation->Add(newPic);
280 
281  // Hide new image until it's needed
282  m_new->SetPosition(width, 0);
283 }
284 
285 
290 {
291  // Undo effects of the transition
292  m_old->SetPosition(0,0);
293 }
294 
295 
300 {
301  // Both images are same size
302  int width = m_old->GetArea().width();
303 
304  // Zoom away to left
305  auto *oldZoom = new Animation(m_prev, Animation::Zoom);
306  oldZoom->Set(1.0, 0.0, m_duration, QEasingCurve::OutQuad);
307  m_animation->Add(oldZoom);
308 
309  auto *oldMove = new Animation(m_prev, Animation::Position);
310  oldMove->Set(QPoint(0, 0), QPoint(-width / 2, 0), m_duration,
311  QEasingCurve::InQuad);
312  m_animation->Add(oldMove);
313 
314  // Zoom in from right
315  auto *newZoom = new Animation(m_next, Animation::Zoom);
316  newZoom->Set(0.0, 1.0, m_duration, QEasingCurve::InQuad);
317  m_animation->Add(newZoom);
318 
319  auto *newMove = new Animation(m_next, Animation::Position);
320  newMove->Set(QPoint(width / 2, 0), QPoint(0, 0), m_duration,
321  QEasingCurve::OutQuad);
322  m_animation->Add(newMove);
323 
324  // Hide new image until it's needed
325  m_new->SetZoom(0.0);
326 }
327 
328 
334 {
335  // Undo effects of the transition
336  m_old->SetZoom(1.0);
337  m_old->SetPosition(0, 0);
338 }
339 
340 
345 {
346  // Set up blend
348 
349  // Add simultaneous spin
350  auto *an = new Animation(m_prev, Animation::Angle);
351  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
352  m_animation->Add(an);
353 
354  an = new Animation(m_next, Animation::Angle);
355  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
356  m_animation->Add(an);
357 
358  // Zoom prev away, then back
359  auto *seq = new SequentialAnimation();
360  m_animation->Add(seq);
361 
362  an = new Animation(m_prev, Animation::Zoom);
363  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
364  seq->Add(an);
365 
366  an = new Animation(m_prev, Animation::Zoom);
367  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
368  seq->Add(an);
369 
370  // Zoom next away, then back
371  seq = new SequentialAnimation();
372  m_animation->Add(seq);
373 
374  an = new Animation(m_next, Animation::Zoom);
375  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
376  seq->Add(an);
377 
378  an = new Animation(m_next, Animation::Zoom);
379  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
380  seq->Add(an);
381 }
382 
383 
389 {
390  // Undo blend
392 
393  // Undo effects of the transition
394  m_old->SetZoom(1.0);
395  m_old->SetAngle(0.0);
396 }
397 
398 
406 void TransitionRandom::Start(Slide &from, Slide &to, bool forwards, float speed)
407 {
408  // Select a random peer.
409  int rand = QRandomGenerator::global()->bounded(m_peers.size());
410  m_current = m_peers[rand];
411  // Invoke peer
413  m_current->Start(from, to, forwards, speed);
414 }
415 
416 
421 {
422  // Clean up
423  disconnect(m_current, nullptr, nullptr, nullptr);
424  m_current = nullptr;
425  emit finished();
426 }
427 
428 
Slide
A specialised image for slideshows.
Definition: galleryslide.h:156
Slide::SetZoom
void SetZoom(float zoom)
Sets slide zoom.
Definition: galleryslide.cpp:477
Transition::Finished
virtual void Finished()
Transition has completed.
Definition: gallerytransitions.cpp:107
TransitionTwist::Initialise
void Initialise() override
Set up Twist transition.
Definition: gallerytransitions.cpp:236
GroupTransition::GroupTransition
GroupTransition(GroupAnimation *animation, const QString &name)
Create group transition.
Definition: gallerytransitions.cpp:145
Animation::HorizontalZoom
@ HorizontalZoom
Definition: galleryslide.h:58
TransitionRandom::m_current
Transition * m_current
Selected transition.
Definition: gallerytransitions.h:179
TransitionZoom::Finalise
void Finalise() override
Reset Zoom transition.
Definition: gallerytransitions.cpp:333
kTwistTransition
@ kTwistTransition
Definition: gallerytransitions.h:19
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:388
Transition::Finalise
virtual void Finalise()
Definition: gallerytransitions.h:39
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
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:266
GroupTransition::m_animation
GroupAnimation * m_animation
Definition: gallerytransitions.h:97
Animation::Angle
@ Angle
Definition: galleryslide.h:58
TransitionBlend::Finalise
void Finalise() override
Reset Blend transition.
Definition: gallerytransitions.cpp:226
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:884
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:945
TransitionTwist::Finalise
void Finalise() override
Reset Twist transition.
Definition: gallerytransitions.cpp:256
Transition::Transition
Transition(const QString &name)
Definition: gallerytransitions.cpp:12
TransitionSlide::Finalise
void Finalise() override
Reset Slide transition.
Definition: gallerytransitions.cpp:289
kSpinTransition
@ kSpinTransition
Definition: gallerytransitions.h:22
GroupAnimation
Abstract class for groups of animations.
Definition: galleryslide.h:90
GroupTransition::~GroupTransition
~GroupTransition() override
Destroy group transition.
Definition: gallerytransitions.cpp:156
Transition::Start
virtual void Start(Slide &from, Slide &to, bool forwards, float speed=1.0)
Start base transition.
Definition: gallerytransitions.cpp:81
MythUIType::SetPosition
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:532
TransitionNone::Start
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Switch images.
Definition: gallerytransitions.cpp:129
GroupAnimation::Start
void Start(bool forwards, float speed=1.0) override
Initialise & start base animation.
Definition: galleryslide.h:96
TransitionRandom::Finished
void Finished() override
Invoked when peer transition completes.
Definition: gallerytransitions.cpp:420
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:206
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:406
LOC
#define LOC
Definition: gallerytransitions.cpp:9
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:54
GroupTransition::Pulse
void Pulse() override
Update group transition.
Definition: gallerytransitions.cpp:196
kLastTransSentinel
@ kLastTransSentinel
Definition: gallerytransitions.h:23
TransitionSlide
Slide on from right, then off to left.
Definition: gallerytransitions.h:124
Transition::m_duration
std::chrono::milliseconds m_duration
Seconds transition will last.
Definition: gallerytransitions.h:49
TransitionTwist
Image twists into the next.
Definition: gallerytransitions.h:113
SequentialAnimation
A group of animations to be played sequentially.
Definition: galleryslide.h:110
Animation::Zoom
@ Zoom
Definition: galleryslide.h:58
Animation
A single animation controlling alpha, zoom, rotation and position.
Definition: galleryslide.h:54
GroupAnimation::Pulse
void Pulse() override=0
Animation::Position
@ Position
Definition: galleryslide.h:58
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:344
mythcorecontext.h
GroupAnimation::SetSpeed
void SetSpeed(float speed) override
Definition: galleryslide.h:98
TransitionRegistry::m_immediate
TransitionNone m_immediate
A transition that updates instantly.
Definition: gallerytransitions.h:193
std
Definition: mythchrono.h:23
kZoomTransition
@ kZoomTransition
Definition: gallerytransitions.h:21
GroupAnimation::Add
virtual void Add(AbstractAnimation *child)
Add child animation to group.
Definition: galleryslide.cpp:126
GroupTransition::Start
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Start group transition.
Definition: gallerytransitions.cpp:170
MythUIType::SetVisible
virtual void SetVisible(bool visible)
Definition: mythuitype.cpp:1108
TransitionRegistry::TransitionRegistry
TransitionRegistry(bool includeAnimations)
Constructor.
Definition: gallerytransitions.cpp:52
TransitionRegistry::Select
Transition & Select(int setting)
Returns the requested transition or an alternative if it is not suitable for the painter.
Definition: gallerytransitions.cpp:25
GroupTransition::SetSpeed
void SetSpeed(float speed) override
Change transition speed.
Definition: gallerytransitions.cpp:187
kNoTransition
@ kNoTransition
Definition: gallerytransitions.h:16
MythUIType::SetHorizontalZoom
void SetHorizontalZoom(float zoom)
Definition: mythuitype.cpp:970
GroupAnimation::Clear
void Clear() override
Delete all child animations.
Definition: galleryslide.cpp:137
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:982
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:299
Animation::Alpha
@ Alpha
Definition: galleryslide.h:58
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