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
151  connect(m_animation, SIGNAL(finished()), this, SLOT(Finished()));
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 
201 void GroupTransition::Pulse(int interval)
202 {
203  if (m_animation)
204  m_animation->Pulse(interval);
205 }
206 
207 
212 {
213  // Fade out prev image
214  auto *oldPic = new Animation(m_prev, Animation::Alpha);
215  oldPic->Set(255, 0, m_duration, QEasingCurve::InOutQuad);
216  m_animation->Add(oldPic);
217 
218  // Fade in next image
219  auto *newPic = new Animation(m_next, Animation::Alpha);
220  newPic->Set(0, 255, m_duration, QEasingCurve::InOutQuad);
221  m_animation->Add(newPic);
222 
223  // Hide new image until it's needed
224  m_new->SetAlpha(0);
225 }
226 
227 
232 {
233  // Undo effects of the transition
234  m_old->SetAlpha(255);
235 }
236 
237 
242 {
243  // Reduce hzoom of left image to nothing
244  auto *oldPic = new Animation(m_prev, Animation::HorizontalZoom);
245  oldPic->Set(1.0, 0.0, m_duration / 2, QEasingCurve::InQuart);
246  m_animation->Add(oldPic);
247 
248  // Increase hzoom of right image from nothing to full
249  auto *newPic = new Animation(m_next, Animation::HorizontalZoom);
250  newPic->Set(0.0, 1.0, m_duration / 2, QEasingCurve::OutQuart);
251  m_animation->Add(newPic);
252 
253  // Hide new image until it's needed
255 }
256 
257 
262 {
263  // Undo effects of the transition
264  m_old->SetHorizontalZoom(1.0);
265 }
266 
267 
272 {
273  // Both images are same size
274  int width = m_old->GetArea().width();
275 
276  // Slide off to left
277  auto *oldPic = new Animation(m_prev, Animation::Position);
278  oldPic->Set(QPoint(0, 0), QPoint(-width, 0), m_duration, QEasingCurve::InOutQuart);
279  m_animation->Add(oldPic);
280 
281  // Slide in from right
282  auto *newPic = new Animation(m_next, Animation::Position);
283  newPic->Set(QPoint(width, 0), QPoint(0, 0), m_duration, QEasingCurve::InOutQuart);
284  m_animation->Add(newPic);
285 
286  // Hide new image until it's needed
287  m_new->SetPosition(width, 0);
288 }
289 
290 
295 {
296  // Undo effects of the transition
297  m_old->SetPosition(0,0);
298 }
299 
300 
305 {
306  // Both images are same size
307  int width = m_old->GetArea().width();
308 
309  // Zoom away to left
310  auto *oldZoom = new Animation(m_prev, Animation::Zoom);
311  oldZoom->Set(1.0, 0.0, m_duration, QEasingCurve::OutQuad);
312  m_animation->Add(oldZoom);
313 
314  auto *oldMove = new Animation(m_prev, Animation::Position);
315  oldMove->Set(QPoint(0, 0), QPoint(-width / 2, 0), m_duration,
316  QEasingCurve::InQuad);
317  m_animation->Add(oldMove);
318 
319  // Zoom in from right
320  auto *newZoom = new Animation(m_next, Animation::Zoom);
321  newZoom->Set(0.0, 1.0, m_duration, QEasingCurve::InQuad);
322  m_animation->Add(newZoom);
323 
324  auto *newMove = new Animation(m_next, Animation::Position);
325  newMove->Set(QPoint(width / 2, 0), QPoint(0, 0), m_duration,
326  QEasingCurve::OutQuad);
327  m_animation->Add(newMove);
328 
329  // Hide new image until it's needed
330  m_new->SetZoom(0.0);
331 }
332 
333 
339 {
340  // Undo effects of the transition
341  m_old->SetZoom(1.0);
342  m_old->SetPosition(0, 0);
343 }
344 
345 
350 {
351  // Set up blend
353 
354  // Add simultaneous spin
355  auto *an = new Animation(m_prev, Animation::Angle);
356  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
357  m_animation->Add(an);
358 
359  an = new Animation(m_next, Animation::Angle);
360  an->Set(0.0, 360.1, m_duration, QEasingCurve::InOutQuad);
361  m_animation->Add(an);
362 
363  // Zoom prev away, then back
364  auto *seq = new SequentialAnimation();
365  m_animation->Add(seq);
366 
367  an = new Animation(m_prev, Animation::Zoom);
368  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
369  seq->Add(an);
370 
371  an = new Animation(m_prev, Animation::Zoom);
372  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
373  seq->Add(an);
374 
375  // Zoom next away, then back
376  seq = new SequentialAnimation();
377  m_animation->Add(seq);
378 
379  an = new Animation(m_next, Animation::Zoom);
380  an->Set(1.0, 0.5, m_duration / 2, QEasingCurve::InOutQuad);
381  seq->Add(an);
382 
383  an = new Animation(m_next, Animation::Zoom);
384  an->Set(0.5, 1.0, m_duration / 2, QEasingCurve::InOutQuad);
385  seq->Add(an);
386 }
387 
388 
394 {
395  // Undo blend
397 
398  // Undo effects of the transition
399  m_old->SetZoom(1.0);
400  m_old->SetAngle(0.0);
401 }
402 
403 
411 void TransitionRandom::Start(Slide &from, Slide &to, bool forwards, float speed)
412 {
413  // Select a random peer.
414 #if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
415  int rand = QRandomGenerator::global()->bounded(m_peers.size());
416 #else
417  int rand = qrand() % m_peers.size();
418 #endif
419  m_current = m_peers[rand];
420  // Invoke peer
421  connect(m_current, SIGNAL(finished()), this, SLOT(Finished()));
422  m_current->Start(from, to, forwards, speed);
423 }
424 
425 
430 {
431  // Clean up
432  disconnect(m_current, nullptr, nullptr, nullptr);
433  m_current = nullptr;
434  emit finished();
435 }
436 
437 
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:109
TransitionTwist::Initialise
void Initialise() override
Set up Twist transition.
Definition: gallerytransitions.cpp:241
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:338
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:393
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:271
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:231
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:864
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:925
TransitionTwist::Finalise
void Finalise() override
Reset Twist transition.
Definition: gallerytransitions.cpp:261
Transition::Transition
Transition(const QString &name)
Definition: gallerytransitions.cpp:14
TransitionSlide::Finalise
void Finalise() override
Reset Slide transition.
Definition: gallerytransitions.cpp:294
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: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:517
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:96
TransitionRandom::Finished
void Finished() override
Invoked when peer transition completes.
Definition: gallerytransitions.cpp:429
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:211
Transition::m_duration
int m_duration
Seconds transition will last.
Definition: gallerytransitions.h:49
TransitionSpin
Images blend whilst rotating.
Definition: gallerytransitions.h:146
GroupAnimation::Pulse
void Pulse(int interval) override=0
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:411
LOC
#define LOC
Definition: gallerytransitions.cpp:11
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
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:110
GroupTransition::Pulse
void Pulse(int interval) override
Update group transition.
Definition: gallerytransitions.cpp:201
Animation::Zoom
@ Zoom
Definition: galleryslide.h:59
Animation
A single animation controlling alpha, zoom, rotation and position.
Definition: galleryslide.h:55
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:349
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
kZoomTransition
@ kZoomTransition
Definition: gallerytransitions.h:21
GroupAnimation::Add
virtual void Add(AbstractAnimation *child)
Add child animation to group.
Definition: galleryslide.cpp:121
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:1080
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:950
GroupAnimation::Clear
void Clear() override
Delete all child animations.
Definition: galleryslide.cpp:132
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:962
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:304
Animation::Alpha
@ Alpha
Definition: galleryslide.h:59
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