xref: /AOO41X/main/oox/source/drawingml/diagram/diagram.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <functional>
30*cdf0e10cSrcweir #include <boost/bind.hpp>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <com/sun/star/awt/Point.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/awt/Size.hpp>
34*cdf0e10cSrcweir #include "oox/drawingml/diagram/diagram.hxx"
35*cdf0e10cSrcweir #include "oox/drawingml/fillproperties.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir using rtl::OUString;
38*cdf0e10cSrcweir using namespace ::com::sun::star;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace oox { namespace drawingml {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace dgm {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir void Connection::dump()
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	OSL_TRACE("dgm: cnx modelId %s, srcId %s, dstId %s",
48*cdf0e10cSrcweir 			  OUSTRING_TO_CSTR( msModelId ),
49*cdf0e10cSrcweir 			  OUSTRING_TO_CSTR( msSourceId ),
50*cdf0e10cSrcweir 			  OUSTRING_TO_CSTR( msDestId ) );
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir Point::Point()
54*cdf0e10cSrcweir 	: mpShape( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) )
55*cdf0e10cSrcweir 	, mnType( 0 )
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir void Point::dump()
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir 	OSL_TRACE( "dgm: pt cnxId %s, modelId %s",
62*cdf0e10cSrcweir 			   OUSTRING_TO_CSTR( msCnxId ),
63*cdf0e10cSrcweir 			   OUSTRING_TO_CSTR( msModelId ) );
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir void Point::setModelId( const ::rtl::OUString & sModelId )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir 	msModelId = sModelId;
69*cdf0e10cSrcweir 	mpShape->setName( msModelId );
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir bool PointsTree::addChild( const PointsTreePtr & pChild )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	bool added = false;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	OSL_ENSURE( pChild->mpParent.expired(), "can't add, has already a parent" );
78*cdf0e10cSrcweir 	OSL_ENSURE( mpNode, "has no node" );
79*cdf0e10cSrcweir 	if( mpNode && pChild->mpParent.expired() )
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		pChild->mpParent = shared_from_this();
82*cdf0e10cSrcweir 		maChildrens.push_back( pChild );
83*cdf0e10cSrcweir 		added = true;
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	return added;
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir PointsTreePtr PointsTree::getParent() const
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	if( !mpParent.expired() )
92*cdf0e10cSrcweir 	{
93*cdf0e10cSrcweir 		return mpParent.lock() ;
94*cdf0e10cSrcweir 	}
95*cdf0e10cSrcweir 	return PointsTreePtr();
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir } // dgm namespace
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir DiagramData::DiagramData()
102*cdf0e10cSrcweir     : mpFillProperties( new FillProperties )
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir void DiagramData::dump()
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	OSL_TRACE("Dgm: DiagramData # of cnx: %d", maConnections.size() );
109*cdf0e10cSrcweir 	std::for_each( maConnections.begin(), maConnections.end(),
110*cdf0e10cSrcweir 				  boost::bind( &dgm::Connection::dump, _1 ) );
111*cdf0e10cSrcweir 	OSL_TRACE("Dgm: DiagramData # of pt: %d", maPoints.size() );
112*cdf0e10cSrcweir 	std::for_each( maPoints.begin(), maPoints.end(),
113*cdf0e10cSrcweir 				  boost::bind( &dgm::Point::dump, _1 ) );
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir static void setPosition( const dgm::PointPtr & pPoint, const awt::Point & pt )
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	ShapePtr pShape = pPoint->getShape();
119*cdf0e10cSrcweir 	awt::Size sz;
120*cdf0e10cSrcweir 	sz.Width = 50;
121*cdf0e10cSrcweir 	sz.Height = 50;
122*cdf0e10cSrcweir 	pShape->setPosition( pt );
123*cdf0e10cSrcweir 	pShape->setSize( sz );
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir void DiagramLayout::layout( const dgm::PointsTreePtr & pTree, const awt::Point & pt )
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	setPosition( pTree->getPoint(), pt );
129*cdf0e10cSrcweir 	awt::Point nextPt = pt;
130*cdf0e10cSrcweir 	nextPt.Y += 50;
131*cdf0e10cSrcweir 	dgm::PointsTree::Childrens::const_iterator iter;
132*cdf0e10cSrcweir 	for( iter = pTree->beginChild(); iter != pTree->endChild(); iter++ )
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		layout( *iter, nextPt );
135*cdf0e10cSrcweir 		nextPt.X += 50;
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir void Diagram::setData( const DiagramDataPtr & pData)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	mpData = pData;
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void Diagram::setLayout( const DiagramLayoutPtr & pLayout)
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	mpLayout = pLayout;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir void Diagram::setQStyles( const DiagramQStylesPtr & pStyles)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	mpQStyles = pStyles;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir void Diagram::setColors( const DiagramColorsPtr & pColors)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	mpColors = pColors;
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir void Diagram::build(  )
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir 	OSL_TRACE( "building diagram" );
164*cdf0e10cSrcweir 	typedef std::map< OUString, dgm::PointPtr > PointsMap;
165*cdf0e10cSrcweir 	PointsMap aPointsMap;
166*cdf0e10cSrcweir 	dgm::Points::iterator aPointsIter( mpData->getPoints( ).begin() );
167*cdf0e10cSrcweir 	for( ; aPointsIter != mpData->getPoints( ).end() ; aPointsIter++ )
168*cdf0e10cSrcweir 	{
169*cdf0e10cSrcweir 		const OUString & sName((*aPointsIter)->getModelId());
170*cdf0e10cSrcweir 		if( sName.getLength() > 0 )
171*cdf0e10cSrcweir 		{
172*cdf0e10cSrcweir 			aPointsMap[ sName ] = *aPointsIter;
173*cdf0e10cSrcweir 		}
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	typedef std::map< OUString, dgm::PointsTreePtr > PointsTreeMap;
177*cdf0e10cSrcweir 	PointsTreeMap aTreeMap;
178*cdf0e10cSrcweir 	PointsTreeMap aRoots;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	dgm::Connections & aConnections(mpData->getConnections( ) );
181*cdf0e10cSrcweir 	dgm::Connections::iterator aCnxIter;
182*cdf0e10cSrcweir 	for( aCnxIter = aConnections.begin(); aCnxIter != aConnections.end(); ++aCnxIter )
183*cdf0e10cSrcweir 	{
184*cdf0e10cSrcweir 		OSL_ENSURE( *aCnxIter, "NULL connection found" );
185*cdf0e10cSrcweir 		if( (*aCnxIter)->mnType != XML_parOf )
186*cdf0e10cSrcweir 		{
187*cdf0e10cSrcweir //			OSL_TRACE( "ignoring relation %s", OUSTRING_TO_CSTR( (*aCnxIter)->msModelId ) );
188*cdf0e10cSrcweir 			continue;
189*cdf0e10cSrcweir 		}
190*cdf0e10cSrcweir 		dgm::PointPtr pDest;
191*cdf0e10cSrcweir 		dgm::PointsTreePtr pSource;
192*cdf0e10cSrcweir 		PointsMap::iterator iterP;
193*cdf0e10cSrcweir 		OUString & srcId( (*aCnxIter)->msSourceId );
194*cdf0e10cSrcweir 		OUString & dstId( (*aCnxIter)->msDestId );
195*cdf0e10cSrcweir 		OSL_TRACE( "connexion %s -> %s", OUSTRING_TO_CSTR( srcId ),
196*cdf0e10cSrcweir 				   OUSTRING_TO_CSTR( dstId ) );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		PointsTreeMap::iterator iterT = aTreeMap.find( srcId );
199*cdf0e10cSrcweir 		if( iterT != aTreeMap.end() )
200*cdf0e10cSrcweir 		{
201*cdf0e10cSrcweir 			pSource = iterT->second;
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 		else
204*cdf0e10cSrcweir 		{
205*cdf0e10cSrcweir 			// this tree node is not found. create it with the source
206*cdf0e10cSrcweir 			// and make it the root node.
207*cdf0e10cSrcweir 			iterP = aPointsMap.find( srcId );
208*cdf0e10cSrcweir 			if( iterP != aPointsMap.end() )
209*cdf0e10cSrcweir 			{
210*cdf0e10cSrcweir 				pSource.reset( new dgm::PointsTree( iterP->second ) );
211*cdf0e10cSrcweir 				aRoots[ srcId ] = pSource;
212*cdf0e10cSrcweir 				aTreeMap[ srcId ] = pSource;
213*cdf0e10cSrcweir 			}
214*cdf0e10cSrcweir 			else
215*cdf0e10cSrcweir 			{
216*cdf0e10cSrcweir 				OSL_TRACE("parent node not found !");
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 		iterP = aPointsMap.find( dstId );
220*cdf0e10cSrcweir 		if( iterP != aPointsMap.end() )
221*cdf0e10cSrcweir 		{
222*cdf0e10cSrcweir 			pDest = iterP->second;
223*cdf0e10cSrcweir 		}
224*cdf0e10cSrcweir 		OSL_ENSURE( pDest, "destination not found" );
225*cdf0e10cSrcweir 		OSL_ENSURE( pSource, "source not found" );
226*cdf0e10cSrcweir 		if(pDest && pSource)
227*cdf0e10cSrcweir 		{
228*cdf0e10cSrcweir 			dgm::PointsTreePtr pNode( new dgm::PointsTree( pDest ) );
229*cdf0e10cSrcweir 			bool added = pSource->addChild( pNode );
230*cdf0e10cSrcweir 			(void)added;
231*cdf0e10cSrcweir 			aRoots.erase( dstId );
232*cdf0e10cSrcweir 			OSL_ENSURE( added, "add child failed" );
233*cdf0e10cSrcweir 			aTreeMap[ dstId ] = pNode;
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 	// check bounds
237*cdf0e10cSrcweir 	OSL_ENSURE( aRoots.size() == 1, "more than one root" );
238*cdf0e10cSrcweir     // #i92239# roots may be empty
239*cdf0e10cSrcweir     if( !aRoots.empty() )
240*cdf0e10cSrcweir     {
241*cdf0e10cSrcweir         mpRoot = aRoots.begin()->second;
242*cdf0e10cSrcweir         OSL_TRACE( "root is %s", OUSTRING_TO_CSTR( mpRoot->getPoint()->getModelId() ) );
243*cdf0e10cSrcweir         for( PointsTreeMap::iterator iter = aTreeMap.begin();
244*cdf0e10cSrcweir              iter != aTreeMap.end(); iter++ )
245*cdf0e10cSrcweir         {
246*cdf0e10cSrcweir             if(! iter->second->getParent() )
247*cdf0e10cSrcweir             {
248*cdf0e10cSrcweir                 OSL_TRACE("node without parent %s", OUSTRING_TO_CSTR( iter->first ) );
249*cdf0e10cSrcweir             }
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir     }
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir void Diagram::addTo( const ShapePtr & pParentShape )
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	dgm::Points & aPoints( mpData->getPoints( ) );
258*cdf0e10cSrcweir 	dgm::Points::iterator aPointsIter;
259*cdf0e10cSrcweir 	build( );
260*cdf0e10cSrcweir     if( mpRoot.get() )
261*cdf0e10cSrcweir         mpLayout->layout( mpRoot, awt::Point( 0, 0 ) );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 	for( aPointsIter = aPoints.begin(); aPointsIter != aPoints.end(); ++aPointsIter )
264*cdf0e10cSrcweir 	{
265*cdf0e10cSrcweir 		if( ( *aPointsIter )->getType() != XML_node )
266*cdf0e10cSrcweir 		{
267*cdf0e10cSrcweir 			continue;
268*cdf0e10cSrcweir 		}
269*cdf0e10cSrcweir 		ShapePtr pShape = ( *aPointsIter )->getShape( );
270*cdf0e10cSrcweir 		if( pShape->getName( ).getLength() > 0 )
271*cdf0e10cSrcweir 		{
272*cdf0e10cSrcweir 			maShapeMap[ pShape->getName( ) ] = pShape;
273*cdf0e10cSrcweir 			OSL_TRACE( "Dgm: added shape %s to map", OUSTRING_TO_CSTR( pShape->getName() ) );
274*cdf0e10cSrcweir 		}
275*cdf0e10cSrcweir 		pParentShape->addChild( pShape );
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     OSL_TRACE( "Dgm: addTo() # of childs %d", pParentShape->getChildren().size() );
279*cdf0e10cSrcweir     for( std::vector< ShapePtr >::iterator iter = pParentShape->getChildren().begin();
280*cdf0e10cSrcweir          iter != pParentShape->getChildren().end(); ++iter)
281*cdf0e10cSrcweir 	{
282*cdf0e10cSrcweir 		OSL_TRACE( "Dgm: shape name %s", OUSTRING_TO_CSTR( (*iter)->getName() ) );
283*cdf0e10cSrcweir 	}
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir OUString Diagram::getLayoutId() const
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	OUString sLayoutId;
289*cdf0e10cSrcweir 	if( mpLayout )
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir 		sLayoutId = mpLayout->getUniqueId();
292*cdf0e10cSrcweir 	}
293*cdf0e10cSrcweir 	return sLayoutId;
294*cdf0e10cSrcweir }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir } }
298