Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "PresenterPreviewCache.hxx"
22 :
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>
28 : #include <com/sun/star/rendering/XBitmapCanvas.hpp>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::sd::slidesorter::cache;
33 :
34 : namespace sd { namespace presenter {
35 :
36 :
37 : class PresenterPreviewCache::PresenterCacheContext : public CacheContext
38 : {
39 : public:
40 : PresenterCacheContext (void);
41 : virtual ~PresenterCacheContext (void);
42 :
43 : void SetDocumentSlides (
44 : const Reference<container::XIndexAccess>& rxSlides,
45 : const Reference<XInterface>& rxDocument);
46 : void SetVisibleSlideRange (
47 : const sal_Int32 nFirstVisibleSlideIndex,
48 : const sal_Int32 nLastVisibleSlideIndex);
49 : const SdrPage* GetPage (const sal_Int32 nSlideIndex) const;
50 : void AddPreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
51 : void RemovePreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
52 :
53 : // CacheContext
54 : virtual void NotifyPreviewCreation (
55 : CacheKey aKey,
56 : const Bitmap& rPreview);
57 : virtual bool IsIdle (void);
58 : virtual bool IsVisible (CacheKey aKey);
59 : virtual const SdrPage* GetPage (CacheKey aKey);
60 : virtual ::boost::shared_ptr<std::vector<CacheKey> > GetEntryList (bool bVisible);
61 : virtual sal_Int32 GetPriority (CacheKey aKey);
62 : virtual ::com::sun::star::uno::Reference<com::sun::star::uno::XInterface> GetModel (void);
63 :
64 : private:
65 : Reference<container::XIndexAccess> mxSlides;
66 : Reference<XInterface> mxDocument;
67 : sal_Int32 mnFirstVisibleSlideIndex;
68 : sal_Int32 mnLastVisibleSlideIndex;
69 : typedef ::std::vector<css::uno::Reference<css::drawing::XSlidePreviewCacheListener> > ListenerContainer;
70 : ListenerContainer maListeners;
71 :
72 : void CallListeners (const sal_Int32 nSlideIndex);
73 : };
74 :
75 :
76 :
77 :
78 : //===== Service ===============================================================
79 :
80 0 : Reference<XInterface> SAL_CALL PresenterPreviewCache_createInstance (
81 : const Reference<XComponentContext>& rxContext)
82 : {
83 0 : return Reference<XInterface>(static_cast<XWeak*>(new PresenterPreviewCache(rxContext)));
84 : }
85 :
86 :
87 :
88 :
89 10 : OUString PresenterPreviewCache_getImplementationName (void) throw(RuntimeException)
90 : {
91 10 : return OUString("com.sun.star.comp.Draw.PresenterPreviewCache");
92 : }
93 :
94 :
95 :
96 :
97 0 : Sequence<OUString> SAL_CALL PresenterPreviewCache_getSupportedServiceNames (void)
98 : throw (RuntimeException)
99 : {
100 0 : static const OUString sServiceName("com.sun.star.drawing.PresenterPreviewCache");
101 0 : return Sequence<OUString>(&sServiceName, 1);
102 : }
103 :
104 :
105 :
106 :
107 : //===== PresenterPreviewCache =================================================
108 :
109 0 : PresenterPreviewCache::PresenterPreviewCache (const Reference<XComponentContext>& rxContext)
110 : : PresenterPreviewCacheInterfaceBase(m_aMutex),
111 : maPreviewSize(Size(200,200)),
112 0 : mpCacheContext(new PresenterCacheContext()),
113 0 : mpCache(new PageCache(maPreviewSize, false, mpCacheContext))
114 : {
115 : (void)rxContext;
116 0 : }
117 :
118 :
119 :
120 :
121 0 : PresenterPreviewCache::~PresenterPreviewCache (void)
122 : {
123 0 : }
124 :
125 :
126 :
127 :
128 : //----- XInitialize -----------------------------------------------------------
129 :
130 0 : void SAL_CALL PresenterPreviewCache::initialize (const Sequence<Any>& rArguments)
131 : throw(Exception, RuntimeException)
132 : {
133 0 : if (rArguments.getLength() != 0)
134 0 : throw RuntimeException();
135 0 : }
136 :
137 :
138 :
139 :
140 : //----- XSlidePreviewCache ----------------------------------------------------
141 :
142 0 : void SAL_CALL PresenterPreviewCache::setDocumentSlides (
143 : const Reference<container::XIndexAccess>& rxSlides,
144 : const Reference<XInterface>& rxDocument)
145 : throw (RuntimeException)
146 : {
147 0 : ThrowIfDisposed();
148 : OSL_ASSERT(mpCacheContext.get()!=NULL);
149 :
150 0 : mpCacheContext->SetDocumentSlides(rxSlides, rxDocument);
151 0 : }
152 :
153 :
154 :
155 :
156 0 : void SAL_CALL PresenterPreviewCache::setVisibleRange (
157 : sal_Int32 nFirstVisibleSlideIndex,
158 : sal_Int32 nLastVisibleSlideIndex)
159 : throw (css::uno::RuntimeException)
160 : {
161 0 : ThrowIfDisposed();
162 : OSL_ASSERT(mpCacheContext.get()!=NULL);
163 :
164 0 : mpCacheContext->SetVisibleSlideRange (nFirstVisibleSlideIndex, nLastVisibleSlideIndex);
165 0 : }
166 :
167 :
168 :
169 :
170 0 : void SAL_CALL PresenterPreviewCache::setPreviewSize (
171 : const css::geometry::IntegerSize2D& rSize)
172 : throw (css::uno::RuntimeException)
173 : {
174 0 : ThrowIfDisposed();
175 : OSL_ASSERT(mpCache.get()!=NULL);
176 :
177 0 : maPreviewSize = Size(rSize.Width, rSize.Height);
178 0 : mpCache->ChangeSize(maPreviewSize, false);
179 0 : }
180 :
181 :
182 :
183 :
184 0 : Reference<rendering::XBitmap> SAL_CALL PresenterPreviewCache::getSlidePreview (
185 : sal_Int32 nSlideIndex,
186 : const Reference<rendering::XCanvas>& rxCanvas)
187 : throw (css::uno::RuntimeException)
188 : {
189 0 : ThrowIfDisposed();
190 : OSL_ASSERT(mpCacheContext.get()!=NULL);
191 :
192 : cppcanvas::BitmapCanvasSharedPtr pCanvas (
193 0 : cppcanvas::VCLFactory::getInstance().createCanvas(
194 0 : Reference<rendering::XBitmapCanvas>(rxCanvas, UNO_QUERY)));
195 :
196 0 : const SdrPage* pPage = mpCacheContext->GetPage(nSlideIndex);
197 0 : if (pPage == NULL)
198 0 : throw RuntimeException();
199 :
200 0 : const BitmapEx aPreview (mpCache->GetPreviewBitmap(pPage,true));
201 0 : if (aPreview.IsEmpty())
202 0 : return NULL;
203 : else
204 0 : return cppcanvas::VCLFactory::getInstance().createBitmap(
205 : pCanvas,
206 0 : aPreview)->getUNOBitmap();
207 : }
208 :
209 :
210 :
211 :
212 0 : void SAL_CALL PresenterPreviewCache::addPreviewCreationNotifyListener (
213 : const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
214 : throw (css::uno::RuntimeException)
215 : {
216 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
217 0 : return;
218 0 : if (rxListener.is())
219 0 : mpCacheContext->AddPreviewCreationNotifyListener(rxListener);
220 : }
221 :
222 :
223 :
224 :
225 0 : void SAL_CALL PresenterPreviewCache::removePreviewCreationNotifyListener (
226 : const css::uno::Reference<css::drawing::XSlidePreviewCacheListener>& rxListener)
227 : throw (css::uno::RuntimeException)
228 : {
229 0 : ThrowIfDisposed();
230 0 : mpCacheContext->RemovePreviewCreationNotifyListener(rxListener);
231 0 : }
232 :
233 :
234 :
235 :
236 0 : void SAL_CALL PresenterPreviewCache::pause (void)
237 : throw (css::uno::RuntimeException)
238 : {
239 0 : ThrowIfDisposed();
240 : OSL_ASSERT(mpCache.get()!=NULL);
241 0 : mpCache->Pause();
242 0 : }
243 :
244 :
245 :
246 :
247 0 : void SAL_CALL PresenterPreviewCache::resume (void)
248 : throw (css::uno::RuntimeException)
249 : {
250 0 : ThrowIfDisposed();
251 : OSL_ASSERT(mpCache.get()!=NULL);
252 0 : mpCache->Resume();
253 0 : }
254 :
255 :
256 :
257 :
258 : //-----------------------------------------------------------------------------
259 :
260 0 : void PresenterPreviewCache::ThrowIfDisposed (void)
261 : throw (::com::sun::star::lang::DisposedException)
262 : {
263 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
264 : {
265 : throw lang::DisposedException ("PresenterPreviewCache object has already been disposed",
266 0 : static_cast<uno::XWeak*>(this));
267 : }
268 0 : }
269 :
270 :
271 :
272 :
273 : //===== PresenterPreviewCache::PresenterCacheContext ==========================
274 :
275 :
276 0 : PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext (void)
277 : : mxSlides(),
278 : mxDocument(),
279 : mnFirstVisibleSlideIndex(-1),
280 : mnLastVisibleSlideIndex(-1),
281 0 : maListeners()
282 : {
283 0 : }
284 :
285 :
286 :
287 :
288 0 : PresenterPreviewCache::PresenterCacheContext::~PresenterCacheContext (void)
289 : {
290 0 : }
291 :
292 :
293 :
294 :
295 0 : void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
296 : const Reference<container::XIndexAccess>& rxSlides,
297 : const Reference<XInterface>& rxDocument)
298 : {
299 0 : mxSlides = rxSlides;
300 0 : mxDocument = rxDocument;
301 0 : mnFirstVisibleSlideIndex = -1;
302 0 : mnLastVisibleSlideIndex = -1;
303 0 : }
304 :
305 :
306 :
307 :
308 0 : void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
309 : const sal_Int32 nFirstVisibleSlideIndex,
310 : const sal_Int32 nLastVisibleSlideIndex)
311 : {
312 0 : if (nFirstVisibleSlideIndex > nLastVisibleSlideIndex || nFirstVisibleSlideIndex<0)
313 : {
314 0 : mnFirstVisibleSlideIndex = -1;
315 0 : mnLastVisibleSlideIndex = -1;
316 : }
317 : else
318 : {
319 0 : mnFirstVisibleSlideIndex = nFirstVisibleSlideIndex;
320 0 : mnLastVisibleSlideIndex = nLastVisibleSlideIndex;
321 : }
322 0 : if (mxSlides.is() && mnLastVisibleSlideIndex >= mxSlides->getCount())
323 0 : mnLastVisibleSlideIndex = mxSlides->getCount() - 1;
324 0 : }
325 :
326 :
327 :
328 :
329 0 : void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
330 : const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
331 : {
332 0 : maListeners.push_back(rxListener);
333 0 : }
334 :
335 :
336 :
337 :
338 0 : void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
339 : const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
340 : {
341 0 : ListenerContainer::iterator iListener;
342 0 : for (iListener=maListeners.begin(); iListener!=maListeners.end(); ++iListener)
343 0 : if (*iListener == rxListener)
344 : {
345 0 : maListeners.erase(iListener);
346 0 : return;
347 : }
348 : }
349 :
350 :
351 :
352 :
353 : //----- CacheContext ----------------------------------------------------------
354 :
355 0 : void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
356 : CacheKey aKey,
357 : const Bitmap& rPreview)
358 : {
359 : (void)rPreview;
360 :
361 0 : if ( ! mxSlides.is())
362 0 : return;
363 0 : const sal_Int32 nCount(mxSlides->getCount());
364 0 : for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
365 0 : if (aKey == GetPage(nIndex))
366 0 : CallListeners(nIndex);
367 : }
368 :
369 :
370 :
371 :
372 0 : bool PresenterPreviewCache::PresenterCacheContext::IsIdle (void)
373 : {
374 0 : return true;
375 : }
376 :
377 :
378 :
379 :
380 0 : bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey)
381 : {
382 0 : if (mnFirstVisibleSlideIndex < 0)
383 0 : return false;
384 0 : for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
385 : {
386 0 : const SdrPage* pPage = GetPage(nIndex);
387 0 : if (pPage == static_cast<const SdrPage*>(aKey))
388 0 : return true;
389 : }
390 0 : return false;
391 : }
392 :
393 :
394 :
395 :
396 0 : const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey)
397 : {
398 0 : return static_cast<const SdrPage*>(aKey);
399 : }
400 :
401 :
402 :
403 :
404 : ::boost::shared_ptr<std::vector<CacheKey> >
405 0 : PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible)
406 : {
407 0 : ::boost::shared_ptr<std::vector<CacheKey> > pKeys (new std::vector<CacheKey>());
408 :
409 0 : if ( ! mxSlides.is())
410 0 : return pKeys;
411 :
412 0 : const sal_Int32 nFirstIndex (bVisible ? mnFirstVisibleSlideIndex : 0);
413 0 : const sal_Int32 nLastIndex (bVisible ? mnLastVisibleSlideIndex : mxSlides->getCount()-1);
414 :
415 0 : if (nFirstIndex < 0)
416 0 : return pKeys;
417 :
418 0 : for (sal_Int32 nIndex=nFirstIndex; nIndex<=nLastIndex; ++nIndex)
419 : {
420 0 : pKeys->push_back(GetPage(nIndex));
421 : }
422 :
423 0 : return pKeys;
424 : }
425 :
426 :
427 :
428 :
429 0 : sal_Int32 PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey)
430 : {
431 0 : if ( ! mxSlides.is())
432 0 : return 0;
433 :
434 0 : const sal_Int32 nCount (mxSlides->getCount());
435 :
436 0 : for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
437 0 : if (aKey == GetPage(nIndex))
438 0 : return -nCount-1+nIndex;
439 :
440 0 : for (sal_Int32 nIndex=0; nIndex<=nCount; ++nIndex)
441 0 : if (aKey == GetPage(nIndex))
442 0 : return nIndex;
443 :
444 0 : return 0;
445 : }
446 :
447 :
448 :
449 :
450 0 : Reference<XInterface> PresenterPreviewCache::PresenterCacheContext::GetModel (void)
451 : {
452 0 : return mxDocument;
453 : }
454 :
455 :
456 :
457 :
458 : //-----------------------------------------------------------------------------
459 :
460 0 : const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (
461 : const sal_Int32 nSlideIndex) const
462 : {
463 0 : if ( ! mxSlides.is())
464 0 : return NULL;
465 0 : if (nSlideIndex < 0 || nSlideIndex >= mxSlides->getCount())
466 0 : return NULL;
467 :
468 0 : Reference<drawing::XDrawPage> xSlide (mxSlides->getByIndex(nSlideIndex), UNO_QUERY);
469 0 : const SdPage* pPage = SdPage::getImplementation(xSlide);
470 0 : return dynamic_cast<const SdrPage*>(pPage);
471 : }
472 :
473 :
474 :
475 :
476 0 : void PresenterPreviewCache::PresenterCacheContext::CallListeners (
477 : const sal_Int32 nIndex)
478 : {
479 0 : ListenerContainer aListeners (maListeners);
480 0 : ListenerContainer::const_iterator iListener;
481 0 : for (iListener=aListeners.begin(); iListener!=aListeners.end(); ++iListener)
482 : {
483 : try
484 : {
485 0 : (*iListener)->notifyPreviewCreation(nIndex);
486 : }
487 0 : catch (lang::DisposedException&)
488 : {
489 0 : RemovePreviewCreationNotifyListener(*iListener);
490 : }
491 0 : }
492 0 : }
493 :
494 33 : } } // end of namespace ::sd::presenter
495 :
496 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|