tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsInsertionIndicatorHandler.cxx
blobdafeef5a5627586cc63199b7f5c677a161d6c12e
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 <utility>
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>
29 #include <osl/diagnose.h>
31 #include <SlideSorter.hxx>
33 using namespace ::com::sun::star::datatransfer::dnd::DNDConstants;
35 namespace sd::slidesorter::controller {
37 InsertionIndicatorHandler::InsertionIndicatorHandler (SlideSorter& rSlideSorter)
38 : mrSlideSorter(rSlideSorter),
39 mpInsertionIndicatorOverlay(std::make_shared<view::InsertionIndicatorOverlay>(rSlideSorter)),
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() COVERITY_NOEXCEPT_FALSE
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 = std::make_shared<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 && meMode == eMode)
152 return;
154 maInsertPosition = aInsertPosition;
155 meMode = eMode;
156 mbIsInsertionTrivial = IsInsertionTrivial(maInsertPosition.GetIndex(), eMode);
157 if (maInsertPosition.GetIndex()>=0 && ! mbIsInsertionTrivial)
159 mpInsertionIndicatorOverlay->SetLocation(maInsertPosition.GetLocation());
161 GetInsertAnimator()->SetInsertPosition(maInsertPosition);
162 mpInsertionIndicatorOverlay->Show();
164 else
166 GetInsertAnimator()->Reset(Animator::AM_Animated);
167 mpInsertionIndicatorOverlay->Hide();
171 std::shared_ptr<view::InsertAnimator> const & InsertionIndicatorHandler::GetInsertAnimator()
173 if ( ! mpInsertAnimator)
174 mpInsertAnimator = std::make_shared<view::InsertAnimator>(mrSlideSorter);
175 return mpInsertAnimator;
178 bool InsertionIndicatorHandler::IsInsertionTrivial (
179 const sal_Int32 nInsertionIndex,
180 const Mode eMode) const
182 if (eMode == CopyMode)
183 return false;
184 else if (eMode == UnknownMode)
185 return true;
187 if ( ! mbIsOverSourceView)
188 return false;
190 // Iterate over all selected pages and check whether there are
191 // holes. While we do this we remember the indices of the first and
192 // last selected page as preparation for the next step.
193 sal_Int32 nCurrentIndex = -1;
194 sal_Int32 nFirstIndex = -1;
195 sal_Int32 nLastIndex = -1;
196 model::PageEnumeration aSelectedPages (
197 model::PageEnumerationProvider::CreateSelectedPagesEnumeration(
198 mrSlideSorter.GetModel()));
199 while (aSelectedPages.HasMoreElements())
201 model::SharedPageDescriptor pDescriptor (aSelectedPages.GetNextElement());
203 // Get the page number and compare it to the last one.
204 const sal_Int32 nPageNumber (pDescriptor->GetPageIndex());
205 if (nCurrentIndex>=0 && nPageNumber>(nCurrentIndex+1))
206 return false;
207 else
208 nCurrentIndex = nPageNumber;
210 // Remember indices of the first and last page of the selection.
211 if (nFirstIndex == -1)
212 nFirstIndex = nPageNumber;
213 nLastIndex = nPageNumber;
216 // When we come here then the selection has no holes. We still have
217 // to check that the insertion position is not directly in front or
218 // directly behind the selection and thus moving the selection there
219 // would not change the model.
220 return nInsertionIndex >= nFirstIndex && nInsertionIndex <= (nLastIndex+1);
223 bool InsertionIndicatorHandler::IsInsertionTrivial (const sal_Int8 nDndAction)
225 return IsInsertionTrivial(GetInsertionPageIndex(), GetModeFromDndAction(nDndAction));
228 //===== InsertionIndicatorHandler::ForceShowContext ===========================
230 InsertionIndicatorHandler::ForceShowContext::ForceShowContext (
231 std::shared_ptr<InsertionIndicatorHandler> pHandler)
232 : mpHandler(std::move(pHandler))
234 mpHandler->ForceShow();
237 InsertionIndicatorHandler::ForceShowContext::~ForceShowContext() COVERITY_NOEXCEPT_FALSE
239 mpHandler->ForceEnd();
242 } // end of namespace ::sd::slidesorter::controller
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */