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

Generated by: LCOV version 1.10