Debian Codegraph @

chart2/source/controller/uitest/uiobject.cxx

raw
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 <memory>
11 #include <uiobject.hxx>
12
13 #include <ChartWindow.hxx>
14 #include <ChartView.hxx>
15 #include <ChartController.hxx>
16 #include <ChartModel.hxx>
17 #include <ObjectHierarchy.hxx>
18
19 #include <utility>
20 #include <vcl/svapp.hxx>
21
22 #include <algorithm>
23 #include <iterator>
24
25 ChartUIObject::ChartUIObject(const VclPtr<chart::ChartWindow>& xChartWindow,
26 OUString aCID):
27 maCID(std::move(aCID)),
28 mxChartWindow(xChartWindow)
29 {
30 }
31
32 StringMap ChartUIObject::get_state()
33 {
34 StringMap aMap;
35 aMap[u"CID"_ustr] = maCID;
36
37 return aMap;
38 }
39
40 void ChartUIObject::execute(const OUString& rAction,
41 const StringMap& rParameters)
42 {
43 if (rAction == "SELECT")
44 {
45 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
46
47 StringMap aParams;
48 aParams[u"NAME"_ustr] = maCID;
49 pWindow->execute(rAction, aParams);
50 }
51 else if (rAction == "COMMAND")
52 {
53 // first select object
54 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
55
56 StringMap aParams;
57 aParams[u"NAME"_ustr] = maCID;
58 pWindow->execute(u"SELECT"_ustr, aParams);
59
60 auto itr = rParameters.find(u"COMMAND"_ustr);
61 if (itr == rParameters.end())
62 throw css::uno::RuntimeException(u"missing COMMAND parameter"_ustr);
63
64 maCommands.emplace_back(new OUString(itr->second));
65 OUString* pCommand = maCommands.rbegin()->get();
66
67 Application::PostUserEvent(LINK(this, ChartUIObject, PostCommand), pCommand);
68 }
69 }
70
71 IMPL_LINK(ChartUIObject, PostCommand, void*, pCommand, void)
72 {
73 css::util::URL aURL;
74 aURL.Path = *static_cast<OUString*>(pCommand);
75 mxChartWindow->GetController()->dispatch(aURL, css::uno::Sequence<css::beans::PropertyValue>());
76 }
77
78 std::unique_ptr<UIObject> ChartUIObject::get_child(const OUString& rID)
79 {
80 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
81
82 return pWindow->get_child(rID);
83 }
84
85 std::set<OUString> ChartUIObject::get_children() const
86 {
87 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
88
89 return pWindow->get_children();
90 }
91
92 OUString ChartUIObject::get_type() const
93 {
94 return u"ChartUIObject for type: "_ustr;
95 }
96
97 bool ChartUIObject::equals(const UIObject& rOther) const
98 {
99 const ChartUIObject* pOther = dynamic_cast<const ChartUIObject*>(&rOther);
100 if (!pOther)
101 return false;
102 return mxChartWindow.get() == pOther->mxChartWindow.get();
103 }
104
105 ChartWindowUIObject::ChartWindowUIObject(const VclPtr<chart::ChartWindow>& xChartWindow):
106 WindowUIObject(xChartWindow),
107 mxChartWindow(xChartWindow)
108 {
109 }
110
111 StringMap ChartWindowUIObject::get_state()
112 {
113 StringMap aMap = WindowUIObject::get_state();
114
115 chart::ChartController* pController = mxChartWindow->GetController();
116 if (pController)
117 {
118 css::uno::Any aAny = pController->getSelection();
119 OUString aSelectedObject;
120 aAny >>= aSelectedObject;
121 aMap[u"SelectedObject"_ustr] = aSelectedObject;
122 }
123
124 return aMap;
125 }
126
127 void ChartWindowUIObject::execute(const OUString& rAction,
128 const StringMap& rParameters)
129 {
130 if (rAction == "SELECT")
131 {
132 auto itr = rParameters.find(u"NAME"_ustr);
133 if (itr == rParameters.end())
134 throw css::uno::RuntimeException(u"Missing Parameter 'NAME' for action 'SELECT'"_ustr);
135
136
137 const OUString& rName = itr->second;
138 css::uno::Any aAny;
139 aAny <<= rName;
140
141 chart::ChartController* pController = mxChartWindow->GetController();
142 pController->select(aAny);
143 }
144 else
145 WindowUIObject::execute(rAction, rParameters);
146 }
147
148 std::unique_ptr<UIObject> ChartWindowUIObject::get_child(const OUString& rID)
149 {
150 if (chart::ObjectIdentifier::isCID(rID))
151 return std::unique_ptr<UIObject>(new ChartUIObject(mxChartWindow, rID));
152
153 throw css::uno::RuntimeException(u"unknown child"_ustr);
154 }
155
156 namespace {
157
158 void recursiveAdd(chart::ObjectIdentifier const & rID, std::set<OUString>& rChildren, const chart::ObjectHierarchy& rHierarchy)
159 {
160 std::vector<chart::ObjectIdentifier> aChildIdentifiers = rHierarchy.getChildren(rID);
161 std::transform(aChildIdentifiers.begin(), aChildIdentifiers.end(), std::inserter(rChildren, rChildren.begin()),
162 [](const chart::ObjectIdentifier& rObject)
163 {
164 return rObject.getObjectCID();
165 }
166 );
167
168 for (const chart::ObjectIdentifier& ID: aChildIdentifiers)
169 recursiveAdd(ID, rChildren, rHierarchy);
170 }
171
172 }
173
174 std::set<OUString> ChartWindowUIObject::get_children() const
175 {
176 std::set<OUString> aChildren;
177
178 chart::ChartController* pController = mxChartWindow->GetController();
179 if (!pController)
180 return aChildren;
181
182 rtl::Reference<::chart::ChartModel> xChartDoc = pController->getChartModel();
183 rtl::Reference<::chart::ChartView> xChartView = pController->getChartView();
184 chart::ChartView* pValueProvider = xChartView.get();
185 chart::ObjectHierarchy aHierarchy(xChartDoc, pValueProvider);
186 chart::ObjectIdentifier aIdentifier = chart::ObjectHierarchy::getRootNodeOID();
187 aChildren.insert(aIdentifier.getObjectCID());
188
189 recursiveAdd(aIdentifier, aChildren, aHierarchy);
190
191 return aChildren;
192 }
193
194 std::unique_ptr<UIObject> ChartWindowUIObject::create(vcl::Window* pWindow)
195 {
196 chart::ChartWindow* pChartWindow = dynamic_cast<chart::ChartWindow*>(pWindow);
197 assert(pChartWindow);
198
199 return std::unique_ptr<UIObject>(new ChartWindowUIObject(pChartWindow));
200 }
201
202 OUString ChartWindowUIObject::get_name() const
203 {
204 return u"ChartWindowUIObject"_ustr;
205 }
206
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */