LCOV - code coverage report
Current view: top level - sc/source/ui/docshell - autostyl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 83 1.2 %
Date: 2015-06-13 12:38:46 Functions: 2 25 8.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 <time.h>
      21             : 
      22             : #include "attrib.hxx"
      23             : #include "autostyl.hxx"
      24             : #include "docsh.hxx"
      25             : #include "sc.hrc"
      26             : 
      27           0 : struct ScAutoStyleInitData
      28             : {
      29             :     ScRange     aRange;
      30             :     OUString    aStyle1;
      31             :     sal_uLong   nTimeout;
      32             :     OUString    aStyle2;
      33             : 
      34           0 :     ScAutoStyleInitData( const ScRange& rR, const OUString& rSt1, sal_uLong nT, const OUString& rSt2 ) :
      35           0 :         aRange(rR), aStyle1(rSt1), nTimeout(nT), aStyle2(rSt2) {}
      36             : };
      37             : 
      38           0 : struct ScAutoStyleData
      39             : {
      40             :     sal_uLong nTimeout;
      41             :     ScRange   aRange;
      42             :     OUString  aStyle;
      43             : 
      44           0 :     ScAutoStyleData( sal_uLong nT, const ScRange& rR, const OUString& rT ) :
      45           0 :         nTimeout(nT), aRange(rR), aStyle(rT) {}
      46             : };
      47             : 
      48           0 : inline sal_uLong TimeNow()          // Sekunden
      49             : {
      50           0 :     return (sal_uLong) time(0);
      51             : }
      52             : 
      53             : namespace {
      54             : 
      55           0 : class FindByRange : public ::std::unary_function<ScAutoStyleData, bool>
      56             : {
      57             :     ScRange maRange;
      58             : public:
      59           0 :     FindByRange(const ScRange& r) : maRange(r) {}
      60           0 :     bool operator() (const ScAutoStyleData& rData) const { return rData.aRange == maRange; }
      61             : };
      62             : 
      63             : class FindByTimeout : public ::std::unary_function<ScAutoStyleData, bool>
      64             : {
      65             :     sal_uLong mnTimeout;
      66             : public:
      67           0 :     FindByTimeout(sal_uLong n) : mnTimeout(n) {}
      68           0 :     bool operator() (const ScAutoStyleData& rData) const { return rData.nTimeout >= mnTimeout; }
      69             : };
      70             : 
      71             : struct FindNonZeroTimeout : public ::std::unary_function<ScAutoStyleData, bool>
      72             : {
      73           0 :     bool operator() (const ScAutoStyleData& rData) const
      74             :     {
      75           0 :         return rData.nTimeout != 0;
      76             :     }
      77             : };
      78             : 
      79             : }
      80             : 
      81           0 : ScAutoStyleList::ScAutoStyleList(ScDocShell* pShell)
      82             :     : pDocSh(pShell)
      83           0 :     , nTimerStart(0)
      84             : {
      85           0 :     aTimer.SetTimeoutHdl( LINK( this, ScAutoStyleList, TimerHdl ) );
      86           0 :     aInitIdle.SetIdleHdl( LINK( this, ScAutoStyleList, InitHdl ) );
      87           0 :     aInitIdle.SetPriority( SchedulerPriority::HIGHEST );
      88           0 : }
      89             : 
      90           0 : ScAutoStyleList::~ScAutoStyleList()
      91             : {
      92           0 : }
      93             : 
      94             : //  initial short delay (asynchronous call)
      95             : 
      96           0 : void ScAutoStyleList::AddInitial( const ScRange& rRange, const OUString& rStyle1,
      97             :                                     sal_uLong nTimeout, const OUString& rStyle2 )
      98             : {
      99           0 :     aInitials.push_back(new ScAutoStyleInitData( rRange, rStyle1, nTimeout, rStyle2 ));
     100           0 :     aInitIdle.Start();
     101           0 : }
     102             : 
     103           0 : IMPL_LINK_NOARG_TYPED(ScAutoStyleList, InitHdl, Idle *, void)
     104             : {
     105           0 :     boost::ptr_vector<ScAutoStyleInitData>::iterator iter;
     106           0 :     for (iter = aInitials.begin(); iter != aInitials.end(); ++iter)
     107             :     {
     108             :         //  apply first style immediately
     109           0 :         pDocSh->DoAutoStyle(iter->aRange,iter->aStyle1);
     110             : 
     111             :         //  add second style to list
     112           0 :         if (iter->nTimeout)
     113           0 :             AddEntry(iter->nTimeout,iter->aRange,iter->aStyle2 );
     114             :     }
     115             : 
     116           0 :     aInitials.clear();
     117           0 : }
     118             : 
     119           0 : void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
     120             : {
     121           0 :     aTimer.Stop();
     122           0 :     sal_uLong nNow = TimeNow();
     123             : 
     124             :     // Remove the first item with the same range.
     125             :     ::boost::ptr_vector<ScAutoStyleData>::iterator itr =
     126           0 :         ::std::find_if(aEntries.begin(), aEntries.end(), FindByRange(rRange));
     127             : 
     128           0 :     if (itr != aEntries.end())
     129           0 :         aEntries.erase(itr);
     130             : 
     131             :     //  Timeouts von allen Eintraegen anpassen
     132             : 
     133           0 :     if (!aEntries.empty() && nNow != nTimerStart)
     134             :     {
     135             :         OSL_ENSURE(nNow>nTimerStart, "Zeit laeuft rueckwaerts?");
     136           0 :         AdjustEntries((nNow-nTimerStart)*1000);
     137             :     }
     138             : 
     139             :     //  Einfuege-Position suchen
     140             :     boost::ptr_vector<ScAutoStyleData>::iterator iter =
     141           0 :         ::std::find_if(aEntries.begin(), aEntries.end(), FindByTimeout(nTimeout));
     142             : 
     143           0 :     aEntries.insert(iter,new ScAutoStyleData(nTimeout,rRange,rStyle));
     144             : 
     145             :     //  abgelaufene ausfuehren, Timer neu starten
     146             : 
     147           0 :     ExecuteEntries();
     148           0 :     StartTimer(nNow);
     149           0 : }
     150             : 
     151           0 : void ScAutoStyleList::AdjustEntries( sal_uLong nDiff )  // Millisekunden
     152             : {
     153           0 :     boost::ptr_vector<ScAutoStyleData>::iterator iter;
     154           0 :     for (iter = aEntries.begin(); iter != aEntries.end(); ++iter)
     155             :     {
     156           0 :         if (iter->nTimeout <= nDiff)
     157           0 :             iter->nTimeout = 0;                 // abgelaufen
     158             :         else
     159           0 :             iter->nTimeout -= nDiff;                // weiterzaehlen
     160             :     }
     161           0 : }
     162             : 
     163           0 : void ScAutoStyleList::ExecuteEntries()
     164             : {
     165             :     // Execute and remove all items with timeout == 0 from the begin position
     166             :     // until the first item with non-zero timeout value.
     167           0 :     ::boost::ptr_vector<ScAutoStyleData>::iterator itr = aEntries.begin(), itrEnd = aEntries.end();
     168           0 :     for (; itr != itrEnd; ++itr)
     169             :     {
     170           0 :         if (itr->nTimeout)
     171           0 :             break;
     172             : 
     173           0 :         pDocSh->DoAutoStyle(itr->aRange, itr->aStyle);
     174             :     }
     175             :     // At this point itr should be on the first item with non-zero timeout, or
     176             :     // the end position in case all items have timeout == 0.
     177           0 :     aEntries.erase(aEntries.begin(), itr);
     178           0 : }
     179             : 
     180           0 : void ScAutoStyleList::ExecuteAllNow()
     181             : {
     182           0 :     aTimer.Stop();
     183             : 
     184           0 :     boost::ptr_vector<ScAutoStyleData>::iterator iter;
     185           0 :     for (iter = aEntries.begin(); iter != aEntries.end(); ++iter)
     186           0 :         pDocSh->DoAutoStyle(iter->aRange,iter->aStyle);
     187             : 
     188           0 :     aEntries.clear();
     189           0 : }
     190             : 
     191           0 : void ScAutoStyleList::StartTimer( sal_uLong nNow )      // Sekunden
     192             : {
     193             :     // ersten Eintrag mit Timeout != 0 suchen
     194             :     boost::ptr_vector<ScAutoStyleData>::iterator iter =
     195           0 :         ::std::find_if(aEntries.begin(),aEntries.end(), FindNonZeroTimeout());
     196             : 
     197           0 :     if (iter != aEntries.end())
     198             :     {
     199           0 :         aTimer.SetTimeout(iter->nTimeout);
     200           0 :         aTimer.Start();
     201             :     }
     202             : 
     203           0 :     nTimerStart = nNow;
     204           0 : }
     205             : 
     206           0 : IMPL_LINK_NOARG_TYPED(ScAutoStyleList, TimerHdl, Timer *, void)
     207             : {
     208           0 :     sal_uLong nNow = TimeNow();
     209           0 :     AdjustEntries(aTimer.GetTimeout());             // eingestellte Wartezeit
     210           0 :     ExecuteEntries();
     211           0 :     StartTimer(nNow);
     212         156 : }
     213             : 
     214             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11