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 <controller/SlsInsertionIndicatorHandler.hxx>
21 #include <controller/SlsProperties.hxx>
22 #include <view/SlideSorterView.hxx>
23 #include <view/SlsLayouter.hxx>
24 #include <view/SlsInsertAnimator.hxx>
25 #include <view/SlsInsertionIndicatorOverlay.hxx>
26 #include <model/SlideSorterModel.hxx>
27 #include <model/SlsPageEnumerationProvider.hxx>
28 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
30 #include <SlideSorter.hxx>
32 using namespace ::com::sun::star::datatransfer::dnd::DNDConstants
;
34 namespace sd
{ namespace slidesorter
{ namespace controller
{
36 InsertionIndicatorHandler::InsertionIndicatorHandler (SlideSorter
& rSlideSorter
)
37 : mrSlideSorter(rSlideSorter
),
39 mpInsertionIndicatorOverlay(new view::InsertionIndicatorOverlay(rSlideSorter
)),
42 mbIsInsertionTrivial(false),
44 mbIsReadOnly(mrSlideSorter
.GetModel().IsReadOnly()),
45 mbIsOverSourceView(true),
51 InsertionIndicatorHandler::~InsertionIndicatorHandler() COVERITY_NOEXCEPT_FALSE
55 void InsertionIndicatorHandler::Start (const bool bIsOverSourceView
)
59 OSL_ASSERT(!mbIsActive
);
62 mbIsReadOnly
= mrSlideSorter
.GetModel().IsReadOnly();
67 mbIsOverSourceView
= bIsOverSourceView
;
70 void InsertionIndicatorHandler::End (const controller::Animator::AnimationMode eMode
)
72 if (mbIsForcedShow
|| ! mbIsActive
|| mbIsReadOnly
)
75 GetInsertAnimator()->Reset(eMode
);
78 // maInsertPosition = view::InsertPosition();
81 mpInsertionIndicatorOverlay
->Hide();
82 mpInsertionIndicatorOverlay
.reset(new view::InsertionIndicatorOverlay(mrSlideSorter
));
85 void InsertionIndicatorHandler::ForceShow()
87 mbIsForcedShow
= true;
90 void InsertionIndicatorHandler::ForceEnd()
92 mbIsForcedShow
= false;
93 End(Animator::AM_Immediate
);
96 void InsertionIndicatorHandler::UpdateIndicatorIcon (const SdTransferable
* pTransferable
)
98 mpInsertionIndicatorOverlay
->Create(pTransferable
);
99 maIconSize
= mpInsertionIndicatorOverlay
->GetSize();
102 InsertionIndicatorHandler::Mode
InsertionIndicatorHandler::GetModeFromDndAction (
103 const sal_Int8 nDndAction
)
105 if ((nDndAction
& ACTION_MOVE
) != 0)
107 else if ((nDndAction
& ACTION_COPY
) != 0)
113 void InsertionIndicatorHandler::UpdatePosition (
114 const Point
& rMouseModelPosition
,
123 SetPosition(rMouseModelPosition
, eMode
);
126 void InsertionIndicatorHandler::UpdatePosition (
127 const Point
& rMouseModelPosition
,
128 const sal_Int8 nDndAction
)
130 UpdatePosition(rMouseModelPosition
, GetModeFromDndAction(nDndAction
));
133 sal_Int32
InsertionIndicatorHandler::GetInsertionPageIndex() const
138 return maInsertPosition
.GetIndex();
141 void InsertionIndicatorHandler::SetPosition (
145 view::Layouter
& rLayouter (mrSlideSorter
.GetView().GetLayouter());
147 const view::InsertPosition
aInsertPosition (rLayouter
.GetInsertPosition(
150 mrSlideSorter
.GetModel()));
152 if (maInsertPosition
== aInsertPosition
&& meMode
== eMode
)
155 maInsertPosition
= aInsertPosition
;
157 mbIsInsertionTrivial
= IsInsertionTrivial(maInsertPosition
.GetIndex(), eMode
);
158 if (maInsertPosition
.GetIndex()>=0 && ! mbIsInsertionTrivial
)
160 mpInsertionIndicatorOverlay
->SetLocation(maInsertPosition
.GetLocation());
162 GetInsertAnimator()->SetInsertPosition(maInsertPosition
);
163 mpInsertionIndicatorOverlay
->Show();
167 GetInsertAnimator()->Reset(Animator::AM_Animated
);
168 mpInsertionIndicatorOverlay
->Hide();
172 std::shared_ptr
<view::InsertAnimator
> const & InsertionIndicatorHandler::GetInsertAnimator()
174 if ( ! mpInsertAnimator
)
175 mpInsertAnimator
.reset(new view::InsertAnimator(mrSlideSorter
));
176 return mpInsertAnimator
;
179 bool InsertionIndicatorHandler::IsInsertionTrivial (
180 const sal_Int32 nInsertionIndex
,
181 const Mode eMode
) const
183 if (eMode
== CopyMode
)
185 else if (eMode
== UnknownMode
)
188 if ( ! mbIsOverSourceView
)
191 // Iterate over all selected pages and check whether there are
192 // holes. While we do this we remember the indices of the first and
193 // last selected page as preparation for the next step.
194 sal_Int32 nCurrentIndex
= -1;
195 sal_Int32 nFirstIndex
= -1;
196 sal_Int32 nLastIndex
= -1;
197 model::PageEnumeration
aSelectedPages (
198 model::PageEnumerationProvider::CreateSelectedPagesEnumeration(
199 mrSlideSorter
.GetModel()));
200 while (aSelectedPages
.HasMoreElements())
202 model::SharedPageDescriptor
pDescriptor (aSelectedPages
.GetNextElement());
204 // Get the page number and compare it to the last one.
205 const sal_Int32
nPageNumber (pDescriptor
->GetPageIndex());
206 if (nCurrentIndex
>=0 && nPageNumber
>(nCurrentIndex
+1))
209 nCurrentIndex
= nPageNumber
;
211 // Remember indices of the first and last page of the selection.
212 if (nFirstIndex
== -1)
213 nFirstIndex
= nPageNumber
;
214 nLastIndex
= nPageNumber
;
217 // When we come here then the selection has no holes. We still have
218 // to check that the insertion position is not directly in front or
219 // directly behind the selection and thus moving the selection there
220 // would not change the model.
221 return nInsertionIndex
>= nFirstIndex
&& nInsertionIndex
<= (nLastIndex
+1);
224 bool InsertionIndicatorHandler::IsInsertionTrivial (const sal_Int8 nDndAction
)
226 return IsInsertionTrivial(GetInsertionPageIndex(), GetModeFromDndAction(nDndAction
));
229 //===== InsertionIndicatorHandler::ForceShowContext ===========================
231 InsertionIndicatorHandler::ForceShowContext::ForceShowContext (
232 const std::shared_ptr
<InsertionIndicatorHandler
>& rpHandler
)
233 : mpHandler(rpHandler
)
235 mpHandler
->ForceShow();
238 InsertionIndicatorHandler::ForceShowContext::~ForceShowContext() COVERITY_NOEXCEPT_FALSE
240 mpHandler
->ForceEnd();
243 } } } // end of namespace ::sd::slidesorter::controller
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */