tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / oox / source / ppt / slidepersist.cxx
blob446b7d5623a21160c57b215f1dae741130ae8c0c
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 <basegfx/matrix/b2dhommatrix.hxx>
21 #include <com/sun/star/drawing/XDrawPage.hpp>
22 #include <com/sun/star/drawing/XShapes.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <oox/ppt/timenode.hxx>
25 #include <oox/ppt/pptshape.hxx>
26 #include <oox/ppt/slidepersist.hxx>
27 #include <drawingml/fillproperties.hxx>
28 #include <oox/drawingml/shapepropertymap.hxx>
29 #include <oox/helper/propertymap.hxx>
30 #include <oox/helper/propertyset.hxx>
31 #include <oox/vml/vmldrawing.hxx>
32 #include <oox/token/properties.hxx>
33 #include <oox/token/tokens.hxx>
34 #include <oox/core/xmlfilterbase.hxx>
35 #include <drawingml/textliststyle.hxx>
36 #include <drawingml/textparagraphproperties.hxx>
37 #include <drawingml/connectorhelper.hxx>
39 #include <osl/diagnose.h>
41 #include <com/sun/star/style/XStyle.hpp>
42 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
43 #include <com/sun/star/container/XNamed.hpp>
44 #include <com/sun/star/animations/XAnimationNodeSupplier.hpp>
45 #include <com/sun/star/drawing/XGluePointsSupplier.hpp>
46 #include <com/sun/star/container/XIdentifierContainer.hpp>
47 #include <com/sun/star/drawing/EnhancedCustomShapeGluePointType.hpp>
48 #include <com/sun/star/drawing/ConnectorType.hpp>
49 #include <utility>
50 #include <svx/svdobj.hxx>
51 #include <svx/svdmodel.hxx>
53 using namespace ::com::sun::star;
54 using namespace ::oox::core;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::drawing;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::animations;
61 namespace oox::ppt {
63 SlidePersist::SlidePersist( XmlFilterBase& rFilter, bool bMaster, bool bNotes,
64 const css::uno::Reference< css::drawing::XDrawPage >& rxPage,
65 oox::drawingml::ShapePtr pShapesPtr, drawingml::TextListStylePtr pDefaultTextStyle )
66 : mpDrawingPtr( std::make_shared<oox::vml::Drawing>( rFilter, rxPage, oox::vml::VMLDRAWING_POWERPOINT ) )
67 , mxPage( rxPage )
68 , maShapesPtr(std::move( pShapesPtr ))
69 , mnLayoutValueToken( 0 )
70 , mbMaster( bMaster )
71 , mbNotes ( bNotes )
72 , maDefaultTextStylePtr(std::move( pDefaultTextStyle ))
73 , maTitleTextStylePtr( std::make_shared<oox::drawingml::TextListStyle>() )
74 , maBodyTextStylePtr( std::make_shared<oox::drawingml::TextListStyle>() )
75 , maNotesTextStylePtr( std::make_shared<oox::drawingml::TextListStyle>() )
76 , maOtherTextStylePtr( std::make_shared<oox::drawingml::TextListStyle>() )
78 #if OSL_DEBUG_LEVEL > 0
79 mxDebugPage = mxPage;
80 #endif
83 #if OSL_DEBUG_LEVEL > 0
84 css::uno::WeakReference< css::drawing::XDrawPage > SlidePersist::mxDebugPage;
85 #endif
87 SlidePersist::~SlidePersist()
91 sal_Int16 SlidePersist::getLayoutFromValueToken() const
93 sal_Int16 nLayout = 20; // 20 == blank (so many magic numbers :-( the description at com.sun.star.presentation.DrawPage.Layout does not help)
94 switch( mnLayoutValueToken )
96 case XML_blank: nLayout = 20; break;
97 case XML_chart: nLayout = 2; break;
98 case XML_chartAndTx: nLayout = 7; break;
99 case XML_clipArtAndTx: nLayout = 9; break;
100 case XML_clipArtAndVertTx: nLayout = 24; break;
101 case XML_fourObj: nLayout = 18; break;
102 case XML_obj: nLayout = 11; break;
103 case XML_objAndTx: nLayout = 13; break;
104 case XML_objOverTx: nLayout = 14; break;
105 case XML_tbl: nLayout = 8; break;
106 case XML_title: nLayout = 0; break;
107 case XML_titleOnly: nLayout = 19; break;
108 case XML_twoObj:
109 case XML_twoColTx: nLayout = 3; break;
110 case XML_twoObjAndObj:
111 case XML_twoObjAndTx: nLayout = 15; break;
112 case XML_twoObjOverTx: nLayout = 16; break;
113 case XML_tx: nLayout = 1; break;
114 case XML_txAndChart: nLayout = 4; break;
115 case XML_txAndClipArt: nLayout = 6; break;
116 case XML_txAndMedia: nLayout = 6; break;
117 case XML_txAndObj: nLayout = 10; break;
118 case XML_objAndTwoObj:
119 case XML_txAndTwoObj: nLayout = 12; break;
120 case XML_txOverObj: nLayout = 17; break;
121 case XML_vertTitleAndTx: nLayout = 22; break;
122 case XML_vertTitleAndTxOverChart: nLayout = 21; break;
123 case XML_vertTx: nLayout = 23; break;
124 case XML_objOnly: nLayout = 32; break;
126 case XML_twoTxTwoObj:
127 case XML_objTx:
128 case XML_picTx:
129 case XML_secHead:
130 case XML_mediaAndTx:
131 case XML_dgm:
132 case XML_cust:
133 default:
134 nLayout = 20;
136 return nLayout;
139 static void lcl_createShapeMap(oox::drawingml::ShapePtr rShapePtr,
140 oox::drawingml::ShapeIdMap& rShapeMap)
142 std::vector<oox::drawingml::ShapePtr>& rChildren = rShapePtr->getChildren();
143 if (!rChildren.empty())
145 for (const auto& pIt : rChildren)
147 if (pIt->isConnectorShape())
149 rShapeMap[pIt->getId()] = pIt; // add child itself
150 lcl_createShapeMap(pIt, rShapeMap); // and all its descendants
154 else
156 if(rShapePtr->isConnectorShape())
157 rShapeMap[rShapePtr->getId()] = rShapePtr;
161 void SlidePersist::createXShapes( XmlFilterBase& rFilterBase )
163 applyTextStyles( rFilterBase );
165 Reference< XShapes > xShapes( getPage() );
166 std::vector< oox::drawingml::ShapePtr >& rShapes( maShapesPtr->getChildren() );
167 oox::drawingml::ShapeIdMap aConnectorShapeMap;
169 for (auto const& shape : rShapes)
171 std::vector< oox::drawingml::ShapePtr >& rChildren( shape->getChildren() );
172 for (auto const& child : rChildren)
174 PPTShape* pPPTShape = dynamic_cast< PPTShape* >( child.get() );
175 basegfx::B2DHomMatrix aTransformation;
176 if ( pPPTShape )
178 pPPTShape->addShape( rFilterBase, *this, getTheme().get(), xShapes, aTransformation, &getShapeMap() );
180 const auto pIter = maShapeMap.find(pPPTShape->getId());
181 if (pIter != maShapeMap.end())
182 lcl_createShapeMap(pIter->second, aConnectorShapeMap);
184 else
185 child->addShape( rFilterBase, getTheme().get(), xShapes, aTransformation, maShapesPtr->getFillProperties(), &getShapeMap() );
189 if (!aConnectorShapeMap.empty())
191 for (auto& pIt : aConnectorShapeMap)
193 ConnectorHelper::applyConnections(pIt.second, getShapeMap());
195 SdrObject* pObj = SdrObject::getSdrObjectFromXShape(pIt.second->getXShape());
196 SdrModel& rModel(pObj->getSdrModelFromSdrObject());
197 rModel.setLock(false);
199 if (pIt.second->getConnectorName() == u"bentConnector3"_ustr
200 || pIt.second->getConnectorName() == u"bentConnector4"_ustr
201 || pIt.second->getConnectorName() == u"bentConnector5"_ustr)
203 ConnectorHelper::applyBentHandleAdjustments(pIt.second);
205 else if (pIt.second->getConnectorName() == u"curvedConnector3"_ustr
206 || pIt.second->getConnectorName() == u"curvedConnector4"_ustr
207 || pIt.second->getConnectorName() == u"curvedConnector5"_ustr)
209 ConnectorHelper::applyCurvedHandleAdjustments(pIt.second);
211 else // bentConnector2
212 createConnectorShapeConnection(pIt.second);
216 Reference< XAnimationNodeSupplier > xNodeSupplier( getPage(), UNO_QUERY);
217 if( !xNodeSupplier.is() )
218 return;
220 Reference< XAnimationNode > xNode( xNodeSupplier->getAnimationNode() );
221 if( xNode.is() && !maTimeNodeList.empty() )
223 SlidePersistPtr pSlidePtr( shared_from_this() );
224 TimeNodePtr pNode(maTimeNodeList.front());
225 OSL_ENSURE( pNode, "pNode" );
227 Reference<XAnimationNode> xDummy;
228 pNode->setNode(rFilterBase, xNode, pSlidePtr, xDummy);
232 void SlidePersist::createBackground( const XmlFilterBase& rFilterBase )
234 if ( mpBackgroundPropertiesPtr )
236 ::Color nPhClr = maBackgroundColor.isUsed() ?
237 maBackgroundColor.getColor( rFilterBase.getGraphicHelper() ) : API_RGB_TRANSPARENT;
239 css::awt::Size aSize;
240 Reference< css::beans::XPropertySet > xSet(mxPage, UNO_QUERY);
241 xSet->getPropertyValue(u"Width"_ustr) >>= aSize.Width;
242 xSet->getPropertyValue(u"Height"_ustr) >>= aSize.Height;
244 oox::drawingml::ShapePropertyIds aPropertyIds = oox::drawingml::ShapePropertyInfo::DEFAULT.mrPropertyIds;
245 aPropertyIds[oox::drawingml::ShapeProperty::FillGradient] = PROP_FillGradientName;
246 oox::drawingml::ShapePropertyInfo aPropInfo( aPropertyIds, true, false, true, false, false );
247 oox::drawingml::ShapePropertyMap aPropMap( rFilterBase.getModelObjectHelper(), aPropInfo );
248 mpBackgroundPropertiesPtr->pushToPropMap( aPropMap, rFilterBase.getGraphicHelper(), 0, nPhClr, aSize);
249 PropertySet( mxPage ).setProperty( PROP_Background, aPropMap.makePropertySet() );
253 static void setTextStyle( Reference< beans::XPropertySet > const & rxPropSet, const XmlFilterBase& rFilter,
254 oox::drawingml::TextListStylePtr const & pTextListStylePtr, int nLevel )
256 ::oox::drawingml::TextParagraphProperties* pTextParagraphPropertiesPtr( &pTextListStylePtr->getListStyle()[ nLevel ] );
257 if( pTextParagraphPropertiesPtr == nullptr )
259 // no properties. return
260 return;
263 PropertyMap& rTextParagraphPropertyMap( pTextParagraphPropertiesPtr->getTextParagraphPropertyMap() );
265 PropertySet aPropSet( rxPropSet );
266 aPropSet.setProperties( rTextParagraphPropertyMap );
267 pTextParagraphPropertiesPtr->getTextCharacterProperties().pushToPropSet( aPropSet, rFilter );
270 void SlidePersist::applyTextStyles( const XmlFilterBase& rFilterBase )
272 if ( !mbMaster )
273 return;
277 Reference< style::XStyleFamiliesSupplier > aXStyleFamiliesSupplier( rFilterBase.getModel(), UNO_QUERY_THROW );
278 Reference< container::XNameAccess > aXNameAccess( aXStyleFamiliesSupplier->getStyleFamilies() );
279 Reference< container::XNamed > aXNamed( mxPage, UNO_QUERY_THROW );
281 if ( aXNameAccess.is() )
283 oox::drawingml::TextListStylePtr pTextListStylePtr;
284 OUString aStyle;
285 OUString aFamily;
287 static constexpr OUStringLiteral sOutline( u"outline1" );
288 static constexpr OUString sTitle( u"title"_ustr );
289 static constexpr OUStringLiteral sStandard( u"standard" );
290 static constexpr OUStringLiteral sSubtitle( u"subtitle" );
292 for( int i = 0; i < 4; i++ ) // todo: aggregation of bodystyle (subtitle)
294 switch( i )
296 case 0 : // title style
298 pTextListStylePtr = maTitleTextStylePtr;
299 aStyle = sTitle;
300 aFamily= aXNamed->getName();
301 break;
303 case 1 : // body style
305 pTextListStylePtr = maBodyTextStylePtr;
306 aStyle = sOutline;
307 aFamily= aXNamed->getName();
308 break;
310 case 3 : // notes style
312 pTextListStylePtr = maNotesTextStylePtr;
313 aStyle = sTitle;
314 aFamily= aXNamed->getName();
315 break;
317 case 4 : // standard style
319 pTextListStylePtr = maOtherTextStylePtr;
320 aStyle = sStandard;
321 aFamily = "graphics";
322 break;
324 case 5 : // subtitle
326 pTextListStylePtr = maBodyTextStylePtr;
327 aStyle = sSubtitle;
328 aFamily = aXNamed->getName();
329 break;
332 Reference< container::XNameAccess > xFamilies;
333 if ( aXNameAccess->hasByName( aFamily ) )
335 if( aXNameAccess->getByName( aFamily ) >>= xFamilies )
337 if ( xFamilies->hasByName( aStyle ) )
339 Reference< style::XStyle > aXStyle;
340 if ( xFamilies->getByName( aStyle ) >>= aXStyle )
342 Reference< beans::XPropertySet > xPropSet( aXStyle, UNO_QUERY_THROW );
343 setTextStyle( xPropSet, rFilterBase, maDefaultTextStylePtr, 0 );
344 setTextStyle( xPropSet, rFilterBase, pTextListStylePtr, 0 );
345 if ( i == 1 /* BodyStyle */ )
347 for ( int nLevel = 1; nLevel < 5; nLevel++ )
350 char pOutline[ 9 ] = "outline1";
351 pOutline[ 7 ] = static_cast< char >( '0' + nLevel );
352 OUString sOutlineStyle( OUString::createFromAscii( pOutline ) );
353 if ( xFamilies->hasByName( sOutlineStyle ) )
355 xFamilies->getByName( sOutlineStyle ) >>= aXStyle;
356 if( aXStyle.is() )
357 xPropSet.set( aXStyle, UNO_QUERY_THROW );
360 setTextStyle( xPropSet, rFilterBase, maDefaultTextStylePtr, nLevel );
361 setTextStyle( xPropSet, rFilterBase, pTextListStylePtr, nLevel );
371 catch( const Exception& )
376 void SlidePersist::hideShapesAsMasterShapes()
378 std::vector< oox::drawingml::ShapePtr >& rShapes( maShapesPtr->getChildren() );
379 for (auto const& shape : rShapes)
381 std::vector< oox::drawingml::ShapePtr >& rChildren( shape->getChildren() );
382 for (auto const& child : rChildren)
384 PPTShape* pPPTShape = dynamic_cast< PPTShape* >( child.get() );
385 if (!pPPTShape)
386 continue;
387 pPPTShape->setHiddenMasterShape( true );
392 // This angle determines in the direction of the line
393 static sal_Int32 lcl_GetAngle(uno::Reference<drawing::XShape>& rXShape, awt::Point& rPt)
395 SdrObject* pObj = SdrObject::getSdrObjectFromXShape(rXShape);
396 tools::Rectangle aR(pObj->GetSnapRect());
397 sal_Int32 nLeftX = rPt.X - aR.Left();
398 sal_Int32 nTopY = rPt.Y - aR.Top();
399 sal_Int32 nRightX = aR.Right() - rPt.X;
400 sal_Int32 nBottomY = aR.Bottom() - rPt.Y;
401 sal_Int32 nX = std::min(nLeftX, nRightX);
402 sal_Int32 nY = std::min(nTopY, nBottomY);
404 sal_Int32 nAngle;
405 if (nX < nY)
407 if (nLeftX < nRightX)
408 nAngle = 180; // Left
409 else
410 nAngle = 0; // Right
412 else
414 if (nTopY < nBottomY)
415 nAngle = 270; // Top
416 else
417 nAngle = 90; // Bottom
419 return nAngle;
422 Reference<XAnimationNode> SlidePersist::getAnimationNode(const OUString& sId) const
424 const auto pIter = maAnimNodesMap.find(sId);
425 if (pIter != maAnimNodesMap.end())
426 return pIter->second;
428 Reference<XAnimationNode> aResult;
429 return aResult;
432 static void lcl_SetEdgeLineValue(uno::Reference<drawing::XShape>& rXConnector,
433 oox::drawingml::ShapePtr& rShapePtr)
435 sal_Int32 nEdge = 0;
436 awt::Point aStartPt, aEndPt;
437 tools::Rectangle aS, aE; // Start, End rectangle
438 uno::Reference<drawing::XShape> xStartSp, xEndSp;
439 uno::Reference<beans::XPropertySet> xPropSet(rXConnector, uno::UNO_QUERY);
440 xPropSet->getPropertyValue(u"EdgeStartPoint"_ustr) >>= aStartPt;
441 xPropSet->getPropertyValue(u"EdgeEndPoint"_ustr) >>= aEndPt;
442 xPropSet->getPropertyValue(u"StartShape"_ustr) >>= xStartSp;
443 xPropSet->getPropertyValue(u"EndShape"_ustr) >>= xEndSp;
444 xPropSet->setPropertyValue(u"EdgeNode1HorzDist"_ustr, Any(sal_Int32(0)));
445 xPropSet->setPropertyValue(u"EdgeNode1VertDist"_ustr, Any(sal_Int32(0)));
446 xPropSet->setPropertyValue(u"EdgeNode2HorzDist"_ustr, Any(sal_Int32(0)));
447 xPropSet->setPropertyValue(u"EdgeNode2VertDist"_ustr, Any(sal_Int32(0)));
449 SdrObject* pStartObj = xStartSp.is() ? SdrObject::getSdrObjectFromXShape(xStartSp) : nullptr;
450 SdrObject* pEndObj = xEndSp.is() ? SdrObject::getSdrObjectFromXShape(xEndSp) : nullptr;
452 sal_Int32 nStartA = -1;
453 sal_Int32 nEndA = -1;
454 if (pStartObj)
456 aS = pStartObj->GetSnapRect();
457 nStartA = lcl_GetAngle(xStartSp, aStartPt);
459 if (pEndObj)
461 aE = pEndObj->GetSnapRect();
462 nEndA = lcl_GetAngle(xEndSp, aEndPt);
465 const OUString sConnectorName = rShapePtr->getConnectorName();
466 if (sConnectorName == "bentConnector2")
468 awt::Size aConnSize = rXConnector->getSize();
469 if (xStartSp.is() || xEndSp.is())
471 if (nStartA >= 0)
473 switch (nStartA)
475 case 0: nEdge = aEndPt.X - aS.Right(); break;
476 case 180: nEdge = aEndPt.X - aS.Left(); break;
477 case 90: nEdge = aEndPt.Y - aS.Bottom(); break;
478 case 270: nEdge = aEndPt.Y - aS.Top(); break;
481 else
483 switch (nEndA)
485 case 0: nEdge = aStartPt.X - aE.Right(); break;
486 case 180: nEdge = aStartPt.X - aE.Left(); break;
487 case 90: nEdge = aStartPt.Y - aE.Bottom(); break;
488 case 270: nEdge = aStartPt.Y - aE.Top(); break;
492 else
494 bool bFlipH = rShapePtr->getFlipH();
495 bool bFlipV = rShapePtr->getFlipV();
496 sal_Int32 nConnectorAngle = rShapePtr->getRotation() / 60000;
497 if (aConnSize.Height < aConnSize.Width)
499 if ((nConnectorAngle == 90 && bFlipH && bFlipV) || (nConnectorAngle == 180)
500 || (nConnectorAngle == 270 && bFlipH))
501 nEdge -= aConnSize.Width;
502 else
503 nEdge += aConnSize.Width;
505 else
507 if ((nConnectorAngle == 180 && bFlipV) || (nConnectorAngle == 270 && bFlipV)
508 || (nConnectorAngle == 90 && bFlipH && bFlipV)
509 || (nConnectorAngle == 0 && !bFlipV))
510 nEdge -= aConnSize.Height;
511 else
512 nEdge += aConnSize.Height;
515 xPropSet->setPropertyValue(u"EdgeLine1Delta"_ustr, Any(nEdge / 2));
519 // create connection between two shape with a connector shape.
520 void SlidePersist::createConnectorShapeConnection(oox::drawingml::ShapePtr& pConnector)
522 oox::drawingml::ConnectorShapePropertiesList aConnectorShapeProperties
523 = pConnector->getConnectorShapeProperties();
524 uno::Reference<drawing::XShape> xConnector(pConnector->getXShape(), uno::UNO_QUERY);
525 uno::Reference<beans::XPropertySet> xPropertySet(xConnector, uno::UNO_QUERY);
527 if (xConnector.is())
529 sal_Int32 nCount = aConnectorShapeProperties.size();
530 for (sal_Int32 j = 0; j < nCount; j++)
532 OUString aDestShapeId = aConnectorShapeProperties[j].maDestShapeId;
533 const auto pShape = maShapeMap.find(aDestShapeId);
534 if (pShape == maShapeMap.end())
535 continue;
536 uno::Reference<drawing::XShape> xShape(pShape->second->getXShape(), uno::UNO_QUERY);
537 if (xShape.is())
539 uno::Reference<drawing::XGluePointsSupplier> xSupplier(xShape, uno::UNO_QUERY);
540 css::uno::Reference<css::container::XIdentifierContainer> xGluePoints(
541 xSupplier->getGluePoints(), uno::UNO_QUERY);
543 sal_Int32 nCountGluePoints = xGluePoints->getIdentifiers().getLength();
544 sal_Int32 nGlueId = aConnectorShapeProperties[j].mnDestGlueId;
546 // The first 4 glue points belong to the bounding box.
547 if (nCountGluePoints > 4)
548 nGlueId += 4;
549 else
551 bool bFlipH = pShape->second->getFlipH();
552 bool bFlipV = pShape->second->getFlipV();
553 if ((!bFlipH && !bFlipV) || (bFlipH && bFlipV))
555 // change id of the left and right glue points of the bounding box (1 <-> 3)
556 if (nGlueId == 1)
557 nGlueId = 3; // Right
558 else if (nGlueId == 3)
559 nGlueId = 1; // Left
563 bool bStart = aConnectorShapeProperties[j].mbStartShape;
564 if (bStart)
566 xPropertySet->setPropertyValue(u"StartShape"_ustr, uno::Any(xShape));
567 xPropertySet->setPropertyValue(u"StartGluePointIndex"_ustr, uno::Any(nGlueId));
569 else
571 xPropertySet->setPropertyValue(u"EndShape"_ustr, uno::Any(xShape));
572 xPropertySet->setPropertyValue(u"EndGluePointIndex"_ustr, uno::Any(nGlueId));
576 uno::Reference<beans::XPropertySetInfo> xPropInfo = xPropertySet->getPropertySetInfo();
577 if (xPropInfo->hasPropertyByName(u"EdgeKind"_ustr))
579 ConnectorType aConnectorType;
580 xPropertySet->getPropertyValue(u"EdgeKind"_ustr) >>= aConnectorType;
581 if (aConnectorType == ConnectorType_STANDARD)
582 lcl_SetEdgeLineValue(xConnector, pConnector);
589 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */