LibreOffice Module sd (master) 1
sdtransform.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 <svl/style.hxx>
21#include <svl/itemset.hxx>
22#include <svl/whiter.hxx>
23
24#include <svx/svdoutl.hxx>
25#include <editeng/xmlcnitm.hxx>
26#include <svx/svdotext.hxx>
27#include <svx/svdogrp.hxx>
28#include <editeng/eeitem.hxx>
29#include <editeng/lrspitem.hxx>
30#include <editeng/outlobj.hxx>
31
32#include <drawdoc.hxx>
33#include "sdtransform.hxx"
34
35using namespace ::com::sun::star::style;
36
37namespace {
38
39class SdTransformOOo2xDocument
40{
41public:
42 explicit SdTransformOOo2xDocument( SdDrawDocument& rDocument );
43
44 void transform();
45
46 void transformMasterPages();
47 void transformDrawPages();
48
49 void transformStyles();
50 void transformStyles( SfxStyleFamily eFam );
51 void transformStyle( SfxStyleSheetBase& rSheet );
52
53 void transformShapes( SdrObjList const & rShapes );
54 void transformShape( SdrObject& rObj );
55
56 void transformTextShape( SdrTextObj& rTextShape );
57
58 bool getBulletState( const SfxItemSet& rSet, SfxStyleSheetBase* pSheet, bool& rState );
59 static bool getBulletState( const SfxItemSet& rSet, sal_uInt16 nWhich, bool& rState );
60
61 static bool transformItemSet( SfxItemSet& rSet, bool bNumbering );
62
63 static bool removeAlienAttributes( SfxItemSet& rSet );
64 static bool removeAlienAttributes( SfxItemSet& rSet, sal_uInt16 nWhich );
65
66 SdDrawDocument& mrDocument;
67 SdrOutliner& mrOutliner;
68};
69
70}
71
76{
77 if( pDocument )
78 {
79 SdTransformOOo2xDocument aTransformer( *pDocument );
80 aTransformer.transform();
81 }
82}
83
84constexpr OUStringLiteral gsEnableNumbering( u"enable-numbering" );
85constexpr OUStringLiteral gsTextNamespace( u"urn:oasis:names:tc:opendocument:xmlns:text:1.0" );
86constexpr OUStringLiteral gsTrue( u"true" );
87
88SdTransformOOo2xDocument::SdTransformOOo2xDocument( SdDrawDocument& rDocument )
89: mrDocument( rDocument )
90, mrOutliner( rDocument.GetDrawOutliner() )
91{
92}
93
94void SdTransformOOo2xDocument::transform()
95{
96 transformMasterPages();
97 transformDrawPages();
98 transformStyles();
99}
100
101void SdTransformOOo2xDocument::transformMasterPages()
102{
103 sal_uInt16 nMasterPageCount = mrDocument.GetMasterPageCount();
104 for( sal_uInt16 nMasterPage = 0; nMasterPage < nMasterPageCount; nMasterPage++ )
105 {
106 SdrObjList* pPage = mrDocument.GetMasterPage( nMasterPage );
107 if( pPage )
108 transformShapes( *pPage );
109 }
110}
111
112void SdTransformOOo2xDocument::transformDrawPages()
113{
114 sal_uInt16 nPageCount = mrDocument.GetPageCount();
115 for( sal_uInt16 nPage = 0; nPage < nPageCount; nPage++ )
116 {
117 SdrObjList* pPage = mrDocument.GetPage( nPage );
118 if( pPage )
119 transformShapes( *pPage );
120 }
121}
122
123void SdTransformOOo2xDocument::transformStyles()
124{
125 transformStyles( SfxStyleFamily::Para );
126 transformStyles( SfxStyleFamily::Page );
127}
128
129void SdTransformOOo2xDocument::transformStyles( SfxStyleFamily eFam )
130{
131
132 rtl::Reference< SfxStyleSheetBasePool > xStyleSheetPool( mrDocument.GetStyleSheetPool() );
133
134 SfxStyleSheetIterator aIter( xStyleSheetPool.get(), eFam );
135
136 SfxStyleSheetBase* pSheet = aIter.First();
137 while( pSheet )
138 {
139 transformStyle( *pSheet );
140 pSheet = aIter.Next();
141 }
142}
143
144void SdTransformOOo2xDocument::transformStyle( SfxStyleSheetBase& rSheet )
145{
146 SfxItemSet& rSet = rSheet.GetItemSet();
147
148 bool bState = false;
149 getBulletState( rSheet.GetItemSet(), rSheet.GetPool()->Find( rSheet.GetParent(), rSheet.GetFamily() ), bState );
150
151 transformItemSet( rSet, bState );
152 removeAlienAttributes( rSet );
153}
154
155void SdTransformOOo2xDocument::transformShapes( SdrObjList const & rShapes )
156{
157 const size_t nShapeCount = rShapes.GetObjCount();
158 for( size_t nShape = 0; nShape < nShapeCount; ++nShape )
159 {
160 SdrObject* pObj = rShapes.GetObj( nShape );
161 if( pObj )
162 transformShape( *pObj );
163 }
164}
165
166void SdTransformOOo2xDocument::transformShape( SdrObject& rObj )
167{
168 SdrTextObj* pTextShape = DynCastSdrTextObj( &rObj );
169 if( pTextShape )
170 {
171 transformTextShape( *pTextShape );
172 return;
173 }
174
175 SdrObjGroup* pGroupShape = dynamic_cast< SdrObjGroup* >( &rObj );
176 if( pGroupShape )
177 {
178 SdrObjList* pObjList = pGroupShape->GetSubList();
179 if( pObjList )
180 transformShapes( *pObjList );
181 return;
182 }
183}
184
185void SdTransformOOo2xDocument::transformTextShape( SdrTextObj& rTextShape )
186{
187
188 if(rTextShape.IsEmptyPresObj())
189 return;
190
191 OutlinerParaObject* pOPO = rTextShape.GetOutlinerParaObject();
192 if (!pOPO)
193 return;
194
195 mrOutliner.SetText( *pOPO );
196
197 sal_Int32 nCount = mrOutliner.GetParagraphCount();
198
199 bool bChange = false;
200
201 for(sal_Int32 nPara = 0; nPara < nCount; nPara++)
202 {
203 SfxItemSet aParaSet( mrOutliner.GetParaAttribs( nPara ) );
204
205 bool bItemChange = false;
206
207 bool bState = false;
208 const sal_Int16 nDepth = mrOutliner.GetDepth( nPara );
209 if( (nDepth != -1) && (!getBulletState( aParaSet, mrOutliner.GetStyleSheet( nPara ), bState ) || !bState) )
210 {
211 // disable bullet if text::enable-bullet="false" is found
212 if( (nDepth > 0 ) && (rTextShape.GetObjInventor() == SdrInventor::Default) && (rTextShape.GetObjIdentifier() == SdrObjKind::OutlineText) )
213 {
214 // for outline object and level > 0 burn in the style sheet because it will be changed to "outline 1"
215 SfxStyleSheet* pStyleSheet = mrOutliner.GetStyleSheet( nPara );
216
217 if( pStyleSheet )
218 {
219 // optimize me: only put items hard into paragraph that are not equal to "outline 1" style!
220 SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
221
222 SfxWhichIter aIter(aParaSet);
223 sal_uInt16 nWhich(aIter.FirstWhich());
224
225 // now set all none hard attributes from the style
226 while(nWhich)
227 {
228 if(SfxItemState::SET != aIter.GetItemState())
229 {
230 aParaSet.Put(rStyleSet.Get(nWhich));
231 bItemChange = true;
232 }
233
234 nWhich = aIter.NextWhich();
235 }
236 }
237 }
238
239 mrOutliner.SetDepth( mrOutliner.GetParagraph( nPara ), -1 );
240
241 bChange = true;
242 }
243
244 bItemChange |= transformItemSet( aParaSet, bState );
245
246 bItemChange |= removeAlienAttributes( aParaSet );
247
248 if( bItemChange )
249 {
250 mrOutliner.SetParaAttribs( nPara, aParaSet );
251 bChange = true;
252 }
253 }
254
255 if( bChange )
256 rTextShape.SetOutlinerParaObject(mrOutliner.CreateParaObject());
257
258 mrOutliner.Clear();
259}
260
261bool SdTransformOOo2xDocument::getBulletState( const SfxItemSet& rSet, SfxStyleSheetBase* pSheet, bool& rState )
262{
263 if( getBulletState( rSet, EE_PARA_XMLATTRIBS, rState ) )
264 return true;
265
266 if( getBulletState( rSet, SDRATTR_XMLATTRIBUTES, rState ) )
267 return true;
268
269 if( pSheet && getBulletState( pSheet->GetItemSet(), pSheet->GetPool()->Find( pSheet->GetParent(), pSheet->GetFamily() ), rState ) )
270 return true;
271
272 return false;
273}
274
275bool SdTransformOOo2xDocument::getBulletState( const SfxItemSet& rSet, sal_uInt16 nWhich, bool& rState )
276{
277 if( rSet.GetItemState( nWhich ) == SfxItemState::SET )
278 {
280
281 const sal_uInt16 nCount = rAttr.GetAttrCount();
282 for( sal_uInt16 nItem = 0; nItem < nCount; nItem++ )
283 {
284 if( ( rAttr.GetAttrLName( nItem ) == gsEnableNumbering ) && ( rAttr.GetAttrNamespace( nItem ) == gsTextNamespace ) )
285 {
286 const OUString& sValue( rAttr.GetAttrValue( nItem ) );
287 rState = sValue == gsTrue;
288 return true;
289 }
290 }
291 }
292
293 return false;
294}
295
296bool SdTransformOOo2xDocument::transformItemSet( SfxItemSet& rSet, bool bNumbering )
297{
298 bool bRet = false;
299 const SvxLRSpaceItem* pItem = bNumbering ? rSet.GetItem<SvxLRSpaceItem>(EE_PARA_LRSPACE) : nullptr;
300 if (pItem)
301 {
302 SvxLRSpaceItem aItem(*pItem);
303 if( (aItem.GetLeft() != 0) || (aItem.GetTextFirstLineOffset() != 0) )
304 {
305 aItem.SetLeftValue( 0 );
306 aItem.SetTextFirstLineOffset( 0 );
307 rSet.Put( aItem );
308 bRet = true;
309 }
310 }
311
312 return bRet;
313}
314
315bool SdTransformOOo2xDocument::removeAlienAttributes( SfxItemSet& rSet )
316{
317 bool b = removeAlienAttributes( rSet, EE_PARA_XMLATTRIBS );
318 b |= removeAlienAttributes( rSet, SDRATTR_XMLATTRIBUTES );
319 return b;
320}
321
322bool SdTransformOOo2xDocument::removeAlienAttributes( SfxItemSet& rSet, sal_uInt16 nWhich )
323{
324 if( rSet.GetItemState( nWhich ) == SfxItemState::SET )
325 {
327
328 const sal_uInt16 nCount = rAttr.GetAttrCount();
329 for( sal_uInt16 nItem = 0; nItem < nCount; nItem++ )
330 {
331 if( ( rAttr.GetAttrLName( nItem ) == gsEnableNumbering ) && ( rAttr.GetAttrNamespace( nItem ) == gsTextNamespace ) )
332 {
333 if( nCount == 1 )
334 {
335 rSet.ClearItem( nWhich );
336 }
337 else
338 {
339 SvXMLAttrContainerItem aNewItem( nWhich );
340
341 const sal_uInt16 nFound = nItem;
342 for( nItem = 0; nItem < nCount; nItem++ )
343 {
344 if( nItem != nFound )
345 {
346 OUString const& rNamespace(rAttr.GetAttrNamespace(nItem));
347 OUString const& rPrefix(rAttr.GetAttrPrefix(nItem));
348 if (rPrefix.isEmpty())
349 {
350 aNewItem.AddAttr(rAttr.GetAttrLName(nItem), rAttr.GetAttrValue(nItem));
351 }
352 else
353 {
354 aNewItem.AddAttr(rPrefix, rNamespace, rAttr.GetAttrLName(nItem), rAttr.GetAttrValue(nItem));
355 }
356 }
357 }
358
359 rSet.Put( aNewItem );
360 }
361 return true;
362 }
363 }
364 }
365 return false;
366}
367
368/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual SdrObjList * GetSubList() const override
SdrObject * GetObj(size_t nNum) const
size_t GetObjCount() const
void SetOutlinerParaObject(std::optional< OutlinerParaObject > pTextObject)
virtual SdrInventor GetObjInventor() const
bool IsEmptyPresObj() const
virtual OutlinerParaObject * GetOutlinerParaObject() const override
virtual SdrObjKind GetObjIdentifier() const override
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
virtual const OUString & GetParent() const
SfxStyleFamily GetFamily() const
virtual SfxItemSet & GetItemSet()
SfxStyleSheetBasePool * GetPool()
OUString GetAttrPrefix(sal_uInt16 i) const
const OUString & GetAttrValue(sal_uInt16 i) const
OUString GetAttrNamespace(sal_uInt16 i) const
sal_uInt16 GetAttrCount() const
const OUString & GetAttrLName(sal_uInt16 i) const
int nCount
constexpr TypedWhichId< SvxLRSpaceItem > EE_PARA_LRSPACE(EE_PARA_START+13)
constexpr OUStringLiteral gsEnableNumbering(u"enable-numbering")
constexpr OUStringLiteral gsTrue(u"true")
void TransformOOo2xDocument(SdDrawDocument *pDocument)
transforms the given model from OOo 2.x to OOo 3.x.
Definition: sdtransform.cxx:75
constexpr OUStringLiteral gsTextNamespace(u"urn:oasis:names:tc:opendocument:xmlns:text:1.0")
static SfxItemSet & rSet
SfxStyleFamily
SVXCORE_DLLPUBLIC SdrTextObj * DynCastSdrTextObj(SdrObject *)