diff options
author | Rafael Dominguez <venccsralph@gmail.com> | 2012-07-29 15:43:18 -0430 |
---|---|---|
committer | Rafael Dominguez <venccsralph@gmail.com> | 2012-07-29 21:29:29 -0430 |
commit | 76b7f976e4020b90ef5be7151944dd98abd927d7 (patch) | |
tree | 381caa12921c41f9a7c405362b473d9ae7d91a44 | |
parent | 724d575e79797bb2b734d39f91ae5e2806f2e528 (diff) |
Move fetching thumbnail functions to TemplateAbstractView.
- Reallocate lcl_ScaleImg and lcl_fetchThumbnail to be available for
other classes to use them.
- Mark this functions as static public.
Change-Id: Ia64a1bad76d7d47519f50e144daa6ea78dfa0e59
-rw-r--r-- | sfx2/inc/sfx2/templateabstractview.hxx | 4 | ||||
-rw-r--r-- | sfx2/source/control/templateabstractview.cxx | 130 | ||||
-rw-r--r-- | sfx2/source/control/templatefolderview.cxx | 137 |
3 files changed, 142 insertions, 129 deletions
diff --git a/sfx2/inc/sfx2/templateabstractview.hxx b/sfx2/inc/sfx2/templateabstractview.hxx index 8bb6426cd4ec..24ca449d038e 100644 --- a/sfx2/inc/sfx2/templateabstractview.hxx +++ b/sfx2/inc/sfx2/templateabstractview.hxx @@ -41,6 +41,10 @@ public: void setOverlayCloseHdl (const Link &rLink); + static BitmapEx scaleImg (const BitmapEx &rImg, long width, long height); + + static BitmapEx fetchThumbnail (const rtl::OUString &msURL, long width, long height); + protected: virtual void OnSelectionMode (bool bMode); diff --git a/sfx2/source/control/templateabstractview.cxx b/sfx2/source/control/templateabstractview.cxx index 28a78b6ba2e1..940874b1a44c 100644 --- a/sfx2/source/control/templateabstractview.cxx +++ b/sfx2/source/control/templateabstractview.cxx @@ -9,7 +9,16 @@ #include <sfx2/templateabstractview.hxx> +#include <comphelper/processfactory.hxx> #include <sfx2/templateview.hxx> +#include <unotools/ucbstreamhelper.hxx> +#include <vcl/pngread.hxx> + +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/embed/XStorage.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> TemplateAbstractView::TemplateAbstractView (Window *pParent, WinBits nWinStyle, bool bDisableTransientChildren) : ThumbnailView(pParent,nWinStyle,bDisableTransientChildren), @@ -54,6 +63,127 @@ void TemplateAbstractView::setOverlayCloseHdl(const Link &rLink) mpItemView->setCloseHdl(rLink); } +BitmapEx TemplateAbstractView::scaleImg (const BitmapEx &rImg, long width, long height) +{ + BitmapEx aImg = rImg; + + int sWidth = std::min(aImg.GetSizePixel().getWidth(),width); + int sHeight = std::min(aImg.GetSizePixel().getHeight(),height); + + aImg.Scale(Size(sWidth,sHeight),BMP_SCALE_INTERPOLATE); + + return aImg; +} + +BitmapEx TemplateAbstractView::fetchThumbnail (const rtl::OUString &msURL, long width, long height) +{ + using namespace ::com::sun::star; + using namespace ::com::sun::star::uno; + + // Load the thumbnail from a template document. + uno::Reference<io::XInputStream> xIStream; + + uno::Reference< lang::XMultiServiceFactory > xServiceManager (comphelper::getProcessServiceFactory()); + + if (xServiceManager.is()) + { + try + { + uno::Reference<lang::XSingleServiceFactory> xStorageFactory( + xServiceManager->createInstance( "com.sun.star.embed.StorageFactory"), + uno::UNO_QUERY); + + if (xStorageFactory.is()) + { + uno::Sequence<uno::Any> aArgs (2); + aArgs[0] <<= msURL; + aArgs[1] <<= embed::ElementModes::READ; + uno::Reference<embed::XStorage> xDocStorage ( + xStorageFactory->createInstanceWithArguments(aArgs), + uno::UNO_QUERY); + + try + { + if (xDocStorage.is()) + { + uno::Reference<embed::XStorage> xStorage ( + xDocStorage->openStorageElement( + "Thumbnails", + embed::ElementModes::READ)); + if (xStorage.is()) + { + uno::Reference<io::XStream> xThumbnailCopy ( + xStorage->cloneStreamElement("thumbnail.png")); + if (xThumbnailCopy.is()) + xIStream = xThumbnailCopy->getInputStream(); + } + } + } + catch (const uno::Exception& rException) + { + OSL_TRACE ( + "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s", + ::rtl::OUStringToOString(msURL, + RTL_TEXTENCODING_UTF8).getStr(), + ::rtl::OUStringToOString(rException.Message, + RTL_TEXTENCODING_UTF8).getStr()); + } + + try + { + // An (older) implementation had a bug - The storage + // name was "Thumbnail" instead of "Thumbnails". The + // old name is still used as fallback but this code can + // be removed soon. + if ( ! xIStream.is()) + { + uno::Reference<embed::XStorage> xStorage ( + xDocStorage->openStorageElement( "Thumbnail", + embed::ElementModes::READ)); + if (xStorage.is()) + { + uno::Reference<io::XStream> xThumbnailCopy ( + xStorage->cloneStreamElement("thumbnail.png")); + if (xThumbnailCopy.is()) + xIStream = xThumbnailCopy->getInputStream(); + } + } + } + catch (const uno::Exception& rException) + { + OSL_TRACE ( + "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s", + ::rtl::OUStringToOString(msURL, + RTL_TEXTENCODING_UTF8).getStr(), + ::rtl::OUStringToOString(rException.Message, + RTL_TEXTENCODING_UTF8).getStr()); + } + } + } + catch (const uno::Exception& rException) + { + OSL_TRACE ( + "caught exception while trying to access tuhmbnail of %s: %s", + ::rtl::OUStringToOString(msURL, + RTL_TEXTENCODING_UTF8).getStr(), + ::rtl::OUStringToOString(rException.Message, + RTL_TEXTENCODING_UTF8).getStr()); + } + } + + // Extract the image from the stream. + BitmapEx aThumbnail; + if (xIStream.is()) + { + ::std::auto_ptr<SvStream> pStream ( + ::utl::UcbStreamHelper::CreateStream (xIStream)); + ::vcl::PNGReader aReader (*pStream); + aThumbnail = aReader.Read (); + } + + return TemplateAbstractView::scaleImg(aThumbnail,width,height); +} + void TemplateAbstractView::OnSelectionMode (bool bMode) { if (mpItemView->IsVisible()) diff --git a/sfx2/source/control/templatefolderview.cxx b/sfx2/source/control/templatefolderview.cxx index 5521962dbe42..c9f53b15116c 100644 --- a/sfx2/source/control/templatefolderview.cxx +++ b/sfx2/source/control/templatefolderview.cxx @@ -38,127 +38,6 @@ void lcl_updateThumbnails (TemplateFolderViewItem *pItem); -BitmapEx lcl_ScaleImg (const BitmapEx &rImg, long width, long height) -{ - BitmapEx aImg = rImg; - - int sWidth = std::min(aImg.GetSizePixel().getWidth(),width); - int sHeight = std::min(aImg.GetSizePixel().getHeight(),height); - - aImg.Scale(Size(sWidth,sHeight),BMP_SCALE_INTERPOLATE); - - return aImg; -} - -BitmapEx lcl_fetchThumbnail (const rtl::OUString &msURL, long width, long height) -{ - using namespace ::com::sun::star; - using namespace ::com::sun::star::uno; - - // Load the thumbnail from a template document. - uno::Reference<io::XInputStream> xIStream; - - uno::Reference< lang::XMultiServiceFactory > xServiceManager ( - ::comphelper::getProcessServiceFactory()); - if (xServiceManager.is()) - { - try - { - uno::Reference<lang::XSingleServiceFactory> xStorageFactory( - xServiceManager->createInstance( "com.sun.star.embed.StorageFactory"), - uno::UNO_QUERY); - - if (xStorageFactory.is()) - { - uno::Sequence<uno::Any> aArgs (2); - aArgs[0] <<= msURL; - aArgs[1] <<= embed::ElementModes::READ; - uno::Reference<embed::XStorage> xDocStorage ( - xStorageFactory->createInstanceWithArguments(aArgs), - uno::UNO_QUERY); - - try - { - if (xDocStorage.is()) - { - uno::Reference<embed::XStorage> xStorage ( - xDocStorage->openStorageElement( - "Thumbnails", - embed::ElementModes::READ)); - if (xStorage.is()) - { - uno::Reference<io::XStream> xThumbnailCopy ( - xStorage->cloneStreamElement("thumbnail.png")); - if (xThumbnailCopy.is()) - xIStream = xThumbnailCopy->getInputStream(); - } - } - } - catch (const uno::Exception& rException) - { - OSL_TRACE ( - "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s", - ::rtl::OUStringToOString(msURL, - RTL_TEXTENCODING_UTF8).getStr(), - ::rtl::OUStringToOString(rException.Message, - RTL_TEXTENCODING_UTF8).getStr()); - } - - try - { - // An (older) implementation had a bug - The storage - // name was "Thumbnail" instead of "Thumbnails". The - // old name is still used as fallback but this code can - // be removed soon. - if ( ! xIStream.is()) - { - uno::Reference<embed::XStorage> xStorage ( - xDocStorage->openStorageElement( "Thumbnail", - embed::ElementModes::READ)); - if (xStorage.is()) - { - uno::Reference<io::XStream> xThumbnailCopy ( - xStorage->cloneStreamElement("thumbnail.png")); - if (xThumbnailCopy.is()) - xIStream = xThumbnailCopy->getInputStream(); - } - } - } - catch (const uno::Exception& rException) - { - OSL_TRACE ( - "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s", - ::rtl::OUStringToOString(msURL, - RTL_TEXTENCODING_UTF8).getStr(), - ::rtl::OUStringToOString(rException.Message, - RTL_TEXTENCODING_UTF8).getStr()); - } - } - } - catch (const uno::Exception& rException) - { - OSL_TRACE ( - "caught exception while trying to access tuhmbnail of %s: %s", - ::rtl::OUStringToOString(msURL, - RTL_TEXTENCODING_UTF8).getStr(), - ::rtl::OUStringToOString(rException.Message, - RTL_TEXTENCODING_UTF8).getStr()); - } - } - - // Extract the image from the stream. - BitmapEx aThumbnail; - if (xIStream.is()) - { - ::std::auto_ptr<SvStream> pStream ( - ::utl::UcbStreamHelper::CreateStream (xIStream)); - ::vcl::PNGReader aReader (*pStream); - aThumbnail = aReader.Read (); - } - - return lcl_ScaleImg(aThumbnail,width,height); -} - // Display template items depending on the generator application class ViewFilter_Application { @@ -331,7 +210,7 @@ void TemplateFolderView::Populate () aProperties.aName = aName; aProperties.aPath = aURL; aProperties.aType = aType; - aProperties.aThumbnail = lcl_fetchThumbnail(aURL,THUMBNAIL_MAX_WIDTH,THUMBNAIL_MAX_HEIGHT); + aProperties.aThumbnail = TemplateAbstractView::fetchThumbnail(aURL,THUMBNAIL_MAX_WIDTH,THUMBNAIL_MAX_HEIGHT); pItem->maTemplates.push_back(aProperties); } @@ -688,7 +567,7 @@ void TemplateFolderView::copyFrom (TemplateFolderViewItem *pItem, const rtl::OUS aTemplate.nId = nId; aTemplate.nRegionId = nRegionId; aTemplate.aName = mpDocTemplates->GetName(nRegionId,nId); - aTemplate.aThumbnail = lcl_fetchThumbnail(rPath,128,128); + aTemplate.aThumbnail = TemplateAbstractView::fetchThumbnail(rPath,128,128); aTemplate.aPath = rPath; aTemplate.aType = SvFileInformationManager::GetDescription(INetURLObject(rPath)); @@ -765,15 +644,15 @@ void lcl_updateThumbnails (TemplateFolderViewItem *pItem) { if (i == 0) { - pItem->maPreview1 = lcl_ScaleImg(pItem->maTemplates[i].aThumbnail, - THUMBNAIL_MAX_WIDTH*0.75, - THUMBNAIL_MAX_HEIGHT*0.75); + pItem->maPreview1 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail, + THUMBNAIL_MAX_WIDTH*0.75, + THUMBNAIL_MAX_HEIGHT*0.75); } else { - pItem->maPreview2 = lcl_ScaleImg(pItem->maTemplates[i].aThumbnail, - THUMBNAIL_MAX_WIDTH*0.75, - THUMBNAIL_MAX_HEIGHT*0.75); + pItem->maPreview2 = TemplateAbstractView::scaleImg(pItem->maTemplates[i].aThumbnail, + THUMBNAIL_MAX_WIDTH*0.75, + THUMBNAIL_MAX_HEIGHT*0.75); } } } |