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

Generated by: LCOV version 1.10