Branch data Line data Source code
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 : :
21 : : #include <functional>
22 : : #include <boost/bind.hpp>
23 : :
24 : : #include <com/sun/star/awt/Point.hpp>
25 : : #include <com/sun/star/awt/Size.hpp>
26 : : #include <com/sun/star/xml/dom/XDocument.hpp>
27 : : #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
28 : : #include <rtl/ustrbuf.hxx>
29 : : #include "oox/drawingml/textbody.hxx"
30 : : #include "oox/drawingml/textparagraph.hxx"
31 : : #include "oox/drawingml/textrun.hxx"
32 : : #include "oox/drawingml/diagram/diagram.hxx"
33 : : #include "oox/drawingml/fillproperties.hxx"
34 : : #include "oox/ppt/pptshapegroupcontext.hxx"
35 : : #include "oox/ppt/pptshape.hxx"
36 : :
37 : : #include "diagramlayoutatoms.hxx"
38 : : #include "diagramfragmenthandler.hxx"
39 : :
40 : : #include <iostream>
41 : : #include <fstream>
42 : :
43 : : using rtl::OUString;
44 : : using namespace ::com::sun::star;
45 : :
46 : : namespace oox { namespace drawingml {
47 : :
48 : : namespace dgm {
49 : :
50 : 42 : void Connection::dump()
51 : : {
52 : : OSL_TRACE("dgm: cnx modelId %s, srcId %s, dstId %s, parTransId %s, presId %s, sibTransId %s, srcOrd %d, dstOrd %d",
53 : : OUSTRING_TO_CSTR( msModelId ),
54 : : OUSTRING_TO_CSTR( msSourceId ),
55 : : OUSTRING_TO_CSTR( msDestId ),
56 : : OUSTRING_TO_CSTR( msParTransId ),
57 : : OUSTRING_TO_CSTR( msPresId ),
58 : : OUSTRING_TO_CSTR( msSibTransId ),
59 : : mnSourceOrder,
60 : : mnDestOrder );
61 : 42 : }
62 : :
63 : 54 : void Point::dump()
64 : : {
65 : : OSL_TRACE( "dgm: pt text %x, cnxId %s, modelId %s, type %d",
66 : : mpShape.get(),
67 : : OUSTRING_TO_CSTR( msCnxId ),
68 : : OUSTRING_TO_CSTR( msModelId ),
69 : : mnType );
70 : 54 : }
71 : :
72 : : } // dgm namespace
73 : :
74 : 3 : DiagramData::DiagramData()
75 [ + - ][ + - ]: 3 : : mpFillProperties( new FillProperties )
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
76 : : {
77 : 3 : }
78 : :
79 : 3 : void DiagramData::dump()
80 : : {
81 : : OSL_TRACE("Dgm: DiagramData # of cnx: %d", maConnections.size() );
82 : : std::for_each( maConnections.begin(), maConnections.end(),
83 : 3 : boost::bind( &dgm::Connection::dump, _1 ) );
84 : : OSL_TRACE("Dgm: DiagramData # of pt: %d", maPoints.size() );
85 : : std::for_each( maPoints.begin(), maPoints.end(),
86 : 3 : boost::bind( &dgm::Point::dump, _1 ) );
87 : 3 : }
88 : :
89 : :
90 : 3 : void Diagram::setData( const DiagramDataPtr & pData)
91 : : {
92 : 3 : mpData = pData;
93 : 3 : }
94 : :
95 : :
96 : 3 : void Diagram::setLayout( const DiagramLayoutPtr & pLayout)
97 : : {
98 : 3 : mpLayout = pLayout;
99 : 3 : }
100 : :
101 : : #if OSL_DEBUG_LEVEL > 1
102 : : rtl::OString normalizeDotName( const rtl::OUString& rStr )
103 : : {
104 : : rtl::OUStringBuffer aBuf;
105 : : aBuf.append((sal_Unicode)'N');
106 : :
107 : : const sal_Int32 nLen(rStr.getLength());
108 : : sal_Int32 nCurrIndex(0);
109 : : while( nCurrIndex < nLen )
110 : : {
111 : : const sal_Int32 aChar=rStr.iterateCodePoints(&nCurrIndex);
112 : : if( aChar != '-' && aChar != '{' && aChar != '}' )
113 : : aBuf.append((sal_Unicode)aChar);
114 : : }
115 : :
116 : : return rtl::OUStringToOString(aBuf.makeStringAndClear(),
117 : : RTL_TEXTENCODING_UTF8);
118 : : }
119 : : #endif
120 : :
121 : 21 : static sal_Int32 calcDepth( const rtl::OUString& rNodeName,
122 : : const dgm::Connections& rCnx )
123 : : {
124 : : // find length of longest path in 'isChild' graph, ending with rNodeName
125 : 21 : dgm::Connections::const_iterator aCurrCxn( rCnx.begin() );
126 : 21 : const dgm::Connections::const_iterator aEndCxn( rCnx.end() );
127 [ + - ][ + + ]: 228 : while( aCurrCxn != aEndCxn )
128 : : {
129 [ + + + - : 540 : if( !aCurrCxn->msParTransId.isEmpty() &&
+ - + - +
- + - +
+ ][ + + ]
130 : 54 : !aCurrCxn->msSibTransId.isEmpty() &&
131 : 54 : !aCurrCxn->msSourceId.isEmpty() &&
132 : 54 : !aCurrCxn->msDestId.isEmpty() &&
133 : 54 : aCurrCxn->mnType != XML_presOf &&
134 : 54 : aCurrCxn->mnType != XML_presParOf &&
135 : 54 : rNodeName == aCurrCxn->msDestId )
136 : : {
137 : 9 : return calcDepth(aCurrCxn->msSourceId,
138 [ + - ]: 9 : rCnx) + 1;
139 : : }
140 : 207 : ++aCurrCxn;
141 : : }
142 : :
143 : 21 : return 0;
144 : : }
145 : :
146 : :
147 : 3 : void Diagram::build( )
148 : : {
149 : : // build name-object maps
150 : : // ======================
151 : :
152 : : #if OSL_DEBUG_LEVEL > 1
153 : : std::ofstream output("/tmp/tree.dot");
154 : :
155 : : output << "digraph datatree {" << std::endl;
156 : : #endif
157 : :
158 [ + - ][ + - ]: 3 : dgm::Points::iterator aCurrPoint( getData()->getPoints( ).begin() );
159 [ + - ][ + - ]: 3 : const dgm::Points::iterator aEndPoint( getData()->getPoints( ).end() );
160 [ + - ][ + + ]: 57 : while( aCurrPoint != aEndPoint )
161 : : {
162 : : #if OSL_DEBUG_LEVEL > 1
163 : : output << "\t"
164 : : << normalizeDotName(aCurrPoint->msModelId).getStr()
165 : : << "[";
166 : :
167 : : if( !aCurrPoint->msPresentationLayoutName.isEmpty() )
168 : : output << "label=\""
169 : : << rtl::OUStringToOString(
170 : : aCurrPoint->msPresentationLayoutName,
171 : : RTL_TEXTENCODING_UTF8).getStr() << "\", ";
172 : : else
173 : : output << "label=\""
174 : : << rtl::OUStringToOString(
175 : : aCurrPoint->msModelId,
176 : : RTL_TEXTENCODING_UTF8).getStr() << "\", ";
177 : :
178 : : switch( aCurrPoint->mnType )
179 : : {
180 : : case XML_doc: output << "style=filled, color=red"; break;
181 : : case XML_asst: output << "style=filled, color=green"; break;
182 : : default:
183 : : case XML_node: output << "style=filled, color=blue"; break;
184 : : case XML_pres: output << "style=filled, color=yellow"; break;
185 : : case XML_parTrans: output << "color=grey"; break;
186 : : case XML_sibTrans: output << " "; break;
187 : : }
188 : :
189 : : output << "];" << std::endl;
190 : :
191 : : // does currpoint have any text set?
192 : : if( aCurrPoint->mpShape &&
193 : : aCurrPoint->mpShape->getTextBody() &&
194 : : !aCurrPoint->mpShape->getTextBody()->getParagraphs().empty() &&
195 : : !aCurrPoint->mpShape->getTextBody()->getParagraphs().front()->getRuns().empty() )
196 : : {
197 : : static sal_Int32 nCount=0;
198 : :
199 : : output << "\t"
200 : : << "textNode" << nCount
201 : : << " ["
202 : : << "label=\""
203 : : << rtl::OUStringToOString(
204 : : aCurrPoint->mpShape->getTextBody()->getParagraphs().front()->getRuns().front()->getText(),
205 : : RTL_TEXTENCODING_UTF8).getStr()
206 : : << "\"" << "];" << std::endl;
207 : : output << "\t"
208 : : << normalizeDotName(aCurrPoint->msModelId).getStr()
209 : : << " -> "
210 : : << "textNode" << nCount++
211 : : << ";" << std::endl;
212 : : }
213 : : #endif
214 : :
215 [ + - ][ + - ]: 108 : const bool bInserted1=getData()->getPointNameMap().insert(
216 [ + - ]: 162 : std::make_pair(aCurrPoint->msModelId,&(*aCurrPoint))).second;
217 : : (void)bInserted1;
218 : :
219 : : OSL_ENSURE(bInserted1,"Diagram::build(): non-unique point model id");
220 : :
221 [ + + ]: 54 : if( !aCurrPoint->msPresentationLayoutName.isEmpty() )
222 : : {
223 : : DiagramData::PointsNameMap::value_type::second_type& rVec=
224 [ + - ][ + - ]: 24 : getData()->getPointsPresNameMap()[aCurrPoint->msPresentationLayoutName];
[ + - ]
225 [ + - ]: 24 : rVec.push_back(&(*aCurrPoint));
226 : : }
227 : 54 : ++aCurrPoint;
228 : : }
229 : :
230 [ + - ][ + - ]: 3 : dgm::Connections::const_iterator aCurrCxn( getData()->getConnections( ).begin() );
[ + - ]
231 [ + - ][ + - ]: 3 : const dgm::Connections::const_iterator aEndCxn( getData()->getConnections( ).end() );
[ + - ]
232 [ + - ][ + + ]: 45 : while( aCurrCxn != aEndCxn )
233 : : {
234 : : #if OSL_DEBUG_LEVEL > 1
235 : : if( !aCurrCxn->msParTransId.isEmpty() ||
236 : : !aCurrCxn->msSibTransId.isEmpty() )
237 : : {
238 : : if( !aCurrCxn->msSourceId.isEmpty() ||
239 : : !aCurrCxn->msDestId.isEmpty() )
240 : : {
241 : : output << "\t"
242 : : << normalizeDotName(aCurrCxn->msSourceId).getStr()
243 : : << " -> "
244 : : << normalizeDotName(aCurrCxn->msParTransId).getStr()
245 : : << " -> "
246 : : << normalizeDotName(aCurrCxn->msSibTransId).getStr()
247 : : << " -> "
248 : : << normalizeDotName(aCurrCxn->msDestId).getStr()
249 : : << " [style=dotted,"
250 : : << ((aCurrCxn->mnType == XML_presOf) ? " color=red, " : ((aCurrCxn->mnType == XML_presParOf) ? " color=green, " : " "))
251 : : << "label=\""
252 : : << rtl::OUStringToOString(aCurrCxn->msModelId,
253 : : RTL_TEXTENCODING_UTF8 ).getStr()
254 : : << "\"];" << std::endl;
255 : : }
256 : : else
257 : : {
258 : : output << "\t"
259 : : << normalizeDotName(aCurrCxn->msParTransId).getStr()
260 : : << " -> "
261 : : << normalizeDotName(aCurrCxn->msSibTransId).getStr()
262 : : << " ["
263 : : << ((aCurrCxn->mnType == XML_presOf) ? " color=red, " : ((aCurrCxn->mnType == XML_presParOf) ? " color=green, " : " "))
264 : : << "label=\""
265 : : << rtl::OUStringToOString(aCurrCxn->msModelId,
266 : : RTL_TEXTENCODING_UTF8 ).getStr()
267 : : << "\"];" << std::endl;
268 : : }
269 : : }
270 : : else if( !aCurrCxn->msSourceId.isEmpty() ||
271 : : !aCurrCxn->msDestId.isEmpty() )
272 : : output << "\t"
273 : : << normalizeDotName(aCurrCxn->msSourceId).getStr()
274 : : << " -> "
275 : : << normalizeDotName(aCurrCxn->msDestId).getStr()
276 : : << " [label=\""
277 : : << rtl::OUStringToOString(aCurrCxn->msModelId,
278 : : RTL_TEXTENCODING_UTF8 ).getStr()
279 : : << ((aCurrCxn->mnType == XML_presOf) ? "\", color=red]" : ((aCurrCxn->mnType == XML_presParOf) ? "\", color=green]" : "\"]"))
280 : : << ";" << std::endl;
281 : : #endif
282 : :
283 [ + - ][ + - ]: 84 : const bool bInserted1=getData()->getConnectionNameMap().insert(
284 [ + - ]: 126 : std::make_pair(aCurrCxn->msModelId,&(*aCurrCxn))).second;
285 : : (void)bInserted1;
286 : :
287 : : OSL_ENSURE(bInserted1,"Diagram::build(): non-unique connection model id");
288 : :
289 [ + + ]: 42 : if( aCurrCxn->mnType == XML_presOf )
290 : : {
291 [ + - ][ + - ]: 12 : DiagramData::StringMap::value_type::second_type& rVec=getData()->getPresOfNameMap()[aCurrCxn->msDestId];
[ + - ]
292 : : rVec.push_back(
293 : : std::make_pair(
294 [ + - ][ + - ]: 12 : aCurrCxn->msSourceId,sal_Int32(0)));
295 : : }
296 : :
297 : 42 : ++aCurrCxn;
298 : : }
299 : :
300 : : // assign outline levels
301 [ + - ][ + - ]: 3 : DiagramData::StringMap::iterator aPresOfIter=getData()->getPresOfNameMap().begin();
302 [ + - ][ + - ]: 3 : const DiagramData::StringMap::iterator aPresOfEnd=getData()->getPresOfNameMap().end();
303 [ + + ]: 15 : while( aPresOfIter != aPresOfEnd )
304 : : {
305 : 12 : DiagramData::StringMap::value_type::second_type::iterator aPresOfNodeIterCalcLevel=aPresOfIter->second.begin();
306 : 12 : const DiagramData::StringMap::value_type::second_type::iterator aPresOfNodeEnd=aPresOfIter->second.end();
307 [ + - ][ + + ]: 24 : while(aPresOfNodeIterCalcLevel != aPresOfNodeEnd)
308 : : {
309 : 12 : const sal_Int32 nDepth=calcDepth(aPresOfNodeIterCalcLevel->first,
310 [ + - ][ + - ]: 24 : getData()->getConnections());
[ + - ]
311 [ + + ]: 12 : aPresOfNodeIterCalcLevel->second = nDepth != 0 ? nDepth : -1;
312 : 12 : ++aPresOfNodeIterCalcLevel;
313 : : }
314 : :
315 : 12 : ++aPresOfIter;
316 : : }
317 : :
318 : : #if OSL_DEBUG_LEVEL > 1
319 : : output << "}" << std::endl;
320 : : #endif
321 : 3 : }
322 : :
323 : :
324 : 3 : void Diagram::addTo( const ShapePtr & pParentShape )
325 : : {
326 : : // collect data, init maps
327 [ + - ]: 3 : build( );
328 : :
329 : : // create Shape hierarchy
330 [ + - ]: 3 : ShapeCreationVisitor aCreationVisitor(pParentShape, *this);
331 [ - + ]: 3 : if( mpLayout->getNode() )
332 [ # # ][ + - ]: 3 : mpLayout->getNode()->accept( aCreationVisitor );
333 : 3 : }
334 : :
335 : 3 : uno::Reference<xml::dom::XDocument> loadFragment(
336 : : core::XmlFilterBase& rFilter,
337 : : const rtl::Reference< core::FragmentHandler >& rxHandler )
338 : : {
339 : : // load diagramming fragments into DOM representation, that later
340 : : // gets serialized back to SAX events and parsed
341 : 3 : return rFilter.importFragment( rxHandler->getFragmentPath() );
342 : : }
343 : :
344 : 3 : void importFragment( core::XmlFilterBase& rFilter,
345 : : const uno::Reference<xml::dom::XDocument>& rXDom,
346 : : const char* /*pPropName*/,
347 : : const ShapePtr& /*pShape*/,
348 : : const rtl::Reference< core::FragmentHandler >& rxHandler )
349 : : {
350 : : uno::Reference<xml::sax::XFastSAXSerializable> xSerializer(
351 [ + - ]: 3 : rXDom, uno::UNO_QUERY_THROW);
352 : :
353 : : // now serialize DOM tree into internal data structures
354 [ + - ]: 3 : rFilter.importFragment( rxHandler, xSerializer );
355 : 3 : }
356 : :
357 : 3 : void loadDiagram( ShapePtr& pShape,
358 : : core::XmlFilterBase& rFilter,
359 : : const ::rtl::OUString& rDataModelPath,
360 : : const ::rtl::OUString& rLayoutPath,
361 : : const ::rtl::OUString& rQStylePath,
362 : : const ::rtl::OUString& rColorStylePath )
363 : : {
364 [ + - ][ + - ]: 3 : DiagramPtr pDiagram( new Diagram() );
[ + - ]
365 : :
366 [ + - ][ + - ]: 3 : DiagramDataPtr pData( new DiagramData() );
[ + - ]
367 [ + - ]: 3 : pDiagram->setData( pData );
368 : :
369 [ + - ][ + - ]: 3 : DiagramLayoutPtr pLayout( new DiagramLayout() );
[ + - ]
370 [ + - ]: 3 : pDiagram->setLayout( pLayout );
371 : :
372 : : // data
373 [ + - ]: 3 : if( !rDataModelPath.isEmpty() )
374 : : {
375 : : rtl::Reference< core::FragmentHandler > xRef(
376 [ + - ][ + - ]: 3 : new DiagramDataFragmentHandler( rFilter, rDataModelPath, pData ));
377 : :
378 : : importFragment(rFilter,
379 : : loadFragment(rFilter,xRef),
380 : : "DiagramData",
381 : : pShape,
382 [ + - ][ + - ]: 3 : xRef);
383 : : // Pass the info to pShape
384 [ + - ][ + - ]: 6 : for( ::std::vector<OUString>::const_iterator aIt = pData->getExtDrawings().begin(), aEnd = pData->getExtDrawings().end();
[ + + ][ + - ]
385 : : aIt != aEnd; ++aIt )
386 [ + - ]: 6 : pShape->addExtDrawingRelId( *aIt );
387 : : }
388 : :
389 : : // extLst is present, lets bet on that and ignore the rest of the data from here
390 [ - + ]: 3 : if( !pData->getExtDrawings().size() )
391 : : {
392 : : // layout
393 [ # # ]: 0 : if( !rLayoutPath.isEmpty() )
394 : : {
395 : : rtl::Reference< core::FragmentHandler > xRef(
396 [ # # ][ # # ]: 0 : new DiagramLayoutFragmentHandler( rFilter, rLayoutPath, pLayout ));
397 : : importFragment(rFilter,
398 : : loadFragment(rFilter,xRef),
399 : : "DiagramLayout",
400 : : pShape,
401 [ # # ][ # # ]: 0 : xRef);
402 : : }
403 : :
404 : : // style
405 [ # # ]: 0 : if( !rQStylePath.isEmpty() )
406 : : {
407 : : rtl::Reference< core::FragmentHandler > xRef(
408 [ # # ]: 0 : new DiagramQStylesFragmentHandler( rFilter, rQStylePath, pDiagram->getStyles() ));
409 : : importFragment(rFilter,
410 : : loadFragment(rFilter,xRef),
411 : : "DiagramQStyle",
412 : : pShape,
413 [ # # ][ # # ]: 0 : xRef);
414 : : }
415 : :
416 : : // colors
417 [ # # ]: 0 : if( !rColorStylePath.isEmpty() )
418 : : {
419 : : rtl::Reference< core::FragmentHandler > xRef(
420 [ # # ]: 0 : new ColorFragmentHandler( rFilter, rColorStylePath, pDiagram->getColors() ));
421 : : importFragment(rFilter,
422 : : loadFragment(rFilter,xRef),
423 : : "DiagramColorStyle",
424 : : pShape,
425 [ # # ][ # # ]: 0 : xRef);
426 : : }
427 : : }
428 : :
429 : : // diagram loaded. now lump together & attach to shape
430 [ + - ][ + - ]: 3 : pDiagram->addTo(pShape);
[ + - ][ + - ]
431 : 3 : }
432 : :
433 : 0 : void loadDiagram( const ShapePtr& pShape,
434 : : core::XmlFilterBase& rFilter,
435 : : const uno::Reference<xml::dom::XDocument>& rXDataModelDom,
436 : : const uno::Reference<xml::dom::XDocument>& rXLayoutDom,
437 : : const uno::Reference<xml::dom::XDocument>& rXQStyleDom,
438 : : const uno::Reference<xml::dom::XDocument>& rXColorStyleDom )
439 : : {
440 [ # # ][ # # ]: 0 : DiagramPtr pDiagram( new Diagram() );
[ # # ]
441 : :
442 [ # # ][ # # ]: 0 : DiagramDataPtr pData( new DiagramData() );
[ # # ]
443 [ # # ]: 0 : pDiagram->setData( pData );
444 : :
445 [ # # ][ # # ]: 0 : DiagramLayoutPtr pLayout( new DiagramLayout() );
[ # # ]
446 [ # # ]: 0 : pDiagram->setLayout( pLayout );
447 : :
448 : 0 : OUString aEmpty;
449 : :
450 : : // data
451 [ # # ]: 0 : if( rXDataModelDom.is() )
452 : : importFragment(rFilter,
453 : : rXDataModelDom,
454 : : "DiagramData",
455 : : pShape,
456 [ # # ][ # # ]: 0 : new DiagramDataFragmentHandler( rFilter, aEmpty, pData ));
[ # # ]
457 : :
458 : : // layout
459 [ # # ]: 0 : if( rXLayoutDom.is() )
460 : : importFragment(rFilter,
461 : : rXLayoutDom,
462 : : "DiagramLayout",
463 : : pShape,
464 [ # # ][ # # ]: 0 : new DiagramLayoutFragmentHandler( rFilter, aEmpty, pLayout ));
[ # # ]
465 : :
466 : : // style
467 [ # # ]: 0 : if( rXQStyleDom.is() )
468 : : importFragment(rFilter,
469 : : rXQStyleDom,
470 : : "DiagramQStyle",
471 : : pShape,
472 [ # # ][ # # ]: 0 : new DiagramQStylesFragmentHandler( rFilter, aEmpty, pDiagram->getStyles() ));
473 : :
474 : : // colors
475 [ # # ]: 0 : if( rXColorStyleDom.is() )
476 : : importFragment(rFilter,
477 : : rXColorStyleDom,
478 : : "DiagramColorStyle",
479 : : pShape,
480 [ # # ][ # # ]: 0 : new ColorFragmentHandler( rFilter, aEmpty, pDiagram->getColors() ));
481 : :
482 : : // diagram loaded. now lump together & attach to shape
483 [ # # ][ # # ]: 0 : pDiagram->addTo(pShape);
[ # # ][ # # ]
484 : 0 : }
485 : :
486 [ + - ][ + - ]: 285 : } }
487 : :
488 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|