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