Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / oox / commentsbuffer.cxx
blob8fa3277517387f8971ed7e666f38b0474b460515
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 <oox/token/properties.hxx>
21 #include <oox/token/tokens.hxx>
23 #include "commentsbuffer.hxx"
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
27 #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
28 #include <com/sun/star/sheet/XSheetAnnotations.hpp>
29 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
30 #include <osl/diagnose.h>
31 #include <oox/helper/attributelist.hxx>
32 #include <oox/vml/vmlshape.hxx>
33 #include "addressconverter.hxx"
34 #include "biffinputstream.hxx"
35 #include "drawingfragment.hxx"
36 #include <svx/sdtaitm.hxx>
37 #include "unitconverter.hxx"
38 #include "drawingmanager.hxx"
40 #include <com/sun/star/text/XText.hpp>
42 using ::com::sun::star::text::XText;
44 namespace oox {
45 namespace xls {
47 using namespace ::com::sun::star::drawing;
48 using namespace ::com::sun::star::sheet;
49 using namespace ::com::sun::star::table;
50 using namespace ::com::sun::star::text;
51 using namespace ::com::sun::star::uno;
53 static sal_Int32 lcl_ToHorizAlign( sal_Int32 nAlign )
55 switch( nAlign )
57 case XML_left:
58 return SDRTEXTHORZADJUST_LEFT;
59 case XML_right:
60 return SDRTEXTHORZADJUST_RIGHT;
61 case XML_center:
62 return SDRTEXTHORZADJUST_CENTER;
63 default:
64 return SDRTEXTHORZADJUST_BLOCK;
68 static sal_Int32 lcl_ToVertAlign( sal_Int32 nAlign )
70 switch( nAlign )
72 case XML_top:
73 return SDRTEXTVERTADJUST_TOP;
74 case XML_center:
75 return SDRTEXTVERTADJUST_CENTER;
76 case XML_bottom:
77 return SDRTEXTVERTADJUST_BOTTOM;
78 default:
79 return SDRTEXTVERTADJUST_BLOCK;
83 CommentModel::CommentModel()
84 : mnAuthorId(-1)
85 , mbAutoFill(false)
86 , mbAutoScale(false)
87 , mbColHidden(false)
88 , mbLocked(false)
89 , mbRowHidden(false)
90 , mnTHA(0)
91 , mnTVA(0)
92 , mbVisible( false )
96 Comment::Comment( const WorksheetHelper& rHelper ) :
97 WorksheetHelper( rHelper )
101 void Comment::importComment( const AttributeList& rAttribs )
103 maModel.mnAuthorId = rAttribs.getInteger( XML_authorId, -1 );
104 // cell range will be checked while inserting the comment into the document
105 AddressConverter::convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), getSheetIndex() );
108 void Comment::importCommentPr( const AttributeList& rAttribs )
110 maModel.mbAutoFill = rAttribs.getBool( XML_autoFill, true );
111 maModel.mbAutoScale = rAttribs.getBool( XML_autoScale, false );
112 maModel.mbColHidden = rAttribs.getBool( XML_colHidden, false );
113 maModel.mbLocked = rAttribs.getBool( XML_locked, false );
114 maModel.mbRowHidden = rAttribs.getBool( XML_rowHidden, false );
115 maModel.mnTHA = rAttribs.getToken( XML_textHAlign, XML_left );
116 maModel.mnTVA = rAttribs.getToken( XML_textVAlign, XML_top );
119 void Comment::importComment( SequenceInputStream& rStrm )
121 BinRange aBinRange;
122 maModel.mnAuthorId = rStrm.readInt32();
123 rStrm >> aBinRange;
124 // cell range will be checked while inserting the comment into the document
125 AddressConverter::convertToCellRangeUnchecked( maModel.maRange, aBinRange, getSheetIndex() );
128 RichStringRef Comment::createText()
130 maModel.mxText.reset( new RichString( *this ) );
131 return maModel.mxText;
134 void Comment::finalizeImport()
136 // BIFF12 stores cell range instead of cell address, use first cell of this range
137 OSL_ENSURE( (maModel.maRange.StartColumn == maModel.maRange.EndColumn) &&
138 (maModel.maRange.StartRow == maModel.maRange.EndRow),
139 "Comment::finalizeImport - comment anchor should be a single cell" );
140 CellAddress aNotePos( maModel.maRange.Sheet, maModel.maRange.StartColumn, maModel.maRange.StartRow );
141 if( getAddressConverter().checkCellAddress( aNotePos, true ) && maModel.mxText.get() ) try
143 Reference< XSheetAnnotationsSupplier > xAnnosSupp( getSheet(), UNO_QUERY_THROW );
144 Reference< XSheetAnnotations > xAnnos( xAnnosSupp->getAnnotations(), UNO_SET_THROW );
145 // non-empty string required by note implementation (real text will be added below)
146 xAnnos->insertNew( aNotePos, OUString( ' ' ) );
148 // receive created note from cell (insertNew does not return the note)
149 Reference< XSheetAnnotationAnchor > xAnnoAnchor( getCell( aNotePos ), UNO_QUERY_THROW );
150 Reference< XSheetAnnotation > xAnno( xAnnoAnchor->getAnnotation(), UNO_SET_THROW );
151 Reference< XSheetAnnotationShapeSupplier > xAnnoShapeSupp( xAnno, UNO_QUERY_THROW );
152 Reference< XShape > xAnnoShape( xAnnoShapeSupp->getAnnotationShape(), UNO_SET_THROW );
154 // convert shape formatting and visibility
155 bool bVisible = true;
156 switch( getFilterType() )
158 case FILTER_OOXML:
160 // Add shape formatting properties (autoFill, colHidden and rowHidden are dropped)
161 PropertySet aCommentPr( xAnnoShape );
162 aCommentPr.setProperty( PROP_TextFitToSize, maModel.mbAutoScale );
163 aCommentPr.setProperty( PROP_MoveProtect, maModel.mbLocked );
164 aCommentPr.setProperty( PROP_TextHorizontalAdjust, lcl_ToHorizAlign( maModel.mnTHA ) );
165 aCommentPr.setProperty( PROP_TextVerticalAdjust, lcl_ToVertAlign( maModel.mnTVA ) );
166 if( maModel.maAnchor.Width > 0 && maModel.maAnchor.Height > 0 )
168 xAnnoShape->setPosition( css::awt::Point( maModel.maAnchor.X, maModel.maAnchor.Y ) );
169 xAnnoShape->setSize( css::awt::Size( maModel.maAnchor.Width, maModel.maAnchor.Height ) );
172 // convert shape formatting and visibility
173 if( const ::oox::vml::ShapeBase* pNoteShape = getVmlDrawing().getNoteShape( aNotePos ) )
175 // position and formatting
176 pNoteShape->convertFormatting( xAnnoShape );
177 // visibility
178 bVisible = pNoteShape->getTypeModel().mbVisible;
181 break;
182 case FILTER_BIFF:
183 bVisible = maModel.mbVisible;
184 break;
185 case FILTER_UNKNOWN:
186 break;
188 xAnno->setIsVisible( bVisible );
190 // insert text and convert text formatting
191 maModel.mxText->finalizeImport();
192 Reference< XText > xAnnoText( xAnnoShape, UNO_QUERY_THROW );
193 maModel.mxText->convert( xAnnoText, true );
195 catch( Exception& )
200 // private --------------------------------------------------------------------
202 CommentsBuffer::CommentsBuffer( const WorksheetHelper& rHelper ) :
203 WorksheetHelper( rHelper )
207 void CommentsBuffer::appendAuthor( const OUString& rAuthor )
209 maAuthors.push_back( rAuthor );
212 CommentRef CommentsBuffer::createComment()
214 CommentRef xComment( new Comment( *this ) );
215 maComments.push_back( xComment );
216 return xComment;
219 void CommentsBuffer::finalizeImport()
221 maComments.forEachMem( &Comment::finalizeImport );
224 } // namespace xls
225 } // namespace oox
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */