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