LibreOffice Module sd (master) 1
MasterPageContainerQueue.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
22
24
25#include <set>
26#include <utility>
27
28namespace sd::sidebar {
29
35
36//===== MasterPageContainerQueue::PreviewCreationRequest ======================
37
39{
40public:
42 : mpDescriptor(std::move(pDescriptor)),
43 mnPriority(nPriority)
44 {}
47 class Compare
48 {
49 public:
51 {
52 if (r1.mnPriority != r2.mnPriority)
53 {
54 // Prefer requests with higher priority.
55 return r1.mnPriority > r2.mnPriority;
56 }
57 else
58 {
59 // Prefer tokens that have been earlier created (those with lower
60 // value).
61 return r1.mpDescriptor->maToken < r2.mpDescriptor->maToken;
62 }
63 }
64 };
66 {
67 public:
69 explicit CompareToken(MasterPageContainer::Token aToken) : maToken(aToken) {}
70 bool operator() (const PreviewCreationRequest& rRequest) const
71 { return maToken==rRequest.mpDescriptor->maToken; }
72 };
73};
74
75//===== MasterPageContainerQueue::RequestQueue ================================
76
78 : public ::std::set<PreviewCreationRequest,PreviewCreationRequest::Compare>
79{
80public:
82};
83
84//===== MasterPageContainerQueue ==============================================
85
87 const std::weak_ptr<ContainerAdapter>& rpContainer)
88{
89 MasterPageContainerQueue* pQueue = new MasterPageContainerQueue(rpContainer);
90 pQueue->LateInit();
91 return pQueue;
92}
93
95 std::weak_ptr<ContainerAdapter> pContainer)
96 : mpWeakContainer(std::move(pContainer)),
97 mpRequestQueue(new RequestQueue()),
98 maDelayedPreviewCreationTimer("sd MasterPageContainerQueue maDelayedPreviewCreationTimer"),
99 mnRequestsServedCount(0)
100{
101}
102
104{
106 while ( ! mpRequestQueue->empty())
107 mpRequestQueue->erase(mpRequestQueue->begin());
108}
109
111{
112 // Set up the timer for the delayed creation of preview bitmaps.
115 LINK(this,MasterPageContainerQueue,DelayedPreviewCreation) );
116}
117
119{
120 bool bSuccess (false);
121 if (rpDescriptor
122 && rpDescriptor->maLargePreview.GetSizePixel().Width() == 0)
123 {
124 sal_Int32 nPriority (CalculatePriority(rpDescriptor));
125
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)
135 {
136 mpRequestQueue->erase(iRequest);
137 iRequest = mpRequestQueue->end();
138 }
139
140 // Add a new request when none exists (or has just been erased).
141 if (iRequest == mpRequestQueue->end())
142 {
143 mpRequestQueue->insert(PreviewCreationRequest(rpDescriptor,nPriority));
145 bSuccess = true;
146 }
147 }
148 return bSuccess;
149}
150
152 const SharedMasterPageDescriptor& rpDescriptor)
153{
154 sal_Int32 nPriority;
155
156 // The cost is used as a starting value.
157 int nCost (0);
158 if (rpDescriptor->mpPreviewProvider != nullptr)
159 {
160 nCost = rpDescriptor->mpPreviewProvider->GetCostIndex();
161 if (rpDescriptor->mpPreviewProvider->NeedsPageObject())
162 if (rpDescriptor->mpPageObjectProvider != nullptr)
163 nCost += rpDescriptor->mpPageObjectProvider->GetCostIndex();
164 }
165
166 // Its negative value is used so that requests with a low cost are
167 // preferred over those with high costs.
168 nPriority = -nCost;
169
170 // Add a term that introduces an order based on the appearance in the
171 // AllMasterPagesSelector.
172 nPriority -= rpDescriptor->maToken / 3;
173
174 // Process requests for the CurrentMasterPagesSelector first.
175 if (rpDescriptor->meOrigin == MasterPageContainer::MASTERPAGE)
176 nPriority += snMasterPagePriorityBoost;
177
178 return nPriority;
179}
180
181IMPL_LINK(MasterPageContainerQueue, DelayedPreviewCreation, Timer*, pTimer, void)
182{
183 bool bIsShowingFullScreenShow (false);
184 bool bWaitForMoreRequests (false);
185
186 do
187 {
188 if (mpRequestQueue->empty())
189 break;
190
191 // First check whether the system is idle.
193 if (nIdleState != tools::IdleState::Idle)
194 {
196 bIsShowingFullScreenShow = true;
197 break;
198 }
199
200 PreviewCreationRequest aRequest (*mpRequestQueue->begin());
201
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))
208 {
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;
215 }
216
217 mpRequestQueue->erase(mpRequestQueue->begin());
218
219 if (aRequest.mpDescriptor)
220 {
221 mnRequestsServedCount += 1;
222 if ( ! mpWeakContainer.expired())
223 {
224 std::shared_ptr<ContainerAdapter> pContainer (mpWeakContainer);
225 if (pContainer != nullptr)
226 pContainer->UpdateDescriptor(aRequest.mpDescriptor,false,true,true);
227 }
228 }
229 }
230 while (false);
231
232 if (!mpRequestQueue->empty() && ! bWaitForMoreRequests)
233 {
234 int nTimeout (snDelayedCreationTimeout);
235 if (bIsShowingFullScreenShow)
236 nTimeout = snDelayedCreationTimeoutWhenNotIdle;
237 maDelayedPreviewCreationTimer.SetTimeout(nTimeout);
238 pTimer->Start();
239 }
240}
241
243{
244 return std::any_of(
245 mpRequestQueue->begin(),
246 mpRequestQueue->end(),
248}
249
251{
252 return mpRequestQueue->empty();
253}
254
256{
258 if (!mpRequestQueue->empty())
260}
261
262} // end of namespace sd::sidebar
263
264/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Stop()
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
bool operator()(const PreviewCreationRequest &r1, const PreviewCreationRequest &r2) const
PreviewCreationRequest(SharedMasterPageDescriptor pDescriptor, int nPriority)
The queue stores and processes all requests from a MasterPageContainer for the creation of previews.
static sal_Int32 CalculatePriority(const SharedMasterPageDescriptor &rDescriptor)
Calculate the priority that defines the order in which requests are processed.
bool RequestPreview(const SharedMasterPageDescriptor &rDescriptor)
This method is typically called for entries in the container for which GetPreviewState() returns OS_C...
bool HasRequest(MasterPageContainer::Token aToken) const
Return <TRUE> when there is a request currently in the queue for the given token.
bool IsEmpty() const
Return <TRUE> when there is at least one request in the queue.
static MasterPageContainerQueue * Create(const std::weak_ptr< ContainerAdapter > &rpContainer)
MasterPageContainerQueue(std::weak_ptr< ContainerAdapter > pContainer)
static const sal_Int32 snMasterPagePriorityBoost
Requests for previews of master pages in a document have their priority increased by this value.
std::unique_ptr< RequestQueue > mpRequestQueue
void ProcessAllRequests()
After this call the queue does not wait anymore for requests with higher priority when only a small n...
static const sal_Int32 snWaitForMoreRequestsPriorityThreshold
When only requests which a priority lower than this threshold exist and not many requests have been m...
static const sal_Int32 snDelayedCreationTimeoutWhenNotIdle
The time to wait when the system is not idle.
static const sal_Int32 snDelayedCreationTimeout
The time to wait (in milliseconds) between the creation of previews.
static sal_uInt32 snWaitForMoreRequestsCount
When only requests which a priority lower than a threshold exist and not more requests than this numb...
static IdleState GetIdleState(const vcl::Window *pWindow)
Determine whether the system is idle.
std::shared_ptr< MasterPageDescriptor > SharedMasterPageDescriptor
IMPL_LINK(CurrentMasterPagesSelector, EventMultiplexerListener, sd::tools::EventMultiplexerEvent &, rEvent, void)
@ FullScreenShowActive
A full screen slide show is running and is active.
@ Idle
When GetIdleState() returns this value, then the system is idle.