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 , mpBtnOk(m_xBuilder->weld_button("btnOk"))
25 , mpBtnCancel(m_xBuilder->weld_button("btnCancel"))
26 , mpBtnAdd(m_xBuilder->weld_button("btnAdd"))
27 , mpBtnRemove(m_xBuilder->weld_button("btnRemove"))
28 , mpTreeDiagram(m_xBuilder->weld_tree_view("treeDiagram"))
29 , mpTextAdd(m_xBuilder->weld_text_view("textAdd"))
30{
31 mpBtnCancel->connect_clicked(LINK(this, DiagramDialog, OnAddCancel));
32 mpBtnAdd->connect_clicked(LINK(this, DiagramDialog, OnAddClick));
33 mpBtnRemove->connect_clicked(LINK(this, DiagramDialog, OnRemoveClick));
34
35 populateTree(nullptr, OUString());
36
37 // expand all items
38 weld::TreeView* pTreeDiagram = mpTreeDiagram.get();
39 pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
40 pTreeDiagram->expand_row(rEntry);
41 return false;
42 });
43}
44
46{
47 // If the user cancels the dialog, undo all changes done so far. It may
48 // even be feasible to then delete the redo-stack, since it stays
49 // available (?) - but it does no harm either...
50 while (0 != m_nUndos)
51 {
52 comphelper::dispatchCommand(".uno:Undo", {});
53 m_nUndos--;
54 }
55
56 m_xDialog->response(RET_CANCEL);
57}
58
60{
61 if (!m_rDiagram.isDiagram())
62 return;
63
64 OUString sText = mpTextAdd->get_text();
65 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
66
67 if (pDiagramHelper && !sText.isEmpty())
68 {
69 SdrModel& rDrawModel(m_rDiagram.getSdrModelFromSdrObject());
70 const bool bUndo(rDrawModel.IsUndoEnabled());
72
73 if (bUndo)
74 {
75 // rescue all start state Diagram-defining data
76 aStartState = pDiagramHelper->extractDiagramDataState();
77 }
78
79 OUString sNodeId = pDiagramHelper->addNode(sText);
80
81 if (bUndo)
82 {
83 // create undo action. That will internally secure the
84 // current Diagram-defining data as end state
85 rDrawModel.AddUndo(
86 rDrawModel.GetSdrUndoFactory().CreateUndoDiagramModelData(m_rDiagram, aStartState));
87 m_nUndos++;
88 }
89
90 std::unique_ptr<weld::TreeIter> pEntry(mpTreeDiagram->make_iterator());
91 mpTreeDiagram->insert(nullptr, -1, &sText, &sNodeId, nullptr, nullptr, false, pEntry.get());
92 mpTreeDiagram->select(*pEntry);
93 comphelper::dispatchCommand(".uno:RegenerateDiagram", {});
94 }
95}
96
98{
99 if (!m_rDiagram.isDiagram())
100 return;
101
102 std::unique_ptr<weld::TreeIter> pEntry(mpTreeDiagram->make_iterator());
103 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
104
105 if (pDiagramHelper && mpTreeDiagram->get_selected(pEntry.get()))
106 {
107 SdrModel& rDrawModel(m_rDiagram.getSdrModelFromSdrObject());
108 const bool bUndo(rDrawModel.IsUndoEnabled());
110
111 if (bUndo)
112 {
113 // rescue all start state Diagram-defining data
114 aStartState = pDiagramHelper->extractDiagramDataState();
115 }
116
117 if (pDiagramHelper->removeNode(mpTreeDiagram->get_id(*pEntry)))
118 {
119 if (bUndo)
120 {
121 // create undo action. That will internally secure the
122 // current Diagram-defining data as end state
124 m_rDiagram, aStartState));
125 m_nUndos++;
126 }
127
128 mpTreeDiagram->remove(*pEntry);
129 comphelper::dispatchCommand(".uno:RegenerateDiagram", {});
130 }
131 }
132}
133
134void DiagramDialog::populateTree(const weld::TreeIter* pParent, const OUString& rParentId)
135{
136 if (!m_rDiagram.isDiagram())
137 return;
138
139 const std::shared_ptr< svx::diagram::IDiagramHelper >& pDiagramHelper(m_rDiagram.getDiagramHelper());
140
141 if (!pDiagramHelper)
142 return;
143
144 auto aItems = pDiagramHelper->getChildren(rParentId);
145 for (auto& aItem : aItems)
146 {
147 std::unique_ptr<weld::TreeIter> pEntry(mpTreeDiagram->make_iterator());
148 mpTreeDiagram->insert(pParent, -1, &aItem.second, &aItem.first, nullptr, nullptr, false,
149 pEntry.get());
150 populateTree(pEntry.get(), aItem.first);
151 }
152}
153
155
156/* 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