1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <oox/helper/helper.hxx>
23 #include <oox/helper/refvector.hxx>
24 #include "workbookhelper.hxx"
25 #include <com/sun/star/sheet/TableFilterField3.hpp>
26 #include <com/sun/star/util/Color.hpp>
28 namespace com::sun::star
{
29 namespace sheet
{ class XDatabaseRange
; }
30 namespace sheet
{ class XSheetFilterDescriptor3
; }
33 namespace oox
{ class AttributeList
; }
34 namespace oox
{ class SequenceInputStream
; }
39 /** Contains UNO API filter settings for a column in a filtered range. */
40 struct ApiFilterSettings
42 typedef ::std::vector
<css::sheet::TableFilterField3
> FilterFieldVector
;
44 FilterFieldVector maFilterFields
; /// List of UNO API filter settings.
45 std::optional
< bool > mobNeedsRegExp
; /// If set, requires regular expressions to be enabled/disabled.
47 explicit ApiFilterSettings();
49 void appendField( bool bAnd
, sal_Int32 nOperator
, double fValue
);
50 void appendField( bool bAnd
, sal_Int32 nOperator
, const OUString
& rValue
);
51 void appendField( bool bAnd
, css::util::Color aColor
, bool bIsBackgroundColor
);
52 void appendField( bool bAnd
, const std::vector
<std::pair
<OUString
, bool>>& rValues
);
55 /** Base class for specific filter settings for a column in a filtered range.
57 class FilterSettingsBase
: public WorkbookHelper
60 explicit FilterSettingsBase( const WorkbookHelper
& rHelper
);
62 /** Derived classes import filter settings from the passed attribute list. */
63 virtual void importAttribs( sal_Int32 nElement
, const AttributeList
& rAttribs
);
64 /** Derived classes import filter settings from the passed record. */
65 virtual void importRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
);
67 /** Derived classes return converted UNO API filter settings representing all filter settings. */
68 virtual ApiFilterSettings
finalizeImport();
72 /** Settings for a discrete filter, specifying a list of values to be shown in
75 class DiscreteFilter final
: public FilterSettingsBase
78 explicit DiscreteFilter( const WorkbookHelper
& rHelper
);
80 /** Imports filter settings from the filters and filter elements. */
81 virtual void importAttribs( sal_Int32 nElement
, const AttributeList
& rAttribs
) override
;
82 /** Imports filter settings from the FILTERS and FILTER records. */
83 virtual void importRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
) override
;
85 /** Returns converted UNO API filter settings representing all filter settings. */
86 virtual ApiFilterSettings
finalizeImport() override
;
90 std::vector
<std::pair
<OUString
, bool>> maValues
; // first->values, second->bDatefFormat
91 sal_Int32 mnCalendarType
;
95 /** Settings for a top-10 filter. */
96 class Top10Filter final
: public FilterSettingsBase
99 explicit Top10Filter( const WorkbookHelper
& rHelper
);
101 /** Imports filter settings from the filters and filter elements. */
102 virtual void importAttribs( sal_Int32 nElement
, const AttributeList
& rAttribs
) override
;
103 /** Imports filter settings from the FILTERS and FILTER records. */
104 virtual void importRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
) override
;
106 /** Returns converted UNO API filter settings representing all filter settings. */
107 virtual ApiFilterSettings
finalizeImport() override
;
110 double mfValue
; /// Number of items or percentage.
111 bool mbTop
; /// True = show top (greatest) items/percentage.
112 bool mbPercent
; /// True = percentage, false = number of items.
115 /** Settings for a color filter. */
116 class ColorFilter final
: public FilterSettingsBase
119 explicit ColorFilter(const WorkbookHelper
& rHelper
);
121 /** Imports filter settings from the filters and filter elements. */
122 virtual void importAttribs(sal_Int32 nElement
, const AttributeList
& rAttribs
) override
;
123 /** Imports filter settings from the FILTERS and FILTER records. */
124 virtual void importRecord(sal_Int32 nRecId
, SequenceInputStream
& rStrm
) override
;
126 /** Returns converted UNO API filter settings representing all filter settings. */
127 virtual ApiFilterSettings
finalizeImport() override
;
130 /// Whether we are dealing with the background color (vs. text color)
131 bool mbIsBackgroundColor
;
132 /// Style name to retrieve the color from
133 OUString msStyleName
;
136 /** A filter criterion for a custom filter. */
137 struct FilterCriterionModel
139 css::uno::Any maValue
; /// Comparison operand.
140 sal_Int32 mnOperator
; /// Comparison operator.
141 sal_uInt8 mnDataType
; /// Operand data type (BIFF only).
143 explicit FilterCriterionModel();
145 /** Sets the passed BIFF operator constant. */
146 void setBiffOperator( sal_uInt8 nOperator
);
148 /** Imports the criterion model from the passed BIFF12 stream. */
149 void readBiffData( SequenceInputStream
& rStrm
);
152 /** Settings for a custom filter, specifying one or two comparison operators
153 associated with some values.
155 class CustomFilter final
: public FilterSettingsBase
158 explicit CustomFilter( const WorkbookHelper
& rHelper
);
160 /** Imports filter settings from the filters and filter elements. */
161 virtual void importAttribs( sal_Int32 nElement
, const AttributeList
& rAttribs
) override
;
162 /** Imports filter settings from the FILTERS and FILTER records. */
163 virtual void importRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
) override
;
165 /** Returns converted UNO API filter settings representing all filter settings. */
166 virtual ApiFilterSettings
finalizeImport() override
;
169 /** Appends the passed filter criterion, if it contains valid settings. */
170 void appendCriterion( const FilterCriterionModel
& rCriterion
);
173 typedef ::std::vector
< FilterCriterionModel
> FilterCriterionVector
;
175 FilterCriterionVector maCriteria
;
179 /** A column in a filtered range. Contains an object with specific filter
180 settings for the cells in the column.
182 class FilterColumn final
: public WorkbookHelper
185 explicit FilterColumn( const WorkbookHelper
& rHelper
);
187 /** Imports auto filter column settings from the filterColumn element. */
188 void importFilterColumn( const AttributeList
& rAttribs
);
189 /** Imports auto filter column settings from the FILTERCOLUMN record. */
190 void importFilterColumn( SequenceInputStream
& rStrm
);
192 /** Creates and returns the specified filter settings object. */
193 template< typename FilterSettingsType
>
194 FilterSettingsBase
& createFilterSettings()
195 { mxSettings
= std::make_shared
<FilterSettingsType
>( *this ); return *mxSettings
; }
197 /** Returns converted UNO API filter settings representing all filter
198 settings of this column. */
199 ApiFilterSettings
finalizeImport();
200 bool isButtonHidden();
203 std::shared_ptr
< FilterSettingsBase
>
210 // class SortCondition
212 class SortCondition final
: public WorkbookHelper
215 explicit SortCondition( const WorkbookHelper
& rHelper
);
217 void importSortCondition( const AttributeList
& rAttribs
, sal_Int16 nSheet
);
219 ScRange maRange
; // Column/Row that this sort condition applies to.
220 OUString maSortCustomList
; // Sort by a custom list.
226 class AutoFilter final
: public WorkbookHelper
229 explicit AutoFilter( const WorkbookHelper
& rHelper
);
231 /** Imports auto filter settings from the autoFilter element. */
232 void importAutoFilter( const AttributeList
& rAttribs
, sal_Int16 nSheet
);
233 /** Imports auto filter settings from the AUTOFILTER record. */
234 void importAutoFilter( SequenceInputStream
& rStrm
, sal_Int16 nSheet
);
236 void importSortState( const AttributeList
& rAttribs
, sal_Int16 nSheet
);
238 /** Creates a new auto filter column and stores it internally. */
239 FilterColumn
& createFilterColumn();
241 SortCondition
& createSortCondition();
243 /** Applies the filter to the passed filter descriptor. */
244 void finalizeImport( const css::uno::Reference
< css::sheet::XDatabaseRange
>& rxDatabaseRange
,
248 typedef RefVector
< FilterColumn
> FilterColumnVector
;
250 FilterColumnVector maFilterColumns
;
253 ScRange maSortRange
; // The whole range of data to sort (not just the sort-by column).
254 typedef RefVector
< SortCondition
> SortConditionVector
;
255 SortConditionVector maSortConditions
;
258 class AutoFilterBuffer final
: public WorkbookHelper
261 explicit AutoFilterBuffer( const WorkbookHelper
& rHelper
);
263 /** Creates a new auto filter and stores it internally. */
264 AutoFilter
& createAutoFilter();
266 /** Applies filter settings to a new database range object (used for sheet
267 autofilter or advanced filter as specified by built-in defined names). */
268 void finalizeImport( sal_Int16 nSheet
);
270 /** Applies the filters to the passed database range object.
271 @return True = this buffer contains valid auto filter settings. */
272 bool finalizeImport( const css::uno::Reference
< css::sheet::XDatabaseRange
>& rxDatabaseRange
,
276 /** Returns the auto filter object used to perform auto filtering. */
277 AutoFilter
* getActiveAutoFilter();
280 typedef RefVector
< AutoFilter
> AutoFilterVector
;
281 AutoFilterVector maAutoFilters
;
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */