LibreOffice Module cui (master) 1
DiagramDialog.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#include <DiagramDialog.hxx>
11
13#include <svx/svdogrp.hxx>
14#include <svx/svdmodel.hxx>
15#include <svx/svdundo.hxx>
16#include <com/sun/star/beans/PropertyValue.hpp>
19
21 : GenericDialogController(pWindow, "cui/ui/diagramdialog.ui", "DiagramDialog")
22 , m_rDiagram(rDiagram)
23 , m_nUndos(0)
24 , mpBtnCancel(m_xBuilder->weld_button("btnCancel"))
25 , mpBtnAdd(m_xBuilder->weld_button("btnAdd"))
26 , mpBtnRemove(m_xBuilder->weld_button("btnRemove"))
27 , mpTreeDiagram(m_xBuilder->weld_tree_view("treeDiagram"))
28 , mpTextAdd(m_xBuilder->weld_text_view("textAdd"))
29{
30 mpBtnCancel->connect_clicked(LINK(this, DiagramDialog, OnAddCancel));
31 mpBtnAdd->connect_clicked(LINK(this, DiagramDialog, OnAddClick));
32 mpBtnRemove->connect_clicked(LINK(this, DiagramDialog, OnRemoveClick));
33
34 populateTree(nullptr, OUString());
35
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 });
42}
43
45{
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)
50 {
51 comphelper::dispatchCommand(".uno:Undo", {});
52 m_nUndos--;
53 }
54
55 m_xDialog->response(RET_CANCEL);
56}
57
59{
60 if (!m_rDiagram.isDiagram())
61 return;
62
63 OUString sText = mpTextAdd->get_text();
64 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
65
66 if (pDiagramHelper && !sText.isEmpty())
67 {
68 SdrModel& rDrawModel(m_rDiagram.getSdrModelFromSdrObject());
69 const bool bUndo(rDrawModel.IsUndoEnabled());
71
72 if (bUndo)
73 {
74 // rescue all start state Diagram-defining data
75 aStartState = pDiagramHelper->extractDiagramDataState();
76 }
77
78 OUString sNodeId = pDiagramHelper->addNode(sText);
79
80 if (bUndo)
81 {
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++;
87 }
88
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(".uno:RegenerateDiagram", {});
93 }
94}
95
97{
98 if (!m_rDiagram.isDiagram())
99 return;
100
101 std::unique_ptr<weld::TreeIter> pEntry(mpTreeDiagram->make_iterator());
102 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
103
104 if (pDiagramHelper && mpTreeDiagram->get_selected(pEntry.get()))
105 {
106 SdrModel& rDrawModel(m_rDiagram.getSdrModelFromSdrObject());
107 const bool bUndo(rDrawModel.IsUndoEnabled());
109
110 if (bUndo)
111 {
112 // rescue all start state Diagram-defining data
113 aStartState = pDiagramHelper->extractDiagramDataState();
114 }
115
116 if (pDiagramHelper->removeNode(mpTreeDiagram->get_id(*pEntry)))
117 {
118 if (bUndo)
119 {
120 // create undo action. That will internally secure the
121 // current Diagram-defining data as end state
123 m_rDiagram, aStartState));
124 m_nUndos++;
125 }
126
127 mpTreeDiagram->remove(*pEntry);
128 comphelper::dispatchCommand(".uno:RegenerateDiagram", {});
129 }
130 }
131}
132
133void DiagramDialog::populateTree(const weld::TreeIter* pParent, const OUString& rParentId)
134{
135 if (!m_rDiagram.isDiagram())
136 return;
137
138 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
139
140 if (!pDiagramHelper)
141 return;
142
143 auto aItems = pDiagramHelper->getChildren(rParentId);
144 for (auto& aItem : aItems)
145 {
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);
150 }
151}
152
154
155/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK_NOARG(DiagramDialog, OnAddCancel, weld::Button &, void)
Reference< XExecutableDialog > m_xDialog
Edit Diagram dialog.
std::unique_ptr< weld::Button > mpBtnRemove
std::unique_ptr< weld::TreeView > mpTreeDiagram
SdrObjGroup & m_rDiagram
DiagramDialog(weld::Window *pWindow, SdrObjGroup &rDiagram)
std::unique_ptr< weld::Button > mpBtnAdd
void populateTree(const weld::TreeIter *pParent, const OUString &rParentId)
virtual ~DiagramDialog() override
std::unique_ptr< weld::Button > mpBtnCancel
void AddUndo(std::unique_ptr< SdrUndoAction > pUndo)
SdrUndoFactory & GetSdrUndoFactory() const
bool IsUndoEnabled() const
virtual const std::shared_ptr< svx::diagram::IDiagramHelper > & getDiagramHelper() const override
bool isDiagram() const
virtual std::unique_ptr< SdrUndoAction > CreateUndoDiagramModelData(SdrObject &rObject, std::shared_ptr< svx::diagram::DiagramDataState > &rStartState)
virtual void expand_row(const TreeIter &rIter)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
std::shared_ptr< DiagramDataState > DiagramDataStatePtr
RET_CANCEL