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

Generated by: LCOV version 1.10