LibreOffice Module chart2 (master) 1
ElementSelector.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 * 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"
22#include <ObjectHierarchy.hxx>
23#include <servicenames.hxx>
24#include <DrawViewWrapper.hxx>
25#include <ResId.hxx>
26#include <strings.hrc>
27#include <ObjectIdentifier.hxx>
28#include <ChartController.hxx>
29#include <ChartModel.hxx>
30
32#include <o3tl/safeint.hxx>
34#include <vcl/svapp.hxx>
35
36namespace chart { class ExplicitValueProvider; }
37
38namespace chart
39{
40
41using namespace com::sun::star;
42using namespace com::sun::star::uno;
43using ::com::sun::star::uno::Any;
44using ::com::sun::star::uno::Reference;
45using ::com::sun::star::uno::Sequence;
46
47namespace
48{
49constexpr OUStringLiteral lcl_aServiceName
50 = u"com.sun.star.comp.chart.ElementSelectorToolbarController";
51}
52
54 : InterimItemWindow(pParent, "modules/schart/ui/combobox.ui", "ComboBox")
55 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
56 , m_bReleaseFocus(true)
57{
59
60 m_xWidget->connect_key_press(LINK(this, SelectorListBox, KeyInputHdl));
61 m_xWidget->connect_changed(LINK(this, SelectorListBox, SelectHdl));
62 m_xWidget->connect_focus_out(LINK(this, SelectorListBox, FocusOutHdl));
63
64 ::Size aLogicalSize(75, 0);
65 ::Size aPixelSize = LogicToPixel(aLogicalSize, MapMode(MapUnit::MapAppFont));
66 m_xWidget->set_size_request(aPixelSize.Width(), -1);
67 SetSizePixel(m_xContainer->get_preferred_size());
68}
69
71{
72 m_xWidget.reset();
74}
75
77{
79}
80
81static void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectIdentifier & rParent, std::vector< ListBoxEntryData >& rEntries
82 , const sal_Int32 nHierarchyDepth, const rtl::Reference<::chart::ChartModel>& xChartDoc )
83{
84 ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
85 for (auto const& child : aChildren)
86 {
87 ListBoxEntryData aEntry;
88 aEntry.OID = child;
89 aEntry.UIName = ObjectNameProvider::getNameForCID( child.getObjectCID(), xChartDoc );
90 aEntry.nHierarchyDepth = nHierarchyDepth;
91 rEntries.push_back(aEntry);
92 lcl_addObjectsToList( rHierarchy, child, rEntries, nHierarchyDepth+1, xChartDoc );
93 }
94}
95
97{
98 m_xChartController = xChartController.get();
99}
100
102{
103 m_xWidget->clear();
104 m_aEntries.clear();
105
107 if( xChartController.is() )
108 {
109 ObjectIdentifier aSelectedOID( xChartController->getSelection() );
110 OUString aSelectedCID = aSelectedOID.getObjectCID();
111
112 rtl::Reference<::chart::ChartModel> xChartDoc = xChartController->getChartModel();
113 ObjectType eType( aSelectedOID.getObjectType() );
114 bool bAddSelectionToList = false;
116 bAddSelectionToList = true;
117
119 rtl::Reference< ChartModel > xFact = xChartController->getChartModel();
120 if( xFact.is() )
121 xChartView = xFact->createInstance( CHART_VIEW_SERVICE_NAME );
122 ExplicitValueProvider* pExplicitValueProvider = nullptr; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
123 ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
125
126 if( bAddSelectionToList )
127 {
128 if ( aSelectedOID.isAutoGeneratedObject() )
129 {
131 std::vector< ListBoxEntryData >::iterator aIt = std::find_if(m_aEntries.begin(), m_aEntries.end(),
132 [&aSeriesCID](const ListBoxEntryData& rEntry) { return rEntry.OID.getObjectCID().match(aSeriesCID); });
133 if (aIt != m_aEntries.end())
134 {
135 ListBoxEntryData aEntry;
136 aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
137 aEntry.OID = aSelectedOID;
138 ++aIt;
139 if( aIt != m_aEntries.end() )
140 m_aEntries.insert(aIt, aEntry);
141 else
142 m_aEntries.push_back( aEntry );
143 }
144 }
145 else if ( aSelectedOID.isAdditionalShape() )
146 {
147 ListBoxEntryData aEntry;
148 SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
149 OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
150 aEntry.UIName = ( aName.isEmpty() ? SchResId( STR_OBJECT_SHAPE ) : aName );
151 aEntry.OID = aSelectedOID;
152 m_aEntries.push_back( aEntry );
153 }
154 }
155
156 m_xWidget->freeze();
157 sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
158 sal_uInt16 nN=0;
159 for (auto const& entry : m_aEntries)
160 {
161 // tdf#152087 strip any newlines from the entry
162 m_xWidget->append_text(entry.UIName.replaceAll("\n", " "));
163 if ( !bSelectionFound && aSelectedOID == entry.OID )
164 {
165 nEntryPosToSelect = nN;
166 bSelectionFound = true;
167 }
168 ++nN;
169 }
170 m_xWidget->thaw();
171
172 if( bSelectionFound )
173 m_xWidget->set_active(nEntryPosToSelect);
174 }
175 m_xWidget->save_value(); //remind current selection pos
176}
177
179{
180 if ( !m_bReleaseFocus )
181 {
182 m_bReleaseFocus = true;
183 return;
184 }
185
188 if ( xFrame.is() && xFrame->getContainerWindow().is() )
189 xFrame->getContainerWindow()->setFocus();
190}
191
192IMPL_LINK(SelectorListBox, SelectHdl, weld::ComboBox&, rComboBox, void)
193{
194 if (rComboBox.changed_by_direct_pick())
195 {
196 const sal_Int32 nPos = rComboBox.get_active();
197 if (o3tl::make_unsigned(nPos) < m_aEntries.size())
198 {
199 ObjectIdentifier aOID = m_aEntries[nPos].OID;
201 if( xController.is() )
202 xController->select( aOID.getAny() );
203 }
204 ReleaseFocus_Impl();
205 }
206}
207
208IMPL_LINK(SelectorListBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
209{
210 bool bHandled = false;
211
212 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
213
214 switch ( nCode )
215 {
216 case KEY_RETURN:
217 case KEY_TAB:
218 {
219 if ( nCode == KEY_TAB )
220 m_bReleaseFocus = false;
221 else
222 bHandled = true;
223 SelectHdl(*m_xWidget);
224 break;
225 }
226
227 case KEY_ESCAPE:
228 m_xWidget->set_active_text(m_xWidget->get_saved_value()); //restore saved selection
229 ReleaseFocus_Impl();
230 break;
231 }
232
233 return bHandled || ChildKeyInput(rKEvt);
234}
235
237{
238 if (m_xWidget && !m_xWidget->has_focus()) // comboboxes can be comprised of multiple widgets, ensure all have lost focus
239 m_xWidget->set_active_text(m_xWidget->get_saved_value());
240}
241
243{
244 return lcl_aServiceName;
245}
246
247sal_Bool SAL_CALL ElementSelectorToolbarController::supportsService( const OUString& rServiceName )
248{
249 return cppu::supportsService(this, rServiceName);
250}
251
252css::uno::Sequence< OUString > SAL_CALL ElementSelectorToolbarController::getSupportedServiceNames()
253{
254 return { "com.sun.star.frame.ToolbarController" };
255}
257{
258}
260{
261}
262// XInterface
264{
265 Any aReturn = ToolboxController::queryInterface(_rType);
266 if (!aReturn.hasValue())
268 return aReturn;
269}
271{
272 ToolboxController::acquire();
273}
275{
276 ToolboxController::release();
277}
278void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent )
279{
281 {
282 SolarMutexGuard aSolarMutexGuard;
283 if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
284 {
285 Reference< frame::XController > xChartController;
286 rEvent.State >>= xChartController;
287 ::chart::ChartController* pController = dynamic_cast<::chart::ChartController*>(xChartController.get());
288 assert(!xChartController || pController);
289 m_apSelectorListBox->SetChartController( pController );
290 m_apSelectorListBox->UpdateChartElementsListAndSelection();
291 }
292 }
293}
295{
298 {
299 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow( xParent );
300 if( pParent )
301 {
303 }
304 }
306 xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
307 return xItemWindow;
308}
309
310} // chart2
311
312extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
314 css::uno::Sequence<css::uno::Any> const &)
315{
316 return cppu::acquire(new chart::ElementSelectorToolbarController );
317}
318
319/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nPos
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
std::unique_ptr< weld::Image > m_xWidget
virtual void dispose() override
std::unique_ptr< weld::Container > m_xContainer
void InitControlBase(weld::Widget *pWidget)
virtual const OUString & GetName() const
constexpr tools::Long Width() const
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
static SdrObject * getSdrObject(const css::uno::Reference< css::drawing::XShape > &xShape)
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
VclPtr< SelectorListBox > m_apSelectorListBox
virtual void SAL_CALL release() noexcept override
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createItemWindow(const css::uno::Reference< css::awt::XWindow > &Parent) override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL acquire() noexcept override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
static ObjectIdentifier getRootNodeOID()
std::vector< ObjectIdentifier > tChildContainer
const tChildContainer & getChildren(const ObjectIdentifier &rParent) const
static OUString createClassifiedIdentifierForParticle(std::u16string_view rParticle)
static OUString getSeriesParticleFromCID(std::u16string_view rCID)
css::uno::Any getAny() const
const OUString & getObjectCID() const
static ObjectType getObjectType(std::u16string_view rCID)
const css::uno::Reference< css::drawing::XShape > & getAdditionalShape() const
static OUString getNameForCID(std::u16string_view rObjectCID, const rtl::Reference<::chart::ChartModel > &xChartDocument)
virtual void dispose() override
std::vector< ListBoxEntryData > m_aEntries
virtual ~SelectorListBox() override
unotools::WeakReference<::chart::ChartController > m_xChartController
void SetChartController(const rtl::Reference< ::chart::ChartController > &xChartController)
SelectorListBox(vcl::Window *pParent)
std::unique_ptr< weld::ComboBox > m_xWidget
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &rType) SAL_OVERRIDE
Point LogicToPixel(const Point &rLogicPt) const
virtual void SetSizePixel(const Size &rNewSize)
float u
DocumentType eType
OUString aName
constexpr sal_uInt16 KEY_RETURN
constexpr sal_uInt16 KEY_ESCAPE
constexpr sal_uInt16 KEY_TAB
@ OBJECTTYPE_DATA_POINT
@ OBJECTTYPE_DATA_LABEL
static void lcl_addObjectsToList(const ObjectHierarchy &rHierarchy, const ObjectIdentifier &rParent, std::vector< ListBoxEntryData > &rEntries, const sal_Int32 nHierarchyDepth, const rtl::Reference<::chart::ChartModel > &xChartDoc)
OUString OOO_DLLPUBLIC_CHARTTOOLS SchResId(TranslateId aId)
Definition: ResId.cxx:24
IMPL_LINK(StackingResourceGroup, StackingChangeHdl, weld::Toggleable &, rRadio, void)
IMPL_LINK_NOARG(SplinePropertiesDialog, SplineTypeListBoxHdl, weld::ComboBox &, void)
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
constexpr OUStringLiteral CHART_VIEW_SERVICE_NAME
Reference< XController > xController
Reference< XFrame > xFrame
unsigned char sal_Bool