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