bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageDescriptor.cxx
blob8151a4f6918cdc74b424a2db607e9d5ca45079a2
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>
30 namespace sd { namespace sidebar {
32 //===== MasterPageDescriptor ==================================================
34 MasterPageDescriptor::MasterPageDescriptor (
35 MasterPageContainer::Origin eOrigin,
36 const sal_Int32 nTemplateIndex,
37 const OUString& rsURL,
38 const OUString& rsPageName,
39 const OUString& rsStyleName,
40 const bool bIsPrecious,
41 const std::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
42 const std::shared_ptr<PreviewProvider>& rpPreviewProvider)
43 : maToken(MasterPageContainer::NIL_TOKEN),
44 meOrigin(eOrigin),
45 msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DecodeMechanism::Unambiguous)),
46 msPageName(rsPageName),
47 msStyleName(rsStyleName),
48 mbIsPrecious(bIsPrecious),
49 mpMasterPage(nullptr),
50 mpSlide(nullptr),
51 maSmallPreview(),
52 maLargePreview(),
53 mpPreviewProvider(rpPreviewProvider),
54 mpPageObjectProvider(rpPageObjectProvider),
55 mnTemplateIndex(nTemplateIndex),
56 meURLClassification(URLCLASS_UNDETERMINED),
57 mnUseCount(0)
61 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
63 maToken = aToken;
66 const Image& MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize) const
68 if (eSize == MasterPageContainer::SMALL)
69 return maSmallPreview;
70 else
71 return maLargePreview;
74 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
75 MasterPageDescriptor::Update (
76 const MasterPageDescriptor& rDescriptor)
78 bool bDataChanged (false);
79 bool bIndexChanged (false);
80 bool bPreviewChanged (false);
82 if (meOrigin==MasterPageContainer::UNKNOWN
83 && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
85 meOrigin = rDescriptor.meOrigin;
86 bIndexChanged = true;
89 if (msURL.isEmpty() && !rDescriptor.msURL.isEmpty())
91 msURL = rDescriptor.msURL;
92 bDataChanged = true;
95 if (msPageName.isEmpty() && !rDescriptor.msPageName.isEmpty())
97 msPageName = rDescriptor.msPageName;
98 bDataChanged = true;
101 if (msStyleName.isEmpty() && !rDescriptor.msStyleName.isEmpty())
103 msStyleName = rDescriptor.msStyleName;
104 bDataChanged = true;
107 if (mpPageObjectProvider == nullptr && rDescriptor.mpPageObjectProvider != nullptr)
109 mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
110 bDataChanged = true;
113 if (mpPreviewProvider == nullptr && rDescriptor.mpPreviewProvider != nullptr)
115 mpPreviewProvider = rDescriptor.mpPreviewProvider;
116 bPreviewChanged = true;
119 if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
121 mnTemplateIndex = rDescriptor.mnTemplateIndex;
122 bIndexChanged = true;
125 // Prepare the list of event types that will be returned.
126 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
127 if (bDataChanged || bIndexChanged || bPreviewChanged)
129 pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>);
130 if (bDataChanged)
131 pResult->push_back(MasterPageContainerChangeEvent::EventType::DATA_CHANGED);
132 if (bIndexChanged)
133 pResult->push_back(MasterPageContainerChangeEvent::EventType::INDEX_CHANGED);
134 if (bPreviewChanged)
135 pResult->push_back(MasterPageContainerChangeEvent::EventType::PREVIEW_CHANGED);
138 return pResult;
141 int MasterPageDescriptor::UpdatePageObject (
142 sal_Int32 nCostThreshold,
143 SdDrawDocument* pDocument)
145 int nModified = 0;
147 // Update the page object when that is not yet known.
148 if (mpMasterPage == nullptr && mpPageObjectProvider != nullptr
149 && (nCostThreshold < 0 || mpPageObjectProvider->GetCostIndex() <= nCostThreshold))
151 // Note that pDocument may be NULL.
153 SdPage* pPage = (*mpPageObjectProvider)(pDocument);
154 if (meOrigin == MasterPageContainer::MASTERPAGE)
156 mpMasterPage = pPage;
157 if (mpMasterPage != nullptr)
158 mpMasterPage->SetPrecious(mbIsPrecious);
160 else
162 // Master pages from templates are copied into the local document.
163 if (pDocument != nullptr)
164 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
165 mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
168 if (mpMasterPage != nullptr)
170 // Update page name and style name.
171 if (msPageName.isEmpty())
172 msPageName = mpMasterPage->GetName();
173 msStyleName = mpMasterPage->GetName();
175 // Delete an existing substitution. The next request for a preview
176 // will create the real one.
177 maSmallPreview = Image();
178 maLargePreview = Image();
179 mpPreviewProvider = std::shared_ptr<PreviewProvider>(new PagePreviewProvider());
181 else
183 SAL_WARN( "sd", "UpdatePageObject: master page is NULL");
184 return -1;
187 nModified = 1;
190 return nModified;
193 bool MasterPageDescriptor::UpdatePreview (
194 sal_Int32 nCostThreshold,
195 const Size& rSmallSize,
196 const Size& rLargeSize,
197 ::sd::PreviewRenderer& rRenderer)
199 bool bModified (false);
201 // Update the preview when that is not yet known.
202 if (maLargePreview.GetSizePixel().Width() == 0 && mpPreviewProvider != nullptr
203 && (nCostThreshold < 0 || mpPreviewProvider->GetCostIndex() <= nCostThreshold))
205 SdPage* pPage = mpSlide;
206 if (pPage == nullptr)
208 pPage = mpMasterPage;
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 (const OUString& sURL)
263 : msURL(sURL)
267 bool MasterPageDescriptor::URLComparator::operator() (
268 const SharedMasterPageDescriptor& rDescriptor)
270 if (rDescriptor.get() == nullptr)
271 return false;
272 else
273 return rDescriptor->msURL == msURL;
276 // ===== StyleNameComparator ==================================================
278 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const OUString& sStyleName)
279 : msStyleName(sStyleName)
283 bool MasterPageDescriptor::StyleNameComparator::operator() (
284 const SharedMasterPageDescriptor& rDescriptor)
286 if (rDescriptor.get() == nullptr)
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.get() == nullptr)
303 return false;
304 else
305 return rDescriptor->mpMasterPage==mpMasterPage;
308 //===== AllComparator =========================================================
310 MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
311 : mpDescriptor(rDescriptor)
315 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
317 if (rDescriptor.get() == nullptr)
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: */