LCOV - code coverage report
Current view: top level - libreoffice/svx/source/sdr/animation - scheduler.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 22 119 18.5 %
Date: 2012-12-27 Functions: 8 25 32.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      20             : #include <svx/sdr/animation/scheduler.hxx>
      21             : 
      22             : #include <vector>
      23             : 
      24             : //////////////////////////////////////////////////////////////////////////////
      25             : // event class
      26             : 
      27             : namespace sdr
      28             : {
      29             :     namespace animation
      30             :     {
      31          12 :         Event::Event(sal_uInt32 nTime)
      32             :         :   mnTime(nTime),
      33          12 :             mpNext(0L)
      34             :         {
      35          12 :         }
      36             : 
      37           6 :         Event::~Event()
      38             :         {
      39           6 :         }
      40             : 
      41           0 :         Event* Event::GetNext() const
      42             :         {
      43           0 :             return mpNext;
      44             :         }
      45             : 
      46           0 :         void Event::SetNext(Event* pNew)
      47             :         {
      48           0 :             if(pNew != mpNext)
      49             :             {
      50           0 :                 mpNext = pNew;
      51             :             }
      52           0 :         }
      53             : 
      54           0 :         sal_uInt32 Event::GetTime() const
      55             :         {
      56           0 :             return mnTime;
      57             :         }
      58             : 
      59           0 :         void Event::SetTime(sal_uInt32 nNew)
      60             :         {
      61           0 :             if(mnTime != nNew)
      62             :             {
      63           0 :                 mnTime = nNew;
      64             :             }
      65           0 :         }
      66             :     } // end of namespace animation
      67             : } // end of namespace sdr
      68             : 
      69             : //////////////////////////////////////////////////////////////////////////////
      70             : // eventlist class
      71             : 
      72             : namespace sdr
      73             : {
      74             :     namespace animation
      75             :     {
      76         280 :         EventList::EventList()
      77         280 :         :   mpHead(0L)
      78             :         {
      79         280 :         }
      80             : 
      81          30 :         EventList::~EventList()
      82             :         {
      83          30 :             Clear();
      84          30 :         }
      85             : 
      86           0 :         void EventList::Insert(Event* pNew)
      87             :         {
      88           0 :             if(pNew)
      89             :             {
      90           0 :                 Event* pCurrent = mpHead;
      91           0 :                 Event* pPrev = 0L;
      92             : 
      93           0 :                 while(pCurrent && pCurrent->GetTime() < pNew->GetTime())
      94             :                 {
      95           0 :                     pPrev = pCurrent;
      96           0 :                     pCurrent = pCurrent->GetNext();
      97             :                 }
      98             : 
      99           0 :                 if(pPrev)
     100             :                 {
     101           0 :                     pNew->SetNext(pPrev->GetNext());
     102           0 :                     pPrev->SetNext(pNew);
     103             :                 }
     104             :                 else
     105             :                 {
     106           0 :                     pNew->SetNext(mpHead);
     107           0 :                     mpHead = pNew;
     108             :                 }
     109             :             }
     110           0 :         }
     111             : 
     112           0 :         void EventList::Remove(Event* pOld)
     113             :         {
     114           0 :             if(pOld && mpHead)
     115             :             {
     116           0 :                 Event* pCurrent = mpHead;
     117           0 :                 Event* pPrev = 0L;
     118             : 
     119           0 :                 while(pCurrent && pCurrent != pOld)
     120             :                 {
     121           0 :                     pPrev = pCurrent;
     122           0 :                     pCurrent = pCurrent->GetNext();
     123             :                 }
     124             : 
     125           0 :                 if(pPrev)
     126             :                 {
     127           0 :                     pPrev->SetNext(pOld->GetNext());
     128             :                 }
     129             :                 else
     130             :                 {
     131           0 :                     mpHead = pOld->GetNext();
     132             :                 }
     133             : 
     134           0 :                 pOld->SetNext(0L);
     135             :             }
     136           0 :         }
     137             : 
     138          30 :         void EventList::Clear()
     139             :         {
     140          60 :             while(mpHead)
     141             :             {
     142           0 :                 Event* pNext = mpHead->GetNext();
     143           0 :                 mpHead->SetNext(0L);
     144           0 :                 mpHead = pNext;
     145             :             }
     146          30 :         }
     147             : 
     148           0 :         Event* EventList::GetFirst()
     149             :         {
     150           0 :             return mpHead;
     151             :         }
     152             :     } // end of namespace animation
     153             : } // end of namespace sdr
     154             : 
     155             : //////////////////////////////////////////////////////////////////////////////
     156             : // scheduler class
     157             : 
     158             : namespace sdr
     159             : {
     160             :     namespace animation
     161             :     {
     162         280 :         Scheduler::Scheduler()
     163             :         :   mnTime(0L),
     164             :             mnDeltaTime(0L),
     165         280 :             mbIsPaused(false)
     166             :         {
     167         280 :         }
     168             : 
     169          60 :         Scheduler::~Scheduler()
     170             :         {
     171          30 :             Stop();
     172          30 :         }
     173             : 
     174           0 :         void Scheduler::Timeout()
     175             :         {
     176             :             // stop timer and add time
     177           0 :             Stop();
     178           0 :             mnTime += mnDeltaTime;
     179             : 
     180             :             // execute events
     181           0 :             triggerEvents();
     182             : 
     183             :             // re-start or stop timer according to event list
     184           0 :             checkTimeout();
     185           0 :         }
     186             : 
     187           0 :         void Scheduler::triggerEvents()
     188             :         {
     189           0 :             Event* pNextEvent = maList.GetFirst();
     190             : 
     191           0 :             if(pNextEvent)
     192             :             {
     193             :                 // copy events which need to be executed to a vector. Remove them from
     194             :                 // the scheduler
     195           0 :                 ::std::vector< Event* > EventPointerVector;
     196             : 
     197           0 :                 while(pNextEvent && pNextEvent->GetTime() <= mnTime)
     198             :                 {
     199           0 :                     maList.Remove(pNextEvent);
     200           0 :                     EventPointerVector.push_back(pNextEvent);
     201           0 :                     pNextEvent = maList.GetFirst();
     202             :                 }
     203             : 
     204             :                 // execute events from the vector
     205           0 :                 for(::std::vector< Event* >::iterator aCandidate = EventPointerVector.begin();
     206           0 :                     aCandidate != EventPointerVector.end(); ++aCandidate)
     207             :                 {
     208             :                     // trigger event. This may re-insert the event to the scheduler again
     209           0 :                     (*aCandidate)->Trigger(mnTime);
     210           0 :                 }
     211             :             }
     212           0 :         }
     213             : 
     214           0 :         void Scheduler::checkTimeout()
     215             :         {
     216             :             // re-start or stop timer according to event list
     217           0 :             if(!IsPaused() && maList.GetFirst())
     218             :             {
     219           0 :                 mnDeltaTime = maList.GetFirst()->GetTime() - mnTime;
     220             : 
     221           0 :                 if(0L != mnDeltaTime)
     222             :                 {
     223           0 :                     SetTimeout(mnDeltaTime);
     224           0 :                     Start();
     225             :                 }
     226             :             }
     227             :             else
     228             :             {
     229           0 :                 Stop();
     230             :             }
     231           0 :         }
     232             : 
     233         816 :         sal_uInt32 Scheduler::GetTime()
     234             :         {
     235         816 :             return mnTime;
     236             :         }
     237             : 
     238             :         // #i38135#
     239           0 :         void Scheduler::SetTime(sal_uInt32 nTime)
     240             :         {
     241             :             // reset time
     242           0 :             Stop();
     243           0 :             mnTime = nTime;
     244             : 
     245             :             // get event pointer
     246           0 :             Event* pEvent = maList.GetFirst();
     247             : 
     248           0 :             if(pEvent)
     249             :             {
     250             :                 // retet event time points
     251           0 :                 while(pEvent)
     252             :                 {
     253           0 :                     pEvent->SetTime(nTime);
     254           0 :                     pEvent = pEvent->GetNext();
     255             :                 }
     256             : 
     257           0 :                 if(!IsPaused())
     258             :                 {
     259             :                     // without delta time, init events by triggering them. This will invalidate
     260             :                     // painted objects and add them to the scheduler again
     261           0 :                     mnDeltaTime = 0L;
     262           0 :                     triggerEvents();
     263           0 :                     checkTimeout();
     264             :                 }
     265             :             }
     266           0 :         }
     267             : 
     268           0 :         void Scheduler::InsertEvent(Event* pNew)
     269             :         {
     270           0 :             if(pNew)
     271             :             {
     272           0 :                 maList.Insert(pNew);
     273           0 :                 checkTimeout();
     274             :             }
     275           0 :         }
     276             : 
     277           0 :         void Scheduler::RemoveEvent(Event* pOld)
     278             :         {
     279           0 :             if(pOld && maList.GetFirst())
     280             :             {
     281           0 :                 maList.Remove(pOld);
     282           0 :                 checkTimeout();
     283             :             }
     284           0 :         }
     285             : 
     286           0 :         void Scheduler::SetPaused(bool bNew)
     287             :         {
     288           0 :             if(bNew != mbIsPaused)
     289             :             {
     290           0 :                 mbIsPaused = bNew;
     291           0 :                 checkTimeout();
     292             :             }
     293           0 :         }
     294             :     } // end of namespace animation
     295             : } // end of namespace sdr
     296             : 
     297             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10