Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageDescriptor.cxx
blob87a27d597870f2e373480c39a6d8c30e1685e3b3
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"
23 #include "DocumentHelper.hxx"
24 #include <sdpage.hxx>
25 #include <tools/urlobj.hxx>
27 namespace sd { namespace sidebar {
29 //===== MasterPageDescriptor ==================================================
31 MasterPageDescriptor::MasterPageDescriptor (
32 MasterPageContainer::Origin eOrigin,
33 const sal_Int32 nTemplateIndex,
34 const OUString& rsURL,
35 const OUString& rsPageName,
36 const OUString& rsStyleName,
37 const bool bIsPrecious,
38 const std::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
39 const std::shared_ptr<PreviewProvider>& rpPreviewProvider)
40 : maToken(MasterPageContainer::NIL_TOKEN),
41 meOrigin(eOrigin),
42 msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DecodeMechanism::Unambiguous)),
43 msPageName(rsPageName),
44 msStyleName(rsStyleName),
45 mbIsPrecious(bIsPrecious),
46 mpMasterPage(nullptr),
47 mpSlide(nullptr),
48 maSmallPreview(),
49 maLargePreview(),
50 mpPreviewProvider(rpPreviewProvider),
51 mpPageObjectProvider(rpPageObjectProvider),
52 mnTemplateIndex(nTemplateIndex),
53 meURLClassification(URLCLASS_UNDETERMINED),
54 mnUseCount(0)
58 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
60 maToken = aToken;
63 const Image& MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize) const
65 if (eSize == MasterPageContainer::SMALL)
66 return maSmallPreview;
67 else
68 return maLargePreview;
71 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
72 MasterPageDescriptor::Update (
73 const MasterPageDescriptor& rDescriptor)
75 bool bDataChanged (false);
76 bool bIndexChanged (false);
77 bool bPreviewChanged (false);
79 if (meOrigin==MasterPageContainer::UNKNOWN
80 && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
82 meOrigin = rDescriptor.meOrigin;
83 bIndexChanged = true;
86 if (msURL.isEmpty() && !rDescriptor.msURL.isEmpty())
88 msURL = rDescriptor.msURL;
89 bDataChanged = true;
92 if (msPageName.isEmpty() && !rDescriptor.msPageName.isEmpty())
94 msPageName = rDescriptor.msPageName;
95 bDataChanged = true;
98 if (msStyleName.isEmpty() && !rDescriptor.msStyleName.isEmpty())
100 msStyleName = rDescriptor.msStyleName;
101 bDataChanged = true;
104 if (mpPageObjectProvider.get()==nullptr && rDescriptor.mpPageObjectProvider.get()!=nullptr)
106 mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
107 bDataChanged = true;
110 if (mpPreviewProvider.get()==nullptr && rDescriptor.mpPreviewProvider.get()!=nullptr)
112 mpPreviewProvider = rDescriptor.mpPreviewProvider;
113 bPreviewChanged = true;
116 if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
118 mnTemplateIndex = rDescriptor.mnTemplateIndex;
119 bIndexChanged = true;
122 // Prepare the list of event types that will be returned.
123 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
124 if (bDataChanged || bIndexChanged || bPreviewChanged)
126 pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>);
127 if (bDataChanged)
128 pResult->push_back(MasterPageContainerChangeEvent::EventType::DATA_CHANGED);
129 if (bIndexChanged)
130 pResult->push_back(MasterPageContainerChangeEvent::EventType::INDEX_CHANGED);
131 if (bPreviewChanged)
132 pResult->push_back(MasterPageContainerChangeEvent::EventType::PREVIEW_CHANGED);
135 return pResult;
138 int MasterPageDescriptor::UpdatePageObject (
139 sal_Int32 nCostThreshold,
140 SdDrawDocument* pDocument)
142 int nModified = 0;
144 // Update the page object when that is not yet known.
145 if (mpMasterPage == nullptr
146 && mpPageObjectProvider.get()!=nullptr
147 && (nCostThreshold<0 || mpPageObjectProvider->GetCostIndex()<=nCostThreshold))
149 // Note that pDocument may be NULL.
151 SdPage* pPage = (*mpPageObjectProvider)(pDocument);
152 if (meOrigin == MasterPageContainer::MASTERPAGE)
154 mpMasterPage = pPage;
155 if (mpMasterPage != nullptr)
156 mpMasterPage->SetPrecious(mbIsPrecious);
158 else
160 // Master pages from templates are copied into the local document.
161 if (pDocument != nullptr)
162 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
163 mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
166 if (mpMasterPage != nullptr)
168 // Update page name and style name.
169 if (msPageName.isEmpty())
170 msPageName = mpMasterPage->GetName();
171 msStyleName = mpMasterPage->GetName();
173 // Delete an existing substitution. The next request for a preview
174 // will create the real one.
175 maSmallPreview = Image();
176 maLargePreview = Image();
177 mpPreviewProvider = std::shared_ptr<PreviewProvider>(new PagePreviewProvider());
179 else
181 SAL_WARN( "sd", "UpdatePageObject: master page is NULL");
182 return -1;
185 nModified = 1;
188 return nModified;
191 bool MasterPageDescriptor::UpdatePreview (
192 sal_Int32 nCostThreshold,
193 const Size& rSmallSize,
194 const Size& rLargeSize,
195 ::sd::PreviewRenderer& rRenderer)
197 bool bModified (false);
199 // Update the preview when that is not yet known.
200 if (maLargePreview.GetSizePixel().Width()==0
201 && mpPreviewProvider.get()!=nullptr
202 && (nCostThreshold<0 || mpPreviewProvider->GetCostIndex()<=nCostThreshold))
204 SdPage* pPage = mpSlide;
205 if (pPage == nullptr)
207 pPage = mpMasterPage;
209 maLargePreview = (*mpPreviewProvider)(
210 rLargeSize.Width(),
211 pPage,
212 rRenderer);
213 if (maLargePreview.GetSizePixel().Width() > 0)
215 // Create the small preview by scaling the large one down.
216 maSmallPreview = rRenderer.ScaleBitmap(
217 maLargePreview.GetBitmapEx(),
218 rSmallSize.Width());
219 // The large preview may not have the desired width. Scale it
220 // accordingly.
221 if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
222 maLargePreview = rRenderer.ScaleBitmap(
223 maLargePreview.GetBitmapEx(),
224 rLargeSize.Width());
225 bModified = true;
229 return bModified;
232 MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification()
234 if (meURLClassification == URLCLASS_UNDETERMINED)
236 if (msURL.isEmpty())
237 meURLClassification = URLCLASS_UNKNOWN;
238 else if (msURL.indexOf("presnt")>=0)
240 meURLClassification = URLCLASS_PRESENTATION;
242 else if (msURL.indexOf("layout")>=0)
244 meURLClassification = URLCLASS_LAYOUT;
246 else if (msURL.indexOf("educate")>=0)
248 meURLClassification = URLCLASS_OTHER;
250 else
252 meURLClassification = URLCLASS_USER;
256 return meURLClassification;
259 //===== URLComparator =========================================================
261 MasterPageDescriptor::URLComparator::URLComparator (const OUString& sURL)
262 : msURL(sURL)
266 bool MasterPageDescriptor::URLComparator::operator() (
267 const SharedMasterPageDescriptor& rDescriptor)
269 if (rDescriptor.get() == nullptr)
270 return false;
271 else
272 return rDescriptor->msURL == msURL;
275 // ===== StyleNameComparator ==================================================
277 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const OUString& sStyleName)
278 : msStyleName(sStyleName)
282 bool MasterPageDescriptor::StyleNameComparator::operator() (
283 const SharedMasterPageDescriptor& rDescriptor)
285 if (rDescriptor.get() == nullptr)
286 return false;
287 else
288 return rDescriptor->msStyleName == msStyleName;
291 //===== PageObjectComparator ==================================================
293 MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
294 : mpMasterPage(pPageObject)
298 bool MasterPageDescriptor::PageObjectComparator::operator() (
299 const SharedMasterPageDescriptor& rDescriptor)
301 if (rDescriptor.get() == nullptr)
302 return false;
303 else
304 return rDescriptor->mpMasterPage==mpMasterPage;
307 //===== AllComparator =========================================================
309 MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
310 : mpDescriptor(rDescriptor)
314 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
316 if (rDescriptor.get() == nullptr)
317 return false;
318 else
320 // Take URL, page name, style name, and page object into account
321 // when comparing two descriptors. When two descriptors are
322 // identical in any of these values then there are thought of as
323 // equivalent. Only the Origin has to be the same in both
324 // descriptors.
325 return
326 mpDescriptor->meOrigin == rDescriptor->meOrigin
327 && (
328 (!mpDescriptor->msURL.isEmpty()
329 && mpDescriptor->msURL == rDescriptor->msURL)
330 || (!mpDescriptor->msPageName.isEmpty()
331 && mpDescriptor->msPageName == rDescriptor->msPageName)
332 || (!mpDescriptor->msStyleName.isEmpty()
333 && mpDescriptor->msStyleName == rDescriptor->msStyleName)
334 || (mpDescriptor->mpMasterPage!=nullptr
335 && mpDescriptor->mpMasterPage==rDescriptor->mpMasterPage)
336 || (mpDescriptor->mpPageObjectProvider.get()!=nullptr
337 && rDescriptor->mpPageObjectProvider.get()!=nullptr
338 && mpDescriptor->mpPageObjectProvider==rDescriptor->mpPageObjectProvider));
342 } } // end of namespace sd::sidebar
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */