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