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