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