tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / presenter / PresenterPreviewCache.cxx
blob052ed7e0853834228adbd02150ec2ac8f18e5d85
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"
22 #include <cache/SlsPageCache.hxx>
23 #include <cache/SlsCacheContext.hxx>
24 #include <vcl/bitmapex.hxx>
25 #include <sdpage.hxx>
26 #include <cppcanvas/vclfactory.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <com/sun/star/drawing/XDrawPage.hpp>
29 #include <osl/diagnose.h>
30 #include <unomodel.hxx>
32 namespace com::sun::star::uno { class XComponentContext; }
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using namespace ::sd::slidesorter::cache;
38 namespace sd::presenter {
40 class PresenterPreviewCache::PresenterCacheContext : public CacheContext
42 public:
43 PresenterCacheContext();
45 void SetDocumentSlides (
46 const Reference<container::XIndexAccess>& rxSlides,
47 const rtl::Reference<SdXImpressDocument>& rxDocument);
48 void SetVisibleSlideRange (
49 const sal_Int32 nFirstVisibleSlideIndex,
50 const sal_Int32 nLastVisibleSlideIndex);
51 const SdrPage* GetPage (const sal_Int32 nSlideIndex) const;
52 void AddPreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
53 void RemovePreviewCreationNotifyListener (const Reference<drawing::XSlidePreviewCacheListener>& rxListener);
55 // CacheContext
56 virtual void NotifyPreviewCreation (CacheKey aKey) override;
57 virtual bool IsIdle() override;
58 virtual bool IsVisible (CacheKey aKey) override;
59 virtual const SdrPage* GetPage (CacheKey aKey) override;
60 virtual std::shared_ptr<std::vector<CacheKey> > GetEntryList (bool bVisible) override;
61 virtual sal_Int32 GetPriority (CacheKey aKey) override;
62 virtual SdXImpressDocument* GetModel() override;
64 private:
65 Reference<container::XIndexAccess> mxSlides;
66 rtl::Reference<SdXImpressDocument> mxDocument;
67 sal_Int32 mnFirstVisibleSlideIndex;
68 sal_Int32 mnLastVisibleSlideIndex;
69 typedef ::std::vector<css::uno::Reference<css::drawing::XSlidePreviewCacheListener> > ListenerContainer;
70 ListenerContainer maListeners;
72 void CallListeners (const sal_Int32 nSlideIndex);
75 //===== PresenterPreviewCache =================================================
77 PresenterPreviewCache::PresenterPreviewCache ()
78 : maPreviewSize(Size(200,200)),
79 mpCacheContext(std::make_shared<PresenterCacheContext>()),
80 mpCache(std::make_shared<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 OUString PresenterPreviewCache::getImplementationName() {
97 return u"com.sun.star.comp.Draw.PresenterPreviewCache"_ustr;
100 sal_Bool PresenterPreviewCache::supportsService(OUString const & ServiceName) {
101 return cppu::supportsService(this, ServiceName);
104 css::uno::Sequence<OUString> PresenterPreviewCache::getSupportedServiceNames() {
105 return {u"com.sun.star.drawing.PresenterPreviewCache"_ustr};
108 //----- XSlidePreviewCache ----------------------------------------------------
110 void SAL_CALL PresenterPreviewCache::setDocumentSlides (
111 const Reference<container::XIndexAccess>& rxSlides,
112 const Reference<XInterface>& rxDocument)
114 ThrowIfDisposed();
115 OSL_ASSERT(mpCacheContext != nullptr);
117 SdXImpressDocument* pImpressDoc = dynamic_cast<SdXImpressDocument*>(rxDocument.get());
118 assert(pImpressDoc);
119 mpCacheContext->SetDocumentSlides(rxSlides, pImpressDoc);
122 void SAL_CALL PresenterPreviewCache::setVisibleRange (
123 sal_Int32 nFirstVisibleSlideIndex,
124 sal_Int32 nLastVisibleSlideIndex)
126 ThrowIfDisposed();
127 OSL_ASSERT(mpCacheContext != nullptr);
129 mpCacheContext->SetVisibleSlideRange (nFirstVisibleSlideIndex, nLastVisibleSlideIndex);
132 void SAL_CALL PresenterPreviewCache::setPreviewSize (
133 const css::geometry::IntegerSize2D& rSize)
135 ThrowIfDisposed();
136 OSL_ASSERT(mpCache != nullptr);
138 maPreviewSize = Size(rSize.Width, rSize.Height);
139 mpCache->ChangeSize(maPreviewSize, Bitmap::HasFastScale());
142 Reference<rendering::XBitmap> SAL_CALL PresenterPreviewCache::getSlidePreview (
143 sal_Int32 nSlideIndex,
144 const Reference<rendering::XCanvas>& rxCanvas)
146 ThrowIfDisposed();
147 OSL_ASSERT(mpCacheContext != nullptr);
149 cppcanvas::CanvasSharedPtr pCanvas (
150 cppcanvas::VCLFactory::createCanvas(rxCanvas));
152 const SdrPage* pPage = mpCacheContext->GetPage(nSlideIndex);
153 if (pPage == nullptr)
154 throw RuntimeException();
156 const BitmapEx aPreview (mpCache->GetPreviewBitmap(pPage,true));
157 if (aPreview.IsEmpty())
158 return nullptr;
159 else
160 return cppcanvas::VCLFactory::createBitmap(
161 pCanvas,
162 aPreview)->getUNOBitmap();
165 void SAL_CALL PresenterPreviewCache::addPreviewCreationNotifyListener (
166 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
168 if (m_bDisposed)
169 return;
170 if (rxListener.is())
171 mpCacheContext->AddPreviewCreationNotifyListener(rxListener);
174 void SAL_CALL PresenterPreviewCache::removePreviewCreationNotifyListener (
175 const css::uno::Reference<css::drawing::XSlidePreviewCacheListener>& rxListener)
177 ThrowIfDisposed();
178 mpCacheContext->RemovePreviewCreationNotifyListener(rxListener);
181 void SAL_CALL PresenterPreviewCache::pause()
183 ThrowIfDisposed();
184 OSL_ASSERT(mpCache != nullptr);
185 mpCache->Pause();
188 void SAL_CALL PresenterPreviewCache::resume()
190 ThrowIfDisposed();
191 OSL_ASSERT(mpCache != nullptr);
192 mpCache->Resume();
195 void PresenterPreviewCache::ThrowIfDisposed()
197 if (m_bDisposed)
199 throw lang::DisposedException (u"PresenterPreviewCache object has already been disposed"_ustr,
200 static_cast<uno::XWeak*>(this));
204 //===== PresenterPreviewCache::PresenterCacheContext ==========================
206 PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext()
207 : mnFirstVisibleSlideIndex(-1),
208 mnLastVisibleSlideIndex(-1)
212 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
213 const Reference<container::XIndexAccess>& rxSlides,
214 const rtl::Reference<SdXImpressDocument>& rxDocument)
216 mxSlides = rxSlides;
217 mxDocument = rxDocument;
218 mnFirstVisibleSlideIndex = -1;
219 mnLastVisibleSlideIndex = -1;
222 void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
223 const sal_Int32 nFirstVisibleSlideIndex,
224 const sal_Int32 nLastVisibleSlideIndex)
226 if (nFirstVisibleSlideIndex > nLastVisibleSlideIndex || nFirstVisibleSlideIndex<0)
228 mnFirstVisibleSlideIndex = -1;
229 mnLastVisibleSlideIndex = -1;
231 else
233 mnFirstVisibleSlideIndex = nFirstVisibleSlideIndex;
234 mnLastVisibleSlideIndex = nLastVisibleSlideIndex;
236 if (mxSlides.is() && mnLastVisibleSlideIndex >= mxSlides->getCount())
237 mnLastVisibleSlideIndex = mxSlides->getCount() - 1;
240 void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
241 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
243 maListeners.push_back(rxListener);
246 void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
247 const Reference<drawing::XSlidePreviewCacheListener>& rxListener)
249 auto iListener = std::find(maListeners.begin(), maListeners.end(), rxListener);
250 if (iListener != maListeners.end())
251 maListeners.erase(iListener);
254 //----- CacheContext ----------------------------------------------------------
256 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
257 CacheKey aKey)
259 if ( ! mxSlides.is())
260 return;
261 const sal_Int32 nCount(mxSlides->getCount());
262 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
263 if (aKey == GetPage(nIndex))
264 CallListeners(nIndex);
267 bool PresenterPreviewCache::PresenterCacheContext::IsIdle()
269 return true;
272 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey)
274 if (mnFirstVisibleSlideIndex < 0)
275 return false;
276 for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
278 const SdrPage* pPage = GetPage(nIndex);
279 if (pPage == aKey)
280 return true;
282 return false;
285 const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey)
287 return aKey;
290 std::shared_ptr<std::vector<CacheKey> >
291 PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible)
293 auto pKeys = std::make_shared<std::vector<CacheKey>>();
295 if ( ! mxSlides.is())
296 return pKeys;
298 const sal_Int32 nFirstIndex (bVisible ? mnFirstVisibleSlideIndex : 0);
299 const sal_Int32 nLastIndex (bVisible ? mnLastVisibleSlideIndex : mxSlides->getCount()-1);
301 if (nFirstIndex < 0)
302 return pKeys;
304 for (sal_Int32 nIndex=nFirstIndex; nIndex<=nLastIndex; ++nIndex)
306 pKeys->push_back(GetPage(nIndex));
309 return pKeys;
312 sal_Int32 PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey)
314 if ( ! mxSlides.is())
315 return 0;
317 const sal_Int32 nCount (mxSlides->getCount());
319 for (sal_Int32 nIndex=mnFirstVisibleSlideIndex; nIndex<=mnLastVisibleSlideIndex; ++nIndex)
320 if (aKey == GetPage(nIndex))
321 return -nCount-1+nIndex;
323 for (sal_Int32 nIndex=0; nIndex<=nCount; ++nIndex)
324 if (aKey == GetPage(nIndex))
325 return nIndex;
327 return 0;
330 SdXImpressDocument* PresenterPreviewCache::PresenterCacheContext::GetModel()
332 return mxDocument.get();
335 const SdrPage* PresenterPreviewCache::PresenterCacheContext::GetPage (
336 const sal_Int32 nSlideIndex) const
338 if ( ! mxSlides.is())
339 return nullptr;
340 if (nSlideIndex < 0 || nSlideIndex >= mxSlides->getCount())
341 return nullptr;
343 Reference<drawing::XDrawPage> xSlide (mxSlides->getByIndex(nSlideIndex), UNO_QUERY);
344 const SdPage* pPage = SdPage::getImplementation(xSlide);
345 return pPage;
348 void PresenterPreviewCache::PresenterCacheContext::CallListeners (
349 const sal_Int32 nIndex)
351 ListenerContainer aListeners (maListeners);
352 for (const auto& rxListener : aListeners)
356 rxListener->notifyPreviewCreation(nIndex);
358 catch (lang::DisposedException&)
360 RemovePreviewCreationNotifyListener(rxListener);
365 } // end of namespace ::sd::presenter
368 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
369 com_sun_star_comp_Draw_PresenterPreviewCache_get_implementation(css::uno::XComponentContext*,
370 css::uno::Sequence<css::uno::Any> const &)
372 return cppu::acquire(new sd::presenter::PresenterPreviewCache);
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */