android: Update app-specific/MIME type icons
[LibreOffice.git] / sd / source / ui / func / fumorph.cxx
blob5f1021dcf327456017aa752e5af76b0a029ee087
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 <fumorph.hxx>
21 #include <svx/xfillit0.hxx>
22 #include <svx/xlineit0.hxx>
23 #include <svx/xlnclit.hxx>
24 #include <svx/xlnwtit.hxx>
25 #include <svx/xflclit.hxx>
26 #include <svx/svdopath.hxx>
27 #include <svx/svdogrp.hxx>
28 #include <editeng/eeitem.hxx>
30 #include <View.hxx>
31 #include <Window.hxx>
32 #include <basegfx/polygon/b2dpolygontools.hxx>
33 #include <basegfx/polygon/b2dpolypolygontools.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 #include <strings.hrc>
38 #include <sdresid.hxx>
40 #include <sdabstdlg.hxx>
42 #include <svx/svditer.hxx>
44 #include <basegfx/color/bcolor.hxx>
45 #include <com/sun/star/drawing/LineStyle.hpp>
47 using namespace com::sun::star;
49 namespace sd {
51 FuMorph::FuMorph (
52 ViewShell* pViewSh,
53 ::sd::Window* pWin,
54 ::sd::View* pView,
55 SdDrawDocument* pDoc,
56 SfxRequest& rReq )
57 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
61 rtl::Reference<FuPoor> FuMorph::Create(
62 ViewShell* pViewSh,
63 ::sd::Window* pWin,
64 ::sd::View* pView,
65 SdDrawDocument* pDoc,
66 SfxRequest& rReq
69 rtl::Reference<FuPoor> xFunc( new FuMorph( pViewSh, pWin, pView, pDoc, rReq ) );
70 xFunc->DoExecute(rReq);
71 return xFunc;
74 void FuMorph::DoExecute( SfxRequest& )
76 const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
78 if(rMarkList.GetMarkCount() != 2)
79 return;
81 // create clones
82 SdrObject* pObj1 = rMarkList.GetMark(0)->GetMarkedSdrObj();
83 SdrObject* pObj2 = rMarkList.GetMark(1)->GetMarkedSdrObj();
84 rtl::Reference<SdrObject> pCloneObj1(pObj1->CloneSdrObject(pObj1->getSdrModelFromSdrObject()));
85 rtl::Reference<SdrObject> pCloneObj2(pObj2->CloneSdrObject(pObj2->getSdrModelFromSdrObject()));
87 // delete text at clone, otherwise we do not get a correct PathObj
88 pCloneObj1->SetOutlinerParaObject(std::nullopt);
89 pCloneObj2->SetOutlinerParaObject(std::nullopt);
91 // create path objects
92 rtl::Reference<SdrObject> pPolyObj1 = pCloneObj1->ConvertToPolyObj(false, false);
93 rtl::Reference<SdrObject> pPolyObj2 = pCloneObj2->ConvertToPolyObj(false, false);
94 SdAbstractDialogFactory* pFact = SdAbstractDialogFactory::Create();
95 ScopedVclPtr<AbstractMorphDlg> pDlg( pFact->CreateMorphDlg(mpWindow ? mpWindow->GetFrameWeld() : nullptr, pObj1, pObj2) );
96 if(pPolyObj1 && pPolyObj2 && (pDlg->Execute() == RET_OK))
98 B2DPolyPolygonList_impl aPolyPolyList;
99 ::basegfx::B2DPolyPolygon aPolyPoly1;
100 ::basegfx::B2DPolyPolygon aPolyPoly2;
102 pDlg->SaveSettings();
104 // #i48168# Not always is the pPolyObj1/pPolyObj2 a SdrPathObj, it may also be a group object
105 // containing SdrPathObjs. To get the polygons, I add two iters here
106 SdrObjListIter aIter1(*pPolyObj1);
107 SdrObjListIter aIter2(*pPolyObj2);
109 while(aIter1.IsMore())
111 SdrObject* pObj = aIter1.Next();
112 if(auto pPathObj = dynamic_cast< SdrPathObj *>( pObj ))
113 aPolyPoly1.append(pPathObj->GetPathPoly());
116 while(aIter2.IsMore())
118 SdrObject* pObj = aIter2.Next();
119 if(auto pPathObj = dynamic_cast< SdrPathObj *>( pObj ))
120 aPolyPoly2.append(pPathObj->GetPathPoly());
123 // perform morphing
124 if(aPolyPoly1.count() && aPolyPoly2.count())
126 aPolyPoly1 = ::basegfx::utils::correctOrientations(aPolyPoly1);
127 aPolyPoly1.removeDoublePoints();
128 ::basegfx::B2VectorOrientation eIsClockwise1(::basegfx::utils::getOrientation(aPolyPoly1.getB2DPolygon(0)));
130 aPolyPoly2 = ::basegfx::utils::correctOrientations(aPolyPoly2);
131 aPolyPoly2.removeDoublePoints();
132 ::basegfx::B2VectorOrientation eIsClockwise2(::basegfx::utils::getOrientation(aPolyPoly2.getB2DPolygon(0)));
134 // set same orientation
135 if(eIsClockwise1 != eIsClockwise2)
136 aPolyPoly2.flip();
138 // force same poly count
139 if(aPolyPoly1.count() < aPolyPoly2.count())
140 ImpAddPolys(aPolyPoly1, aPolyPoly2);
141 else if(aPolyPoly2.count() < aPolyPoly1.count())
142 ImpAddPolys(aPolyPoly2, aPolyPoly1);
144 // use orientation flag from dialog
145 if(!pDlg->IsOrientationFade())
146 aPolyPoly2.flip();
148 // force same point counts
149 for( sal_uInt32 a(0); a < aPolyPoly1.count(); a++ )
151 ::basegfx::B2DPolygon aSub1(aPolyPoly1.getB2DPolygon(a));
152 ::basegfx::B2DPolygon aSub2(aPolyPoly2.getB2DPolygon(a));
154 if(aSub1.count() < aSub2.count())
155 ImpEqualizePolyPointCount(aSub1, aSub2);
156 else if(aSub2.count() < aSub1.count())
157 ImpEqualizePolyPointCount(aSub2, aSub1);
159 aPolyPoly1.setB2DPolygon(a, aSub1);
160 aPolyPoly2.setB2DPolygon(a, aSub2);
163 ImpMorphPolygons(aPolyPoly1, aPolyPoly2, pDlg->GetFadeSteps(), aPolyPolyList);
165 OUString aString = mpView->GetDescriptionOfMarkedObjects() +
166 " " + SdResId(STR_UNDO_MORPHING);
168 mpView->BegUndo(aString);
169 ImpInsertPolygons(aPolyPolyList, pDlg->IsAttributeFade(), pObj1, pObj2);
170 mpView->EndUndo();
175 static ::basegfx::B2DPolygon ImpGetExpandedPolygon(
176 const ::basegfx::B2DPolygon& rCandidate,
177 sal_uInt32 nNum
180 if(rCandidate.count() && nNum && rCandidate.count() != nNum)
182 // length of step in dest poly
183 ::basegfx::B2DPolygon aRetval;
184 const double fStep(::basegfx::utils::getLength(rCandidate) / static_cast<double>(rCandidate.isClosed() ? nNum : nNum - 1));
185 double fDestPos(0.0);
186 double fSrcPos(0.0);
187 sal_uInt32 nSrcPos(0);
188 sal_uInt32 nSrcPosNext((nSrcPos + 1 == rCandidate.count()) ? 0 : nSrcPos + 1);
189 double fNextSrcLen(::basegfx::B2DVector(rCandidate.getB2DPoint(nSrcPos) - rCandidate.getB2DPoint(nSrcPosNext)).getLength());
191 for(sal_uInt32 b(0); b < nNum; b++)
193 // calc fDestPos in source
194 while(fSrcPos + fNextSrcLen < fDestPos)
196 fSrcPos += fNextSrcLen;
197 nSrcPos++;
198 nSrcPosNext = (nSrcPos + 1 == rCandidate.count()) ? 0 : nSrcPos + 1;
199 fNextSrcLen = ::basegfx::B2DVector(rCandidate.getB2DPoint(nSrcPos) - rCandidate.getB2DPoint(nSrcPosNext)).getLength();
202 // fDestPos is between fSrcPos and (fSrcPos + fNextSrcLen)
203 const double fLenA((fDestPos - fSrcPos) / fNextSrcLen);
204 const ::basegfx::B2DPoint aOld1(rCandidate.getB2DPoint(nSrcPos));
205 const ::basegfx::B2DPoint aOld2(rCandidate.getB2DPoint(nSrcPosNext));
206 ::basegfx::B2DPoint aNewPoint(basegfx::interpolate(aOld1, aOld2, fLenA));
207 aRetval.append(aNewPoint);
209 // next step
210 fDestPos += fStep;
213 if(aRetval.count() >= 3)
215 aRetval.setClosed(rCandidate.isClosed());
218 return aRetval;
220 else
222 return rCandidate;
227 * make the point count of the polygons equal in adding points
229 void FuMorph::ImpEqualizePolyPointCount(
230 ::basegfx::B2DPolygon& rSmall,
231 const ::basegfx::B2DPolygon& rBig
234 // create poly with equal point count
235 const sal_uInt32 nCnt(rBig.count());
236 ::basegfx::B2DPolygon aPoly1(ImpGetExpandedPolygon(rSmall, nCnt));
238 // create transformation for rBig to do the compare
239 const ::basegfx::B2DRange aSrcSize(::basegfx::utils::getRange(rBig));
240 const ::basegfx::B2DPoint aSrcPos(aSrcSize.getCenter());
241 const ::basegfx::B2DRange aDstSize(::basegfx::utils::getRange(rSmall));
242 const ::basegfx::B2DPoint aDstPos(aDstSize.getCenter());
244 basegfx::B2DHomMatrix aTrans(basegfx::utils::createTranslateB2DHomMatrix(-aSrcPos.getX(), -aSrcPos.getY()));
245 aTrans.scale(aDstSize.getWidth() / aSrcSize.getWidth(), aDstSize.getHeight() / aSrcSize.getHeight());
246 aTrans.translate(aDstPos.getX(), aDstPos.getY());
248 // transpose points to have smooth linear blending
249 ::basegfx::B2DPolygon aPoly2;
250 aPoly2.append(::basegfx::B2DPoint(), nCnt);
251 sal_uInt32 nInd(ImpGetNearestIndex(aPoly1, aTrans * rBig.getB2DPoint(0)));
253 for(sal_uInt32 a(0); a < nCnt; a++)
255 aPoly2.setB2DPoint((a + nCnt - nInd) % nCnt, aPoly1.getB2DPoint(a));
258 aPoly2.setClosed(rBig.isClosed());
259 rSmall = aPoly2;
262 sal_uInt32 FuMorph::ImpGetNearestIndex(
263 const ::basegfx::B2DPolygon& rPoly,
264 const ::basegfx::B2DPoint& rPos
267 double fMinDist = 0.0;
268 sal_uInt32 nActInd = 0;
270 for(sal_uInt32 a(0); a < rPoly.count(); a++)
272 double fNewDist(::basegfx::B2DVector(rPoly.getB2DPoint(a) - rPos).getLength());
274 if(!a || fNewDist < fMinDist)
276 fMinDist = fNewDist;
277 nActInd = a;
281 return nActInd;
285 * add to a point reduced polys until count is same
287 void FuMorph::ImpAddPolys(
288 ::basegfx::B2DPolyPolygon& rSmaller,
289 const ::basegfx::B2DPolyPolygon& rBigger
292 while(rSmaller.count() < rBigger.count())
294 const ::basegfx::B2DPolygon& aToBeCopied(rBigger.getB2DPolygon(rSmaller.count()));
295 const ::basegfx::B2DRange aToBeCopiedPolySize(::basegfx::utils::getRange(aToBeCopied));
296 ::basegfx::B2DPoint aNewPoint(aToBeCopiedPolySize.getCenter());
297 ::basegfx::B2DPolygon aNewPoly;
299 const ::basegfx::B2DRange aSrcSize(::basegfx::utils::getRange(rBigger.getB2DPolygon(0)));
300 const ::basegfx::B2DPoint aSrcPos(aSrcSize.getCenter());
301 const ::basegfx::B2DRange aDstSize(::basegfx::utils::getRange(rSmaller.getB2DPolygon(0)));
302 const ::basegfx::B2DPoint aDstPos(aDstSize.getCenter());
303 aNewPoint = aNewPoint - aSrcPos + aDstPos;
305 for(sal_uInt32 a(0); a < aToBeCopied.count(); a++)
307 aNewPoly.append(aNewPoint);
310 rSmaller.append(aNewPoly);
315 * create group object with morphed polygons
317 void FuMorph::ImpInsertPolygons(
318 B2DPolyPolygonList_impl& rPolyPolyList3D,
319 bool bAttributeFade,
320 const SdrObject* pObj1,
321 const SdrObject* pObj2
324 Color aStartFillCol;
325 Color aEndFillCol;
326 Color aStartLineCol;
327 Color aEndLineCol;
328 ::tools::Long nStartLineWidth = 0;
329 ::tools::Long nEndLineWidth = 0;
330 SdrPageView* pPageView = mpView->GetSdrPageView();
331 SfxItemPool & rPool = pObj1->GetObjectItemPool();
332 SfxItemSetFixed<SDRATTR_START,SDRATTR_NOTPERSIST_FIRST-1,EE_ITEMS_START,EE_ITEMS_END> aSet1( rPool );
333 SfxItemSet aSet2( aSet1 );
334 bool bLineColor = false;
335 bool bFillColor = false;
336 bool bLineWidth = false;
337 bool bIgnoreLine = false;
338 bool bIgnoreFill = false;
340 aSet1.Put(pObj1->GetMergedItemSet());
341 aSet2.Put(pObj2->GetMergedItemSet());
343 const drawing::LineStyle eLineStyle1 = aSet1.Get(XATTR_LINESTYLE).GetValue();
344 const drawing::LineStyle eLineStyle2 = aSet2.Get(XATTR_LINESTYLE).GetValue();
345 const drawing::FillStyle eFillStyle1 = aSet1.Get(XATTR_FILLSTYLE).GetValue();
346 const drawing::FillStyle eFillStyle2 = aSet2.Get(XATTR_FILLSTYLE).GetValue();
348 if ( bAttributeFade )
350 if ( ( eLineStyle1 != drawing::LineStyle_NONE ) && ( eLineStyle2 != drawing::LineStyle_NONE ) )
352 bLineWidth = bLineColor = true;
354 aStartLineCol = aSet1.Get(XATTR_LINECOLOR).GetColorValue();
355 aEndLineCol = aSet2.Get(XATTR_LINECOLOR).GetColorValue();
357 nStartLineWidth = aSet1.Get(XATTR_LINEWIDTH).GetValue();
358 nEndLineWidth = aSet2.Get(XATTR_LINEWIDTH).GetValue();
360 else if ( ( eLineStyle1 == drawing::LineStyle_NONE ) && ( eLineStyle2 == drawing::LineStyle_NONE ) )
361 bIgnoreLine = true;
363 if ( ( eFillStyle1 == drawing::FillStyle_SOLID ) && ( eFillStyle2 == drawing::FillStyle_SOLID ) )
365 bFillColor = true;
366 aStartFillCol = aSet1.Get(XATTR_FILLCOLOR).GetColorValue();
367 aEndFillCol = aSet2.Get(XATTR_FILLCOLOR).GetColorValue();
369 else if ( ( eFillStyle1 == drawing::FillStyle_NONE ) && ( eFillStyle2 == drawing::FillStyle_NONE ) )
370 bIgnoreFill = true;
373 if ( !pPageView )
374 return;
376 SfxItemSet aSet( aSet1 );
377 rtl::Reference<SdrObjGroup> xObjGroup(new SdrObjGroup(mpView->getSdrModelFromSdrView()));
378 SdrObjList* pObjList = xObjGroup->GetSubList();
379 const size_t nCount = rPolyPolyList3D.size();
380 const double fStep = 1. / ( nCount + 1 );
381 const double fDelta = nEndLineWidth - nStartLineWidth;
382 double fFactor = fStep;
384 aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
385 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
387 for ( size_t i = 0; i < nCount; i++, fFactor += fStep )
389 const ::basegfx::B2DPolyPolygon& rPolyPoly3D = rPolyPolyList3D[ i ];
390 rtl::Reference<SdrPathObj> pNewObj = new SdrPathObj(
391 mpView->getSdrModelFromSdrView(),
392 SdrObjKind::Polygon,
393 rPolyPoly3D);
395 // line color
396 if ( bLineColor )
398 const basegfx::BColor aLineColor(basegfx::interpolate(aStartLineCol.getBColor(), aEndLineCol.getBColor(), fFactor));
399 aSet.Put( XLineColorItem( "", Color(aLineColor)));
401 else if ( bIgnoreLine )
402 aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
404 // fill color
405 if ( bFillColor )
407 const basegfx::BColor aFillColor(basegfx::interpolate(aStartFillCol.getBColor(), aEndFillCol.getBColor(), fFactor));
408 aSet.Put( XFillColorItem( "", Color(aFillColor)));
410 else if ( bIgnoreFill )
411 aSet.Put( XFillStyleItem( drawing::FillStyle_NONE ) );
413 // line width
414 if ( bLineWidth )
415 aSet.Put( XLineWidthItem( nStartLineWidth + static_cast<::tools::Long>( fFactor * fDelta + 0.5 ) ) );
417 pNewObj->SetMergedItemSetAndBroadcast(aSet);
419 pObjList->InsertObject( pNewObj.get() );
422 if ( nCount )
424 pObjList->InsertObject(
425 pObj1->CloneSdrObject(pObj1->getSdrModelFromSdrObject()).get(),
426 0 );
427 pObjList->InsertObject(
428 pObj2->CloneSdrObject(pObj2->getSdrModelFromSdrObject()).get() );
430 mpView->DeleteMarked();
431 mpView->InsertObjectAtView(xObjGroup.get(), *pPageView, SdrInsertFlags:: SETDEFLAYER);
436 * create single morphed PolyPolygon
438 ::basegfx::B2DPolyPolygon FuMorph::ImpCreateMorphedPolygon(
439 const ::basegfx::B2DPolyPolygon& rPolyPolyStart,
440 const ::basegfx::B2DPolyPolygon& rPolyPolyEnd,
441 double fMorphingFactor
444 ::basegfx::B2DPolyPolygon aNewPolyPolygon;
445 const double fFactor = 1.0 - fMorphingFactor;
447 for(sal_uInt32 a(0); a < rPolyPolyStart.count(); a++)
449 const ::basegfx::B2DPolygon& aPolyStart(rPolyPolyStart.getB2DPolygon(a));
450 const ::basegfx::B2DPolygon& aPolyEnd(rPolyPolyEnd.getB2DPolygon(a));
451 const sal_uInt32 nCount(aPolyStart.count());
452 ::basegfx::B2DPolygon aNewPolygon;
454 for(sal_uInt32 b(0); b < nCount; b++)
456 const ::basegfx::B2DPoint& aPtStart(aPolyStart.getB2DPoint(b));
457 const ::basegfx::B2DPoint& aPtEnd(aPolyEnd.getB2DPoint(b));
458 aNewPolygon.append(aPtEnd + ((aPtStart - aPtEnd) * fFactor));
461 aNewPolygon.setClosed(aPolyStart.isClosed() && aPolyEnd.isClosed());
462 aNewPolyPolygon.append(aNewPolygon);
465 return aNewPolyPolygon;
469 * create morphed PolyPolygons
471 void FuMorph::ImpMorphPolygons(
472 const ::basegfx::B2DPolyPolygon& rPolyPoly1,
473 const ::basegfx::B2DPolyPolygon& rPolyPoly2,
474 const sal_uInt16 nSteps,
475 B2DPolyPolygonList_impl& rPolyPolyList3D
478 if(!nSteps)
479 return;
481 const ::basegfx::B2DRange aStartPolySize(::basegfx::utils::getRange(rPolyPoly1));
482 const ::basegfx::B2DPoint aStartCenter(aStartPolySize.getCenter());
483 const ::basegfx::B2DRange aEndPolySize(::basegfx::utils::getRange(rPolyPoly2));
484 const ::basegfx::B2DPoint aEndCenter(aEndPolySize.getCenter());
485 const ::basegfx::B2DPoint aDelta(aEndCenter - aStartCenter);
486 const double fFactor(1.0 / (nSteps + 1));
487 double fValue(0.0);
489 for(sal_uInt16 i(0); i < nSteps; i++)
491 fValue += fFactor;
492 ::basegfx::B2DPolyPolygon aNewPolyPoly2D = ImpCreateMorphedPolygon(rPolyPoly1, rPolyPoly2, fValue);
494 const ::basegfx::B2DRange aNewPolySize(::basegfx::utils::getRange(aNewPolyPoly2D));
495 const ::basegfx::B2DPoint aNewS(aNewPolySize.getCenter());
496 const ::basegfx::B2DPoint aRealS(aStartCenter + (aDelta * fValue));
497 const ::basegfx::B2DPoint aDiff(aRealS - aNewS);
499 aNewPolyPoly2D.transform(basegfx::utils::createTranslateB2DHomMatrix(aDiff));
500 rPolyPolyList3D.push_back( std::move(aNewPolyPoly2D) );
504 } // end of namespace sd
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */