Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / uitest / uiobject.cxx
blob0f3ec51b1bf80c294fb4d0ecccd9d9c4d14ca94c
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 <memory>
11 #include <uiobject.hxx>
13 #include <ChartWindow.hxx>
14 #include <ChartController.hxx>
15 #include <ObjectHierarchy.hxx>
16 #include <chartview/ExplicitValueProvider.hxx>
17 #include <com/sun/star/chart2/XChartDocument.hpp>
19 #include <comphelper/servicehelper.hxx>
21 #include <vcl/svapp.hxx>
23 #include <algorithm>
24 #include <iterator>
26 ChartUIObject::ChartUIObject(const VclPtr<chart::ChartWindow>& xChartWindow,
27 const OUString& rCID):
28 maCID(rCID),
29 mxChartWindow(xChartWindow)
33 StringMap ChartUIObject::get_state()
35 StringMap aMap;
36 aMap["CID"] = maCID;
38 return aMap;
41 void ChartUIObject::execute(const OUString& rAction,
42 const StringMap& rParameters)
44 if (rAction == "SELECT")
46 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
48 StringMap aParams;
49 aParams["NAME"] = maCID;
50 pWindow->execute(rAction, aParams);
52 else if (rAction == "COMMAND")
54 // first select object
55 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
57 StringMap aParams;
58 aParams["NAME"] = maCID;
59 pWindow->execute("SELECT", aParams);
61 auto itr = rParameters.find("COMMAND");
62 if (itr == rParameters.end())
63 throw css::uno::RuntimeException("missing COMMAND parameter");
65 maCommands.emplace_back(new OUString(itr->second));
66 OUString* pCommand = maCommands.rbegin()->get();
68 Application::PostUserEvent(LINK(this, ChartUIObject, PostCommand), pCommand);
72 IMPL_LINK(ChartUIObject, PostCommand, void*, pCommand, void)
74 css::util::URL aURL;
75 aURL.Path = *static_cast<OUString*>(pCommand);
76 mxChartWindow->GetController()->dispatch(aURL, css::uno::Sequence<css::beans::PropertyValue>());
79 std::unique_ptr<UIObject> ChartUIObject::get_child(const OUString& rID)
81 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
83 return pWindow->get_child(rID);
86 std::set<OUString> ChartUIObject::get_children() const
88 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
90 return pWindow->get_children();
93 OUString ChartUIObject::get_type() const
95 return "ChartUIObject for type: ";
98 ChartWindowUIObject::ChartWindowUIObject(const VclPtr<chart::ChartWindow>& xChartWindow):
99 WindowUIObject(xChartWindow),
100 mxChartWindow(xChartWindow)
104 StringMap ChartWindowUIObject::get_state()
106 StringMap aMap = WindowUIObject::get_state();
108 chart::ChartController* pController = mxChartWindow->GetController();
109 if (pController)
111 css::uno::Any aAny = pController->getSelection();
112 OUString aSelectedObject;
113 aAny >>= aSelectedObject;
114 aMap["SelectedObject"] = aSelectedObject;
117 return aMap;
120 void ChartWindowUIObject::execute(const OUString& rAction,
121 const StringMap& rParameters)
123 if (rAction == "SELECT")
125 auto itr = rParameters.find("NAME");
126 if (itr == rParameters.end())
127 throw css::uno::RuntimeException("Missing Parameter 'NAME' for action 'SELECT'");
130 const OUString& rName = itr->second;
131 css::uno::Any aAny;
132 aAny <<= rName;
134 chart::ChartController* pController = mxChartWindow->GetController();
135 pController->select(aAny);
137 else
138 WindowUIObject::execute(rAction, rParameters);
141 std::unique_ptr<UIObject> ChartWindowUIObject::get_child(const OUString& rID)
143 if (chart::ObjectIdentifier::isCID(rID))
144 return std::unique_ptr<UIObject>(new ChartUIObject(mxChartWindow, rID));
146 throw css::uno::RuntimeException("unknown child");
149 namespace {
151 void recursiveAdd(chart::ObjectIdentifier const & rID, std::set<OUString>& rChildren, const chart::ObjectHierarchy& rHierarchy)
153 std::vector<chart::ObjectIdentifier> aChildIndentifiers = rHierarchy.getChildren(rID);
154 std::transform(aChildIndentifiers.begin(), aChildIndentifiers.end(), std::inserter(rChildren, rChildren.begin()),
155 [](const chart::ObjectIdentifier& rObject)
157 return rObject.getObjectCID();
161 for (const chart::ObjectIdentifier& ID: aChildIndentifiers)
162 recursiveAdd(ID, rChildren, rHierarchy);
167 std::set<OUString> ChartWindowUIObject::get_children() const
169 std::set<OUString> aChildren;
171 chart::ChartController* pController = mxChartWindow->GetController();
172 if (!pController)
173 return aChildren;
175 css::uno::Reference< css::chart2::XChartDocument > xChartDoc( pController->getModel(), css::uno::UNO_QUERY );
177 css::uno::Reference<css::uno::XInterface> xChartView = pController->getChartView();
178 chart::ExplicitValueProvider* pValueProvider = comphelper::getUnoTunnelImplementation<chart::ExplicitValueProvider>( xChartView );
179 chart::ObjectHierarchy aHierarchy(xChartDoc, pValueProvider, true);
180 chart::ObjectIdentifier aIdentifier = chart::ObjectHierarchy::getRootNodeOID();
181 aChildren.insert(aIdentifier.getObjectCID());
183 recursiveAdd(aIdentifier, aChildren, aHierarchy);
185 return aChildren;
188 std::unique_ptr<UIObject> ChartWindowUIObject::create(vcl::Window* pWindow)
190 chart::ChartWindow* pChartWindow = dynamic_cast<chart::ChartWindow*>(pWindow);
191 assert(pChartWindow);
193 return std::unique_ptr<UIObject>(new ChartWindowUIObject(pChartWindow));
196 OUString ChartWindowUIObject::get_name() const
198 return "ChartWindowUIObject";
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */