sd: keep a non-owning pointer to the OverridingShell
[LibreOffice.git] / oox / source / helper / modelobjecthelper.cxx
blob5ea9fa94ef832d3ed62a9310d558c65767231eb2
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 <oox/helper/modelobjecthelper.hxx>
22 #include <com/sun/star/awt/Gradient2.hpp>
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/drawing/LineDash.hpp>
25 #include <com/sun/star/drawing/Hatch.hpp>
26 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/graphic/XGraphic.hpp>
29 #include <com/sun/star/awt/XBitmap.hpp>
30 #include <oox/helper/containerhelper.hxx>
31 #include <utility>
32 #include <osl/diagnose.h>
34 namespace oox {
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::drawing;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::uno;
41 ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, OUString aServiceName ) :
42 mxModelFactory( rxModelFactory ),
43 maServiceName(std::move( aServiceName )),
44 mnIndex( 0 )
46 OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
49 ObjectContainer::~ObjectContainer()
53 bool ObjectContainer::hasObject( const OUString& rObjName ) const
55 createContainer();
56 return mxContainer.is() && mxContainer->hasByName( rObjName );
59 Any ObjectContainer::getObject( const OUString& rObjName ) const
61 if( hasObject( rObjName ) )
62 return mxContainer->getByName( rObjName );
63 return Any();
66 OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
68 createContainer();
69 if( mxContainer.is() )
71 if( bInsertByUnusedName )
72 return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::number( ++mnIndex ), ' ', rObj );
73 if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
74 return rObjName;
76 return OUString();
79 void ObjectContainer::createContainer() const
81 if( !mxContainer.is() && mxModelFactory.is() ) try
83 mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
84 mxModelFactory.clear();
86 catch( Exception& )
89 OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
92 constexpr OUStringLiteral gaDashNameBase( u"msLineDash " ); ///< Base name for all named line dashes.
93 constexpr OUString gaGradientNameBase( u"msFillGradient "_ustr ); ///< Base name for all named fill gradients.
94 constexpr OUString gaTransGradNameBase( u"msTransGradient "_ustr ); ///< Base name for all named fill gradients.
95 constexpr OUStringLiteral gaBitmapUrlNameBase( u"msFillBitmap " ); ///< Base name for all named fill bitmap URLs.
96 constexpr OUStringLiteral gaHatchNameBase( u"msFillHatch " ); ///< Base name for all named fill hatches.
98 ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
99 maMarkerContainer( rxModelFactory, u"com.sun.star.drawing.MarkerTable"_ustr ),
100 maDashContainer( rxModelFactory, u"com.sun.star.drawing.DashTable"_ustr ),
101 maGradientContainer( rxModelFactory, u"com.sun.star.drawing.GradientTable"_ustr ),
102 maTransGradContainer( rxModelFactory, u"com.sun.star.drawing.TransparencyGradientTable"_ustr ),
103 maBitmapUrlContainer( rxModelFactory, u"com.sun.star.drawing.BitmapTable"_ustr ),
104 maHatchContainer( rxModelFactory, u"com.sun.star.drawing.HatchTable"_ustr )
108 bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
110 return maMarkerContainer.hasObject( rMarkerName );
113 bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
115 OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" );
116 if( rMarker.Coordinates.hasElements() )
117 return !maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).isEmpty();
118 return false;
121 OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
123 return maDashContainer.insertObject( gaDashNameBase, Any( rDash ), true );
126 OUString ModelObjectHelper::insertFillGradient( const awt::Gradient2& rGradient )
128 return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true );
131 OUString ModelObjectHelper::insertFillGradient( const awt::Gradient& rGradient )
133 return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true );
136 OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient2& rGradient )
138 return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true );
141 OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient& rGradient )
143 return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true );
146 OUString ModelObjectHelper::insertFillBitmapXGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic)
148 uno::Reference<awt::XBitmap> xBitmap(rxGraphic, uno::UNO_QUERY);
149 if (xBitmap.is())
150 return maBitmapUrlContainer.insertObject(gaBitmapUrlNameBase, Any(xBitmap), true);
151 return OUString();
154 OUString ModelObjectHelper::insertFillHatch(const drawing::Hatch& rHatch)
156 return maHatchContainer.insertObject( gaHatchNameBase, Any( rHatch ), true );
159 uno::Reference<awt::XBitmap> ModelObjectHelper::getFillBitmap(OUString const & rGraphicName)
161 uno::Reference<awt::XBitmap> xBitmap;
162 uno::Any aAny = maBitmapUrlContainer.getObject(rGraphicName);
163 if (aAny.has<uno::Reference<awt::XBitmap>>())
164 xBitmap = aAny.get<uno::Reference<awt::XBitmap>>();
165 return xBitmap;
168 } // namespace oox
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */