LibreOffice Module sd (master) 1
SlsPageSelector.cxx
Go to the documentation of this file.
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
21
22#include <SlideSorter.hxx>
31#include <osl/diagnose.h>
32
33#include <sdpage.hxx>
34#include <tools/debug.hxx>
35#include <memory>
36
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::uno;
39using namespace ::sd::slidesorter::model;
40using namespace ::sd::slidesorter::view;
41
43
45 : mrModel(rSlideSorter.GetModel()),
46 mrSlideSorter(rSlideSorter),
48 mnSelectedPageCount(0),
49 mnBroadcastDisableLevel(0),
50 mbSelectionChangeBroadcastPending(false),
51 mnUpdateLockCount(0),
52 mbIsUpdateCurrentPagePending(true)
53{
55}
56
58{
60 PageSelector::UpdateLock aLock (*this);
61
62 int nPageCount = mrModel.GetPageCount();
63 for (int nPageIndex=0; nPageIndex<nPageCount; nPageIndex++)
64 SelectPage(nPageIndex);
65}
66
68{
70 PageSelector::UpdateLock aLock (*this);
71
72 int nPageCount = mrModel.GetPageCount();
73 for (int nPageIndex=0; nPageIndex<nPageCount; nPageIndex++)
74 DeselectPage(nPageIndex);
75
77 "PageSelector::DeselectAllPages: the selected pages counter is not 0");
79 mpSelectionAnchor.reset();
80}
81
83{
84 PageSelector::UpdateLock aLock (*this);
85
86 bool bSelectionHasChanged (true);
88 model::PageEnumeration aAllPages (
90 while (aAllPages.HasMoreElements())
91 {
92 model::SharedPageDescriptor pDescriptor (aAllPages.GetNextElement());
93 if (pDescriptor->GetCoreSelection())
94 {
97 bSelectionHasChanged = true;
98 }
99
100 if (pDescriptor->HasState(PageDescriptor::ST_Selected))
102 }
103
104 if (bSelectionHasChanged)
105 {
108 else
109 mrController.GetSelectionManager()->SelectionHasChanged();
110 }
111}
112
114{
115 model::PageEnumeration aAllPages (
117 while (aAllPages.HasMoreElements())
118 {
119 model::SharedPageDescriptor pDescriptor (aAllPages.GetNextElement());
120 pDescriptor->SetCoreSelection();
121 }
122}
123
124void PageSelector::SelectPage (int nPageIndex)
125{
126 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
127 if (pDescriptor)
128 SelectPage(pDescriptor);
129}
130
132{
133 const sal_Int32 nPageIndex (mrModel.GetIndex(pPage));
134 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
135 if (pDescriptor && pDescriptor->GetPage()==pPage)
136 SelectPage(pDescriptor);
137}
138
139void PageSelector::SelectPage (const SharedPageDescriptor& rpDescriptor)
140{
141 if (!rpDescriptor
142 || !mrSlideSorter.GetView().SetState(rpDescriptor, PageDescriptor::ST_Selected, true))
143 return;
144
147 mrSlideSorter.GetView().RequestRepaint(rpDescriptor);
148
149 mpMostRecentlySelectedPage = rpDescriptor;
150 if (mpSelectionAnchor == nullptr)
151 mpSelectionAnchor = rpDescriptor;
152
155 else
156 mrController.GetSelectionManager()->SelectionHasChanged();
158
160}
161
162void PageSelector::DeselectPage (int nPageIndex)
163{
164 model::SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
165 if (pDescriptor)
166 DeselectPage(pDescriptor);
167}
168
170 const SharedPageDescriptor& rpDescriptor,
171 const bool bUpdateCurrentPage)
172{
173 if (!rpDescriptor
174 || !mrSlideSorter.GetView().SetState(rpDescriptor, PageDescriptor::ST_Selected, false))
175 return;
176
179 mrSlideSorter.GetView().RequestRepaint(rpDescriptor);
180 if (mpMostRecentlySelectedPage == rpDescriptor)
184 else
185 mrController.GetSelectionManager()->SelectionHasChanged();
186 if (bUpdateCurrentPage)
188
190}
191
193{
194 int nSelectionCount (0);
195 for (int nPageIndex=0,nPageCount=mrModel.GetPageCount(); nPageIndex<nPageCount; nPageIndex++)
196 {
197 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
198 assert(pDescriptor);
199 if (pDescriptor->HasState(PageDescriptor::ST_Selected))
200 ++nSelectionCount;
201 }
202 if (nSelectionCount!=mnSelectedPageCount)
203 {
204 // #i120020# The former call to assert(..) internally calls
205 // SlideSorterModel::GetPageDescriptor which will crash in this situation
206 // (only in non-pro code). All what is wanted there is to assert it (the
207 // error is already detected), so do this directly.
208 OSL_ENSURE(false, "PageSelector: Consistency error (!)");
209 }
210}
211
213{
214 SharedPageDescriptor pDescriptor (mrModel.GetPageDescriptor(nPageIndex));
215 if (pDescriptor)
216 return pDescriptor->HasState(PageDescriptor::ST_Selected);
217 else
218 return false;
219}
220
222{
223 SharedPageDescriptor pDescriptor(mrModel.GetPageDescriptor(nPageIndex));
224 if (pDescriptor)
225 return pDescriptor->HasState(PageDescriptor::ST_Excluded);
226 else
227 return false;
228}
229
231{
232 return mrModel.GetPageCount();
233}
234
236{
238 model::PageEnumeration aSelectedPages (
240 while (aSelectedPages.HasMoreElements())
241 {
243 aSelectedPages.GetNextElement();
244 }
245}
246
248{
252 {
253 mrController.GetSelectionManager()->SelectionHasChanged();
255 }
256}
257
259{
261}
262
263std::shared_ptr<PageSelector::PageSelection> PageSelector::GetPageSelection() const
264{
265 auto pSelection = std::make_shared<PageSelection>();
266 pSelection->reserve(GetSelectedPageCount());
267
268 int nPageCount = GetPageCount();
269 for (int nIndex=0; nIndex<nPageCount; nIndex++)
270 {
272 if (pDescriptor && pDescriptor->HasState(PageDescriptor::ST_Selected))
273 pSelection->push_back(pDescriptor->GetPage());
274 }
275
276 return pSelection;
277}
278
280 const std::shared_ptr<PageSelection>& rpSelection,
281 const bool bUpdateCurrentPage)
282{
283 for (const auto& rpPage : *rpSelection)
284 SelectPage(rpPage);
285 if (bUpdateCurrentPage)
287}
288
289void PageSelector::UpdateCurrentPage (const bool bUpdateOnlyWhenPending)
290{
291 if (mnUpdateLockCount > 0)
292 {
294 return;
295 }
296
297 if ( ! mbIsUpdateCurrentPagePending && bUpdateOnlyWhenPending)
298 return;
299
301
302 // Make the first selected page the current page.
303 SharedPageDescriptor pCurrentPageDescriptor;
304 const sal_Int32 nPageCount (GetPageCount());
305 for (sal_Int32 nIndex=0; nIndex<nPageCount; ++nIndex)
306 {
308 if ( ! pDescriptor)
309 continue;
310 if (pDescriptor->HasState(PageDescriptor::ST_Selected))
311 {
312 pCurrentPageDescriptor = pDescriptor;
313 break;
314 }
315 }
316
317 if (!pCurrentPageDescriptor)
318 return;
319
320 // Switching the current slide normally sets also the
321 // selection to just the new current slide. To prevent that,
322 // we store (and at the end of this scope restore) the current
323 // selection.
324 std::shared_ptr<PageSelection> pSelection (GetPageSelection());
325
326 mrController.GetCurrentSlideManager()->SwitchCurrentSlide(pCurrentPageDescriptor);
327
328 // Restore the selection and prevent a recursive call to
329 // UpdateCurrentPage().
330 SetPageSelection(pSelection, false);
331}
332
333//===== PageSelector::UpdateLock ==============================================
334
336 : mpSelector(&rSlideSorter.GetController().GetPageSelector())
337{
339}
340
342 : mpSelector(&rSelector)
343{
345}
346
348{
349 Release();
350}
351
353{
354 if (mpSelector != nullptr)
355 {
356 --mpSelector->mnUpdateLockCount;
357 OSL_ASSERT(mpSelector->mnUpdateLockCount >= 0);
358 if (mpSelector->mnUpdateLockCount == 0)
359 mpSelector->UpdateCurrentPage(true);
360
361 mpSelector = nullptr;
362 }
363}
364
365//===== PageSelector::BroadcastLock ==============================================
366
368 : mrSelector(rSlideSorter.GetController().GetPageSelector())
369{
371}
372
374 : mrSelector(rSelector)
375{
377}
378
380{
381 mrSelector.EnableBroadcasting();
382}
383
384} // end of namespace ::sd::slidesorter::controller
385
386/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ChartModel & mrModel
SlideSorterController & mrController
SlideSorter & mrSlideSorter
Show previews for all the slides in a document and allow the user to insert or delete slides and modi...
Definition: SlideSorter.hxx:62
SD_DLLPUBLIC controller::SlideSorterController & GetController() const
view::SlideSorterView & GetView() const
Use the UpdateLock whenever you do a complex selection, i.e.
A sub-controller that handles page selection of the slide browser.
void DisableBroadcasting()
Disable the broadcasting of selection change events.
void EnableBroadcasting()
Enable the broadcasting of selection change events.
SD_DLLPUBLIC bool IsPageExcluded(int nPageIndex)
Return whether the specified page is excluded.
void DeselectPage(int nPageIndex)
Deselect the descriptor that is associated with the given page.
void UpdateCurrentPage(const bool bUpdateOnlyWhenPending=false)
void SetCoreSelection()
Update the selection state of the SdPage objects to be the same as that of the corresponding page des...
SD_DLLPUBLIC bool IsPageSelected(int nPageIndex)
Return whether the specified page is selected.
model::SharedPageDescriptor mpSelectionAnchor
Anchor for a range selection.
model::SharedPageDescriptor mpMostRecentlySelectedPage
void GetCoreSelection()
Update the selection state of all page descriptors to be the same as that of the corresponding pages ...
void SetPageSelection(const std::shared_ptr< PageSelection > &rSelection, const bool bUpdateCurrentPage)
Restore a page selection according to the given selection object.
std::shared_ptr< PageSelection > GetPageSelection() const
Return an object that describes the current selection.
void CountSelectedPages()
Call this method after the model has changed to set the number of selected pages.
int GetPageCount() const
This convenience method returns the same number of pages that SlideSorterModel.GetPageCount() returns...
void SelectPage(int nPageIndex)
Select the specified descriptor.
std::shared_ptr< CurrentSlideManager > const & GetCurrentSlideManager() const
std::shared_ptr< SelectionManager > const & GetSelectionManager() const
Temporarily disable the update of the visible area.
void RequestVisible(const model::SharedPageDescriptor &rpDescriptor, const bool bForce=false)
Request to make the specified page object visible.
static PageEnumeration CreateAllPagesEnumeration(const SlideSorterModel &rModel)
The returned enumeration of slides iterates over all slides of the given model.
static PageEnumeration CreateSelectedPagesEnumeration(const SlideSorterModel &rModel)
The returned enumeration of slides iterates over the currently selected slides of the given model.
Public class of page enumerations that delegates its calls to an implementation object that can filte...
virtual SharedPageDescriptor GetNextElement() override
Return the next element of the enumeration.
virtual bool HasMoreElements() const override
Return <TRUE> when the enumeration has more elements, i.e.
sal_Int32 GetPageCount() const
Return the number of slides in the document regardless of whether they are visible or not or whether ...
SharedPageDescriptor GetPageDescriptor(const sal_Int32 nPageIndex, const bool bCreate=true) const
Return a page descriptor for the page with the specified index.
sal_Int32 GetIndex(const css::uno::Reference< css::drawing::XDrawPage > &rxSlide) const
Return a page descriptor for the given XDrawPage.
bool SetState(const model::SharedPageDescriptor &rpDescriptor, const model::PageDescriptor::State eState, const bool bStateValue)
#define DBG_ASSERT(sCon, aError)
virtual std::shared_ptr< SfxDialogController > GetController() override
sal_Int32 nIndex
std::shared_ptr< PageDescriptor > SharedPageDescriptor