LibreOffice Module oox (master) 1
modelobjecthelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
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>
31#include <utility>
32#include <osl/diagnose.h>
33
34namespace oox {
35
36using namespace ::com::sun::star;
37using namespace ::com::sun::star::drawing;
38using namespace ::com::sun::star::lang;
39using namespace ::com::sun::star::uno;
40
41ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, OUString aServiceName ) :
42 mxModelFactory( rxModelFactory ),
43 maServiceName(std::move( aServiceName )),
44 mnIndex( 0 )
45{
46 OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
47}
48
50{
51}
52
53bool ObjectContainer::hasObject( const OUString& rObjName ) const
54{
56 return mxContainer.is() && mxContainer->hasByName( rObjName );
57}
58
59Any ObjectContainer::getObject( const OUString& rObjName ) const
60{
61 if( hasObject( rObjName ) )
62 return mxContainer->getByName( rObjName );
63 return Any();
64}
65
66OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
67{
69 if( mxContainer.is() )
70 {
71 if( bInsertByUnusedName )
72 return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::number( ++mnIndex ), ' ', rObj );
73 if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
74 return rObjName;
75 }
76 return OUString();
77}
78
80{
81 if( !mxContainer.is() && mxModelFactory.is() ) try
82 {
83 mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
84 mxModelFactory.clear();
85 }
86 catch( Exception& )
87 {
88 }
89 OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
90}
91
92constexpr OUStringLiteral gaDashNameBase( u"msLineDash " );
93constexpr OUStringLiteral gaGradientNameBase( u"msFillGradient " );
94constexpr OUStringLiteral gaTransGradNameBase( u"msTransGradient " );
95constexpr OUStringLiteral gaBitmapUrlNameBase( u"msFillBitmap " );
96constexpr OUStringLiteral gaHatchNameBase( u"msFillHatch " );
97
98ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
99 maMarkerContainer( rxModelFactory, "com.sun.star.drawing.MarkerTable" ),
100 maDashContainer( rxModelFactory, "com.sun.star.drawing.DashTable" ),
101 maGradientContainer( rxModelFactory, "com.sun.star.drawing.GradientTable" ),
102 maTransGradContainer( rxModelFactory, "com.sun.star.drawing.TransparencyGradientTable" ),
103 maBitmapUrlContainer( rxModelFactory, "com.sun.star.drawing.BitmapTable" ),
104 maHatchContainer( rxModelFactory, "com.sun.star.drawing.HatchTable" )
105{
106}
107
108bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
109{
110 return maMarkerContainer.hasObject( rMarkerName );
111}
112
113bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
114{
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;
119}
120
121OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
122{
123 return maDashContainer.insertObject( gaDashNameBase, Any( rDash ), true );
124}
125
126OUString ModelObjectHelper::insertFillGradient( const awt::Gradient2& rGradient )
127{
128 return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true );
129}
130
131OUString ModelObjectHelper::insertFillGradient( const awt::Gradient& rGradient )
132{
133 return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true );
134}
135
136OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient2& rGradient )
137{
138 return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true );
139}
140
141OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient& rGradient )
142{
143 return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true );
144}
145
146OUString ModelObjectHelper::insertFillBitmapXGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic)
147{
148 uno::Reference<awt::XBitmap> xBitmap(rxGraphic, uno::UNO_QUERY);
149 if (xBitmap.is())
151 return OUString();
152}
153
154OUString ModelObjectHelper::insertFillHatch(const drawing::Hatch& rHatch)
155{
156 return maHatchContainer.insertObject( gaHatchNameBase, Any( rHatch ), true );
157}
158
159uno::Reference<awt::XBitmap> ModelObjectHelper::getFillBitmap(OUString const & rGraphicName)
160{
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;
166}
167
168} // namespace oox
169
170/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString maServiceName
static bool insertByName(const css::uno::Reference< css::container::XNameContainer > &rxNameContainer, const OUString &rName, const css::uno::Any &rObject)
Inserts an object into a name container.
static OUString insertByUnusedName(const css::uno::Reference< css::container::XNameContainer > &rxNameContainer, const OUString &rSuggestedName, sal_Unicode cSeparator, const css::uno::Any &rObject)
Inserts an object into a name container.
OUString insertFillHatch(const css::drawing::Hatch &rHatch)
OUString insertTransGrandient(const css::awt::Gradient2 &rGradient)
ModelObjectHelper(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxModelFactory)
css::uno::Reference< css::awt::XBitmap > getFillBitmap(OUString const &rGraphicName)
OUString insertLineDash(const css::drawing::LineDash &rDash)
Inserts a new named line dash, returns the line dash name, based on an internal constant name with a ...
OUString insertFillBitmapXGraphic(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic)
Inserts a new named fill graphic, returns the bitmap name, based on an internal constant name with a ...
bool hasLineMarker(const OUString &rMarkerName) const
Returns true, if the model contains a line marker with the passed name.
ObjectContainer maBitmapUrlContainer
Contains all named fill bitmap URLs.
ObjectContainer maHatchContainer
Contains all named fill hatches.
ObjectContainer maMarkerContainer
Contains all named line markers (line end polygons).
ObjectContainer maDashContainer
Contains all named line dashes.
bool insertLineMarker(const OUString &rMarkerName, const css::drawing::PolyPolygonBezierCoords &rMarker)
Inserts a new named line marker, overwrites an existing line marker with the same name.
ObjectContainer maTransGradContainer
Contains all named transparency Gradients.
OUString insertFillGradient(const css::awt::Gradient2 &rGradient)
Inserts a new named fill gradient, returns the gradient name, based on an internal constant name with...
ObjectContainer maGradientContainer
Contains all named fill gradients.
ObjectContainer(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxModelFactory, OUString aServiceName)
bool hasObject(const OUString &rObjName) const
Returns true, if the object with the passed name exists in the container.
OUString maServiceName
Service name to create the container.
OUString insertObject(const OUString &rObjName, const css::uno::Any &rObj, bool bInsertByUnusedName)
Inserts the passed object into the container, returns its final name.
css::uno::Reference< css::lang::XMultiServiceFactory > mxModelFactory
Factory to create the container.
css::uno::Reference< css::container::XNameContainer > mxContainer
Container for the objects.
sal_Int32 mnIndex
Index to create unique identifiers.
css::uno::Any getObject(const OUString &rObjName) const
@ Exception
constexpr OUStringLiteral gaBitmapUrlNameBase(u"msFillBitmap ")
Base name for all named fill bitmap URLs.
constexpr OUStringLiteral gaTransGradNameBase(u"msTransGradient ")
Base name for all named fill gradients.
constexpr OUStringLiteral gaHatchNameBase(u"msFillHatch ")
Base name for all named fill hatches.
constexpr OUStringLiteral gaGradientNameBase(u"msFillGradient ")
Base name for all named fill gradients.
constexpr OUStringLiteral gaDashNameBase(u"msLineDash ")
Base name for all named line dashes.
sal_Int32 mnIndex
Definition: vbacontrol.cxx:101