/src/libreoffice/oox/source/helper/modelobjecthelper.cxx
Line | Count | Source |
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 | | |
20 | | #include <oox/helper/modelobjecthelper.hxx> |
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> |
30 | | #include <oox/helper/containerhelper.hxx> |
31 | | #include <utility> |
32 | | #include <osl/diagnose.h> |
33 | | |
34 | | namespace oox { |
35 | | |
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; |
40 | | |
41 | | ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, OUString aServiceName ) : |
42 | 26.0k | mxModelFactory( rxModelFactory ), |
43 | 26.0k | maServiceName(std::move( aServiceName )), |
44 | 26.0k | mnIndex( 0 ) |
45 | 26.0k | { |
46 | 26.0k | OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" ); |
47 | 26.0k | } |
48 | | |
49 | | ObjectContainer::~ObjectContainer() |
50 | 26.0k | { |
51 | 26.0k | } |
52 | | |
53 | | bool ObjectContainer::hasObject( const OUString& rObjName ) const |
54 | 727 | { |
55 | 727 | createContainer(); |
56 | 727 | return mxContainer.is() && mxContainer->hasByName( rObjName ); |
57 | 727 | } |
58 | | |
59 | | Any ObjectContainer::getObject( const OUString& rObjName ) const |
60 | 0 | { |
61 | 0 | if( hasObject( rObjName ) ) |
62 | 0 | return mxContainer->getByName( rObjName ); |
63 | 0 | return Any(); |
64 | 0 | } |
65 | | |
66 | | OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName ) |
67 | 315 | { |
68 | 315 | createContainer(); |
69 | 315 | if( mxContainer.is() ) |
70 | 315 | { |
71 | 315 | if( bInsertByUnusedName ) |
72 | 84 | return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::number( ++mnIndex ), ' ', rObj ); |
73 | 231 | if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) ) |
74 | 231 | return rObjName; |
75 | 231 | } |
76 | 0 | return OUString(); |
77 | 315 | } |
78 | | |
79 | | void ObjectContainer::createContainer() const |
80 | 1.04k | { |
81 | 1.04k | if( !mxContainer.is() && mxModelFactory.is() ) try |
82 | 288 | { |
83 | 288 | mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW ); |
84 | 288 | mxModelFactory.clear(); |
85 | 288 | } |
86 | 288 | catch( Exception& ) |
87 | 288 | { |
88 | 0 | } |
89 | 1.04k | OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" ); |
90 | 1.04k | } |
91 | | |
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. |
97 | | |
98 | | ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) : |
99 | 4.33k | maMarkerContainer( rxModelFactory, u"com.sun.star.drawing.MarkerTable"_ustr ), |
100 | 4.33k | maDashContainer( rxModelFactory, u"com.sun.star.drawing.DashTable"_ustr ), |
101 | 4.33k | maGradientContainer( rxModelFactory, u"com.sun.star.drawing.GradientTable"_ustr ), |
102 | 4.33k | maTransGradContainer( rxModelFactory, u"com.sun.star.drawing.TransparencyGradientTable"_ustr ), |
103 | 4.33k | maBitmapUrlContainer( rxModelFactory, u"com.sun.star.drawing.BitmapTable"_ustr ), |
104 | 4.33k | maHatchContainer( rxModelFactory, u"com.sun.star.drawing.HatchTable"_ustr ) |
105 | 4.33k | { |
106 | 4.33k | } |
107 | | |
108 | | bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const |
109 | 727 | { |
110 | 727 | return maMarkerContainer.hasObject( rMarkerName ); |
111 | 727 | } |
112 | | |
113 | | bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker ) |
114 | 231 | { |
115 | 231 | OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" ); |
116 | 231 | if( rMarker.Coordinates.hasElements() ) |
117 | 231 | return !maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).isEmpty(); |
118 | 0 | return false; |
119 | 231 | } |
120 | | |
121 | | OUString ModelObjectHelper::insertLineDash( const LineDash& rDash ) |
122 | 0 | { |
123 | 0 | return maDashContainer.insertObject( gaDashNameBase, Any( rDash ), true ); |
124 | 0 | } |
125 | | |
126 | | OUString ModelObjectHelper::insertFillGradient( const awt::Gradient2& rGradient ) |
127 | 45 | { |
128 | 45 | return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true ); |
129 | 45 | } |
130 | | |
131 | | OUString ModelObjectHelper::insertFillGradient( const awt::Gradient& rGradient ) |
132 | 0 | { |
133 | 0 | return maGradientContainer.insertObject( gaGradientNameBase, Any( rGradient ), true ); |
134 | 0 | } |
135 | | |
136 | | OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient2& rGradient ) |
137 | 9 | { |
138 | 9 | return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true ); |
139 | 9 | } |
140 | | |
141 | | OUString ModelObjectHelper::insertTransGrandient( const awt::Gradient& rGradient ) |
142 | 0 | { |
143 | 0 | return maTransGradContainer.insertObject( gaTransGradNameBase, Any( rGradient ), true ); |
144 | 0 | } |
145 | | |
146 | | OUString ModelObjectHelper::insertFillBitmapXGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic) |
147 | 30 | { |
148 | 30 | uno::Reference<awt::XBitmap> xBitmap(rxGraphic, uno::UNO_QUERY); |
149 | 30 | if (xBitmap.is()) |
150 | 30 | return maBitmapUrlContainer.insertObject(gaBitmapUrlNameBase, Any(xBitmap), true); |
151 | 0 | return OUString(); |
152 | 30 | } |
153 | | |
154 | | OUString ModelObjectHelper::insertFillHatch(const drawing::Hatch& rHatch) |
155 | 0 | { |
156 | 0 | return maHatchContainer.insertObject( gaHatchNameBase, Any( rHatch ), true ); |
157 | 0 | } |
158 | | |
159 | | uno::Reference<awt::XBitmap> ModelObjectHelper::getFillBitmap(OUString const & rGraphicName) |
160 | 0 | { |
161 | 0 | uno::Reference<awt::XBitmap> xBitmap; |
162 | 0 | uno::Any aAny = maBitmapUrlContainer.getObject(rGraphicName); |
163 | 0 | if (aAny.has<uno::Reference<awt::XBitmap>>()) |
164 | 0 | xBitmap = aAny.get<uno::Reference<awt::XBitmap>>(); |
165 | 0 | return xBitmap; |
166 | 0 | } |
167 | | |
168 | | } // namespace oox |
169 | | |
170 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |