LibreOffice Module svx (master)  1
scheduler.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
21 
22 #include <algorithm>
23 #include <vector>
24 
25 
26 // event class
27 
28 namespace sdr
29 {
30  namespace animation
31  {
32  Event::Event() : mnTime(0)
33  {
34  }
35 
37  {
38  }
39 
40 
41  void Event::SetTime(sal_uInt32 nNew)
42  {
43  if(mnTime != nNew)
44  {
45  mnTime = nNew;
46  }
47  }
48 
50  : mnTime(0),
51  mnDeltaTime(0),
52  mbIsPaused(false)
53  {
54  }
55 
57  {
58  Stop();
59  }
60 
62  {
63  // stop timer and add time
64  Stop();
66 
67  // execute events
68  triggerEvents();
69 
70  // re-start or stop timer according to event list
71  checkTimeout();
72  }
73 
75  {
76  if (mvEvents.empty())
77  return;
78 
79  // copy events which need to be executed to a vector. Remove them from
80  // the scheduler
81  ::std::vector< Event* > aToBeExecutedList;
82 
83  while(!mvEvents.empty() && mvEvents.front()->GetTime() <= mnTime)
84  {
85  Event* pNextEvent = mvEvents.front();
86  mvEvents.erase(mvEvents.begin());
87  aToBeExecutedList.push_back(pNextEvent);
88  }
89 
90  // execute events from the vector
91  for(auto& rpCandidate : aToBeExecutedList)
92  {
93  // trigger event. This may re-insert the event to the scheduler again
94  rpCandidate->Trigger(mnTime);
95  }
96  }
97 
99  {
100  // re-start or stop timer according to event list
101  if(!IsPaused() && !mvEvents.empty())
102  {
103  mnDeltaTime = mvEvents.front()->GetTime() - mnTime;
104 
105  if(0L != mnDeltaTime)
106  {
108  Start();
109  }
110  }
111  else
112  {
113  Stop();
114  }
115  }
116 
117 
118  // #i38135#
119  void Scheduler::SetTime(sal_uInt32 nTime)
120  {
121  // reset time
122  Stop();
123  mnTime = nTime;
124 
125  if (mvEvents.empty())
126  return;
127 
128  // reset event time points
129  for (auto & rEvent : mvEvents)
130  {
131  rEvent->SetTime(nTime);
132  }
133 
134  if(!IsPaused())
135  {
136  // without delta time, init events by triggering them. This will invalidate
137  // painted objects and add them to the scheduler again
138  mnDeltaTime = 0;
139  triggerEvents();
140  checkTimeout();
141  }
142  }
143 
145  {
146  // insert maintaining time ordering
147  auto it = std::find_if(mvEvents.begin(), mvEvents.end(),
148  [&rNew](const Event* pEvent) { return rNew.GetTime() < pEvent->GetTime(); });
149  mvEvents.insert(it, &rNew);
150  checkTimeout();
151  }
152 
154  {
155  if(!mvEvents.empty())
156  {
157  mvEvents.erase(std::remove(mvEvents.begin(), mvEvents.end(), pOld), mvEvents.end());
158  checkTimeout();
159  }
160  }
161 
162  void Scheduler::SetPaused(bool bNew)
163  {
164  if(bNew != mbIsPaused)
165  {
166  mbIsPaused = bNew;
167  checkTimeout();
168  }
169  }
170  } // end of namespace animation
171 } // end of namespace sdr
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE void RemoveEvent(Event *pOld)
Definition: scheduler.cxx:153
virtual ~Scheduler() override
Definition: scheduler.cxx:56
std::vector< Event * > mvEvents
Definition: scheduler.hxx:61
SAL_DLLPRIVATE void checkTimeout()
Definition: scheduler.cxx:98
void InsertEvent(Event &rNew)
Definition: scheduler.cxx:144
SAL_DLLPRIVATE Event()
Definition: scheduler.cxx:32
virtual void Invoke() override
Definition: scheduler.cxx:61
SAL_DLLPRIVATE void SetPaused(bool bNew)
Definition: scheduler.cxx:162
SAL_DLLPRIVATE Scheduler()
Definition: scheduler.cxx:49
void SetTime(sal_uInt32 nNew)
Definition: scheduler.cxx:41
virtual void Start() override
void SetTimeout(sal_uInt64 nTimeoutMs)
void Stop()
SAL_DLLPRIVATE sal_uInt32 GetTime() const
Definition: scheduler.hxx:45
SAL_DLLPRIVATE void triggerEvents()
Definition: scheduler.cxx:74
SAL_DLLPRIVATE bool IsPaused() const
Definition: scheduler.hxx:92
SAL_DLLPRIVATE void SetTime(sal_uInt32 nTime)
Definition: scheduler.cxx:119