LibreOffice Module sc (master)  1
autostyl.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 
20 #include <time.h>
21 #include <osl/diagnose.h>
22 
23 #include <address.hxx>
24 #include <autostyl.hxx>
25 #include <docsh.hxx>
26 
27 static sal_uLong TimeNow() // seconds
28 {
29  return static_cast<sal_uLong>(time(nullptr));
30 }
31 
32 namespace {
33 
34 class FindByRange
35 {
37 public:
38  explicit FindByRange(const ScRange& r) : maRange(r) {}
39  bool operator() (const ScAutoStyleData& rData) const { return rData.aRange == maRange; }
40 };
41 
42 class FindByTimeout
43 {
44  sal_uLong mnTimeout;
45 public:
46  explicit FindByTimeout(sal_uLong n) : mnTimeout(n) {}
47  bool operator() (const ScAutoStyleData& rData) const { return rData.nTimeout >= mnTimeout; }
48 };
49 
50 struct FindNonZeroTimeout
51 {
52  bool operator() (const ScAutoStyleData& rData) const
53  {
54  return rData.nTimeout != 0;
55  }
56 };
57 
58 }
59 
61  : pDocSh(pShell)
62  , aTimer("ScAutoStyleList Timer")
63  , aInitIdle("ScAutoStyleList InitIdle")
64  , nTimerStart(0)
65 {
66  aTimer.SetInvokeHandler( LINK( this, ScAutoStyleList, TimerHdl ) );
67  aInitIdle.SetInvokeHandler( LINK( this, ScAutoStyleList, InitHdl ) );
68  aInitIdle.SetPriority( TaskPriority::HIGHEST );
69 }
70 
72 {
73 }
74 
75 // initial short delay (asynchronous call)
76 
77 void ScAutoStyleList::AddInitial( const ScRange& rRange, const OUString& rStyle1,
78  sal_uLong nTimeout, const OUString& rStyle2 )
79 {
80  aInitials.emplace_back( rRange, rStyle1, nTimeout, rStyle2 );
81  aInitIdle.Start();
82 }
83 
85 {
86  for (const auto& rInitial : aInitials)
87  {
88  // apply first style immediately
89  pDocSh->DoAutoStyle(rInitial.aRange, rInitial.aStyle1);
90 
91  // add second style to list
92  if (rInitial.nTimeout)
93  AddEntry(rInitial.nTimeout, rInitial.aRange, rInitial.aStyle2 );
94  }
95 
96  aInitials.clear();
97 }
98 
99 void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
100 {
101  aTimer.Stop();
102  sal_uLong nNow = TimeNow();
103 
104  // Remove the first item with the same range.
105  std::vector<ScAutoStyleData>::iterator itr =
106  ::std::find_if(aEntries.begin(), aEntries.end(), FindByRange(rRange));
107 
108  if (itr != aEntries.end())
109  aEntries.erase(itr);
110 
111  // adjust timeouts of all entries
112 
113  if (!aEntries.empty() && nNow != nTimerStart)
114  {
115  OSL_ENSURE(nNow>nTimerStart, "Time is running backwards?");
116  AdjustEntries((nNow-nTimerStart)*1000);
117  }
118 
119  // find insert position
120  std::vector<ScAutoStyleData>::iterator iter =
121  ::std::find_if(aEntries.begin(), aEntries.end(), FindByTimeout(nTimeout));
122 
123  aEntries.insert(iter, ScAutoStyleData(nTimeout,rRange,rStyle));
124 
125  // execute expired, restart timer
126 
127  ExecuteEntries();
128  StartTimer(nNow);
129 }
130 
131 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff ) // milliseconds
132 {
133  for (auto& rEntry : aEntries)
134  {
135  if (rEntry.nTimeout <= nDiff)
136  rEntry.nTimeout = 0; // expired
137  else
138  rEntry.nTimeout -= nDiff; // continue counting
139  }
140 }
141 
143 {
144  // Execute and remove all items with timeout == 0 from the begin position
145  // until the first item with non-zero timeout value.
146  std::vector<ScAutoStyleData>::iterator itr = aEntries.begin(), itrEnd = aEntries.end();
147  for (; itr != itrEnd; ++itr)
148  {
149  if (itr->nTimeout)
150  break;
151 
152  pDocSh->DoAutoStyle(itr->aRange, itr->aStyle);
153  }
154  // At this point itr should be on the first item with non-zero timeout, or
155  // the end position in case all items have timeout == 0.
156  aEntries.erase(aEntries.begin(), itr);
157 }
158 
160 {
161  aTimer.Stop();
162 
163  for (const auto& rEntry : aEntries)
164  pDocSh->DoAutoStyle(rEntry.aRange, rEntry.aStyle);
165 
166  aEntries.clear();
167 }
168 
169 void ScAutoStyleList::StartTimer( sal_uLong nNow ) // seconds
170 {
171  // find first entry with Timeout != 0
172  std::vector<ScAutoStyleData>::iterator iter =
173  ::std::find_if(aEntries.begin(),aEntries.end(), FindNonZeroTimeout());
174 
175  if (iter != aEntries.end())
176  {
177  aTimer.SetTimeout(iter->nTimeout);
178  aTimer.Start();
179  }
180 
181  nTimerStart = nNow;
182 }
183 
185 {
186  sal_uLong nNow = TimeNow();
187  AdjustEntries(aTimer.GetTimeout()); // the set waiting time
188  ExecuteEntries();
189  StartTimer(nNow);
190 }
191 
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_uLong TimeNow()
Definition: autostyl.cxx:27
ScDocShell * pDocSh
Definition: autostyl.hxx:57
std::vector< ScAutoStyleInitData > aInitials
Definition: autostyl.hxx:62
sal_uIntPtr sal_uLong
void ExecuteEntries()
Definition: autostyl.cxx:142
sal_uLong nTimerStart
Definition: autostyl.hxx:60
sal_uLong nTimeout
Definition: autostyl.hxx:34
virtual void Start() override
std::vector< ScAutoStyleData > aEntries
Definition: autostyl.hxx:61
void DoAutoStyle(const ScRange &rRange, const OUString &rStyle)
Definition: docsh4.cxx:1502
ScRange aRange
Definition: autostyl.hxx:35
virtual void Start() override
IMPL_LINK_NOARG(ScAutoStyleList, InitHdl, Timer *, void)
Definition: autostyl.cxx:84
void StartTimer(sal_uLong nNow)
Definition: autostyl.cxx:169
void AdjustEntries(sal_uLong nDiff)
Definition: autostyl.cxx:131
void SetTimeout(sal_uInt64 nTimeoutMs)
void ExecuteAllNow()
Definition: autostyl.cxx:159
void Stop()
void SetInvokeHandler(const Link< Timer *, void > &rLink)
void SetPriority(TaskPriority ePriority)
void AddEntry(sal_uLong nTimeout, const ScRange &rRange, const OUString &rStyle)
Definition: autostyl.cxx:99
void AddInitial(const ScRange &rRange, const OUString &rStyle1, sal_uLong nTimeout, const OUString &rStyle2)
Definition: autostyl.cxx:77
B2DRange maRange
ScAutoStyleList(ScDocShell *pShell)
Definition: autostyl.cxx:60