bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageContainerQueue.cxx
blob2655fad6f3e4a815197181072356a9b0c05c4768
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"
21 #include "MasterPageContainerProviders.hxx"
23 #include <tools/IdleDetection.hxx>
24 #include <PreviewRenderer.hxx>
26 #include <set>
28 namespace sd { namespace sidebar {
30 const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeout (15);
31 const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeoutWhenNotIdle (100);
32 const sal_Int32 MasterPageContainerQueue::snMasterPagePriorityBoost (5);
33 const sal_Int32 MasterPageContainerQueue::snWaitForMoreRequestsPriorityThreshold (-10);
34 sal_uInt32 MasterPageContainerQueue::snWaitForMoreRequestsCount(15);
36 //===== MasterPageContainerQueue::PreviewCreationRequest ======================
38 class MasterPageContainerQueue::PreviewCreationRequest
40 public:
41 PreviewCreationRequest (const SharedMasterPageDescriptor& rpDescriptor, int nPriority)
42 : mpDescriptor(rpDescriptor),
43 mnPriority(nPriority)
45 SharedMasterPageDescriptor const mpDescriptor;
46 int const mnPriority;
47 class Compare
49 public:
50 bool operator() (const PreviewCreationRequest& r1,const PreviewCreationRequest& r2) const
52 if (r1.mnPriority != r2.mnPriority)
54 // Prefer requests with higher priority.
55 return r1.mnPriority > r2.mnPriority;
57 else
59 // Prefer tokens that have been earlier created (those with lower
60 // value).
61 return r1.mpDescriptor->maToken < r2.mpDescriptor->maToken;
65 class CompareToken
67 public:
68 MasterPageContainer::Token const maToken;
69 explicit CompareToken(MasterPageContainer::Token aToken) : maToken(aToken) {}
70 bool operator() (const PreviewCreationRequest& rRequest) const
71 { return maToken==rRequest.mpDescriptor->maToken; }
75 //===== MasterPageContainerQueue::RequestQueue ================================
77 class MasterPageContainerQueue::RequestQueue
78 : public ::std::set<PreviewCreationRequest,PreviewCreationRequest::Compare>
80 public:
81 RequestQueue() {}
84 //===== MasterPageContainerQueue ==============================================
86 MasterPageContainerQueue* MasterPageContainerQueue::Create (
87 const std::weak_ptr<ContainerAdapter>& rpContainer)
89 MasterPageContainerQueue* pQueue = new MasterPageContainerQueue(rpContainer);
90 pQueue->LateInit();
91 return pQueue;
94 MasterPageContainerQueue::MasterPageContainerQueue (
95 const std::weak_ptr<ContainerAdapter>& rpContainer)
96 : mpWeakContainer(rpContainer),
97 mpRequestQueue(new RequestQueue()),
98 maDelayedPreviewCreationTimer(),
99 mnRequestsServedCount(0)
103 MasterPageContainerQueue::~MasterPageContainerQueue()
105 maDelayedPreviewCreationTimer.Stop();
106 while ( ! mpRequestQueue->empty())
107 mpRequestQueue->erase(mpRequestQueue->begin());
110 void MasterPageContainerQueue::LateInit()
112 // Set up the timer for the delayed creation of preview bitmaps.
113 maDelayedPreviewCreationTimer.SetTimeout (snDelayedCreationTimeout);
114 maDelayedPreviewCreationTimer.SetInvokeHandler(
115 LINK(this,MasterPageContainerQueue,DelayedPreviewCreation) );
118 bool MasterPageContainerQueue::RequestPreview (const SharedMasterPageDescriptor& rpDescriptor)
120 bool bSuccess (false);
121 if (rpDescriptor.get() != nullptr
122 && rpDescriptor->maLargePreview.GetSizePixel().Width() == 0)
124 sal_Int32 nPriority (CalculatePriority(rpDescriptor));
126 // Add a new or replace an existing request.
127 RequestQueue::iterator iRequest (::std::find_if(
128 mpRequestQueue->begin(),
129 mpRequestQueue->end(),
130 PreviewCreationRequest::CompareToken(rpDescriptor->maToken)));
131 // When a request for the same token exists then the lowest of the
132 // two priorities is used.
133 if (iRequest != mpRequestQueue->end())
134 if (iRequest->mnPriority < nPriority)
136 mpRequestQueue->erase(iRequest);
137 iRequest = mpRequestQueue->end();
140 // Add a new request when none exists (or has just been erased).
141 if (iRequest == mpRequestQueue->end())
143 mpRequestQueue->insert(PreviewCreationRequest(rpDescriptor,nPriority));
144 maDelayedPreviewCreationTimer.Start();
145 bSuccess = true;
148 return bSuccess;
151 sal_Int32 MasterPageContainerQueue::CalculatePriority (
152 const SharedMasterPageDescriptor& rpDescriptor)
154 sal_Int32 nPriority;
156 // The cost is used as a starting value.
157 int nCost (0);
158 if (rpDescriptor->mpPreviewProvider != nullptr)
160 nCost = rpDescriptor->mpPreviewProvider->GetCostIndex();
161 if (rpDescriptor->mpPreviewProvider->NeedsPageObject())
162 if (rpDescriptor->mpPageObjectProvider != nullptr)
163 nCost += rpDescriptor->mpPageObjectProvider->GetCostIndex();
166 // Its negative value is used so that requests with a low cost are
167 // preferred over those with high costs.
168 nPriority = -nCost;
170 // Add a term that introduces an order based on the appearance in the
171 // AllMasterPagesSelector.
172 nPriority -= rpDescriptor->maToken / 3;
174 // Process requests for the CurrentMasterPagesSelector first.
175 if (rpDescriptor->meOrigin == MasterPageContainer::MASTERPAGE)
176 nPriority += snMasterPagePriorityBoost;
178 return nPriority;
181 IMPL_LINK(MasterPageContainerQueue, DelayedPreviewCreation, Timer*, pTimer, void)
183 bool bIsShowingFullScreenShow (false);
184 bool bWaitForMoreRequests (false);
188 if (mpRequestQueue->empty())
189 break;
191 // First check whether the system is idle.
192 tools::IdleState nIdleState (tools::IdleDetection::GetIdleState(nullptr));
193 if (nIdleState != tools::IdleState::Idle)
195 if (nIdleState & tools::IdleState::FullScreenShowActive)
196 bIsShowingFullScreenShow = true;
197 break;
200 PreviewCreationRequest aRequest (*mpRequestQueue->begin());
202 // Check if the request should really be processed right now.
203 // Reasons to not do it are when its cost is high and not many other
204 // requests have been inserted into the queue that would otherwise
205 // be processed first.
206 if (aRequest.mnPriority < snWaitForMoreRequestsPriorityThreshold
207 && (mnRequestsServedCount+mpRequestQueue->size() < snWaitForMoreRequestsCount))
209 // Wait for more requests before this one is processed. Note
210 // that the queue processing is not started anew when this
211 // method is left. That is done when the next request is
212 // inserted.
213 bWaitForMoreRequests = true;
214 break;
217 mpRequestQueue->erase(mpRequestQueue->begin());
219 if (aRequest.mpDescriptor.get() != nullptr)
221 mnRequestsServedCount += 1;
222 if ( ! mpWeakContainer.expired())
224 std::shared_ptr<ContainerAdapter> pContainer (mpWeakContainer);
225 if (pContainer != nullptr)
226 pContainer->UpdateDescriptor(aRequest.mpDescriptor,false,true,true);
230 while (false);
232 if (!mpRequestQueue->empty() && ! bWaitForMoreRequests)
234 int nTimeout (snDelayedCreationTimeout);
235 if (bIsShowingFullScreenShow)
236 nTimeout = snDelayedCreationTimeoutWhenNotIdle;
237 maDelayedPreviewCreationTimer.SetTimeout(nTimeout);
238 pTimer->Start();
242 bool MasterPageContainerQueue::HasRequest (MasterPageContainer::Token aToken) const
244 return std::any_of(
245 mpRequestQueue->begin(),
246 mpRequestQueue->end(),
247 PreviewCreationRequest::CompareToken(aToken));
250 bool MasterPageContainerQueue::IsEmpty() const
252 return mpRequestQueue->empty();
255 void MasterPageContainerQueue::ProcessAllRequests()
257 snWaitForMoreRequestsCount = 0;
258 if (!mpRequestQueue->empty())
259 maDelayedPreviewCreationTimer.Start();
262 } } // end of namespace sd::sidebar
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */