summaryrefslogtreecommitdiffstats
path: root/reportdesign/source/ui/dlg/Navigator.cxx
blob: 690d90ca1b170fcb8044466a59b55eafe59b0efb (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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* -*- 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 <Navigator.hxx>

#include <strings.hxx>
#include <bitmaps.hlst>
#include <ReportController.hxx>
#include <UITools.hxx>
#include <reportformula.hxx>
#include <com/sun/star/report/XFixedText.hpp>
#include <com/sun/star/report/XFixedLine.hpp>
#include <com/sun/star/report/XFormattedField.hpp>
#include <com/sun/star/report/XImageControl.hpp>
#include <com/sun/star/report/XShape.hpp>
#include <helpids.h>
#include <strings.hrc>
#include <rptui_slotid.hrc>
#include <comphelper/propmultiplex.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/containermultiplexer.hxx>
#include <cppuhelper/basemutex.hxx>
#include <comphelper/SelectionMultiplex.hxx>
#include <utility>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <vcl/commandevent.hxx>
#include <ReportVisitor.hxx>
#include <core_resource.hxx>
#include <rtl/ref.hxx>
#include <svx/svxids.hrc>

#include <memory>
#include <string_view>

namespace rptui
{
using namespace ::com::sun::star;
using namespace utl;
using namespace ::comphelper;

static OUString lcl_getImageId(const uno::Reference< report::XReportComponent>& _xElement)
{
    OUString sId;
    uno::Reference< report::XFixedLine> xFixedLine(_xElement,uno::UNO_QUERY);
    if ( uno::Reference< report::XFixedText>(_xElement,uno::UNO_QUERY).is() )
        sId = RID_SVXBMP_FM_FIXEDTEXT;
    else if ( xFixedLine.is() )
        sId = xFixedLine->getOrientation() ? RID_SVXBMP_INSERT_VFIXEDLINE : RID_SVXBMP_INSERT_HFIXEDLINE;
    else if ( uno::Reference< report::XFormattedField>(_xElement,uno::UNO_QUERY).is() )
        sId = RID_SVXBMP_FM_EDIT;
    else if ( uno::Reference< report::XImageControl>(_xElement,uno::UNO_QUERY).is() )
        sId = RID_SVXBMP_FM_IMAGECONTROL;
    else if ( uno::Reference< report::XShape>(_xElement,uno::UNO_QUERY).is() )
        sId = RID_SVXBMP_DRAWTBX_CS_BASIC;
    return sId;
}

static OUString lcl_getName(const uno::Reference< beans::XPropertySet>& _xElement)
{
    OSL_ENSURE(_xElement.is(),"Found report element which is NULL!");
    OUString sTempName;
    _xElement->getPropertyValue(PROPERTY_NAME) >>= sTempName;
    OUStringBuffer sName(sTempName);
    uno::Reference< report::XFixedText> xFixedText(_xElement,uno::UNO_QUERY);
    uno::Reference< report::XReportControlModel> xReportModel(_xElement,uno::UNO_QUERY);
    if ( xFixedText.is() )
    {
        sName.append(" : " + xFixedText->getLabel());
    }
    else if ( xReportModel.is() && _xElement->getPropertySetInfo()->hasPropertyByName(PROPERTY_DATAFIELD) )
    {
        ReportFormula aFormula( xReportModel->getDataField() );
        if ( aFormula.isValid() )
        {
            sName.append(" : " + aFormula.getUndecoratedContent() );
        }
    }
    return sName.makeStringAndClear();
}

class NavigatorTree : public ::cppu::BaseMutex
                    , public reportdesign::ITraverseReport
                    , public comphelper::OSelectionChangeListener
                    , public ::comphelper::OPropertyChangeListener
{
    class UserData;
    friend class UserData;
    class UserData : public ::cppu::BaseMutex
                    ,public ::comphelper::OPropertyChangeListener
                    ,public ::comphelper::OContainerListener
    {
        uno::Reference< uno::XInterface >                           m_xContent;
        ::rtl::Reference< comphelper::OPropertyChangeMultiplexer>   m_pListener;
        ::rtl::Reference< comphelper::OContainerListenerAdapter>    m_pContainerListener;
        NavigatorTree*                                              m_pTree;
    public:
        UserData(NavigatorTree* pTree, uno::Reference<uno::XInterface> xContent);
        virtual ~UserData() override;

        const uno::Reference< uno::XInterface >& getContent() const { return m_xContent; }
        void setContent(const uno::Reference< uno::XInterface >& _xContent) { m_xContent = _xContent; }

    protected:
        // OPropertyChangeListener
        virtual void _propertyChanged(const beans::PropertyChangeEvent& _rEvent) override;

        // OContainerListener
        virtual void _elementInserted( const container::ContainerEvent& _rEvent ) override;
        virtual void _elementRemoved( const container::ContainerEvent& Event ) override;
        virtual void _elementReplaced( const container::ContainerEvent& _rEvent ) override;
        virtual void _disposing(const lang::EventObject& _rSource) override;
    };

    std::unique_ptr<weld::TreeView>                                             m_xTreeView;
    OReportController&                                                          m_rController;
    std::unique_ptr<weld::TreeIter>                                             m_xMasterReport;
    ::rtl::Reference< comphelper::OPropertyChangeMultiplexer>                   m_pReportListener;
    ::rtl::Reference< comphelper::OSelectionChangeMultiplexer>                  m_pSelectionListener;

    void insertEntry(const OUString& rName, const weld::TreeIter* pParent, const OUString& rImageId, int nPosition, const UserData* pData, weld::TreeIter& rRet);

    void traverseSection(const uno::Reference<report::XSection>& xSection, const weld::TreeIter* pParent, const OUString& rImageId, int nPosition = -1);
    void traverseFunctions(const uno::Reference< report::XFunctions>& xFunctions, const weld::TreeIter* pParent);

protected:
    // OSelectionChangeListener
    virtual void _disposing(const lang::EventObject& _rSource) override;

    // OPropertyChangeListener
    virtual void _propertyChanged(const beans::PropertyChangeEvent& _rEvent) override;

    // OContainerListener Helper
    void _elementInserted( const container::ContainerEvent& _rEvent );
    void _elementRemoved( const container::ContainerEvent& Event );
    void _elementReplaced( const container::ContainerEvent& _rEvent );

public:
    NavigatorTree(std::unique_ptr<weld::TreeView>, OReportController& rController);
    virtual ~NavigatorTree() override;

    DECL_LINK(OnEntrySelDesel, weld::TreeView&, void);
    DECL_LINK(CommandHdl, const CommandEvent&, bool);

    virtual void _selectionChanged( const lang::EventObject& aEvent ) override;

    // ITraverseReport
    virtual void traverseReport(const uno::Reference< report::XReportDefinition>& xReport) override;
    virtual void traverseReportFunctions(const uno::Reference< report::XFunctions>& xFunctions) override;
    virtual void traverseReportHeader(const uno::Reference< report::XSection>& xSection) override;
    virtual void traverseReportFooter(const uno::Reference< report::XSection>& xSection) override;
    virtual void traversePageHeader(const uno::Reference< report::XSection>& xSection) override;
    virtual void traversePageFooter(const uno::Reference< report::XSection>& xSection) override;

    virtual void traverseGroups(const uno::Reference< report::XGroups>& xGroups) override;
    virtual void traverseGroup(const uno::Reference< report::XGroup>& xGroup) override;
    virtual void traverseGroupFunctions(const uno::Reference< report::XFunctions>& xFunctions) override;
    virtual void traverseGroupHeader(const uno::Reference< report::XSection>& xSection) override;
    virtual void traverseGroupFooter(const uno::Reference< report::XSection>& xSection) override;

    virtual void traverseDetail(const uno::Reference< report::XSection>& xSection) override;

    bool find(const uno::Reference<uno::XInterface>& xContent, weld::TreeIter& rIter);
    void removeEntry(const weld::TreeIter& rEntry, bool bRemove = true);

    void grab_focus() { m_xTreeView->grab_focus(); }

    void set_text(const weld::TreeIter& rIter, const OUString& rStr)
    {
        m_xTreeView->set_text(rIter, rStr);
    }

    void expand_row(const weld::TreeIter& rIter)
    {
        m_xTreeView->expand_row(rIter);
    }

    std::unique_ptr<weld::TreeIter> make_iterator() const
    {
        return m_xTreeView->make_iterator();
    }

    int iter_n_children(const weld::TreeIter& rIter) const
    {
        return m_xTreeView->iter_n_children(rIter);
    }
};

NavigatorTree::NavigatorTree(std::unique_ptr<weld::TreeView> xTreeView, OReportController& rController)
    : OPropertyChangeListener(m_aMutex)
    , m_xTreeView(std::move(xTreeView))
    , m_rController(rController)
{
    m_xTreeView->set_size_request(m_xTreeView->get_approximate_digit_width() * 25, m_xTreeView->get_height_rows(18));

    m_pReportListener = new OPropertyChangeMultiplexer(this,m_rController.getReportDefinition());
    m_pReportListener->addProperty(PROPERTY_PAGEHEADERON);
    m_pReportListener->addProperty(PROPERTY_PAGEFOOTERON);
    m_pReportListener->addProperty(PROPERTY_REPORTHEADERON);
    m_pReportListener->addProperty(PROPERTY_REPORTFOOTERON);

    m_pSelectionListener = new OSelectionChangeMultiplexer(this,&m_rController);

    m_xTreeView->set_help_id(HID_REPORT_NAVIGATOR_TREE);

    m_xTreeView->set_selection_mode(SelectionMode::Multiple);

    m_xTreeView->connect_changed(LINK(this, NavigatorTree, OnEntrySelDesel));
    m_xTreeView->connect_popup_menu(LINK(this, NavigatorTree, CommandHdl));
}

NavigatorTree::~NavigatorTree()
{
    m_xTreeView->all_foreach([this](weld::TreeIter& rIter) {
        UserData* pData = weld::fromId<UserData*>(m_xTreeView->get_id(rIter));
        delete pData;
        return false;
    });
    m_pSelectionListener->dispose();
    m_pReportListener->dispose();
}

namespace
{
    sal_uInt16 mapIdent(std::u16string_view rIdent)
    {
        if (rIdent == u"sorting")
            return SID_SORTINGANDGROUPING;
        else if (rIdent == u"page")
            return SID_PAGEHEADERFOOTER;
        else if (rIdent == u"report")
            return SID_REPORTHEADERFOOTER;
        else if (rIdent == u"function")
            return SID_RPT_NEW_FUNCTION;
        else if (rIdent == u"properties")
            return SID_SHOW_PROPERTYBROWSER;
        else if (rIdent == u"delete")
            return SID_DELETE;
        return 0;
    }
}

IMPL_LINK(NavigatorTree, CommandHdl, const CommandEvent&, rEvt, bool)
{
    bool bHandled = false;
    switch( rEvt.GetCommand())
    {
    case CommandEventId::ContextMenu:
        {
            UserData* pData = weld::fromId<UserData*>(m_xTreeView->get_selected_id());
            if (!pData)
                break;

            uno::Reference< report::XFunctionsSupplier> xSupplier(pData->getContent(),uno::UNO_QUERY);
            uno::Reference< report::XFunctions> xFunctions(pData->getContent(),uno::UNO_QUERY);
            uno::Reference< report::XGroup> xGroup(pData->getContent(),uno::UNO_QUERY);
            bool bDeleteAllowed = m_rController.isEditable() && (xGroup.is() ||
                                      uno::Reference< report::XFunction>(pData->getContent(),uno::UNO_QUERY).is());

            std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xTreeView.get(), "modules/dbreport/ui/navigatormenu.ui"));
            std::unique_ptr<weld::Menu> xContextMenu(xBuilder->weld_menu("menu"));

            const OUString aIds[] = { "sorting", "page", "report", "function", "properties", "delete" };
            for (size_t i = 0; i < SAL_N_ELEMENTS(aIds); ++i)
            {
                sal_uInt16 nSId = mapIdent(aIds[i]);

                if (aIds[i] == "page" || aIds[i] == "report" || aIds[i] == "properties")
                    xContextMenu->set_active(aIds[i], m_rController.isCommandChecked(nSId));
                bool bEnabled = m_rController.isCommandEnabled(nSId);
                if (nSId == SID_RPT_NEW_FUNCTION)
                    xContextMenu->set_sensitive(aIds[i], m_rController.isEditable() && (xSupplier.is() || xFunctions.is()));
                // special condition, check for function and group
                else if (nSId == SID_DELETE)
                    xContextMenu->set_sensitive(aIds[i], bDeleteAllowed);
                else
                    xContextMenu->set_sensitive(aIds[i], bEnabled);
            }

            // the point that was clicked on
            ::Point aWhere(rEvt.GetMousePosPixel());
            OUString sCurItemIdent = xContextMenu->popup_at_rect(m_xTreeView.get(), tools::Rectangle(aWhere, Size(1,1)));
            if (!sCurItemIdent.isEmpty())
            {
                sal_uInt16 nId = mapIdent(sCurItemIdent);
                uno::Sequence< beans::PropertyValue> aArgs;
                if ( nId == SID_RPT_NEW_FUNCTION )
                {
                    aArgs.realloc(1);
                    aArgs.getArray()[0].Value <<= (xFunctions.is() ? xFunctions : xSupplier->getFunctions());
                }
                else if ( nId == SID_DELETE )
                {
                    if ( xGroup.is() )
                        nId = SID_GROUP_REMOVE;
                    aArgs = { comphelper::makePropertyValue(PROPERTY_GROUP, pData->getContent()) };
                }
                m_rController.executeUnChecked(nId,aArgs);
            }

            bHandled = true;
        }
        break;
        default: break;
    }

    return bHandled;
}

IMPL_LINK_NOARG(NavigatorTree, OnEntrySelDesel, weld::TreeView&, void)
{
    if ( !m_pSelectionListener->locked() )
    {
        m_pSelectionListener->lock();
        std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
        bool bEntry = m_xTreeView->get_cursor(xEntry.get());
        uno::Any aSelection;
        if (bEntry && m_xTreeView->is_selected(*xEntry))
            aSelection <<= weld::fromId<UserData*>(m_xTreeView->get_id(*xEntry))->getContent();
        m_rController.select(aSelection);
        m_pSelectionListener->unlock();
    }
}

void NavigatorTree::_selectionChanged( const lang::EventObject& aEvent )
{
    m_pSelectionListener->lock();
    uno::Reference< view::XSelectionSupplier> xSelectionSupplier(aEvent.Source,uno::UNO_QUERY);
    uno::Any aSec = xSelectionSupplier->getSelection();
    uno::Sequence< uno::Reference< report::XReportComponent > > aSelection;
    aSec >>= aSelection;
    std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
    if ( !aSelection.hasElements() )
    {
        uno::Reference< uno::XInterface> xSelection(aSec,uno::UNO_QUERY);
        bool bEntry = find(xSelection, *xEntry);
        if (bEntry && !m_xTreeView->is_selected(*xEntry))
        {
            m_xTreeView->select(*xEntry);
            m_xTreeView->set_cursor(*xEntry);
        }
        else if (!bEntry)
            m_xTreeView->unselect_all();
    }
    else
    {
        for (const uno::Reference<report::XReportComponent>& rElem : std::as_const(aSelection))
        {
            bool bEntry = find(rElem, *xEntry);
            if (bEntry && !m_xTreeView->is_selected(*xEntry))
            {
                m_xTreeView->select(*xEntry);
                m_xTreeView->set_cursor(*xEntry);
            }
        }
    }
    m_pSelectionListener->unlock();
}

void NavigatorTree::insertEntry(const OUString& rName, const weld::TreeIter* pParent, const OUString& rImageId,
                                int nPosition, const UserData* pData, weld::TreeIter& rRet)
{
    OUString sId = pData ? weld::toId(pData) : OUString();
    m_xTreeView->insert(pParent, nPosition, &rName, &sId, nullptr, nullptr, false, &rRet);
    if (!rImageId.isEmpty())
        m_xTreeView->set_image(rRet, rImageId);
}

void NavigatorTree::traverseSection(const uno::Reference<report::XSection>& xSection, const weld::TreeIter* pParent, const OUString& rImageId, int nPosition)
{
    std::unique_ptr<weld::TreeIter> xSectionIter = m_xTreeView->make_iterator();
    std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
    insertEntry(xSection->getName(), pParent, rImageId, nPosition, new UserData(this, xSection), *xSectionIter);
    const sal_Int32 nCount = xSection->getCount();
    for (sal_Int32 i = 0; i < nCount; ++i)
    {
        uno::Reference< report::XReportComponent> xElement(xSection->getByIndex(i), uno::UNO_QUERY_THROW);
        insertEntry(lcl_getName(xElement), xSectionIter.get(), lcl_getImageId(xElement), -1, new UserData(this, xElement), *xScratch);
        uno::Reference< report::XReportDefinition> xSubReport(xElement,uno::UNO_QUERY);
        if ( xSubReport.is() )
        {
            bool bMasterReport = find(xSection->getReportDefinition(), *xScratch);
            if (!bMasterReport)
                m_xMasterReport.reset();
            else
                m_xMasterReport = m_xTreeView->make_iterator(xScratch.get());
            reportdesign::OReportVisitor aSubVisitor(this);
            aSubVisitor.start(xSubReport);
        }
    }
}

void NavigatorTree::traverseFunctions(const uno::Reference< report::XFunctions>& xFunctions, const weld::TreeIter* pParent)
{
    std::unique_ptr<weld::TreeIter> xFunctionIter = m_xTreeView->make_iterator();
    std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
    insertEntry(RptResId(RID_STR_FUNCTIONS), pParent, RID_SVXBMP_RPT_NEW_FUNCTION, -1, new UserData(this, xFunctions), *xFunctionIter);
    const sal_Int32 nCount = xFunctions->getCount();
    for (sal_Int32 i = 0; i< nCount; ++i)
    {
        uno::Reference< report::XFunction> xElement(xFunctions->getByIndex(i),uno::UNO_QUERY);
        insertEntry(xElement->getName(), xFunctionIter.get(), RID_SVXBMP_RPT_NEW_FUNCTION, -1, new UserData(this,xElement), *xScratch);
    }
}

bool NavigatorTree::find(const uno::Reference<uno::XInterface>& xContent, weld::TreeIter& rRet)
{
    bool bRet = false;
    if (xContent.is())
    {
        m_xTreeView->all_foreach([this, &xContent, &bRet, &rRet](weld::TreeIter& rIter) {
            UserData* pData = weld::fromId<UserData*>(m_xTreeView->get_id(rIter));
            if (pData->getContent() == xContent)
            {
                m_xTreeView->copy_iterator(rIter, rRet);
                bRet = true;
                return true;
            }
            return false;
        });
    }
    return bRet;
}

// ITraverseReport

void NavigatorTree::traverseReport(const uno::Reference< report::XReportDefinition>& xReport)
{
    std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
    insertEntry(xReport->getName(), m_xMasterReport.get(), RID_SVXBMP_SELECT_REPORT,-1, new UserData(this, xReport), *xScratch);
}

void NavigatorTree::traverseReportFunctions(const uno::Reference< report::XFunctions>& xFunctions)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xFunctions->getParent(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseFunctions(xFunctions, xReport.get());
}

void NavigatorTree::traverseReportHeader(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getReportDefinition(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_REPORTHEADERFOOTER);
}

void NavigatorTree::traverseReportFooter(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getReportDefinition(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_REPORTHEADERFOOTER);
}

void NavigatorTree::traversePageHeader(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getReportDefinition(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_PAGEHEADERFOOTER);
}

void NavigatorTree::traversePageFooter(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getReportDefinition(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_PAGEHEADERFOOTER);
}

void NavigatorTree::traverseGroups(const uno::Reference< report::XGroups>& xGroups)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xGroups->getReportDefinition(), *xReport);
    if (!bReport)
        xReport.reset();
    std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
    insertEntry(RptResId(RID_STR_GROUPS), xReport.get(), RID_SVXBMP_SORTINGANDGROUPING, -1, new UserData(this, xGroups), *xScratch);
}

void NavigatorTree::traverseGroup(const uno::Reference< report::XGroup>& xGroup)
{
    uno::Reference< report::XGroups> xGroups(xGroup->getParent(),uno::UNO_QUERY);
    std::unique_ptr<weld::TreeIter> xGroupsIter = m_xTreeView->make_iterator();
    bool bGroups = find(xGroups, *xGroupsIter);
    OSL_ENSURE(bGroups, "No Groups inserted so far. Why!");
    if (!bGroups)
        xGroupsIter.reset();
    std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
    insertEntry(xGroup->getExpression(), xGroupsIter.get(), RID_SVXBMP_GROUP, rptui::getPositionInIndexAccess(xGroups,xGroup), new UserData(this,xGroup), *xScratch);
}

void NavigatorTree::traverseGroupFunctions(const uno::Reference< report::XFunctions>& xFunctions)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xFunctions->getParent(), *xReport);
    if (!bReport)
        xReport.reset();
    traverseFunctions(xFunctions, xReport.get());
}

void NavigatorTree::traverseGroupHeader(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getGroup(), *xReport);
    OSL_ENSURE(bReport, "No group found");
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_GROUPHEADER, 1);
}

void NavigatorTree::traverseGroupFooter(const uno::Reference< report::XSection>& xSection)
{
    std::unique_ptr<weld::TreeIter> xReport = m_xTreeView->make_iterator();
    bool bReport = find(xSection->getGroup(), *xReport);
    OSL_ENSURE(bReport, "No group found");
    if (!bReport)
        xReport.reset();
    traverseSection(xSection, xReport.get(), RID_SVXBMP_GROUPFOOTER);
}

void NavigatorTree::traverseDetail(const uno::Reference< report::XSection>& xSection)
{
    uno::Reference< report::XReportDefinition> xReport = xSection->getReportDefinition();
    std::unique_ptr<weld::TreeIter> xParent = m_xTreeView->make_iterator();
    bool bParent = find(xReport, *xParent);
    if (!bParent)
        xParent.reset();
    traverseSection(xSection, xParent.get(), RID_SVXBMP_ICON_DETAIL);
}

void NavigatorTree::_propertyChanged(const beans::PropertyChangeEvent& _rEvent)
{
    uno::Reference< report::XReportDefinition> xReport(_rEvent.Source,uno::UNO_QUERY);
    if ( !xReport.is() )
        return;

    bool bEnabled = false;
    _rEvent.NewValue >>= bEnabled;
    if ( !bEnabled )
        return;

    std::unique_ptr<weld::TreeIter> xParent = m_xTreeView->make_iterator();
    bool bParent = find(xReport, *xParent);
    if (!bParent)
        xParent.reset();
    if ( _rEvent.PropertyName == PROPERTY_REPORTHEADERON )
    {
        int nPos = xReport->getReportHeaderOn() ? 2 : 1;
        traverseSection(xReport->getReportHeader(),xParent.get(),RID_SVXBMP_REPORTHEADERFOOTER,nPos);
    }
    else if ( _rEvent.PropertyName == PROPERTY_PAGEHEADERON )
    {
        traverseSection(xReport->getPageHeader(),xParent.get(), RID_SVXBMP_PAGEHEADERFOOTER,1);
    }
    else if ( _rEvent.PropertyName == PROPERTY_PAGEFOOTERON )
        traverseSection(xReport->getPageFooter(),xParent.get(), RID_SVXBMP_PAGEHEADERFOOTER);
    else if ( _rEvent.PropertyName == PROPERTY_REPORTFOOTERON )
    {
        int nPos = -1;
        if (xReport->getPageFooterOn() && xParent)
            nPos = m_xTreeView->iter_n_children(*xParent) - 1;
        traverseSection(xReport->getReportFooter(),xParent.get(),RID_SVXBMP_REPORTHEADERFOOTER,nPos);
    }
}

void NavigatorTree::_elementInserted( const container::ContainerEvent& _rEvent )
{
    std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
    bool bEntry = find(_rEvent.Source, *xEntry);
    if (!bEntry)
        xEntry.reset();
    uno::Reference<beans::XPropertySet> xProp(_rEvent.Element,uno::UNO_QUERY_THROW);
    OUString sName;
    uno::Reference< beans::XPropertySetInfo> xInfo = xProp->getPropertySetInfo();
    if ( xInfo.is() )
    {
        if ( xInfo->hasPropertyByName(PROPERTY_NAME) )
            xProp->getPropertyValue(PROPERTY_NAME) >>= sName;
        else if ( xInfo->hasPropertyByName(PROPERTY_EXPRESSION) )
            xProp->getPropertyValue(PROPERTY_EXPRESSION) >>= sName;
    }
    uno::Reference< report::XGroup> xGroup(xProp,uno::UNO_QUERY);
    if ( xGroup.is() )
    {
        reportdesign::OReportVisitor aSubVisitor(this);
        aSubVisitor.start(xGroup);
    }
    else
    {
        uno::Reference< report::XReportComponent> xElement(xProp,uno::UNO_QUERY);
        if ( xProp.is() )
            sName = lcl_getName(xProp);
        std::unique_ptr<weld::TreeIter> xScratch = m_xTreeView->make_iterator();
        insertEntry(sName, xEntry.get(), (!xElement.is() ? RID_SVXBMP_RPT_NEW_FUNCTION : lcl_getImageId(xElement)),
                    -1, new UserData(this,xProp), *xScratch);
    }
    if (bEntry && !m_xTreeView->get_row_expanded(*xEntry))
        m_xTreeView->expand_row(*xEntry);
}

void NavigatorTree::_elementRemoved( const container::ContainerEvent& _rEvent )
{
    uno::Reference<beans::XPropertySet> xProp(_rEvent.Element,uno::UNO_QUERY);
    std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
    bool bEntry = find(xProp, *xEntry);
    OSL_ENSURE(bEntry,"NavigatorTree::_elementRemoved: No Entry found!");

    if (bEntry)
    {
        removeEntry(*xEntry);
    }
}

void NavigatorTree::_elementReplaced( const container::ContainerEvent& _rEvent )
{
    uno::Reference<beans::XPropertySet> xProp(_rEvent.ReplacedElement,uno::UNO_QUERY);
    std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
    bool bEntry = find(xProp, *xEntry);
    if (bEntry)
    {
        UserData* pData = weld::fromId<UserData*>(m_xTreeView->get_id(*xEntry));
        xProp.set(_rEvent.Element,uno::UNO_QUERY);
        pData->setContent(xProp);
        OUString sName;
        xProp->getPropertyValue(PROPERTY_NAME) >>= sName;
        m_xTreeView->set_text(*xEntry, sName);
    }
}

void NavigatorTree::_disposing(const lang::EventObject& _rSource)
{
    std::unique_ptr<weld::TreeIter> xEntry = m_xTreeView->make_iterator();
    if (find(_rSource.Source, *xEntry))
        removeEntry(*xEntry);
}

void NavigatorTree::removeEntry(const weld::TreeIter& rEntry, bool bRemove)
{
    std::unique_ptr<weld::TreeIter> xChild = m_xTreeView->make_iterator(&rEntry);
    bool bChild = m_xTreeView->iter_children(*xChild);
    while (bChild)
    {
        removeEntry(*xChild, false);
        bChild = m_xTreeView->iter_next_sibling(*xChild);
    }
    delete weld::fromId<UserData*>(m_xTreeView->get_id(rEntry));
    if (bRemove)
        m_xTreeView->remove(rEntry);
}

NavigatorTree::UserData::UserData(NavigatorTree* pTree,uno::Reference<uno::XInterface> xContent)
    : OPropertyChangeListener(m_aMutex)
    , OContainerListener(m_aMutex)
    , m_xContent(std::move(xContent))
    , m_pTree(pTree)
{
    uno::Reference<beans::XPropertySet> xProp(m_xContent,uno::UNO_QUERY);
    if ( xProp.is() )
    {
        uno::Reference< beans::XPropertySetInfo> xInfo = xProp->getPropertySetInfo();
        if ( xInfo.is() )
        {
            m_pListener = new ::comphelper::OPropertyChangeMultiplexer(this,xProp);
            if ( xInfo->hasPropertyByName(PROPERTY_NAME) )
                m_pListener->addProperty(PROPERTY_NAME);
            else if ( xInfo->hasPropertyByName(PROPERTY_EXPRESSION) )
                m_pListener->addProperty(PROPERTY_EXPRESSION);
            if ( xInfo->hasPropertyByName(PROPERTY_DATAFIELD) )
                m_pListener->addProperty(PROPERTY_DATAFIELD);
            if ( xInfo->hasPropertyByName(PROPERTY_LABEL) )
                m_pListener->addProperty(PROPERTY_LABEL);
            if ( xInfo->hasPropertyByName(PROPERTY_HEADERON) )
                m_pListener->addProperty(PROPERTY_HEADERON);
            if ( xInfo->hasPropertyByName(PROPERTY_FOOTERON) )
                m_pListener->addProperty(PROPERTY_FOOTERON);
        }
    }
    uno::Reference< container::XContainer> xContainer(m_xContent,uno::UNO_QUERY);
    if ( xContainer.is() )
    {
        m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
    }
}

NavigatorTree::UserData::~UserData()
{
    if ( m_pContainerListener.is() )
        m_pContainerListener->dispose();
    if ( m_pListener.is() )
        m_pListener->dispose();
}

// OPropertyChangeListener
void NavigatorTree::UserData::_propertyChanged(const beans::PropertyChangeEvent& _rEvent)
{
    std::unique_ptr<weld::TreeIter> xEntry = m_pTree->make_iterator();
    bool bEntry = m_pTree->find(_rEvent.Source, *xEntry);
    OSL_ENSURE(bEntry,"No entry could be found! Why not!");
    if (!bEntry)
        return;
    const bool bFooterOn = (PROPERTY_FOOTERON == _rEvent.PropertyName);
    try
    {
        if ( bFooterOn || PROPERTY_HEADERON == _rEvent.PropertyName )
        {
            sal_Int32 nPos = 1;
            uno::Reference< report::XGroup> xGroup(_rEvent.Source,uno::UNO_QUERY);
            ::std::function<bool(OGroupHelper *)> pIsOn = ::std::mem_fn(&OGroupHelper::getHeaderOn);
            ::std::function<uno::Reference<report::XSection>(OGroupHelper *)> pMemFunSection = ::std::mem_fn(&OGroupHelper::getHeader);
            if ( bFooterOn )
            {
                pIsOn = ::std::mem_fn(&OGroupHelper::getFooterOn);
                pMemFunSection = ::std::mem_fn(&OGroupHelper::getFooter);
                nPos = m_pTree->iter_n_children(*xEntry) - 1;
            }

            OGroupHelper aGroupHelper(xGroup);
            if ( pIsOn(&aGroupHelper) )
            {
                if ( bFooterOn )
                    ++nPos;
                m_pTree->traverseSection(pMemFunSection(&aGroupHelper),xEntry.get(),bFooterOn ? RID_SVXBMP_GROUPFOOTER : RID_SVXBMP_GROUPHEADER,nPos);
            }
        }
        else if ( PROPERTY_EXPRESSION == _rEvent.PropertyName)
        {
            OUString sNewName;
            _rEvent.NewValue >>= sNewName;
            m_pTree->set_text(*xEntry, sNewName);
        }
        else if ( PROPERTY_DATAFIELD == _rEvent.PropertyName || PROPERTY_LABEL == _rEvent.PropertyName || PROPERTY_NAME == _rEvent.PropertyName )
        {
            uno::Reference<beans::XPropertySet> xProp(_rEvent.Source,uno::UNO_QUERY);
            m_pTree->set_text(*xEntry, lcl_getName(xProp));
        }
    }
    catch(const uno::Exception &)
    {}
}

void NavigatorTree::UserData::_elementInserted( const container::ContainerEvent& _rEvent )
{
    m_pTree->_elementInserted( _rEvent );
}

void NavigatorTree::UserData::_elementRemoved( const container::ContainerEvent& _rEvent )
{
    m_pTree->_elementRemoved( _rEvent );
}

void NavigatorTree::UserData::_elementReplaced( const container::ContainerEvent& _rEvent )
{
    m_pTree->_elementReplaced( _rEvent );
}

void NavigatorTree::UserData::_disposing(const lang::EventObject& _rSource)
{
    m_pTree->_disposing( _rSource );
}

ONavigator::ONavigator(weld::Window* pParent, OReportController& rController)
    : GenericDialogController(pParent, "modules/dbreport/ui/floatingnavigator.ui", "FloatingNavigator")
    , m_xReport(rController.getReportDefinition())
    , m_xNavigatorTree(std::make_unique<NavigatorTree>(m_xBuilder->weld_tree_view("treeview"), rController))
{
    reportdesign::OReportVisitor aVisitor(m_xNavigatorTree.get());
    aVisitor.start(m_xReport);
    std::unique_ptr<weld::TreeIter> xScratch = m_xNavigatorTree->make_iterator();
    if (m_xNavigatorTree->find(m_xReport, *xScratch))
        m_xNavigatorTree->expand_row(*xScratch);
    lang::EventObject aEvent(rController);
    m_xNavigatorTree->_selectionChanged(aEvent);
    m_xNavigatorTree->grab_focus();

    m_xDialog->connect_container_focus_changed(LINK(this, ONavigator, FocusChangeHdl));
}

ONavigator::~ONavigator()
{
}

IMPL_LINK_NOARG(ONavigator, FocusChangeHdl, weld::Container&, void)
{
    if (m_xDialog->has_toplevel_focus())
        m_xNavigatorTree->grab_focus();
}

} // rptui

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