tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / xmloff / source / style / styleexp.cxx
blob5f129061318da0aa4c9887e6bdf4da3a728dc21b
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 <sal/config.h>
22 #include <o3tl/any.hxx>
23 #include <xmloff/xmlnamespace.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlexppr.hxx>
26 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/style/XStyle.hpp>
30 #include <com/sun/star/beans/NamedValue.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/beans/XPropertyState.hpp>
33 #include <com/sun/star/document/XEventsSupplier.hpp>
34 #include <com/sun/star/text/XChapterNumberingSupplier.hpp>
35 #include <xmloff/xmlaustp.hxx>
36 #include <xmloff/styleexp.hxx>
37 #include <xmloff/xmlexp.hxx>
38 #include <xmloff/XMLEventExport.hxx>
39 #include <xmloff/maptype.hxx>
40 #include <set>
41 #include <prstylecond.hxx>
42 #include <sal/log.hxx>
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::style;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::text;
50 using namespace ::xmloff::token;
52 using ::com::sun::star::document::XEventsSupplier;
54 constexpr OUString gsIsPhysical( u"IsPhysical"_ustr );
55 constexpr OUString gsIsAutoUpdate( u"IsAutoUpdate"_ustr );
56 constexpr OUString gsFollowStyle( u"FollowStyle"_ustr );
57 constexpr OUString gsNumberingStyleName( u"NumberingStyleName"_ustr );
58 constexpr OUString gsOutlineLevel( u"OutlineLevel"_ustr );
60 XMLStyleExport::XMLStyleExport(
61 SvXMLExport& rExp,
62 SvXMLAutoStylePoolP *pAutoStyleP ) :
63 m_rExport( rExp ),
64 m_pAutoStylePool( pAutoStyleP )
68 XMLStyleExport::~XMLStyleExport()
72 void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
76 void XMLStyleExport::exportStyleContent( const Reference< XStyle >& rStyle )
78 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
79 assert(xPropSet.is());
81 try
83 uno::Any aProperty = xPropSet->getPropertyValue( u"ParaStyleConditions"_ustr );
84 uno::Sequence< beans::NamedValue > aSeq;
86 aProperty >>= aSeq;
88 for (beans::NamedValue const& rNamedCond : aSeq)
90 OUString aStyleName;
92 if (rNamedCond.Value >>= aStyleName)
94 if (!aStyleName.isEmpty())
96 OUString aExternal = GetParaStyleCondExternal(rNamedCond.Name);
98 if (!aExternal.isEmpty())
100 bool bEncoded;
102 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
103 XML_CONDITION,
104 aExternal);
105 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
106 XML_APPLY_STYLE_NAME,
107 GetExport().EncodeStyleName( aStyleName,
108 &bEncoded ) );
109 SvXMLElementExport aElem( GetExport(),
110 XML_NAMESPACE_STYLE,
111 XML_MAP,
112 true,
113 true );
119 catch( const beans::UnknownPropertyException& )
124 namespace
126 /// Writes <style:style style:list-level="..."> for Writer paragraph styles.
127 void ExportStyleListlevel(const uno::Reference<beans::XPropertySetInfo>& xPropSetInfo,
128 const uno::Reference<beans::XPropertyState>& xPropState,
129 const uno::Reference<beans::XPropertySet>& xPropSet, SvXMLExport& rExport)
131 if (!xPropSetInfo->hasPropertyByName(u"NumberingLevel"_ustr))
133 SAL_WARN("xmloff", "ExportStyleListlevel: no NumberingLevel for a Writer paragraph style");
134 return;
137 if (xPropState->getPropertyState(u"NumberingLevel"_ustr) != beans::PropertyState_DIRECT_VALUE)
139 return;
142 sal_Int16 nNumberingLevel{};
143 if (!(xPropSet->getPropertyValue(u"NumberingLevel"_ustr) >>= nNumberingLevel))
145 return;
148 // The spec is positiveInteger (1-based), but the implementation is 0-based.
149 rExport.AddAttribute(XML_NAMESPACE_STYLE, XML_LIST_LEVEL, OUString::number(++nNumberingLevel));
153 bool XMLStyleExport::exportStyle(
154 const Reference< XStyle >& rStyle,
155 const OUString& rXMLFamily,
156 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
157 const Reference< XNameAccess >& xStyles,
158 const OUString* pPrefix )
160 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
161 if (!xPropSet)
162 return false;
164 Reference< XPropertySetInfo > xPropSetInfo =
165 xPropSet->getPropertySetInfo();
166 Any aAny;
168 // Don't export styles that aren't existing really. This may be the
169 // case for StarOffice Writer's pool styles.
170 if( xPropSetInfo->hasPropertyByName( gsIsPhysical ) )
172 aAny = xPropSet->getPropertyValue( gsIsPhysical );
173 if( !*o3tl::doAccess<bool>(aAny) )
174 return false;
177 // <style:style ...>
178 GetExport().CheckAttrList();
180 // style:name="..."
181 OUString sName;
183 if(pPrefix)
184 sName = *pPrefix;
185 sName += rStyle->getName();
187 bool bEncoded = false;
188 const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
189 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
191 if( bEncoded )
192 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
193 sName);
195 // style:family="..."
196 if( !rXMLFamily.isEmpty() )
197 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, rXMLFamily);
199 if ( xPropSetInfo->hasPropertyByName( u"Hidden"_ustr ) )
201 aAny = xPropSet->getPropertyValue( u"Hidden"_ustr );
202 bool bHidden = false;
203 if ((aAny >>= bHidden) && bHidden
204 && GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED)
206 GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_HIDDEN, u"true"_ustr);
207 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_HIDDEN, u"true"_ustr); // FIXME for compatibility
211 // style:parent-style-name="..."
212 OUString sParentString(rStyle->getParentStyle());
213 OUString sParent;
215 if(!sParentString.isEmpty())
217 if(pPrefix)
218 sParent = *pPrefix;
219 sParent += sParentString;
222 if( !sParent.isEmpty() )
223 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PARENT_STYLE_NAME,
224 GetExport().EncodeStyleName( sParent ) );
226 // style:next-style-name="..." (paragraph styles only)
227 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
229 aAny = xPropSet->getPropertyValue( gsFollowStyle );
230 OUString sNextName;
231 aAny >>= sNextName;
232 if( sName != sNextName )
234 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
235 GetExport().EncodeStyleName( sNextName ) );
239 // style:linked-style-name="..." (SW paragraph and character styles only)
240 if (xPropSetInfo->hasPropertyByName(u"LinkStyle"_ustr))
242 aAny = xPropSet->getPropertyValue(u"LinkStyle"_ustr);
243 OUString sLinkName;
244 aAny >>= sLinkName;
245 if (!sLinkName.isEmpty()
246 && (GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED))
248 GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_LINKED_STYLE_NAME,
249 GetExport().EncodeStyleName(sLinkName));
253 // style:auto-update="..." (SW only)
254 if( xPropSetInfo->hasPropertyByName( gsIsAutoUpdate ) )
256 aAny = xPropSet->getPropertyValue( gsIsAutoUpdate );
257 if( *o3tl::doAccess<bool>(aAny) )
258 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_AUTO_UPDATE,
259 XML_TRUE );
262 // style:default-outline-level"..."
263 sal_Int32 nOutlineLevel = 0;
264 if( xPropSetInfo->hasPropertyByName( gsOutlineLevel ) )
266 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
267 if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( gsOutlineLevel ) )
269 aAny = xPropSet->getPropertyValue( gsOutlineLevel );
270 aAny >>= nOutlineLevel;
271 if( nOutlineLevel > 0 )
273 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
274 XML_DEFAULT_OUTLINE_LEVEL,
275 OUString::number(nOutlineLevel) );
277 else
279 /* Empty value for style:default-outline-level does exist
280 since ODF 1.2. Thus, suppress its export for former versions. (#i104889#)
282 if ( ( GetExport().getExportFlags() & SvXMLExportFlags::OASIS ) &&
283 GetExport().getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012)
285 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
286 XML_DEFAULT_OUTLINE_LEVEL,
287 u""_ustr);
293 // style:list-style-name="..." (SW paragraph styles only)
294 if( xPropSetInfo->hasPropertyByName( gsNumberingStyleName ) )
296 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
297 if( PropertyState_DIRECT_VALUE ==
298 xPropState->getPropertyState( gsNumberingStyleName ) )
300 aAny = xPropSet->getPropertyValue( gsNumberingStyleName );
301 if( aAny.hasValue() )
303 OUString sListName;
304 aAny >>= sListName;
306 /* A direct set empty list style has to be written. Otherwise,
307 this information is lost and causes an error, if the parent
308 style has a list style set. (#i69523#)
310 if ( sListName.isEmpty() )
312 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
313 XML_LIST_STYLE_NAME,
314 sListName /* empty string */);
316 else
318 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
319 bool bSuppressListStyle( false );
321 if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
323 Reference< XChapterNumberingSupplier > xCNSupplier
324 (GetExport().GetModel(), UNO_QUERY);
326 if (xCNSupplier.is())
328 Reference< XIndexReplace > xNumRule
329 ( xCNSupplier->getChapterNumberingRules() );
330 assert(xNumRule.is());
332 Reference< XPropertySet > xNumRulePropSet
333 (xNumRule, UNO_QUERY);
334 OUString sOutlineName;
335 xNumRulePropSet->getPropertyValue(u"Name"_ustr)
336 >>= sOutlineName;
337 bSuppressListStyle = sListName == sOutlineName;
342 if ( !sListName.isEmpty() && !bSuppressListStyle )
344 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
345 XML_LIST_STYLE_NAME,
346 GetExport().EncodeStyleName( sListName ) );
348 ExportStyleListlevel(xPropSetInfo, xPropState, xPropSet, GetExport());
353 else if( nOutlineLevel > 0 )
356 bool bNoInheritedListStyle( true );
358 Reference<XStyle> xStyle( xPropState, UNO_QUERY );
359 while ( xStyle.is() )
361 OUString aParentStyle( xStyle->getParentStyle() );
362 if ( aParentStyle.isEmpty() || !xStyles->hasByName( aParentStyle ) )
364 break;
366 else
368 xPropState.set( xStyles->getByName( aParentStyle ), UNO_QUERY );
369 if ( !xPropState.is() )
371 break;
373 if ( xPropState->getPropertyState( gsNumberingStyleName ) == PropertyState_DIRECT_VALUE )
375 bNoInheritedListStyle = false;
376 break;
378 else
380 xStyle.set( xPropState, UNO_QUERY );
384 if ( bNoInheritedListStyle )
385 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
386 XML_LIST_STYLE_NAME,
387 u""_ustr);
391 // style:pool-id="..." is not required any longer since we use
392 // english style names only
393 exportStyleAttributes( rStyle );
395 // TODO: style:help-file-name="..." and style:help-id="..." can neither
396 // be modified by UI nor by API and that for, have not to be exported
397 // currently.
400 // <style:style>
401 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_STYLE,
402 true, true );
404 rPropMapper->SetStyleName( sName );
406 // <style:properties>
407 ::std::vector< XMLPropertyState > aPropStates =
408 rPropMapper->Filter(GetExport(), xPropSet, true);
409 bool const bUseExtensionNamespaceForGraphicProperties(
410 rXMLFamily != "drawing-page" &&
411 rXMLFamily != "graphic" &&
412 rXMLFamily != "presentation" &&
413 rXMLFamily != "chart");
414 rPropMapper->exportXML( GetExport(), aPropStates,
415 SvXmlExportFlags::IGN_WS,
416 bUseExtensionNamespaceForGraphicProperties );
418 rPropMapper->SetStyleName( OUString() );
420 exportStyleContent( rStyle );
422 // <script:events>, if they are supported by this style
423 Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
424 GetExport().GetEventExport().Export(xEventsSupp);
426 return true;
429 void XMLStyleExport::exportDefaultStyle(
430 const Reference< XPropertySet >& xPropSet,
431 const OUString& rXMLFamily,
432 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper )
434 // <style:default-style ...>
435 GetExport().CheckAttrList();
438 // style:family="..."
439 if( !rXMLFamily.isEmpty() )
440 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY,
441 rXMLFamily );
442 // <style:style>
443 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
444 XML_DEFAULT_STYLE,
445 true, true );
446 // <style:properties>
447 ::std::vector< XMLPropertyState > aPropStates =
448 rPropMapper->FilterDefaults(GetExport(), xPropSet);
449 rPropMapper->exportXML( GetExport(), aPropStates,
450 SvXmlExportFlags::IGN_WS );
454 void XMLStyleExport::exportStyleFamily(
455 const OUString& rFamily, const OUString& rXMLFamily,
456 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
457 bool bUsed, XmlStyleFamily nFamily, const OUString* pPrefix)
459 assert(GetExport().GetModel().is());
460 Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(), UNO_QUERY );
461 if( !xFamiliesSupp.is() )
462 return; // family not available in current model
464 Reference< XNameAccess > xStyleCont;
466 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
467 if( xFamilies->hasByName( rFamily ) )
468 xFamilies->getByName( rFamily ) >>= xStyleCont;
470 if( !xStyleCont.is() )
471 return;
473 // If next styles are supported and used styles should be exported only,
474 // the next style may be unused but has to be exported, too. In this case
475 // the names of all exported styles are remembered.
476 std::optional<std::set<OUString> > xExportedStyles;
477 bool bFirstStyle = true;
479 const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
480 for(const auto& rName : aSeq)
482 Reference< XStyle > xStyle;
485 xStyleCont->getByName( rName ) >>= xStyle;
487 catch(const lang::IndexOutOfBoundsException&)
489 // due to bugs in prior versions it is possible that
490 // a binary file is missing some critical styles.
491 // The only possible way to deal with this is to
492 // not export them here and remain silent.
493 continue;
495 catch(css::container::NoSuchElementException&)
497 continue;
500 assert(xStyle.is());
501 if (!bUsed || xStyle->isInUse())
503 bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
504 xStyleCont,pPrefix );
505 if (bUsed && bFirstStyle && bExported)
507 // If this is the first style, find out whether next styles
508 // are supported.
509 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
510 Reference< XPropertySetInfo > xPropSetInfo =
511 xPropSet->getPropertySetInfo();
513 if (xPropSetInfo->hasPropertyByName( gsFollowStyle ))
514 xExportedStyles.emplace();
515 bFirstStyle = false;
518 if (xExportedStyles && bExported)
520 // If next styles are supported, remember this style's name.
521 xExportedStyles->insert( xStyle->getName() );
525 // if an auto style pool is given, remember this style's name as a
526 // style name that must not be used by automatic styles.
527 if (m_pAutoStylePool)
528 m_pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
531 if( !xExportedStyles )
532 return;
534 // if next styles are supported, export all next styles that are
535 // unused and that for, haven't been exported in the first loop.
536 for(const auto& rName : aSeq)
538 Reference< XStyle > xStyle;
539 xStyleCont->getByName( rName ) >>= xStyle;
541 assert(xStyle.is());
543 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
544 Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
546 // styles that aren't existing really are ignored.
547 if (xPropSetInfo->hasPropertyByName( gsIsPhysical ))
549 Any aAny( xPropSet->getPropertyValue( gsIsPhysical ) );
550 if (!*o3tl::doAccess<bool>(aAny))
551 continue;
554 if (!xStyle->isInUse())
555 continue;
557 if (!xPropSetInfo->hasPropertyByName( gsFollowStyle ))
559 continue;
562 OUString sNextName;
563 xPropSet->getPropertyValue( gsFollowStyle ) >>= sNextName;
564 OUString sTmp( sNextName );
565 // if the next style hasn't been exported by now, export it now
566 // and remember its name.
567 if (xStyle->getName() != sNextName &&
568 0 == xExportedStyles->count( sTmp ))
570 xStyleCont->getByName( sNextName ) >>= xStyle;
571 assert(xStyle.is());
573 if (exportStyle(xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix))
574 xExportedStyles->insert( sTmp );
579 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */