LCOV - code coverage report
Current view: top level - sd/source/ui/presenter - PresenterPreviewCache.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 3 152 2.0 %
Date: 2014-11-03 Functions: 3 34 8.8 %
Legend: Lines: hit not hit

          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             : #include "PresenterPreviewCache.hxx"
      21             : #include "facreg.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             : 
      29             : using namespace ::com::sun::star;
      30             : using namespace ::com::sun::star::uno;
      31             : using namespace ::sd::slidesorter::cache;
      32             : 
      33             : namespace sd { namespace presenter {
      34             : 
      35             : class PresenterPreviewCache::PresenterCacheContext : public CacheContext
      36             : {
      37             : public:
      38             :     PresenterCacheContext (void);
      39             :     virtual ~PresenterCacheContext (void);
      40             : 
      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);
      50             : 
      51             :     // CacheContext
      52             :     virtual void NotifyPreviewCreation (
      53             :         CacheKey aKey,
      54             :         const Bitmap& rPreview) SAL_OVERRIDE;
      55             :     virtual bool IsIdle (void) 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 (void) SAL_OVERRIDE;
      61             : 
      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;
      69             : 
      70             :     void CallListeners (const sal_Int32 nSlideIndex);
      71             : };
      72             : 
      73             : //===== Service ===============================================================
      74             : 
      75           0 : Reference<XInterface> SAL_CALL PresenterPreviewCache_createInstance (
      76             :     const Reference<XComponentContext>& rxContext) throw (css::uno::Exception)
      77             : {
      78           0 :     return Reference<XInterface>(static_cast<XWeak*>(new PresenterPreviewCache(rxContext)));
      79             : }
      80             : 
      81          34 : OUString PresenterPreviewCache_getImplementationName (void) throw(RuntimeException)
      82             : {
      83          34 :     return OUString("com.sun.star.comp.Draw.PresenterPreviewCache");
      84             : }
      85             : 
      86           0 : Sequence<OUString> SAL_CALL PresenterPreviewCache_getSupportedServiceNames (void)
      87             :     throw (RuntimeException)
      88             : {
      89           0 :     static const OUString sServiceName("com.sun.star.drawing.PresenterPreviewCache");
      90           0 :     return Sequence<OUString>(&sServiceName, 1);
      91             : }
      92             : 
      93             : //===== PresenterPreviewCache =================================================
      94             : 
      95           0 : PresenterPreviewCache::PresenterPreviewCache (const Reference<XComponentContext>& rxContext)
      96             :     : PresenterPreviewCacheInterfaceBase(m_aMutex),
      97             :       maPreviewSize(Size(200,200)),
      98           0 :       mpCacheContext(new PresenterCacheContext()),
      99           0 :       mpCache(new PageCache(maPreviewSize, false, mpCacheContext))
     100             : {
     101             :     (void)rxContext;
     102           0 : }
     103             : 
     104           0 : PresenterPreviewCache::~PresenterPreviewCache (void)
     105             : {
     106           0 : }
     107             : 
     108             : //----- XInitialize -----------------------------------------------------------
     109             : 
     110           0 : void SAL_CALL PresenterPreviewCache::initialize (const Sequence<Any>& rArguments)
     111             :     throw(Exception, RuntimeException, std::exception)
     112             : {
     113           0 :     if (rArguments.getLength() != 0)
     114           0 :         throw RuntimeException();
     115           0 : }
     116             : 
     117             : //----- XSlidePreviewCache ----------------------------------------------------
     118             : 
     119           0 : void SAL_CALL PresenterPreviewCache::setDocumentSlides (
     120             :     const Reference<container::XIndexAccess>& rxSlides,
     121             :     const Reference<XInterface>& rxDocument)
     122             :     throw (RuntimeException, std::exception)
     123             : {
     124           0 :     ThrowIfDisposed();
     125             :     OSL_ASSERT(mpCacheContext.get()!=NULL);
     126             : 
     127           0 :     mpCacheContext->SetDocumentSlides(rxSlides, rxDocument);
     128           0 : }
     129             : 
     130           0 : void SAL_CALL PresenterPreviewCache::setVisibleRange (
     131             :     sal_Int32 nFirstVisibleSlideIndex,
     132             :     sal_Int32 nLastVisibleSlideIndex)
     133             :     throw (css::uno::RuntimeException, std::exception)
     134             : {
     135           0 :     ThrowIfDisposed();
     136             :     OSL_ASSERT(mpCacheContext.get()!=NULL);
     137             : 
     138           0 :     mpCacheContext->SetVisibleSlideRange (nFirstVisibleSlideIndex, nLastVisibleSlideIndex);
     139           0 : }
     140             : 
     141           0 : void SAL_CALL PresenterPreviewCache::setPreviewSize (
     142             :     const css::geometry::IntegerSize2D& rSize)
     143             :     throw (css::uno::RuntimeException, std::exception)
     144             : {
     145           0 :     ThrowIfDisposed();
     146             :     OSL_ASSERT(mpCache.get()!=NULL);
     147             : 
     148           0 :     maPreviewSize = Size(rSize.Width, rSize.Height);
     149           0 :     mpCache->ChangeSize(maPreviewSize, false);
     150           0 : }
     151             : 
     152           0 : Reference<rendering::XBitmap> SAL_CALL PresenterPreviewCache::getSlidePreview (
     153             :     sal_Int32 nSlideIndex,
     154             :     const Reference<rendering::XCanvas>& rxCanvas)
     155             :     throw (css::uno::RuntimeException, std::exception)
     156             : {
     157           0 :     ThrowIfDisposed();
     158             :     OSL_ASSERT(mpCacheContext.get()!=NULL);
     159             : 
     160             :     cppcanvas::CanvasSharedPtr pCanvas (
     161           0 :         cppcanvas::VCLFactory::getInstance().createCanvas(rxCanvas));
     162             : 
     163           0 :     const SdrPage* pPage = mpCacheContext->GetPage(nSlideIndex);
     164           0 :     if (pPage == NULL)
     165           0 :         throw RuntimeException();
     166             : 
     167           0 :     const BitmapEx aPreview (mpCache->GetPreviewBitmap(pPage,true));
     168           0 :     if (aPreview.IsEmpty())
     169           0 :         return NULL;
     170             :     else
     171           0 :         return cppcanvas::VCLFactory::getInstance().createBitmap(
     172             :             pCanvas,
     173           0 :             aPreview)->getUNOBitmap();
     174             : }
     175             : 
     176           0 : void SAL_CALL PresenterPreviewCache::addPreviewCreationNotifyListener (
     177             :     const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
     178             :     throw (css::uno::RuntimeException, std::exception)
     179             : {
     180           0 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
     181           0 :         return;
     182           0 :     if (rxListener.is())
     183           0 :         mpCacheContext->AddPreviewCreationNotifyListener(rxListener);
     184             : }
     185             : 
     186           0 : void SAL_CALL PresenterPreviewCache::removePreviewCreationNotifyListener (
     187             :     const css::uno::Reference<css::drawing::XSlidePreviewCacheListener>& rxListener)
     188             :     throw (css::uno::RuntimeException, std::exception)
     189             : {
     190           0 :     ThrowIfDisposed();
     191           0 :     mpCacheContext->RemovePreviewCreationNotifyListener(rxListener);
     192           0 : }
     193             : 
     194           0 : void SAL_CALL PresenterPreviewCache::pause (void)
     195             :     throw (css::uno::RuntimeException, std::exception)
     196             : {
     197           0 :     ThrowIfDisposed();
     198             :     OSL_ASSERT(mpCache.get()!=NULL);
     199           0 :     mpCache->Pause();
     200           0 : }
     201             : 
     202           0 : void SAL_CALL PresenterPreviewCache::resume (void)
     203             :     throw (css::uno::RuntimeException, std::exception)
     204             : {
     205           0 :     ThrowIfDisposed();
     206             :     OSL_ASSERT(mpCache.get()!=NULL);
     207           0 :     mpCache->Resume();
     208           0 : }
     209             : 
     210           0 : void PresenterPreviewCache::ThrowIfDisposed (void)
     211             :     throw (::com::sun::star::lang::DisposedException)
     212             : {
     213           0 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
     214             :     {
     215             :         throw lang::DisposedException ("PresenterPreviewCache object has already been disposed",
     216           0 :             static_cast<uno::XWeak*>(this));
     217             :     }
     218           0 : }
     219             : 
     220             : //===== PresenterPreviewCache::PresenterCacheContext ==========================
     221             : 
     222           0 : PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext (void)
     223             :     : mxSlides(),
     224             :       mxDocument(),
     225             :       mnFirstVisibleSlideIndex(-1),
     226             :       mnLastVisibleSlideIndex(-1),
     227           0 :       maListeners()
     228             : {
     229           0 : }
     230             : 
     231           0 : PresenterPreviewCache::PresenterCacheContext::~PresenterCacheContext (void)
     232             : {
     233           0 : }
     234             : 
     235           0 : void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
     236             :     const Reference<container::XIndexAccess>& rxSlides,
     237             :     const Reference<XInterface>& rxDocument)
     238             : {
     239           0 :     mxSlides = rxSlides;
     240           0 :     mxDocument = rxDocument;
     241           0 :     mnFirstVisibleSlideIndex = -1;
     242           0 :     mnLastVisibleSlideIndex = -1;
     243           0 : }
     244             : 
     245           0 : void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
     246             :     const sal_Int32 nFirstVisibleSlideIndex,
     247             :     const sal_Int32 nLastVisibleSlideIndex)
     248             : {
     249           0 :     if (nFirstVisibleSlideIndex > nLastVisibleSlideIndex || nFirstVisibleSlideIndex<0)
     250             :     {
     251           0 :         mnFirstVisibleSlideIndex = -1;
     252           0 :         mnLastVisibleSlideIndex = -1;
     253             :     }
     254             :     else
     255             :     {
     256           0 :         mnFirstVisibleSlideIndex = nFirstVisibleSlideIndex;
     257           0 :         mnLastVisibleSlideIndex = nLastVisibleSlideIndex;
     258             :     }
     259           0 :     if (mxSlides.is() && mnLastVisibleSlideIndex >= mxSlides->getCount())
     260           0 :         mnLastVisibleSlideIndex = mxSlides->getCount() - 1;
     261           0 : }
     262             : 
     263           0 : void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
     264             :     const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
     265             : {
     266           0 :     maListeners.push_back(rxListener);
     267           0 : }
     268             : 
     269           0 : void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
     270             :     const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
     271             : {
     272           0 :     ListenerContainer::iterator iListener;
     273           0 :     for (iListener=maListeners.begin(); iListener!=maListeners.end(); ++iListener)
     274           0 :         if (*iListener == rxListener)
     275             :         {
     276           0 :             maListeners.erase(iListener);
     277           0 :             return;
     278             :         }
     279             : }
     280             : 
     281             : //----- CacheContext ----------------------------------------------------------
     282             : 
     283           0 : void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
     284             :     CacheKey aKey,
     285             :     const Bitmap& rPreview)
     286             : {
     287             :     (void)rPreview;
     288             : 
     289           0 :     if ( ! mxSlides.is())
     290           0 :         return;
     291           0 :     const sal_Int32 nCount(mxSlides->getCount());
     292           0 :     for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
     293           0 :         if (aKey == GetPage(nIndex))
     294           0 :             CallListeners(nIndex);
     295             : }
     296             : 
     297           0 : bool PresenterPreviewCache::PresenterCacheContext::IsIdle (void)
     298             : {
     299           0 :     return true;
     300             : }
     301             : 
     302           0 : bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey)
     303             : {
     304           0 :     if (mnFirstVisibleSlideIndex < 0)
     305           0 :         return false;
     306           0 :     for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
     307             :     {
     308           0 :         const SdrPage* pPage = GetPage(nIndex);
     309           0 :         if (pPage == static_cast<const SdrPage*>(aKey))
     310           0 :             return true;
     311             :     }
     312           0 :     return false;
     313             : }
     314             : 
     315           0 : const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey)
     316             : {
     317           0 :     return static_cast<const SdrPage*>(aKey);
     318             : }
     319             : 
     320             : ::boost::shared_ptr<std::vector<CacheKey> >
     321           0 :     PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible)
     322             : {
     323           0 :     ::boost::shared_ptr<std::vector<CacheKey> > pKeys (new std::vector<CacheKey>());
     324             : 
     325           0 :     if ( ! mxSlides.is())
     326           0 :         return pKeys;
     327             : 
     328           0 :     const sal_Int32 nFirstIndex (bVisible ? mnFirstVisibleSlideIndex : 0);
     329           0 :     const sal_Int32 nLastIndex (bVisible ? mnLastVisibleSlideIndex : mxSlides->getCount()-1);
     330             : 
     331           0 :     if (nFirstIndex < 0)
     332           0 :         return pKeys;
     333             : 
     334           0 :     for (sal_Int32 nIndex=nFirstIndex; nIndex<=nLastIndex; ++nIndex)
     335             :     {
     336           0 :         pKeys->push_back(GetPage(nIndex));
     337             :     }
     338             : 
     339           0 :     return pKeys;
     340             : }
     341             : 
     342           0 : sal_Int32 PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey)
     343             : {
     344           0 :     if ( ! mxSlides.is())
     345           0 :         return 0;
     346             : 
     347           0 :     const sal_Int32 nCount (mxSlides->getCount());
     348             : 
     349           0 :     for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
     350           0 :         if (aKey == GetPage(nIndex))
     351           0 :             return -nCount-1+nIndex;
     352             : 
     353           0 :     for (sal_Int32 nIndex=0; nIndex<=nCount; ++nIndex)
     354           0 :         if (aKey == GetPage(nIndex))
     355           0 :             return nIndex;
     356             : 
     357           0 :     return 0;
     358             : }
     359             : 
     360           0 : Reference<XInterface> PresenterPreviewCache::PresenterCacheContext::GetModel (void)
     361             : {
     362           0 :     return mxDocument;
     363             : }
     364             : 
     365           0 : const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (
     366             :     const sal_Int32 nSlideIndex) const
     367             : {
     368           0 :     if ( ! mxSlides.is())
     369           0 :         return NULL;
     370           0 :     if (nSlideIndex < 0 || nSlideIndex >= mxSlides->getCount())
     371           0 :         return NULL;
     372             : 
     373           0 :     Reference<drawing::XDrawPage> xSlide (mxSlides->getByIndex(nSlideIndex), UNO_QUERY);
     374           0 :     const SdPage* pPage = SdPage::getImplementation(xSlide);
     375           0 :     return dynamic_cast<const SdrPage*>(pPage);
     376             : }
     377             : 
     378           0 : void PresenterPreviewCache::PresenterCacheContext::CallListeners (
     379             :     const sal_Int32 nIndex)
     380             : {
     381           0 :     ListenerContainer aListeners (maListeners);
     382           0 :     ListenerContainer::const_iterator iListener;
     383           0 :     for (iListener=aListeners.begin(); iListener!=aListeners.end(); ++iListener)
     384             :     {
     385             :         try
     386             :         {
     387           0 :             (*iListener)->notifyPreviewCreation(nIndex);
     388             :         }
     389           0 :         catch (lang::DisposedException&)
     390             :         {
     391           0 :             RemovePreviewCreationNotifyListener(*iListener);
     392             :         }
     393           0 :     }
     394           0 : }
     395             : 
     396         114 : } } // end of namespace ::sd::presenter
     397             : 
     398             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10