LCOV - code coverage report
Current view: top level - chart2/source/controller/main - ElementSelector.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 116 177 65.5 %
Date: 2015-06-13 12:38:46 Functions: 21 25 84.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "macros.hxx"
      22             : #include "ObjectNameProvider.hxx"
      23             : #include "ObjectHierarchy.hxx"
      24             : #include "servicenames.hxx"
      25             : #include <chartview/ExplicitValueProvider.hxx>
      26             : #include "DrawViewWrapper.hxx"
      27             : #include "ResId.hxx"
      28             : #include "Strings.hrc"
      29             : 
      30             : #include <cppuhelper/supportsservice.hxx>
      31             : #include <toolkit/helper/vclunohelper.hxx>
      32             : #include <osl/mutex.hxx>
      33             : #include <vcl/svapp.hxx>
      34             : 
      35             : #include <com/sun/star/chart2/XChartDocument.hpp>
      36             : #include <com/sun/star/frame/XControlNotificationListener.hpp>
      37             : #include <com/sun/star/util/XURLTransformer.hpp>
      38             : #include <com/sun/star/view/XSelectionSupplier.hpp>
      39             : 
      40             : namespace chart
      41             : {
      42             : 
      43             : using namespace com::sun::star;
      44             : using namespace com::sun::star::uno;
      45             : using ::com::sun::star::uno::Any;
      46             : using ::com::sun::star::uno::Reference;
      47             : using ::com::sun::star::uno::Sequence;
      48             : 
      49             : namespace
      50             : {
      51             : static const char lcl_aServiceName[] = "com.sun.star.comp.chart.ElementSelectorToolbarController";
      52             : }
      53             : 
      54          17 : SelectorListBox::SelectorListBox( vcl::Window* pParent, WinBits nStyle )
      55             :     : ListBox( pParent, nStyle )
      56          17 :     , m_bReleaseFocus( true )
      57             : {
      58          17 : }
      59             : 
      60        8390 : void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectHierarchy::tOID & rParent, std::vector< ListBoxEntryData >& rEntries
      61             :                           , const sal_Int32 nHierarchyDepth, const Reference< chart2::XChartDocument >& xChartDoc )
      62             : {
      63        8390 :     ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
      64        8390 :     ObjectHierarchy::tChildContainer::const_iterator aIt( aChildren.begin());
      65       24350 :     while( aIt != aChildren.end() )
      66             :     {
      67        7570 :         ObjectHierarchy::tOID aOID = *aIt;
      68       15140 :         OUString aCID = aOID.getObjectCID();
      69       15140 :         ListBoxEntryData aEntry;
      70        7570 :         aEntry.OID = aOID;
      71        7570 :         aEntry.UIName += ObjectNameProvider::getNameForCID( aCID, xChartDoc );
      72        7570 :         aEntry.nHierarchyDepth = nHierarchyDepth;
      73        7570 :         rEntries.push_back(aEntry);
      74        7570 :         lcl_addObjectsToList( rHierarchy, aOID, rEntries, nHierarchyDepth+1, xChartDoc );
      75        7570 :         ++aIt;
      76       15960 :     }
      77        8390 : }
      78             : 
      79         820 : void SelectorListBox::SetChartController( const Reference< frame::XController >& xChartController )
      80             : {
      81         820 :     m_xChartController = xChartController;
      82         820 : }
      83             : 
      84         820 : void SelectorListBox::UpdateChartElementsListAndSelection()
      85             : {
      86         820 :     Clear();
      87         820 :     m_aEntries.clear();
      88             : 
      89         820 :     Reference< frame::XController > xChartController( m_xChartController );
      90         820 :     if( xChartController.is() )
      91             :     {
      92         820 :         Reference< view::XSelectionSupplier > xSelectionSupplier( xChartController, uno::UNO_QUERY);
      93        1640 :         ObjectHierarchy::tOID aSelectedOID;
      94        1640 :         OUString aSelectedCID;
      95         820 :         if( xSelectionSupplier.is() )
      96             :         {
      97         820 :             aSelectedOID = ObjectIdentifier( xSelectionSupplier->getSelection() );
      98         820 :             aSelectedCID = aSelectedOID.getObjectCID();
      99             :         }
     100             : 
     101        1640 :         Reference< chart2::XChartDocument > xChartDoc( xChartController->getModel(), uno::UNO_QUERY );
     102         820 :         ObjectType eType( aSelectedOID.getObjectType() );
     103         820 :         bool bAddSelectionToList = false;
     104         820 :         if ( eType == OBJECTTYPE_DATA_POINT || eType == OBJECTTYPE_DATA_LABEL || eType == OBJECTTYPE_SHAPE )
     105           0 :             bAddSelectionToList = true;
     106             : 
     107        1640 :         Reference< uno::XInterface > xChartView;
     108        1640 :         Reference< lang::XMultiServiceFactory > xFact( xChartController->getModel(), uno::UNO_QUERY );
     109         820 :         if( xFact.is() )
     110         820 :             xChartView = xFact->createInstance( CHART_VIEW_SERVICE_NAME );
     111         820 :         ExplicitValueProvider* pExplicitValueProvider = 0; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
     112        1640 :         ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
     113         820 :         lcl_addObjectsToList( aHierarchy, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries, 0, xChartDoc );
     114             : 
     115         820 :         std::vector< ListBoxEntryData >::iterator aIt( m_aEntries.begin() );
     116         820 :         if( bAddSelectionToList )
     117             :         {
     118           0 :             if ( aSelectedOID.isAutoGeneratedObject() )
     119             :             {
     120           0 :                 OUString aSeriesCID = ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID ) );
     121           0 :                 for( aIt = m_aEntries.begin(); aIt != m_aEntries.end(); ++aIt )
     122             :                 {
     123           0 :                     if( aIt->OID.getObjectCID().match( aSeriesCID ) )
     124             :                     {
     125           0 :                         ListBoxEntryData aEntry;
     126           0 :                         aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
     127           0 :                         aEntry.OID = aSelectedOID;
     128           0 :                         ++aIt;
     129           0 :                         if( aIt != m_aEntries.end() )
     130           0 :                             m_aEntries.insert(aIt, aEntry);
     131             :                         else
     132           0 :                             m_aEntries.push_back( aEntry );
     133           0 :                         break;
     134             :                     }
     135           0 :                 }
     136             :             }
     137           0 :             else if ( aSelectedOID.isAdditionalShape() )
     138             :             {
     139           0 :                 ListBoxEntryData aEntry;
     140           0 :                 SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
     141           0 :                 OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
     142           0 :                 aEntry.UIName = ( aName.isEmpty() ?  SCH_RESSTR( STR_OBJECT_SHAPE ) : aName );
     143           0 :                 aEntry.OID = aSelectedOID;
     144           0 :                 m_aEntries.push_back( aEntry );
     145             :             }
     146             :         }
     147             : 
     148         820 :         sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
     149         820 :         aIt = m_aEntries.begin();
     150        8390 :         for( sal_uInt16 nN=0; aIt != m_aEntries.end(); ++aIt, ++nN )
     151             :         {
     152        7570 :             InsertEntry( aIt->UIName );
     153        7570 :             if ( !bSelectionFound && aSelectedOID == aIt->OID )
     154             :             {
     155           0 :                 nEntryPosToSelect = nN;
     156           0 :                 bSelectionFound = true;
     157             :             }
     158             :         }
     159             : 
     160         820 :         if( bSelectionFound )
     161           0 :             SelectEntryPos(nEntryPosToSelect);
     162             : 
     163         820 :         sal_uInt16 nEntryCount = GetEntryCount();
     164         820 :         if( nEntryCount > 100 )
     165           0 :             nEntryCount = 100;
     166        1640 :         SetDropDownLineCount( nEntryCount );
     167             :     }
     168         820 :     SaveValue(); //remind current selection pos
     169         820 : }
     170             : 
     171           0 : void SelectorListBox::ReleaseFocus_Impl()
     172             : {
     173           0 :     if ( !m_bReleaseFocus )
     174             :     {
     175           0 :         m_bReleaseFocus = true;
     176           0 :         return;
     177             :     }
     178             : 
     179           0 :     Reference< frame::XController > xController( m_xChartController );
     180           0 :     Reference< frame::XFrame > xFrame( xController->getFrame() );
     181           0 :     if ( xFrame.is() && xFrame->getContainerWindow().is() )
     182           0 :         xFrame->getContainerWindow()->setFocus();
     183             : }
     184             : 
     185           0 : void SelectorListBox::Select()
     186             : {
     187           0 :     ListBox::Select();
     188             : 
     189           0 :     if ( !IsTravelSelect() )
     190             :     {
     191           0 :         sal_uInt16 nPos = GetSelectEntryPos();
     192           0 :         if( nPos < m_aEntries.size() )
     193             :         {
     194           0 :             ObjectHierarchy::tOID aOID = m_aEntries[nPos].OID;
     195           0 :             Reference< view::XSelectionSupplier > xSelectionSupplier( m_xChartController.get(), uno::UNO_QUERY );
     196           0 :             if( xSelectionSupplier.is() )
     197           0 :                 xSelectionSupplier->select( aOID.getAny() );
     198             :         }
     199           0 :         ReleaseFocus_Impl();
     200             :     }
     201           0 : }
     202             : 
     203          34 : bool SelectorListBox::Notify( NotifyEvent& rNEvt )
     204             : {
     205          34 :     bool nHandled = false;
     206             : 
     207          34 :     if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
     208             :     {
     209           0 :         sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
     210             : 
     211           0 :         switch ( nCode )
     212             :         {
     213             :             case KEY_RETURN:
     214             :             case KEY_TAB:
     215             :             {
     216           0 :                 if ( KEY_TAB == nCode )
     217           0 :                     m_bReleaseFocus = false;
     218             :                 else
     219           0 :                     nHandled = true;
     220           0 :                 Select();
     221           0 :                 break;
     222             :             }
     223             : 
     224             :             case KEY_ESCAPE:
     225           0 :                 SelectEntryPos( GetSavedValue() ); //restore saved selection
     226           0 :                 ReleaseFocus_Impl();
     227           0 :                 break;
     228             :         }
     229             :     }
     230          34 :     else if ( MouseNotifyEvent::LOSEFOCUS == rNEvt.GetType() )
     231             :     {
     232           0 :         if ( !HasFocus() )
     233           0 :             SelectEntryPos( GetSavedValue() );
     234             :     }
     235             : 
     236          34 :     return nHandled || ListBox::Notify( rNEvt );
     237             : }
     238             : 
     239           0 : Reference< ::com::sun::star::accessibility::XAccessible > SelectorListBox::CreateAccessible()
     240             : {
     241           0 :     UpdateChartElementsListAndSelection();
     242           0 :     return ListBox::CreateAccessible();
     243             : }
     244             : 
     245             : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
     246           1 : OUString SAL_CALL ElementSelectorToolbarController::getImplementationName()
     247             :     throw( css::uno::RuntimeException, std::exception )
     248             : {
     249           1 :     return getImplementationName_Static();
     250             : }
     251             : 
     252           1 : OUString ElementSelectorToolbarController::getImplementationName_Static()
     253             : {
     254           1 :     return OUString(lcl_aServiceName);
     255             : }
     256             : 
     257           0 : sal_Bool SAL_CALL ElementSelectorToolbarController::supportsService( const OUString& rServiceName )
     258             :     throw( css::uno::RuntimeException, std::exception )
     259             : {
     260           0 :     return cppu::supportsService(this, rServiceName);
     261             : }
     262             : 
     263           1 : css::uno::Sequence< OUString > SAL_CALL ElementSelectorToolbarController::getSupportedServiceNames()
     264             :     throw( css::uno::RuntimeException, std::exception )
     265             : {
     266           1 :     return getSupportedServiceNames_Static();
     267             : }
     268             : 
     269           1 : Sequence< OUString > ElementSelectorToolbarController::getSupportedServiceNames_Static()
     270             : {
     271           1 :     Sequence< OUString > aServices(1);
     272           1 :     aServices[ 0 ] = "com.sun.star.frame.ToolbarController";
     273           1 :     return aServices;
     274             : }
     275          18 : ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference< uno::XComponentContext > & xContext )
     276          18 :     : m_xCC( xContext )
     277             : {
     278          18 : }
     279          36 : ElementSelectorToolbarController::~ElementSelectorToolbarController()
     280             : {
     281          36 : }
     282             : // XInterface
     283         994 : Any SAL_CALL ElementSelectorToolbarController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
     284             : {
     285         994 :     Any aReturn = ToolboxController::queryInterface(_rType);
     286         994 :     if (!aReturn.hasValue())
     287          18 :         aReturn = ElementSelectorToolbarController_BASE::queryInterface(_rType);
     288         994 :     return aReturn;
     289             : }
     290        2868 : void SAL_CALL ElementSelectorToolbarController::acquire() throw ()
     291             : {
     292        2868 :     ToolboxController::acquire();
     293        2868 : }
     294        2868 : void SAL_CALL ElementSelectorToolbarController::release() throw ()
     295             : {
     296        2868 :     ToolboxController::release();
     297        2868 : }
     298          17 : void SAL_CALL ElementSelectorToolbarController::initialize( const Sequence< Any >& rArguments ) throw (Exception, RuntimeException, std::exception)
     299             : {
     300          17 :     ToolboxController::initialize(rArguments);
     301          17 : }
     302         820 : void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent ) throw ( RuntimeException, std::exception )
     303             : {
     304         820 :     if( m_apSelectorListBox.get() )
     305             :     {
     306         820 :         SolarMutexGuard aSolarMutexGuard;
     307         820 :         if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
     308             :         {
     309         820 :             Reference< frame::XController > xChartController;
     310         820 :             rEvent.State >>= xChartController;
     311         820 :             m_apSelectorListBox->SetChartController( xChartController );
     312         820 :             m_apSelectorListBox->UpdateChartElementsListAndSelection();
     313         820 :         }
     314             :     }
     315         820 : }
     316          17 : uno::Reference< awt::XWindow > SAL_CALL ElementSelectorToolbarController::createItemWindow( const uno::Reference< awt::XWindow >& xParent )
     317             :         throw (uno::RuntimeException, std::exception)
     318             : {
     319          17 :     uno::Reference< awt::XWindow > xItemWindow;
     320          17 :     if( !m_apSelectorListBox.get() )
     321             :     {
     322          17 :         vcl::Window* pParent = VCLUnoHelper::GetWindow( xParent );
     323          17 :         if( pParent )
     324             :         {
     325          17 :             m_apSelectorListBox.reset( VclPtr<SelectorListBox>::Create( pParent, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER ) );
     326          17 :             ::Size aLogicalSize( 95, 160 );
     327          17 :             ::Size aPixelSize = m_apSelectorListBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
     328          17 :             m_apSelectorListBox->SetSizePixel( aPixelSize );
     329          17 :             m_apSelectorListBox->SetDropDownLineCount( 5 );
     330             :         }
     331             :     }
     332          17 :     if( m_apSelectorListBox.get() )
     333          17 :         xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
     334          17 :     return xItemWindow;
     335             : }
     336             : 
     337             : } // chart2
     338             : 
     339             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     340          18 : com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *context,
     341             :                                                                             css::uno::Sequence<css::uno::Any> const &)
     342             : {
     343          18 :     return cppu::acquire(new chart::ElementSelectorToolbarController(context));
     344          57 : }
     345             : 
     346             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11