tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / xmloff / source / style / DashStyle.cxx
blob73aede0f557a7ca2de3324c23afd4f291702da04
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 <com/sun/star/drawing/DashStyle.hpp>
21 #include <com/sun/star/drawing/LineDash.hpp>
23 #include <sax/tools/converter.hxx>
25 #include <xmloff/DashStyle.hxx>
26 #include <xmloff/xmluconv.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmlexp.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmlement.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <sal/log.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::xmloff::token;
39 SvXMLEnumMapEntry<drawing::DashStyle> const pXML_DashStyle_Enum[] =
41 { XML_RECT, drawing::DashStyle_RECT },
42 { XML_ROUND, drawing::DashStyle_ROUND },
43 { XML_RECT, drawing::DashStyle_RECTRELATIVE },
44 { XML_ROUND, drawing::DashStyle_ROUNDRELATIVE },
45 { XML_TOKEN_INVALID, drawing::DashStyle(0) }
48 // Import
50 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
51 : m_rImport(rImp)
55 void XMLDashStyleImport::importXML(
56 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
57 uno::Any& rValue,
58 OUString& rStrName )
60 drawing::LineDash aLineDash;
61 aLineDash.Style = drawing::DashStyle_RECT;
62 aLineDash.Dots = 0;
63 aLineDash.DotLen = 0;
64 aLineDash.Dashes = 0;
65 aLineDash.DashLen = 0;
66 aLineDash.Distance = 20;
67 OUString aDisplayName;
69 bool bIsRel = false;
71 SvXMLUnitConverter& rUnitConverter = m_rImport.GetMM100UnitConverter();
73 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
75 switch( aIter.getToken() )
77 case XML_ELEMENT(DRAW, XML_NAME):
78 case XML_ELEMENT(DRAW_OOO, XML_NAME):
80 rStrName = aIter.toString();
82 break;
83 case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
84 case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
86 aDisplayName = aIter.toString();
88 break;
89 case XML_ELEMENT(DRAW, XML_STYLE):
90 case XML_ELEMENT(DRAW_OOO, XML_STYLE):
92 SvXMLUnitConverter::convertEnum( aLineDash.Style, aIter.toView(), pXML_DashStyle_Enum );
94 break;
95 case XML_ELEMENT(DRAW, XML_DOTS1):
96 case XML_ELEMENT(DRAW_OOO, XML_DOTS1):
97 aLineDash.Dots = static_cast<sal_Int16>(aIter.toInt32());
98 break;
100 case XML_ELEMENT(DRAW, XML_DOTS1_LENGTH):
101 case XML_ELEMENT(DRAW_OOO, XML_DOTS1_LENGTH):
103 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
105 bIsRel = true;
106 ::sax::Converter::convertPercent(aLineDash.DotLen, aIter.toView());
108 else
110 rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
111 aIter.toView() );
114 break;
116 case XML_ELEMENT(DRAW, XML_DOTS2):
117 case XML_ELEMENT(DRAW_OOO, XML_DOTS2):
118 aLineDash.Dashes = static_cast<sal_Int16>(aIter.toInt32());
119 break;
121 case XML_ELEMENT(DRAW, XML_DOTS2_LENGTH):
122 case XML_ELEMENT(DRAW_OOO, XML_DOTS2_LENGTH):
124 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
126 bIsRel = true;
127 ::sax::Converter::convertPercent(aLineDash.DashLen, aIter.toView());
129 else
131 rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
132 aIter.toView() );
135 break;
137 case XML_ELEMENT(DRAW, XML_DISTANCE):
138 case XML_ELEMENT(DRAW_OOO, XML_DISTANCE):
140 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
142 bIsRel = true;
143 ::sax::Converter::convertPercent(aLineDash.Distance, aIter.toView());
145 else
147 rUnitConverter.convertMeasureToCore( aLineDash.Distance,
148 aIter.toView() );
151 break;
152 default:
153 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
157 if( bIsRel )
158 aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
160 rValue <<= aLineDash;
162 if( !aDisplayName.isEmpty() )
164 m_rImport.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID,
165 rStrName, aDisplayName );
166 rStrName = aDisplayName;
170 // Export
172 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
173 : m_rExport(rExp)
177 void XMLDashStyleExport::exportXML(
178 const OUString& rStrName,
179 const uno::Any& rValue )
181 SvXMLUnitConverter & rUnitConverter = m_rExport.GetMM100UnitConverter();
183 drawing::LineDash aLineDash;
185 if( rStrName.isEmpty() )
186 return;
188 if( !(rValue >>= aLineDash) )
189 return;
191 bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
193 OUString aStrValue;
194 OUStringBuffer aOut;
196 // Name
197 bool bEncoded = false;
198 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
199 m_rExport.EncodeStyleName( rStrName,
200 &bEncoded ) );
201 if( bEncoded )
202 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
203 rStrName );
205 // Style
206 SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
207 aStrValue = aOut.makeStringAndClear();
208 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
210 // dots
211 if( aLineDash.Dots )
213 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
215 if( aLineDash.DotLen )
217 // dashes length
218 if( bIsRel )
220 ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
222 else
224 rUnitConverter.convertMeasureToXML( aOut,
225 aLineDash.DotLen );
227 aStrValue = aOut.makeStringAndClear();
228 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
232 // dashes
233 if( aLineDash.Dashes )
235 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
237 if( aLineDash.DashLen )
239 // dashes length
240 if( bIsRel )
242 ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
244 else
246 rUnitConverter.convertMeasureToXML( aOut,
247 aLineDash.DashLen );
249 aStrValue = aOut.makeStringAndClear();
250 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
254 // distance
255 if( bIsRel )
257 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
259 else
261 rUnitConverter.convertMeasureToXML( aOut,
262 aLineDash.Distance );
264 aStrValue = aOut.makeStringAndClear();
265 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
267 // do Write
268 SvXMLElementExport rElem( m_rExport,
269 XML_NAMESPACE_DRAW, XML_STROKE_DASH,
270 true, false );
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */