bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageDescriptor.cxx
blob5c4889d9476510a3ec76cfac6df652c5c49942e7
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 "MasterPageDescriptor.hxx"
22 #include "DocumentHelper.hxx"
23 #include "sdpage.hxx"
24 #include <tools/urlobj.hxx>
26 namespace sd { namespace sidebar {
28 //===== MasterPageDescriptor ==================================================
30 MasterPageDescriptor::MasterPageDescriptor (
31 MasterPageContainer::Origin eOrigin,
32 const sal_Int32 nTemplateIndex,
33 const OUString& rsURL,
34 const OUString& rsPageName,
35 const OUString& rsStyleName,
36 const bool bIsPrecious,
37 const ::boost::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
38 const ::boost::shared_ptr<PreviewProvider>& rpPreviewProvider)
39 : maToken(MasterPageContainer::NIL_TOKEN),
40 meOrigin(eOrigin),
41 msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS)),
42 msPageName(rsPageName),
43 msStyleName(rsStyleName),
44 mbIsPrecious(bIsPrecious),
45 mpMasterPage(NULL),
46 mpSlide(NULL),
47 maSmallPreview(),
48 maLargePreview(),
49 mpPreviewProvider(rpPreviewProvider),
50 mpPageObjectProvider(rpPageObjectProvider),
51 mnTemplateIndex(nTemplateIndex),
52 meURLClassification(URLCLASS_UNDETERMINED),
53 mnUseCount(0)
57 MasterPageDescriptor::MasterPageDescriptor (const MasterPageDescriptor& rDescriptor)
58 : maToken(rDescriptor.maToken),
59 meOrigin(rDescriptor.meOrigin),
60 msURL(rDescriptor.msURL),
61 msPageName(rDescriptor.msPageName),
62 msStyleName(rDescriptor.msStyleName),
63 mbIsPrecious(rDescriptor.mbIsPrecious),
64 mpMasterPage(rDescriptor.mpMasterPage),
65 mpSlide(rDescriptor.mpSlide),
66 maSmallPreview(rDescriptor.maSmallPreview),
67 maLargePreview(rDescriptor.maLargePreview),
68 mpPreviewProvider(rDescriptor.mpPreviewProvider),
69 mpPageObjectProvider(rDescriptor.mpPageObjectProvider),
70 mnTemplateIndex(rDescriptor.mnTemplateIndex),
71 meURLClassification(rDescriptor.meURLClassification),
72 mnUseCount(rDescriptor.mnUseCount)
76 MasterPageDescriptor::~MasterPageDescriptor()
80 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
82 maToken = aToken;
85 Image MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize) const
87 if (eSize == MasterPageContainer::SMALL)
88 return maSmallPreview;
89 else
90 return maLargePreview;
93 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
94 MasterPageDescriptor::Update (
95 const MasterPageDescriptor& rDescriptor)
97 bool bDataChanged (false);
98 bool bIndexChanged (false);
99 bool bPreviewChanged (false);
101 if (meOrigin==MasterPageContainer::UNKNOWN
102 && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
104 meOrigin = rDescriptor.meOrigin;
105 bIndexChanged = true;
108 if (msURL.isEmpty() && !rDescriptor.msURL.isEmpty())
110 msURL = rDescriptor.msURL;
111 bDataChanged = true;
114 if (msPageName.isEmpty() && !rDescriptor.msPageName.isEmpty())
116 msPageName = rDescriptor.msPageName;
117 bDataChanged = true;
120 if (msStyleName.isEmpty() && !rDescriptor.msStyleName.isEmpty())
122 msStyleName = rDescriptor.msStyleName;
123 bDataChanged = true;
126 if (mpPageObjectProvider.get()==NULL && rDescriptor.mpPageObjectProvider.get()!=NULL)
128 mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
129 bDataChanged = true;
132 if (mpPreviewProvider.get()==NULL && rDescriptor.mpPreviewProvider.get()!=NULL)
134 mpPreviewProvider = rDescriptor.mpPreviewProvider;
135 bPreviewChanged = true;
138 if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
140 mnTemplateIndex = rDescriptor.mnTemplateIndex;
141 bIndexChanged = true;
144 // Prepare the list of event types that will be returned.
145 ::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
146 if (bDataChanged || bIndexChanged || bPreviewChanged)
148 pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>());
149 if (bDataChanged)
150 pResult->push_back(MasterPageContainerChangeEvent::DATA_CHANGED);
151 if (bIndexChanged)
152 pResult->push_back(MasterPageContainerChangeEvent::INDEX_CHANGED);
153 if (bPreviewChanged)
154 pResult->push_back(MasterPageContainerChangeEvent::PREVIEW_CHANGED);
157 return pResult;
160 int MasterPageDescriptor::UpdatePageObject (
161 sal_Int32 nCostThreshold,
162 SdDrawDocument* pDocument)
164 int nModified = 0;
166 // Update the page object when that is not yet known.
167 if (mpMasterPage == NULL
168 && mpPageObjectProvider.get()!=NULL
169 && (nCostThreshold<0 || mpPageObjectProvider->GetCostIndex()<=nCostThreshold))
171 // Note that pDocument may be NULL.
173 SdPage* pPage = (*mpPageObjectProvider)(pDocument);
174 if (meOrigin == MasterPageContainer::MASTERPAGE)
176 mpMasterPage = pPage;
177 if (mpMasterPage != NULL)
178 mpMasterPage->SetPrecious(mbIsPrecious);
180 else
182 // Master pages from templates are copied into the local document.
183 if (pDocument != NULL)
184 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
185 mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
188 if (mpMasterPage != NULL)
190 // Update page name and style name.
191 if (msPageName.isEmpty())
192 msPageName = mpMasterPage->GetName();
193 msStyleName = mpMasterPage->GetName();
195 // Delete an existing substitution. The next request for a preview
196 // will create the real one.
197 maSmallPreview = Image();
198 maLargePreview = Image();
199 mpPreviewProvider = ::boost::shared_ptr<PreviewProvider>(new PagePreviewProvider());
201 else
203 DBG_ASSERT(false, "UpdatePageObject: master page is NULL");
204 return -1;
207 nModified = 1;
210 return nModified;
213 bool MasterPageDescriptor::UpdatePreview (
214 sal_Int32 nCostThreshold,
215 const Size& rSmallSize,
216 const Size& rLargeSize,
217 ::sd::PreviewRenderer& rRenderer)
219 bool bModified (false);
221 // Update the preview when that is not yet known.
222 if (maLargePreview.GetSizePixel().Width()==0
223 && mpPreviewProvider.get()!=NULL
224 && (nCostThreshold<0 || mpPreviewProvider->GetCostIndex()<=nCostThreshold))
226 SdPage* pPage = mpSlide;
227 if (pPage == NULL)
229 pPage = mpMasterPage;
231 maLargePreview = (*mpPreviewProvider)(
232 rLargeSize.Width(),
233 pPage,
234 rRenderer);
235 if (maLargePreview.GetSizePixel().Width() > 0)
237 // Create the small preview by scaling the large one down.
238 maSmallPreview = rRenderer.ScaleBitmap(
239 maLargePreview.GetBitmapEx(),
240 rSmallSize.Width());
241 // The large preview may not have the desired width. Scale it
242 // accrodingly.
243 if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
244 maLargePreview = rRenderer.ScaleBitmap(
245 maLargePreview.GetBitmapEx(),
246 rLargeSize.Width());
247 bModified = true;
251 return bModified;
254 MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification()
256 if (meURLClassification == URLCLASS_UNDETERMINED)
258 if (msURL.isEmpty())
259 meURLClassification = URLCLASS_UNKNOWN;
260 else if (msURL.indexOf("presnt")>=0)
262 meURLClassification = URLCLASS_PRESENTATION;
264 else if (msURL.indexOf("layout")>=0)
266 meURLClassification = URLCLASS_LAYOUT;
268 else if (msURL.indexOf("educate")>=0)
270 meURLClassification = URLCLASS_OTHER;
272 else
274 meURLClassification = URLCLASS_USER;
278 return meURLClassification;
281 //===== URLComparator =========================================================
283 MasterPageDescriptor::URLComparator::URLComparator (const OUString& sURL)
284 : msURL(sURL)
288 bool MasterPageDescriptor::URLComparator::operator() (
289 const SharedMasterPageDescriptor& rDescriptor)
291 if (rDescriptor.get() == NULL)
292 return false;
293 else
294 return rDescriptor->msURL.equals(msURL);
297 // ===== StyleNameComparator ==================================================
299 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const OUString& sStyleName)
300 : msStyleName(sStyleName)
304 bool MasterPageDescriptor::StyleNameComparator::operator() (
305 const SharedMasterPageDescriptor& rDescriptor)
307 if (rDescriptor.get() == NULL)
308 return false;
309 else
310 return rDescriptor->msStyleName.equals(msStyleName);
313 //===== PageObjectComparator ==================================================
315 MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
316 : mpMasterPage(pPageObject)
320 bool MasterPageDescriptor::PageObjectComparator::operator() (
321 const SharedMasterPageDescriptor& rDescriptor)
323 if (rDescriptor.get() == NULL)
324 return false;
325 else
326 return rDescriptor->mpMasterPage==mpMasterPage;
329 //===== AllComparator =========================================================
331 MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
332 : mpDescriptor(rDescriptor)
336 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
338 if (rDescriptor.get() == NULL)
339 return false;
340 else
342 // Take URL, page name, style name, and page object into account
343 // when comparing two descriptors. When two descriptors are
344 // identical in any of these values then there are thought of as
345 // equivalent. Only the Origin has to be the same in both
346 // descriptors.
347 return
348 mpDescriptor->meOrigin == rDescriptor->meOrigin
349 && (
350 (!mpDescriptor->msURL.isEmpty()
351 && mpDescriptor->msURL.equals(rDescriptor->msURL))
352 || (!mpDescriptor->msPageName.isEmpty()
353 && mpDescriptor->msPageName.equals(rDescriptor->msPageName))
354 || (!mpDescriptor->msStyleName.isEmpty()
355 && mpDescriptor->msStyleName.equals(rDescriptor->msStyleName))
356 || (mpDescriptor->mpMasterPage!=NULL
357 && mpDescriptor->mpMasterPage==rDescriptor->mpMasterPage)
358 || (mpDescriptor->mpPageObjectProvider.get()!=NULL
359 && rDescriptor->mpPageObjectProvider.get()!=NULL
360 && mpDescriptor->mpPageObjectProvider==rDescriptor->mpPageObjectProvider));
364 } } // end of namespace sd::sidebar
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */