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"
23 #include "cache/SlsCacheContext.hxx"
24 #include "tools/IdleDetection.hxx"
26 #include <cppcanvas/vclfactory.hxx>
27 #include <com/sun/star/drawing/XDrawPage.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::uno
;
31 using namespace ::sd::slidesorter::cache
;
33 namespace sd
{ namespace presenter
{
35 class PresenterPreviewCache::PresenterCacheContext
: public CacheContext
38 PresenterCacheContext();
39 virtual ~PresenterCacheContext();
41 void SetDocumentSlides (
42 const Reference
<container::XIndexAccess
>& rxSlides
,
43 const Reference
<XInterface
>& rxDocument
);
44 void SetVisibleSlideRange (
45 const sal_Int32 nFirstVisibleSlideIndex
,
46 const sal_Int32 nLastVisibleSlideIndex
);
47 const SdrPage
* GetPage (const sal_Int32 nSlideIndex
) const;
48 void AddPreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
49 void RemovePreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
52 virtual void NotifyPreviewCreation (
54 const Bitmap
& rPreview
) SAL_OVERRIDE
;
55 virtual bool IsIdle() SAL_OVERRIDE
;
56 virtual bool IsVisible (CacheKey aKey
) SAL_OVERRIDE
;
57 virtual const SdrPage
* GetPage (CacheKey aKey
) SAL_OVERRIDE
;
58 virtual ::boost::shared_ptr
<std::vector
<CacheKey
> > GetEntryList (bool bVisible
) SAL_OVERRIDE
;
59 virtual sal_Int32
GetPriority (CacheKey aKey
) SAL_OVERRIDE
;
60 virtual ::com::sun::star::uno::Reference
<com::sun::star::uno::XInterface
> GetModel() SAL_OVERRIDE
;
63 Reference
<container::XIndexAccess
> mxSlides
;
64 Reference
<XInterface
> mxDocument
;
65 sal_Int32 mnFirstVisibleSlideIndex
;
66 sal_Int32 mnLastVisibleSlideIndex
;
67 typedef ::std::vector
<css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
> > ListenerContainer
;
68 ListenerContainer maListeners
;
70 void CallListeners (const sal_Int32 nSlideIndex
);
73 //===== PresenterPreviewCache =================================================
75 PresenterPreviewCache::PresenterPreviewCache (const Reference
<XComponentContext
>& rxContext
)
76 : PresenterPreviewCacheInterfaceBase(m_aMutex
),
77 maPreviewSize(Size(200,200)),
78 mpCacheContext(new PresenterCacheContext()),
79 mpCache(new PageCache(maPreviewSize
, Bitmap::HasFastScale(), mpCacheContext
))
84 PresenterPreviewCache::~PresenterPreviewCache()
88 //----- XInitialize -----------------------------------------------------------
90 void SAL_CALL
PresenterPreviewCache::initialize (const Sequence
<Any
>& rArguments
)
91 throw(Exception
, RuntimeException
, std::exception
)
93 if (rArguments
.getLength() != 0)
94 throw RuntimeException();
97 //----- XSlidePreviewCache ----------------------------------------------------
99 void SAL_CALL
PresenterPreviewCache::setDocumentSlides (
100 const Reference
<container::XIndexAccess
>& rxSlides
,
101 const Reference
<XInterface
>& rxDocument
)
102 throw (RuntimeException
, std::exception
)
105 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
107 mpCacheContext
->SetDocumentSlides(rxSlides
, rxDocument
);
110 void SAL_CALL
PresenterPreviewCache::setVisibleRange (
111 sal_Int32 nFirstVisibleSlideIndex
,
112 sal_Int32 nLastVisibleSlideIndex
)
113 throw (css::uno::RuntimeException
, std::exception
)
116 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
118 mpCacheContext
->SetVisibleSlideRange (nFirstVisibleSlideIndex
, nLastVisibleSlideIndex
);
121 void SAL_CALL
PresenterPreviewCache::setPreviewSize (
122 const css::geometry::IntegerSize2D
& rSize
)
123 throw (css::uno::RuntimeException
, std::exception
)
126 OSL_ASSERT(mpCache
.get()!=NULL
);
128 maPreviewSize
= Size(rSize
.Width
, rSize
.Height
);
129 mpCache
->ChangeSize(maPreviewSize
, Bitmap::HasFastScale());
132 Reference
<rendering::XBitmap
> SAL_CALL
PresenterPreviewCache::getSlidePreview (
133 sal_Int32 nSlideIndex
,
134 const Reference
<rendering::XCanvas
>& rxCanvas
)
135 throw (css::uno::RuntimeException
, std::exception
)
138 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
140 cppcanvas::CanvasSharedPtr
pCanvas (
141 cppcanvas::VCLFactory::createCanvas(rxCanvas
));
143 const SdrPage
* pPage
= mpCacheContext
->GetPage(nSlideIndex
);
145 throw RuntimeException();
147 const BitmapEx
aPreview (mpCache
->GetPreviewBitmap(pPage
,true));
148 if (aPreview
.IsEmpty())
151 return cppcanvas::VCLFactory::createBitmap(
153 aPreview
)->getUNOBitmap();
156 void SAL_CALL
PresenterPreviewCache::addPreviewCreationNotifyListener (
157 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
158 throw (css::uno::RuntimeException
, std::exception
)
160 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
163 mpCacheContext
->AddPreviewCreationNotifyListener(rxListener
);
166 void SAL_CALL
PresenterPreviewCache::removePreviewCreationNotifyListener (
167 const css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
>& rxListener
)
168 throw (css::uno::RuntimeException
, std::exception
)
171 mpCacheContext
->RemovePreviewCreationNotifyListener(rxListener
);
174 void SAL_CALL
PresenterPreviewCache::pause()
175 throw (css::uno::RuntimeException
, std::exception
)
178 OSL_ASSERT(mpCache
.get()!=NULL
);
182 void SAL_CALL
PresenterPreviewCache::resume()
183 throw (css::uno::RuntimeException
, std::exception
)
186 OSL_ASSERT(mpCache
.get()!=NULL
);
190 void PresenterPreviewCache::ThrowIfDisposed()
191 throw (::com::sun::star::lang::DisposedException
)
193 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
195 throw lang::DisposedException ("PresenterPreviewCache object has already been disposed",
196 static_cast<uno::XWeak
*>(this));
200 //===== PresenterPreviewCache::PresenterCacheContext ==========================
202 PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext()
205 mnFirstVisibleSlideIndex(-1),
206 mnLastVisibleSlideIndex(-1),
211 PresenterPreviewCache::PresenterCacheContext::~PresenterCacheContext()
215 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
216 const Reference
<container::XIndexAccess
>& rxSlides
,
217 const Reference
<XInterface
>& rxDocument
)
220 mxDocument
= rxDocument
;
221 mnFirstVisibleSlideIndex
= -1;
222 mnLastVisibleSlideIndex
= -1;
225 void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
226 const sal_Int32 nFirstVisibleSlideIndex
,
227 const sal_Int32 nLastVisibleSlideIndex
)
229 if (nFirstVisibleSlideIndex
> nLastVisibleSlideIndex
|| nFirstVisibleSlideIndex
<0)
231 mnFirstVisibleSlideIndex
= -1;
232 mnLastVisibleSlideIndex
= -1;
236 mnFirstVisibleSlideIndex
= nFirstVisibleSlideIndex
;
237 mnLastVisibleSlideIndex
= nLastVisibleSlideIndex
;
239 if (mxSlides
.is() && mnLastVisibleSlideIndex
>= mxSlides
->getCount())
240 mnLastVisibleSlideIndex
= mxSlides
->getCount() - 1;
243 void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
244 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
246 maListeners
.push_back(rxListener
);
249 void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
250 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
252 ListenerContainer::iterator iListener
;
253 for (iListener
=maListeners
.begin(); iListener
!=maListeners
.end(); ++iListener
)
254 if (*iListener
== rxListener
)
256 maListeners
.erase(iListener
);
261 //----- CacheContext ----------------------------------------------------------
263 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
265 const Bitmap
& rPreview
)
269 if ( ! mxSlides
.is())
271 const sal_Int32
nCount(mxSlides
->getCount());
272 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
273 if (aKey
== GetPage(nIndex
))
274 CallListeners(nIndex
);
277 bool PresenterPreviewCache::PresenterCacheContext::IsIdle()
282 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey
)
284 if (mnFirstVisibleSlideIndex
< 0)
286 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
288 const SdrPage
* pPage
= GetPage(nIndex
);
289 if (pPage
== static_cast<const SdrPage
*>(aKey
))
295 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey
)
297 return static_cast<const SdrPage
*>(aKey
);
300 ::boost::shared_ptr
<std::vector
<CacheKey
> >
301 PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible
)
303 ::boost::shared_ptr
<std::vector
<CacheKey
> > pKeys (new std::vector
<CacheKey
>());
305 if ( ! mxSlides
.is())
308 const sal_Int32
nFirstIndex (bVisible
? mnFirstVisibleSlideIndex
: 0);
309 const sal_Int32
nLastIndex (bVisible
? mnLastVisibleSlideIndex
: mxSlides
->getCount()-1);
314 for (sal_Int32 nIndex
=nFirstIndex
; nIndex
<=nLastIndex
; ++nIndex
)
316 pKeys
->push_back(GetPage(nIndex
));
322 sal_Int32
PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey
)
324 if ( ! mxSlides
.is())
327 const sal_Int32
nCount (mxSlides
->getCount());
329 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
330 if (aKey
== GetPage(nIndex
))
331 return -nCount
-1+nIndex
;
333 for (sal_Int32 nIndex
=0; nIndex
<=nCount
; ++nIndex
)
334 if (aKey
== GetPage(nIndex
))
340 Reference
<XInterface
> PresenterPreviewCache::PresenterCacheContext::GetModel()
345 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (
346 const sal_Int32 nSlideIndex
) const
348 if ( ! mxSlides
.is())
350 if (nSlideIndex
< 0 || nSlideIndex
>= mxSlides
->getCount())
353 Reference
<drawing::XDrawPage
> xSlide (mxSlides
->getByIndex(nSlideIndex
), UNO_QUERY
);
354 const SdPage
* pPage
= SdPage::getImplementation(xSlide
);
355 return dynamic_cast<const SdrPage
*>(pPage
);
358 void PresenterPreviewCache::PresenterCacheContext::CallListeners (
359 const sal_Int32 nIndex
)
361 ListenerContainer
aListeners (maListeners
);
362 ListenerContainer::const_iterator iListener
;
363 for (iListener
=aListeners
.begin(); iListener
!=aListeners
.end(); ++iListener
)
367 (*iListener
)->notifyPreviewCreation(nIndex
);
369 catch (lang::DisposedException
&)
371 RemovePreviewCreationNotifyListener(*iListener
);
376 } } // end of namespace ::sd::presenter
379 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
380 com_sun_star_comp_Draw_PresenterPreviewCache_get_implementation(::com::sun::star::uno::XComponentContext
* context
,
381 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
383 return cppu::acquire(new sd::presenter::PresenterPreviewCache(context
));
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */