cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / svx / source / sdr / overlay / overlayselection.cxx
blob6f9284696d5157a7df3211dfdf09b30a9365b735
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 <svx/sdr/overlay/overlayselection.hxx>
21 #include <basegfx/polygon/b2dpolygontools.hxx>
22 #include <basegfx/polygon/b2dpolygon.hxx>
23 #include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
24 #include <drawinglayer/primitive2d/PolyPolygonHairlinePrimitive2D.hxx>
25 #include <svtools/optionsdrawinglayer.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/outdev.hxx>
28 #include <vcl/settings.hxx>
29 #include <drawinglayer/primitive2d/invertprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
31 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
32 #include <svx/sdr/overlay/overlaymanager.hxx>
33 #include <officecfg/Office/Common.hxx>
34 #include <o3tl/sorted_vector.hxx>
35 #include <map>
36 #include <tools/fract.hxx>
38 namespace sdr::overlay
41 // combine ranges geometrically to a single, ORed polygon
42 static basegfx::B2DPolyPolygon impCombineRangesToPolyPolygon(const std::vector< basegfx::B2DRange >& rRanges, bool bOffset, double fOffset)
44 const sal_uInt32 nCount(rRanges.size());
45 basegfx::B2DPolyPolygon aRetval;
47 for(sal_uInt32 a(0); a < nCount; a++)
49 basegfx::B2DRange aRange(rRanges[a]);
50 if (bOffset)
51 aRange.grow(fOffset);
52 const basegfx::B2DPolygon aDiscretePolygon(basegfx::utils::createPolygonFromRect(aRange));
54 if(0 == a)
56 aRetval.append(aDiscretePolygon);
58 else
60 aRetval = basegfx::utils::solvePolygonOperationOr(aRetval, basegfx::B2DPolyPolygon(aDiscretePolygon));
64 return aRetval;
67 // tdf#161204 Creates a poly-polygon using white hairline to provide contrast
68 static basegfx::B2DPolyPolygon impCombineRangesToContrastPolyPolygon(const std::vector< basegfx::B2DRange >& rRanges)
70 const sal_uInt32 nCount(rRanges.size());
71 basegfx::B2DPolyPolygon aRetval;
73 for(sal_uInt32 a(0); a < nCount; a++)
75 const basegfx::B2DPolygon aDiscretePolygon(basegfx::utils::createPolygonFromRect(rRanges[a]));
77 if(0 == a)
79 aRetval.append(aDiscretePolygon);
81 else
83 aRetval = basegfx::utils::solvePolygonOperationOr(aRetval, basegfx::B2DPolyPolygon(aDiscretePolygon));
87 return aRetval;
90 // check if wanted type OverlayType::Transparent or OverlayType::Solid
91 // is possible. If not, fallback to invert mode (classic mode)
92 static OverlayType impCheckPossibleOverlayType(OverlayType aOverlayType)
94 if(OverlayType::Invert != aOverlayType)
96 if(!officecfg::Office::Common::Drawinglayer::TransparentSelection::get())
98 // not possible when switched off by user
99 return OverlayType::Invert;
101 else if (const OutputDevice* pOut = Application::GetDefaultDevice())
104 if(pOut->GetSettings().GetStyleSettings().GetHighContrastMode())
106 // not possible when in high contrast mode
107 return OverlayType::Invert;
112 return aOverlayType;
115 drawinglayer::primitive2d::Primitive2DContainer OverlaySelection::createOverlayObjectPrimitive2DSequence()
117 drawinglayer::primitive2d::Primitive2DContainer aRetval;
118 const sal_uInt32 nCount(getRanges().size());
120 if(nCount)
122 // create range primitives
123 const bool bInvert(OverlayType::Invert == maLastOverlayType);
124 basegfx::BColor aRGBColor(getBaseColor().getBColor());
125 aRetval.resize(nCount);
127 if(bInvert)
129 // force color to white for invert to get a full invert
130 aRGBColor = basegfx::BColor(1.0, 1.0, 1.0);
133 for(sal_uInt32 a(0);a < nCount; a++)
135 const basegfx::B2DPolygon aPolygon(basegfx::utils::createPolygonFromRect(maRanges[a]));
136 aRetval[a] =
137 new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(
138 basegfx::B2DPolyPolygon(aPolygon),
139 aRGBColor);
142 if(bInvert)
144 // embed all in invert primitive
145 aRetval = drawinglayer::primitive2d::Primitive2DContainer {
146 new drawinglayer::primitive2d::InvertPrimitive2D(
147 std::move(aRetval))
150 else if(maLastOverlayType == OverlayType::Transparent || maLastOverlayType == OverlayType::NoFill)
152 // Determine transparency level
153 double fTransparence;
154 if (maLastOverlayType == OverlayType::NoFill)
155 fTransparence = 1;
156 else
157 fTransparence = mnLastTransparence / 100.0;
159 // embed all rectangles in transparent paint
160 const drawinglayer::primitive2d::Primitive2DReference aUnifiedTransparence(
161 new drawinglayer::primitive2d::UnifiedTransparencePrimitive2D(
162 std::move(aRetval),
163 fTransparence));
165 if(mbBorder)
167 aRetval = drawinglayer::primitive2d::Primitive2DContainer {aUnifiedTransparence};
169 // tdf#161204 Outline with white color to provide contrast
170 if (mbContrastOutline)
172 basegfx::B2DPolyPolygon aContrastPolyPolygon(impCombineRangesToContrastPolyPolygon(getRanges()));
173 const drawinglayer::primitive2d::Primitive2DReference aContrastSelectionOutline(
174 new drawinglayer::primitive2d::PolyPolygonHairlinePrimitive2D(
175 std::move(aContrastPolyPolygon),
176 basegfx::BColor(1.0, 1.0, 1.0)));
177 aRetval.append(drawinglayer::primitive2d::Primitive2DContainer{aContrastSelectionOutline});
180 // Offset to be applied to the external outline
181 double fOffset(0);
182 if (getOverlayManager())
183 fOffset = getOverlayManager()->getOutputDevice().PixelToLogic(Size(1, 1)).getWidth();
185 // External outline using themed color
186 basegfx::B2DPolyPolygon aPolyPolygon(impCombineRangesToPolyPolygon(getRanges(), mbContrastOutline, fOffset));
187 const drawinglayer::primitive2d::Primitive2DReference aSelectionOutline(
188 new drawinglayer::primitive2d::PolyPolygonHairlinePrimitive2D(
189 std::move(aPolyPolygon),
190 aRGBColor));
192 // Add to result
193 aRetval.append(drawinglayer::primitive2d::Primitive2DContainer {aSelectionOutline});
195 else
197 // just add transparent part
198 aRetval = drawinglayer::primitive2d::Primitive2DContainer { aUnifiedTransparence };
203 return aRetval;
206 OverlaySelection::OverlaySelection(
207 OverlayType eType,
208 const Color& rColor,
209 std::vector< basegfx::B2DRange >&& rRanges,
210 bool bBorder,
211 bool bContrastOutline)
212 : OverlayObject(rColor),
213 meOverlayType(eType),
214 maRanges(std::move(rRanges)),
215 maLastOverlayType(eType),
216 mnLastTransparence(0),
217 mbBorder(bBorder),
218 mbContrastOutline(bContrastOutline)
220 // no AA for selection overlays
221 allowAntiAliase(false);
224 OverlaySelection::~OverlaySelection()
226 if(getOverlayManager())
228 getOverlayManager()->remove(*this);
232 drawinglayer::primitive2d::Primitive2DContainer OverlaySelection::getOverlayObjectPrimitive2DSequence() const
234 // get current values
235 const OverlayType aNewOverlayType(impCheckPossibleOverlayType(meOverlayType));
236 const sal_uInt16 nNewTransparence(SvtOptionsDrawinglayer::GetTransparentSelectionPercent());
238 if(!getPrimitive2DSequence().empty())
240 if(aNewOverlayType != maLastOverlayType
241 || nNewTransparence != mnLastTransparence)
243 // conditions of last local decomposition have changed, delete
244 const_cast< OverlaySelection* >(this)->resetPrimitive2DSequence();
248 if(getPrimitive2DSequence().empty())
250 // remember new values
251 const_cast< OverlaySelection* >(this)->maLastOverlayType = aNewOverlayType;
252 const_cast< OverlaySelection* >(this)->mnLastTransparence = nNewTransparence;
255 // call base implementation
256 return OverlayObject::getOverlayObjectPrimitive2DSequence();
259 void OverlaySelection::setRanges(std::vector< basegfx::B2DRange >&& rNew)
261 if(rNew != maRanges)
263 maRanges = std::move(rNew);
264 objectChange();
267 } // end of namespace
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */