LibreOffice Module sw (master)  1
grfatr.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 <com/sun/star/drawing/ColorMode.hpp>
21 #include <o3tl/any.hxx>
22 #include <vcl/GraphicObject.hxx>
23 #include <grfatr.hxx>
24 #include <swunohelper.hxx>
25 #include <osl/diagnose.h>
26 
27 #include <unomid.h>
28 
29 using namespace ::com::sun::star;
30 
32 {
33  return new SwMirrorGrf( *this );
34 }
35 
36 sal_uInt16 SwMirrorGrf::GetValueCount() const
37 {
38  return 4;
39 }
40 
41 bool SwMirrorGrf::operator==( const SfxPoolItem& rItem) const
42 {
43  return SfxEnumItem::operator==(static_cast<const SfxEnumItem<MirrorGraph>&>(rItem)) &&
44  static_cast<const SwMirrorGrf&>(rItem).IsGrfToggle() == IsGrfToggle();
45 }
46 
47 static bool lcl_IsHoriOnEvenPages(MirrorGraph nEnum, bool bToggle)
48 {
49  bool bEnum = nEnum == MirrorGraph::Vertical ||
50  nEnum == MirrorGraph::Both;
51  return bEnum != bToggle;
52 }
53 
55 {
56  bool bEnum = nEnum == MirrorGraph::Vertical ||
57  nEnum == MirrorGraph::Both;
58  return bEnum;
59 }
60 
61 bool SwMirrorGrf::QueryValue( uno::Any& rVal, sal_uInt8 nMemberId ) const
62 {
63  bool bRet = true;
64  bool bVal = false;
65  // vertical and horizontal were swapped at some point
66  nMemberId &= ~CONVERT_TWIPS;
67  switch ( nMemberId )
68  {
71  break;
74  break;
75  case MID_MIRROR_VERT:
76  bVal = GetValue() == MirrorGraph::Horizontal ||
78  break;
79  default:
80  OSL_ENSURE( false, "unknown MemberId" );
81  bRet = false;
82  }
83  rVal <<= bVal;
84  return bRet;
85 }
86 
87 bool SwMirrorGrf::PutValue( const uno::Any& rVal, sal_uInt8 nMemberId )
88 {
89  bool bRet = true;
90  bool bVal = *o3tl::doAccess<bool>(rVal);
91  // vertical and horizontal were swapped at some point
92  nMemberId &= ~CONVERT_TWIPS;
93  switch ( nMemberId )
94  {
97  {
98  bool bIsVert = GetValue() == MirrorGraph::Horizontal ||
100  bool bOnOddPages = nMemberId == MID_MIRROR_HORZ_EVEN_PAGES ?
101  lcl_IsHoriOnOddPages(GetValue()) : bVal;
102  bool bOnEvenPages = nMemberId == MID_MIRROR_HORZ_ODD_PAGES ?
104  MirrorGraph nEnum = bOnOddPages ?
107  bool bToggle = bOnOddPages != bOnEvenPages;
108  SetValue(nEnum);
109  SetGrfToggle( bToggle );
110  }
111  break;
112  case MID_MIRROR_VERT:
113  if ( bVal )
114  {
115  if ( GetValue() == MirrorGraph::Vertical )
117  else if ( GetValue() != MirrorGraph::Both )
119  }
120  else
121  {
122  if ( GetValue() == MirrorGraph::Both )
124  else if ( GetValue() == MirrorGraph::Horizontal )
126  }
127  break;
128  default:
129  OSL_ENSURE( false, "unknown MemberId" );
130  bRet = false;
131  }
132  return bRet;
133 }
134 
137 {}
138 
139 SwCropGrf::SwCropGrf(sal_Int32 nL, sal_Int32 nR, sal_Int32 nT, sal_Int32 nB )
140  : SvxGrfCrop( nL, nR, nT, nB, RES_GRFATR_CROPGRF )
141 {}
142 
144 {
145  return new SwCropGrf( *this );
146 }
147 
148 sal_Int16 SwRotationGrf::checkAndCorrectValue(sal_Int16 nValue)
149 {
150  if(nValue < 0)
151  {
152  // smaller zero, modulo (will keep negative) and add one range
153  DBG_ASSERT(false, "SwRotationGrf: Value is in 10th degree and *has* to be in [0 .. 3600[ (!)");
154  return 3600 + (nValue % 3600);
155  }
156  else if (nValue >= 3600)
157  {
158  // bigger range, use modulo
159  DBG_ASSERT(false, "SwRotationGrf: Value is in 10th degree and *has* to be in [0 .. 3600[ (!)");
160  return nValue % 3600;
161  }
162 
163  return nValue;
164 }
165 
166 SwRotationGrf::SwRotationGrf( sal_Int16 nVal, const Size& rSz )
167  // tdf#115529 check and evtl. correct value
168 : SfxUInt16Item( RES_GRFATR_ROTATION, checkAndCorrectValue(nVal) ),
169  m_aUnrotatedSize( rSz )
170 {
171 }
172 
174 {
175  return new SwRotationGrf( *this );
176 }
177 
178 bool SwRotationGrf::operator==( const SfxPoolItem& rCmp ) const
179 {
180  return SfxUInt16Item::operator==( rCmp ) &&
181  GetUnrotatedSize() == static_cast<const SwRotationGrf&>(rCmp).GetUnrotatedSize();
182 }
183 
185 {
186  // SfxUInt16Item::QueryValue returns sal_Int32 in Any now... (srx642w)
187  // where we still want this to be a sal_Int16
188  rVal <<= static_cast<sal_Int16>(GetValue());
189  return true;
190 }
191 
193 {
194  // SfxUInt16Item::QueryValue returns sal_Int32 in Any now... (srx642w)
195  // where we still want this to be a sal_Int16
196  sal_Int16 nValue = 0;
197  if (rVal >>= nValue)
198  {
199  // sal_uInt16 argument needed
200  // tdf#115529 check and evtl. correct value
201  SetValue(static_cast<sal_uInt16>(checkAndCorrectValue(nValue)));
202  return true;
203  }
204 
205  OSL_FAIL( "SwRotationGrf::PutValue - Wrong type!" );
206  return false;
207 }
208 
209 // Sw___Grf::Clone(..)
210 
212 {
213  return new SwLuminanceGrf( *this );
214 }
215 
217 {
218  return new SwContrastGrf( *this );
219 }
220 
222 {
223  return new SwChannelRGrf( *this );
224 }
225 
227 {
228  return new SwChannelGGrf( *this );
229 }
230 
232 {
233  return new SwChannelBGrf( *this );
234 }
235 
237 {
238  return new SwGammaGrf( *this );
239 }
240 
241 // SwGammaGrf
242 
243 bool SwGammaGrf::operator==( const SfxPoolItem& rCmp ) const
244 {
245  return SfxPoolItem::operator==( rCmp ) &&
246  m_nValue == static_cast<const SwGammaGrf&>(rCmp).GetValue();
247 }
248 
250 {
251  rVal <<= m_nValue;
252  return true;
253 }
254 
256 {
257  return rVal >>= m_nValue;
258 }
259 
260 // Sw___Grf::Clone(..) cont'd
261 
263 {
264  return new SwInvertGrf( *this );
265 }
266 
268 {
269  return new SwTransparencyGrf( *this );
270 }
271 
272 // SwTransparencyGrf
273 
275  sal_uInt8 ) const
276 {
277  OSL_ENSURE(dynamic_cast<const SfxByteItem*>( this ) != nullptr,"Put/QueryValue should be removed!");
278  sal_Int16 nRet = GetValue();
279  OSL_ENSURE( 0 <= nRet && nRet <= 100, "value out of range" );
280  rVal <<= nRet;
281  return true;
282 }
283 
285  sal_uInt8 )
286 {
287  //temporary conversion until this is a SfxInt16Item!
288  OSL_ENSURE(dynamic_cast<const SfxByteItem*>( this ) != nullptr,"Put/QueryValue should be removed!");
289  sal_Int16 nVal = 0;
290  if(!(rVal >>= nVal) || nVal < -100 || nVal > 100)
291  return false;
292  if(nVal < 0)
293  {
294  // for compatibility with old documents
295  // introduce rounding as for SO 6.0 PP2
296  nVal = ( ( nVal * 128 ) - (99/2) ) / 100;
297  nVal += 128;
298  }
299  OSL_ENSURE( 0 <= nVal && nVal <= 100, "value out of range" );
300  SetValue(static_cast<sal_uInt8>(nVal));
301  return true;
302 }
303 
304 // Sw___Grf::Clone(..) cont'd
305 
307 {
308  return new SwDrawModeGrf( *this );
309 }
310 
311 // SwDrawModeGrf
312 
314 {
315  return sal_uInt16(GraphicDrawMode::Watermark) + 1;
316 }
317 
319  sal_uInt8 ) const
320 {
321  drawing::ColorMode eRet = static_cast<drawing::ColorMode>(GetEnumValue());
322  rVal <<= eRet;
323  return true;
324 }
325 
327  sal_uInt8 )
328 {
329  sal_Int32 eVal = SWUnoHelper::GetEnumAsInt32( rVal );
330  if(eVal >= 0 && eVal <= sal_uInt16(GraphicDrawMode::Watermark))
331  {
332  SetEnumValue(static_cast<sal_uInt16>(eVal));
333  return true;
334  }
335  return false;
336 }
337 
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_uInt16 GetValueCount() const override
Definition: grfatr.cxx:313
virtual bool operator==(const SfxPoolItem &) const override
Definition: grfatr.cxx:178
virtual SwInvertGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:262
const double & GetValue() const
Definition: grfatr.hxx:209
SwChannelGGrf(sal_Int16 nVal=0)
Definition: grfatr.hxx:170
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: grfatr.cxx:274
virtual bool operator==(const SfxPoolItem &) const =0
void SetGrfToggle(bool bNew)
Definition: grfatr.hxx:66
virtual SwChannelBGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:231
virtual SwGammaGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:236
SwChannelBGrf(sal_Int16 nVal=0)
Definition: grfatr.hxx:178
virtual void SetEnumValue(sal_uInt16 nTheValue) override
virtual sal_uInt16 GetValueCount() const override
Definition: grfatr.cxx:36
sal_uInt16 GetValue() const
virtual SwRotationGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:173
MirrorGraph
Definition: grfatr.hxx:31
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: grfatr.cxx:61
const Size & GetUnrotatedSize() const
Definition: grfatr.hxx:108
SwContrastGrf(sal_Int16 nVal=0)
Definition: grfatr.hxx:130
void SetValue(sal_uInt16 nTheValue)
virtual SwChannelGGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:226
virtual bool operator==(SfxPoolItem const &other) const override
SwLuminanceGrf(sal_Int16 nVal=0)
Definition: grfatr.hxx:114
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: grfatr.cxx:184
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: grfatr.cxx:192
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: grfatr.cxx:87
sal_Int16 const nValue
static bool lcl_IsHoriOnOddPages(MirrorGraph nEnum)
Definition: grfatr.cxx:54
virtual bool operator==(const SfxPoolItem &) const override
Definition: grfatr.cxx:243
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: grfatr.cxx:284
#define MID_MIRROR_HORZ_ODD_PAGES
Definition: unomid.h:24
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: grfatr.cxx:326
SwCropGrf()
Definition: grfatr.cxx:135
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: grfatr.cxx:255
virtual SwLuminanceGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:211
virtual SwDrawModeGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:306
virtual SwContrastGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:216
SwDrawModeGrf(GraphicDrawMode nMode=GraphicDrawMode::Standard)
Definition: grfatr.hxx:258
double m_nValue
Definition: grfatr.hxx:186
#define DBG_ASSERT(sCon, aError)
void SetValue(EnumT nTheValue)
#define RES_GRFATR_ROTATION
Definition: hintids.hxx:337
virtual SwTransparencyGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:267
virtual bool operator==(const SfxPoolItem &) const override
Definition: grfatr.cxx:41
virtual SwChannelRGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:221
SwTransparencyGrf(sal_Int8 nVal=0)
Definition: grfatr.hxx:231
virtual SwCropGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:143
#define MID_MIRROR_HORZ_EVEN_PAGES
Definition: unomid.h:23
static sal_Int16 checkAndCorrectValue(sal_Int16 nValue)
Definition: grfatr.cxx:148
static bool lcl_IsHoriOnEvenPages(MirrorGraph nEnum, bool bToggle)
Definition: grfatr.cxx:47
virtual bool operator==(const SfxPoolItem &rItem) const override
unsigned char sal_uInt8
SwInvertGrf(bool bVal=false)
Definition: grfatr.hxx:215
#define RES_GRFATR_CROPGRF
Definition: hintids.hxx:335
SwGammaGrf()
Definition: grfatr.hxx:188
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: grfatr.cxx:318
virtual SwMirrorGrf * Clone(SfxItemPool *pPool=nullptr) const override
Definition: grfatr.cxx:31
sal_uInt8 GetValue() const
void SetValue(sal_uInt8 nTheValue)
#define MID_MIRROR_VERT
Definition: unomid.h:22
SwMirrorGrf(MirrorGraph eMiro=MirrorGraph::Dont)
Definition: grfatr.hxx:44
bool IsGrfToggle() const
Definition: grfatr.hxx:65
SwChannelRGrf(sal_Int16 nVal=0)
Definition: grfatr.hxx:162
sal_Int32 GetEnumAsInt32(const css::uno::Any &rVal)
Definition: swunohelper.cxx:49
virtual sal_uInt16 GetEnumValue() const override
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: grfatr.cxx:249
#define CONVERT_TWIPS