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