Get the style color and number just once
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageDescriptor.cxx
blob549e6c25bd83bec0254457f0979f93f298f7c96a
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 <memory>
21 #include "MasterPageDescriptor.hxx"
22 #include "MasterPageContainerProviders.hxx"
24 #include "DocumentHelper.hxx"
25 #include <PreviewRenderer.hxx>
26 #include <sdpage.hxx>
27 #include <tools/urlobj.hxx>
28 #include <sal/log.hxx>
29 #include <utility>
31 namespace sd::sidebar {
33 //===== MasterPageDescriptor ==================================================
35 MasterPageDescriptor::MasterPageDescriptor (
36 MasterPageContainer::Origin eOrigin,
37 const sal_Int32 nTemplateIndex,
38 std::u16string_view rsURL,
39 OUString sPageName,
40 OUString sStyleName,
41 const bool bIsPrecious,
42 std::shared_ptr<PageObjectProvider> pPageObjectProvider,
43 std::shared_ptr<PreviewProvider> pPreviewProvider)
44 : maToken(MasterPageContainer::NIL_TOKEN),
45 meOrigin(eOrigin),
46 msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DecodeMechanism::Unambiguous)),
47 msPageName(std::move(sPageName)),
48 msStyleName(std::move(sStyleName)),
49 mbIsPrecious(bIsPrecious),
50 mpMasterPage(nullptr),
51 mpSlide(nullptr),
52 mpPreviewProvider(std::move(pPreviewProvider)),
53 mpPageObjectProvider(std::move(pPageObjectProvider)),
54 mnTemplateIndex(nTemplateIndex),
55 meURLClassification(URLCLASS_UNDETERMINED),
56 mnUseCount(0)
60 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
62 maToken = aToken;
65 const Image& MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize) const
67 if (eSize == MasterPageContainer::SMALL)
68 return maSmallPreview;
69 else
70 return maLargePreview;
73 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
74 MasterPageDescriptor::Update (
75 const MasterPageDescriptor& rDescriptor)
77 bool bDataChanged (false);
78 bool bIndexChanged (false);
79 bool bPreviewChanged (false);
81 if (meOrigin==MasterPageContainer::UNKNOWN
82 && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
84 meOrigin = rDescriptor.meOrigin;
85 bIndexChanged = true;
88 if (msURL.isEmpty() && !rDescriptor.msURL.isEmpty())
90 msURL = rDescriptor.msURL;
91 bDataChanged = true;
94 if (msPageName.isEmpty() && !rDescriptor.msPageName.isEmpty())
96 msPageName = rDescriptor.msPageName;
97 bDataChanged = true;
100 if (msStyleName.isEmpty() && !rDescriptor.msStyleName.isEmpty())
102 msStyleName = rDescriptor.msStyleName;
103 bDataChanged = true;
106 if (mpPageObjectProvider == nullptr && rDescriptor.mpPageObjectProvider != nullptr)
108 mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
109 bDataChanged = true;
112 if (mpPreviewProvider == nullptr && rDescriptor.mpPreviewProvider != nullptr)
114 mpPreviewProvider = rDescriptor.mpPreviewProvider;
115 bPreviewChanged = true;
118 if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
120 mnTemplateIndex = rDescriptor.mnTemplateIndex;
121 bIndexChanged = true;
124 // Prepare the list of event types that will be returned.
125 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
126 if (bDataChanged || bIndexChanged || bPreviewChanged)
128 pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>);
129 if (bDataChanged)
130 pResult->push_back(MasterPageContainerChangeEvent::EventType::DATA_CHANGED);
131 if (bIndexChanged)
132 pResult->push_back(MasterPageContainerChangeEvent::EventType::INDEX_CHANGED);
133 if (bPreviewChanged)
134 pResult->push_back(MasterPageContainerChangeEvent::EventType::PREVIEW_CHANGED);
137 return pResult;
140 int MasterPageDescriptor::UpdatePageObject (
141 sal_Int32 nCostThreshold,
142 SdDrawDocument* pDocument)
144 int nModified = 0;
146 // Update the page object when that is not yet known.
147 if (mpMasterPage == nullptr && mpPageObjectProvider != nullptr
148 && (nCostThreshold < 0 || mpPageObjectProvider->GetCostIndex() <= nCostThreshold))
150 // Note that pDocument may be NULL.
152 SdPage* pPage = (*mpPageObjectProvider)(pDocument);
153 if (meOrigin == MasterPageContainer::MASTERPAGE)
155 mpMasterPage = pPage;
156 if (mpMasterPage != nullptr)
157 mpMasterPage->SetPrecious(mbIsPrecious);
159 else
161 // Master pages from templates are copied into the local document.
162 if (pDocument != nullptr)
163 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
164 mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
167 if (mpMasterPage != nullptr)
169 // Update page name and style name.
170 if (msPageName.isEmpty())
171 msPageName = mpMasterPage->GetName();
172 msStyleName = mpMasterPage->GetName();
174 // Delete an existing substitution. The next request for a preview
175 // will create the real one.
176 maSmallPreview = Image();
177 maLargePreview = Image();
178 mpPreviewProvider = std::make_shared<PagePreviewProvider>();
180 else
182 SAL_WARN( "sd", "UpdatePageObject: master page is NULL");
183 return -1;
186 nModified = 1;
189 return nModified;
192 bool MasterPageDescriptor::UpdatePreview (
193 sal_Int32 nCostThreshold,
194 const Size& rSmallSize,
195 const Size& rLargeSize,
196 ::sd::PreviewRenderer& rRenderer)
198 bool bModified (false);
200 // Update the preview when that is not yet known.
201 if (maLargePreview.GetSizePixel().Width() == 0 && mpPreviewProvider != nullptr
202 && (nCostThreshold < 0 || mpPreviewProvider->GetCostIndex() <= nCostThreshold))
204 SdPage* pPage = mpSlide;
205 if (pPage == nullptr)
207 pPage = mpMasterPage;
209 //TODO: Notify LOOL of preview updates.
210 maLargePreview = (*mpPreviewProvider)(
211 rLargeSize.Width(),
212 pPage,
213 rRenderer);
214 if (maLargePreview.GetSizePixel().Width() > 0)
216 // Create the small preview by scaling the large one down.
217 maSmallPreview = rRenderer.ScaleBitmap(
218 maLargePreview.GetBitmapEx(),
219 rSmallSize.Width());
220 // The large preview may not have the desired width. Scale it
221 // accordingly.
222 if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
223 maLargePreview = rRenderer.ScaleBitmap(
224 maLargePreview.GetBitmapEx(),
225 rLargeSize.Width());
226 bModified = true;
230 return bModified;
233 MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification()
235 if (meURLClassification == URLCLASS_UNDETERMINED)
237 if (msURL.isEmpty())
238 meURLClassification = URLCLASS_UNKNOWN;
239 else if (msURL.indexOf("presnt")>=0)
241 meURLClassification = URLCLASS_PRESENTATION;
243 else if (msURL.indexOf("layout")>=0)
245 meURLClassification = URLCLASS_LAYOUT;
247 else if (msURL.indexOf("educate")>=0)
249 meURLClassification = URLCLASS_OTHER;
251 else
253 meURLClassification = URLCLASS_USER;
257 return meURLClassification;
260 //===== URLComparator =========================================================
262 MasterPageDescriptor::URLComparator::URLComparator (OUString sURL)
263 : msURL(std::move(sURL))
267 bool MasterPageDescriptor::URLComparator::operator() (
268 const SharedMasterPageDescriptor& rDescriptor)
270 if (!rDescriptor)
271 return false;
272 else
273 return rDescriptor->msURL == msURL;
276 // ===== StyleNameComparator ==================================================
278 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (OUString sStyleName)
279 : msStyleName(std::move(sStyleName))
283 bool MasterPageDescriptor::StyleNameComparator::operator() (
284 const SharedMasterPageDescriptor& rDescriptor)
286 if (!rDescriptor)
287 return false;
288 else
289 return rDescriptor->msStyleName == msStyleName;
292 //===== PageObjectComparator ==================================================
294 MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
295 : mpMasterPage(pPageObject)
299 bool MasterPageDescriptor::PageObjectComparator::operator() (
300 const SharedMasterPageDescriptor& rDescriptor)
302 if (!rDescriptor)
303 return false;
304 else
305 return rDescriptor->mpMasterPage==mpMasterPage;
308 //===== AllComparator =========================================================
310 MasterPageDescriptor::AllComparator::AllComparator(SharedMasterPageDescriptor aDescriptor)
311 : mpDescriptor(std::move(aDescriptor))
315 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
317 if (!rDescriptor)
318 return false;
319 else
321 // Take URL, page name, style name, and page object into account
322 // when comparing two descriptors. When two descriptors are
323 // identical in any of these values then there are thought of as
324 // equivalent. Only the Origin has to be the same in both
325 // descriptors.
326 return mpDescriptor->meOrigin == rDescriptor->meOrigin
327 && ((!mpDescriptor->msURL.isEmpty() && mpDescriptor->msURL == rDescriptor->msURL)
328 || (!mpDescriptor->msPageName.isEmpty()
329 && mpDescriptor->msPageName == rDescriptor->msPageName)
330 || (!mpDescriptor->msStyleName.isEmpty()
331 && mpDescriptor->msStyleName == rDescriptor->msStyleName)
332 || (mpDescriptor->mpMasterPage != nullptr
333 && mpDescriptor->mpMasterPage == rDescriptor->mpMasterPage)
334 || (mpDescriptor->mpPageObjectProvider != nullptr
335 && rDescriptor->mpPageObjectProvider != nullptr
336 && mpDescriptor->mpPageObjectProvider == rDescriptor->mpPageObjectProvider));
340 } // end of namespace sd::sidebar
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */