1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <DiagramDialog.hxx>
12 #include <comphelper/dispatchcommand.hxx>
13 #include <svx/svdogrp.hxx>
14 #include <svx/svdmodel.hxx>
15 #include <svx/svdundo.hxx>
16 #include <com/sun/star/beans/PropertyValue.hpp>
17 #include <svx/diagram/datamodel.hxx>
18 #include <svx/diagram/IDiagramHelper.hxx>
20 DiagramDialog::DiagramDialog(weld::Window
* pWindow
, SdrObjGroup
& rDiagram
)
21 : GenericDialogController(pWindow
, u
"cui/ui/diagramdialog.ui"_ustr
, u
"DiagramDialog"_ustr
)
22 , m_rDiagram(rDiagram
)
24 , mpBtnCancel(m_xBuilder
->weld_button(u
"btnCancel"_ustr
))
25 , mpBtnAdd(m_xBuilder
->weld_button(u
"btnAdd"_ustr
))
26 , mpBtnRemove(m_xBuilder
->weld_button(u
"btnRemove"_ustr
))
27 , mpTreeDiagram(m_xBuilder
->weld_tree_view(u
"treeDiagram"_ustr
))
28 , mpTextAdd(m_xBuilder
->weld_text_view(u
"textAdd"_ustr
))
30 mpBtnCancel
->connect_clicked(LINK(this, DiagramDialog
, OnAddCancel
));
31 mpBtnAdd
->connect_clicked(LINK(this, DiagramDialog
, OnAddClick
));
32 mpBtnRemove
->connect_clicked(LINK(this, DiagramDialog
, OnRemoveClick
));
34 populateTree(nullptr, OUString());
37 weld::TreeView
* pTreeDiagram
= mpTreeDiagram
.get();
38 pTreeDiagram
->all_foreach([pTreeDiagram
](weld::TreeIter
& rEntry
) {
39 pTreeDiagram
->expand_row(rEntry
);
44 IMPL_LINK_NOARG(DiagramDialog
, OnAddCancel
, weld::Button
&, void)
46 // If the user cancels the dialog, undo all changes done so far. It may
47 // even be feasible to then delete the redo-stack, since it stays
48 // available (?) - but it does no harm either...
51 comphelper::dispatchCommand(u
".uno:Undo"_ustr
, {});
55 m_xDialog
->response(RET_CANCEL
);
58 IMPL_LINK_NOARG(DiagramDialog
, OnAddClick
, weld::Button
&, void)
60 if (!m_rDiagram
.isDiagram())
63 OUString sText
= mpTextAdd
->get_text();
64 const std::shared_ptr
< svx::diagram::IDiagramHelper
>& pDiagramHelper(m_rDiagram
.getDiagramHelper());
66 if (pDiagramHelper
&& !sText
.isEmpty())
68 SdrModel
& rDrawModel(m_rDiagram
.getSdrModelFromSdrObject());
69 const bool bUndo(rDrawModel
.IsUndoEnabled());
70 svx::diagram::DiagramDataStatePtr aStartState
;
74 // rescue all start state Diagram-defining data
75 aStartState
= pDiagramHelper
->extractDiagramDataState();
78 OUString sNodeId
= pDiagramHelper
->addNode(sText
);
82 // create undo action. That will internally secure the
83 // current Diagram-defining data as end state
85 rDrawModel
.GetSdrUndoFactory().CreateUndoDiagramModelData(m_rDiagram
, aStartState
));
89 std::unique_ptr
<weld::TreeIter
> pEntry(mpTreeDiagram
->make_iterator());
90 mpTreeDiagram
->insert(nullptr, -1, &sText
, &sNodeId
, nullptr, nullptr, false, pEntry
.get());
91 mpTreeDiagram
->select(*pEntry
);
92 comphelper::dispatchCommand(u
".uno:RegenerateDiagram"_ustr
, {});
96 IMPL_LINK_NOARG(DiagramDialog
, OnRemoveClick
, weld::Button
&, void)
98 if (!m_rDiagram
.isDiagram())
101 std::unique_ptr
<weld::TreeIter
> pEntry(mpTreeDiagram
->make_iterator());
102 const std::shared_ptr
< svx::diagram::IDiagramHelper
>& pDiagramHelper(m_rDiagram
.getDiagramHelper());
104 if (pDiagramHelper
&& mpTreeDiagram
->get_selected(pEntry
.get()))
106 SdrModel
& rDrawModel(m_rDiagram
.getSdrModelFromSdrObject());
107 const bool bUndo(rDrawModel
.IsUndoEnabled());
108 svx::diagram::DiagramDataStatePtr aStartState
;
112 // rescue all start state Diagram-defining data
113 aStartState
= pDiagramHelper
->extractDiagramDataState();
116 if (pDiagramHelper
->removeNode(mpTreeDiagram
->get_id(*pEntry
)))
120 // create undo action. That will internally secure the
121 // current Diagram-defining data as end state
122 rDrawModel
.AddUndo(rDrawModel
.GetSdrUndoFactory().CreateUndoDiagramModelData(
123 m_rDiagram
, aStartState
));
127 mpTreeDiagram
->remove(*pEntry
);
128 comphelper::dispatchCommand(u
".uno:RegenerateDiagram"_ustr
, {});
133 void DiagramDialog::populateTree(const weld::TreeIter
* pParent
, const OUString
& rParentId
)
135 if (!m_rDiagram
.isDiagram())
138 const std::shared_ptr
< svx::diagram::IDiagramHelper
>& pDiagramHelper(m_rDiagram
.getDiagramHelper());
143 auto aItems
= pDiagramHelper
->getChildren(rParentId
);
144 for (auto& aItem
: aItems
)
146 std::unique_ptr
<weld::TreeIter
> pEntry(mpTreeDiagram
->make_iterator());
147 mpTreeDiagram
->insert(pParent
, -1, &aItem
.second
, &aItem
.first
, nullptr, nullptr, false,
149 populateTree(pEntry
.get(), aItem
.first
);
153 DiagramDialog::~DiagramDialog() {}
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */