bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsVisibleAreaManager.cxx
blob78d61da203964557f508e991c92ebd5b9ff1bfe0
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 <sal/config.h>
22 #include <cstdlib>
24 #include <controller/SlsVisibleAreaManager.hxx>
25 #include <controller/SlideSorterController.hxx>
26 #include <controller/SlsProperties.hxx>
27 #include <controller/SlsAnimationFunction.hxx>
28 #include <controller/SlsScrollBarManager.hxx>
29 #include <controller/SlsCurrentSlideManager.hxx>
30 #include <Window.hxx>
31 #include <SlideSorter.hxx>
32 #include <view/SlideSorterView.hxx>
34 namespace sd { namespace slidesorter { namespace controller {
36 namespace {
37 class VisibleAreaScroller
39 public:
40 VisibleAreaScroller (
41 SlideSorter& rSlideSorter,
42 const Point& rStart,
43 const Point& rEnd);
44 void operator() (const double nValue);
45 private:
46 SlideSorter& mrSlideSorter;
47 Point maStart;
48 const Point maEnd;
49 const ::std::function<double (double)> maAccelerationFunction;
52 } // end of anonymous namespace
54 VisibleAreaManager::VisibleAreaManager (SlideSorter& rSlideSorter)
55 : mrSlideSorter(rSlideSorter),
56 maVisibleRequests(),
57 maRequestedVisibleTopLeft(),
58 mbIsCurrentSlideTrackingActive(true),
59 mnDisableCount(0)
63 VisibleAreaManager::~VisibleAreaManager()
67 void VisibleAreaManager::ActivateCurrentSlideTracking()
69 mbIsCurrentSlideTrackingActive = true;
72 void VisibleAreaManager::DeactivateCurrentSlideTracking()
74 mbIsCurrentSlideTrackingActive = false;
77 void VisibleAreaManager::RequestVisible (
78 const model::SharedPageDescriptor& rpDescriptor,
79 const bool bForce)
81 if (!rpDescriptor)
82 return;
84 if (mnDisableCount == 0)
86 maVisibleRequests.push_back(
87 mrSlideSorter.GetView().GetLayouter().GetPageObjectBox(
88 rpDescriptor->GetPageIndex(),
89 true));
91 if (bForce && ! mbIsCurrentSlideTrackingActive)
92 ActivateCurrentSlideTracking();
93 MakeVisible();
96 void VisibleAreaManager::RequestCurrentSlideVisible()
98 if (mbIsCurrentSlideTrackingActive && mnDisableCount==0)
99 RequestVisible(
100 mrSlideSorter.GetController().GetCurrentSlideManager()->GetCurrentSlide());
103 void VisibleAreaManager::MakeVisible()
105 if (maVisibleRequests.empty())
106 return;
108 sd::Window *pWindow (mrSlideSorter.GetContentWindow().get());
109 if ( ! pWindow)
110 return;
111 const Point aCurrentTopLeft (pWindow->PixelToLogic(Point(0,0)));
113 const ::boost::optional<Point> aNewVisibleTopLeft (GetRequestedTopLeft());
114 maVisibleRequests.clear();
115 if ( ! aNewVisibleTopLeft)
116 return;
118 maRequestedVisibleTopLeft = aNewVisibleTopLeft.get();
119 VisibleAreaScroller aAnimation(
120 mrSlideSorter,
121 aCurrentTopLeft,
122 maRequestedVisibleTopLeft);
123 // Execute the animation at its final value.
124 aAnimation(1.0);
127 ::boost::optional<Point> VisibleAreaManager::GetRequestedTopLeft() const
129 sd::Window *pWindow (mrSlideSorter.GetContentWindow().get());
130 if ( ! pWindow)
131 return ::boost::optional<Point>();
133 // Get the currently visible area and the model area.
134 const ::tools::Rectangle aVisibleArea (pWindow->PixelToLogic(
135 ::tools::Rectangle(
136 Point(0,0),
137 pWindow->GetOutputSizePixel())));
138 const ::tools::Rectangle aModelArea (mrSlideSorter.GetView().GetModelArea());
140 sal_Int32 nVisibleTop (aVisibleArea.Top());
141 const sal_Int32 nVisibleWidth (aVisibleArea.GetWidth());
142 sal_Int32 nVisibleLeft (aVisibleArea.Left());
143 const sal_Int32 nVisibleHeight (aVisibleArea.GetHeight());
145 // Find the longest run of boxes whose union fits into the visible area.
146 for (const auto& rBox : maVisibleRequests)
148 if (nVisibleTop+nVisibleHeight <= rBox.Bottom())
149 nVisibleTop = rBox.Bottom()-nVisibleHeight;
150 if (nVisibleTop > rBox.Top())
151 nVisibleTop = rBox.Top();
153 if (nVisibleLeft+nVisibleWidth <= rBox.Right())
154 nVisibleLeft = rBox.Right()-nVisibleWidth;
155 if (nVisibleLeft > rBox.Left())
156 nVisibleLeft = rBox.Left();
158 // Make sure the visible area does not move outside the model area.
159 if (nVisibleTop + nVisibleHeight > aModelArea.Bottom())
160 nVisibleTop = aModelArea.Bottom() - nVisibleHeight;
161 if (nVisibleTop < aModelArea.Top())
162 nVisibleTop = aModelArea.Top();
164 if (nVisibleLeft + nVisibleWidth > aModelArea.Right())
165 nVisibleLeft = aModelArea.Right() - nVisibleWidth;
166 if (nVisibleLeft < aModelArea.Left())
167 nVisibleLeft = aModelArea.Left();
170 const Point aRequestedTopLeft (nVisibleLeft, nVisibleTop);
171 if (aRequestedTopLeft == aVisibleArea.TopLeft())
172 return ::boost::optional<Point>();
173 else
174 return ::boost::optional<Point>(aRequestedTopLeft);
177 //===== VisibleAreaManager::TemporaryDisabler =================================
179 VisibleAreaManager::TemporaryDisabler::TemporaryDisabler (SlideSorter const & rSlideSorter)
180 : mrVisibleAreaManager(rSlideSorter.GetController().GetVisibleAreaManager())
182 ++mrVisibleAreaManager.mnDisableCount;
185 VisibleAreaManager::TemporaryDisabler::~TemporaryDisabler()
187 --mrVisibleAreaManager.mnDisableCount;
190 //===== VerticalVisibleAreaScroller ===========================================
192 namespace {
194 const static sal_Int32 gnMaxScrollDistance = 300;
196 VisibleAreaScroller::VisibleAreaScroller (
197 SlideSorter& rSlideSorter,
198 const Point& rStart,
199 const Point& rEnd)
200 : mrSlideSorter(rSlideSorter),
201 maStart(rStart),
202 maEnd(rEnd),
203 maAccelerationFunction(
204 controller::AnimationParametricFunction(
205 controller::AnimationBezierFunction (0.1,0.6)))
207 // When the distance to scroll is larger than a threshold then first
208 // jump to within this distance of the final value and start the
209 // animation from there.
210 if (std::abs(rStart.X()-rEnd.X()) > gnMaxScrollDistance)
212 if (rStart.X() < rEnd.X())
213 maStart.setX( rEnd.X()-gnMaxScrollDistance );
214 else
215 maStart.setX( rEnd.X()+gnMaxScrollDistance );
217 if (std::abs(rStart.Y()-rEnd.Y()) > gnMaxScrollDistance)
219 if (rStart.Y() < rEnd.Y())
220 maStart.setY( rEnd.Y()-gnMaxScrollDistance );
221 else
222 maStart.setY( rEnd.Y()+gnMaxScrollDistance );
226 void VisibleAreaScroller::operator() (const double nTime)
228 const double nLocalTime (maAccelerationFunction(nTime));
229 mrSlideSorter.GetController().GetScrollBarManager().SetTopLeft(
230 Point(
231 sal_Int32(0.5 + maStart.X() * (1.0 - nLocalTime) + maEnd.X() * nLocalTime),
232 sal_Int32 (0.5 + maStart.Y() * (1.0 - nLocalTime) + maEnd.Y() * nLocalTime)));
235 } // end of anonymous namespace
237 } } } // end of namespace ::sd::slidesorter::controller
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */