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 : #ifndef SC_DPCACHETABLE_HXX
21 : #define SC_DPCACHETABLE_HXX
22 :
23 : #include "sal/types.h"
24 : #include "osl/mutex.hxx"
25 : #include "global.hxx"
26 : #include "dpitemdata.hxx"
27 : #include "calcmacros.hxx"
28 :
29 : #include <vector>
30 : #include <boost/unordered_set.hpp>
31 : #include <boost/shared_ptr.hpp>
32 :
33 : #include <mdds/flat_segment_tree.hpp>
34 :
35 : class ScDPItemData;
36 : class ScDPCache;
37 : class ScDocument;
38 : class ScRange;
39 : struct ScDPValue;
40 : struct ScQueryParam;
41 :
42 : /**
43 : * This class is only a wrapper to the actual cache, to provide filtering on
44 : * the raw data based on the query filter and/or page field filters.
45 : */
46 : class SC_DLLPUBLIC ScDPFilteredCache
47 : {
48 : typedef mdds::flat_segment_tree<SCROW, bool> RowFlagType;
49 :
50 : public:
51 : /** interface class used for filtering of rows. */
52 0 : class FilterBase
53 : {
54 : public:
55 0 : virtual ~FilterBase() {}
56 : /** returns true if the matching condition is met for a single cell
57 : value, or false otherwise. */
58 : virtual bool match( const ScDPItemData& rCellData ) const = 0;
59 :
60 : virtual std::vector<ScDPItemData> getMatchValues() const = 0;
61 : };
62 :
63 : /** ordinary single-item filter. */
64 : class SingleFilter : public FilterBase
65 : {
66 : public:
67 : explicit SingleFilter(const ScDPItemData &rItem);
68 0 : virtual ~SingleFilter() {}
69 :
70 : virtual bool match(const ScDPItemData& rCellData) const SAL_OVERRIDE;
71 : virtual std::vector<ScDPItemData> getMatchValues() const SAL_OVERRIDE;
72 :
73 : private:
74 : explicit SingleFilter();
75 :
76 : ScDPItemData maItem;
77 : };
78 :
79 : /** multi-item (group) filter. */
80 : class GroupFilter : public FilterBase
81 : {
82 : public:
83 : GroupFilter();
84 0 : virtual ~GroupFilter() {}
85 : virtual bool match(const ScDPItemData& rCellData) const SAL_OVERRIDE;
86 : virtual std::vector<ScDPItemData> getMatchValues() const SAL_OVERRIDE;
87 : void addMatchItem(const ScDPItemData& rItem);
88 : size_t getMatchItemCount() const;
89 :
90 : private:
91 : ::std::vector<ScDPItemData> maItems;
92 : };
93 :
94 : /** single filtering criterion. */
95 0 : struct Criterion
96 : {
97 : sal_Int32 mnFieldIndex;
98 : ::boost::shared_ptr<FilterBase> mpFilter;
99 :
100 : Criterion();
101 : };
102 :
103 : ScDPFilteredCache(const ScDPCache& rCache);
104 : ~ScDPFilteredCache();
105 :
106 : sal_Int32 getRowSize() const;
107 : sal_Int32 getColSize() const;
108 :
109 : const ScDPCache* getCache() const;
110 :
111 : void fillTable(const ScQueryParam& rQuery, bool bIgnoreEmptyRows, bool bRepeatIfEmpty);
112 :
113 : void fillTable();
114 :
115 : /** Check whether a specified row is active or not. When a row is active,
116 : it is used in calculation of the results data. A row becomes inactive
117 : when it is filtered out by page field. */
118 : bool isRowActive(sal_Int32 nRow, sal_Int32* pLastRow = NULL) const;
119 :
120 : /** Set filter on/off flag to each row to control visibility. The caller
121 : must ensure that the table is filled before calling this function. */
122 : void filterByPageDimension(const ::std::vector<Criterion>& rCriteria, const ::boost::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
123 :
124 : /** Get the cell instance at specified location within the data grid. Note
125 : that the data grid doesn't include the header row. Don't delete the
126 : returned object! */
127 : const ScDPItemData* getCell(SCCOL nCol, SCROW nRow, bool bRepeatIfEmpty) const;
128 : void getValue( ScDPValue& rVal, SCCOL nCol, SCROW nRow, bool bRepeatIfEmpty) const;
129 : OUString getFieldName(SCCOL nIndex) const;
130 :
131 : /** Get the unique entries for a field specified by index. The caller must
132 : make sure that the table is filled before calling function, or it will
133 : get an empty collection. */
134 : const ::std::vector<SCROW>& getFieldEntries( sal_Int32 nColumn ) const;
135 :
136 : /** Filter the table based on the specified criteria, and copy the
137 : result to rTabData. This method is used, for example, to generate
138 : a drill-down data table. */
139 : void filterTable(const ::std::vector<Criterion>& rCriteria,
140 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > >& rTabData,
141 : const ::boost::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
142 :
143 : SCROW getOrder(long nDim, SCROW nIndex) const;
144 : void clear();
145 : bool empty() const;
146 :
147 : #if DEBUG_PIVOT_TABLE
148 : void dumpRowFlag(const RowFlagType& rFlag) const;
149 : void dump() const;
150 : #endif
151 :
152 : private:
153 : ScDPFilteredCache();
154 : ScDPFilteredCache(const ScDPFilteredCache&);
155 :
156 : /**
157 : * Check if a given row meets all specified criteria.
158 : *
159 : * @param nRow index of row to be tested.
160 : * @param rCriteria a list of criteria
161 : */
162 : bool isRowQualified(sal_Int32 nRow, const ::std::vector<Criterion>& rCriteria, const ::boost::unordered_set<sal_Int32>& rRepeatIfEmptyDims) const;
163 :
164 : private:
165 :
166 : /** unique field entires for each field (column). */
167 : ::std::vector< ::std::vector<SCROW> > maFieldEntries;
168 :
169 : /** Rows visible by standard filter query. */
170 : RowFlagType maShowByFilter;
171 : /** Rows visible by page dimension filtering. */
172 : RowFlagType maShowByPage;
173 :
174 : const ScDPCache& mrCache;
175 : };
176 : #endif
177 :
178 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|