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 .
20 #include "MasterPageDescriptor.hxx"
22 #include "DocumentHelper.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
),
41 msURL(INetURLObject(rsURL
).GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS
)),
42 msPageName(rsPageName
),
43 msStyleName(rsStyleName
),
44 mbIsPrecious(bIsPrecious
),
49 mpPreviewProvider(rpPreviewProvider
),
50 mpPageObjectProvider(rpPageObjectProvider
),
51 mnTemplateIndex(nTemplateIndex
),
52 meURLClassification(URLCLASS_UNDETERMINED
),
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
)
85 Image
MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize
) const
87 if (eSize
== MasterPageContainer::SMALL
)
88 return maSmallPreview
;
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
;
114 if (msPageName
.isEmpty() && !rDescriptor
.msPageName
.isEmpty())
116 msPageName
= rDescriptor
.msPageName
;
120 if (msStyleName
.isEmpty() && !rDescriptor
.msStyleName
.isEmpty())
122 msStyleName
= rDescriptor
.msStyleName
;
126 if (mpPageObjectProvider
.get()==NULL
&& rDescriptor
.mpPageObjectProvider
.get()!=NULL
)
128 mpPageObjectProvider
= rDescriptor
.mpPageObjectProvider
;
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
>());
150 pResult
->push_back(MasterPageContainerChangeEvent::DATA_CHANGED
);
152 pResult
->push_back(MasterPageContainerChangeEvent::INDEX_CHANGED
);
154 pResult
->push_back(MasterPageContainerChangeEvent::PREVIEW_CHANGED
);
160 int MasterPageDescriptor::UpdatePageObject (
161 sal_Int32 nCostThreshold
,
162 SdDrawDocument
* pDocument
)
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
);
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());
203 DBG_ASSERT(false, "UpdatePageObject: master page is NULL");
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
;
229 pPage
= mpMasterPage
;
231 maLargePreview
= (*mpPreviewProvider
)(
235 if (maLargePreview
.GetSizePixel().Width() > 0)
237 // Create the small preview by scaling the large one down.
238 maSmallPreview
= rRenderer
.ScaleBitmap(
239 maLargePreview
.GetBitmapEx(),
241 // The large preview may not have the desired width. Scale it
243 if (maLargePreview
.GetSizePixel().Width() != rLargeSize
.Width())
244 maLargePreview
= rRenderer
.ScaleBitmap(
245 maLargePreview
.GetBitmapEx(),
254 MasterPageDescriptor::URLClassification
MasterPageDescriptor::GetURLClassification()
256 if (meURLClassification
== URLCLASS_UNDETERMINED
)
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
;
274 meURLClassification
= URLCLASS_USER
;
278 return meURLClassification
;
281 //===== URLComparator =========================================================
283 MasterPageDescriptor::URLComparator::URLComparator (const OUString
& sURL
)
288 bool MasterPageDescriptor::URLComparator::operator() (
289 const SharedMasterPageDescriptor
& rDescriptor
)
291 if (rDescriptor
.get() == NULL
)
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
)
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
)
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
)
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
348 mpDescriptor
->meOrigin
== rDescriptor
->meOrigin
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: */