tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / docshell / autostyl.cxx
blob5b6eaa30c29684d41ee1a9318ca7e0d73ee27574
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <time.h>
21 #include <osl/diagnose.h>
23 #include <address.hxx>
24 #include <autostyl.hxx>
25 #include <docsh.hxx>
27 static sal_uLong TimeNow() // seconds
29 return static_cast<sal_uLong>(time(nullptr));
32 namespace {
34 class FindByRange
36 ScRange maRange;
37 public:
38 explicit FindByRange(const ScRange& r) : maRange(r) {}
39 bool operator() (const ScAutoStyleData& rData) const { return rData.aRange == maRange; }
42 class FindByTimeout
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; }
50 struct FindNonZeroTimeout
52 bool operator() (const ScAutoStyleData& rData) const
54 return rData.nTimeout != 0;
60 ScAutoStyleList::ScAutoStyleList(ScDocShell* pShell)
61 : pDocSh(pShell)
62 , aTimer("ScAutoStyleList Timer")
63 , aInitIdle("ScAutoStyleList InitIdle")
64 , nTimerStart(0)
66 aTimer.SetInvokeHandler( LINK( this, ScAutoStyleList, TimerHdl ) );
67 aInitIdle.SetInvokeHandler( LINK( this, ScAutoStyleList, InitHdl ) );
68 aInitIdle.SetPriority( TaskPriority::HIGHEST );
71 ScAutoStyleList::~ScAutoStyleList()
75 // initial short delay (asynchronous call)
77 void ScAutoStyleList::AddInitial( const ScRange& rRange, const OUString& rStyle1,
78 sal_uLong nTimeout, const OUString& rStyle2 )
80 aInitials.emplace_back( rRange, rStyle1, nTimeout, rStyle2 );
81 aInitIdle.Start();
84 IMPL_LINK_NOARG(ScAutoStyleList, InitHdl, Timer *, void)
86 std::vector<ScAutoStyleInitData> aLocalInitials(std::move(aInitials));
87 for (const auto& rInitial : aLocalInitials)
89 // apply first style immediately
90 pDocSh->DoAutoStyle(rInitial.aRange, rInitial.aStyle1);
92 // add second style to list
93 if (rInitial.nTimeout)
94 AddEntry(rInitial.nTimeout, rInitial.aRange, rInitial.aStyle2 );
98 void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
100 aTimer.Stop();
101 sal_uLong nNow = TimeNow();
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));
107 if (itr != aEntries.end())
108 aEntries.erase(itr);
110 // adjust timeouts of all entries
112 if (!aEntries.empty() && nNow != nTimerStart)
114 OSL_ENSURE(nNow>nTimerStart, "Time is running backwards?");
115 AdjustEntries((nNow-nTimerStart)*1000);
118 // find insert position
119 std::vector<ScAutoStyleData>::iterator iter =
120 ::std::find_if(aEntries.begin(), aEntries.end(), FindByTimeout(nTimeout));
122 aEntries.insert(iter, ScAutoStyleData(nTimeout,rRange,rStyle));
124 // execute expired, restart timer
126 ExecuteEntries();
127 StartTimer(nNow);
130 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff ) // milliseconds
132 for (auto& rEntry : aEntries)
134 if (rEntry.nTimeout <= nDiff)
135 rEntry.nTimeout = 0; // expired
136 else
137 rEntry.nTimeout -= nDiff; // continue counting
141 void ScAutoStyleList::ExecuteEntries()
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)
148 if (itr->nTimeout)
149 break;
151 pDocSh->DoAutoStyle(itr->aRange, itr->aStyle);
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);
158 void ScAutoStyleList::ExecuteAllNow()
160 aTimer.Stop();
162 for (const auto& rEntry : aEntries)
163 pDocSh->DoAutoStyle(rEntry.aRange, rEntry.aStyle);
165 aEntries.clear();
168 void ScAutoStyleList::StartTimer( sal_uLong nNow ) // seconds
170 // find first entry with Timeout != 0
171 std::vector<ScAutoStyleData>::iterator iter =
172 ::std::find_if(aEntries.begin(),aEntries.end(), FindNonZeroTimeout());
174 if (iter != aEntries.end())
176 aTimer.SetTimeout(iter->nTimeout);
177 aTimer.Start();
180 nTimerStart = nNow;
183 IMPL_LINK_NOARG(ScAutoStyleList, TimerHdl, Timer *, void)
185 sal_uLong nNow = TimeNow();
186 AdjustEntries(aTimer.GetTimeout()); // the set waiting time
187 ExecuteEntries();
188 StartTimer(nNow);
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */