LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/controller - SlsPageSelector.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 158 198 79.8 %
Date: 2015-06-13 12:38:46 Functions: 23 28 82.1 %
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             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "controller/SlsPageSelector.hxx"
      21             : 
      22             : #include "SlideSorter.hxx"
      23             : #include "SlideSorterViewShell.hxx"
      24             : #include "controller/SlideSorterController.hxx"
      25             : #include "controller/SlsSelectionManager.hxx"
      26             : #include "controller/SlsAnimator.hxx"
      27             : #include "controller/SlsCurrentSlideManager.hxx"
      28             : #include "controller/SlsVisibleAreaManager.hxx"
      29             : #include "model/SlsPageDescriptor.hxx"
      30             : #include "model/SlsPageEnumerationProvider.hxx"
      31             : #include "model/SlideSorterModel.hxx"
      32             : #include "view/SlideSorterView.hxx"
      33             : 
      34             : #include "sdpage.hxx"
      35             : #include "ViewShell.hxx"
      36             : #include "DrawViewShell.hxx"
      37             : #include "ViewShellBase.hxx"
      38             : #include <com/sun/star/drawing/XDrawView.hpp>
      39             : #include <com/sun/star/beans/XPropertySet.hpp>
      40             : #include <boost/shared_ptr.hpp>
      41             : 
      42             : using namespace ::com::sun::star;
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::sd::slidesorter::model;
      45             : using namespace ::sd::slidesorter::view;
      46             : 
      47             : namespace sd { namespace slidesorter { namespace controller {
      48             : 
      49          64 : PageSelector::PageSelector (SlideSorter& rSlideSorter)
      50          64 :     : mrModel(rSlideSorter.GetModel()),
      51             :       mrSlideSorter(rSlideSorter),
      52          64 :       mrController(mrSlideSorter.GetController()),
      53             :       mnSelectedPageCount(0),
      54             :       mnBroadcastDisableLevel(0),
      55             :       mbSelectionChangeBroadcastPending(false),
      56             :       mpMostRecentlySelectedPage(),
      57             :       mpSelectionAnchor(),
      58             :       mpCurrentPage(),
      59             :       mnUpdateLockCount(0),
      60         192 :       mbIsUpdateCurrentPagePending(true)
      61             : {
      62          64 :     CountSelectedPages ();
      63          64 : }
      64             : 
      65           0 : void PageSelector::SelectAllPages()
      66             : {
      67           0 :     VisibleAreaManager::TemporaryDisabler aDisabler (mrSlideSorter);
      68           0 :     PageSelector::UpdateLock aLock (*this);
      69             : 
      70           0 :     int nPageCount = mrModel.GetPageCount();
      71           0 :     for (int nPageIndex=0; nPageIndex<nPageCount; nPageIndex++)
      72           0 :         SelectPage(nPageIndex);
      73           0 : }
      74             : 
      75          97 : void PageSelector::DeselectAllPages()
      76             : {
      77          97 :     VisibleAreaManager::TemporaryDisabler aDisabler (mrSlideSorter);
      78         194 :     PageSelector::UpdateLock aLock (*this);
      79             : 
      80          97 :     int nPageCount = mrModel.GetPageCount();
      81         207 :     for (int nPageIndex=0; nPageIndex<nPageCount; nPageIndex++)
      82         110 :         DeselectPage(nPageIndex);
      83             : 
      84             :     DBG_ASSERT (mnSelectedPageCount==0,
      85             :         "PageSelector::DeselectAllPages: the selected pages counter is not 0");
      86          97 :     mnSelectedPageCount = 0;
      87         194 :     mpSelectionAnchor.reset();
      88          97 : }
      89             : 
      90          64 : void PageSelector::GetCoreSelection()
      91             : {
      92          64 :     PageSelector::UpdateLock aLock (*this);
      93             : 
      94          64 :     bool bSelectionHasChanged (true);
      95          64 :     mnSelectedPageCount = 0;
      96             :     model::PageEnumeration aAllPages (
      97         128 :         model::PageEnumerationProvider::CreateAllPagesEnumeration(mrModel));
      98         128 :     while (aAllPages.HasMoreElements())
      99             :     {
     100           0 :         model::SharedPageDescriptor pDescriptor (aAllPages.GetNextElement());
     101           0 :         if (pDescriptor->GetCoreSelection())
     102             :         {
     103           0 :             mrSlideSorter.GetController().GetVisibleAreaManager().RequestVisible(pDescriptor);
     104           0 :             mrSlideSorter.GetView().RequestRepaint(pDescriptor);
     105           0 :             bSelectionHasChanged = true;
     106             :         }
     107             : 
     108           0 :         if (pDescriptor->HasState(PageDescriptor::ST_Selected))
     109           0 :             mnSelectedPageCount++;
     110           0 :     }
     111             : 
     112          64 :     if (bSelectionHasChanged)
     113             :     {
     114          64 :         if (mnBroadcastDisableLevel > 0)
     115           0 :             mbSelectionChangeBroadcastPending = true;
     116             :         else
     117          64 :             mrController.GetSelectionManager()->SelectionHasChanged();
     118          64 :     }
     119          64 : }
     120             : 
     121           0 : void PageSelector::SetCoreSelection()
     122             : {
     123             :     model::PageEnumeration aAllPages (
     124           0 :         model::PageEnumerationProvider::CreateAllPagesEnumeration(mrModel));
     125           0 :     while (aAllPages.HasMoreElements())
     126             :     {
     127           0 :         model::SharedPageDescriptor pDescriptor (aAllPages.GetNextElement());
     128           0 :         pDescriptor->SetCoreSelection();
     129           0 :     }
     130           0 : }
     131             : 
     132          20 : void PageSelector::SelectPage (int nPageIndex)
     133             : {
     134          20 :     SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
     135          20 :     if (pDescriptor.get() != NULL)
     136          18 :         SelectPage(pDescriptor);
     137          20 : }
     138             : 
     139          85 : void PageSelector::SelectPage (const SdPage* pPage)
     140             : {
     141          85 :     const sal_Int32 nPageIndex (mrModel.GetIndex(pPage));
     142          85 :     SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
     143          85 :     if (pDescriptor.get()!=NULL && pDescriptor->GetPage()==pPage)
     144          85 :         SelectPage(pDescriptor);
     145          85 : }
     146             : 
     147         188 : void PageSelector::SelectPage (const SharedPageDescriptor& rpDescriptor)
     148             : {
     149         376 :     if (rpDescriptor.get()!=NULL
     150         188 :         && mrSlideSorter.GetView().SetState(rpDescriptor, PageDescriptor::ST_Selected, true))
     151             :     {
     152          85 :         ++mnSelectedPageCount;
     153          85 :         mrSlideSorter.GetController().GetVisibleAreaManager().RequestVisible(rpDescriptor,true);
     154          85 :         mrSlideSorter.GetView().RequestRepaint(rpDescriptor);
     155             : 
     156          85 :         mpMostRecentlySelectedPage = rpDescriptor;
     157          85 :         if (mpSelectionAnchor == 0)
     158          85 :             mpSelectionAnchor = rpDescriptor;
     159             : 
     160          85 :         if (mnBroadcastDisableLevel > 0)
     161          85 :             mbSelectionChangeBroadcastPending = true;
     162             :         else
     163           0 :             mrController.GetSelectionManager()->SelectionHasChanged();
     164          85 :         UpdateCurrentPage();
     165             : 
     166          85 :         CheckConsistency();
     167             :     }
     168         188 : }
     169             : 
     170         110 : void PageSelector::DeselectPage (
     171             :     int nPageIndex,
     172             :     const bool bUpdateCurrentPage)
     173             : {
     174         110 :     model::SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
     175         110 :     if (pDescriptor.get() != NULL)
     176         110 :         DeselectPage(pDescriptor, bUpdateCurrentPage);
     177         110 : }
     178             : 
     179         110 : void PageSelector::DeselectPage (
     180             :     const SharedPageDescriptor& rpDescriptor,
     181             :     const bool bUpdateCurrentPage)
     182             : {
     183         220 :     if (rpDescriptor.get()!=NULL
     184         110 :         && mrSlideSorter.GetView().SetState(rpDescriptor, PageDescriptor::ST_Selected, false))
     185             :     {
     186          85 :         --mnSelectedPageCount;
     187          85 :         mrSlideSorter.GetController().GetVisibleAreaManager().RequestVisible(rpDescriptor);
     188          85 :         mrSlideSorter.GetView().RequestRepaint(rpDescriptor);
     189          85 :         if (mpMostRecentlySelectedPage == rpDescriptor)
     190          14 :             mpMostRecentlySelectedPage.reset();
     191          85 :         if (mnBroadcastDisableLevel > 0)
     192          85 :             mbSelectionChangeBroadcastPending = true;
     193             :         else
     194           0 :             mrController.GetSelectionManager()->SelectionHasChanged();
     195          85 :         if (bUpdateCurrentPage)
     196          85 :             UpdateCurrentPage();
     197             : 
     198          85 :         CheckConsistency();
     199             :     }
     200         110 : }
     201             : 
     202         170 : void PageSelector::CheckConsistency() const
     203             : {
     204         170 :     int nSelectionCount (0);
     205         373 :     for (int nPageIndex=0,nPageCount=mrModel.GetPageCount(); nPageIndex<nPageCount; nPageIndex++)
     206             :     {
     207         203 :         SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
     208             :         assert(pDescriptor);
     209         203 :         if (pDescriptor->HasState(PageDescriptor::ST_Selected))
     210          85 :             ++nSelectionCount;
     211         203 :     }
     212         170 :     if (nSelectionCount!=mnSelectedPageCount)
     213             :     {
     214             :         // #i120020# The former call to assert(..) internally calls
     215             :         // SlideSorterModel::GetPageDescriptor which will crash in this situation
     216             :         // (only in non-pro code). All what is wanted there is to assert it (the
     217             :         // error is already detected), so do this directly.
     218             :         OSL_ENSURE(false, "PageSelector: Consistency error (!)");
     219             :     }
     220         170 : }
     221             : 
     222           0 : bool PageSelector::IsPageSelected (int nPageIndex)
     223             : {
     224           0 :     SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
     225           0 :     if (pDescriptor.get() != NULL)
     226           0 :         return pDescriptor->HasState(PageDescriptor::ST_Selected);
     227             :     else
     228           0 :         return false;
     229             : }
     230             : 
     231         319 : int PageSelector::GetPageCount() const
     232             : {
     233         319 :     return mrModel.GetPageCount();
     234             : }
     235             : 
     236         140 : void PageSelector::CountSelectedPages()
     237             : {
     238         140 :     mnSelectedPageCount = 0;
     239             :     model::PageEnumeration aSelectedPages (
     240         140 :         model::PageEnumerationProvider::CreateSelectedPagesEnumeration(mrModel));
     241         351 :     while (aSelectedPages.HasMoreElements())
     242             :     {
     243          71 :         mnSelectedPageCount++;
     244          71 :         aSelectedPages.GetNextElement();
     245         140 :     }
     246         140 : }
     247             : 
     248          97 : void PageSelector::EnableBroadcasting()
     249             : {
     250          97 :     if (mnBroadcastDisableLevel > 0)
     251          97 :         mnBroadcastDisableLevel --;
     252          97 :     if (mnBroadcastDisableLevel==0 && mbSelectionChangeBroadcastPending)
     253             :     {
     254          91 :         mrController.GetSelectionManager()->SelectionHasChanged();
     255          91 :         mbSelectionChangeBroadcastPending = false;
     256             :     }
     257          97 : }
     258             : 
     259          97 : void PageSelector::DisableBroadcasting()
     260             : {
     261          97 :     mnBroadcastDisableLevel ++;
     262          97 : }
     263             : 
     264          85 : ::boost::shared_ptr<PageSelector::PageSelection> PageSelector::GetPageSelection() const
     265             : {
     266          85 :     ::boost::shared_ptr<PageSelection> pSelection (new PageSelection());
     267          85 :     pSelection->reserve(GetSelectedPageCount());
     268             : 
     269          85 :     int nPageCount = GetPageCount();
     270         186 :     for (int nIndex=0; nIndex<nPageCount; nIndex++)
     271             :     {
     272         101 :         SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nIndex));
     273         101 :         if (pDescriptor.get()!=NULL && pDescriptor->HasState(PageDescriptor::ST_Selected))
     274          85 :             pSelection->push_back(pDescriptor->GetPage());
     275         101 :     }
     276             : 
     277          85 :     return pSelection;
     278             : }
     279             : 
     280          85 : void PageSelector::SetPageSelection (
     281             :     const ::boost::shared_ptr<PageSelection>& rpSelection,
     282             :     const bool bUpdateCurrentPage)
     283             : {
     284          85 :     PageSelection::const_iterator iPage;
     285         170 :     for (iPage=rpSelection->begin(); iPage!=rpSelection->end(); ++iPage)
     286          85 :         SelectPage(*iPage);
     287          85 :     if (bUpdateCurrentPage)
     288           0 :         UpdateCurrentPage();
     289          85 : }
     290             : 
     291         331 : void PageSelector::UpdateCurrentPage (const bool bUpdateOnlyWhenPending)
     292             : {
     293         331 :     if (mnUpdateLockCount > 0)
     294             :     {
     295          85 :         mbIsUpdateCurrentPagePending = true;
     296         182 :         return;
     297             :     }
     298             : 
     299         246 :     if ( ! mbIsUpdateCurrentPagePending && bUpdateOnlyWhenPending)
     300          12 :         return;
     301             : 
     302         234 :     mbIsUpdateCurrentPagePending = false;
     303             : 
     304             :     // Make the first selected page the current page.
     305         234 :     SharedPageDescriptor pCurrentPageDescriptor;
     306         234 :     const sal_Int32 nPageCount (GetPageCount());
     307         347 :     for (sal_Int32 nIndex=0; nIndex<nPageCount; ++nIndex)
     308             :     {
     309         198 :         SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nIndex));
     310         198 :         if ( ! pDescriptor)
     311           0 :             continue;
     312         198 :         if (pDescriptor->HasState(PageDescriptor::ST_Selected))
     313             :         {
     314          85 :             pCurrentPageDescriptor = pDescriptor;
     315          85 :             break;
     316             :         }
     317         113 :     }
     318             : 
     319         234 :     if (pCurrentPageDescriptor)
     320             :     {
     321             :         // Switching the current slide normally sets also the
     322             :         // selection to just the new current slide.  To prevent that,
     323             :         // we store (and at the end of this scope restore) the current
     324             :         // selection.
     325          85 :         ::boost::shared_ptr<PageSelection> pSelection (GetPageSelection());
     326             : 
     327          85 :         mrController.GetCurrentSlideManager()->SwitchCurrentSlide(pCurrentPageDescriptor);
     328             : 
     329             :         // Restore the selection and prevent a recursive call to
     330             :         // UpdateCurrentPage().
     331          85 :         SetPageSelection(pSelection, false);
     332         234 :     }
     333             : }
     334             : 
     335             : //===== PageSelector::UpdateLock ==============================================
     336             : 
     337           0 : PageSelector::UpdateLock::UpdateLock (SlideSorter& rSlideSorter)
     338           0 :     : mpSelector(&rSlideSorter.GetController().GetPageSelector())
     339             : {
     340           0 :     ++mpSelector->mnUpdateLockCount;
     341           0 : }
     342             : 
     343         161 : PageSelector::UpdateLock::UpdateLock (PageSelector& rSelector)
     344         161 :     : mpSelector(&rSelector)
     345             : {
     346         161 :     ++mpSelector->mnUpdateLockCount;
     347         161 : }
     348             : 
     349         161 : PageSelector::UpdateLock::~UpdateLock()
     350             : {
     351         161 :     Release();
     352         161 : }
     353             : 
     354         161 : void PageSelector::UpdateLock::Release()
     355             : {
     356         161 :     if (mpSelector != NULL)
     357             :     {
     358         161 :         --mpSelector->mnUpdateLockCount;
     359             :         OSL_ASSERT(mpSelector->mnUpdateLockCount >= 0);
     360         161 :         if (mpSelector->mnUpdateLockCount == 0)
     361         161 :             mpSelector->UpdateCurrentPage(true);
     362             : 
     363         161 :         mpSelector = NULL;
     364             :     }
     365         161 : }
     366             : 
     367             : //===== PageSelector::BroadcastLock ==============================================
     368             : 
     369           0 : PageSelector::BroadcastLock::BroadcastLock (SlideSorter& rSlideSorter)
     370           0 :     : mrSelector(rSlideSorter.GetController().GetPageSelector())
     371             : {
     372           0 :     mrSelector.DisableBroadcasting();
     373           0 : }
     374             : 
     375          97 : PageSelector::BroadcastLock::BroadcastLock (PageSelector& rSelector)
     376          97 :     : mrSelector(rSelector)
     377             : {
     378          97 :     mrSelector.DisableBroadcasting();
     379          97 : }
     380             : 
     381          97 : PageSelector::BroadcastLock::~BroadcastLock()
     382             : {
     383          97 :     mrSelector.EnableBroadcasting();
     384          97 : }
     385             : 
     386          66 : } } } // end of namespace ::sd::slidesorter::controller
     387             : 
     388             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11