LCOV - code coverage report
Current view: top level - sfx2/source/control - templateabstractview.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 22 150 14.7 %
Date: 2015-06-13 12:38:46 Functions: 7 22 31.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             : 
      10             : #include <sfx2/templateabstractview.hxx>
      11             : 
      12             : #include <comphelper/processfactory.hxx>
      13             : #include <sfx2/sfxresid.hxx>
      14             : #include <sfx2/templatecontaineritem.hxx>
      15             : #include <sfx2/templateviewitem.hxx>
      16             : #include <tools/urlobj.hxx>
      17             : #include <unotools/ucbstreamhelper.hxx>
      18             : #include <vcl/pngread.hxx>
      19             : 
      20             : #include <basegfx/polygon/b2dpolygon.hxx>
      21             : #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
      22             : #include <drawinglayer/processor2d/baseprocessor2d.hxx>
      23             : 
      24             : #include <com/sun/star/embed/ElementModes.hpp>
      25             : #include <com/sun/star/embed/XStorage.hpp>
      26             : #include <com/sun/star/embed/StorageFactory.hpp>
      27             : #include <com/sun/star/lang/XComponent.hpp>
      28             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      29             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      30             : 
      31             : #include "../doc/doc.hrc"
      32             : #include "templateview.hrc"
      33             : 
      34             : using namespace basegfx;
      35             : using namespace drawinglayer::primitive2d;
      36             : 
      37           0 : bool ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION filter, const OUString &rExt)
      38             : {
      39           0 :     bool bRet = true;
      40             : 
      41           0 :     if (filter == FILTER_APPLICATION::WRITER)
      42             :     {
      43           0 :         bRet = rExt == "ott" || rExt == "stw" || rExt == "oth" || rExt == "dot" || rExt == "dotx" || rExt == "otm";
      44             :     }
      45           0 :     else if (filter == FILTER_APPLICATION::CALC)
      46             :     {
      47           0 :         bRet = rExt == "ots" || rExt == "stc" || rExt == "xlt" || rExt == "xltm" || rExt == "xltx";
      48             :     }
      49           0 :     else if (filter == FILTER_APPLICATION::IMPRESS)
      50             :     {
      51           0 :         bRet = rExt == "otp" || rExt == "sti" || rExt == "pot" || rExt == "potm" || rExt == "potx";
      52             :     }
      53           0 :     else if (filter == FILTER_APPLICATION::DRAW)
      54             :     {
      55           0 :         bRet = rExt == "otg" || rExt == "std";
      56             :     }
      57             : 
      58           0 :     return bRet;
      59             : }
      60             : 
      61           0 : bool ViewFilter_Application::isValid (const OUString &rPath) const
      62             : {
      63           0 :     INetURLObject aUrl(rPath);
      64           0 :     return isFilteredExtension(mApp, aUrl.getExtension());
      65             : }
      66             : 
      67           0 : bool ViewFilter_Application::operator () (const ThumbnailViewItem *pItem)
      68             : {
      69           0 :     const TemplateViewItem *pTempItem = dynamic_cast<const TemplateViewItem*>(pItem);
      70           0 :     if (pTempItem)
      71           0 :         return isValid(pTempItem->getPath());
      72             : 
      73           0 :     TemplateContainerItem *pContainerItem = const_cast<TemplateContainerItem*>(dynamic_cast<const TemplateContainerItem*>(pItem));
      74           0 :     if (pContainerItem)
      75             :     {
      76           0 :         std::vector<TemplateItemProperties> &rTemplates = pContainerItem->maTemplates;
      77             : 
      78           0 :         size_t nVisCount = 0;
      79             : 
      80             :         // Clear thumbnails
      81           0 :         pContainerItem->maPreview1.Clear();
      82           0 :         pContainerItem->maPreview2.Clear();
      83           0 :         pContainerItem->maPreview3.Clear();
      84           0 :         pContainerItem->maPreview4.Clear();
      85             : 
      86           0 :         for (size_t i = 0, n = rTemplates.size(); i < n && pContainerItem->HasMissingPreview(); ++i)
      87             :         {
      88           0 :             if (isValid(rTemplates[i].aPath))
      89             :             {
      90           0 :                 ++nVisCount;
      91           0 :                 if ( pContainerItem->maPreview1.IsEmpty( ) )
      92             :                 {
      93           0 :                     pContainerItem->maPreview1 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
      94             :                                                                        TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
      95           0 :                                                                        TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
      96             :                 }
      97           0 :                 else if ( pContainerItem->maPreview2.IsEmpty() )
      98             :                 {
      99           0 :                     pContainerItem->maPreview2 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
     100             :                                                                        TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
     101           0 :                                                                        TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
     102             :                 }
     103           0 :                 else if ( pContainerItem->maPreview3.IsEmpty() )
     104             :                 {
     105           0 :                     pContainerItem->maPreview3 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
     106             :                                                                        TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
     107           0 :                                                                        TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
     108             :                 }
     109           0 :                 else if ( pContainerItem->maPreview4.IsEmpty() )
     110             :                 {
     111           0 :                     pContainerItem->maPreview4 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
     112             :                                                                        TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
     113           0 :                                                                        TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
     114             :                 }
     115             :             }
     116             :         }
     117             :     }
     118           0 :     return true;
     119             : }
     120             : 
     121           0 : bool ViewFilter_Keyword::operator ()(const ThumbnailViewItem *pItem)
     122             : {
     123             :     assert(pItem);
     124             : 
     125           0 :     return pItem->maTitle.matchIgnoreAsciiCase(maKeyword);
     126             : }
     127             : 
     128           0 : TemplateAbstractView::TemplateAbstractView (vcl::Window *pParent, WinBits nWinStyle, bool bDisableTransientChildren)
     129             :     : ThumbnailView(pParent,nWinStyle,bDisableTransientChildren),
     130             :       mnCurRegionId(0),
     131             :       maAllButton(VclPtr<PushButton>::Create(this, SfxResId(BTN_ALL_TEMPLATES))),
     132           0 :       maFTName(VclPtr<FixedText>::Create(this, SfxResId(FT_NAME)))
     133             : {
     134           0 :     maAllButton->Hide();
     135           0 :     maAllButton->SetStyle(maAllButton->GetStyle() | WB_FLATBUTTON);
     136           0 :     maAllButton->SetClickHdl(LINK(this,TemplateAbstractView,ShowRootRegionHdl));
     137           0 :     maFTName->Hide();
     138           0 : }
     139             : 
     140           1 : TemplateAbstractView::TemplateAbstractView(vcl::Window *pParent)
     141             :     : ThumbnailView(pParent),
     142             :       mnCurRegionId(0),
     143             :       maAllButton(VclPtr<PushButton>::Create(this, SfxResId(BTN_ALL_TEMPLATES))),
     144           1 :       maFTName(VclPtr<FixedText>::Create(this, SfxResId(FT_NAME)))
     145             : {
     146           1 :     maAllButton->Hide();
     147           1 :     maAllButton->SetStyle(maAllButton->GetStyle() | WB_FLATBUTTON);
     148           1 :     maAllButton->SetClickHdl(LINK(this,TemplateAbstractView,ShowRootRegionHdl));
     149           1 :     maFTName->Hide();
     150           1 : }
     151             : 
     152           2 : TemplateAbstractView::~TemplateAbstractView()
     153             : {
     154           1 :     disposeOnce();
     155           1 : }
     156             : 
     157           1 : void TemplateAbstractView::dispose()
     158             : {
     159           1 :     maAllButton.disposeAndClear();
     160           1 :     maFTName.disposeAndClear();
     161           1 :     ThumbnailView::dispose();
     162           1 : }
     163             : 
     164           0 : void TemplateAbstractView::insertItem(const TemplateItemProperties &rTemplate)
     165             : {
     166           0 :     const TemplateItemProperties *pCur = &rTemplate;
     167             : 
     168           0 :     TemplateViewItem *pChild = new TemplateViewItem(*this, pCur->nId);
     169           0 :     pChild->mnDocId = pCur->nDocId;
     170           0 :     pChild->mnRegionId = pCur->nRegionId;
     171           0 :     pChild->maTitle = pCur->aName;
     172           0 :     pChild->setPath(pCur->aPath);
     173           0 :     pChild->maPreview1 = pCur->aThumbnail;
     174             : 
     175           0 :     if ( pCur->aThumbnail.IsEmpty() )
     176             :     {
     177             :         // Use the default thumbnail if we have nothing else
     178           0 :         pChild->maPreview1 = TemplateAbstractView::getDefaultThumbnail(pCur->aPath);
     179             :     }
     180             : 
     181           0 :     pChild->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
     182             : 
     183           0 :     AppendItem(pChild);
     184             : 
     185           0 :     CalculateItemPositions();
     186           0 :     Invalidate();
     187           0 : }
     188             : 
     189           0 : void TemplateAbstractView::insertItems(const std::vector<TemplateItemProperties> &rTemplates)
     190             : {
     191           0 :     std::vector<ThumbnailViewItem*> aItems(rTemplates.size());
     192           0 :     for (size_t i = 0, n = rTemplates.size(); i < n; ++i )
     193             :     {
     194             :         //TODO: CHECK IF THE ITEM IS A FOLDER OR NOT
     195           0 :         const TemplateItemProperties *pCur = &rTemplates[i];
     196             : 
     197           0 :         TemplateViewItem *pChild = new TemplateViewItem(*this, pCur->nId);
     198           0 :         pChild->mnDocId = pCur->nDocId;
     199           0 :         pChild->mnRegionId = pCur->nRegionId;
     200           0 :         pChild->maTitle = pCur->aName;
     201           0 :         pChild->setPath(pCur->aPath);
     202           0 :         pChild->maPreview1 = pCur->aThumbnail;
     203             : 
     204           0 :         if ( pCur->aThumbnail.IsEmpty() )
     205             :         {
     206             :             // Use the default thumbnail if we have nothing else
     207           0 :             pChild->maPreview1 = TemplateAbstractView::getDefaultThumbnail(pCur->aPath);
     208             :         }
     209             : 
     210           0 :         pChild->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
     211             : 
     212           0 :         aItems[i] = pChild;
     213             :     }
     214             : 
     215           0 :     updateItems(aItems);
     216           0 : }
     217             : 
     218             : 
     219             : 
     220             : 
     221           1 : void TemplateAbstractView::setOpenRegionHdl(const Link<> &rLink)
     222             : {
     223           1 :     maOpenRegionHdl = rLink;
     224           1 : }
     225             : 
     226           1 : void TemplateAbstractView::setOpenTemplateHdl(const Link<> &rLink)
     227             : {
     228           1 :     maOpenTemplateHdl = rLink;
     229           1 : }
     230             : 
     231           0 : BitmapEx TemplateAbstractView::scaleImg (const BitmapEx &rImg, long width, long height)
     232             : {
     233           0 :     BitmapEx aImg = rImg;
     234             : 
     235           0 :     if (!rImg.IsEmpty())
     236             :     {
     237           0 :         Size aSize = rImg.GetSizePixel();
     238             : 
     239           0 :         if (aSize.Width() == 0)
     240           0 :             aSize.Width() = 1;
     241             : 
     242           0 :         if (aSize.Height() == 0)
     243           0 :             aSize.Height() = 1;
     244             : 
     245             :         // make the picture fit the given width/height constraints
     246           0 :         double nRatio = std::min(double(width)/double(aSize.Width()), double(height)/double(aSize.Height()));
     247             : 
     248           0 :         aImg.Scale(Size(aSize.Width() * nRatio, aSize.Height() * nRatio));
     249             :     }
     250             : 
     251           0 :     return aImg;
     252             : }
     253             : 
     254           0 : BitmapEx TemplateAbstractView::getDefaultThumbnail( const OUString& rPath )
     255             : {
     256           0 :     BitmapEx aImg;
     257           0 :     INetURLObject aUrl(rPath);
     258           0 :     OUString aExt = aUrl.getExtension();
     259             : 
     260           0 :     if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::WRITER, aExt) )
     261           0 :         aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_TEXT ) );
     262           0 :     else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::CALC, aExt) )
     263           0 :         aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_SHEET ) );
     264           0 :     else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::IMPRESS, aExt) )
     265           0 :         aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_PRESENTATION ) );
     266           0 :     else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::DRAW, aExt) )
     267           0 :         aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_DRAWING ) );
     268             : 
     269           0 :     return aImg;
     270             : }
     271             : 
     272           0 : BitmapEx TemplateAbstractView::fetchThumbnail (const OUString &msURL, long width, long height)
     273             : {
     274           0 :     return TemplateAbstractView::scaleImg(ThumbnailView::readThumbnail(msURL), width, height);
     275             : }
     276             : 
     277           0 : IMPL_LINK_NOARG(TemplateAbstractView, ShowRootRegionHdl)
     278             : {
     279           0 :     showRootRegion();
     280           0 :     return 0;
     281             : }
     282             : 
     283           0 : void TemplateAbstractView::OnItemDblClicked (ThumbnailViewItem *pItem)
     284             : {
     285             :     //Check if the item is a TemplateContainerItem (Folder) or a TemplateViewItem (File)
     286             : 
     287           0 :     TemplateContainerItem* pContainerItem = dynamic_cast<TemplateContainerItem*>(pItem);
     288           0 :     if ( pContainerItem )
     289             :     {
     290             :         // Fill templates
     291             : 
     292           0 :         mnCurRegionId = pContainerItem->mnRegionId+1;
     293           0 :         maCurRegionName = pContainerItem->maTitle;
     294           0 :         maFTName->SetText(maCurRegionName);
     295           0 :         showRegion(pItem);
     296             :     }
     297             :     else
     298             :     {
     299           0 :         maOpenTemplateHdl.Call(pItem);
     300             :     }
     301           0 : }
     302             : 
     303           0 : void TemplateAbstractView::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
     304             : {
     305           0 :     ThumbnailView::Paint(rRenderContext, rRect);
     306             : 
     307             :     Rectangle aRect(rRect.TopLeft(),
     308           0 :                     Point(rRect.BottomRight().X(),
     309           0 :                           mnHeaderHeight));
     310             : 
     311           0 :     drawinglayer::primitive2d::Primitive2DSequence aSeq(1);
     312           0 :     aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(
     313             :         new PolyPolygonColorPrimitive2D(B2DPolyPolygon(Polygon(aRect).getB2DPolygon()),
     314           0 :         BColor(1.0, 1.0, 1.0)));
     315             : 
     316           0 :     mpProcessor->process(aSeq);
     317         648 : }
     318             : 
     319             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11