Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageContainerQueue.cxx
blob8b9a167ad20fd801a32721bb5ff485a24323e306
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 "MasterPageContainerQueue.hxx"
22 #include <tools/IdleDetection.hxx>
24 #include <set>
26 namespace sd { namespace sidebar {
28 const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeout (15);
29 const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeoutWhenNotIdle (100);
30 const sal_Int32 MasterPageContainerQueue::snMasterPagePriorityBoost (5);
31 const sal_Int32 MasterPageContainerQueue::snWaitForMoreRequestsPriorityThreshold (-10);
32 sal_uInt32 MasterPageContainerQueue::snWaitForMoreRequestsCount(15);
34 //===== MasterPageContainerQueue::PreviewCreationRequest ======================
36 class MasterPageContainerQueue::PreviewCreationRequest
38 public:
39 PreviewCreationRequest (const SharedMasterPageDescriptor& rpDescriptor, int nPriority)
40 : mpDescriptor(rpDescriptor),
41 mnPriority(nPriority)
43 SharedMasterPageDescriptor mpDescriptor;
44 int mnPriority;
45 class Compare
47 public:
48 bool operator() (const PreviewCreationRequest& r1,const PreviewCreationRequest& r2) const
50 if (r1.mnPriority != r2.mnPriority)
52 // Prefer requests with higher priority.
53 return r1.mnPriority > r2.mnPriority;
55 else
57 // Prefer tokens that have been earlier created (those with lower
58 // value).
59 return r1.mpDescriptor->maToken < r2.mpDescriptor->maToken;
63 class CompareToken
65 public:
66 MasterPageContainer::Token maToken;
67 explicit CompareToken(MasterPageContainer::Token aToken) : maToken(aToken) {}
68 bool operator() (const PreviewCreationRequest& rRequest) const
69 { return maToken==rRequest.mpDescriptor->maToken; }
73 //===== MasterPageContainerQueue::RequestQueue ================================
75 class MasterPageContainerQueue::RequestQueue
76 : public ::std::set<PreviewCreationRequest,PreviewCreationRequest::Compare>
78 public:
79 RequestQueue() {}
82 //===== MasterPageContainerQueue ==============================================
84 MasterPageContainerQueue* MasterPageContainerQueue::Create (
85 const std::weak_ptr<ContainerAdapter>& rpContainer)
87 MasterPageContainerQueue* pQueue = new MasterPageContainerQueue(rpContainer);
88 pQueue->LateInit();
89 return pQueue;
92 MasterPageContainerQueue::MasterPageContainerQueue (
93 const std::weak_ptr<ContainerAdapter>& rpContainer)
94 : mpWeakContainer(rpContainer),
95 mpRequestQueue(new RequestQueue()),
96 maDelayedPreviewCreationTimer(),
97 mnRequestsServedCount(0)
101 MasterPageContainerQueue::~MasterPageContainerQueue()
103 maDelayedPreviewCreationTimer.Stop();
104 while ( ! mpRequestQueue->empty())
105 mpRequestQueue->erase(mpRequestQueue->begin());
108 void MasterPageContainerQueue::LateInit()
110 // Set up the timer for the delayed creation of preview bitmaps.
111 maDelayedPreviewCreationTimer.SetTimeout (snDelayedCreationTimeout);
112 maDelayedPreviewCreationTimer.SetInvokeHandler(
113 LINK(this,MasterPageContainerQueue,DelayedPreviewCreation) );
116 bool MasterPageContainerQueue::RequestPreview (const SharedMasterPageDescriptor& rpDescriptor)
118 bool bSuccess (false);
119 if (rpDescriptor.get() != nullptr
120 && rpDescriptor->maLargePreview.GetSizePixel().Width() == 0)
122 sal_Int32 nPriority (CalculatePriority(rpDescriptor));
124 // Add a new or replace an existing request.
125 RequestQueue::iterator iRequest (::std::find_if(
126 mpRequestQueue->begin(),
127 mpRequestQueue->end(),
128 PreviewCreationRequest::CompareToken(rpDescriptor->maToken)));
129 // When a request for the same token exists then the lowest of the
130 // two priorities is used.
131 if (iRequest != mpRequestQueue->end())
132 if (iRequest->mnPriority < nPriority)
134 mpRequestQueue->erase(iRequest);
135 iRequest = mpRequestQueue->end();
138 // Add a new request when none exists (or has just been erased).
139 if (iRequest == mpRequestQueue->end())
141 mpRequestQueue->insert(PreviewCreationRequest(rpDescriptor,nPriority));
142 maDelayedPreviewCreationTimer.Start();
143 bSuccess = true;
146 return bSuccess;
149 sal_Int32 MasterPageContainerQueue::CalculatePriority (
150 const SharedMasterPageDescriptor& rpDescriptor)
152 sal_Int32 nPriority;
154 // The cost is used as a starting value.
155 int nCost (0);
156 if (rpDescriptor->mpPreviewProvider.get() != nullptr)
158 nCost = rpDescriptor->mpPreviewProvider->GetCostIndex();
159 if (rpDescriptor->mpPreviewProvider->NeedsPageObject())
160 if (rpDescriptor->mpPageObjectProvider.get() != nullptr)
161 nCost += rpDescriptor->mpPageObjectProvider->GetCostIndex();
164 // Its negative value is used so that requests with a low cost are
165 // preferred over those with high costs.
166 nPriority = -nCost;
168 // Add a term that introduces an order based on the appearance in the
169 // AllMasterPagesSelector.
170 nPriority -= rpDescriptor->maToken / 3;
172 // Process requests for the CurrentMasterPagesSelector first.
173 if (rpDescriptor->meOrigin == MasterPageContainer::MASTERPAGE)
174 nPriority += snMasterPagePriorityBoost;
176 return nPriority;
179 IMPL_LINK(MasterPageContainerQueue, DelayedPreviewCreation, Timer*, pTimer, void)
181 bool bIsShowingFullScreenShow (false);
182 bool bWaitForMoreRequests (false);
186 if (mpRequestQueue->empty())
187 break;
189 // First check whether the system is idle.
190 tools::IdleState nIdleState (tools::IdleDetection::GetIdleState(nullptr));
191 if (nIdleState != tools::IdleState::Idle)
193 if (nIdleState & tools::IdleState::FullScreenShowActive)
194 bIsShowingFullScreenShow = true;
195 break;
198 PreviewCreationRequest aRequest (*mpRequestQueue->begin());
200 // Check if the request should really be processed right now.
201 // Reasons to not do it are when its cost is high and not many other
202 // requests have been inserted into the queue that would otherwise
203 // be processed first.
204 if (aRequest.mnPriority < snWaitForMoreRequestsPriorityThreshold
205 && (mnRequestsServedCount+mpRequestQueue->size() < snWaitForMoreRequestsCount))
207 // Wait for more requests before this one is processed. Note
208 // that the queue processing is not started anew when this
209 // method is left. That is done when the next request is
210 // inserted.
211 bWaitForMoreRequests = true;
212 break;
215 mpRequestQueue->erase(mpRequestQueue->begin());
217 if (aRequest.mpDescriptor.get() != nullptr)
219 mnRequestsServedCount += 1;
220 if ( ! mpWeakContainer.expired())
222 std::shared_ptr<ContainerAdapter> pContainer (mpWeakContainer);
223 if (pContainer.get() != nullptr)
224 pContainer->UpdateDescriptor(aRequest.mpDescriptor,false,true,true);
228 while (false);
230 if (mpRequestQueue->size() > 0 && ! bWaitForMoreRequests)
232 int nTimeout (snDelayedCreationTimeout);
233 if (bIsShowingFullScreenShow)
234 nTimeout = snDelayedCreationTimeoutWhenNotIdle;
235 maDelayedPreviewCreationTimer.SetTimeout(nTimeout);
236 pTimer->Start();
240 bool MasterPageContainerQueue::HasRequest (MasterPageContainer::Token aToken) const
242 RequestQueue::iterator iRequest (::std::find_if(
243 mpRequestQueue->begin(),
244 mpRequestQueue->end(),
245 PreviewCreationRequest::CompareToken(aToken)));
246 return (iRequest != mpRequestQueue->end());
249 bool MasterPageContainerQueue::IsEmpty() const
251 return mpRequestQueue->empty();
254 void MasterPageContainerQueue::ProcessAllRequests()
256 snWaitForMoreRequestsCount = 0;
257 if (mpRequestQueue->size() > 0)
258 maDelayedPreviewCreationTimer.Start();
261 } } // end of namespace sd::sidebar
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */