Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / main / ElementSelector.cxx
bloba7ce1843d8f1136cdf6bdef519c3888af51ad3a6
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ElementSelector.hxx"
21 #include <ObjectNameProvider.hxx>
22 #include <ObjectHierarchy.hxx>
23 #include <servicenames.hxx>
24 #include <chartview/ExplicitValueProvider.hxx>
25 #include <DrawViewWrapper.hxx>
26 #include <ResId.hxx>
27 #include <strings.hrc>
28 #include <ObjectIdentifier.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <vcl/svapp.hxx>
34 #include <com/sun/star/chart2/XChartDocument.hpp>
35 #include <com/sun/star/util/XURLTransformer.hpp>
36 #include <com/sun/star/view/XSelectionSupplier.hpp>
38 namespace chart
41 using namespace com::sun::star;
42 using namespace com::sun::star::uno;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
47 namespace
49 static const char lcl_aServiceName[] = "com.sun.star.comp.chart.ElementSelectorToolbarController";
52 SelectorListBox::SelectorListBox( vcl::Window* pParent, WinBits nStyle )
53 : ListBox( pParent, nStyle )
54 , m_bReleaseFocus( true )
58 void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectIdentifier & rParent, std::vector< ListBoxEntryData >& rEntries
59 , const sal_Int32 nHierarchyDepth, const Reference< chart2::XChartDocument >& xChartDoc )
61 ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
62 for (auto const& child : aChildren)
64 ObjectIdentifier aOID = child;
65 OUString aCID = aOID.getObjectCID();
66 ListBoxEntryData aEntry;
67 aEntry.OID = aOID;
68 aEntry.UIName += ObjectNameProvider::getNameForCID( aCID, xChartDoc );
69 aEntry.nHierarchyDepth = nHierarchyDepth;
70 rEntries.push_back(aEntry);
71 lcl_addObjectsToList( rHierarchy, aOID, rEntries, nHierarchyDepth+1, xChartDoc );
75 void SelectorListBox::SetChartController( const Reference< frame::XController >& xChartController )
77 m_xChartController = xChartController;
80 void SelectorListBox::UpdateChartElementsListAndSelection()
82 Clear();
83 m_aEntries.clear();
85 Reference< frame::XController > xChartController( m_xChartController );
86 if( xChartController.is() )
88 Reference< view::XSelectionSupplier > xSelectionSupplier( xChartController, uno::UNO_QUERY);
89 ObjectIdentifier aSelectedOID;
90 OUString aSelectedCID;
91 if( xSelectionSupplier.is() )
93 aSelectedOID = ObjectIdentifier( xSelectionSupplier->getSelection() );
94 aSelectedCID = aSelectedOID.getObjectCID();
97 Reference< chart2::XChartDocument > xChartDoc( xChartController->getModel(), uno::UNO_QUERY );
98 ObjectType eType( aSelectedOID.getObjectType() );
99 bool bAddSelectionToList = false;
100 if ( eType == OBJECTTYPE_DATA_POINT || eType == OBJECTTYPE_DATA_LABEL || eType == OBJECTTYPE_SHAPE )
101 bAddSelectionToList = true;
103 Reference< uno::XInterface > xChartView;
104 Reference< lang::XMultiServiceFactory > xFact( xChartController->getModel(), uno::UNO_QUERY );
105 if( xFact.is() )
106 xChartView = xFact->createInstance( CHART_VIEW_SERVICE_NAME );
107 ExplicitValueProvider* pExplicitValueProvider = nullptr; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
108 ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
109 lcl_addObjectsToList( aHierarchy, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries, 0, xChartDoc );
111 std::vector< ListBoxEntryData >::iterator aIt( m_aEntries.begin() );
112 if( bAddSelectionToList )
114 if ( aSelectedOID.isAutoGeneratedObject() )
116 OUString aSeriesCID = ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID ) );
117 for( aIt = m_aEntries.begin(); aIt != m_aEntries.end(); ++aIt )
119 if( aIt->OID.getObjectCID().match( aSeriesCID ) )
121 ListBoxEntryData aEntry;
122 aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
123 aEntry.OID = aSelectedOID;
124 ++aIt;
125 if( aIt != m_aEntries.end() )
126 m_aEntries.insert(aIt, aEntry);
127 else
128 m_aEntries.push_back( aEntry );
129 break;
133 else if ( aSelectedOID.isAdditionalShape() )
135 ListBoxEntryData aEntry;
136 SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
137 OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
138 aEntry.UIName = ( aName.isEmpty() ? SchResId( STR_OBJECT_SHAPE ) : aName );
139 aEntry.OID = aSelectedOID;
140 m_aEntries.push_back( aEntry );
144 sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
145 sal_uInt16 nN=0;
146 for (auto const& entry : m_aEntries)
148 InsertEntry(entry.UIName);
149 if ( !bSelectionFound && aSelectedOID == entry.OID )
151 nEntryPosToSelect = nN;
152 bSelectionFound = true;
154 ++nN;
157 if( bSelectionFound )
158 SelectEntryPos(nEntryPosToSelect);
160 sal_Int32 nEntryCount = GetEntryCount();
161 if( nEntryCount > 100 )
162 nEntryCount = 100;
163 SetDropDownLineCount( nEntryCount );
165 SaveValue(); //remind current selection pos
168 void SelectorListBox::ReleaseFocus_Impl()
170 if ( !m_bReleaseFocus )
172 m_bReleaseFocus = true;
173 return;
176 Reference< frame::XController > xController( m_xChartController );
177 Reference< frame::XFrame > xFrame( xController->getFrame() );
178 if ( xFrame.is() && xFrame->getContainerWindow().is() )
179 xFrame->getContainerWindow()->setFocus();
182 void SelectorListBox::Select()
184 ListBox::Select();
186 if ( !IsTravelSelect() )
188 const sal_Int32 nPos = GetSelectedEntryPos();
189 if( static_cast<size_t>(nPos) < m_aEntries.size() )
191 ObjectIdentifier aOID = m_aEntries[nPos].OID;
192 Reference< view::XSelectionSupplier > xSelectionSupplier( m_xChartController.get(), uno::UNO_QUERY );
193 if( xSelectionSupplier.is() )
194 xSelectionSupplier->select( aOID.getAny() );
196 ReleaseFocus_Impl();
200 bool SelectorListBox::EventNotify( NotifyEvent& rNEvt )
202 bool bHandled = false;
204 if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
206 sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
208 switch ( nCode )
210 case KEY_RETURN:
211 case KEY_TAB:
213 if ( nCode == KEY_TAB )
214 m_bReleaseFocus = false;
215 else
216 bHandled = true;
217 Select();
218 break;
221 case KEY_ESCAPE:
222 SelectEntryPos( GetSavedValue() ); //restore saved selection
223 ReleaseFocus_Impl();
224 break;
227 else if ( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
229 if ( !HasFocus() )
230 SelectEntryPos( GetSavedValue() );
233 return bHandled || ListBox::EventNotify(rNEvt);
236 Reference< css::accessibility::XAccessible > SelectorListBox::CreateAccessible()
238 UpdateChartElementsListAndSelection();
239 return ListBox::CreateAccessible();
242 OUString SAL_CALL ElementSelectorToolbarController::getImplementationName()
244 return OUString(lcl_aServiceName);
247 sal_Bool SAL_CALL ElementSelectorToolbarController::supportsService( const OUString& rServiceName )
249 return cppu::supportsService(this, rServiceName);
252 css::uno::Sequence< OUString > SAL_CALL ElementSelectorToolbarController::getSupportedServiceNames()
254 return { "com.sun.star.frame.ToolbarController" };
256 ElementSelectorToolbarController::ElementSelectorToolbarController()
259 ElementSelectorToolbarController::~ElementSelectorToolbarController()
262 // XInterface
263 Any SAL_CALL ElementSelectorToolbarController::queryInterface( const Type& _rType )
265 Any aReturn = ToolboxController::queryInterface(_rType);
266 if (!aReturn.hasValue())
267 aReturn = ElementSelectorToolbarController_BASE::queryInterface(_rType);
268 return aReturn;
270 void SAL_CALL ElementSelectorToolbarController::acquire() throw ()
272 ToolboxController::acquire();
274 void SAL_CALL ElementSelectorToolbarController::release() throw ()
276 ToolboxController::release();
278 void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent )
280 if( m_apSelectorListBox.get() )
282 SolarMutexGuard aSolarMutexGuard;
283 if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
285 Reference< frame::XController > xChartController;
286 rEvent.State >>= xChartController;
287 m_apSelectorListBox->SetChartController( xChartController );
288 m_apSelectorListBox->UpdateChartElementsListAndSelection();
292 uno::Reference< awt::XWindow > SAL_CALL ElementSelectorToolbarController::createItemWindow( const uno::Reference< awt::XWindow >& xParent )
294 uno::Reference< awt::XWindow > xItemWindow;
295 if( !m_apSelectorListBox.get() )
297 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow( xParent );
298 if( pParent )
300 m_apSelectorListBox.reset( VclPtr<SelectorListBox>::Create( pParent, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER ) );
301 ::Size aLogicalSize( 75, 160 );
302 ::Size aPixelSize = m_apSelectorListBox->LogicToPixel(aLogicalSize, MapMode(MapUnit::MapAppFont));
303 m_apSelectorListBox->SetSizePixel( aPixelSize );
304 m_apSelectorListBox->SetDropDownLineCount( 5 );
307 if( m_apSelectorListBox.get() )
308 xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
309 return xItemWindow;
312 } // chart2
314 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
315 com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *,
316 css::uno::Sequence<css::uno::Any> const &)
318 return cppu::acquire(new chart::ElementSelectorToolbarController );
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */