LibreOffice Module svx (master)  1
overlaymanager.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
23 #include <tools/gen.hxx>
24 #include <vcl/canvastools.hxx>
25 #include <vcl/outdev.hxx>
26 #include <vcl/window.hxx>
31 #include <memory>
32 
33 
34 using namespace com::sun::star;
35 
36 
37 namespace sdr::overlay
38  {
39  void OverlayManager::ImpDrawMembers(const basegfx::B2DRange& rRange, OutputDevice& rDestinationDevice) const
40  {
41  const sal_uInt32 nSize(maOverlayObjects.size());
42 
43  if(nSize)
44  {
45  const AntialiasingFlags nOriginalAA(rDestinationDevice.GetAntialiasing());
46  const bool bIsAntiAliasing(getDrawinglayerOpt().IsAntiAliasing());
47 
48  // create processor
49  std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(drawinglayer::processor2d::createProcessor2DFromOutputDevice(
50  rDestinationDevice,
51  getCurrentViewInformation2D()));
52 
53  if(pProcessor)
54  {
55  for(const auto& rpOverlayObject : maOverlayObjects)
56  {
57  OSL_ENSURE(rpOverlayObject, "Corrupted OverlayObject List (!)");
58  const OverlayObject& rCandidate = *rpOverlayObject;
59 
60  if(rCandidate.isVisible())
61  {
63 
64  if(!rSequence.empty())
65  {
66  if(rRange.overlaps(rCandidate.getBaseRange()))
67  {
68  if(bIsAntiAliasing && rCandidate.allowsAntiAliase())
69  {
70  rDestinationDevice.SetAntialiasing(nOriginalAA | AntialiasingFlags::EnableB2dDraw);
71  }
72  else
73  {
74  rDestinationDevice.SetAntialiasing(nOriginalAA & ~AntialiasingFlags::EnableB2dDraw);
75  }
76 
77  pProcessor->process(rSequence);
78  }
79  }
80  }
81  }
82 
83  pProcessor.reset();
84  }
85 
86  // restore AA settings
87  rDestinationDevice.SetAntialiasing(nOriginalAA);
88  }
89  }
90 
91  void OverlayManager::ImpStripeDefinitionChanged()
92  {
93  const sal_uInt32 nSize(maOverlayObjects.size());
94 
95  if(nSize)
96  {
97  for(const auto& rpOverlayObject : maOverlayObjects)
98  {
99  OSL_ENSURE(rpOverlayObject, "Corrupted OverlayObject List (!)");
100  OverlayObject& rCandidate = *rpOverlayObject;
101  rCandidate.stripeDefinitionHasChanged();
102  }
103  }
104  }
105 
106  double OverlayManager::getDiscreteOne() const
107  {
108  if(basegfx::fTools::equalZero(mfDiscreteOne))
109  {
110  const basegfx::B2DVector aDiscreteInLogic(getOutputDevice().GetInverseViewTransformation() * basegfx::B2DVector(1.0, 0.0));
111  const_cast< OverlayManager* >(this)->mfDiscreteOne = aDiscreteInLogic.getLength();
112  }
113 
114  return mfDiscreteOne;
115  }
116 
117  OverlayManager::OverlayManager(OutputDevice& rOutputDevice)
118  : Scheduler(),
119  mrOutputDevice(rOutputDevice),
120  maOverlayObjects(),
121  maStripeColorA(COL_BLACK),
122  maStripeColorB(COL_WHITE),
123  mnStripeLengthPixel(5),
124  maDrawinglayerOpt(),
125  maViewTransformation(),
126  maViewInformation2D(),
127  mfDiscreteOne(0.0)
128  {
129  // set Property 'ReducedDisplayQuality' to true to allow simpler interaction
130  // visualisations
131  uno::Sequence< beans::PropertyValue > xProperties(1);
132  xProperties[0].Name = "ReducedDisplayQuality";
133  xProperties[0].Value <<= true;
135  }
136 
138  {
139  return rtl::Reference<OverlayManager>(new OverlayManager(rOutputDevice));
140  }
141 
143  {
144  if(getOutputDevice().GetViewTransformation() != maViewTransformation)
145  {
147 
149  {
150  const Size aOutputSizePixel(getOutputDevice().GetOutputSizePixel());
151 
152  // only set when we *have* an output size, else let aViewRange
153  // stay on empty
154  if(aOutputSizePixel.Width() && aOutputSizePixel.Height())
155  {
156  aViewRange = basegfx::B2DRange(0.0, 0.0, aOutputSizePixel.getWidth(), aOutputSizePixel.getHeight());
157  aViewRange.transform(getOutputDevice().GetInverseViewTransformation());
158  }
159  }
160 
161  OverlayManager* pThis = const_cast< OverlayManager* >(this);
162 
167  aViewRange,
171  pThis->mfDiscreteOne = 0.0;
172  }
173 
174  return maViewInformation2D;
175  }
176 
178  {
179  // handle evtl. animation
180  if(rTarget.allowsAnimation())
181  {
182  // remove from event chain
183  RemoveEvent(&rTarget);
184  }
185 
186  // make invisible
187  invalidateRange(rTarget.getBaseRange());
188 
189  // clear manager
190  rTarget.mpOverlayManager = nullptr;
191  }
192 
194  {
195  // set manager
196  rTarget.mpOverlayManager = this;
197 
198  // make visible
199  invalidateRange(rTarget.getBaseRange());
200 
201  // handle evtl. animation
202  if(rTarget.allowsAnimation())
203  {
204  // Trigger at current time to get alive. This will do the
205  // object-specific next time calculation and hand over adding
206  // again to the scheduler to the animated object, too. This works for
207  // a paused or non-paused animator.
208  rTarget.Trigger(GetTime());
209  }
210  }
211 
213  {
214  // The OverlayManager is not the owner of the OverlayObjects
215  // and thus will not delete them, but remove them. Profit here
216  // from knowing that all will be removed
217  const sal_uInt32 nSize(maOverlayObjects.size());
218 
219  if(nSize)
220  {
221  for(const auto& rpOverlayObject : maOverlayObjects)
222  {
223  OSL_ENSURE(rpOverlayObject, "Corrupted OverlayObject List (!)");
224  OverlayObject& rCandidate = *rpOverlayObject;
225  impApplyRemoveActions(rCandidate);
226  }
227 
228  // erase vector
229  maOverlayObjects.clear();
230  }
231  }
232 
233  void OverlayManager::completeRedraw(const vcl::Region& rRegion, OutputDevice* pPreRenderDevice) const
234  {
235  if(!rRegion.IsEmpty() && !maOverlayObjects.empty())
236  {
237  // check for changed MapModes. That may influence the
238  // logical size of pixel based OverlayObjects (like BitmapHandles)
239  //ImpCheckMapModeChange();
240 
241  // paint members
242  const tools::Rectangle aRegionBoundRect(rRegion.GetBoundRect());
243  const basegfx::B2DRange aRegionRange = vcl::unotools::b2DRectangleFromRectangle(aRegionBoundRect);
244 
245  OutputDevice& rTarget = pPreRenderDevice ? *pPreRenderDevice : getOutputDevice();
246  ImpDrawMembers(aRegionRange, rTarget);
247  }
248  }
249 
251  {
252  // default has nothing to do
253  }
254 
255  void OverlayManager::add(OverlayObject& rOverlayObject)
256  {
257  OSL_ENSURE(nullptr == rOverlayObject.mpOverlayManager, "OverlayObject is added twice to an OverlayManager (!)");
258 
259  // add to the end of chain to preserve display order in paint
260  maOverlayObjects.push_back(&rOverlayObject);
261 
262  // execute add actions
263  impApplyAddActions(rOverlayObject);
264  }
265 
266  void OverlayManager::remove(OverlayObject& rOverlayObject)
267  {
268  OSL_ENSURE(rOverlayObject.mpOverlayManager == this, "OverlayObject is removed from wrong OverlayManager (!)");
269 
270  // execute remove actions
271  impApplyRemoveActions(rOverlayObject);
272 
273  // remove from vector
274  const OverlayObjectVector::iterator aFindResult = ::std::find(maOverlayObjects.begin(), maOverlayObjects.end(), &rOverlayObject);
275  const bool bFound(aFindResult != maOverlayObjects.end());
276  OSL_ENSURE(bFound, "OverlayObject NOT found at OverlayManager (!)");
277 
278  if(bFound)
279  {
280  maOverlayObjects.erase(aFindResult);
281  }
282  }
283 
285  {
286  if (getDrawinglayerOpt().IsAntiAliasing())
287  {
288  // assume AA needs one pixel more and invalidate one pixel more
289  const double fDiscreteOne(getDiscreteOne());
290  const tools::Rectangle aInvalidateRectangle(
291  static_cast<sal_Int32>(floor(rRange.getMinX() - fDiscreteOne)),
292  static_cast<sal_Int32>(floor(rRange.getMinY() - fDiscreteOne)),
293  static_cast<sal_Int32>(ceil(rRange.getMaxX() + fDiscreteOne)),
294  static_cast<sal_Int32>(ceil(rRange.getMaxY() + fDiscreteOne)));
295  return aInvalidateRectangle;
296  }
297  else
298  {
299  // #i77674# transform to rectangle. Use floor/ceil to get all covered
300  // discrete pixels, see #i75163# and OverlayManagerBuffered::invalidateRange
301  const tools::Rectangle aInvalidateRectangle(
302  static_cast<sal_Int32>(floor(rRange.getMinX())), static_cast<sal_Int32>(floor(rRange.getMinY())),
303  static_cast<sal_Int32>(ceil(rRange.getMaxX())), static_cast<sal_Int32>(ceil(rRange.getMaxY())));
304  return aInvalidateRectangle;
305  }
306  }
307 
309  {
310  if (OUTDEV_WINDOW == getOutputDevice().GetOutDevType())
311  {
312  tools::Rectangle aInvalidateRectangle(RangeToInvalidateRectangle(rRange));
313  // simply invalidate
314  static_cast<vcl::Window&>(getOutputDevice()).Invalidate(aInvalidateRectangle, InvalidateFlags::NoErase);
315  }
316  }
317 
318  // stripe support ColA
320  {
321  if(aNew != maStripeColorA)
322  {
323  maStripeColorA = aNew;
325  }
326  }
327 
328  // stripe support ColB
330  {
331  if(aNew != maStripeColorB)
332  {
333  maStripeColorB = aNew;
335  }
336  }
337 
338  // stripe support StripeLengthPixel
340  {
341  if(nNew != mnStripeLengthPixel)
342  {
343  mnStripeLengthPixel = nNew;
345  }
346  }
347 
348  } // end of namespace sdr
349 
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE void RemoveEvent(Event *pOld)
Definition: scheduler.cxx:151
long Width() const
OutDevType GetOutDevType() const
drawinglayer::geometry::ViewInformation2D maViewInformation2D
const basegfx::B2DRange & getViewport() const
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
void SetAntialiasing(AntialiasingFlags nMode)
virtual ~OverlayManager() override
void impApplyAddActions(OverlayObject &rTarget)
long Height() const
virtual void stripeDefinitionHasChanged()
FilterGroup & rTarget
static rtl::Reference< OverlayManager > create(OutputDevice &rOutputDevice)
tools::Rectangle GetBoundRect() const
double getMaxX() const
AntialiasingFlags GetAntialiasing() const
std::unique_ptr< BaseProcessor2D > createProcessor2DFromOutputDevice(OutputDevice &rTargetOutDev, const drawinglayer::geometry::ViewInformation2D &rViewInformation2D)
void add(OverlayObject &rOverlayObject)
const css::uno::Sequence< css::beans::PropertyValue > & getExtendedInformationSequence() const
OUTDEV_WINDOW
BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix &rMatrix)
void remove(OverlayObject &rOverlayObject)
const basegfx::B2DRange & getBaseRange() const
const css::uno::Reference< css::drawing::XDrawPage > & getVisualizedPage() const
double getMaxY() const
const basegfx::B2DHomMatrix & getObjectTransformation() const
bool IsEmpty() const
OverlayManager * mpOverlayManager
static bool equalZero(const double &rfVal)
AntialiasingFlags
SAL_DLLPRIVATE sal_uInt32 GetTime() const
Definition: scheduler.hxx:76
virtual drawinglayer::primitive2d::Primitive2DContainer getOverlayObjectPrimitive2DSequence() const
tools::Rectangle RangeToInvalidateRectangle(const basegfx::B2DRange &rRange) const
void impApplyRemoveActions(OverlayObject &rTarget)
drawinglayer::geometry::ViewInformation2D const & getCurrentViewInformation2D() const
OverlayObjectVector maOverlayObjects
basegfx::B2DRange b2DRectangleFromRectangle(const ::tools::Rectangle &rRect)
OverlayManager(const OverlayManager &)=delete
OutputDevice & getOutputDevice() const
virtual void Trigger(sal_uInt32 nTime) override
double getMinY() const
void setStripeLengthPixel(sal_uInt32 nNew)
basegfx::B2DHomMatrix maViewTransformation
virtual void completeRedraw(const vcl::Region &rRegion, OutputDevice *pPreRenderDevice=nullptr) const
const SvtOptionsDrawinglayer & getDrawinglayerOpt() const
void ImpDrawMembers(const basegfx::B2DRange &rRange, OutputDevice &rDestinationDevice) const
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
double getLength() const
double getMinX() const
long getHeight() const
long getWidth() const
basegfx::B2DHomMatrix GetViewTransformation() const
bool overlaps(const B2DRange &rRange) const
virtual void invalidateRange(const basegfx::B2DRange &rRange)