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/Gradient.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 <osl/diagnose.h>
32 
33 namespace oox {
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::drawing;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
39 
40 ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, const OUString& rServiceName ) :
41  mxModelFactory( rxModelFactory ),
42  maServiceName( rServiceName ),
43  mnIndex( 0 )
44 {
45  OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
46 }
47 
49 {
50 }
51 
52 bool ObjectContainer::hasObject( const OUString& rObjName ) const
53 {
55  return mxContainer.is() && mxContainer->hasByName( rObjName );
56 }
57 
58 Any ObjectContainer::getObject( const OUString& rObjName ) const
59 {
60  if( hasObject( rObjName ) )
61  return mxContainer->getByName( rObjName );
62  return Any();
63 }
64 
65 OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
66 {
68  if( mxContainer.is() )
69  {
70  if( bInsertByUnusedName )
71  return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::number( ++mnIndex ), ' ', rObj );
72  if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
73  return rObjName;
74  }
75  return OUString();
76 }
77 
79 {
80  if( !mxContainer.is() && mxModelFactory.is() ) try
81  {
82  mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
83  mxModelFactory.clear();
84  }
85  catch( Exception& )
86  {
87  }
88  OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
89 }
90 
91 constexpr OUStringLiteral gaDashNameBase( u"msLineDash " );
92 constexpr OUStringLiteral gaGradientNameBase( u"msFillGradient " );
93 constexpr OUStringLiteral gaTransGradNameBase( u"msTransGradient " );
94 constexpr OUStringLiteral gaBitmapUrlNameBase( u"msFillBitmap " );
95 constexpr OUStringLiteral gaHatchNameBase( u"msFillHatch " );
96 
97 ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
98  maMarkerContainer( rxModelFactory, "com.sun.star.drawing.MarkerTable" ),
99  maDashContainer( rxModelFactory, "com.sun.star.drawing.DashTable" ),
100  maGradientContainer( rxModelFactory, "com.sun.star.drawing.GradientTable" ),
101  maTransGradContainer( rxModelFactory, "com.sun.star.drawing.TransparencyGradientTable" ),
102  maBitmapUrlContainer( rxModelFactory, "com.sun.star.drawing.BitmapTable" ),
103  maHatchContainer( rxModelFactory, "com.sun.star.drawing.HatchTable" )
104 {
105 }
106 
107 bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
108 {
109  return maMarkerContainer.hasObject( rMarkerName );
110 }
111 
112 bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
113 {
114  OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" );
115  if( rMarker.Coordinates.hasElements() )
116  return !maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).isEmpty();
117  return false;
118 }
119 
120 OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
121 {
122  return maDashContainer.insertObject( gaDashNameBase, Any( rDash ), true );
123 }
124 
125 OUString ModelObjectHelper::insertFillGradient( const awt::Gradient& rGradient )
126 {
127  return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true );
128 }
129 
130 OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient& rGradient )
131 {
132  return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true );
133 }
134 
135 OUString ModelObjectHelper::insertFillBitmapXGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic)
136 {
137  uno::Reference<awt::XBitmap> xBitmap(rxGraphic, uno::UNO_QUERY);
138  if (xBitmap.is())
140  return OUString();
141 }
142 
143 OUString ModelObjectHelper::insertFillHatch(const drawing::Hatch& rHatch)
144 {
145  return maHatchContainer.insertObject( gaHatchNameBase, Any( rHatch ), true );
146 }
147 
148 uno::Reference<awt::XBitmap> ModelObjectHelper::getFillBitmap(OUString const & rGraphicName)
149 {
150  uno::Reference<awt::XBitmap> xBitmap;
151  uno::Any aAny = maBitmapUrlContainer.getObject(rGraphicName);
152  if (aAny.has<uno::Reference<awt::XBitmap>>())
153  xBitmap = aAny.get<uno::Reference<awt::XBitmap>>();
154  return xBitmap;
155 }
156 
157 } // namespace oox
158 
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maServiceName
Service name to create the container.
constexpr OUStringLiteral gaHatchNameBase(u"msFillHatch ")
Base name for all named fill hatches.
sal_Int32 mnIndex
Definition: vbacontrol.cxx:101
constexpr OUStringLiteral gaBitmapUrlNameBase(u"msFillBitmap ")
Base name for all named fill bitmap URLs.
ObjectContainer(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxModelFactory, const OUString &rServiceName)
ModelObjectHelper(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxModelFactory)
constexpr OUStringLiteral gaGradientNameBase(u"msFillGradient ")
Base name for all named fill gradients.
bool hasLineMarker(const OUString &rMarkerName) const
Returns true, if the model contains a line marker with the passed name.
void createContainer() const
ObjectContainer maDashContainer
Contains all named line dashes.
css::uno::Reference< css::awt::XBitmap > getFillBitmap(OUString const &rGraphicName)
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 ...
ObjectContainer maMarkerContainer
Contains all named line markers (line end polygons).
css::uno::Any getObject(const OUString &rObjName) const
bool hasObject(const OUString &rObjName) const
Returns true, if the object with the passed name exists in the container.
sal_Int32 mnIndex
Index to create unique identifiers.
ObjectContainer maGradientContainer
Contains all named fill gradients.
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.
css::uno::Reference< css::container::XNameContainer > mxContainer
Container for the objects.
OUString insertObject(const OUString &rObjName, const css::uno::Any &rObj, bool bInsertByUnusedName)
Inserts the passed object into the container, returns its final name.
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.
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 ...
ObjectContainer maHatchContainer
Contains all named fill hatches.
constexpr OUStringLiteral gaTransGradNameBase(u"msTransGradient ")
Base name for all named fill gradients.
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...
OUString insertFillHatch(const css::drawing::Hatch &rHatch)
const OUString maServiceName
OUString insertFillGradient(const css::awt::Gradient &rGradient)
Inserts a new named fill gradient, returns the gradient name, based on an internal constant name with...
ObjectContainer maTransGradContainer
Contains all named transparency Gradients.
constexpr OUStringLiteral gaDashNameBase(u"msLineDash ")
Base name for all named line dashes.
css::uno::Reference< css::lang::XMultiServiceFactory > mxModelFactory
Factory to create the container.
OUString insertTransGrandient(const css::awt::Gradient &rGradient)
ObjectContainer maBitmapUrlContainer
Contains all named fill bitmap URLs.