LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/controller - SlsSelectionManager.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 39 119 32.8 %
Date: 2014-11-03 Functions: 7 12 58.3 %
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/SlsSelectionManager.hxx"
      21             : 
      22             : #include "SlideSorter.hxx"
      23             : #include "SlsCommand.hxx"
      24             : #include "controller/SlideSorterController.hxx"
      25             : #include "controller/SlsAnimator.hxx"
      26             : #include "controller/SlsAnimationFunction.hxx"
      27             : #include "controller/SlsCurrentSlideManager.hxx"
      28             : #include "controller/SlsFocusManager.hxx"
      29             : #include "controller/SlsPageSelector.hxx"
      30             : #include "controller/SlsProperties.hxx"
      31             : #include "controller/SlsScrollBarManager.hxx"
      32             : #include "controller/SlsSlotManager.hxx"
      33             : #include "controller/SlsSelectionObserver.hxx"
      34             : #include "model/SlideSorterModel.hxx"
      35             : #include "model/SlsPageEnumerationProvider.hxx"
      36             : #include "model/SlsPageDescriptor.hxx"
      37             : #include "view/SlideSorterView.hxx"
      38             : #include "view/SlsLayouter.hxx"
      39             : #include "drawdoc.hxx"
      40             : #include "Window.hxx"
      41             : #include <svx/svxids.hrc>
      42             : #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
      43             : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
      44             : 
      45             : #include "res_bmp.hrc"
      46             : #include "sdresid.hxx"
      47             : #include "strings.hrc"
      48             : #include "app.hrc"
      49             : #include "glob.hrc"
      50             : 
      51             : using namespace ::com::sun::star;
      52             : using namespace ::com::sun::star::drawing;
      53             : using namespace ::com::sun::star::uno;
      54             : using namespace ::sd::slidesorter::model;
      55             : using namespace ::sd::slidesorter::view;
      56             : using namespace ::sd::slidesorter::controller;
      57             : 
      58             : namespace sd { namespace slidesorter { namespace controller {
      59             : 
      60             : class SelectionManager::PageInsertionListener
      61             :     : public SfxListener
      62             : {
      63             : public:
      64             : 
      65             : };
      66             : 
      67         126 : SelectionManager::SelectionManager (SlideSorter& rSlideSorter)
      68             :     : mrSlideSorter(rSlideSorter),
      69         126 :       mrController(rSlideSorter.GetController()),
      70             :       maSelectionBeforeSwitch(),
      71             :       mbIsMakeSelectionVisiblePending(true),
      72             :       mnInsertionPosition(-1),
      73             :       mnAnimationId(Animator::NotAnAnimationId),
      74             :       mpPageInsertionListener(),
      75         252 :       mpSelectionObserver(new SelectionObserver(rSlideSorter))
      76             : {
      77         126 : }
      78             : 
      79         252 : SelectionManager::~SelectionManager (void)
      80             : {
      81         126 :     if (mnAnimationId != Animator::NotAnAnimationId)
      82           0 :         mrController.GetAnimator()->RemoveAnimation(mnAnimationId);
      83         126 : }
      84             : 
      85           0 : void SelectionManager::DeleteSelectedPages (const bool bSelectFollowingPage)
      86             : {
      87             :     // Create some locks to prevent updates of the model, view, selection
      88             :     // state while modifying any of them.
      89           0 :     SlideSorterController::ModelChangeLock aLock (mrController);
      90           0 :     SlideSorterView::DrawLock aDrawLock (mrSlideSorter);
      91           0 :     PageSelector::UpdateLock aSelectionLock (mrSlideSorter);
      92             : 
      93             :     // Hide focus.
      94           0 :     bool bIsFocusShowing = mrController.GetFocusManager().IsFocusShowing();
      95           0 :     if (bIsFocusShowing)
      96           0 :         mrController.GetFocusManager().ToggleFocus();
      97             : 
      98             :     // Store pointers to all selected page descriptors.  This is necessary
      99             :     // because the pages get deselected when the first one is deleted.
     100             :     model::PageEnumeration aPageEnumeration (
     101           0 :         PageEnumerationProvider::CreateSelectedPagesEnumeration(mrSlideSorter.GetModel()));
     102           0 :     ::std::vector<SdPage*> aSelectedPages;
     103           0 :     sal_Int32 nNewCurrentSlide (-1);
     104           0 :     while (aPageEnumeration.HasMoreElements())
     105             :     {
     106           0 :         SharedPageDescriptor pDescriptor (aPageEnumeration.GetNextElement());
     107           0 :         aSelectedPages.push_back(pDescriptor->GetPage());
     108           0 :         if (bSelectFollowingPage || nNewCurrentSlide<0)
     109           0 :             nNewCurrentSlide = pDescriptor->GetPageIndex();
     110           0 :     }
     111           0 :     if (aSelectedPages.empty())
     112           0 :         return;
     113             : 
     114             :     // Determine the slide to select (and thereby make the current slide)
     115             :     // after the deletion.
     116           0 :     if (bSelectFollowingPage)
     117           0 :         nNewCurrentSlide -= aSelectedPages.size() - 1;
     118             :     else
     119           0 :         --nNewCurrentSlide;
     120             : 
     121             :     // The actual deletion of the selected pages is done in one of two
     122             :     // helper functions.  They are specialized for normal respectively for
     123             :     // master pages.
     124           0 :     mrSlideSorter.GetView().BegUndo (SdResId(STR_UNDO_DELETEPAGES));
     125           0 :     if (mrSlideSorter.GetModel().GetEditMode() == EM_PAGE)
     126           0 :         DeleteSelectedNormalPages(aSelectedPages);
     127             :     else
     128           0 :         DeleteSelectedMasterPages(aSelectedPages);
     129           0 :     mrSlideSorter.GetView().EndUndo ();
     130             : 
     131           0 :     mrController.HandleModelChange();
     132           0 :     aLock.Release();
     133             : 
     134             :     // Show focus and move it to next valid location.
     135           0 :     if (bIsFocusShowing)
     136           0 :         mrController.GetFocusManager().ToggleFocus();
     137             : 
     138             :     // Set the new current slide.
     139           0 :     if (nNewCurrentSlide < 0)
     140           0 :         nNewCurrentSlide = 0;
     141           0 :     else if (nNewCurrentSlide >= mrSlideSorter.GetModel().GetPageCount())
     142           0 :         nNewCurrentSlide = mrSlideSorter.GetModel().GetPageCount()-1;
     143           0 :     mrController.GetPageSelector().CountSelectedPages();
     144           0 :     mrController.GetPageSelector().SelectPage(nNewCurrentSlide);
     145           0 :     mrController.GetFocusManager().SetFocusedPage(nNewCurrentSlide);
     146             : }
     147             : 
     148           0 : void SelectionManager::DeleteSelectedNormalPages (const ::std::vector<SdPage*>& rSelectedPages)
     149             : {
     150             :     // Prepare the deletion via the UNO API.
     151             :     OSL_ASSERT(mrSlideSorter.GetModel().GetEditMode() == EM_PAGE);
     152             : 
     153             :     try
     154             :     {
     155           0 :         Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier( mrSlideSorter.GetModel().GetDocument()->getUnoModel(), UNO_QUERY_THROW );
     156           0 :         Reference<drawing::XDrawPages> xPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
     157             : 
     158             :         // Iterate over all pages that where seleted when this method was called
     159             :         // and delete the draw page the notes page.  The iteration is done in
     160             :         // reverse order so that when one slide is not deleted (to avoid an
     161             :         // empty document) the remaining slide is the first one.
     162           0 :         ::std::vector<SdPage*>::const_reverse_iterator aI;
     163           0 :         for (aI=rSelectedPages.rbegin(); aI!=rSelectedPages.rend(); ++aI)
     164             :         {
     165             :             // Do not delete the last slide in the document.
     166           0 :             if (xPages->getCount() <= 1)
     167           0 :                 break;
     168             : 
     169           0 :             const sal_uInt16 nPage (model::FromCoreIndex((*aI)->GetPageNum()));
     170             : 
     171           0 :             Reference< XDrawPage > xPage( xPages->getByIndex( nPage ), UNO_QUERY_THROW );
     172           0 :             xPages->remove(xPage);
     173           0 :         }
     174             :     }
     175           0 :     catch( Exception& )
     176             :     {
     177             :         OSL_FAIL("SelectionManager::DeleteSelectedNormalPages(), exception caught!");
     178             :     }
     179           0 : }
     180             : 
     181           0 : void SelectionManager::DeleteSelectedMasterPages (const ::std::vector<SdPage*>& rSelectedPages)
     182             : {
     183             :     // Prepare the deletion via the UNO API.
     184             :     OSL_ASSERT(mrSlideSorter.GetModel().GetEditMode() == EM_MASTERPAGE);
     185             : 
     186             :     try
     187             :     {
     188           0 :         Reference<drawing::XMasterPagesSupplier> xDrawPagesSupplier( mrSlideSorter.GetModel().GetDocument()->getUnoModel(), UNO_QUERY_THROW );
     189           0 :         Reference<drawing::XDrawPages> xPages( xDrawPagesSupplier->getMasterPages(), UNO_QUERY_THROW );
     190             : 
     191             :         // Iterate over all pages that where seleted when this method was called
     192             :         // and delete the draw page the notes page.  The iteration is done in
     193             :         // reverse order so that when one slide is not deleted (to avoid an
     194             :         // empty document) the remaining slide is the first one.
     195           0 :         ::std::vector<SdPage*>::const_reverse_iterator aI;
     196           0 :         for (aI=rSelectedPages.rbegin(); aI!=rSelectedPages.rend(); ++aI)
     197             :         {
     198             :             // Do not delete the last slide in the document.
     199           0 :             if (xPages->getCount() <= 1)
     200           0 :                 break;
     201             : 
     202           0 :             const sal_uInt16 nPage (model::FromCoreIndex((*aI)->GetPageNum()));
     203             : 
     204           0 :             Reference< XDrawPage > xPage( xPages->getByIndex( nPage ), UNO_QUERY_THROW );
     205           0 :             xPages->remove(xPage);
     206           0 :         }
     207             :     }
     208           0 :     catch( Exception& )
     209             :     {
     210             :         OSL_FAIL("SelectionManager::DeleteSelectedMasterPages(), exception caught!");
     211             :     }
     212           0 : }
     213             : 
     214         386 : void SelectionManager::SelectionHasChanged (const bool bMakeSelectionVisible)
     215             : {
     216         386 :     if (bMakeSelectionVisible)
     217         386 :         mbIsMakeSelectionVisiblePending = true;
     218             : 
     219         386 :     ViewShell* pViewShell = mrSlideSorter.GetViewShell();
     220         386 :     if (pViewShell != NULL)
     221             :     {
     222         386 :         pViewShell->Invalidate (SID_EXPAND_PAGE);
     223         386 :         pViewShell->Invalidate (SID_SUMMARY_PAGE);
     224         386 :         pViewShell->Invalidate(SID_SHOW_SLIDE);
     225         386 :         pViewShell->Invalidate(SID_HIDE_SLIDE);
     226         386 :         pViewShell->Invalidate(SID_DELETE_PAGE);
     227         386 :         pViewShell->Invalidate(SID_DELETE_MASTER_PAGE);
     228         386 :         pViewShell->Invalidate(SID_ASSIGN_LAYOUT);
     229             : 
     230             :         // StatusBar
     231         386 :         pViewShell->Invalidate (SID_STATUS_PAGE);
     232         386 :         pViewShell->Invalidate (SID_STATUS_LAYOUT);
     233             : 
     234             :         OSL_ASSERT(mrController.GetCurrentSlideManager());
     235         386 :         SharedPageDescriptor pDescriptor(mrController.GetCurrentSlideManager()->GetCurrentSlide());
     236         386 :         if (pDescriptor.get() != NULL)
     237         134 :             pViewShell->UpdatePreview(pDescriptor->GetPage());
     238             : 
     239             :         // Tell the slection change listeners that the selection has changed.
     240         386 :         ::std::vector<Link>::iterator iListener (maSelectionChangeListeners.begin());
     241         386 :         ::std::vector<Link>::iterator iEnd (maSelectionChangeListeners.end());
     242         394 :         for (; iListener!=iEnd; ++iListener)
     243             :         {
     244           8 :             iListener->Call(NULL);
     245             :         }
     246             : 
     247             :         // Reset the insertion position: until set again it is calculated from
     248             :         // the current selection.
     249         386 :         mnInsertionPosition = -1;
     250             :     }
     251         386 : }
     252             : 
     253         126 : void SelectionManager::AddSelectionChangeListener (const Link& rListener)
     254             : {
     255         252 :     if (::std::find (
     256             :         maSelectionChangeListeners.begin(),
     257             :         maSelectionChangeListeners.end(),
     258         252 :         rListener) == maSelectionChangeListeners.end())
     259             :     {
     260         126 :         maSelectionChangeListeners.push_back (rListener);
     261             :     }
     262         126 : }
     263             : 
     264         126 : void SelectionManager::RemoveSelectionChangeListener(const Link&rListener)
     265             : {
     266             :     maSelectionChangeListeners.erase (
     267             :         ::std::find (
     268             :             maSelectionChangeListeners.begin(),
     269             :             maSelectionChangeListeners.end(),
     270         126 :             rListener));
     271         126 : }
     272             : 
     273           0 : sal_Int32 SelectionManager::GetInsertionPosition (void) const
     274             : {
     275           0 :     sal_Int32 nInsertionPosition (mnInsertionPosition);
     276           0 :     if (nInsertionPosition < 0)
     277             :     {
     278             :         model::PageEnumeration aSelectedPages
     279             :             (model::PageEnumerationProvider::CreateSelectedPagesEnumeration(
     280           0 :                 mrSlideSorter.GetModel()));
     281             :         // Initialize (for the case of an empty selection) with the position
     282             :         // at the end of the document.
     283           0 :         nInsertionPosition = mrSlideSorter.GetModel().GetPageCount();
     284           0 :         while (aSelectedPages.HasMoreElements())
     285             :         {
     286           0 :             const sal_Int32 nPosition (aSelectedPages.GetNextElement()->GetPage()->GetPageNum());
     287             :             // Convert *2+1 index to straight index (n-1)/2 after the page
     288             :             // (+1).
     289           0 :             nInsertionPosition = model::FromCoreIndex(nPosition) + 1;
     290           0 :         }
     291             : 
     292             :     }
     293           0 :     return nInsertionPosition;
     294             : }
     295             : 
     296           0 : void SelectionManager::SetInsertionPosition (const sal_Int32 nInsertionPosition)
     297             : {
     298           0 :     if (nInsertionPosition < 0)
     299           0 :         mnInsertionPosition = -1;
     300           0 :     else if (nInsertionPosition > mrSlideSorter.GetModel().GetPageCount())
     301             :     {
     302             :         // Assert but then ignore invalid values.
     303             :         OSL_ASSERT(nInsertionPosition<=mrSlideSorter.GetModel().GetPageCount());
     304           0 :         return;
     305             :     }
     306             :     else
     307           0 :         mnInsertionPosition = nInsertionPosition;
     308             : }
     309             : 
     310         114 : } } } // end of namespace ::sd::slidesorter
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10