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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include "dpoutputgeometry.hxx"
32 #include "address.hxx"
33 
34 #include <vector>
35 
36 using ::std::vector;
37 
ScDPOutputGeometry(const ScRange & rOutRange,bool bShowFilter,ImportType eImportType)38 ScDPOutputGeometry::ScDPOutputGeometry(const ScRange& rOutRange, bool bShowFilter, ImportType eImportType) :
39     maOutRange(rOutRange),
40     mnRowFields(0),
41     mnColumnFields(0),
42     mnPageFields(0),
43     mnDataFields(0),
44     meImportType(eImportType),
45     mbShowFilter(bShowFilter)
46 {
47 }
48 
~ScDPOutputGeometry()49 ScDPOutputGeometry::~ScDPOutputGeometry()
50 {
51 }
52 
setRowFieldCount(sal_uInt32 nCount)53 void ScDPOutputGeometry::setRowFieldCount(sal_uInt32 nCount)
54 {
55     mnRowFields = nCount;
56 }
57 
setColumnFieldCount(sal_uInt32 nCount)58 void ScDPOutputGeometry::setColumnFieldCount(sal_uInt32 nCount)
59 {
60     mnColumnFields = nCount;
61 }
62 
setPageFieldCount(sal_uInt32 nCount)63 void ScDPOutputGeometry::setPageFieldCount(sal_uInt32 nCount)
64 {
65     mnPageFields = nCount;
66 }
67 
setDataFieldCount(sal_uInt32 nCount)68 void ScDPOutputGeometry::setDataFieldCount(sal_uInt32 nCount)
69 {
70     mnDataFields = nCount;
71 }
72 
getColumnFieldPositions(vector<ScAddress> & rAddrs) const73 void ScDPOutputGeometry::getColumnFieldPositions(vector<ScAddress>& rAddrs) const
74 {
75     vector<ScAddress> aAddrs;
76     if (!mnColumnFields)
77     {
78         rAddrs.swap(aAddrs);
79         return;
80     }
81 
82     bool bDataLayout = mnDataFields > 1;
83 
84     SCROW nCurRow = maOutRange.aStart.Row();
85 
86     if (mnPageFields)
87     {
88         SCROW nRowStart = maOutRange.aStart.Row() + mbShowFilter;
89         SCROW nRowEnd   = nRowStart + static_cast<SCCOL>(mnPageFields-1);
90         nCurRow = nRowEnd + 2;
91     }
92     else if (mbShowFilter)
93         nCurRow += 2;
94 
95     SCROW nRow = nCurRow;
96     SCTAB nTab = maOutRange.aStart.Tab();
97     SCCOL nColStart = static_cast<SCCOL>(maOutRange.aStart.Col() + mnRowFields + (bDataLayout ? 1 : 0));
98     SCCOL nColEnd = nColStart + static_cast<SCCOL>(mnColumnFields-1);
99 
100     for (SCCOL nCol = nColStart; nCol <= nColEnd; ++nCol)
101         aAddrs.push_back(ScAddress(nCol, nRow, nTab));
102     rAddrs.swap(aAddrs);
103 }
104 
getRowFieldPositions(vector<ScAddress> & rAddrs) const105 void ScDPOutputGeometry::getRowFieldPositions(vector<ScAddress>& rAddrs) const
106 {
107     vector<ScAddress> aAddrs;
108     if (!mnRowFields)
109     {
110         rAddrs.swap(aAddrs);
111         return;
112     }
113 
114     SCROW nRow = getRowFieldHeaderRow();
115     SCTAB nTab = maOutRange.aStart.Tab();
116     SCCOL nColStart = maOutRange.aStart.Col();
117     SCCOL nColEnd = nColStart + static_cast<SCCOL>(mnRowFields-1);
118 
119     for (SCCOL nCol = nColStart; nCol <= nColEnd; ++nCol)
120         aAddrs.push_back(ScAddress(nCol, nRow, nTab));
121     rAddrs.swap(aAddrs);
122 }
123 
getPageFieldPositions(vector<ScAddress> & rAddrs) const124 void ScDPOutputGeometry::getPageFieldPositions(vector<ScAddress>& rAddrs) const
125 {
126     vector<ScAddress> aAddrs;
127     if (!mnPageFields)
128     {
129         rAddrs.swap(aAddrs);
130         return;
131     }
132 
133     SCTAB nTab = maOutRange.aStart.Tab();
134     SCCOL nCol = maOutRange.aStart.Col();
135 
136     SCROW nRowStart = maOutRange.aStart.Row() + mbShowFilter;
137     SCROW nRowEnd   = nRowStart + static_cast<SCCOL>(mnPageFields-1);
138 
139     for (SCROW nRow = nRowStart; nRow <= nRowEnd; ++nRow)
140         aAddrs.push_back(ScAddress(nCol, nRow, nTab));
141     rAddrs.swap(aAddrs);
142 }
143 
getRowFieldHeaderRow() const144 SCROW ScDPOutputGeometry::getRowFieldHeaderRow() const
145 {
146     SCROW nCurRow = maOutRange.aStart.Row();
147 
148     if (mnPageFields)
149     {
150         SCROW nRowStart = maOutRange.aStart.Row() + mbShowFilter;
151         SCROW nRowEnd   = nRowStart + static_cast<SCCOL>(mnPageFields-1);
152         nCurRow = nRowEnd + 2;
153     }
154     else if (mbShowFilter)
155         nCurRow += 2;
156 
157     if (mnColumnFields)
158         nCurRow += static_cast<SCROW>(mnColumnFields);
159     else if (mnRowFields)
160         ++nCurRow;
161 
162     return nCurRow;
163 }
164 
getFieldButtonType(const ScAddress & rPos) const165 ScDPOutputGeometry::FieldType ScDPOutputGeometry::getFieldButtonType(const ScAddress& rPos) const
166 {
167     // We will ignore the table position for now.
168 
169     bool bExtraTitleRow = (mnColumnFields == 0 && meImportType == ScDPOutputGeometry::XLS);
170     bool bDataLayout = mnDataFields > 1;
171 
172     SCROW nCurRow = maOutRange.aStart.Row();
173 
174     if (mnPageFields)
175     {
176         SCCOL nCol = maOutRange.aStart.Col();
177         SCROW nRowStart = maOutRange.aStart.Row() + mbShowFilter;
178         SCROW nRowEnd   = nRowStart + static_cast<SCCOL>(mnPageFields-1);
179         if (rPos.Col() == nCol && nRowStart <= rPos.Row() && rPos.Row() <= nRowEnd)
180             return Page;
181 
182         nCurRow = nRowEnd + 2;
183     }
184     else if (mbShowFilter)
185         nCurRow += 2;
186 
187     if (mnColumnFields)
188     {
189         SCROW nRow = nCurRow;
190         SCCOL nColStart = static_cast<SCCOL>(maOutRange.aStart.Col() + mnRowFields + (bDataLayout ? 1 : 0));
191         SCCOL nColEnd = nColStart + static_cast<SCCOL>(mnColumnFields-1);
192         if (rPos.Row() == nRow && nColStart <= rPos.Col() && rPos.Col() <= nColEnd)
193             return Column;
194 
195         nCurRow += static_cast<SCROW>(mnColumnFields);
196     }
197 
198     if (bExtraTitleRow)
199         ++nCurRow;
200 
201     if (mnRowFields)
202     {
203         SCCOL nColStart = maOutRange.aStart.Col();
204         SCCOL nColEnd = nColStart + static_cast<SCCOL>(mnRowFields-1);
205         if (rPos.Row() == nCurRow && nColStart <= rPos.Col() && rPos.Col() <= nColEnd)
206             return Row;
207     }
208 
209     return None;
210 }
211