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