/src/libreoffice/sc/source/ui/view/pfuncache.cxx
Line | Count | Source |
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 <tools/multisel.hxx> |
21 | | #include <osl/diagnose.h> |
22 | | |
23 | | #include <pfuncache.hxx> |
24 | | #include <printfun.hxx> |
25 | | #include <docsh.hxx> |
26 | | #include <markdata.hxx> |
27 | | #include <prevloc.hxx> |
28 | | #include <utility> |
29 | | |
30 | | ScPrintFuncCache::ScPrintFuncCache(ScDocShell& rShell, const ScMarkData& rMark, |
31 | | ScPrintSelectionStatus aStatus, Size aPrintPageSize, bool bPrintPageLandscape, |
32 | | bool bUsePrintDialogSetting) |
33 | | : |
34 | 0 | aSelection(std::move( aStatus )), |
35 | 0 | rDocSh( rShell ), |
36 | 0 | nTotalPages( 0 ), |
37 | 0 | bLocInitialized( false ) |
38 | 0 | { |
39 | | // page count uses the stored cell widths for the printer anyway, |
40 | | // so ScPrintFunc with the document's printer can be used to count |
41 | |
|
42 | 0 | SfxPrinter* pPrinter = rDocSh.GetPrinter(); |
43 | |
|
44 | 0 | ScRange aRange; |
45 | 0 | const ScRange* pSelRange = nullptr; |
46 | 0 | if ( rMark.IsMarked() ) |
47 | 0 | { |
48 | 0 | aRange = rMark.GetMarkArea(); |
49 | 0 | pSelRange = &aRange; |
50 | 0 | } |
51 | |
|
52 | 0 | ScDocument& rDoc = rDocSh.GetDocument(); |
53 | 0 | SCTAB nTabCount = rDoc.GetTableCount(); |
54 | | |
55 | | // avoid repeated progress bars if row heights for all sheets are needed |
56 | 0 | if ( nTabCount > 1 && rMark.GetSelectCount() == nTabCount ) |
57 | 0 | rDocSh.UpdatePendingRowHeights( nTabCount-1, true ); |
58 | |
|
59 | 0 | SCTAB nTab; |
60 | 0 | for ( nTab=0; nTab<nTabCount; nTab++ ) |
61 | 0 | { |
62 | 0 | tools::Long nAttrPage = nTab > 0 ? nFirstAttr[nTab-1] : 1; |
63 | |
|
64 | 0 | tools::Long nThisTab = 0; |
65 | 0 | if ( rMark.GetTableSelect( nTab ) ) |
66 | 0 | { |
67 | 0 | ScPrintFunc aFunc(rDocSh, pPrinter, nTab, nAttrPage, 0, pSelRange, |
68 | 0 | &aSelection.GetOptions(), nullptr, aPrintPageSize, |
69 | 0 | bPrintPageLandscape, bUsePrintDialogSetting); |
70 | 0 | nThisTab = aFunc.GetTotalPages(); |
71 | 0 | nFirstAttr.push_back( aFunc.GetFirstPageNo() ); // from page style or previous sheet |
72 | 0 | } |
73 | 0 | else |
74 | 0 | nFirstAttr.push_back( nAttrPage ); |
75 | |
|
76 | 0 | nPages.push_back( nThisTab ); |
77 | 0 | nTotalPages += nThisTab; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | ScPrintFuncCache::~ScPrintFuncCache() |
82 | 0 | { |
83 | 0 | } |
84 | | |
85 | | void ScPrintFuncCache::InitLocations( const ScMarkData& rMark, OutputDevice* pDev ) |
86 | 0 | { |
87 | 0 | if ( bLocInitialized ) |
88 | 0 | return; // initialize only once |
89 | | |
90 | 0 | ScRange aRange; |
91 | 0 | const ScRange* pSelRange = nullptr; |
92 | 0 | if ( rMark.IsMarked() ) |
93 | 0 | { |
94 | 0 | aRange = rMark.GetMarkArea(); |
95 | 0 | pSelRange = &aRange; |
96 | 0 | } |
97 | |
|
98 | 0 | tools::Long nRenderer = 0; // 0-based physical page number across sheets |
99 | 0 | tools::Long nTabStart = 0; |
100 | |
|
101 | 0 | ScDocument& rDoc = rDocSh.GetDocument(); |
102 | 0 | SCTAB nTabCount = rDoc.GetTableCount(); |
103 | 0 | for (SCTAB nTab : rMark) |
104 | 0 | { |
105 | 0 | if (nTab >= nTabCount) |
106 | 0 | break; |
107 | 0 | ScPrintFunc aFunc( pDev, rDocSh, nTab, nFirstAttr[nTab], nTotalPages, pSelRange, &aSelection.GetOptions() ); |
108 | 0 | aFunc.SetRenderFlag( true ); |
109 | |
|
110 | 0 | tools::Long nDisplayStart = GetDisplayStart( nTab ); |
111 | |
|
112 | 0 | for ( tools::Long nPage=0; nPage<nPages[nTab]; nPage++ ) |
113 | 0 | { |
114 | 0 | Range aPageRange( nRenderer+1, nRenderer+1 ); |
115 | 0 | MultiSelection aPage( aPageRange ); |
116 | 0 | aPage.SetTotalRange( Range(0,RANGE_MAX) ); |
117 | 0 | aPage.Select( aPageRange ); |
118 | |
|
119 | 0 | ScPreviewLocationData aLocData( &rDoc, pDev ); |
120 | 0 | aFunc.DoPrint( aPage, nTabStart, nDisplayStart, false, &aLocData ); |
121 | |
|
122 | 0 | ScRange aCellRange; |
123 | 0 | tools::Rectangle aPixRect; |
124 | 0 | if ( aLocData.GetMainCellRange( aCellRange, aPixRect ) ) |
125 | 0 | aLocations.emplace_back( nRenderer, aCellRange, aPixRect ); |
126 | |
|
127 | 0 | ++nRenderer; |
128 | 0 | } |
129 | |
|
130 | 0 | nTabStart += nPages[nTab]; |
131 | 0 | } |
132 | |
|
133 | 0 | bLocInitialized = true; |
134 | 0 | } |
135 | | |
136 | | bool ScPrintFuncCache::FindLocation( const ScAddress& rCell, ScPrintPageLocation& rLocation ) const |
137 | 0 | { |
138 | 0 | auto aIter = std::find_if(aLocations.begin(), aLocations.end(), |
139 | 0 | [&rCell](const ScPrintPageLocation& rLoc) { return rLoc.aCellRange.Contains(rCell); }); |
140 | 0 | if (aIter != aLocations.end()) |
141 | 0 | { |
142 | 0 | rLocation = *aIter; |
143 | 0 | return true; |
144 | 0 | } |
145 | 0 | return false; // not found |
146 | 0 | } |
147 | | |
148 | | bool ScPrintFuncCache::IsSameSelection( const ScPrintSelectionStatus& rStatus ) const |
149 | 0 | { |
150 | 0 | return aSelection == rStatus; |
151 | 0 | } |
152 | | |
153 | | SCTAB ScPrintFuncCache::GetTabForPage( tools::Long nPage ) const |
154 | 0 | { |
155 | 0 | ScDocument& rDoc = rDocSh.GetDocument(); |
156 | 0 | SCTAB nTabCount = rDoc.GetTableCount(); |
157 | 0 | SCTAB nTab = 0; |
158 | 0 | while ( nTab < nTabCount && nPage >= nPages[nTab] ) |
159 | 0 | nPage -= nPages[nTab++]; |
160 | 0 | if (nTab >= nTabCount) |
161 | 0 | nTab = nTabCount - 1; |
162 | 0 | return nTab; |
163 | 0 | } |
164 | | |
165 | | tools::Long ScPrintFuncCache::GetTabStart( SCTAB nTab ) const |
166 | 0 | { |
167 | 0 | tools::Long nRet = 0; |
168 | 0 | const SCTAB maxIndex = std::min(nTab, static_cast<SCTAB>(nPages.size())); |
169 | 0 | for ( SCTAB i=0; i<maxIndex; i++ ) |
170 | 0 | nRet += nPages[i]; |
171 | 0 | return nRet; |
172 | 0 | } |
173 | | |
174 | | tools::Long ScPrintFuncCache::GetDisplayStart( SCTAB nTab ) const |
175 | 0 | { |
176 | | //! merge with lcl_GetDisplayStart in preview? |
177 | |
|
178 | 0 | tools::Long nDisplayStart = 0; |
179 | 0 | ScDocument& rDoc = rDocSh.GetDocument(); |
180 | 0 | for (SCTAB i=0; i<nTab; i++) |
181 | 0 | { |
182 | 0 | if ( rDoc.NeedPageResetAfterTab(i) ) |
183 | 0 | nDisplayStart = 0; |
184 | 0 | else |
185 | 0 | { |
186 | 0 | if ( i < static_cast<SCTAB>(nPages.size()) ) |
187 | 0 | nDisplayStart += nPages[i]; |
188 | 0 | else |
189 | 0 | OSL_FAIL("nPages out of bounds, FIX IT!"); |
190 | 0 | } |
191 | 0 | } |
192 | 0 | return nDisplayStart; |
193 | 0 | } |
194 | | |
195 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |