1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "MasterPageDescriptor.hxx"
23 #include "DocumentHelper.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
),
42 msURL(INetURLObject(rsURL
).GetMainURL(INetURLObject::DecodeMechanism::Unambiguous
)),
43 msPageName(rsPageName
),
44 msStyleName(rsStyleName
),
45 mbIsPrecious(bIsPrecious
),
46 mpMasterPage(nullptr),
50 mpPreviewProvider(rpPreviewProvider
),
51 mpPageObjectProvider(rpPageObjectProvider
),
52 mnTemplateIndex(nTemplateIndex
),
53 meURLClassification(URLCLASS_UNDETERMINED
),
58 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken
)
63 const Image
& MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize
) const
65 if (eSize
== MasterPageContainer::SMALL
)
66 return maSmallPreview
;
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
;
86 if (msURL
.isEmpty() && !rDescriptor
.msURL
.isEmpty())
88 msURL
= rDescriptor
.msURL
;
92 if (msPageName
.isEmpty() && !rDescriptor
.msPageName
.isEmpty())
94 msPageName
= rDescriptor
.msPageName
;
98 if (msStyleName
.isEmpty() && !rDescriptor
.msStyleName
.isEmpty())
100 msStyleName
= rDescriptor
.msStyleName
;
104 if (mpPageObjectProvider
.get()==nullptr && rDescriptor
.mpPageObjectProvider
.get()!=nullptr)
106 mpPageObjectProvider
= rDescriptor
.mpPageObjectProvider
;
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
>);
128 pResult
->push_back(MasterPageContainerChangeEvent::EventType::DATA_CHANGED
);
130 pResult
->push_back(MasterPageContainerChangeEvent::EventType::INDEX_CHANGED
);
132 pResult
->push_back(MasterPageContainerChangeEvent::EventType::PREVIEW_CHANGED
);
138 int MasterPageDescriptor::UpdatePageObject (
139 sal_Int32 nCostThreshold
,
140 SdDrawDocument
* pDocument
)
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
);
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());
181 SAL_WARN( "sd", "UpdatePageObject: master page is NULL");
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
)(
213 if (maLargePreview
.GetSizePixel().Width() > 0)
215 // Create the small preview by scaling the large one down.
216 maSmallPreview
= rRenderer
.ScaleBitmap(
217 maLargePreview
.GetBitmapEx(),
219 // The large preview may not have the desired width. Scale it
221 if (maLargePreview
.GetSizePixel().Width() != rLargeSize
.Width())
222 maLargePreview
= rRenderer
.ScaleBitmap(
223 maLargePreview
.GetBitmapEx(),
232 MasterPageDescriptor::URLClassification
MasterPageDescriptor::GetURLClassification()
234 if (meURLClassification
== URLCLASS_UNDETERMINED
)
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
;
252 meURLClassification
= URLCLASS_USER
;
256 return meURLClassification
;
259 //===== URLComparator =========================================================
261 MasterPageDescriptor::URLComparator::URLComparator (const OUString
& sURL
)
266 bool MasterPageDescriptor::URLComparator::operator() (
267 const SharedMasterPageDescriptor
& rDescriptor
)
269 if (rDescriptor
.get() == nullptr)
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)
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)
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)
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
326 mpDescriptor
->meOrigin
== rDescriptor
->meOrigin
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: */