bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / presenter / PresenterPreviewCache.cxx
blob00406b0759b0b438a57575ac938e7eb81c21793d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include "facreg.hxx"
23 #include "cache/SlsCacheContext.hxx"
24 #include "tools/IdleDetection.hxx"
25 #include "sdpage.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
37 public:
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);
51 // CacheContext
52 virtual void NotifyPreviewCreation (
53 CacheKey aKey,
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;
62 private:
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))
81 (void)rxContext;
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)
104 ThrowIfDisposed();
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)
115 ThrowIfDisposed();
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)
125 ThrowIfDisposed();
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)
137 ThrowIfDisposed();
138 OSL_ASSERT(mpCacheContext.get()!=NULL);
140 cppcanvas::CanvasSharedPtr pCanvas (
141 cppcanvas::VCLFactory::createCanvas(rxCanvas));
143 const SdrPage* pPage = mpCacheContext->GetPage(nSlideIndex);
144 if (pPage == NULL)
145 throw RuntimeException();
147 const BitmapEx aPreview (mpCache->GetPreviewBitmap(pPage,true));
148 if (aPreview.IsEmpty())
149 return NULL;
150 else
151 return cppcanvas::VCLFactory::createBitmap(
152 pCanvas,
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)
161 return;
162 if (rxListener.is())
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)
170 ThrowIfDisposed();
171 mpCacheContext->RemovePreviewCreationNotifyListener(rxListener);
174 void SAL_CALL PresenterPreviewCache::pause()
175 throw (css::uno::RuntimeException, std::exception)
177 ThrowIfDisposed();
178 OSL_ASSERT(mpCache.get()!=NULL);
179 mpCache->Pause();
182 void SAL_CALL PresenterPreviewCache::resume()
183 throw (css::uno::RuntimeException, std::exception)
185 ThrowIfDisposed();
186 OSL_ASSERT(mpCache.get()!=NULL);
187 mpCache->Resume();
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()
203 : mxSlides(),
204 mxDocument(),
205 mnFirstVisibleSlideIndex(-1),
206 mnLastVisibleSlideIndex(-1),
207 maListeners()
211 PresenterPreviewCache::PresenterCacheContext::~PresenterCacheContext()
215 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
216 const Reference<container::XIndexAccess>& rxSlides,
217 const Reference<XInterface>& rxDocument)
219 mxSlides = rxSlides;
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;
234 else
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);
257 return;
261 //----- CacheContext ----------------------------------------------------------
263 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
264 CacheKey aKey,
265 const Bitmap& rPreview)
267 (void)rPreview;
269 if ( ! mxSlides.is())
270 return;
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()
279 return true;
282 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey)
284 if (mnFirstVisibleSlideIndex < 0)
285 return false;
286 for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
288 const SdrPage* pPage = GetPage(nIndex);
289 if (pPage == static_cast<const SdrPage*>(aKey))
290 return true;
292 return false;
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())
306 return pKeys;
308 const sal_Int32 nFirstIndex (bVisible ? mnFirstVisibleSlideIndex : 0);
309 const sal_Int32 nLastIndex (bVisible ? mnLastVisibleSlideIndex : mxSlides->getCount()-1);
311 if (nFirstIndex < 0)
312 return pKeys;
314 for (sal_Int32 nIndex=nFirstIndex; nIndex<=nLastIndex; ++nIndex)
316 pKeys->push_back(GetPage(nIndex));
319 return pKeys;
322 sal_Int32 PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey)
324 if ( ! mxSlides.is())
325 return 0;
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))
335 return nIndex;
337 return 0;
340 Reference<XInterface> PresenterPreviewCache::PresenterCacheContext::GetModel()
342 return mxDocument;
345 const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (
346 const sal_Int32 nSlideIndex) const
348 if ( ! mxSlides.is())
349 return NULL;
350 if (nSlideIndex < 0 || nSlideIndex >= mxSlides->getCount())
351 return NULL;
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: */