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