1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <SlideSorter.hxx>
21 #include <controller/SlideSorterController.hxx>
22 #include <controller/SlsSelectionManager.hxx>
23 #include <controller/SlsSelectionObserver.hxx>
24 #include <controller/SlsPageSelector.hxx>
25 #include <controller/SlsFocusManager.hxx>
27 #include <osl/diagnose.h>
29 namespace sd::slidesorter::controller
31 SelectionObserver::Context::Context(SlideSorter
const& rSlideSorter
)
32 : mpSelectionObserver(
33 rSlideSorter
.GetController().GetSelectionManager()->GetSelectionObserver())
35 if (mpSelectionObserver
)
36 mpSelectionObserver
->StartObservation();
39 SelectionObserver::Context::~Context() COVERITY_NOEXCEPT_FALSE
41 if (mpSelectionObserver
)
42 mpSelectionObserver
->EndObservation();
45 void SelectionObserver::Context::Abort()
47 if (mpSelectionObserver
)
49 mpSelectionObserver
->AbortObservation();
50 mpSelectionObserver
.reset();
54 //===== SelectionObserver =====================================================
56 SelectionObserver::SelectionObserver(SlideSorter
& rSlideSorter
)
57 : mrSlideSorter(rSlideSorter
)
58 , mbIsObservationActive(false)
59 , mbPageEventOccurred(false)
63 SelectionObserver::~SelectionObserver() {}
65 void SelectionObserver::NotifyPageEvent(const SdrPage
* pSdrPage
)
67 if (!mbIsObservationActive
)
70 mbPageEventOccurred
= true;
72 const SdPage
* pPage
= dynamic_cast<const SdPage
*>(pSdrPage
);
76 //NotifyPageEvent is called for add, remove, *and* change position so for
77 //the change position case we must ensure we don't end up with the slide
78 //duplicated in our list
79 std::vector
<const SdPage
*>::iterator
iPage(
80 std::find(maInsertedPages
.begin(), maInsertedPages
.end(), pPage
));
81 if (iPage
!= maInsertedPages
.end())
82 maInsertedPages
.erase(iPage
);
84 if (pPage
->IsInserted())
85 maInsertedPages
.push_back(pPage
);
88 void SelectionObserver::StartObservation()
90 OSL_ASSERT(!mbIsObservationActive
);
91 maInsertedPages
.clear();
92 mbIsObservationActive
= true;
95 void SelectionObserver::AbortObservation()
97 OSL_ASSERT(mbIsObservationActive
);
98 mbIsObservationActive
= false;
99 maInsertedPages
.clear();
102 void SelectionObserver::EndObservation()
104 OSL_ASSERT(mbIsObservationActive
);
105 mbIsObservationActive
= false;
107 if (!mbPageEventOccurred
)
110 PageSelector
& rSelector(mrSlideSorter
.GetController().GetPageSelector());
111 PageSelector::UpdateLock
aUpdateLock(mrSlideSorter
);
112 rSelector
.DeselectAllPages();
113 if (!maInsertedPages
.empty())
115 // Select the inserted pages.
116 for (const auto& rpPage
: maInsertedPages
)
118 rSelector
.SelectPage(rpPage
);
120 maInsertedPages
.clear();
123 aUpdateLock
.Release();
124 FocusManager
& rFocusManager
= mrSlideSorter
.GetController().GetFocusManager();
125 bool bSuccess
= rFocusManager
.SetFocusedPageFromCurrentPage();
126 // tdf#129346 nothing currently selected, select something, if possible
127 // but (tdf#129346) only if setting focus to current page failed
128 if (rSelector
.GetPageCount() && rSelector
.GetSelectedPageCount() == 0)
131 rSelector
.SelectPage(rFocusManager
.GetFocusedPageDescriptor());
133 rSelector
.SelectPage(0);
137 } // end of namespace ::sd::slidesorter::controller
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */