Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / sw / source / core / docnode / ndnotxt.cxx
blob1ca38f198b0ced33817a51b4bc926ba9b380515b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <hintids.hxx>
21 #include <osl/diagnose.h>
22 #include <tools/poly.hxx>
23 #include <svl/stritem.hxx>
24 #include <svx/contdlg.hxx>
25 #include <vcl/svapp.hxx>
26 #include <doc.hxx>
27 #include <fmtcol.hxx>
28 #include <ndnotxt.hxx>
29 #include <ndgrf.hxx>
30 #include <ndole.hxx>
31 #include <ndindex.hxx>
32 #include <istyleaccess.hxx>
33 #include <SwStyleNameMapper.hxx>
35 #include <frmfmt.hxx>
36 #include <names.hxx>
38 SwNoTextNode::SwNoTextNode( SwNode& rWhere,
39 const SwNodeType nNdType,
40 SwGrfFormatColl *pGrfColl,
41 SwAttrSet const * pAutoAttr ) :
42 SwContentNode( rWhere, nNdType, pGrfColl ),
43 m_bAutomaticContour( false ),
44 m_bContourMapModeValid( true ),
45 m_bPixelContour( false )
47 // Should this set a hard attribute?
48 if( pAutoAttr )
49 SetAttr( *pAutoAttr );
52 SwNoTextNode::~SwNoTextNode()
56 /// Creates an AttrSet for all derivations with ranges for frame-
57 /// and graphics-attributes.
58 void SwNoTextNode::NewAttrSet( SwAttrPool& rPool )
60 OSL_ENSURE( !mpAttrSet, "AttrSet is already set" );
61 SwAttrSet aNewAttrSet( rPool, aNoTextNodeSetRange );
63 // put names of parent style and conditional style:
64 const SwFormatColl* pFormatColl = GetFormatColl();
65 ProgName sVal;
66 SwStyleNameMapper::FillProgName( pFormatColl->GetName(), sVal, SwGetPoolIdFromName::TxtColl );
67 SfxStringItem aFormatColl( RES_FRMATR_STYLE_NAME, sVal.toString() );
68 aNewAttrSet.Put( aFormatColl );
70 aNewAttrSet.SetParent( &GetFormatColl()->GetAttrSet() );
71 mpAttrSet = GetDoc().GetIStyleAccess().getAutomaticStyle( aNewAttrSet, IStyleAccess::AUTO_STYLE_NOTXT );
74 /// Dummies for loading/saving of persistent data
75 /// when working with graphics and OLE objects
76 bool SwNoTextNode::RestorePersistentData()
78 return true;
81 bool SwNoTextNode::SavePersistentData()
83 return true;
86 void SwNoTextNode::SetContour( const tools::PolyPolygon *pPoly, bool bAutomatic )
88 if ( pPoly )
89 m_pContour = *pPoly;
90 else
91 m_pContour.reset();
92 m_bAutomaticContour = bAutomatic;
93 m_bContourMapModeValid = true;
94 m_bPixelContour = false;
97 void SwNoTextNode::CreateContour()
99 OSL_ENSURE( !m_pContour, "Contour available." );
100 m_pContour = SvxContourDlg::CreateAutoContour(GetGraphic());
101 m_bAutomaticContour = true;
102 m_bContourMapModeValid = true;
103 m_bPixelContour = false;
106 const tools::PolyPolygon *SwNoTextNode::HasContour() const
108 if( !m_bContourMapModeValid )
110 const MapMode aGrfMap( GetGraphic().GetPrefMapMode() );
111 bool bPixelGrf = aGrfMap.GetMapUnit() == MapUnit::MapPixel;
112 const MapMode aContourMap( bPixelGrf ? MapUnit::MapPixel : MapUnit::Map100thMM );
113 if( bPixelGrf ? !m_bPixelContour : aGrfMap != aContourMap )
115 double nGrfDPIx = 0.0;
116 double nGrfDPIy = 0.0;
118 if ( !bPixelGrf && m_bPixelContour )
120 basegfx::B2DSize aDPI = GetGraphic().GetPPI();
121 nGrfDPIx = aDPI.getWidth();
122 nGrfDPIy = aDPI.getHeight();
125 OSL_ENSURE( !bPixelGrf || aGrfMap == aContourMap,
126 "scale factor for pixel unsupported" );
127 OutputDevice* pOutDev =
128 (bPixelGrf || m_bPixelContour) ? Application::GetDefaultDevice()
129 : nullptr;
131 for ( auto& rPoly : *m_pContour )
133 sal_uInt16 nCount = rPoly.GetSize();
134 for( sal_uInt16 i=0 ; i<nCount; i++ )
136 if( bPixelGrf )
137 rPoly[i] = pOutDev->LogicToPixel( rPoly[i],
138 aContourMap );
139 else if( m_bPixelContour )
141 rPoly[i] = pOutDev->PixelToLogic( rPoly[i], aGrfMap );
143 if ( nGrfDPIx != 0 && nGrfDPIy != 0 )
145 rPoly[i] = Point( rPoly[i].getX() * pOutDev->GetDPIX() / nGrfDPIx,
146 rPoly[i].getY() * pOutDev->GetDPIY() / nGrfDPIy );
149 else
150 rPoly[i] = OutputDevice::LogicToLogic( rPoly[i],
151 aContourMap,
152 aGrfMap );
156 m_bContourMapModeValid = true;
157 m_bPixelContour = false;
160 return m_pContour ? &*m_pContour : nullptr;
163 void SwNoTextNode::GetContour( tools::PolyPolygon &rPoly ) const
165 OSL_ENSURE( m_pContour, "Contour not available." );
166 rPoly = *HasContour();
169 void SwNoTextNode::SetContourAPI( const tools::PolyPolygon *pPoly )
171 if ( pPoly )
172 m_pContour = *pPoly;
173 else
174 m_pContour.reset();
175 m_bContourMapModeValid = false;
178 bool SwNoTextNode::GetContourAPI( tools::PolyPolygon &rContour ) const
180 if( !m_pContour )
181 return false;
183 rContour = *m_pContour;
184 if( m_bContourMapModeValid )
186 const MapMode aGrfMap( GetGraphic().GetPrefMapMode() );
187 const MapMode aContourMap( MapUnit::Map100thMM );
188 OSL_ENSURE( aGrfMap.GetMapUnit() != MapUnit::MapPixel ||
189 aGrfMap == MapMode( MapUnit::MapPixel ),
190 "scale factor for pixel unsupported" );
191 if( aGrfMap.GetMapUnit() != MapUnit::MapPixel &&
192 aGrfMap != aContourMap )
194 for( auto& rPoly : rContour )
196 sal_uInt16 nCount = rPoly.GetSize();
197 for( sal_uInt16 i=0 ; i<nCount; i++ )
199 rPoly[i] = OutputDevice::LogicToLogic( rPoly[i], aGrfMap,
200 aContourMap );
206 return true;
209 bool SwNoTextNode::IsPixelContour() const
211 bool bRet;
212 if( m_bContourMapModeValid )
214 const MapMode aGrfMap( GetGraphic().GetPrefMapMode() );
215 bRet = aGrfMap.GetMapUnit() == MapUnit::MapPixel;
217 else
219 bRet = m_bPixelContour;
222 return bRet;
225 Graphic SwNoTextNode::GetGraphic() const
227 Graphic aRet;
228 if ( GetGrfNode() )
230 aRet = static_cast<const SwGrfNode*>(this)->GetGrf(true);
232 else
234 OSL_ENSURE( GetOLENode(), "new type of Node?" );
235 if (const Graphic* pGraphic = const_cast<SwOLENode*>(static_cast<const SwOLENode*>(this))->SwOLENode::GetGraphic())
236 aRet = *pGraphic;
238 return aRet;
241 // #i73249#
242 void SwNoTextNode::SetTitle( const OUString& rTitle )
244 // Title attribute of <SdrObject> replaces own AlternateText attribute
245 SwFlyFrameFormat* pFlyFormat = dynamic_cast<SwFlyFrameFormat*>(GetFlyFormat());
246 OSL_ENSURE( pFlyFormat, "<SwNoTextNode::SetTitle(..)> - missing <SwFlyFrameFormat> instance" );
247 if ( !pFlyFormat )
249 return;
252 pFlyFormat->SetObjTitle( rTitle );
255 OUString SwNoTextNode::GetTitle() const
257 const SwFlyFrameFormat* pFlyFormat = dynamic_cast<const SwFlyFrameFormat*>(GetFlyFormat());
258 OSL_ENSURE( pFlyFormat, "<SwNoTextNode::GetTitle(..)> - missing <SwFlyFrameFormat> instance" );
259 if ( !pFlyFormat )
261 return OUString();
264 return pFlyFormat->GetObjTitle();
267 void SwNoTextNode::SetDescription( const OUString& rDescription )
269 SwFlyFrameFormat* pFlyFormat = dynamic_cast<SwFlyFrameFormat*>(GetFlyFormat());
270 OSL_ENSURE( pFlyFormat, "<SwNoTextNode::SetDescription(..)> - missing <SwFlyFrameFormat> instance" );
271 if ( !pFlyFormat )
273 return;
276 pFlyFormat->SetObjDescription( rDescription );
279 OUString SwNoTextNode::GetDescription() const
281 const SwFlyFrameFormat* pFlyFormat = dynamic_cast<const SwFlyFrameFormat*>(GetFlyFormat());
282 OSL_ENSURE( pFlyFormat, "<SwNoTextNode::GetDescription(..)> - missing <SwFlyFrameFormat> instance" );
283 if ( !pFlyFormat )
285 return OUString();
288 return pFlyFormat->GetObjDescription();
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */