bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsInsertionIndicatorHandler.cxx
blob42b932e640a819e6f04614276524013f3819dd84
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/SlsInsertionIndicatorOverlay.hxx"
25 #include "model/SlideSorterModel.hxx"
26 #include "model/SlsPageEnumerationProvider.hxx"
27 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
29 #include "SlideSorter.hxx"
31 using namespace ::com::sun::star::datatransfer::dnd::DNDConstants;
33 namespace sd { namespace slidesorter { namespace controller {
35 InsertionIndicatorHandler::InsertionIndicatorHandler (SlideSorter& rSlideSorter)
36 : mrSlideSorter(rSlideSorter),
37 mpInsertAnimator(),
38 mpInsertionIndicatorOverlay(new view::InsertionIndicatorOverlay(rSlideSorter)),
39 maInsertPosition(),
40 meMode(MoveMode),
41 mbIsInsertionTrivial(false),
42 mbIsActive(false),
43 mbIsReadOnly(mrSlideSorter.GetModel().IsReadOnly()),
44 mbIsOverSourceView(true),
45 maIconSize(0,0),
46 mbIsForcedShow(false)
50 InsertionIndicatorHandler::~InsertionIndicatorHandler()
54 void InsertionIndicatorHandler::Start (const bool bIsOverSourceView)
56 if (mbIsActive)
58 OSL_ASSERT(!mbIsActive);
61 mbIsReadOnly = mrSlideSorter.GetModel().IsReadOnly();
62 if (mbIsReadOnly)
63 return;
65 mbIsActive = true;
66 mbIsOverSourceView = bIsOverSourceView;
69 void InsertionIndicatorHandler::End (const controller::Animator::AnimationMode eMode)
71 if (mbIsForcedShow || ! mbIsActive || mbIsReadOnly)
72 return;
74 GetInsertAnimator()->Reset(eMode);
76 mbIsActive = false;
77 // maInsertPosition = view::InsertPosition();
78 meMode = UnknownMode;
80 mpInsertionIndicatorOverlay->Hide();
81 mpInsertionIndicatorOverlay.reset(new view::InsertionIndicatorOverlay(mrSlideSorter));
84 void InsertionIndicatorHandler::ForceShow()
86 mbIsForcedShow = true;
89 void InsertionIndicatorHandler::ForceEnd()
91 mbIsForcedShow = false;
92 End(Animator::AM_Immediate);
95 void InsertionIndicatorHandler::UpdateIndicatorIcon (const SdTransferable* pTransferable)
97 mpInsertionIndicatorOverlay->Create(pTransferable);
98 maIconSize = mpInsertionIndicatorOverlay->GetSize();
101 InsertionIndicatorHandler::Mode InsertionIndicatorHandler::GetModeFromDndAction (
102 const sal_Int8 nDndAction)
104 if ((nDndAction & ACTION_MOVE) != 0)
105 return MoveMode;
106 else if ((nDndAction & ACTION_COPY) != 0)
107 return CopyMode;
108 else
109 return UnknownMode;
112 void InsertionIndicatorHandler::UpdatePosition (
113 const Point& rMouseModelPosition,
114 const Mode eMode)
116 if ( ! mbIsActive)
117 return;
119 if (mbIsReadOnly)
120 return;
122 SetPosition(rMouseModelPosition, eMode);
125 void InsertionIndicatorHandler::UpdatePosition (
126 const Point& rMouseModelPosition,
127 const sal_Int8 nDndAction)
129 UpdatePosition(rMouseModelPosition, GetModeFromDndAction(nDndAction));
132 sal_Int32 InsertionIndicatorHandler::GetInsertionPageIndex() const
134 if (mbIsReadOnly)
135 return -1;
136 else
137 return maInsertPosition.GetIndex();
140 void InsertionIndicatorHandler::SetPosition (
141 const Point& rPoint,
142 const Mode eMode)
144 view::Layouter& rLayouter (mrSlideSorter.GetView().GetLayouter());
146 const view::InsertPosition aInsertPosition (rLayouter.GetInsertPosition(
147 rPoint,
148 maIconSize,
149 mrSlideSorter.GetModel()));
151 if (maInsertPosition != aInsertPosition
152 || meMode != eMode
153 // || ! mpInsertionIndicatorOverlay->IsVisible()
156 maInsertPosition = aInsertPosition;
157 meMode = eMode;
158 mbIsInsertionTrivial = IsInsertionTrivial(maInsertPosition.GetIndex(), eMode);
159 if (maInsertPosition.GetIndex()>=0 && ! mbIsInsertionTrivial)
161 mpInsertionIndicatorOverlay->SetLocation(maInsertPosition.GetLocation());
163 GetInsertAnimator()->SetInsertPosition(maInsertPosition);
164 mpInsertionIndicatorOverlay->Show();
166 else
168 GetInsertAnimator()->Reset(Animator::AM_Animated);
169 mpInsertionIndicatorOverlay->Hide();
174 ::boost::shared_ptr<view::InsertAnimator> InsertionIndicatorHandler::GetInsertAnimator()
176 if ( ! mpInsertAnimator)
177 mpInsertAnimator.reset(new view::InsertAnimator(mrSlideSorter));
178 return mpInsertAnimator;
181 bool InsertionIndicatorHandler::IsInsertionTrivial (
182 const sal_Int32 nInsertionIndex,
183 const Mode eMode) const
185 if (eMode == CopyMode)
186 return false;
187 else if (eMode == UnknownMode)
188 return true;
190 if ( ! mbIsOverSourceView)
191 return false;
193 // Iterate over all selected pages and check whether there are
194 // holes. While we do this we remember the indices of the first and
195 // last selected page as preparation for the next step.
196 sal_Int32 nCurrentIndex = -1;
197 sal_Int32 nFirstIndex = -1;
198 sal_Int32 nLastIndex = -1;
199 model::PageEnumeration aSelectedPages (
200 model::PageEnumerationProvider::CreateSelectedPagesEnumeration(
201 mrSlideSorter.GetModel()));
202 while (aSelectedPages.HasMoreElements())
204 model::SharedPageDescriptor pDescriptor (aSelectedPages.GetNextElement());
206 // Get the page number and compare it to the last one.
207 const sal_Int32 nPageNumber (pDescriptor->GetPageIndex());
208 if (nCurrentIndex>=0 && nPageNumber>(nCurrentIndex+1))
209 return false;
210 else
211 nCurrentIndex = nPageNumber;
213 // Remember indices of the first and last page of the selection.
214 if (nFirstIndex == -1)
215 nFirstIndex = nPageNumber;
216 nLastIndex = nPageNumber;
219 // When we come here then the selection has no holes. We still have
220 // to check that the insertion position is not directly in front or
221 // directly behind the selection and thus moving the selection there
222 // would not change the model.
223 if (nInsertionIndex<nFirstIndex || nInsertionIndex>(nLastIndex+1))
224 return false;
226 return true;
229 bool InsertionIndicatorHandler::IsInsertionTrivial (const sal_Int8 nDndAction)
231 return IsInsertionTrivial(GetInsertionPageIndex(), GetModeFromDndAction(nDndAction));
234 //===== InsertionIndicatorHandler::ForceShowContext ===========================
236 InsertionIndicatorHandler::ForceShowContext::ForceShowContext (
237 const ::boost::shared_ptr<InsertionIndicatorHandler>& rpHandler)
238 : mpHandler(rpHandler)
240 mpHandler->ForceShow();
243 InsertionIndicatorHandler::ForceShowContext::~ForceShowContext()
245 mpHandler->ForceEnd();
248 } } } // end of namespace ::sd::slidesorter::controller
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */