1de7b3f82SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3de7b3f82SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4de7b3f82SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5de7b3f82SAndrew Rist  * distributed with this work for additional information
6de7b3f82SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7de7b3f82SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8de7b3f82SAndrew Rist  * "License"); you may not use this file except in compliance
9de7b3f82SAndrew Rist  * with the License.  You may obtain a copy of the License at
10de7b3f82SAndrew Rist  *
11de7b3f82SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12de7b3f82SAndrew Rist  *
13de7b3f82SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14de7b3f82SAndrew Rist  * software distributed under the License is distributed on an
15de7b3f82SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16de7b3f82SAndrew Rist  * KIND, either express or implied.  See the License for the
17de7b3f82SAndrew Rist  * specific language governing permissions and limitations
18de7b3f82SAndrew Rist  * under the License.
19de7b3f82SAndrew Rist  *
20de7b3f82SAndrew Rist  *************************************************************/
21de7b3f82SAndrew Rist 
22de7b3f82SAndrew Rist 
23cdf0e10cSrcweir #ifndef CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
24cdf0e10cSrcweir #define CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "WrappedProperty.hxx"
27cdf0e10cSrcweir #include "Chart2ModelContact.hxx"
28cdf0e10cSrcweir #include "macros.hxx"
29cdf0e10cSrcweir #include "DiagramHelper.hxx"
30cdf0e10cSrcweir #include <com/sun/star/chart2/XDataSeries.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
33cdf0e10cSrcweir #include <vector>
34cdf0e10cSrcweir 
35*2eb4bbd2SHerbert Dürr // this operator is not defined by default
operator !=(const::com::sun::star::awt::Size & rSize1,const::com::sun::star::awt::Size & rSize2)36*2eb4bbd2SHerbert Dürr inline bool operator!=( const ::com::sun::star::awt::Size& rSize1, const ::com::sun::star::awt::Size& rSize2 )
37*2eb4bbd2SHerbert Dürr {
38*2eb4bbd2SHerbert Dürr     return (rSize1.Width != rSize2.Width) || (rSize1.Height != rSize2.Height);
39*2eb4bbd2SHerbert Dürr }
40*2eb4bbd2SHerbert Dürr 
41cdf0e10cSrcweir //.............................................................................
42*2eb4bbd2SHerbert Dürr 
43cdf0e10cSrcweir namespace chart
44cdf0e10cSrcweir {
45cdf0e10cSrcweir namespace wrapper
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir enum tSeriesOrDiagramPropertyType
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     DATA_SERIES,
51cdf0e10cSrcweir     DIAGRAM
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //PROPERTYTYPE is the type of the outer property
55cdf0e10cSrcweir 
56cdf0e10cSrcweir template< typename PROPERTYTYPE >
57cdf0e10cSrcweir class WrappedSeriesOrDiagramProperty : public WrappedProperty
58cdf0e10cSrcweir {
59cdf0e10cSrcweir public:
60cdf0e10cSrcweir     virtual PROPERTYTYPE getValueFromSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet ) const =0;
61cdf0e10cSrcweir     virtual void setValueToSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet, PROPERTYTYPE aNewValue ) const =0;
62cdf0e10cSrcweir 
WrappedSeriesOrDiagramProperty(const::rtl::OUString & rName,const::com::sun::star::uno::Any & rDefaulValue,::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact,tSeriesOrDiagramPropertyType ePropertyType)63cdf0e10cSrcweir     explicit WrappedSeriesOrDiagramProperty( const ::rtl::OUString& rName, const ::com::sun::star::uno::Any& rDefaulValue
64cdf0e10cSrcweir         , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact
65cdf0e10cSrcweir         , tSeriesOrDiagramPropertyType ePropertyType )
66cdf0e10cSrcweir             : WrappedProperty(rName,::rtl::OUString())
67cdf0e10cSrcweir             , m_spChart2ModelContact(spChart2ModelContact)
68cdf0e10cSrcweir             , m_aOuterValue(rDefaulValue)
69cdf0e10cSrcweir             , m_aDefaultValue(rDefaulValue)
70cdf0e10cSrcweir             , m_ePropertyType( ePropertyType )
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir     }
~WrappedSeriesOrDiagramProperty()73cdf0e10cSrcweir     virtual ~WrappedSeriesOrDiagramProperty() {};
74cdf0e10cSrcweir 
detectInnerValue(PROPERTYTYPE & rValue,bool & rHasAmbiguousValue) const75cdf0e10cSrcweir     bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         bool bHasDetectableInnerValue = false;
78cdf0e10cSrcweir         rHasAmbiguousValue = false;
79cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM &&
80cdf0e10cSrcweir             m_spChart2ModelContact.get() )
81cdf0e10cSrcweir         {
82cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
83cdf0e10cSrcweir                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
84cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
85cdf0e10cSrcweir                     aSeriesVector.begin();
86cdf0e10cSrcweir             for( ; aIter != aSeriesVector.end(); aIter++ )
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 PROPERTYTYPE aCurValue = getValueFromSeries( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >::query( *aIter ) );
89cdf0e10cSrcweir                 if( !bHasDetectableInnerValue )
90cdf0e10cSrcweir                     rValue = aCurValue;
91cdf0e10cSrcweir                 else
92cdf0e10cSrcweir                 {
93cdf0e10cSrcweir                     if( rValue != aCurValue )
94cdf0e10cSrcweir                     {
95cdf0e10cSrcweir                         rHasAmbiguousValue = true;
96cdf0e10cSrcweir                         break;
97cdf0e10cSrcweir                     }
98cdf0e10cSrcweir                     else
99cdf0e10cSrcweir                         rValue = aCurValue;
100cdf0e10cSrcweir                 }
101cdf0e10cSrcweir                 bHasDetectableInnerValue = true;
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         return bHasDetectableInnerValue;
105cdf0e10cSrcweir     }
setInnerValue(PROPERTYTYPE aNewValue) const106cdf0e10cSrcweir     void setInnerValue( PROPERTYTYPE aNewValue ) const
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM &&
109cdf0e10cSrcweir             m_spChart2ModelContact.get() )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
112cdf0e10cSrcweir                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
113cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
114cdf0e10cSrcweir                     aSeriesVector.begin();
115cdf0e10cSrcweir             for( ; aIter != aSeriesVector.end(); aIter++ )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xSeriesPropertySet( *aIter, ::com::sun::star::uno::UNO_QUERY );
118cdf0e10cSrcweir                 if( xSeriesPropertySet.is() )
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     setValueToSeries( xSeriesPropertySet, aNewValue );
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
setPropertyValue(const::com::sun::star::uno::Any & rOuterValue,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & xInnerPropertySet) const125cdf0e10cSrcweir     virtual void setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
126cdf0e10cSrcweir                     throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         PROPERTYTYPE aNewValue = PROPERTYTYPE();
129cdf0e10cSrcweir         if( ! (rOuterValue >>= aNewValue) )
130cdf0e10cSrcweir             throw ::com::sun::star::lang::IllegalArgumentException( C2U("statistic property requires different type"), 0, 0 );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM )
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             m_aOuterValue = rOuterValue;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir             bool bHasAmbiguousValue = false;
137cdf0e10cSrcweir             PROPERTYTYPE aOldValue = PROPERTYTYPE();
138cdf0e10cSrcweir             if( detectInnerValue( aOldValue, bHasAmbiguousValue ) )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 if( bHasAmbiguousValue || aNewValue != aOldValue )
141cdf0e10cSrcweir                     setInnerValue( aNewValue );
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir         else
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             setValueToSeries( xInnerPropertySet, aNewValue );
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
getPropertyValue(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & xInnerPropertySet) const150cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
151cdf0e10cSrcweir                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM )
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             bool bHasAmbiguousValue = false;
156cdf0e10cSrcweir             PROPERTYTYPE aValue;
157cdf0e10cSrcweir             if( detectInnerValue( aValue, bHasAmbiguousValue ) )
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 if(bHasAmbiguousValue)
160cdf0e10cSrcweir                     m_aOuterValue <<= m_aDefaultValue;
161cdf0e10cSrcweir                 else
162cdf0e10cSrcweir                     m_aOuterValue <<= aValue;
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir             return m_aOuterValue;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         else
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             ::com::sun::star::uno::Any aRet( m_aDefaultValue );
169cdf0e10cSrcweir             aRet <<= getValueFromSeries( xInnerPropertySet );
170cdf0e10cSrcweir             return aRet;
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir 
getPropertyDefault(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertyState> &) const174cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& /* xInnerPropertyState */ ) const
175cdf0e10cSrcweir                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         return m_aDefaultValue;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir protected:
181cdf0e10cSrcweir     ::boost::shared_ptr< Chart2ModelContact >  m_spChart2ModelContact;
182cdf0e10cSrcweir     mutable ::com::sun::star::uno::Any         m_aOuterValue;
183cdf0e10cSrcweir     ::com::sun::star::uno::Any                 m_aDefaultValue;
184cdf0e10cSrcweir     tSeriesOrDiagramPropertyType               m_ePropertyType;
185cdf0e10cSrcweir };
186cdf0e10cSrcweir 
187cdf0e10cSrcweir } //namespace wrapper
188cdf0e10cSrcweir } //namespace chart
189cdf0e10cSrcweir //.............................................................................
190cdf0e10cSrcweir 
191cdf0e10cSrcweir // CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
192cdf0e10cSrcweir #endif
193