bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / presenter / PresenterPreviewCache.cxx
blob54fde4e3408bfc73b5161c9ad348d15905f86fd0
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/SlsPageCache.hxx>
24 #include <cache/SlsCacheContext.hxx>
25 #include <tools/IdleDetection.hxx>
26 #include <vcl/bitmapex.hxx>
27 #include <sdpage.hxx>
28 #include <cppcanvas/vclfactory.hxx>
29 #include <com/sun/star/drawing/XDrawPage.hpp>
31 namespace com { namespace sun { namespace star { namespace uno { class XComponentContext; } } } }
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using namespace ::sd::slidesorter::cache;
37 namespace sd { namespace presenter {
39 class PresenterPreviewCache::PresenterCacheContext : public CacheContext
41 public:
42 PresenterCacheContext();
44 void SetDocumentSlides (
45 const Reference<container::XIndexAccess>& rxSlides,
46 const Reference<XInterface>& rxDocument);
47 void SetVisibleSlideRange (
48 const sal_Int32 nFirstVisibleSlideIndex,
49 const sal_Int32 nLastVisibleSlideIndex);
50 const SdrPage* GetPage (const sal_Int32 nSlideIndex) const;
51 void AddPreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
52 void RemovePreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
54 // CacheContext
55 virtual void NotifyPreviewCreation (CacheKey aKey) override;
56 virtual bool IsIdle() override;
57 virtual bool IsVisible (CacheKey aKey) override;
58 virtual const SdrPage* GetPage (CacheKey aKey) override;
59 virtual std::shared_ptr<std::vector<CacheKey> > GetEntryList (bool bVisible) override;
60 virtual sal_Int32 GetPriority (CacheKey aKey) override;
61 virtual css::uno::Reference<css::uno::XInterface> GetModel() override;
63 private:
64 Reference<container::XIndexAccess> mxSlides;
65 Reference<XInterface> mxDocument;
66 sal_Int32 mnFirstVisibleSlideIndex;
67 sal_Int32 mnLastVisibleSlideIndex;
68 typedef ::std::vector<css::uno::Reference<css::drawing::XSlidePreviewCacheListener> > ListenerContainer;
69 ListenerContainer maListeners;
71 void CallListeners (const sal_Int32 nSlideIndex);
74 //===== PresenterPreviewCache =================================================
76 PresenterPreviewCache::PresenterPreviewCache ()
77 : PresenterPreviewCacheInterfaceBase(m_aMutex),
78 maPreviewSize(Size(200,200)),
79 mpCacheContext(new PresenterCacheContext()),
80 mpCache(new 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 //----- XSlidePreviewCache ----------------------------------------------------
98 void SAL_CALL PresenterPreviewCache::setDocumentSlides (
99 const Reference<container::XIndexAccess>& rxSlides,
100 const Reference<XInterface>& rxDocument)
102 ThrowIfDisposed();
103 OSL_ASSERT(mpCacheContext != nullptr);
105 mpCacheContext->SetDocumentSlides(rxSlides, rxDocument);
108 void SAL_CALL PresenterPreviewCache::setVisibleRange (
109 sal_Int32 nFirstVisibleSlideIndex,
110 sal_Int32 nLastVisibleSlideIndex)
112 ThrowIfDisposed();
113 OSL_ASSERT(mpCacheContext != nullptr);
115 mpCacheContext->SetVisibleSlideRange (nFirstVisibleSlideIndex, nLastVisibleSlideIndex);
118 void SAL_CALL PresenterPreviewCache::setPreviewSize (
119 const css::geometry::IntegerSize2D& rSize)
121 ThrowIfDisposed();
122 OSL_ASSERT(mpCache != nullptr);
124 maPreviewSize = Size(rSize.Width, rSize.Height);
125 mpCache->ChangeSize(maPreviewSize, Bitmap::HasFastScale());
128 Reference<rendering::XBitmap> SAL_CALL PresenterPreviewCache::getSlidePreview (
129 sal_Int32 nSlideIndex,
130 const Reference<rendering::XCanvas>& rxCanvas)
132 ThrowIfDisposed();
133 OSL_ASSERT(mpCacheContext != nullptr);
135 cppcanvas::CanvasSharedPtr pCanvas (
136 cppcanvas::VCLFactory::createCanvas(rxCanvas));
138 const SdrPage* pPage = mpCacheContext->GetPage(nSlideIndex);
139 if (pPage == nullptr)
140 throw RuntimeException();
142 const BitmapEx aPreview (mpCache->GetPreviewBitmap(pPage,true));
143 if (aPreview.IsEmpty())
144 return nullptr;
145 else
146 return cppcanvas::VCLFactory::createBitmap(
147 pCanvas,
148 aPreview)->getUNOBitmap();
151 void SAL_CALL PresenterPreviewCache::addPreviewCreationNotifyListener (
152 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
154 if (rBHelper.bDisposed || rBHelper.bInDispose)
155 return;
156 if (rxListener.is())
157 mpCacheContext->AddPreviewCreationNotifyListener(rxListener);
160 void SAL_CALL PresenterPreviewCache::removePreviewCreationNotifyListener (
161 const css::uno::Reference<css::drawing::XSlidePreviewCacheListener>& rxListener)
163 ThrowIfDisposed();
164 mpCacheContext->RemovePreviewCreationNotifyListener(rxListener);
167 void SAL_CALL PresenterPreviewCache::pause()
169 ThrowIfDisposed();
170 OSL_ASSERT(mpCache != nullptr);
171 mpCache->Pause();
174 void SAL_CALL PresenterPreviewCache::resume()
176 ThrowIfDisposed();
177 OSL_ASSERT(mpCache != nullptr);
178 mpCache->Resume();
181 void PresenterPreviewCache::ThrowIfDisposed()
183 if (rBHelper.bDisposed || rBHelper.bInDispose)
185 throw lang::DisposedException ("PresenterPreviewCache object has already been disposed",
186 static_cast<uno::XWeak*>(this));
190 //===== PresenterPreviewCache::PresenterCacheContext ==========================
192 PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext()
193 : mxSlides(),
194 mxDocument(),
195 mnFirstVisibleSlideIndex(-1),
196 mnLastVisibleSlideIndex(-1),
197 maListeners()
201 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
202 const Reference<container::XIndexAccess>& rxSlides,
203 const Reference<XInterface>& rxDocument)
205 mxSlides = rxSlides;
206 mxDocument = rxDocument;
207 mnFirstVisibleSlideIndex = -1;
208 mnLastVisibleSlideIndex = -1;
211 void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
212 const sal_Int32 nFirstVisibleSlideIndex,
213 const sal_Int32 nLastVisibleSlideIndex)
215 if (nFirstVisibleSlideIndex > nLastVisibleSlideIndex || nFirstVisibleSlideIndex<0)
217 mnFirstVisibleSlideIndex = -1;
218 mnLastVisibleSlideIndex = -1;
220 else
222 mnFirstVisibleSlideIndex = nFirstVisibleSlideIndex;
223 mnLastVisibleSlideIndex = nLastVisibleSlideIndex;
225 if (mxSlides.is() && mnLastVisibleSlideIndex >= mxSlides->getCount())
226 mnLastVisibleSlideIndex = mxSlides->getCount() - 1;
229 void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
230 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
232 maListeners.push_back(rxListener);
235 void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
236 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
238 auto iListener = std::find(maListeners.begin(), maListeners.end(), rxListener);
239 if (iListener != maListeners.end())
240 maListeners.erase(iListener);
243 //----- CacheContext ----------------------------------------------------------
245 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
246 CacheKey aKey)
248 if ( ! mxSlides.is())
249 return;
250 const sal_Int32 nCount(mxSlides->getCount());
251 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
252 if (aKey == GetPage(nIndex))
253 CallListeners(nIndex);
256 bool PresenterPreviewCache::PresenterCacheContext::IsIdle()
258 return true;
261 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey)
263 if (mnFirstVisibleSlideIndex < 0)
264 return false;
265 for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
267 const SdrPage* pPage = GetPage(nIndex);
268 if (pPage == aKey)
269 return true;
271 return false;
274 const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey)
276 return aKey;
279 std::shared_ptr<std::vector<CacheKey> >
280 PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible)
282 std::shared_ptr<std::vector<CacheKey> > pKeys (new std::vector<CacheKey>);
284 if ( ! mxSlides.is())
285 return pKeys;
287 const sal_Int32 nFirstIndex (bVisible ? mnFirstVisibleSlideIndex : 0);
288 const sal_Int32 nLastIndex (bVisible ? mnLastVisibleSlideIndex : mxSlides->getCount()-1);
290 if (nFirstIndex < 0)
291 return pKeys;
293 for (sal_Int32 nIndex=nFirstIndex; nIndex<=nLastIndex; ++nIndex)
295 pKeys->push_back(GetPage(nIndex));
298 return pKeys;
301 sal_Int32 PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey)
303 if ( ! mxSlides.is())
304 return 0;
306 const sal_Int32 nCount (mxSlides->getCount());
308 for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
309 if (aKey == GetPage(nIndex))
310 return -nCount-1+nIndex;
312 for (sal_Int32 nIndex=0; nIndex<=nCount; ++nIndex)
313 if (aKey == GetPage(nIndex))
314 return nIndex;
316 return 0;
319 Reference<XInterface> PresenterPreviewCache::PresenterCacheContext::GetModel()
321 return mxDocument;
324 const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (
325 const sal_Int32 nSlideIndex) const
327 if ( ! mxSlides.is())
328 return nullptr;
329 if (nSlideIndex < 0 || nSlideIndex >= mxSlides->getCount())
330 return nullptr;
332 Reference<drawing::XDrawPage> xSlide (mxSlides->getByIndex(nSlideIndex), UNO_QUERY);
333 const SdPage* pPage = SdPage::getImplementation(xSlide);
334 return dynamic_cast<const SdrPage*>(pPage);
337 void PresenterPreviewCache::PresenterCacheContext::CallListeners (
338 const sal_Int32 nIndex)
340 ListenerContainer aListeners (maListeners);
341 for (const auto& rxListener : aListeners)
345 rxListener->notifyPreviewCreation(nIndex);
347 catch (lang::DisposedException&)
349 RemovePreviewCreationNotifyListener(rxListener);
354 } } // end of namespace ::sd::presenter
357 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
358 com_sun_star_comp_Draw_PresenterPreviewCache_get_implementation(css::uno::XComponentContext*,
359 css::uno::Sequence<css::uno::Any> const &)
361 return cppu::acquire(new sd::presenter::PresenterPreviewCache);
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */