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 
24 #ifndef OOX_XLS_CONDFORMATBUFFER_HXX
25 #define OOX_XLS_CONDFORMATBUFFER_HXX
26 
27 #include <com/sun/star/sheet/ConditionOperator.hpp>
28 #include "oox/xls/formulaparser.hxx"
29 #include "oox/xls/worksheethelper.hxx"
30 
31 namespace com { namespace sun { namespace star {
32     namespace sheet { class XSheetConditionalEntries; }
33 } } }
34 
35 namespace oox {
36 namespace xls {
37 
38 // ============================================================================
39 
40 /** Model for a single rule in a conditional formatting. */
41 struct CondFormatRuleModel
42 {
43     typedef ::std::vector< ApiTokenSequence > ApiTokenSequenceVector;
44 
45     ApiTokenSequenceVector maFormulas;      /// Formulas for rule conditions.
46     ::rtl::OUString     maText;             /// Text for 'contains' rules.
47     sal_Int32           mnPriority;         /// Priority of this rule.
48     sal_Int32           mnType;             /// Type of the rule.
49     sal_Int32           mnOperator;         /// In cell-is rules: Comparison operator.
50     sal_Int32           mnTimePeriod;       /// In time-period rules: Type of time period.
51     sal_Int32           mnRank;             /// In top-10 rules: True = bottom, false = top.
52     sal_Int32           mnStdDev;           /// In average rules: Number of std deviations.
53     sal_Int32           mnDxfId;            /// Differential formatting identifier.
54     bool                mbStopIfTrue;       /// True = stop evaluating rules, if this rule is true.
55     bool                mbBottom;           /// In top-10 rules: True = bottom, false = top.
56     bool                mbPercent;          /// In top-10 rules: True = percent, false = rank.
57     bool                mbAboveAverage;     /// In average rules: True = above average, false = below.
58     bool                mbEqualAverage;     /// In average rules: True = include average, false = exclude.
59 
60     explicit            CondFormatRuleModel();
61 
62     /** Sets the passed BIFF operator for condition type cellIs. */
63     void                setBiffOperator( sal_Int32 nOperator );
64 
65     /** Sets the passed BIFF12 text comparison type and operator. */
66     void                setBiff12TextType( sal_Int32 nOperator );
67 };
68 
69 // ============================================================================
70 
71 class CondFormat;
72 
73 /** Represents a single rule in a conditional formatting. */
74 class CondFormatRule : public WorksheetHelper
75 {
76 public:
77     explicit            CondFormatRule( const CondFormat& rCondFormat );
78 
79     /** Imports rule settings from the cfRule element. */
80     void                importCfRule( const AttributeList& rAttribs );
81     /** Appends a new condition formula string. */
82     void                appendFormula( const ::rtl::OUString& rFormula );
83 
84     /** Imports rule settings from a CFRULE record. */
85     void                importCfRule( SequenceInputStream& rStrm );
86 
87     /** Imports rule settings from a CFRULE record. */
88     void                importCfRule( BiffInputStream& rStrm, sal_Int32 nPriority );
89 
90     /** Creates a conditional formatting rule in the Calc document. */
91     void                finalizeImport(
92                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XSheetConditionalEntries >& rxEntries );
93 
94     /** Returns the priority of this rule. */
getPriority() const95     inline sal_Int32    getPriority() const { return maModel.mnPriority; }
96 
97 private:
98     const CondFormat&   mrCondFormat;
99     CondFormatRuleModel maModel;
100 };
101 
102 typedef ::boost::shared_ptr< CondFormatRule > CondFormatRuleRef;
103 
104 // ============================================================================
105 
106 /** Model for a conditional formatting object. */
107 struct CondFormatModel
108 {
109     ApiCellRangeList    maRanges;           /// Cell ranges for this conditional format.
110     bool                mbPivot;            /// Conditional formatting belongs to pivot table.
111 
112     explicit            CondFormatModel();
113 };
114 
115 // ============================================================================
116 
117 /** Represents a conditional formatting object with a list of affected cell ranges. */
118 class CondFormat : public WorksheetHelper
119 {
120 public:
121     explicit            CondFormat( const WorksheetHelper& rHelper );
122 
123     /** Imports settings from the conditionalFormatting element. */
124     void                importConditionalFormatting( const AttributeList& rAttribs );
125     /** Imports a conditional formatting rule from the cfRule element. */
126     CondFormatRuleRef   importCfRule( const AttributeList& rAttribs );
127 
128     /** Imports settings from the CONDFORMATTING record. */
129     void                importCondFormatting( SequenceInputStream& rStrm );
130     /** Imports a conditional formatting rule from the CFRULE record. */
131     void                importCfRule( SequenceInputStream& rStrm );
132 
133     /** Imports settings from the CFHEADER record. */
134     void                importCfHeader( BiffInputStream& rStrm );
135 
136     /** Creates the conditional formatting in the Calc document. */
137     void                finalizeImport();
138 
139     /** Returns the cell ranges this conditional formatting belongs to. */
getRanges() const140     inline const ApiCellRangeList& getRanges() const { return maModel.maRanges; }
141 
142 private:
143     CondFormatRuleRef   createRule();
144     void                insertRule( CondFormatRuleRef xRule );
145 
146 private:
147     typedef RefMap< sal_Int32, CondFormatRule > CondFormatRuleMap;
148 
149     CondFormatModel     maModel;            /// Model of this conditional formatting.
150     CondFormatRuleMap   maRules;            /// Maps formatting rules by priority.
151 };
152 
153 typedef ::boost::shared_ptr< CondFormat > CondFormatRef;
154 
155 // ============================================================================
156 
157 class CondFormatBuffer : public WorksheetHelper
158 {
159 public:
160     explicit            CondFormatBuffer( const WorksheetHelper& rHelper );
161 
162     /** Imports settings from the conditionalFormatting element. */
163     CondFormatRef       importConditionalFormatting( const AttributeList& rAttribs );
164     /** Imports settings from the CONDFORMATTING record. */
165     CondFormatRef       importCondFormatting( SequenceInputStream& rStrm );
166     /** Imports settings from the CFHEADER record. */
167     void                importCfHeader( BiffInputStream& rStrm );
168 
169     /** Creates all conditional formattings in the Calc document. */
170     void                finalizeImport();
171 
172     /** Converts an OOXML condition operator token to the API constant. */
173     static ::com::sun::star::sheet::ConditionOperator
174                         convertToApiOperator( sal_Int32 nToken );
175 
176 private:
177     CondFormatRef       createCondFormat();
178 
179 private:
180     typedef RefVector< CondFormat > CondFormatVec;
181     CondFormatVec       maCondFormats;      /// All conditional formattings in a sheet.
182 };
183 
184 // ============================================================================
185 
186 } // namespace xls
187 } // namespace oox
188 
189 #endif
190