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::animation
29 {
30  Event::Event() : mnTime(0)
31  {
32  }
33 
35  {
36  }
37 
38 
39  void Event::SetTime(sal_uInt32 nNew)
40  {
41  if(mnTime != nNew)
42  {
43  mnTime = nNew;
44  }
45  }
46 
48  : mnTime(0),
49  mnDeltaTime(0),
50  mbIsPaused(false)
51  {
52  }
53 
55  {
56  Stop();
57  }
58 
60  {
61  // stop timer and add time
62  Stop();
64 
65  // execute events
66  triggerEvents();
67 
68  // re-start or stop timer according to event list
69  checkTimeout();
70  }
71 
73  {
74  if (mvEvents.empty())
75  return;
76 
77  // copy events which need to be executed to a vector. Remove them from
78  // the scheduler
79  ::std::vector< Event* > aToBeExecutedList;
80 
81  while(!mvEvents.empty() && mvEvents.front()->GetTime() <= mnTime)
82  {
83  Event* pNextEvent = mvEvents.front();
84  mvEvents.erase(mvEvents.begin());
85  aToBeExecutedList.push_back(pNextEvent);
86  }
87 
88  // execute events from the vector
89  for(auto& rpCandidate : aToBeExecutedList)
90  {
91  // trigger event. This may re-insert the event to the scheduler again
92  rpCandidate->Trigger(mnTime);
93  }
94  }
95 
97  {
98  // re-start or stop timer according to event list
99  if(!IsPaused() && !mvEvents.empty())
100  {
101  mnDeltaTime = mvEvents.front()->GetTime() - mnTime;
102 
103  if(0 != mnDeltaTime)
104  {
106  Start();
107  }
108  }
109  else
110  {
111  Stop();
112  }
113  }
114 
115 
116  // #i38135#
117  void Scheduler::SetTime(sal_uInt32 nTime)
118  {
119  // reset time
120  Stop();
121  mnTime = nTime;
122 
123  if (mvEvents.empty())
124  return;
125 
126  // reset event time points
127  for (auto & rEvent : mvEvents)
128  {
129  rEvent->SetTime(nTime);
130  }
131 
132  if(!IsPaused())
133  {
134  // without delta time, init events by triggering them. This will invalidate
135  // painted objects and add them to the scheduler again
136  mnDeltaTime = 0;
137  triggerEvents();
138  checkTimeout();
139  }
140  }
141 
143  {
144  // insert maintaining time ordering
145  auto it = std::find_if(mvEvents.begin(), mvEvents.end(),
146  [&rNew](const Event* pEvent) { return rNew.GetTime() < pEvent->GetTime(); });
147  mvEvents.insert(it, &rNew);
148  checkTimeout();
149  }
150 
152  {
153  if(!mvEvents.empty())
154  {
155  mvEvents.erase(std::remove(mvEvents.begin(), mvEvents.end(), pOld), mvEvents.end());
156  checkTimeout();
157  }
158  }
159 
160  void Scheduler::SetPaused(bool bNew)
161  {
162  if(bNew != mbIsPaused)
163  {
164  mbIsPaused = bNew;
165  checkTimeout();
166  }
167  }
168 
169 } // end of namespace
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE void RemoveEvent(Event *pOld)
Definition: scheduler.cxx:151
virtual ~Scheduler() override
Definition: scheduler.cxx:54
std::vector< Event * > mvEvents
Definition: scheduler.hxx:61
SAL_DLLPRIVATE void checkTimeout()
Definition: scheduler.cxx:96
void InsertEvent(Event &rNew)
Definition: scheduler.cxx:142
SAL_DLLPRIVATE Event()
Definition: scheduler.cxx:30
virtual void Invoke() override
Definition: scheduler.cxx:59
SAL_DLLPRIVATE void SetPaused(bool bNew)
Definition: scheduler.cxx:160
SAL_DLLPRIVATE Scheduler()
Definition: scheduler.cxx:47
void SetTime(sal_uInt32 nNew)
Definition: scheduler.cxx:39
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:72
SAL_DLLPRIVATE bool IsPaused() const
Definition: scheduler.hxx:92
SAL_DLLPRIVATE void SetTime(sal_uInt32 nTime)
Definition: scheduler.cxx:117