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 "PresenterPreviewCache.hxx"
22 #include <cache/SlsPageCache.hxx>
23 #include <cache/SlsCacheContext.hxx>
24 #include <vcl/bitmapex.hxx>
26 #include <cppcanvas/vclfactory.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <com/sun/star/drawing/XDrawPage.hpp>
29 #include <osl/diagnose.h>
30 #include <unomodel.hxx>
32 namespace com::sun::star::uno
{ class XComponentContext
; }
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::sd::slidesorter::cache
;
38 namespace sd::presenter
{
40 class PresenterPreviewCache::PresenterCacheContext
: public CacheContext
43 PresenterCacheContext();
45 void SetDocumentSlides (
46 const Reference
<container::XIndexAccess
>& rxSlides
,
47 const rtl::Reference
<SdXImpressDocument
>& rxDocument
);
48 void SetVisibleSlideRange (
49 const sal_Int32 nFirstVisibleSlideIndex
,
50 const sal_Int32 nLastVisibleSlideIndex
);
51 const SdrPage
* GetPage (const sal_Int32 nSlideIndex
) const;
52 void AddPreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
53 void RemovePreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
56 virtual void NotifyPreviewCreation (CacheKey aKey
) override
;
57 virtual bool IsIdle() override
;
58 virtual bool IsVisible (CacheKey aKey
) override
;
59 virtual const SdrPage
* GetPage (CacheKey aKey
) override
;
60 virtual std::shared_ptr
<std::vector
<CacheKey
> > GetEntryList (bool bVisible
) override
;
61 virtual sal_Int32
GetPriority (CacheKey aKey
) override
;
62 virtual SdXImpressDocument
* GetModel() override
;
65 Reference
<container::XIndexAccess
> mxSlides
;
66 rtl::Reference
<SdXImpressDocument
> mxDocument
;
67 sal_Int32 mnFirstVisibleSlideIndex
;
68 sal_Int32 mnLastVisibleSlideIndex
;
69 typedef ::std::vector
<css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
> > ListenerContainer
;
70 ListenerContainer maListeners
;
72 void CallListeners (const sal_Int32 nSlideIndex
);
75 //===== PresenterPreviewCache =================================================
77 PresenterPreviewCache::PresenterPreviewCache ()
78 : maPreviewSize(Size(200,200)),
79 mpCacheContext(std::make_shared
<PresenterCacheContext
>()),
80 mpCache(std::make_shared
<PageCache
>(maPreviewSize
, Bitmap::HasFastScale(), mpCacheContext
))
84 PresenterPreviewCache::~PresenterPreviewCache()
88 //----- XInitialize -----------------------------------------------------------
90 void SAL_CALL
PresenterPreviewCache::initialize (const Sequence
<Any
>& rArguments
)
92 if (rArguments
.hasElements())
93 throw RuntimeException();
96 OUString
PresenterPreviewCache::getImplementationName() {
97 return u
"com.sun.star.comp.Draw.PresenterPreviewCache"_ustr
;
100 sal_Bool
PresenterPreviewCache::supportsService(OUString
const & ServiceName
) {
101 return cppu::supportsService(this, ServiceName
);
104 css::uno::Sequence
<OUString
> PresenterPreviewCache::getSupportedServiceNames() {
105 return {u
"com.sun.star.drawing.PresenterPreviewCache"_ustr
};
108 //----- XSlidePreviewCache ----------------------------------------------------
110 void SAL_CALL
PresenterPreviewCache::setDocumentSlides (
111 const Reference
<container::XIndexAccess
>& rxSlides
,
112 const Reference
<XInterface
>& rxDocument
)
115 OSL_ASSERT(mpCacheContext
!= nullptr);
117 SdXImpressDocument
* pImpressDoc
= dynamic_cast<SdXImpressDocument
*>(rxDocument
.get());
119 mpCacheContext
->SetDocumentSlides(rxSlides
, pImpressDoc
);
122 void SAL_CALL
PresenterPreviewCache::setVisibleRange (
123 sal_Int32 nFirstVisibleSlideIndex
,
124 sal_Int32 nLastVisibleSlideIndex
)
127 OSL_ASSERT(mpCacheContext
!= nullptr);
129 mpCacheContext
->SetVisibleSlideRange (nFirstVisibleSlideIndex
, nLastVisibleSlideIndex
);
132 void SAL_CALL
PresenterPreviewCache::setPreviewSize (
133 const css::geometry::IntegerSize2D
& rSize
)
136 OSL_ASSERT(mpCache
!= nullptr);
138 maPreviewSize
= Size(rSize
.Width
, rSize
.Height
);
139 mpCache
->ChangeSize(maPreviewSize
, Bitmap::HasFastScale());
142 Reference
<rendering::XBitmap
> SAL_CALL
PresenterPreviewCache::getSlidePreview (
143 sal_Int32 nSlideIndex
,
144 const Reference
<rendering::XCanvas
>& rxCanvas
)
147 OSL_ASSERT(mpCacheContext
!= nullptr);
149 cppcanvas::CanvasSharedPtr
pCanvas (
150 cppcanvas::VCLFactory::createCanvas(rxCanvas
));
152 const SdrPage
* pPage
= mpCacheContext
->GetPage(nSlideIndex
);
153 if (pPage
== nullptr)
154 throw RuntimeException();
156 const BitmapEx
aPreview (mpCache
->GetPreviewBitmap(pPage
,true));
157 if (aPreview
.IsEmpty())
160 return cppcanvas::VCLFactory::createBitmap(
162 aPreview
)->getUNOBitmap();
165 void SAL_CALL
PresenterPreviewCache::addPreviewCreationNotifyListener (
166 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
171 mpCacheContext
->AddPreviewCreationNotifyListener(rxListener
);
174 void SAL_CALL
PresenterPreviewCache::removePreviewCreationNotifyListener (
175 const css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
>& rxListener
)
178 mpCacheContext
->RemovePreviewCreationNotifyListener(rxListener
);
181 void SAL_CALL
PresenterPreviewCache::pause()
184 OSL_ASSERT(mpCache
!= nullptr);
188 void SAL_CALL
PresenterPreviewCache::resume()
191 OSL_ASSERT(mpCache
!= nullptr);
195 void PresenterPreviewCache::ThrowIfDisposed()
199 throw lang::DisposedException (u
"PresenterPreviewCache object has already been disposed"_ustr
,
200 static_cast<uno::XWeak
*>(this));
204 //===== PresenterPreviewCache::PresenterCacheContext ==========================
206 PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext()
207 : mnFirstVisibleSlideIndex(-1),
208 mnLastVisibleSlideIndex(-1)
212 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
213 const Reference
<container::XIndexAccess
>& rxSlides
,
214 const rtl::Reference
<SdXImpressDocument
>& rxDocument
)
217 mxDocument
= rxDocument
;
218 mnFirstVisibleSlideIndex
= -1;
219 mnLastVisibleSlideIndex
= -1;
222 void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
223 const sal_Int32 nFirstVisibleSlideIndex
,
224 const sal_Int32 nLastVisibleSlideIndex
)
226 if (nFirstVisibleSlideIndex
> nLastVisibleSlideIndex
|| nFirstVisibleSlideIndex
<0)
228 mnFirstVisibleSlideIndex
= -1;
229 mnLastVisibleSlideIndex
= -1;
233 mnFirstVisibleSlideIndex
= nFirstVisibleSlideIndex
;
234 mnLastVisibleSlideIndex
= nLastVisibleSlideIndex
;
236 if (mxSlides
.is() && mnLastVisibleSlideIndex
>= mxSlides
->getCount())
237 mnLastVisibleSlideIndex
= mxSlides
->getCount() - 1;
240 void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
241 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
243 maListeners
.push_back(rxListener
);
246 void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
247 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
249 auto iListener
= std::find(maListeners
.begin(), maListeners
.end(), rxListener
);
250 if (iListener
!= maListeners
.end())
251 maListeners
.erase(iListener
);
254 //----- CacheContext ----------------------------------------------------------
256 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
259 if ( ! mxSlides
.is())
261 const sal_Int32
nCount(mxSlides
->getCount());
262 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
263 if (aKey
== GetPage(nIndex
))
264 CallListeners(nIndex
);
267 bool PresenterPreviewCache::PresenterCacheContext::IsIdle()
272 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey
)
274 if (mnFirstVisibleSlideIndex
< 0)
276 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
278 const SdrPage
* pPage
= GetPage(nIndex
);
285 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey
)
290 std::shared_ptr
<std::vector
<CacheKey
> >
291 PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible
)
293 auto pKeys
= std::make_shared
<std::vector
<CacheKey
>>();
295 if ( ! mxSlides
.is())
298 const sal_Int32
nFirstIndex (bVisible
? mnFirstVisibleSlideIndex
: 0);
299 const sal_Int32
nLastIndex (bVisible
? mnLastVisibleSlideIndex
: mxSlides
->getCount()-1);
304 for (sal_Int32 nIndex
=nFirstIndex
; nIndex
<=nLastIndex
; ++nIndex
)
306 pKeys
->push_back(GetPage(nIndex
));
312 sal_Int32
PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey
)
314 if ( ! mxSlides
.is())
317 const sal_Int32
nCount (mxSlides
->getCount());
319 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
320 if (aKey
== GetPage(nIndex
))
321 return -nCount
-1+nIndex
;
323 for (sal_Int32 nIndex
=0; nIndex
<=nCount
; ++nIndex
)
324 if (aKey
== GetPage(nIndex
))
330 SdXImpressDocument
* PresenterPreviewCache::PresenterCacheContext::GetModel()
332 return mxDocument
.get();
335 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (
336 const sal_Int32 nSlideIndex
) const
338 if ( ! mxSlides
.is())
340 if (nSlideIndex
< 0 || nSlideIndex
>= mxSlides
->getCount())
343 Reference
<drawing::XDrawPage
> xSlide (mxSlides
->getByIndex(nSlideIndex
), UNO_QUERY
);
344 const SdPage
* pPage
= SdPage::getImplementation(xSlide
);
348 void PresenterPreviewCache::PresenterCacheContext::CallListeners (
349 const sal_Int32 nIndex
)
351 ListenerContainer
aListeners (maListeners
);
352 for (const auto& rxListener
: aListeners
)
356 rxListener
->notifyPreviewCreation(nIndex
);
358 catch (lang::DisposedException
&)
360 RemovePreviewCreationNotifyListener(rxListener
);
365 } // end of namespace ::sd::presenter
368 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
369 com_sun_star_comp_Draw_PresenterPreviewCache_get_implementation(css::uno::XComponentContext
*,
370 css::uno::Sequence
<css::uno::Any
> const &)
372 return cppu::acquire(new sd::presenter::PresenterPreviewCache
);
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */