LibreOffice Module sc (master)  1
lotattr.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 <memory>
21 #include <lotattr.hxx>
22 
23 #include <editeng/borderline.hxx>
24 #include <editeng/boxitem.hxx>
25 #include <editeng/brushitem.hxx>
26 #include <editeng/colritem.hxx>
27 #include <editeng/justifyitem.hxx>
28 #include <sal/log.hxx>
29 
30 #include <docpool.hxx>
31 #include <document.hxx>
32 #include <lotfntbf.hxx>
33 #include "lotfilter.hxx"
34 #include <patattr.hxx>
35 #include <scitems.hxx>
36 
37 using namespace ::com::sun::star;
38 
39 LotAttrCache::ENTRY::ENTRY (std::unique_ptr<ScPatternAttr> p)
40  : pPattAttr(std::move(p))
41  , nHash0(0)
42 {
43 }
44 
46 {
47 }
48 
50  : mrContext(rContext)
51 {
52  pDocPool = rContext.rDoc.GetPool();
53 
54  pColTab.reset( new Color [ 8 ] );
55  pColTab[ 0 ] = COL_WHITE;
56  pColTab[ 1 ] = COL_LIGHTBLUE;
57  pColTab[ 2 ] = COL_LIGHTGREEN;
58  pColTab[ 3 ] = COL_LIGHTCYAN;
59  pColTab[ 4 ] = COL_LIGHTRED;
61  pColTab[ 6 ] = COL_YELLOW;
62  pColTab[ 7 ] = COL_BLACK;
63 
64  ppColorItems[ 0 ].reset( new SvxColorItem( GetColor( 1 ), ATTR_FONT_COLOR ) ); // 1
65  ppColorItems[ 1 ].reset( new SvxColorItem( GetColor( 2 ), ATTR_FONT_COLOR ) );
66  ppColorItems[ 2 ].reset( new SvxColorItem( GetColor( 3 ), ATTR_FONT_COLOR ) );
67  ppColorItems[ 3 ].reset( new SvxColorItem( GetColor( 4 ), ATTR_FONT_COLOR ) );
68  ppColorItems[ 4 ].reset( new SvxColorItem( GetColor( 5 ), ATTR_FONT_COLOR ) );
69  ppColorItems[ 5 ].reset( new SvxColorItem( GetColor( 6 ), ATTR_FONT_COLOR ) ); // 6
70 
72 }
73 
75 {
76 }
77 
79 {
80  sal_uInt32 nRefHash;
81  MakeHash( rAttr, nRefHash );
82 
83  std::vector< std::unique_ptr<ENTRY> >::const_iterator iter
84  = std::find_if(aEntries.begin(),aEntries.end(),
85  [nRefHash] (const std::unique_ptr<ENTRY>& rEntry) { return rEntry->nHash0 == nRefHash; } );
86 
87  if (iter != aEntries.end())
88  return *((*iter)->pPattAttr);
89 
90  // generate new Pattern Attribute
91  ScPatternAttr* pNewPatt = new ScPatternAttr(pDocPool);
92 
93  SfxItemSet& rItemSet = pNewPatt->GetItemSet();
94  ENTRY *pCurrent = new ENTRY( std::unique_ptr<ScPatternAttr>(pNewPatt) );
95 
96  pCurrent->nHash0 = nRefHash;
97 
98  mrContext.maFontBuff.Fill( rAttr.nFont, rItemSet );
99 
100  sal_uInt8 nLine = rAttr.nLineStyle;
101  if( nLine )
102  {
103  SvxBoxItem aBox( ATTR_BORDER );
104  ::editeng::SvxBorderLine aTop, aLeft, aBottom, aRight;
105 
106  LotusToScBorderLine( nLine, aLeft );
107  nLine >>= 2;
108  LotusToScBorderLine( nLine, aRight );
109  nLine >>= 2;
110  LotusToScBorderLine( nLine, aTop );
111  nLine >>= 2;
112  LotusToScBorderLine( nLine, aBottom );
113 
114  aBox.SetLine( &aTop, SvxBoxItemLine::TOP );
115  aBox.SetLine( &aLeft, SvxBoxItemLine::LEFT );
116  aBox.SetLine( &aBottom, SvxBoxItemLine::BOTTOM );
117  aBox.SetLine( &aRight, SvxBoxItemLine::RIGHT );
118 
119  rItemSet.Put( aBox );
120  }
121 
122  sal_uInt8 nFontCol = rAttr.nFontCol & 0x07;
123  if( nFontCol )
124  {
125  // nFontCol > 0
126  if( nFontCol < 7 )
127  rItemSet.Put( GetColorItem( nFontCol ) );
128  else
129  rItemSet.Put( *pWhite );
130  }
131 
132  sal_uInt8 nBack = rAttr.nBack & 0x1F;
133  if( nBack )
134  rItemSet.Put( SvxBrushItem( GetColor( nBack & 0x07 ), ATTR_BACKGROUND ) );
135 
136  if( rAttr.nBack & 0x80 )
137  {
138  SvxHorJustifyItem aHorJustify(SvxCellHorJustify::Center, ATTR_HOR_JUSTIFY );
139  rItemSet.Put( aHorJustify );
140  }
141 
142  aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
143 
144  return *pNewPatt;
145 }
146 
148 {
149  nLine &= 0x03;
150 
151  switch ( nLine )
152  {
153  case 0: aBL.SetBorderLineStyle(SvxBorderLineStyle::NONE); break;
154  case 1: aBL.SetWidth( DEF_LINE_WIDTH_1 ); break;
155  case 2: aBL.SetWidth( DEF_LINE_WIDTH_2 ); break;
156  case 3:
157  {
158  aBL.SetBorderLineStyle(SvxBorderLineStyle::DOUBLE_THIN);
159  aBL.SetWidth( DEF_LINE_WIDTH_1 );
160  }
161  break;
162  }
163 }
164 
165 const SvxColorItem& LotAttrCache::GetColorItem( const sal_uInt8 nLotIndex ) const
166 {
167  // *LotAttrCache::GetColorItem(): caller has to check index!
168  assert( nLotIndex > 0 && nLotIndex < 7 );
169 
170  return *ppColorItems[ nLotIndex - 1 ];
171 }
172 
173 const Color& LotAttrCache::GetColor( const sal_uInt8 nLotIndex ) const
174 {
175  // color <-> index fits background, but not for fonts (0 <-> 7)!
176  // *LotAttrCache::GetColor(): Index > 7, caller hast to check index!"
177  assert( nLotIndex < 8 );
178 
179  return pColTab[ nLotIndex ];
180 }
181 
182 void LotAttrCol::SetAttr( const ScDocument* pDoc, const SCROW nRow, const ScPatternAttr& rAttr )
183 {
184  // Actually with the current implementation of MAXROWCOUNT1>=64k and nRow
185  // being read as sal_uInt16 there's no chance that nRow would be invalid...
186  SAL_WARN_IF( !pDoc->ValidRow(nRow), "sc.filter", "*LotAttrCol::SetAttr(): ... and failed?!" );
187 
188  std::vector<std::unique_ptr<ENTRY> >::reverse_iterator iterLast = aEntries.rbegin();
189 
190  if(iterLast != aEntries.rend())
191  {
192  if( ( (*iterLast)->nLastRow == nRow - 1 ) && ( &rAttr == (*iterLast)->pPattAttr ) )
193  (*iterLast)->nLastRow = nRow;
194  else
195  {
196  ENTRY *pCurrent = new ENTRY;
197 
198  pCurrent->pPattAttr = &rAttr;
199  pCurrent->nFirstRow = pCurrent->nLastRow = nRow;
200 
201  aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
202  }
203  }
204  else
205  { // first entry
206  ENTRY *pCurrent = new ENTRY;
207  pCurrent->pPattAttr = &rAttr;
208  pCurrent->nFirstRow = pCurrent->nLastRow = nRow;
209 
210  aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
211  }
212 }
213 
214 void LotAttrCol::Apply(LotusContext& rContext, const SCCOL nColNum, const SCTAB nTabNum)
215 {
216  ScDocument& rDoc = rContext.rDoc;
217 
218  for (const auto& rxEntry : aEntries)
219  {
220  rDoc.ApplyPatternAreaTab(nColNum, rxEntry->nFirstRow, nColNum, rxEntry->nLastRow,
221  nTabNum, *(rxEntry->pPattAttr));
222  }
223 }
224 
226  aAttrCache(rContext)
227 {
228 }
229 
230 void LotAttrTable::SetAttr( LotusContext& rContext, const SCCOL nColFirst, const SCCOL nColLast, const SCROW nRow,
231  const LotAttrWK3& rAttr )
232 {
233  // With the current implementation of MAXCOLCOUNT>=1024 and nColFirst and
234  // nColLast being calculated as sal_uInt8+sal_uInt8 there's no chance that
235  // they would be invalid.
236  const ScPatternAttr &rPattAttr = aAttrCache.GetPattAttr( rAttr );
237  SCCOL nColCnt;
238 
239  for( nColCnt = nColFirst ; nColCnt <= nColLast ; nColCnt++ )
240  pCols[ nColCnt ].SetAttr( &rContext.rDoc, nRow, rPattAttr );
241 }
242 
243 void LotAttrTable::Apply(LotusContext& rContext, const SCTAB nTabNum)
244 {
245  SCCOL nColCnt;
246  for( nColCnt = 0 ; nColCnt <= aAttrCache.mrContext.rDoc.MaxCol() ; nColCnt++ )
247  pCols[ nColCnt ].Apply(rContext, nColCnt, nTabNum); // does a Clear() at end
248 }
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt32 nHash0
Definition: lotattr.hxx:71
void SetBorderLineStyle(SvxBorderLineStyle nNew)
sal_uInt8 nFont
Definition: lotattr.hxx:37
void SetWidth(tools::Long nWidth)
static void LotusToScBorderLine(sal_uInt8 nLine,::editeng::SvxBorderLine &)
Definition: lotattr.cxx:147
LotusContext & mrContext
Definition: lotattr.hxx:98
constexpr TypedWhichId< SvxBoxItem > ATTR_BORDER(150)
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTMAGENTA
const SvxColorItem & GetColorItem(const sal_uInt8 nLotIndex) const
Definition: lotattr.cxx:165
void Apply(LotusContext &rContext, const SCCOL nCol, const SCTAB nTab)
Definition: lotattr.cxx:214
LotAttrCache aAttrCache
Definition: lotattr.hxx:131
std::unique_ptr< Color[]> pColTab
Definition: lotattr.hxx:95
const SfxItemSet & GetItemSet() const
LotAttrCache(LotusContext &rContext)
Definition: lotattr.cxx:49
ENTRY(std::unique_ptr< ScPatternAttr > p)
Definition: lotattr.cxx:39
ScDocumentPool * pDocPool
Definition: lotattr.hxx:92
#define DEF_LINE_WIDTH_1
ScDocument & rDoc
Definition: lotfilter.hxx:44
void Apply(LotusContext &rContext, const SCTAB nTabNum)
Definition: lotattr.cxx:243
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6087
const ScPatternAttr * pPattAttr
Definition: lotattr.hxx:111
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTRED
LotAttrTable(LotusContext &rContext)
Definition: lotattr.cxx:225
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:871
sal_uInt8 nLineStyle
Definition: lotattr.hxx:38
sal_Int16 SCCOL
Definition: types.hxx:21
std::vector< std::unique_ptr< ENTRY > > aEntries
Definition: lotattr.hxx:116
std::vector< std::unique_ptr< ENTRY > > aEntries
Definition: lotattr.hxx:96
SC_DLLPUBLIC void ApplyPatternAreaTab(SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow, SCTAB nTab, const ScPatternAttr &rAttr)
Definition: document.cxx:4817
const Color & GetColor(const sal_uInt8 nLotIndex) const
Definition: lotattr.cxx:173
LotusFontBuffer maFontBuff
Definition: lotfilter.hxx:56
constexpr TypedWhichId< SvxBrushItem > ATTR_BACKGROUND(148)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
sal_Int32 SCROW
Definition: types.hxx:17
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTCYAN
static void MakeHash(const LotAttrWK3 &rAttr, sal_uInt32 &rOut)
Definition: lotattr.hxx:78
bool ValidRow(SCROW nRow) const
Definition: document.hxx:875
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
void SetAttr(const ScDocument *pDoc, const SCROW nRow, const ScPatternAttr &)
Definition: lotattr.cxx:182
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_WHITE
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTGREEN
constexpr TypedWhichId< SvxHorJustifyItem > ATTR_HOR_JUSTIFY(129)
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_YELLOW
std::unique_ptr< SvxColorItem > pWhite
Definition: lotattr.hxx:94
#define DEF_LINE_WIDTH_2
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTBLUE
sal_uInt8 nFontCol
Definition: lotattr.hxx:39
LotAttrCol pCols[MAXCOLCOUNT]
Definition: lotattr.hxx:130
void SetAttr(LotusContext &rContext, const SCCOL nColFirst, const SCCOL nColLast, const SCROW nRow, const LotAttrWK3 &)
Definition: lotattr.cxx:230
std::unique_ptr< SvxColorItem > ppColorItems[6]
Definition: lotattr.hxx:93
sal_uInt8 nBack
Definition: lotattr.hxx:40
constexpr TypedWhichId< SvxColorItem > ATTR_FONT_COLOR(109)
const ScPatternAttr & GetPattAttr(const LotAttrWK3 &)
Definition: lotattr.cxx:78
sal_Int16 SCTAB
Definition: types.hxx:22
void Fill(const sal_uInt8 nIndex, SfxItemSet &rItemSet)
Definition: fontbuff.cxx:31
void SetLine(const editeng::SvxBorderLine *pNew, SvxBoxItemLine nLine)