Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / view / main / AbstractShapeFactory.cxx
blob87a09efa181009afb0f4991e9dead6e6b579dbaa
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/.
8 */
10 #include <PropertyMapper.hxx>
11 #include <CommonConverters.hxx>
13 #include <AbstractShapeFactory.hxx>
14 #include <com/sun/star/beans/XPropertySet.hpp>
15 #include <com/sun/star/drawing/CircleKind.hpp>
16 #include <com/sun/star/drawing/DoubleSequence.hpp>
17 #include <com/sun/star/drawing/FlagSequence.hpp>
18 #include <com/sun/star/drawing/FillStyle.hpp>
19 #include <com/sun/star/drawing/LineStyle.hpp>
20 #include <com/sun/star/drawing/NormalsKind.hpp>
21 #include <com/sun/star/drawing/PointSequence.hpp>
22 #include <com/sun/star/drawing/PolygonKind.hpp>
23 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
24 #include <com/sun/star/drawing/ProjectionMode.hpp>
25 #include <com/sun/star/drawing/ShadeMode.hpp>
26 #include <com/sun/star/drawing/TextFitToSizeType.hpp>
27 #include <com/sun/star/drawing/TextureProjectionMode.hpp>
28 #include <com/sun/star/text/XText.hpp>
29 #include <com/sun/star/uno/Any.hxx>
31 #include <editeng/unoprnms.hxx>
32 #include <rtl/math.hxx>
33 #include <tools/svlibrary.h>
34 #include <svx/svdocirc.hxx>
35 #include <svx/svdopath.hxx>
36 #include <vcl/svapp.hxx>
38 #include <basegfx/point/b2dpoint.hxx>
39 #include <basegfx/matrix/b2dhommatrix.hxx>
40 #include <basegfx/matrix/b3dhommatrix.hxx>
42 #include <osl/module.hxx>
44 #include <OpenglShapeFactory.hxx>
45 #include <ShapeFactory.hxx>
47 #include <config_features.h>
49 using namespace com::sun::star;
51 namespace chart {
53 namespace {
55 typedef opengl::OpenglShapeFactory* (*getOpenglShapeFactory_)(void);
57 #if HAVE_FEATURE_UI
59 #ifndef DISABLE_DYNLOADING
61 void thisModule() {}
63 osl::Module* getOpenGLModule()
65 static osl::Module aModule;
66 if (aModule.is())
67 // Already loaded.
68 return &aModule;
70 OUString aLibName(SVLIBRARY("chartopengl"));
71 bool bLoaded = aModule.loadRelative(&thisModule, aLibName);
72 if (!bLoaded)
73 bLoaded = aModule.load(aLibName);
75 return bLoaded ? &aModule : nullptr;
78 #endif
80 #endif
84 #ifdef DISABLE_DYNLOADING
85 extern "C" opengl::OpenglShapeFactory* getOpenglShapeFactory();
86 #endif
88 AbstractShapeFactory* AbstractShapeFactory::getOrCreateShapeFactory(const uno::Reference< lang::XMultiServiceFactory>& xFactory)
90 static AbstractShapeFactory* pShapeFactory = nullptr;
92 if (pShapeFactory)
93 return pShapeFactory;
95 #if HAVE_FEATURE_UI
96 if(getenv("CHART_DUMMY_FACTORY") && !Application::IsHeadlessModeEnabled())
98 #ifndef DISABLE_DYNLOADING
99 osl::Module* pModule = getOpenGLModule();
100 if(pModule)
102 oslGenericFunction fn = pModule->getFunctionSymbol("getOpenglShapeFactory");
103 if(fn)
106 pShapeFactory = reinterpret_cast<getOpenglShapeFactory_>(fn)();
107 pShapeFactory->m_xShapeFactory = xFactory;
110 #elif defined(IOS) || defined(ANDROID) // Library_chartopengl is not portable enough yet
111 pShapeFactory = NULL;
112 #else
113 pShapeFactory = getOpenglShapeFactory();
114 pShapeFactory->m_xShapeFactory = xFactory;
115 #endif
117 #endif
119 if (!pShapeFactory)
120 pShapeFactory = new ShapeFactory(xFactory);
122 return pShapeFactory;
125 uno::Reference< drawing::XShapes > AbstractShapeFactory::getChartRootShape(
126 const uno::Reference< drawing::XDrawPage>& xDrawPage )
128 uno::Reference< drawing::XShapes > xRet;
129 uno::Reference< drawing::XShapes > xShapes( xDrawPage, uno::UNO_QUERY );
130 if( xShapes.is() )
132 sal_Int32 nCount = xShapes->getCount();
133 uno::Reference< drawing::XShape > xShape;
134 for( sal_Int32 nN = nCount; nN--; )
136 if( xShapes->getByIndex( nN ) >>= xShape )
138 if( AbstractShapeFactory::getShapeName( xShape ) == "com.sun.star.chart2.shapes" )
140 xRet.set( xShape, uno::UNO_QUERY );
141 break;
146 return xRet;
149 void AbstractShapeFactory::makeShapeInvisible( const uno::Reference< drawing::XShape >& xShape )
151 uno::Reference< beans::XPropertySet > xShapeProp( xShape, uno::UNO_QUERY );
152 OSL_ENSURE(xShapeProp.is(), "created shape offers no XPropertySet");
153 if( xShapeProp.is())
157 xShapeProp->setPropertyValue( "LineStyle", uno::Any( drawing::LineStyle_NONE ));
158 xShapeProp->setPropertyValue( "FillStyle", uno::Any( drawing::FillStyle_NONE ));
160 catch( const uno::Exception& e )
162 SAL_WARN("chart2", "Exception caught. " << e );
167 // set a name/CID at a shape (is used for selection handling)
169 void AbstractShapeFactory::setShapeName( const uno::Reference< drawing::XShape >& xShape
170 , const OUString& rName )
172 if(!xShape.is())
173 return;
174 uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
175 OSL_ENSURE(xProp.is(), "shape offers no XPropertySet");
176 if( xProp.is())
180 xProp->setPropertyValue( UNO_NAME_MISC_OBJ_NAME
181 , uno::Any( rName ) );
183 catch( const uno::Exception& e )
185 SAL_WARN("chart2", "Exception caught. " << e );
190 OUString AbstractShapeFactory::getShapeName( const uno::Reference< drawing::XShape >& xShape )
192 OUString aRet;
194 uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
195 OSL_ENSURE(xProp.is(), "shape offers no XPropertySet");
196 if( xProp.is())
200 xProp->getPropertyValue( UNO_NAME_MISC_OBJ_NAME ) >>= aRet;
202 catch( const uno::Exception& e )
204 SAL_WARN("chart2", "Exception caught. " << e );
208 return aRet;
211 uno::Any AbstractShapeFactory::makeTransformation( const awt::Point& rScreenPosition2D, double fRotationAnglePi )
213 ::basegfx::B2DHomMatrix aM;
214 //As autogrow is active the rectangle is automatically expanded to that side
215 //to which the text is not adjusted.
216 // aM.scale( 1, 1 ); Oops? A scale with this parameters is neutral, line commented out
217 aM.rotate( fRotationAnglePi );
218 aM.translate( rScreenPosition2D.X, rScreenPosition2D.Y );
219 uno::Any aATransformation( B2DHomMatrixToHomogenMatrix3(aM) );
220 return aATransformation;
223 OUString AbstractShapeFactory::getStackedString( const OUString& rString, bool bStacked )
225 sal_Int32 nLen = rString.getLength();
226 if(!bStacked || !nLen)
227 return rString;
229 OUStringBuffer aStackStr;
231 //add a newline after each letter
232 //as we do not no letters here add a newline after each char
233 for( sal_Int32 nPosSrc=0; nPosSrc < nLen; nPosSrc++ )
235 if( nPosSrc )
236 aStackStr.append( '\r' );
237 aStackStr.append(rString[nPosSrc]);
239 return aStackStr.makeStringAndClear();
242 bool AbstractShapeFactory::hasPolygonAnyLines( drawing::PolyPolygonShape3D& rPoly)
244 // #i67757# check all contained polygons, if at least one polygon contains 2 or more points, return true
245 for( sal_Int32 nIdx = 0, nCount = rPoly.SequenceX.getLength(); nIdx < nCount; ++nIdx )
246 if( rPoly.SequenceX[ nIdx ].getLength() > 1 )
247 return true;
248 return false;
251 bool AbstractShapeFactory::isPolygonEmptyOrSinglePoint( drawing::PolyPolygonShape3D& rPoly)
253 // true, if empty polypolygon or one polygon with one point
254 return (rPoly.SequenceX.getLength() == 0) ||
255 ((rPoly.SequenceX.getLength() == 1) && (rPoly.SequenceX[0].getLength() <= 1));
258 void AbstractShapeFactory::closePolygon( drawing::PolyPolygonShape3D& rPoly)
260 OSL_ENSURE( rPoly.SequenceX.getLength() <= 1, "AbstractShapeFactory::closePolygon - single polygon expected" );
261 //add a last point == first point
262 if(isPolygonEmptyOrSinglePoint(rPoly))
263 return;
264 drawing::Position3D aFirst(rPoly.SequenceX[0][0],rPoly.SequenceY[0][0],rPoly.SequenceZ[0][0]);
265 AddPointToPoly( rPoly, aFirst );
268 awt::Size AbstractShapeFactory::calculateNewSizeRespectingAspectRatio(
269 const awt::Size& rTargetSize
270 , const awt::Size& rSourceSizeWithCorrectAspectRatio )
272 awt::Size aNewSize;
274 double fFactorWidth = double(rTargetSize.Width)/double(rSourceSizeWithCorrectAspectRatio.Width);
275 double fFactorHeight = double(rTargetSize.Height)/double(rSourceSizeWithCorrectAspectRatio.Height);
276 double fFactor = std::min(fFactorWidth,fFactorHeight);
277 aNewSize.Width=static_cast<sal_Int32>(fFactor*rSourceSizeWithCorrectAspectRatio.Width);
278 aNewSize.Height=static_cast<sal_Int32>(fFactor*rSourceSizeWithCorrectAspectRatio.Height);
280 return aNewSize;
283 awt::Point AbstractShapeFactory::calculateTopLeftPositionToCenterObject(
284 const awt::Point& rTargetAreaPosition
285 , const awt::Size& rTargetAreaSize
286 , const awt::Size& rObjectSize )
288 awt::Point aNewPosition(rTargetAreaPosition);
289 aNewPosition.X += static_cast<sal_Int32>(double(rTargetAreaSize.Width-rObjectSize.Width)/2.0);
290 aNewPosition.Y += static_cast<sal_Int32>(double(rTargetAreaSize.Height-rObjectSize.Height)/2.0);
291 return aNewPosition;
294 ::basegfx::B2IRectangle AbstractShapeFactory::getRectangleOfShape(
295 const uno::Reference< drawing::XShape >& xShape )
297 ::basegfx::B2IRectangle aRet;
299 if( xShape.is() )
301 awt::Point aPos = xShape->getPosition();
302 awt::Size aSize = xShape->getSize();
303 aRet = BaseGFXHelper::makeRectangle(aPos,aSize);
305 return aRet;
309 awt::Size AbstractShapeFactory::getSizeAfterRotation(
310 const uno::Reference< drawing::XShape >& xShape, double fRotationAngleDegree )
312 awt::Size aRet(0,0);
313 if(xShape.is())
315 const awt::Size aSize( xShape->getSize() );
317 if( fRotationAngleDegree == 0.0 )
318 aRet = aSize;
319 else
321 while(fRotationAngleDegree>=360.0)
322 fRotationAngleDegree-=360.0;
323 while(fRotationAngleDegree<0.0)
324 fRotationAngleDegree+=360.0;
325 if(fRotationAngleDegree>270.0)
326 fRotationAngleDegree=360.0-fRotationAngleDegree;
327 else if(fRotationAngleDegree>180.0)
328 fRotationAngleDegree=fRotationAngleDegree-180.0;
329 else if(fRotationAngleDegree>90.0)
330 fRotationAngleDegree=180.0-fRotationAngleDegree;
332 const double fAnglePi = fRotationAngleDegree*F_PI/180.0;
334 aRet.Height = static_cast<sal_Int32>(
335 aSize.Width*rtl::math::sin( fAnglePi )
336 + aSize.Height*rtl::math::cos( fAnglePi ));
337 aRet.Width = static_cast<sal_Int32>(
338 aSize.Width*rtl::math::cos( fAnglePi )
339 + aSize.Height*rtl::math::sin( fAnglePi ));
342 return aRet;
345 void AbstractShapeFactory::removeSubShapes( const uno::Reference< drawing::XShapes >& xShapes )
347 if( xShapes.is() )
349 sal_Int32 nSubCount = xShapes->getCount();
350 uno::Reference< drawing::XShape > xShape;
351 for( sal_Int32 nS = nSubCount; nS--; )
353 if( xShapes->getByIndex( nS ) >>= xShape )
354 xShapes->remove( xShape );
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */