summaryrefslogtreecommitdiff
path: root/oox/source/drawingml/diagram/diagramlayoutatoms.cxx
blob: 180a61310aa479ef301240e55f30fc1a91541ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "diagramlayoutatoms.hxx"

#include <functional>
#include <boost/bind.hpp>

#include <osl/diagnose.h>
#include <basegfx/numeric/ftools.hxx>

#include "oox/helper/attributelist.hxx"
#include "oox/drawingml/fillproperties.hxx"
#include "oox/drawingml/lineproperties.hxx"
#include "drawingml/textbody.hxx"
#include "drawingml/textparagraph.hxx"
#include "drawingml/textrun.hxx"
#include "drawingml/customshapeproperties.hxx"
#include "layoutnodecontext.hxx"

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::xml::sax;
using namespace ::oox::core;

namespace oox { namespace drawingml {

IteratorAttr::IteratorAttr( )
    : mnAxis( 0 )
    , mnCnt( -1 )
    , mbHideLastTrans( false )
    , mnPtType( 0 )
    , mnSt( 0 )
    , mnStep( 1 )
{
}

void IteratorAttr::loadFromXAttr( const Reference< XFastAttributeList >& xAttr )
{
    AttributeList attr( xAttr );
    mnAxis = xAttr->getOptionalValueToken( XML_axis, 0 );
    mnCnt = attr.getInteger( XML_cnt, -1 );
    mbHideLastTrans = attr.getBool( XML_hideLastTrans, false );
    mnPtType = xAttr->getOptionalValueToken( XML_ptType, 0 );
    mnSt = attr.getInteger( XML_st, 0 );
    mnStep = attr.getInteger( XML_step, 1 );
}

ConditionAttr::ConditionAttr()
    : mnFunc( 0 )
    , mnArg( 0 )
    , mnOp( 0 )
{

}

void ConditionAttr::loadFromXAttr( const Reference< XFastAttributeList >& xAttr )
{
    mnFunc = xAttr->getOptionalValueToken( XML_func, 0 );
    // mnArg will be -1 for "none" or any other unknown value
    mnArg = LayoutNodeContext::tagToVarIdx( xAttr->getOptionalValueToken( XML_arg, XML_none ) );
    mnOp = xAttr->getOptionalValueToken( XML_op, 0 );
    msVal = xAttr->getOptionalValue( XML_val );
}

void LayoutAtom::dump(int level)
{
    OSL_TRACE( "level = %d - %s of type %s", level,
               OUSTRING_TO_CSTR( msName ),
               typeid(*this).name() );
    const std::vector<LayoutAtomPtr>& pChildren=getChildren();
    std::for_each( pChildren.begin(), pChildren.end(),
                   boost::bind( &LayoutAtom::dump, _1, level + 1 ) );
}

ForEachAtom::ForEachAtom(const Reference< XFastAttributeList >& xAttributes)
{
    maIter.loadFromXAttr(xAttributes);
}

void ForEachAtom::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

void ChooseAtom::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

ConditionAtom::ConditionAtom(const Reference< XFastAttributeList >& xAttributes) :
    mbElse( false )
{
    maIter.loadFromXAttr( xAttributes );
    maCond.loadFromXAttr( xAttributes );
}

const std::vector<LayoutAtomPtr>& ConditionAtom::getChildren() const
{
    bool bDecisionVar=true;
    // HACK
    if( maCond.mnFunc == XML_var && maCond.mnArg == XML_dir && maCond.mnOp == XML_equ && maCond.msVal != "norm" )
        bDecisionVar=false;

    if( bDecisionVar )
        return mpChildNodes;
    else
        return mpElseChildNodes;
}

void ConditionAtom::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

void ConditionAtom::addChild( const LayoutAtomPtr & pNode )
{
    if( mbElse )
        mpElseChildNodes.push_back( pNode );
    else
        mpChildNodes.push_back( pNode );
}

void ConstraintAtom::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

void AlgAtom::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

void AlgAtom::layoutShape( const ShapePtr& rShape,
                           const Diagram&  /*rDgm*/,
                           const OUString& rName ) const
{
    switch(mnType)
    {
        case XML_composite:
        {
            if( rShape->getChildren().empty() )
            {
                rShape->setSize(awt::Size(50,50));
                break;
            }

            // just put stuff below each other
            const sal_Int32 nIncX=0;
            const sal_Int32 nIncY=1;

            std::vector<ShapePtr>::const_iterator aCurrShape=rShape->getChildren().begin();
            const std::vector<ShapePtr>::const_iterator aLastShape=rShape->getChildren().end();

            // find biggest shape
            awt::Size aMaxSize;
            while( aCurrShape != aLastShape )
            {
                const awt::Size& sz=(*aCurrShape)->getSize();

                aMaxSize.Width = std::max(
                    aMaxSize.Width,
                    sz.Width);
                aMaxSize.Height = std::max(
                    aMaxSize.Height,
                    sz.Height);

                ++aCurrShape;
            }

            aCurrShape=rShape->getChildren().begin();
            const awt::Point aStartPos=(*aCurrShape)->getPosition();
            awt::Point aCurrPos=aStartPos;
            awt::Size  aTotalSize;
            aTotalSize.Width = aMaxSize.Width;
            while( aCurrShape != aLastShape )
            {
                const awt::Size& sz=(*aCurrShape)->getSize();
                (*aCurrShape)->setPosition(aCurrPos);
                (*aCurrShape)->setSize(
                    awt::Size(aMaxSize.Width,
                              sz.Height));

                aTotalSize.Height = std::max(
                    aTotalSize.Height,
                    aCurrPos.Y + sz.Height);

                aCurrPos.X += nIncX*sz.Width;
                aCurrPos.Y += nIncY*sz.Height;

                ++aCurrShape;
            }

            rShape->setSize(aTotalSize);
            break;
        }

        case XML_conn:
            break;

        case XML_cycle:
        {
            if( rShape->getChildren().empty() )
            {
                rShape->setSize(awt::Size(50,50));
                break;
            }

            const sal_Int32 nStartAngle=maMap.count(XML_stAng) ? maMap.find(XML_stAng)->second : 0;
            const sal_Int32 nSpanAngle=maMap.count(XML_spanAng) ? maMap.find(XML_spanAng)->second : 360;

            std::vector<ShapePtr>::const_iterator aCurrShape=rShape->getChildren().begin();
            const std::vector<ShapePtr>::const_iterator aLastShape=rShape->getChildren().end();
            const sal_Int32 nShapes=aLastShape-aCurrShape;

            // find biggest shape
            awt::Size aMaxSize;
            while( aCurrShape != aLastShape )
            {
                const awt::Size& sz=(*aCurrShape)->getSize();

                aMaxSize.Width = std::max(
                    aMaxSize.Width,
                    sz.Width);
                aMaxSize.Height = std::max(
                    aMaxSize.Height,
                    sz.Height);

                ++aCurrShape;
            }

            // layout shapes
            const sal_Int32 nMaxDim=std::max(aMaxSize.Width,aMaxSize.Height);
            awt::Size aTotalSize;
            aCurrShape=rShape->getChildren().begin();
            for( sal_Int32 i=0; i<nShapes; ++i, ++aCurrShape )
            {
                const awt::Size& sz=(*aCurrShape)->getSize();

                const double r=nShapes*nMaxDim/F_2PI * 360.0/nSpanAngle;
                const awt::Point aCurrPos(
                    r + r*sin( (double(i)*nSpanAngle/nShapes + nStartAngle)*F_PI180 ),
                    r - r*cos( (double(i)*nSpanAngle/nShapes + nStartAngle)*F_PI180 ) );
                (*aCurrShape)->setPosition(aCurrPos);

                aTotalSize.Width = std::max(
                    aTotalSize.Width,
                    aCurrPos.X + sz.Width);
                aTotalSize.Height = std::max(
                    aTotalSize.Height,
                    aCurrPos.Y + sz.Height);
            }

            rShape->setSize(aTotalSize);
            break;
        }

        case XML_hierChild:
        case XML_hierRoot:
            break;

        case XML_lin:
        {
            if( rShape->getChildren().empty() )
            {
                rShape->setSize(awt::Size(50,50));
                break;
            }

            const sal_Int32 nDir=maMap.count(XML_linDir) ? maMap.find(XML_linDir)->second : XML_fromL;
            const sal_Int32 nIncX=nDir==XML_fromL ? 1 : (nDir==XML_fromR ? -1 : 0);
            const sal_Int32 nIncY=nDir==XML_fromT ? 1 : (nDir==XML_fromB ? -1 : 0);

            std::vector<ShapePtr>::const_iterator aCurrShape=rShape->getChildren().begin();
            const std::vector<ShapePtr>::const_iterator aLastShape=rShape->getChildren().end();
            const awt::Point aStartPos=(*aCurrShape)->getPosition();
            awt::Point aCurrPos=aStartPos;
            awt::Size  aTotalSize;
            while( aCurrShape != aLastShape )
            {
                const awt::Size& sz=(*aCurrShape)->getSize();
                (*aCurrShape)->setPosition(aCurrPos);

                aTotalSize.Width = std::max(
                    aTotalSize.Width,
                    aCurrPos.X + sz.Width);
                aTotalSize.Height = std::max(
                    aTotalSize.Height,
                    aCurrPos.Y + sz.Height);

                // HACK: the spacing is arbitrary
                aCurrPos.X += nIncX*(sz.Width+5);
                aCurrPos.Y += nIncY*(sz.Height+5);

                ++aCurrShape;
            }

            rShape->setSize(aTotalSize);
            break;
        }

        case XML_pyra:
        case XML_snake:
            break;

        case XML_sp:
            // HACK. Handled one level higher. Or rather, planned to
            break;

        case XML_tx:
        {
            TextBodyPtr pTextBody=rShape->getTextBody();
            if( !pTextBody ||
                pTextBody->getParagraphs().empty() ||
                pTextBody->getParagraphs().front()->getRuns().empty() )
            {
                rShape->setSize(awt::Size(5,5));
                break;
            }

            // HACK - count chars & paragraphs to come up with *some*
            // notion of necessary size
            const sal_Int32 nHackyFontHeight=50;
            const sal_Int32 nHackyFontWidth=20;
            awt::Size aTotalSize;
            for( sal_uInt32 nPara=0; nPara<pTextBody->getParagraphs().size(); ++nPara )
            {
                aTotalSize.Height += nHackyFontHeight;

                sal_Int32 nLocalWidth=0;
                for( sal_uInt32 nRun=0; nRun<pTextBody->getParagraphs().at(nPara)->getRuns().size(); ++nRun )
                    nLocalWidth +=
                        pTextBody->getParagraphs().at(nPara)->getRuns().at(nRun)->getText().getLength()
                        * nHackyFontWidth;

                aTotalSize.Width = std::max(
                    aTotalSize.Width,
                    nLocalWidth);
            }

            rShape->setSize(aTotalSize);
        }

        default:
            break;
    }

    OSL_TRACE("Layouting shape %s: (%d,%d,%d,%d)",
              OUSTRING_TO_CSTR( rName ),
              rShape->getPosition().X,
              rShape->getPosition().Y,
              rShape->getSize().Width,
              rShape->getSize().Height);
}

void LayoutNode::accept( LayoutAtomVisitor& rVisitor )
{
    rVisitor.visit(*this);
}

bool LayoutNode::setupShape( const ShapePtr& rShape, const Diagram& rDgm, sal_uInt32 nIdx ) const
{
    // find the data node to grab text from
    DiagramData::PointsNameMap::const_iterator aDataNode=rDgm.getData()->getPointsPresNameMap().find(msName);
    if( aDataNode != rDgm.getData()->getPointsPresNameMap().end() &&
        aDataNode->second.size() > nIdx )
    {
        OSL_TRACE( "Filling content from %d th layout node named \"%s\", modelId \"%s\"",
                   nIdx,
                   OUSTRING_TO_CSTR( msName ),
                   OUSTRING_TO_CSTR( aDataNode->second.at(nIdx)->msModelId ) );

        // got the presentation node - now, need the actual data node:
        const DiagramData::StringMap::const_iterator aNodeName=rDgm.getData()->getPresOfNameMap().find(
            aDataNode->second.at(nIdx)->msModelId);
        if( aNodeName != rDgm.getData()->getPresOfNameMap().end() )
        {
            DiagramData::StringMap::value_type::second_type::const_iterator aVecIter=aNodeName->second.begin();
            const DiagramData::StringMap::value_type::second_type::const_iterator aVecEnd=aNodeName->second.end();
            while( aVecIter != aVecEnd )
            {
                DiagramData::PointNameMap& rMap = rDgm.getData()->getPointNameMap();
                DiagramData::PointNameMap::const_iterator aDataNode2 = rMap.find(aVecIter->first);
                if (aDataNode2 == rMap.end())
                {
                    //busted, skip it
                    ++aVecIter;
                    continue;
                }

                if( aVecIter->second == 0 )
                {
                    // grab shape attr from topmost element(s)
                    rShape->getShapeProperties() = aDataNode2->second->mpShape->getShapeProperties();
                    rShape->getLineProperties() = aDataNode2->second->mpShape->getLineProperties();
                    rShape->getFillProperties() = aDataNode2->second->mpShape->getFillProperties();
                    rShape->getCustomShapeProperties() = aDataNode2->second->mpShape->getCustomShapeProperties();
                    rShape->setMasterTextListStyle( aDataNode2->second->mpShape->getMasterTextListStyle() );

                    OSL_TRACE( "Custom shape with preset type %d added for layout node named \"%s\"",
                               rShape->getCustomShapeProperties()->getShapePresetType(),
                               OUSTRING_TO_CSTR( msName ) );
                }

                // append text with right outline level
                if( aDataNode2->second->mpShape->getTextBody() &&
                    !aDataNode2->second->mpShape->getTextBody()->getParagraphs().empty() &&
                    !aDataNode2->second->mpShape->getTextBody()->getParagraphs().front()->getRuns().empty() )
                {
                    TextBodyPtr pTextBody=rShape->getTextBody();
                    if( !pTextBody )
                    {
                        pTextBody.reset( new TextBody() );

                        // also copy text attrs
                        pTextBody->getTextListStyle() =
                            aDataNode2->second->mpShape->getTextBody()->getTextListStyle();
                        pTextBody->getTextProperties() =
                            aDataNode2->second->mpShape->getTextBody()->getTextProperties();

                        rShape->setTextBody(pTextBody);
                    }

                    TextParagraph& rPara=pTextBody->addParagraph();
                    if( aVecIter->second != -1 )
                        rPara.getProperties().setLevel(aVecIter->second);

                    rPara.addRun(
                        aDataNode2->second->mpShape->getTextBody()->getParagraphs().front()->getRuns().front());
                    rPara.getProperties().apply(
                        aDataNode2->second->mpShape->getTextBody()->getParagraphs().front()->getProperties());
                }

                ++aVecIter;
            }
        }
        else
        {
            OSL_TRACE("ShapeCreationVisitor::visit: no data node name found while processing shape type %d for layout node named \"%s\"",
                      rShape->getCustomShapeProperties()->getShapePresetType(),
                      OUSTRING_TO_CSTR( msName ) );
        }

        // TODO(Q1): apply styling & coloring - taking
        // layout node's styleLbl for both style & color
        // now, but docs are a bit unclear on this
        if( !msStyleLabel.isEmpty() )
        {
            OSL_TRACE("setting style with label %s",
                      OUSTRING_TO_CSTR( msStyleLabel ) );

            const DiagramQStyleMap::const_iterator aStyle=rDgm.getStyles().find(msStyleLabel);
            if( aStyle != rDgm.getStyles().end() )
            {
                rShape->getShapeStyleRefs()[XML_fillRef] = aStyle->second.maFillStyle;
                OSL_TRACE("added fill style with id %d", aStyle->second.maFillStyle.mnThemedIdx);
                rShape->getShapeStyleRefs()[XML_lnRef] = aStyle->second.maLineStyle;
                OSL_TRACE("added line style with id %d", aStyle->second.maLineStyle.mnThemedIdx);
                rShape->getShapeStyleRefs()[XML_effectRef] = aStyle->second.maEffectStyle;
                OSL_TRACE("added effect style with id %d", aStyle->second.maEffectStyle.mnThemedIdx);
                rShape->getShapeStyleRefs()[XML_fontRef] = aStyle->second.maTextStyle;
                OSL_TRACE("added fontref style with id %d", aStyle->second.maTextStyle.mnThemedIdx);
                Color aColor=aStyle->second.maTextStyle.maPhClr;
                OSL_TRACE("added fontref color with alpha %d", aColor.getTransparency() );
            }

            const DiagramColorMap::const_iterator aColor=rDgm.getColors().find(msStyleLabel);
            if( aColor != rDgm.getColors().end() )
            {
                const DiagramColor& rColor=aColor->second;
                if( rColor.maFillColor.isUsed() )
                    rShape->getShapeStyleRefs()[XML_fillRef].maPhClr = rColor.maFillColor;
                if( rColor.maLineColor.isUsed() )
                    rShape->getShapeStyleRefs()[XML_lnRef].maPhClr = rColor.maLineColor;
                if( rColor.maEffectColor.isUsed() )
                    rShape->getShapeStyleRefs()[XML_effectRef].maPhClr = rColor.maEffectColor;
                if( rColor.maTextFillColor.isUsed() )
                    rShape->getShapeStyleRefs()[XML_fontRef].maPhClr = rColor.maTextFillColor;
            }
        }

        // even if no data node found, successful anyway. it's
        // contained at the layoutnode
        return true;
    }
    else
    {
        OSL_TRACE("ShapeCreationVisitor::visit: no text found while processing shape type %d for layout node named \"%s\"",
                  rShape->getCustomShapeProperties()->getShapePresetType(),
                  OUSTRING_TO_CSTR( msName ) );
    }

    return false;
}

// Visitation

class ShapeLayoutingVisitor : public LayoutAtomVisitor
{
    ShapePtr mpParentShape;
    const Diagram& mrDgm;
    OUString maName;

    virtual void visit(ConstraintAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(AlgAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ForEachAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ConditionAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ChooseAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(LayoutNode& rAtom) SAL_OVERRIDE;

public:
    ShapeLayoutingVisitor(const ShapePtr& rParentShape,
                          const Diagram& rDgm,
                          const OUString& rName) :
        mpParentShape(rParentShape),
        mrDgm(rDgm),
        maName(rName)
    {}

    void defaultVisit(LayoutAtom& rAtom);
};

class ShallowPresNameVisitor : public LayoutAtomVisitor
{
    const Diagram& mrDgm;
    size_t mnCnt;

    void defaultVisit(LayoutAtom& rAtom);
    virtual void visit(ConstraintAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(AlgAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ForEachAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ConditionAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(ChooseAtom& rAtom) SAL_OVERRIDE;
    virtual void visit(LayoutNode& rAtom) SAL_OVERRIDE;

public:
    ShallowPresNameVisitor(const Diagram& rDgm) :
        mrDgm(rDgm),
        mnCnt(0)
    {}

    size_t getCount() const
        { return mnCnt; }
};

void ShapeCreationVisitor::defaultVisit(LayoutAtom& rAtom)
{
    const std::vector<LayoutAtomPtr>& pChildren=rAtom.getChildren();
    std::for_each( pChildren.begin(), pChildren.end(),
                   boost::bind( &LayoutAtom::accept,
                                _1,
                                boost::ref(*this)) );
}

void ShapeCreationVisitor::visit(ConstraintAtom& /*rAtom*/)
{
    // TODO: eval the constraints
}

void ShapeCreationVisitor::visit(AlgAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShapeCreationVisitor::visit(ForEachAtom& rAtom)
{
    const std::vector<LayoutAtomPtr>& pChildren=rAtom.getChildren();

    sal_Int32 nChildren=1;
    if( rAtom.iterator().mnPtType == XML_node )
    {
        // cound child data nodes - check all child Atoms for "name"
        // attribute that is contained in diagram's
        // getPointsPresNameMap()
        ShallowPresNameVisitor aVisitor(mrDgm);
        std::for_each( pChildren.begin(), pChildren.end(),
                       boost::bind( &LayoutAtom::accept,
                                    _1,
                                    boost::ref(aVisitor)) );
        nChildren = aVisitor.getCount();
    }

    const sal_Int32 nCnt = std::min(
        nChildren,
        rAtom.iterator().mnCnt==-1 ? nChildren : rAtom.iterator().mnCnt);

    const sal_Int32 nOldIdx=mnCurrIdx;
    const sal_Int32 nStep=rAtom.iterator().mnStep;
    for( mnCurrIdx=0; mnCurrIdx<nCnt && nStep>0; mnCurrIdx+=nStep )
    {
        // TODO there is likely some conditions
        std::for_each( pChildren.begin(), pChildren.end(),
                       boost::bind( &LayoutAtom::accept,
                                    _1,
                                    boost::ref(*this)) );
    }

    // and restore idx
    mnCurrIdx = nOldIdx;
}

void ShapeCreationVisitor::visit(ConditionAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShapeCreationVisitor::visit(ChooseAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShapeCreationVisitor::visit(LayoutNode& rAtom)
{
    ShapePtr pCurrParent(mpParentShape);
    ShapePtr pCurrShape(rAtom.getShape());
    if( pCurrShape )
    {
        OSL_TRACE("ShapeCreationVisitor::visit: processing shape type %d",
                  pCurrShape->getCustomShapeProperties()->getShapePresetType() );

        // TODO(F3): cloned shape shares all properties by reference,
        // don't change them!
        ShapePtr pClonedShape(
            new Shape( pCurrShape ));

        if( rAtom.setupShape(pClonedShape, mrDgm, mnCurrIdx) )
        {
            pCurrParent->addChild(pClonedShape);
            pCurrParent = pClonedShape;
        }
    }
    else
    {
        OSL_TRACE("ShapeCreationVisitor::visit: no shape set while processing layoutnode named %s",
                  OUSTRING_TO_CSTR( rAtom.getName() ) );
    }

    // set new parent for children
    ShapePtr pPreviousParent(mpParentShape);
    mpParentShape=pCurrParent;

    // process children
    defaultVisit(rAtom);

    // restore parent
    mpParentShape=pPreviousParent;

    // layout shapes - now all child shapes are created
    ShapeLayoutingVisitor aLayoutingVisitor(pCurrParent,
                                            mrDgm,
                                            rAtom.getName());
    aLayoutingVisitor.defaultVisit(rAtom);
}

void ShapeLayoutingVisitor::defaultVisit(LayoutAtom& rAtom)
{
    // visit all children, one of them needs to be the layout algorithm
    const std::vector<LayoutAtomPtr>& pChildren=rAtom.getChildren();
    std::for_each( pChildren.begin(), pChildren.end(),
                   boost::bind( &LayoutAtom::accept,
                                _1,
                                boost::ref(*this)) );
}

void ShapeLayoutingVisitor::visit(ConstraintAtom& /*rAtom*/)
{
    // stop processing
}

void ShapeLayoutingVisitor::visit(AlgAtom& rAtom)
{
    rAtom.layoutShape(mpParentShape,mrDgm,maName);
}

void ShapeLayoutingVisitor::visit(ForEachAtom& /*rAtom*/)
{
    // stop processing
}

void ShapeLayoutingVisitor::visit(ConditionAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShapeLayoutingVisitor::visit(ChooseAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShapeLayoutingVisitor::visit(LayoutNode& /*rAtom*/)
{
    // stop processing - only traverse Condition/Choose atoms
}

void ShallowPresNameVisitor::defaultVisit(LayoutAtom& rAtom)
{
    // visit all children, at least one of them needs to have proper
    // name set
    const std::vector<LayoutAtomPtr>& pChildren=rAtom.getChildren();
    std::for_each( pChildren.begin(), pChildren.end(),
                   boost::bind( &LayoutAtom::accept,
                                _1,
                                boost::ref(*this)) );
}

void ShallowPresNameVisitor::visit(ConstraintAtom& /*rAtom*/)
{
    // stop processing
}

void ShallowPresNameVisitor::visit(AlgAtom& /*rAtom*/)
{
    // stop processing
}

void ShallowPresNameVisitor::visit(ForEachAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShallowPresNameVisitor::visit(ConditionAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShallowPresNameVisitor::visit(ChooseAtom& rAtom)
{
    defaultVisit(rAtom);
}

void ShallowPresNameVisitor::visit(LayoutNode& rAtom)
{
    DiagramData::PointsNameMap::const_iterator aDataNode=
        mrDgm.getData()->getPointsPresNameMap().find(rAtom.getName());
    if( aDataNode != mrDgm.getData()->getPointsPresNameMap().end() )
        mnCnt = std::max(mnCnt,
                         aDataNode->second.size());
}

} }

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */