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 
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 
148  : Transition(std::move(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  Animation *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  Animation *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
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
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
278  oldPic->Set(QPoint(0, 0), QPoint(-width, 0), m_duration, QEasingCurve::InOutQuart);
279  m_animation->Add(oldPic);
280 
281  // Slide in from right
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  Animation *oldZoom = new Animation(m_prev, Animation::Zoom);
311  oldZoom->Set(1.0, 0.0, m_duration, QEasingCurve::OutQuad);
312  m_animation->Add(oldZoom);
313 
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  Animation *newZoom = new Animation(m_next, Animation::Zoom);
321  newZoom->Set(0.0, 1.0, m_duration, QEasingCurve::InQuad);
322  m_animation->Add(newZoom);
323 
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
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
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 
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Starts a random transition from a set of its peer transitions,.
Images blend whilst rotating.
Switches images instantly with no effects.
Transition(const QString &name)
void Initialise() override
Set up Blend transition.
#define LOC
void Initialise() override
Set up Slide transition.
Slide * m_next
The image occurring later in the slideshow sequence.
Invokes random transitions.
void Initialise() override
Set up Zoom transition.
Transition & Select(int setting)
Returns the requested transition or an alternative if it is not suitable for the painter.
virtual void Add(AbstractAnimation *child)
Add child animation to group.
void SetAlpha(int newalpha)
Definition: mythuitype.cpp:924
GroupAnimation * m_animation
void finished()
void Start(bool forwards, float speed=1.0) override=0
Initialise & start base animation.
void SetHorizontalZoom(float zoom)
Definition: mythuitype.cpp:949
Slide * m_prev
The image occurring earlier in the slideshow sequence.
int m_duration
Seconds transition will last.
A group of animations to be played sequentially.
Definition: galleryslide.h:109
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void Pulse(int interval) override=0
TransitionMap m_map
All possible transitions.
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:519
virtual void Initialise()
TransitionNone m_immediate
A transition that updates instantly.
void SetZoom(float)
Sets slide zoom.
GroupTransition(GroupAnimation *animation, QString name)
Create group transition.
virtual void SetVisible(bool visible)
Slide * m_old
The image currently displayed, which will be replaced (whatever the transition direction)
Slide on from right, then off to left.
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:863
void Initialise() override
Set up Spin transition.
Provides transitions for slideshows.
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Switch images.
Image twists into the next.
const char * name
Definition: ParseText.cpp:328
void Set(const QVariant &from, const QVariant &to, int duration=500, const QEasingCurve &curve=QEasingCurve::InOutCubic, UIEffects::Centre=UIEffects::Middle)
Initialises an animation.
virtual ~GroupTransition()
Destroy group transition.
void Finalise() override
Reset Slide transition.
void SetSpeed(float speed) override
Change transition speed.
virtual void Finished()
Transition has completed.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
Slide * m_new
The new image that will replace the current one (whatever the transition direction)
A specialised image for slideshows.
Definition: galleryslide.h:155
Transition * m_current
Selected transition.
Base class of an animated transition that can be accelerated & reversed.
void Initialise() override
Set up Twist transition.
QList< Transition * > m_peers
Set of distinct transitions.
virtual void Finalise()
virtual void Start(Slide &from, Slide &to, bool forwards, float speed=1.0)
Start base transition.
A single animation controlling alpha, zoom, rotation and position.
Definition: galleryslide.h:55
Image blends into the next by fading.
Zooms from right, then away to left.
void Finalise() override
Reset Blend transition.
TransitionRegistry(bool includeAnimations)
Constructor.
void Finalise() override
Reset Twist transition.
void Pulse(int interval) override
Update group transition.
void Finalise() override
Reset Zoom transition.
void Finalise() override
Reset Spin transition.
void SetAngle(float angle)
Definition: mythuitype.cpp:961
void Clear() override
Delete all child animations.
Abstract class for groups of animations.
Definition: galleryslide.h:91
void SetSpeed(float speed) override=0
void Start(Slide &from, Slide &to, bool forwards, float speed=1.0) override
Start group transition.
void Finished() override
Invoked when peer transition completes.