tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / cui / source / dialogs / DiagramDialog.cxx
bloba2499056210cb49b2a13ea27aa58a641dd6aa489
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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)
23 , m_nUndos(0)
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());
36 // expand all items
37 weld::TreeView* pTreeDiagram = mpTreeDiagram.get();
38 pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
39 pTreeDiagram->expand_row(rEntry);
40 return false;
41 });
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...
49 while (0 != m_nUndos)
51 comphelper::dispatchCommand(u".uno:Undo"_ustr, {});
52 m_nUndos--;
55 m_xDialog->response(RET_CANCEL);
58 IMPL_LINK_NOARG(DiagramDialog, OnAddClick, weld::Button&, void)
60 if (!m_rDiagram.isDiagram())
61 return;
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;
72 if (bUndo)
74 // rescue all start state Diagram-defining data
75 aStartState = pDiagramHelper->extractDiagramDataState();
78 OUString sNodeId = pDiagramHelper->addNode(sText);
80 if (bUndo)
82 // create undo action. That will internally secure the
83 // current Diagram-defining data as end state
84 rDrawModel.AddUndo(
85 rDrawModel.GetSdrUndoFactory().CreateUndoDiagramModelData(m_rDiagram, aStartState));
86 m_nUndos++;
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())
99 return;
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;
110 if (bUndo)
112 // rescue all start state Diagram-defining data
113 aStartState = pDiagramHelper->extractDiagramDataState();
116 if (pDiagramHelper->removeNode(mpTreeDiagram->get_id(*pEntry)))
118 if (bUndo)
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));
124 m_nUndos++;
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())
136 return;
138 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
140 if (!pDiagramHelper)
141 return;
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,
148 pEntry.get());
149 populateTree(pEntry.get(), aItem.first);
153 DiagramDialog::~DiagramDialog() {}
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */