Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / svdraw / polypolygoneditor.cxx
blobd01ce7506cf96787d49e00a39087397ce2c9a7f6
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 .
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
24 #include "svx/polypolygoneditor.hxx"
26 namespace sdr {
28 PolyPolygonEditor::PolyPolygonEditor( const basegfx::B2DPolyPolygon& rPolyPolygon, bool bClosed )
29 : maPolyPolygon( rPolyPolygon )
30 , mbIsClosed( bClosed )
34 bool PolyPolygonEditor::DeletePoints( const std::set< sal_uInt16 >& rAbsPoints )
36 bool bPolyPolyChanged = false;
38 std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
39 for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); ++aIter )
41 sal_uInt32 nPoly, nPnt;
42 if( GetRelativePolyPoint(maPolyPolygon,(*aIter), nPoly, nPnt) )
44 // remove point
45 basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPoly));
47 aCandidate.remove(nPnt);
49 if( ( mbIsClosed && aCandidate.count() < 3L) || (aCandidate.count() < 2L) )
51 maPolyPolygon.remove(nPoly);
53 else
55 maPolyPolygon.setB2DPolygon(nPoly, aCandidate);
58 bPolyPolyChanged = true;
62 return bPolyPolyChanged;
65 bool PolyPolygonEditor::SetSegmentsKind(SdrPathSegmentKind eKind, const std::set< sal_uInt16 >& rAbsPoints )
67 bool bPolyPolyChanged = false;
69 std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
70 for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); ++aIter )
72 sal_uInt32 nPolyNum, nPntNum;
74 if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
76 // do change at aNewPolyPolygon. Take a look at edge.
77 basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
78 bool bCandidateChanged(false);
79 const sal_uInt32 nCount(aCandidate.count());
81 if(nCount && (nPntNum + 1 < nCount || aCandidate.isClosed()))
83 // it's a valid edge, check control point usage
84 const sal_uInt32 nNextIndex((nPntNum + 1) % nCount);
85 const bool bContolUsed(aCandidate.areControlPointsUsed()
86 && (aCandidate.isNextControlPointUsed(nPntNum) || aCandidate.isPrevControlPointUsed(nNextIndex)));
88 if(bContolUsed)
90 if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_LINE == eKind)
92 // remove control
93 aCandidate.resetNextControlPoint(nPntNum);
94 aCandidate.resetPrevControlPoint(nNextIndex);
95 bCandidateChanged = true;
98 else
100 if(SDRPATHSEGMENT_TOGGLE == eKind || SDRPATHSEGMENT_CURVE == eKind)
102 // add control
103 const basegfx::B2DPoint aStart(aCandidate.getB2DPoint(nPntNum));
104 const basegfx::B2DPoint aEnd(aCandidate.getB2DPoint(nNextIndex));
106 aCandidate.setNextControlPoint(nPntNum, interpolate(aStart, aEnd, (1.0 / 3.0)));
107 aCandidate.setPrevControlPoint(nNextIndex, interpolate(aStart, aEnd, (2.0 / 3.0)));
108 bCandidateChanged = true;
112 if(bCandidateChanged)
114 maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
115 bPolyPolyChanged = true;
121 return bPolyPolyChanged;
124 bool PolyPolygonEditor::SetPointsSmooth( basegfx::B2VectorContinuity eFlags, const std::set< sal_uInt16 >& rAbsPoints)
126 bool bPolyPolygonChanged(false);
128 std::set< sal_uInt16 >::const_reverse_iterator aIter;( rAbsPoints.rbegin() );
129 for( aIter = rAbsPoints.rbegin(); aIter != rAbsPoints.rend(); ++aIter )
131 sal_uInt32 nPolyNum, nPntNum;
133 if(PolyPolygonEditor::GetRelativePolyPoint(maPolyPolygon, (*aIter), nPolyNum, nPntNum))
135 // do change at aNewPolyPolygon...
136 basegfx::B2DPolygon aCandidate(maPolyPolygon.getB2DPolygon(nPolyNum));
138 // set continuity in point, make sure there is a curve
139 bool bPolygonChanged(false);
140 bPolygonChanged = basegfx::tools::expandToCurveInPoint(aCandidate, nPntNum);
141 bPolygonChanged |= basegfx::tools::setContinuityInPoint(aCandidate, nPntNum, eFlags);
143 if(bPolygonChanged)
145 maPolyPolygon.setB2DPolygon(nPolyNum, aCandidate);
146 bPolyPolygonChanged = true;
151 return bPolyPolygonChanged;
154 bool PolyPolygonEditor::GetRelativePolyPoint( const basegfx::B2DPolyPolygon& rPoly, sal_uInt32 nAbsPnt, sal_uInt32& rPolyNum, sal_uInt32& rPointNum )
156 const sal_uInt32 nPolyCount(rPoly.count());
157 sal_uInt32 nPolyNum(0L);
159 while(nPolyNum < nPolyCount)
161 const sal_uInt32 nPointCount(rPoly.getB2DPolygon(nPolyNum).count());
163 if(nAbsPnt < nPointCount)
165 rPolyNum = nPolyNum;
166 rPointNum = nAbsPnt;
168 return true;
170 else
172 nPolyNum++;
173 nAbsPnt -= nPointCount;
177 return false;
180 } // end of namespace sdr
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */