LibreOffice Module sc (master) 1
commentsbuffer.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 <oox/token/tokens.hxx>
21
22#include <commentsbuffer.hxx>
23
24#include <com/sun/star/beans/XMultiPropertySet.hpp>
25#include <com/sun/star/text/XText.hpp>
26#include <osl/diagnose.h>
28#include <oox/vml/vmlshape.hxx>
29#include <addressconverter.hxx>
30#include <drawingfragment.hxx>
31#include <svx/sdtaitm.hxx>
32#include <svx/svdocapt.hxx>
33#include <svx/unoshape.hxx>
35#include <document.hxx>
36#include <drwlayer.hxx>
37#include <cellsuno.hxx>
38#include <docfunc.hxx>
39#include <docuno.hxx>
40#include <docsh.hxx>
41#include <postit.hxx>
42
43namespace oox::xls {
44
45using namespace ::com::sun::star::drawing;
46using namespace ::com::sun::star::sheet;
47using namespace ::com::sun::star::table;
48using namespace ::com::sun::star::text;
49using namespace ::com::sun::star::uno;
50
51static sal_Int32 lcl_ToHorizAlign( sal_Int32 nAlign )
52{
53 switch( nAlign )
54 {
55 case XML_left:
57 case XML_right:
59 case XML_center:
61 default:
63 }
64}
65
66static sal_Int32 lcl_ToVertAlign( sal_Int32 nAlign )
67{
68 switch( nAlign )
69 {
70 case XML_top:
71 case XML_Top:
73 case XML_center:
74 case XML_Center:
76 case XML_bottom:
77 case XML_Bottom:
79 default:
81 }
82}
83
84static sal_Int16 lcl_ToParaAlign(sal_Int32 nAlign)
85{
86 switch ( nAlign )
87 {
88 case XML_Left:
89 return sal_Int16(css::style::ParagraphAdjust_LEFT);
90 case XML_Right:
91 return sal_Int16(css::style::ParagraphAdjust_RIGHT);
92 case XML_Center:
93 return sal_Int16(css::style::ParagraphAdjust_CENTER);
94 default:
95 return sal_Int16(css::style::ParagraphAdjust_BLOCK);
96 }
97}
98
100 : mnAuthorId(-1)
101 , mbAutoFill(false)
102 , mbAutoScale(false)
103 , mbColHidden(false)
104 , mbLocked(false)
105 , mbRowHidden(false)
106 , mnTHA(0)
107 , mnTVA(0)
108{
109}
110
112 WorksheetHelper( rHelper )
113{
114}
115
117{
118 maModel.mnAuthorId = rAttribs.getInteger( XML_authorId, -1 );
119 // cell range will be checked while inserting the comment into the document
121}
122
124{
125 maModel.mbAutoFill = rAttribs.getBool( XML_autoFill, true );
126 maModel.mbAutoScale = rAttribs.getBool( XML_autoScale, false );
127 maModel.mbColHidden = rAttribs.getBool( XML_colHidden, false );
128 maModel.mbLocked = rAttribs.getBool( XML_locked, false );
129 maModel.mbRowHidden = rAttribs.getBool( XML_rowHidden, false );
130 maModel.mnTHA = rAttribs.getToken( XML_textHAlign, XML_left );
131 maModel.mnTVA = rAttribs.getToken( XML_textVAlign, XML_top );
132}
133
135{
136 BinRange aBinRange;
137 maModel.mnAuthorId = rStrm.readInt32();
138 rStrm >> aBinRange;
139 // cell range will be checked while inserting the comment into the document
141}
142
144{
145 maModel.mxText = std::make_shared<RichString>();
146 return maModel.mxText;
147}
148
150{
151 // BIFF12 stores cell range instead of cell address, use first cell of this range
152 OSL_ENSURE( maModel.maRange.aStart == maModel.maRange.aEnd,
153 "Comment::finalizeImport - comment anchor should be a single cell" );
154 if( !getAddressConverter().checkCellAddress( maModel.maRange.aStart, true ) || !maModel.mxText )
155 return;
156
157 try
158 {
159 ScTableSheetObj* pAnnosSupp = static_cast<ScTableSheetObj*>(getSheet().get());
160 rtl::Reference<ScAnnotationsObj> xAnnos = static_cast<ScAnnotationsObj*>(pAnnosSupp->getAnnotations().get());
161 ScDocShell* pDocShell = xAnnos->GetDocShell();
162 // non-empty string required by note implementation (real text will be added below)
163 ScPostIt* pPostIt = pDocShell->GetDocFunc().ImportNote( maModel.maRange.aStart, OUString( ' ' ) );
164 SdrCaptionObj* pCaption = pPostIt->GetOrCreateCaption( maModel.maRange.aStart );
165
166 rtl::Reference< SvxShapeText > xAnnoShape( dynamic_cast<SvxShapeText*>(pCaption->getUnoShape().get() ) ); // SvxShapeText
167 assert(xAnnoShape && "will not be null");
168 // setting a property triggers expensive process, so set them all at once
169
170 // Add shape formatting properties (autoFill, colHidden and rowHidden are dropped)
171 static_cast<SvxShape*>(xAnnoShape.get())->setPropertyValues(
172 Sequence<OUString> { "TextFitToSize", "MoveProtect", "TextHorizontalAdjust", "TextVerticalAdjust" },
173 Sequence<Any> { Any(maModel.mbAutoScale), Any(maModel.mbLocked),
175 if( maModel.maAnchor.Width > 0 && maModel.maAnchor.Height > 0 )
176 {
177 xAnnoShape->setPosition( css::awt::Point( maModel.maAnchor.X, maModel.maAnchor.Y ) );
178 xAnnoShape->setSize( css::awt::Size( maModel.maAnchor.Width, maModel.maAnchor.Height ) );
179 }
180
181 // convert shape formatting and visibility
182 bool bVisible = true;
183 if( const ::oox::vml::ShapeBase* pVmlNoteShape = getVmlDrawing().getNoteShape( maModel.maRange.aStart ) )
184 {
185 // position and formatting
186 pVmlNoteShape->convertFormatting( xAnnoShape );
187 // visibility
188 bVisible = pVmlNoteShape->getTypeModel().mbVisible;
189
190 // Setting comment text alignment
191 const ::oox::vml::ClientData* xClientData = pVmlNoteShape->getClientData();
192 static_cast<SvxShape*>(xAnnoShape.get())->setPropertyValues(
193 Sequence<OUString> { "TextVerticalAdjust", "ParaAdjust" },
194 Sequence<Any> { Any(lcl_ToVertAlign( xClientData->mnTextVAlign )), Any(lcl_ToParaAlign( xClientData->mnTextHAlign )) });
195 }
196 if (bVisible)
198
199 // insert text and convert text formatting
200 maModel.mxText->finalizeImport(*this);
201 Reference< XText > xAnnoText( xAnnoShape );
202 xAnnoShape->addActionLock();
203 maModel.mxText->convert( xAnnoText );
204 xAnnoShape->removeActionLock();
205 }
206 catch( Exception& )
207 {
209 }
210}
211
212// private --------------------------------------------------------------------
213
215 WorksheetHelper( rHelper )
216{
217}
218
219void CommentsBuffer::appendAuthor( const OUString& rAuthor )
220{
221 maAuthors.push_back( rAuthor );
222}
223
225{
226 CommentRef xComment = std::make_shared<Comment>( *this );
227 maComments.push_back( xComment );
228 return xComment;
229}
230
232{
233 // keep the model locked to avoid repeated reformatting in the model
234 auto pModel = getScDocument().GetDrawLayer();
235 bool bWasLocked = pModel->isLocked();
236 pModel->setLock(true);
238 pModel->setLock(bWasLocked);
239}
240
241} // namespace oox::xls
242
243/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SC_DLLPUBLIC bool ShowNote(const ScAddress &rPos, bool bShow)
Definition: docfunc.cxx:1288
SC_DLLPUBLIC ScPostIt * ImportNote(const ScAddress &rPos, const OUString &rNoteText)
Definition: docfunc.cxx:1408
ScDocFunc & GetDocFunc()
Definition: docsh.hxx:221
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1084
Additional class containing cell annotation data.
Definition: postit.hxx:58
SdrCaptionObj * GetOrCreateCaption(const ScAddress &rPos) const
Returns the caption object of this note.
Definition: postit.cxx:555
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
virtual css::uno::Reference< css::sheet::XSheetAnnotations > SAL_CALL getAnnotations() override
Definition: cellsuno.cxx:6626
bool isLocked() const
virtual css::uno::Reference< css::drawing::XShape > getUnoShape()
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::optional< OUString > getString(sal_Int32 nAttrToken) const
std::optional< bool > getBool(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
void forEachMem(FuncType pFunc) const
static bool convertToCellRangeUnchecked(ScRange &orRange, std::u16string_view aString, sal_Int16 nSheet)
Converts the passed string to a cell range address, without checking any sheet limits.
void importCommentPr(const AttributeList &rAttribs)
Imports a cell comment Properties from the passed attributes of the comment element.
void finalizeImport()
Finalizes the formatted string of the comment.
RichStringRef const & createText()
Creates and returns a new rich-string object for the comment text.
CommentModel maModel
Comment(const WorksheetHelper &rHelper)
void importComment(const AttributeList &rAttribs)
Imports a cell comment from the passed attributes of the comment element.
CommentsBuffer(const WorksheetHelper &rHelper)
void appendAuthor(const OUString &rAuthor)
Appends a new author to the list of comment authors.
CommentRef createComment()
Creates and returns a new comment.
void finalizeImport()
Finalizes the formatted string of all comments.
std::vector< OUString > maAuthors
const ::oox::vml::ShapeBase * getNoteShape(const ScAddress &rPos) const
Returns the drawing shape for a cell note at the specified position.
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.
VmlDrawing & getVmlDrawing() const
Returns the VML drawing page for this sheet (OOXML/BIFF12 only).
SCTAB getSheetIndex() const
Returns the index of the current sheet.
const css::uno::Reference< css::sheet::XSpreadsheet > & getSheet() const
Returns the XSpreadsheet interface of the current sheet.
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
void SvStream & rStrm
static sal_Int32 lcl_ToHorizAlign(sal_Int32 nAlign)
std::shared_ptr< Comment > CommentRef
static sal_Int16 lcl_ToParaAlign(sal_Int32 nAlign)
static sal_Int32 lcl_ToVertAlign(sal_Int32 nAlign)
std::shared_ptr< RichString > RichStringRef
Definition: richstring.hxx:263
SDRTEXTVERTADJUST_BOTTOM
SDRTEXTVERTADJUST_BLOCK
SDRTEXTVERTADJUST_CENTER
SDRTEXTVERTADJUST_TOP
SDRTEXTHORZADJUST_LEFT
SDRTEXTHORZADJUST_BLOCK
SDRTEXTHORZADJUST_CENTER
SDRTEXTHORZADJUST_RIGHT
A 2D cell range address struct for binary filters.
CommentModel()
Anchor parameters.
bool mbColHidden
Auto Scale comment text.
bool mbLocked
Comment cell's Column is Hidden.
sal_Int32 mnTVA
Horizontal Alignment.
RichStringRef mxText
Position of the comment in the worksheet.
bool mbAutoScale
Auto Selection of comment object's fill style.
sal_Int32 mnAuthorId
Formatted text of the comment (not used in BIFF8).
bool mbAutoFill
Identifier of the comment's author (OOXML and BIFF12 only).
bool mbRowHidden
Comment changes Locked.
css::awt::Rectangle maAnchor
Vertical Alignment.
sal_Int32 mnTHA
Comment cell's Row is Hidden.
bool bVisible