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 : #include "dpoutputgeometry.hxx"
21 : #include "address.hxx"
22 :
23 : #include <vector>
24 :
25 : using ::std::vector;
26 :
27 16 : ScDPOutputGeometry::ScDPOutputGeometry(const ScRange& rOutRange, bool bShowFilter) :
28 : maOutRange(rOutRange),
29 : mnRowFields(0),
30 : mnColumnFields(0),
31 : mnPageFields(0),
32 : mnDataFields(0),
33 : meDataLayoutType(None),
34 16 : mbShowFilter(bShowFilter)
35 : {
36 16 : }
37 :
38 16 : ScDPOutputGeometry::~ScDPOutputGeometry()
39 : {
40 16 : }
41 :
42 16 : void ScDPOutputGeometry::setRowFieldCount(sal_uInt32 nCount)
43 : {
44 16 : mnRowFields = nCount;
45 16 : }
46 :
47 16 : void ScDPOutputGeometry::setColumnFieldCount(sal_uInt32 nCount)
48 : {
49 16 : mnColumnFields = nCount;
50 16 : }
51 :
52 16 : void ScDPOutputGeometry::setPageFieldCount(sal_uInt32 nCount)
53 : {
54 16 : mnPageFields = nCount;
55 16 : }
56 :
57 16 : void ScDPOutputGeometry::setDataFieldCount(sal_uInt32 nCount)
58 : {
59 16 : mnDataFields = nCount;
60 16 : }
61 :
62 14 : void ScDPOutputGeometry::setDataLayoutType(FieldType eType)
63 : {
64 14 : meDataLayoutType = eType;
65 14 : }
66 :
67 2 : void ScDPOutputGeometry::getColumnFieldPositions(vector<ScAddress>& rAddrs) const
68 : {
69 : sal_uInt32 nColumnFields, nRowFields;
70 2 : adjustFieldsForDataLayout(nColumnFields, nRowFields);
71 :
72 2 : vector<ScAddress> aAddrs;
73 2 : if (!nColumnFields)
74 : {
75 2 : rAddrs.swap(aAddrs);
76 4 : return;
77 : }
78 :
79 0 : SCROW nCurRow = maOutRange.aStart.Row();
80 :
81 0 : if (mnPageFields)
82 : {
83 0 : SCROW nRowStart = maOutRange.aStart.Row() + int(mbShowFilter);
84 0 : SCROW nRowEnd = nRowStart + static_cast<SCCOL>(mnPageFields-1);
85 0 : nCurRow = nRowEnd + 2;
86 : }
87 0 : else if (mbShowFilter)
88 0 : nCurRow += 2;
89 :
90 0 : SCROW nRow = nCurRow;
91 0 : SCTAB nTab = maOutRange.aStart.Tab();
92 0 : SCCOL nColStart = static_cast<SCCOL>(maOutRange.aStart.Col() + nRowFields);
93 0 : SCCOL nColEnd = nColStart + static_cast<SCCOL>(nColumnFields-1);
94 :
95 0 : for (SCCOL nCol = nColStart; nCol <= nColEnd; ++nCol)
96 0 : aAddrs.push_back(ScAddress(nCol, nRow, nTab));
97 0 : rAddrs.swap(aAddrs);
98 : }
99 :
100 2 : void ScDPOutputGeometry::getRowFieldPositions(vector<ScAddress>& rAddrs) const
101 : {
102 : sal_uInt32 nColumnFields, nRowFields;
103 2 : adjustFieldsForDataLayout(nColumnFields, nRowFields);
104 :
105 2 : vector<ScAddress> aAddrs;
106 2 : if (!nRowFields)
107 : {
108 0 : rAddrs.swap(aAddrs);
109 2 : return;
110 : }
111 :
112 2 : SCROW nRow = getRowFieldHeaderRow();
113 2 : SCTAB nTab = maOutRange.aStart.Tab();
114 2 : SCCOL nColStart = maOutRange.aStart.Col();
115 2 : SCCOL nColEnd = nColStart + static_cast<SCCOL>(nRowFields-1);
116 :
117 6 : for (SCCOL nCol = nColStart; nCol <= nColEnd; ++nCol)
118 4 : aAddrs.push_back(ScAddress(nCol, nRow, nTab));
119 2 : rAddrs.swap(aAddrs);
120 : }
121 :
122 2 : void ScDPOutputGeometry::getPageFieldPositions(vector<ScAddress>& rAddrs) const
123 : {
124 2 : vector<ScAddress> aAddrs;
125 2 : if (!mnPageFields)
126 : {
127 2 : rAddrs.swap(aAddrs);
128 4 : return;
129 : }
130 :
131 0 : SCTAB nTab = maOutRange.aStart.Tab();
132 0 : SCCOL nCol = maOutRange.aStart.Col();
133 :
134 0 : SCROW nRowStart = maOutRange.aStart.Row() + int(mbShowFilter);
135 0 : SCROW nRowEnd = nRowStart + static_cast<SCCOL>(mnPageFields-1);
136 :
137 0 : for (SCROW nRow = nRowStart; nRow <= nRowEnd; ++nRow)
138 0 : aAddrs.push_back(ScAddress(nCol, nRow, nTab));
139 0 : rAddrs.swap(aAddrs);
140 : }
141 :
142 2 : SCROW ScDPOutputGeometry::getRowFieldHeaderRow() const
143 : {
144 2 : SCROW nCurRow = maOutRange.aStart.Row();
145 : sal_uInt32 nColumnFields, nRowFields;
146 2 : adjustFieldsForDataLayout(nColumnFields, nRowFields);
147 :
148 2 : if (mnPageFields)
149 : {
150 0 : SCROW nRowStart = maOutRange.aStart.Row() + int(mbShowFilter);
151 0 : SCROW nRowEnd = nRowStart + static_cast<SCCOL>(mnPageFields-1);
152 0 : nCurRow = nRowEnd + 2;
153 : }
154 2 : else if (mbShowFilter)
155 0 : nCurRow += 2;
156 :
157 2 : if (nColumnFields)
158 0 : nCurRow += static_cast<SCROW>(nColumnFields);
159 2 : else if (nRowFields)
160 2 : ++nCurRow;
161 :
162 2 : return nCurRow;
163 : }
164 :
165 74 : void ScDPOutputGeometry::adjustFieldsForDataLayout(sal_uInt32& rColumnFields, sal_uInt32& rRowFields) const
166 : {
167 74 : rRowFields = mnRowFields;
168 74 : rColumnFields = mnColumnFields;
169 :
170 74 : if (mnDataFields < 2)
171 : {
172 : // Data layout field can be either row or column field, never page field.
173 74 : switch (meDataLayoutType)
174 : {
175 : case Column:
176 0 : if (rColumnFields > 0)
177 0 : rColumnFields -= 1;
178 0 : break;
179 : case Row:
180 56 : if (rRowFields > 0)
181 56 : rRowFields -= 1;
182 : default:
183 : ;
184 : }
185 : }
186 74 : }
187 :
188 : std::pair<ScDPOutputGeometry::FieldType, size_t>
189 68 : ScDPOutputGeometry::getFieldButtonType(const ScAddress& rPos) const
190 : {
191 68 : SCROW nCurRow = maOutRange.aStart.Row();
192 : sal_uInt32 nColumnFields, nRowFields;
193 68 : adjustFieldsForDataLayout(nColumnFields, nRowFields);
194 :
195 68 : if (mnPageFields)
196 : {
197 56 : SCCOL nCol = maOutRange.aStart.Col();
198 56 : SCROW nRowStart = maOutRange.aStart.Row() + int(mbShowFilter);
199 56 : SCROW nRowEnd = nRowStart + static_cast<SCCOL>(mnPageFields-1);
200 56 : if (rPos.Col() == nCol && nRowStart <= rPos.Row() && rPos.Row() <= nRowEnd)
201 : {
202 12 : size_t nPos = static_cast<size_t>(rPos.Row() - nRowStart);
203 12 : return std::pair<FieldType, size_t>(Page, nPos);
204 : }
205 :
206 44 : nCurRow = nRowEnd + 2;
207 : }
208 12 : else if (mbShowFilter)
209 12 : nCurRow += 2;
210 :
211 56 : if (nColumnFields)
212 : {
213 50 : SCROW nRow = nCurRow;
214 50 : SCCOL nColStart = static_cast<SCCOL>(maOutRange.aStart.Col() + nRowFields);
215 50 : SCCOL nColEnd = nColStart + static_cast<SCCOL>(nColumnFields-1);
216 50 : if (rPos.Row() == nRow && nColStart <= rPos.Col() && rPos.Col() <= nColEnd)
217 : {
218 18 : size_t nPos = static_cast<size_t>(rPos.Col() - nColStart);
219 18 : return std::pair<FieldType, size_t>(Column, nPos);
220 : }
221 :
222 32 : nCurRow += static_cast<SCROW>(nColumnFields);
223 : }
224 : else
225 6 : ++nCurRow;
226 :
227 38 : if (nRowFields)
228 : {
229 36 : SCCOL nColStart = maOutRange.aStart.Col();
230 36 : SCCOL nColEnd = nColStart + static_cast<SCCOL>(nRowFields-1);
231 36 : if (rPos.Row() == nCurRow && nColStart <= rPos.Col() && rPos.Col() <= nColEnd)
232 : {
233 22 : size_t nPos = static_cast<size_t>(rPos.Col() - nColStart);
234 22 : return std::pair<FieldType, size_t>(Row, nPos);
235 : }
236 : }
237 :
238 16 : return std::pair<FieldType, size_t>(None, 0);
239 : }
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|