LibreOffice Module sfx2 (master) 1
DocumentModelTreeHandler.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11#include <memory>
12
14
15#include <sfx2/sfxresid.hxx>
16#include "DevToolsStrings.hrc"
17
18#include <com/sun/star/lang/XServiceInfo.hpp>
19#include <com/sun/star/container/XNamed.hpp>
20#include <com/sun/star/container/XEnumerationAccess.hpp>
21
22#include <com/sun/star/drawing/XDrawPage.hpp>
23#include <com/sun/star/drawing/XDrawPages.hpp>
24#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
25#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
26#include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
27#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
28#include <com/sun/star/sheet/XSpreadsheet.hpp>
29#include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
30#include <com/sun/star/sheet/XDataPilotTables.hpp>
31#include <com/sun/star/table/XTableChartsSupplier.hpp>
32#include <com/sun/star/table/XTableCharts.hpp>
33#include <com/sun/star/text/XTextDocument.hpp>
34#include <com/sun/star/text/XTextTablesSupplier.hpp>
35#include <com/sun/star/text/XTextFramesSupplier.hpp>
36#include <com/sun/star/text/XTextGraphicObjectsSupplier.hpp>
37#include <com/sun/star/text/XTextEmbeddedObjectsSupplier.hpp>
38#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
39#include <utility>
40
41using namespace css;
42
43namespace
44{
45// returns a name of the object, if available
46OUString lclGetNamed(uno::Reference<uno::XInterface> const& xObject)
47{
48 uno::Reference<container::XNamed> xNamed(xObject, uno::UNO_QUERY);
49 if (!xNamed.is())
50 return OUString();
51 return xNamed->getName();
52}
53
60class DocumentModelTreeEntry
61{
62protected:
63 OUString maString;
64 css::uno::Reference<css::uno::XInterface> mxObject;
65
66public:
67 DocumentModelTreeEntry(OUString aString, css::uno::Reference<css::uno::XInterface> xObject)
68 : maString(std::move(aString))
69 , mxObject(std::move(xObject))
70 {
71 }
72
73 virtual ~DocumentModelTreeEntry() {}
74
76 OUString& getString() { return maString; }
77
79 virtual bool shouldShowExpander() { return false; }
80
82 virtual css::uno::Reference<css::uno::XInterface> getMainObject() { return mxObject; }
83
85 virtual void fill(std::unique_ptr<weld::TreeView>& /*pDocumentModelTree*/,
86 weld::TreeIter const& /*rParent*/)
87 {
88 }
89};
90
91// append an entry to a input TreeView to a parent
92void lclAppendToParentEntry(const std::unique_ptr<weld::TreeView>& rTree,
93 weld::TreeIter const& rParent, DocumentModelTreeEntry* pEntry)
94{
95 OUString sId(weld::toId(pEntry));
96 OUString const& rString = pEntry->getString();
97 rTree->insert(&rParent, -1, &rString, &sId, nullptr, nullptr, pEntry->shouldShowExpander(),
98 nullptr);
99}
100
101// append a root entry to a input TreeView
102OUString lclAppend(const std::unique_ptr<weld::TreeView>& rTree, DocumentModelTreeEntry* pEntry)
103{
104 OUString sId(weld::toId(pEntry));
105 OUString const& rString = pEntry->getString();
106 rTree->insert(nullptr, -1, &rString, &sId, nullptr, nullptr, pEntry->shouldShowExpander(),
107 nullptr);
108 return sId;
109}
110
112class NameAccessTreeEntry : public DocumentModelTreeEntry
113{
114protected:
115 NameAccessTreeEntry(OUString const& rString, uno::Reference<uno::XInterface> const& xObject)
116 : DocumentModelTreeEntry(rString, xObject)
117 {
118 }
119
120 bool shouldShowExpander() override
121 {
122 uno::Reference<container::XNameAccess> xNameAccess(getMainObject(), uno::UNO_QUERY);
123 return xNameAccess.is() && xNameAccess->getElementNames().getLength() > 0;
124 }
125
127 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
128 weld::TreeIter const& rParent) override
129 {
130 uno::Reference<container::XNameAccess> xNameAccess(getMainObject(), uno::UNO_QUERY);
131 xNameAccess.set(getMainObject(), uno::UNO_QUERY);
132 if (!xNameAccess.is())
133 return;
134
135 const uno::Sequence<OUString> aNames = xNameAccess->getElementNames();
136 for (auto const& rName : aNames)
137 {
138 uno::Reference<uno::XInterface> xObject(xNameAccess->getByName(rName), uno::UNO_QUERY);
139 auto pEntry = std::make_unique<DocumentModelTreeEntry>(rName, xObject);
140 lclAppendToParentEntry(pDocumentModelTree, rParent, pEntry.release());
141 }
142 }
143};
144
146class DocumentRootEntry : public DocumentModelTreeEntry
147{
148public:
149 DocumentRootEntry(OUString const& rString, uno::Reference<uno::XInterface> const& xObject)
150 : DocumentModelTreeEntry(rString, xObject)
151 {
152 }
153
154 bool shouldShowExpander() override { return false; }
155};
156
158class ParagraphEntry : public DocumentModelTreeEntry
159{
160public:
161 ParagraphEntry(OUString const& rString,
162 css::uno::Reference<css::uno::XInterface> const& xObject)
163 : DocumentModelTreeEntry(rString, xObject)
164 {
165 }
166
167 bool shouldShowExpander() override
168 {
169 uno::Reference<container::XEnumerationAccess> xEnumAccess(getMainObject(), uno::UNO_QUERY);
170 if (!xEnumAccess.is())
171 return false;
172 auto xTextPortions = xEnumAccess->createEnumeration();
173 if (!xTextPortions.is())
174 return false;
175 return xTextPortions->hasMoreElements();
176 }
177
178 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
179 weld::TreeIter const& rParent) override
180 {
181 uno::Reference<container::XEnumerationAccess> xEnumAccess(getMainObject(), uno::UNO_QUERY);
182 if (!xEnumAccess.is())
183 return;
184
185 uno::Reference<container::XEnumeration> xTextPortions = xEnumAccess->createEnumeration();
186 if (!xTextPortions.is())
187 return;
188
189 for (sal_Int32 i = 0; xTextPortions->hasMoreElements(); i++)
190 {
191 uno::Reference<text::XTextRange> const xTextPortion(xTextPortions->nextElement(),
192 uno::UNO_QUERY);
193 OUString aString = lclGetNamed(xTextPortion);
194 if (aString.isEmpty())
195 {
196 OUString aNumber = OUString::number(i + 1);
197 aString = SfxResId(STR_TEXT_PORTION).replaceFirst("%1", aNumber);
198 }
199
200 auto pEntry = std::make_unique<DocumentModelTreeEntry>(aString, xTextPortion);
201 lclAppendToParentEntry(pDocumentModelTree, rParent, pEntry.release());
202 }
203 }
204};
205
207class ParagraphsEntry : public DocumentModelTreeEntry
208{
209public:
210 ParagraphsEntry(OUString const& rString,
211 css::uno::Reference<css::uno::XInterface> const& xObject)
212 : DocumentModelTreeEntry(rString, xObject)
213 {
214 }
215
216 css::uno::Reference<css::uno::XInterface> getMainObject() override
217 {
218 uno::Reference<text::XTextDocument> xDocument(mxObject, uno::UNO_QUERY);
219 if (!xDocument.is())
220 return mxObject;
221
222 return xDocument->getText()->getText();
223 }
224
225 bool shouldShowExpander() override
226 {
227 uno::Reference<container::XEnumerationAccess> xEnumAccess(getMainObject(), uno::UNO_QUERY);
228 if (!xEnumAccess.is())
229 return false;
230 auto xParagraphEnum = xEnumAccess->createEnumeration();
231 if (!xParagraphEnum.is())
232 return false;
233 return xParagraphEnum->hasMoreElements();
234 }
235
236 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
237 weld::TreeIter const& rParent) override
238 {
239 uno::Reference<container::XEnumerationAccess> xEnumAccess(getMainObject(), uno::UNO_QUERY);
240 if (!xEnumAccess.is())
241 return;
242
243 uno::Reference<container::XEnumeration> xParagraphEnum = xEnumAccess->createEnumeration();
244
245 if (!xParagraphEnum.is())
246 return;
247
248 for (sal_Int32 i = 0; xParagraphEnum->hasMoreElements(); i++)
249 {
250 uno::Reference<text::XTextContent> const xParagraph(xParagraphEnum->nextElement(),
251 uno::UNO_QUERY);
252 OUString aString = lclGetNamed(xParagraph);
253 if (aString.isEmpty())
254 {
255 aString = SfxResId(STR_PARAGRAPH).replaceFirst("%1", OUString::number(i + 1));
256 }
257
258 auto pEntry = std::make_unique<ParagraphEntry>(aString, xParagraph);
259 lclAppendToParentEntry(pDocumentModelTree, rParent, pEntry.release());
260 }
261 }
262};
263
265class ShapesEntry : public DocumentModelTreeEntry
266{
267public:
268 ShapesEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
269 : DocumentModelTreeEntry(rString, xObject)
270 {
271 }
272
273 css::uno::Reference<css::uno::XInterface> getMainObject() override
274 {
275 uno::Reference<drawing::XDrawPageSupplier> xSupplier(mxObject, uno::UNO_QUERY);
276 if (!xSupplier.is())
277 return mxObject;
278 return xSupplier->getDrawPage();
279 }
280
281 bool shouldShowExpander() override
282 {
283 uno::Reference<container::XIndexAccess> xShapes(getMainObject(), uno::UNO_QUERY);
284 return xShapes.is() && xShapes->getCount() > 0;
285 }
286
287 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
288 weld::TreeIter const& rParent) override
289 {
290 uno::Reference<container::XIndexAccess> xShapes(getMainObject(), uno::UNO_QUERY);
291 if (!xShapes.is())
292 return;
293 for (sal_Int32 nIndexShapes = 0; nIndexShapes < xShapes->getCount(); ++nIndexShapes)
294 {
295 uno::Reference<uno::XInterface> xShape(xShapes->getByIndex(nIndexShapes),
296 uno::UNO_QUERY);
297 OUString aShapeName = lclGetNamed(xShape);
298 if (aShapeName.isEmpty())
299 {
300 aShapeName
301 = SfxResId(STR_SHAPE).replaceFirst("%1", OUString::number(nIndexShapes + 1));
302 }
303
304 auto pEntry = std::make_unique<DocumentModelTreeEntry>(aShapeName, xShape);
305 lclAppendToParentEntry(pDocumentModelTree, rParent, pEntry.release());
306 }
307 }
308};
309
311class TablesEntry : public NameAccessTreeEntry
312{
313public:
314 TablesEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
315 : NameAccessTreeEntry(rString, xObject)
316 {
317 }
318
319 css::uno::Reference<css::uno::XInterface> getMainObject() override
320 {
321 uno::Reference<text::XTextTablesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
322 if (!xSupplier.is())
323 return mxObject;
324 return xSupplier->getTextTables();
325 }
326};
327
329class FramesEntry : public NameAccessTreeEntry
330{
331public:
332 FramesEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
333 : NameAccessTreeEntry(rString, xObject)
334 {
335 }
336
337 css::uno::Reference<css::uno::XInterface> getMainObject() override
338 {
339 uno::Reference<text::XTextFramesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
340 if (!xSupplier.is())
341 return mxObject;
342 return xSupplier->getTextFrames();
343 }
344};
345
347class WriterGraphicObjectsEntry : public NameAccessTreeEntry
348{
349public:
350 WriterGraphicObjectsEntry(OUString const& rString,
351 css::uno::Reference<css::uno::XInterface> const& xObject)
352 : NameAccessTreeEntry(rString, xObject)
353 {
354 }
355
356 css::uno::Reference<css::uno::XInterface> getMainObject() override
357 {
358 uno::Reference<text::XTextGraphicObjectsSupplier> xSupplier(mxObject, uno::UNO_QUERY);
359 if (!xSupplier.is())
360 return mxObject;
361 return xSupplier->getGraphicObjects();
362 }
363};
364
366class EmbeddedObjectsEntry : public NameAccessTreeEntry
367{
368public:
369 EmbeddedObjectsEntry(OUString const& rString,
370 css::uno::Reference<css::uno::XInterface> const& xObject)
371 : NameAccessTreeEntry(rString, xObject)
372 {
373 }
374
375 css::uno::Reference<css::uno::XInterface> getMainObject() override
376 {
377 uno::Reference<text::XTextEmbeddedObjectsSupplier> xSupplier(mxObject, uno::UNO_QUERY);
378 if (!xSupplier.is())
379 return mxObject;
380 return xSupplier->getEmbeddedObjects();
381 }
382};
383
385class StylesFamilyEntry : public NameAccessTreeEntry
386{
387public:
388 StylesFamilyEntry(OUString const& rString,
389 css::uno::Reference<css::uno::XInterface> const& xObject)
390 : NameAccessTreeEntry(rString, xObject)
391 {
392 }
393};
394
396class StylesFamiliesEntry : public DocumentModelTreeEntry
397{
398public:
399 StylesFamiliesEntry(OUString const& rString,
400 css::uno::Reference<css::uno::XInterface> const& xObject)
401 : DocumentModelTreeEntry(rString, xObject)
402 {
403 }
404
405 css::uno::Reference<css::uno::XInterface> getMainObject() override
406 {
407 uno::Reference<style::XStyleFamiliesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
408 if (!xSupplier.is())
409 return mxObject;
410 return xSupplier->getStyleFamilies();
411 }
412
413 bool shouldShowExpander() override
414 {
415 uno::Reference<container::XNameAccess> xStyleFamilies(getMainObject(), uno::UNO_QUERY);
416 return xStyleFamilies.is() && xStyleFamilies->getElementNames().getLength() > 0;
417 }
418
419 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
420 weld::TreeIter const& rParent) override
421 {
422 uno::Reference<container::XNameAccess> xStyleFamilies(getMainObject(), uno::UNO_QUERY);
423 if (!xStyleFamilies.is())
424 return;
425
426 const uno::Sequence<OUString> aNames = xStyleFamilies->getElementNames();
427 for (auto const& rFamilyName : aNames)
428 {
429 uno::Reference<uno::XInterface> xStyleFamily(xStyleFamilies->getByName(rFamilyName),
430 uno::UNO_QUERY);
431
432 auto pStylesFamilyEntry
433 = std::make_unique<StylesFamilyEntry>(rFamilyName, xStyleFamily);
434 lclAppendToParentEntry(pDocumentModelTree, rParent, pStylesFamilyEntry.release());
435 }
436 }
437};
438
440class PagesEntry : public DocumentModelTreeEntry
441{
442public:
443 PagesEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
444 : DocumentModelTreeEntry(rString, xObject)
445 {
446 }
447
448 css::uno::Reference<css::uno::XInterface> getMainObject() override
449 {
450 uno::Reference<drawing::XDrawPagesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
451 if (!xSupplier.is())
452 return mxObject;
453 return xSupplier->getDrawPages();
454 }
455
456 bool shouldShowExpander() override
457 {
458 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
459 return xDrawPages.is() && xDrawPages->getCount() > 0;
460 }
461
462 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
463 weld::TreeIter const& rParent) override
464 {
465 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
466 for (sal_Int32 i = 0; i < xDrawPages->getCount(); ++i)
467 {
468 uno::Reference<drawing::XDrawPage> xPage(xDrawPages->getByIndex(i), uno::UNO_QUERY);
469 if (!xPage.is())
470 continue;
471
472 OUString aPageString = lclGetNamed(xPage);
473 if (aPageString.isEmpty())
474 aPageString = SfxResId(STR_PAGE).replaceFirst("%1", OUString::number(i + 1));
475
476 auto pShapesEntry = std::make_unique<ShapesEntry>(aPageString, xPage);
477 lclAppendToParentEntry(pDocumentModelTree, rParent, pShapesEntry.release());
478 }
479 }
480};
481
483class SlidesEntry : public DocumentModelTreeEntry
484{
485public:
486 SlidesEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
487 : DocumentModelTreeEntry(rString, xObject)
488 {
489 }
490
491 css::uno::Reference<css::uno::XInterface> getMainObject() override
492 {
493 uno::Reference<drawing::XDrawPagesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
494 if (!xSupplier.is())
495 return mxObject;
496 return xSupplier->getDrawPages();
497 }
498
499 bool shouldShowExpander() override
500 {
501 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
502 return xDrawPages.is() && xDrawPages->getCount() > 0;
503 }
504
505 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
506 weld::TreeIter const& rParent) override
507 {
508 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
509 for (sal_Int32 i = 0; i < xDrawPages->getCount(); ++i)
510 {
511 uno::Reference<drawing::XDrawPage> xPage(xDrawPages->getByIndex(i), uno::UNO_QUERY);
512 if (!xPage.is())
513 continue;
514
515 OUString aPageString = lclGetNamed(xPage);
516 if (aPageString.isEmpty())
517 aPageString = SfxResId(STR_SLIDE).replaceFirst("%1", OUString::number(i + 1));
518
519 auto pShapesEntry = std::make_unique<ShapesEntry>(aPageString, xPage);
520 lclAppendToParentEntry(pDocumentModelTree, rParent, pShapesEntry.release());
521 }
522 }
523};
524
526class MasterSlidesEntry : public DocumentModelTreeEntry
527{
528public:
529 MasterSlidesEntry(OUString const& rString,
530 css::uno::Reference<css::uno::XInterface> const& xObject)
531 : DocumentModelTreeEntry(rString, xObject)
532 {
533 }
534
535 css::uno::Reference<css::uno::XInterface> getMainObject() override
536 {
537 uno::Reference<drawing::XMasterPagesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
538 if (!xSupplier.is())
539 return mxObject;
540 return xSupplier->getMasterPages();
541 }
542
543 bool shouldShowExpander() override
544 {
545 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
546 return xDrawPages.is() && xDrawPages->getCount() > 0;
547 }
548
549 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
550 weld::TreeIter const& rParent) override
551 {
552 uno::Reference<drawing::XDrawPages> xDrawPages(getMainObject(), uno::UNO_QUERY);
553 for (sal_Int32 i = 0; i < xDrawPages->getCount(); ++i)
554 {
555 uno::Reference<drawing::XDrawPage> xPage(xDrawPages->getByIndex(i), uno::UNO_QUERY);
556 if (!xPage.is())
557 continue;
558
559 OUString aPageString = lclGetNamed(xPage);
560 if (aPageString.isEmpty())
561 {
562 aPageString
563 = SfxResId(STR_MASTER_SLIDE).replaceFirst("%1", OUString::number(i + 1));
564 }
565
566 auto pShapesEntry = std::make_unique<ShapesEntry>(aPageString, xPage);
567 lclAppendToParentEntry(pDocumentModelTree, rParent, pShapesEntry.release());
568 }
569 }
570};
571
573class ChartsEntry : public NameAccessTreeEntry
574{
575public:
576 ChartsEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
577 : NameAccessTreeEntry(rString, xObject)
578 {
579 }
580
581 css::uno::Reference<css::uno::XInterface> getMainObject() override
582 {
583 uno::Reference<table::XTableChartsSupplier> xSupplier(mxObject, uno::UNO_QUERY);
584 if (!xSupplier.is())
585 return mxObject;
586 return xSupplier->getCharts();
587 }
588
589 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
590 weld::TreeIter const& rParent) override
591 {
592 uno::Reference<table::XTableCharts> xCharts(getMainObject(), uno::UNO_QUERY);
593 if (!xCharts.is())
594 return;
595 NameAccessTreeEntry::fill(pDocumentModelTree, rParent);
596 }
597};
598
600class PivotTablesEntry : public NameAccessTreeEntry
601{
602public:
603 PivotTablesEntry(OUString const& rString,
604 css::uno::Reference<css::uno::XInterface> const& xObject)
605 : NameAccessTreeEntry(rString, xObject)
606 {
607 }
608
609 bool shouldShowExpander() override { return true; }
610
611 css::uno::Reference<css::uno::XInterface> getMainObject() override
612 {
613 uno::Reference<sheet::XDataPilotTablesSupplier> xSupplier(mxObject, uno::UNO_QUERY);
614 if (!xSupplier.is())
615 return mxObject;
616 return xSupplier->getDataPilotTables();
617 }
618
619 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
620 weld::TreeIter const& rParent) override
621 {
622 uno::Reference<sheet::XDataPilotTables> xPivotTables(getMainObject(), uno::UNO_QUERY);
623 if (!xPivotTables.is())
624 return;
625 NameAccessTreeEntry::fill(pDocumentModelTree, rParent);
626 }
627};
628
630class SheetEntry : public DocumentModelTreeEntry
631{
632public:
633 SheetEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
634 : DocumentModelTreeEntry(rString, xObject)
635 {
636 }
637
638 bool shouldShowExpander() override { return true; }
639
640 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
641 weld::TreeIter const& rParent) override
642 {
643 auto pShapesEntry
644 = std::make_unique<ShapesEntry>(SfxResId(STR_SHAPES_ENTRY), getMainObject());
645 lclAppendToParentEntry(pDocumentModelTree, rParent, pShapesEntry.release());
646
647 auto pChartsEntry
648 = std::make_unique<ChartsEntry>(SfxResId(STR_CHARTS_ENTRY), getMainObject());
649 lclAppendToParentEntry(pDocumentModelTree, rParent, pChartsEntry.release());
650
651 auto pPivotTablesEntry
652 = std::make_unique<PivotTablesEntry>(SfxResId(STR_PIVOT_TABLES_ENTRY), getMainObject());
653 lclAppendToParentEntry(pDocumentModelTree, rParent, pPivotTablesEntry.release());
654 }
655};
656
658class SheetsEntry : public DocumentModelTreeEntry
659{
660public:
661 SheetsEntry(OUString const& rString, css::uno::Reference<css::uno::XInterface> const& xObject)
662 : DocumentModelTreeEntry(rString, xObject)
663 {
664 }
665
666 css::uno::Reference<css::uno::XInterface> getMainObject() override
667 {
668 uno::Reference<sheet::XSpreadsheetDocument> xSheetDocument(mxObject, uno::UNO_QUERY);
669 if (!xSheetDocument.is())
670 return mxObject;
671 return xSheetDocument->getSheets();
672 }
673
674 bool shouldShowExpander() override
675 {
676 uno::Reference<container::XIndexAccess> xIndexAccess(getMainObject(), uno::UNO_QUERY);
677 return xIndexAccess.is() && xIndexAccess->getCount() > 0;
678 }
679
680 void fill(std::unique_ptr<weld::TreeView>& pDocumentModelTree,
681 weld::TreeIter const& rParent) override
682 {
683 uno::Reference<container::XIndexAccess> xIndexAccesss(getMainObject(), uno::UNO_QUERY);
684 if (!xIndexAccesss.is())
685 return;
686
687 for (sal_Int32 i = 0; i < xIndexAccesss->getCount(); ++i)
688 {
689 uno::Reference<sheet::XSpreadsheet> xSheet(xIndexAccesss->getByIndex(i),
690 uno::UNO_QUERY);
691 OUString aString = lclGetNamed(xSheet);
692 if (aString.isEmpty())
693 aString = SfxResId(STR_SHEET).replaceFirst("%1", OUString::number(i + 1));
694 auto pEntry = std::make_unique<SheetEntry>(aString, xSheet);
695 lclAppendToParentEntry(pDocumentModelTree, rParent, pEntry.release());
696 }
697 }
698};
699
700} // end anonymous namespace
701
703 std::unique_ptr<weld::TreeView>& pDocumentModelTree,
704 css::uno::Reference<css::uno::XInterface> xDocument)
705 : mpDocumentModelTree(pDocumentModelTree)
706 , mxDocument(std::move(xDocument))
707{
708 mpDocumentModelTree->connect_expanding(LINK(this, DocumentModelTreeHandler, ExpandingHandler));
709}
710
711uno::Reference<uno::XInterface> DocumentModelTreeHandler::getObjectByID(OUString const& rID)
712{
713 uno::Reference<uno::XInterface> xObject;
714 if (rID.isEmpty())
715 return xObject;
716 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(rID);
717 return pEntry->getMainObject();
718}
719
721{
722 // destroy all DocumentModelTreeEntries from the tree
723 mpDocumentModelTree->all_foreach([this](weld::TreeIter& rEntry) {
724 OUString sID = mpDocumentModelTree->get_id(rEntry);
725 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(sID);
726 delete pEntry;
727 return false;
728 });
729 mpDocumentModelTree->clear();
730}
731
733{
734 bool bChild = false;
735 do
736 {
737 bChild = mpDocumentModelTree->iter_has_child(rParent);
738 if (bChild)
739 {
740 std::unique_ptr<weld::TreeIter> pChild = mpDocumentModelTree->make_iterator(&rParent);
741 bChild = mpDocumentModelTree->iter_children(*pChild);
742 if (bChild)
743 {
744 clearChildren(*pChild);
745 OUString sID = mpDocumentModelTree->get_id(*pChild);
746 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(sID);
747 delete pEntry;
748 mpDocumentModelTree->remove(*pChild);
749 }
750 }
751 } while (bChild);
752}
753
755{
756 mpDocumentModelTree->all_foreach([this](weld::TreeIter& rEntry) {
757 OUString sID = mpDocumentModelTree->get_id(rEntry);
758 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(sID);
759 delete pEntry;
760 return false;
761 });
762}
763
764IMPL_LINK(DocumentModelTreeHandler, ExpandingHandler, weld::TreeIter const&, rParent, bool)
765{
766 OUString sID = mpDocumentModelTree->get_id(rParent);
767 if (sID.isEmpty())
768 return true;
769
770 clearChildren(rParent);
771 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(sID);
772 pEntry->fill(mpDocumentModelTree, rParent);
773
774 return true;
775}
776
778 css::uno::Reference<css::uno::XInterface> const& xInterface)
779{
780 mpDocumentModelTree->unselect_all();
781
782 mpDocumentModelTree->all_foreach([this, xInterface](weld::TreeIter& rEntry) {
783 OUString sID = mpDocumentModelTree->get_id(rEntry);
784 auto* pEntry = weld::fromId<DocumentModelTreeEntry*>(sID);
785 if (xInterface == pEntry->getMainObject())
786 {
787 mpDocumentModelTree->select(rEntry);
788 return true;
789 }
790 return false;
791 });
792}
793
795{
796 clearAll();
797
798 uno::Reference<lang::XServiceInfo> xDocumentServiceInfo(mxDocument, uno::UNO_QUERY_THROW);
799
800 lclAppend(mpDocumentModelTree, new DocumentRootEntry(SfxResId(STR_DOCUMENT_ENTRY), mxDocument));
801
802 if (xDocumentServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
803 {
804 lclAppend(mpDocumentModelTree, new SheetsEntry(SfxResId(STR_SHEETS_ENTRY), mxDocument));
805 lclAppend(mpDocumentModelTree,
806 new StylesFamiliesEntry(SfxResId(STR_STYLES_ENTRY), mxDocument));
807 }
808 else if (xDocumentServiceInfo->supportsService(
809 "com.sun.star.presentation.PresentationDocument"))
810 {
811 lclAppend(mpDocumentModelTree, new SlidesEntry(SfxResId(STR_SLIDES_ENTRY), mxDocument));
812 lclAppend(mpDocumentModelTree,
813 new MasterSlidesEntry(SfxResId(STR_MASTER_SLIDES_ENTRY), mxDocument));
814 lclAppend(mpDocumentModelTree,
815 new StylesFamiliesEntry(SfxResId(STR_STYLES_ENTRY), mxDocument));
816 }
817 else if (xDocumentServiceInfo->supportsService("com.sun.star.drawing.DrawingDocument"))
818 {
819 lclAppend(mpDocumentModelTree, new PagesEntry(SfxResId(STR_PAGES_ENTRY), mxDocument));
820 lclAppend(mpDocumentModelTree,
821 new StylesFamiliesEntry(SfxResId(STR_STYLES_ENTRY), mxDocument));
822 }
823 else if (xDocumentServiceInfo->supportsService("com.sun.star.text.TextDocument")
824 || xDocumentServiceInfo->supportsService("com.sun.star.text.WebDocument"))
825 {
826 lclAppend(mpDocumentModelTree,
827 new ParagraphsEntry(SfxResId(STR_PARAGRAPHS_ENTRY), mxDocument));
828 lclAppend(mpDocumentModelTree, new ShapesEntry(SfxResId(STR_SHAPES_ENTRY), mxDocument));
829 lclAppend(mpDocumentModelTree, new TablesEntry(SfxResId(STR_TABLES_ENTRY), mxDocument));
830 lclAppend(mpDocumentModelTree, new FramesEntry(SfxResId(STR_FRAMES_ENTRY), mxDocument));
831 lclAppend(mpDocumentModelTree,
832 new WriterGraphicObjectsEntry(SfxResId(STR_GRAPHIC_OBJECTS_ENTRY), mxDocument));
833 lclAppend(mpDocumentModelTree,
834 new EmbeddedObjectsEntry(SfxResId(STR_EMBEDDED_OBJECTS_ENTRY), mxDocument));
835 lclAppend(mpDocumentModelTree,
836 new StylesFamiliesEntry(SfxResId(STR_STYLES_ENTRY), mxDocument));
837 }
838}
839
840/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK(DocumentModelTreeHandler, ExpandingHandler, weld::TreeIter const &, rParent, bool)
Document model tree handler.
std::unique_ptr< weld::TreeView > & mpDocumentModelTree
static css::uno::Reference< css::uno::XInterface > getObjectByID(OUString const &rID)
void selectObject(css::uno::Reference< css::uno::XInterface > const &xInterface)
void clearChildren(weld::TreeIter const &rParent)
css::uno::Reference< css::uno::XInterface > mxDocument
DocumentModelTreeHandler(std::unique_ptr< weld::TreeView > &pDocumentModelTree, css::uno::Reference< css::uno::XInterface > xDocument)
OUString maString
OUString getString(const Any &_rAny)
int i
OUString toId(const void *pValue)
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
OUString sId