1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include "MasterPageDescriptor.hxx"
#include "MasterPageContainerProviders.hxx"
#include "DocumentHelper.hxx"
#include <PreviewRenderer.hxx>
#include <sdpage.hxx>
#include <tools/urlobj.hxx>
#include <sal/log.hxx>
namespace sd::sidebar {
//===== MasterPageDescriptor ==================================================
MasterPageDescriptor::MasterPageDescriptor (
MasterPageContainer::Origin eOrigin,
const sal_Int32 nTemplateIndex,
std::u16string_view rsURL,
const OUString& rsPageName,
const OUString& rsStyleName,
const bool bIsPrecious,
const std::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
const std::shared_ptr<PreviewProvider>& rpPreviewProvider)
: maToken(MasterPageContainer::NIL_TOKEN),
meOrigin(eOrigin),
msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DecodeMechanism::Unambiguous)),
msPageName(rsPageName),
msStyleName(rsStyleName),
mbIsPrecious(bIsPrecious),
mpMasterPage(nullptr),
mpSlide(nullptr),
mpPreviewProvider(rpPreviewProvider),
mpPageObjectProvider(rpPageObjectProvider),
mnTemplateIndex(nTemplateIndex),
meURLClassification(URLCLASS_UNDETERMINED),
mnUseCount(0)
{
}
void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
{
maToken = aToken;
}
const Image& MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize) const
{
if (eSize == MasterPageContainer::SMALL)
return maSmallPreview;
else
return maLargePreview;
}
::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
MasterPageDescriptor::Update (
const MasterPageDescriptor& rDescriptor)
{
bool bDataChanged (false);
bool bIndexChanged (false);
bool bPreviewChanged (false);
if (meOrigin==MasterPageContainer::UNKNOWN
&& rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
{
meOrigin = rDescriptor.meOrigin;
bIndexChanged = true;
}
if (msURL.isEmpty() && !rDescriptor.msURL.isEmpty())
{
msURL = rDescriptor.msURL;
bDataChanged = true;
}
if (msPageName.isEmpty() && !rDescriptor.msPageName.isEmpty())
{
msPageName = rDescriptor.msPageName;
bDataChanged = true;
}
if (msStyleName.isEmpty() && !rDescriptor.msStyleName.isEmpty())
{
msStyleName = rDescriptor.msStyleName;
bDataChanged = true;
}
if (mpPageObjectProvider == nullptr && rDescriptor.mpPageObjectProvider != nullptr)
{
mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
bDataChanged = true;
}
if (mpPreviewProvider == nullptr && rDescriptor.mpPreviewProvider != nullptr)
{
mpPreviewProvider = rDescriptor.mpPreviewProvider;
bPreviewChanged = true;
}
if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
{
mnTemplateIndex = rDescriptor.mnTemplateIndex;
bIndexChanged = true;
}
// Prepare the list of event types that will be returned.
::std::unique_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
if (bDataChanged || bIndexChanged || bPreviewChanged)
{
pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>);
if (bDataChanged)
pResult->push_back(MasterPageContainerChangeEvent::EventType::DATA_CHANGED);
if (bIndexChanged)
pResult->push_back(MasterPageContainerChangeEvent::EventType::INDEX_CHANGED);
if (bPreviewChanged)
pResult->push_back(MasterPageContainerChangeEvent::EventType::PREVIEW_CHANGED);
}
return pResult;
}
int MasterPageDescriptor::UpdatePageObject (
sal_Int32 nCostThreshold,
SdDrawDocument* pDocument)
{
int nModified = 0;
// Update the page object when that is not yet known.
if (mpMasterPage == nullptr && mpPageObjectProvider != nullptr
&& (nCostThreshold < 0 || mpPageObjectProvider->GetCostIndex() <= nCostThreshold))
{
// Note that pDocument may be NULL.
SdPage* pPage = (*mpPageObjectProvider)(pDocument);
if (meOrigin == MasterPageContainer::MASTERPAGE)
{
mpMasterPage = pPage;
if (mpMasterPage != nullptr)
mpMasterPage->SetPrecious(mbIsPrecious);
}
else
{
// Master pages from templates are copied into the local document.
if (pDocument != nullptr)
mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
}
if (mpMasterPage != nullptr)
{
// Update page name and style name.
if (msPageName.isEmpty())
msPageName = mpMasterPage->GetName();
msStyleName = mpMasterPage->GetName();
// Delete an existing substitution. The next request for a preview
// will create the real one.
maSmallPreview = Image();
maLargePreview = Image();
mpPreviewProvider = std::make_shared<PagePreviewProvider>();
}
else
{
SAL_WARN( "sd", "UpdatePageObject: master page is NULL");
return -1;
}
nModified = 1;
}
return nModified;
}
bool MasterPageDescriptor::UpdatePreview (
sal_Int32 nCostThreshold,
const Size& rSmallSize,
const Size& rLargeSize,
::sd::PreviewRenderer& rRenderer)
{
bool bModified (false);
// Update the preview when that is not yet known.
if (maLargePreview.GetSizePixel().Width() == 0 && mpPreviewProvider != nullptr
&& (nCostThreshold < 0 || mpPreviewProvider->GetCostIndex() <= nCostThreshold))
{
SdPage* pPage = mpSlide;
if (pPage == nullptr)
{
pPage = mpMasterPage;
}
//TODO: Notify LOOL of preview updates.
maLargePreview = (*mpPreviewProvider)(
rLargeSize.Width(),
pPage,
rRenderer);
if (maLargePreview.GetSizePixel().Width() > 0)
{
// Create the small preview by scaling the large one down.
maSmallPreview = rRenderer.ScaleBitmap(
maLargePreview.GetBitmapEx(),
rSmallSize.Width());
// The large preview may not have the desired width. Scale it
// accordingly.
if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
maLargePreview = rRenderer.ScaleBitmap(
maLargePreview.GetBitmapEx(),
rLargeSize.Width());
bModified = true;
}
}
return bModified;
}
MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification()
{
if (meURLClassification == URLCLASS_UNDETERMINED)
{
if (msURL.isEmpty())
meURLClassification = URLCLASS_UNKNOWN;
else if (msURL.indexOf("presnt")>=0)
{
meURLClassification = URLCLASS_PRESENTATION;
}
else if (msURL.indexOf("layout")>=0)
{
meURLClassification = URLCLASS_LAYOUT;
}
else if (msURL.indexOf("educate")>=0)
{
meURLClassification = URLCLASS_OTHER;
}
else
{
meURLClassification = URLCLASS_USER;
}
}
return meURLClassification;
}
//===== URLComparator =========================================================
MasterPageDescriptor::URLComparator::URLComparator (const OUString& sURL)
: msURL(sURL)
{
}
bool MasterPageDescriptor::URLComparator::operator() (
const SharedMasterPageDescriptor& rDescriptor)
{
if (!rDescriptor)
return false;
else
return rDescriptor->msURL == msURL;
}
// ===== StyleNameComparator ==================================================
MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const OUString& sStyleName)
: msStyleName(sStyleName)
{
}
bool MasterPageDescriptor::StyleNameComparator::operator() (
const SharedMasterPageDescriptor& rDescriptor)
{
if (!rDescriptor)
return false;
else
return rDescriptor->msStyleName == msStyleName;
}
//===== PageObjectComparator ==================================================
MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
: mpMasterPage(pPageObject)
{
}
bool MasterPageDescriptor::PageObjectComparator::operator() (
const SharedMasterPageDescriptor& rDescriptor)
{
if (!rDescriptor)
return false;
else
return rDescriptor->mpMasterPage==mpMasterPage;
}
//===== AllComparator =========================================================
MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
: mpDescriptor(rDescriptor)
{
}
bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
{
if (!rDescriptor)
return false;
else
{
// Take URL, page name, style name, and page object into account
// when comparing two descriptors. When two descriptors are
// identical in any of these values then there are thought of as
// equivalent. Only the Origin has to be the same in both
// descriptors.
return mpDescriptor->meOrigin == rDescriptor->meOrigin
&& ((!mpDescriptor->msURL.isEmpty() && mpDescriptor->msURL == rDescriptor->msURL)
|| (!mpDescriptor->msPageName.isEmpty()
&& mpDescriptor->msPageName == rDescriptor->msPageName)
|| (!mpDescriptor->msStyleName.isEmpty()
&& mpDescriptor->msStyleName == rDescriptor->msStyleName)
|| (mpDescriptor->mpMasterPage != nullptr
&& mpDescriptor->mpMasterPage == rDescriptor->mpMasterPage)
|| (mpDescriptor->mpPageObjectProvider != nullptr
&& rDescriptor->mpPageObjectProvider != nullptr
&& mpDescriptor->mpPageObjectProvider == rDescriptor->mpPageObjectProvider));
}
}
} // end of namespace sd::sidebar
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|