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