LibreOffice Module sc (master)  1
pfuncache.cxx
Go to the documentation of this file.
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 
30  const ScPrintSelectionStatus& rStatus ) :
31  aSelection( rStatus ),
32  pDocSh( pD ),
33  nTotalPages( 0 ),
34  nPages(),
35  nFirstAttr(),
36  bLocInitialized( false )
37 {
38  // page count uses the stored cell widths for the printer anyway,
39  // so ScPrintFunc with the document's printer can be used to count
40 
41  SfxPrinter* pPrinter = pDocSh->GetPrinter();
42 
43  ScRange aRange;
44  const ScRange* pSelRange = nullptr;
45  if ( rMark.IsMarked() )
46  {
47  rMark.GetMarkArea( aRange );
48  pSelRange = &aRange;
49  }
50 
51  ScDocument& rDoc = pDocSh->GetDocument();
52  SCTAB nTabCount = rDoc.GetTableCount();
53 
54  // avoid repeated progress bars if row heights for all sheets are needed
55  if ( nTabCount > 1 && rMark.GetSelectCount() == nTabCount )
56  pDocSh->UpdatePendingRowHeights( nTabCount-1, true );
57 
58  SCTAB nTab;
59  for ( nTab=0; nTab<nTabCount; nTab++ )
60  {
61  tools::Long nAttrPage = nTab > 0 ? nFirstAttr[nTab-1] : 1;
62 
63  tools::Long nThisTab = 0;
64  if ( rMark.GetTableSelect( nTab ) )
65  {
66  ScPrintFunc aFunc( pDocSh, pPrinter, nTab, nAttrPage, 0, pSelRange, &aSelection.GetOptions() );
67  nThisTab = aFunc.GetTotalPages();
68  nFirstAttr.push_back( aFunc.GetFirstPageNo() ); // from page style or previous sheet
69  }
70  else
71  nFirstAttr.push_back( nAttrPage );
72 
73  nPages.push_back( nThisTab );
74  nTotalPages += nThisTab;
75  }
76 }
77 
79 {
80 }
81 
83 {
84  if ( bLocInitialized )
85  return; // initialize only once
86 
87  ScRange aRange;
88  const ScRange* pSelRange = nullptr;
89  if ( rMark.IsMarked() )
90  {
91  rMark.GetMarkArea( aRange );
92  pSelRange = &aRange;
93  }
94 
95  tools::Long nRenderer = 0; // 0-based physical page number across sheets
96  tools::Long nTabStart = 0;
97 
98  ScDocument& rDoc = pDocSh->GetDocument();
99  SCTAB nTabCount = rDoc.GetTableCount();
100  for (SCTAB nTab : rMark)
101  {
102  if (nTab >= nTabCount)
103  break;
104  ScPrintFunc aFunc( pDev, pDocSh, nTab, nFirstAttr[nTab], nTotalPages, pSelRange, &aSelection.GetOptions() );
105  aFunc.SetRenderFlag( true );
106 
107  tools::Long nDisplayStart = GetDisplayStart( nTab );
108 
109  for ( tools::Long nPage=0; nPage<nPages[nTab]; nPage++ )
110  {
111  Range aPageRange( nRenderer+1, nRenderer+1 );
112  MultiSelection aPage( aPageRange );
113  aPage.SetTotalRange( Range(0,RANGE_MAX) );
114  aPage.Select( aPageRange );
115 
116  ScPreviewLocationData aLocData( &rDoc, pDev );
117  aFunc.DoPrint( aPage, nTabStart, nDisplayStart, false, &aLocData );
118 
119  ScRange aCellRange;
120  tools::Rectangle aPixRect;
121  if ( aLocData.GetMainCellRange( aCellRange, aPixRect ) )
122  aLocations.emplace_back( nRenderer, aCellRange, aPixRect );
123 
124  ++nRenderer;
125  }
126 
127  nTabStart += nPages[nTab];
128  }
129 
130  bLocInitialized = true;
131 }
132 
133 bool ScPrintFuncCache::FindLocation( const ScAddress& rCell, ScPrintPageLocation& rLocation ) const
134 {
135  auto aIter = std::find_if(aLocations.begin(), aLocations.end(),
136  [&rCell](const ScPrintPageLocation& rLoc) { return rLoc.aCellRange.In(rCell); });
137  if (aIter != aLocations.end())
138  {
139  rLocation = *aIter;
140  return true;
141  }
142  return false; // not found
143 }
144 
146 {
147  return aSelection == rStatus;
148 }
149 
151 {
152  ScDocument& rDoc = pDocSh->GetDocument();
153  SCTAB nTabCount = rDoc.GetTableCount();
154  SCTAB nTab = 0;
155  while ( nTab < nTabCount && nPage >= nPages[nTab] )
156  nPage -= nPages[nTab++];
157  if (nTab >= nTabCount)
158  nTab = nTabCount - 1;
159  return nTab;
160 }
161 
163 {
164  tools::Long nRet = 0;
165  const SCTAB maxIndex = std::min(nTab, static_cast<SCTAB>(nPages.size()));
166  for ( SCTAB i=0; i<maxIndex; i++ )
167  nRet += nPages[i];
168  return nRet;
169 }
170 
172 {
174 
175  tools::Long nDisplayStart = 0;
176  ScDocument& rDoc = pDocSh->GetDocument();
177  for (SCTAB i=0; i<nTab; i++)
178  {
179  if ( rDoc.NeedPageResetAfterTab(i) )
180  nDisplayStart = 0;
181  else
182  {
183  if ( i < static_cast<SCTAB>(nPages.size()) )
184  nDisplayStart += nPages[i];
185  else
186  OSL_FAIL("nPages out of bounds, FIX IT!");
187  }
188  }
189  return nDisplayStart;
190 }
191 
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Stores the selection in the ScPrintFuncCache so it is only used for the same selection again...
Definition: pfuncache.hxx:46
void UpdatePendingRowHeights(SCTAB nUpdateTab, bool bBefore=false)
Definition: docsh5.cxx:433
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
Definition: markdata.hxx:43
SfxPrinter * GetPrinter(bool bCreateIfNotExist=true)
Definition: docsh3.cxx:450
std::vector< long > nFirstAttr
Definition: pfuncache.hxx:91
tools::Long nTotalPages
Definition: pfuncache.hxx:89
long Long
The range that is printed on a page (excluding repeated columns/rows), and its position on the page...
Definition: pfuncache.hxx:69
bool Select(sal_Int32 nIndex, bool bSelect=true)
void GetMarkArea(ScRange &rRange) const
Definition: markdata.cxx:112
ScPrintFuncCache(ScDocShell *pD, const ScMarkData &rMark, const ScPrintSelectionStatus &rStatus)
Definition: pfuncache.cxx:29
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:314
SCTAB GetSelectCount() const
Definition: markdata.cxx:195
bool IsMarked() const
Definition: markdata.hxx:82
int i
tools::Long GetTotalPages() const
Definition: printfun.hxx:328
ScDocShell * pDocSh
Definition: pfuncache.hxx:88
bool FindLocation(const ScAddress &rCell, ScPrintPageLocation &rLocation) const
Definition: pfuncache.cxx:133
void InitLocations(const ScMarkData &rMark, OutputDevice *pDev)
Definition: pfuncache.cxx:82
tools::Long GetTabStart(SCTAB nTab) const
Definition: pfuncache.cxx:162
#define RANGE_MAX
void SetTotalRange(const Range &rTotRange)
SC_DLLPUBLIC bool NeedPageResetAfterTab(SCTAB nTab) const
Definition: document.cxx:6352
const ScDocument & GetDocument() const
Definition: docsh.hxx:216
SCTAB GetTabForPage(tools::Long nPage) const
Definition: pfuncache.cxx:150
bool IsSameSelection(const ScPrintSelectionStatus &rStatus) const
Definition: pfuncache.cxx:145
tools::Long GetDisplayStart(SCTAB nTab) const
Definition: pfuncache.cxx:171
const ScPrintOptions & GetOptions() const
Definition: pfuncache.hxx:63
bool GetTableSelect(SCTAB nTab) const
Definition: markdata.cxx:184
void SetRenderFlag(bool bFlag)
Definition: printfun.cxx:2382
bool GetMainCellRange(ScRange &rRange, tools::Rectangle &rPixRect) const
Definition: prevloc.cxx:703
ScPrintSelectionStatus aSelection
Definition: pfuncache.hxx:87
std::vector< ScPrintPageLocation > aLocations
Definition: pfuncache.hxx:92
sal_Int16 SCTAB
Definition: types.hxx:23
std::vector< long > nPages
Definition: pfuncache.hxx:90