Debian Codegraph @

chart2/source/controller/main/ElementSelector.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include "ElementSelector.hxx"
21 #include <ObjectNameProvider.hxx>
22 #include <ObjectHierarchy.hxx>
23 #include <DrawViewWrapper.hxx>
24 #include <ResId.hxx>
25 #include <strings.hrc>
26 #include <ObjectIdentifier.hxx>
27 #include <ChartController.hxx>
28 #include <ChartModel.hxx>
29 #include <ChartView.hxx>
30
31 #include <cppuhelper/supportsservice.hxx>
32 #include <o3tl/safeint.hxx>
33 #include <toolkit/helper/vclunohelper.hxx>
34 #include <vcl/svapp.hxx>
35
36 namespace chart
37 {
38
39 using namespace com::sun::star;
40 using namespace com::sun::star::uno;
41 using ::com::sun::star::uno::Any;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44
45 namespace
46 {
47 constexpr OUStringLiteral lcl_aServiceName
48 = u"com.sun.star.comp.chart.ElementSelectorToolbarController";
49 }
50
51 SelectorListBox::SelectorListBox(vcl::Window* pParent)
52 : InterimItemWindow(pParent, u"modules/schart/ui/combobox.ui"_ustr, u"ComboBox"_ustr)
53 , m_xWidget(m_xBuilder->weld_combo_box(u"combobox"_ustr))
54 , m_bReleaseFocus(true)
55 {
56 InitControlBase(m_xWidget.get());
57
58 m_xWidget->connect_key_press(LINK(this, SelectorListBox, KeyInputHdl));
59 m_xWidget->connect_changed(LINK(this, SelectorListBox, SelectHdl));
60 m_xWidget->connect_focus_out(LINK(this, SelectorListBox, FocusOutHdl));
61
62 ::Size aLogicalSize(75, 0);
63 ::Size aPixelSize = LogicToPixel(aLogicalSize, MapMode(MapUnit::MapAppFont));
64 m_xWidget->set_size_request(aPixelSize.Width(), -1);
65 SetSizePixel(m_xContainer->get_preferred_size());
66 }
67
68 void SelectorListBox::dispose()
69 {
70 m_xWidget.reset();
71 InterimItemWindow::dispose();
72 }
73
74 SelectorListBox::~SelectorListBox()
75 {
76 disposeOnce();
77 }
78
79 static void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectIdentifier & rParent, std::vector< ListBoxEntryData >& rEntries
80 , const sal_Int32 nHierarchyDepth, const rtl::Reference<::chart::ChartModel>& xChartDoc )
81 {
82 ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
83 for (auto const& child : aChildren)
84 {
85 ListBoxEntryData aEntry;
86 aEntry.OID = child;
87 aEntry.UIName = ObjectNameProvider::getNameForCID( child.getObjectCID(), xChartDoc );
88 aEntry.nHierarchyDepth = nHierarchyDepth;
89 rEntries.push_back(aEntry);
90 lcl_addObjectsToList( rHierarchy, child, rEntries, nHierarchyDepth+1, xChartDoc );
91 }
92 }
93
94 void SelectorListBox::SetChartController( const rtl::Reference< ::chart::ChartController >& xChartController )
95 {
96 m_xChartController = xChartController.get();
97 }
98
99 void SelectorListBox::UpdateChartElementsListAndSelection()
100 {
101 m_xWidget->clear();
102 m_aEntries.clear();
103
104 rtl::Reference< ::chart::ChartController > xChartController = m_xChartController.get();
105 if( xChartController.is() )
106 {
107 ObjectIdentifier aSelectedOID( xChartController->getSelection() );
108 const OUString& aSelectedCID = aSelectedOID.getObjectCID();
109
110 rtl::Reference<::chart::ChartModel> xChartDoc = xChartController->getChartModel();
111 ObjectType eType( aSelectedOID.getObjectType() );
112 bool bAddSelectionToList = false;
113 if ( eType == OBJECTTYPE_DATA_POINT || eType == OBJECTTYPE_DATA_LABEL || eType == OBJECTTYPE_SHAPE )
114 bAddSelectionToList = true;
115
116 rtl::Reference< ChartView > xChartView;
117 rtl::Reference< ChartModel > xFact = xChartController->getChartModel();
118 if( xFact.is() )
119 xChartView = xFact->createChartView();
120 ChartView* pExplicitValueProvider = nullptr; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
121 ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
122 lcl_addObjectsToList( aHierarchy, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries, 0, xChartDoc );
123
124 if( bAddSelectionToList )
125 {
126 if ( aSelectedOID.isAutoGeneratedObject() )
127 {
128 OUString aSeriesCID = ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID ) );
129 std::vector< ListBoxEntryData >::iterator aIt = std::find_if(m_aEntries.begin(), m_aEntries.end(),
130 [&aSeriesCID](const ListBoxEntryData& rEntry) { return rEntry.OID.getObjectCID().match(aSeriesCID); });
131 if (aIt != m_aEntries.end())
132 {
133 ListBoxEntryData aEntry;
134 aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
135 aEntry.OID = aSelectedOID;
136 ++aIt;
137 if( aIt != m_aEntries.end() )
138 m_aEntries.insert(aIt, aEntry);
139 else
140 m_aEntries.push_back( aEntry );
141 }
142 }
143 else if ( aSelectedOID.isAdditionalShape() )
144 {
145 ListBoxEntryData aEntry;
146 SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
147 OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
148 aEntry.UIName = ( aName.isEmpty() ? SchResId( STR_OBJECT_SHAPE ) : aName );
149 aEntry.OID = aSelectedOID;
150 m_aEntries.push_back( aEntry );
151 }
152 }
153
154 m_xWidget->freeze();
155 sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
156 sal_uInt16 nN=0;
157 for (auto const& entry : m_aEntries)
158 {
159 // tdf#152087 strip any newlines from the entry
160 m_xWidget->append_text(entry.UIName.replaceAll("\n", " "));
161 if ( !bSelectionFound && aSelectedOID == entry.OID )
162 {
163 nEntryPosToSelect = nN;
164 bSelectionFound = true;
165 }
166 ++nN;
167 }
168 m_xWidget->thaw();
169
170 if( bSelectionFound )
171 m_xWidget->set_active(nEntryPosToSelect);
172 }
173 m_xWidget->save_value(); //remind current selection pos
174 }
175
176 void SelectorListBox::ReleaseFocus_Impl()
177 {
178 if ( !m_bReleaseFocus )
179 {
180 m_bReleaseFocus = true;
181 return;
182 }
183
184 rtl::Reference< ::chart::ChartController > xController = m_xChartController.get();
185 Reference< frame::XFrame > xFrame( xController->getFrame() );
186 if ( xFrame.is() && xFrame->getContainerWindow().is() )
187 xFrame->getContainerWindow()->setFocus();
188 }
189
190 IMPL_LINK(SelectorListBox, SelectHdl, weld::ComboBox&, rComboBox, void)
191 {
192 if (rComboBox.changed_by_direct_pick())
193 {
194 const sal_Int32 nPos = rComboBox.get_active();
195 if (o3tl::make_unsigned(nPos) < m_aEntries.size())
196 {
197 ObjectIdentifier aOID = m_aEntries[nPos].OID;
198 rtl::Reference< ::chart::ChartController > xController = m_xChartController.get();
199 if( xController.is() )
200 xController->select( aOID.getAny() );
201 }
202 ReleaseFocus_Impl();
203 }
204 }
205
206 IMPL_LINK(SelectorListBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
207 {
208 bool bHandled = false;
209
210 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
211
212 switch ( nCode )
213 {
214 case KEY_RETURN:
215 case KEY_TAB:
216 {
217 if ( nCode == KEY_TAB )
218 m_bReleaseFocus = false;
219 else
220 bHandled = true;
221 SelectHdl(*m_xWidget);
222 break;
223 }
224
225 case KEY_ESCAPE:
226 m_xWidget->set_active_text(m_xWidget->get_saved_value()); //restore saved selection
227 ReleaseFocus_Impl();
228 break;
229 }
230
231 return bHandled || ChildKeyInput(rKEvt);
232 }
233
234 IMPL_LINK_NOARG(SelectorListBox, FocusOutHdl, weld::Widget&, void)
235 {
236 if (m_xWidget && !m_xWidget->has_focus()) // comboboxes can be comprised of multiple widgets, ensure all have lost focus
237 m_xWidget->set_active_text(m_xWidget->get_saved_value());
238 }
239
240 OUString SAL_CALL ElementSelectorToolbarController::getImplementationName()
241 {
242 return lcl_aServiceName;
243 }
244
245 sal_Bool SAL_CALL ElementSelectorToolbarController::supportsService( const OUString& rServiceName )
246 {
247 return cppu::supportsService(this, rServiceName);
248 }
249
250 css::uno::Sequence< OUString > SAL_CALL ElementSelectorToolbarController::getSupportedServiceNames()
251 {
252 return { u"com.sun.star.frame.ToolbarController"_ustr };
253 }
254 ElementSelectorToolbarController::ElementSelectorToolbarController()
255 {
256 }
257 ElementSelectorToolbarController::~ElementSelectorToolbarController()
258 {
259 }
260 // XInterface
261 Any SAL_CALL ElementSelectorToolbarController::queryInterface( const Type& _rType )
262 {
263 Any aReturn = ToolboxController::queryInterface(_rType);
264 if (!aReturn.hasValue())
265 aReturn = ElementSelectorToolbarController_BASE::queryInterface(_rType);
266 return aReturn;
267 }
268 void SAL_CALL ElementSelectorToolbarController::acquire() noexcept
269 {
270 ToolboxController::acquire();
271 }
272 void SAL_CALL ElementSelectorToolbarController::release() noexcept
273 {
274 ToolboxController::release();
275 }
276 void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent )
277 {
278 if( m_apSelectorListBox )
279 {
280 SolarMutexGuard aSolarMutexGuard;
281 if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
282 {
283 Reference< frame::XController > xChartController;
284 rEvent.State >>= xChartController;
285 ::chart::ChartController* pController = dynamic_cast<::chart::ChartController*>(xChartController.get());
286 assert(!xChartController || pController);
287 m_apSelectorListBox->SetChartController( pController );
288 m_apSelectorListBox->UpdateChartElementsListAndSelection();
289 }
290 }
291 }
292 uno::Reference< awt::XWindow > SAL_CALL ElementSelectorToolbarController::createItemWindow( const uno::Reference< awt::XWindow >& xParent )
293 {
294 uno::Reference< awt::XWindow > xItemWindow;
295 if( !m_apSelectorListBox )
296 {
297 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow( xParent );
298 if( pParent )
299 {
300 m_apSelectorListBox.reset(VclPtr<SelectorListBox>::Create(pParent));
301 }
302 }
303 if( m_apSelectorListBox )
304 xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
305 return xItemWindow;
306 }
307
308 } // chart2
309
310 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
311 com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *,
312 css::uno::Sequence<css::uno::Any> const &)
313 {
314 return cppu::acquire(new chart::ElementSelectorToolbarController );
315 }
316
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */