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