LibreOffice Module xmloff (master) 1
backhdl.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
20#include "backhdl.hxx"
21#include <xmloff/xmltoken.hxx>
22#include <xmloff/xmluconv.hxx>
23#include <xmloff/xmlement.hxx>
25#include <com/sun/star/uno/Any.hxx>
26#include <rtl/ustrbuf.hxx>
27#include <sal/log.hxx>
28
29using namespace ::com::sun::star;
30using namespace ::xmloff::token;
31
33{
34 { XML_LEFT, style::GraphicLocation_LEFT_MIDDLE },
35 { XML_RIGHT, style::GraphicLocation_RIGHT_MIDDLE },
36 { XML_TOKEN_INVALID, style::GraphicLocation(0) }
37};
38
40{
41 { XML_TOP, style::GraphicLocation_MIDDLE_TOP },
42 { XML_BOTTOM, style::GraphicLocation_MIDDLE_BOTTOM },
43 { XML_TOKEN_INVALID, style::GraphicLocation(0) }
44};
45
46
47
48
50{
51 // Nothing to do
52}
53
54bool XMLBackGraphicPositionPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
55{
56 bool bRet = true;
57 style::GraphicLocation ePos = style::GraphicLocation_NONE, eTmp;
58 style::GraphicLocation nTmpGraphicLocation;
59 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
60 std::u16string_view aToken;
61 bool bHori = false, bVert = false;
62
63 while( bRet && aTokenEnum.getNextToken( aToken ) )
64 {
65 if( bHori && bVert )
66 {
67 bRet = false;
68 }
69 else if( std::u16string_view::npos != aToken.find( '%' ) )
70 {
71 sal_Int32 nPrc = 50;
72 if (::sax::Converter::convertPercent( nPrc, aToken ))
73 {
74 if( !bHori )
75 {
76 ePos = nPrc < 25 ? style::GraphicLocation_LEFT_TOP :
77 (nPrc < 75 ? style::GraphicLocation_MIDDLE_MIDDLE :
78 style::GraphicLocation_RIGHT_BOTTOM);
79 bHori = true;
80 }
81 else
82 {
83 eTmp = nPrc < 25 ? style::GraphicLocation_LEFT_TOP:
84 (nPrc < 75 ? style::GraphicLocation_LEFT_MIDDLE :
85 style::GraphicLocation_LEFT_BOTTOM);
86 MergeXMLVertPos( ePos, eTmp );
87 bVert = true;
88 }
89 }
90 else
91 {
92 // wrong percentage
93 bRet = false;
94 }
95 }
96 else if( IsXMLToken( aToken, XML_CENTER ) )
97 {
98 if( bHori )
99 MergeXMLVertPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
100 else if ( bVert )
101 MergeXMLHoriPos( ePos, style::GraphicLocation_MIDDLE_MIDDLE );
102 else
103 ePos = style::GraphicLocation_MIDDLE_MIDDLE;
104 }
105 else if( SvXMLUnitConverter::convertEnum( nTmpGraphicLocation, aToken, pXML_BrushHorizontalPos ) )
106 {
107 if( bVert )
108 MergeXMLHoriPos( ePos, nTmpGraphicLocation );
109 else if( !bHori )
110 ePos = nTmpGraphicLocation;
111 else
112 bRet = false;
113
114 bHori = true;
115 }
116 else if( SvXMLUnitConverter::convertEnum( nTmpGraphicLocation, aToken, pXML_BrushVerticalPos ) )
117 {
118 if( bHori )
119 MergeXMLVertPos( ePos, nTmpGraphicLocation );
120 else if( !bVert )
121 ePos = nTmpGraphicLocation;
122 else
123 bRet = false;
124 bVert = true;
125 }
126 else
127 {
128 bRet = false;
129 }
130 }
131
132 bRet &= style::GraphicLocation_NONE != ePos;
133 if( bRet )
134 rValue <<= static_cast<style::GraphicLocation>(static_cast<sal_uInt16>(ePos));
135
136 return bRet;
137}
138
139bool XMLBackGraphicPositionPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
140{
141 bool bRet = true;
142 OUStringBuffer aOut;
143
144 style::GraphicLocation eLocation;
145 if( !( rValue >>= eLocation ) )
146 {
147 sal_Int32 nValue = 0;
148 if( rValue >>= nValue )
149 eLocation = static_cast<style::GraphicLocation>(nValue);
150 else
151 bRet = false;
152 }
153
154 if( bRet )
155 {
156 bRet = false;
157
158 switch( eLocation )
159 {
160 case style::GraphicLocation_LEFT_TOP:
161 case style::GraphicLocation_MIDDLE_TOP:
162 case style::GraphicLocation_RIGHT_TOP:
163 aOut.append( GetXMLToken(XML_TOP) );
164 bRet = true;
165 break;
166 case style::GraphicLocation_LEFT_MIDDLE:
167 case style::GraphicLocation_MIDDLE_MIDDLE:
168 case style::GraphicLocation_RIGHT_MIDDLE:
169 aOut.append( GetXMLToken(XML_CENTER) );
170 bRet = true;
171 break;
172 case style::GraphicLocation_LEFT_BOTTOM:
173 case style::GraphicLocation_MIDDLE_BOTTOM:
174 case style::GraphicLocation_RIGHT_BOTTOM:
175 aOut.append( GetXMLToken(XML_BOTTOM) );
176 bRet = true;
177 break;
178 default:
179 break;
180 }
181
182 if( bRet )
183 {
184 aOut.append( ' ' );
185
186 switch( eLocation )
187 {
188 case style::GraphicLocation_LEFT_TOP:
189 case style::GraphicLocation_LEFT_BOTTOM:
190 case style::GraphicLocation_LEFT_MIDDLE:
191 aOut.append( GetXMLToken(XML_LEFT) );
192 break;
193 case style::GraphicLocation_MIDDLE_TOP:
194 case style::GraphicLocation_MIDDLE_MIDDLE:
195 case style::GraphicLocation_MIDDLE_BOTTOM:
196 aOut.append( GetXMLToken(XML_CENTER) );
197 break;
198 case style::GraphicLocation_RIGHT_MIDDLE:
199 case style::GraphicLocation_RIGHT_TOP:
200 case style::GraphicLocation_RIGHT_BOTTOM:
201 aOut.append( GetXMLToken(XML_RIGHT) );
202 break;
203 default:
204 break;
205 }
206 }
207 }
208
209 rStrExpValue = aOut.makeStringAndClear();
210
211 return bRet;
212}
213
214void XMLBackGraphicPositionPropHdl::MergeXMLVertPos( style::GraphicLocation& ePos, style::GraphicLocation eVert )
215{
216 switch( ePos )
217 {
218 case style::GraphicLocation_LEFT_TOP:
219 case style::GraphicLocation_LEFT_MIDDLE:
220 case style::GraphicLocation_LEFT_BOTTOM:
221 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
222 style::GraphicLocation_LEFT_TOP :
223 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
224 style::GraphicLocation_LEFT_MIDDLE :
225 style::GraphicLocation_LEFT_BOTTOM);
226 break;
227
228 case style::GraphicLocation_MIDDLE_TOP:
229 case style::GraphicLocation_MIDDLE_MIDDLE:
230 case style::GraphicLocation_MIDDLE_BOTTOM:
231 ePos = eVert;
232 break;
233
234 case style::GraphicLocation_RIGHT_TOP:
235 case style::GraphicLocation_RIGHT_MIDDLE:
236 case style::GraphicLocation_RIGHT_BOTTOM:
237 ePos = style::GraphicLocation_MIDDLE_TOP==eVert ?
238 style::GraphicLocation_RIGHT_TOP :
239 (style::GraphicLocation_MIDDLE_MIDDLE==eVert ?
240 style::GraphicLocation_RIGHT_MIDDLE :
241 style::GraphicLocation_RIGHT_BOTTOM);
242 break;
243 default:
244 break;
245 }
246}
247
248void XMLBackGraphicPositionPropHdl::MergeXMLHoriPos( style::GraphicLocation& ePos, style::GraphicLocation eHori )
249{
250 SAL_WARN_IF( !(style::GraphicLocation_LEFT_MIDDLE==eHori || style::GraphicLocation_MIDDLE_MIDDLE==eHori || style::GraphicLocation_RIGHT_MIDDLE==eHori), "xmloff",
251 "lcl_frmitems_MergeXMLHoriPos: vertical pos must be middle" );
252
253 switch( ePos )
254 {
255 case style::GraphicLocation_LEFT_TOP:
256 case style::GraphicLocation_MIDDLE_TOP:
257 case style::GraphicLocation_RIGHT_TOP:
258 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
259 style::GraphicLocation_LEFT_TOP :
260 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
261 style::GraphicLocation_MIDDLE_TOP :
262 style::GraphicLocation_RIGHT_TOP);
263 break;
264
265 case style::GraphicLocation_LEFT_MIDDLE:
266 case style::GraphicLocation_MIDDLE_MIDDLE:
267 case style::GraphicLocation_RIGHT_MIDDLE:
268 ePos = eHori;
269 break;
270
271 case style::GraphicLocation_LEFT_BOTTOM:
272 case style::GraphicLocation_MIDDLE_BOTTOM:
273 case style::GraphicLocation_RIGHT_BOTTOM:
274 ePos = style::GraphicLocation_LEFT_MIDDLE==eHori ?
275 style::GraphicLocation_LEFT_BOTTOM :
276 (style::GraphicLocation_MIDDLE_MIDDLE==eHori ?
277 style::GraphicLocation_MIDDLE_BOTTOM :
278 style::GraphicLocation_RIGHT_BOTTOM);
279 break;
280 default:
281 break;
282 }
283}
284
285/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::chart::ChartAxisLabelPosition ePos
SvXMLEnumMapEntry< style::GraphicLocation > const pXML_BrushVerticalPos[]
Definition: backhdl.cxx:39
SvXMLEnumMapEntry< style::GraphicLocation > const pXML_BrushHorizontalPos[]
Definition: backhdl.cxx:32
bool getNextToken(std::u16string_view &rToken)
Definition: xmluconv.cxx:529
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:83
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:145
virtual bool exportXML(OUString &rStrExpValue, const css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class.
Definition: backhdl.cxx:139
static void MergeXMLHoriPos(css::style::GraphicLocation &ePos, css::style::GraphicLocation eHori)
Definition: backhdl.cxx:248
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class.
Definition: backhdl.cxx:54
virtual ~XMLBackGraphicPositionPropHdl() override
Definition: backhdl.cxx:49
static void MergeXMLVertPos(css::style::GraphicLocation &ePos, css::style::GraphicLocation eVert)
Definition: backhdl.cxx:214
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
sal_Int16 nValue
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3541