1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <Annotation.hxx>
23 #include <drawdoc.hxx>
25 #include <com/sun/star/drawing/XDrawPage.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/lok.hxx>
30 #include <unotools/datetime.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <svx/svdundo.hxx>
35 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
37 #include <notifydocumentevent.hxx>
42 namespace com::sun::star::uno
{ class XComponentContext
; }
50 /** Undo/redo insertion or removal of an annotation to/from the document */
51 class UndoInsertOrRemoveAnnotation
: public SdrUndoAction
54 UndoInsertOrRemoveAnnotation(rtl::Reference
<sdr::annotation::Annotation
>& xAnnotation
, bool bInsert
);
56 virtual void Undo() override
;
57 virtual void Redo() override
;
60 rtl::Reference
<sdr::annotation::Annotation
> mxAnnotation
;
67 /** Creates an annotation */
68 rtl::Reference
<sdr::annotation::Annotation
> createAnnotation(SdPage
* pPage
)
70 return rtl::Reference
<Annotation
>(new Annotation(comphelper::getProcessComponentContext(), pPage
));
73 Annotation::Annotation(const uno::Reference
<uno::XComponentContext
>& context
, SdrPage
* pPage
)
74 : sdr::annotation::Annotation(context
, pPage
)
78 Annotation::~Annotation()
81 // com.sun.star.beans.XPropertySet:
82 uno::Reference
<beans::XPropertySetInfo
> SAL_CALL
Annotation::getPropertySetInfo()
84 return ::cppu::PropertySetMixin
<office::XAnnotation
>::getPropertySetInfo();
87 void SAL_CALL
Annotation::setPropertyValue(const OUString
& aPropertyName
, const uno::Any
& aValue
)
89 ::cppu::PropertySetMixin
<office::XAnnotation
>::setPropertyValue(aPropertyName
, aValue
);
92 uno::Any SAL_CALL
Annotation::getPropertyValue(const OUString
& aPropertyName
)
94 return ::cppu::PropertySetMixin
<office::XAnnotation
>::getPropertyValue(aPropertyName
);
97 void SAL_CALL
Annotation::addPropertyChangeListener(const OUString
& aPropertyName
, const uno::Reference
<beans::XPropertyChangeListener
> & xListener
)
99 ::cppu::PropertySetMixin
<office::XAnnotation
>::addPropertyChangeListener(aPropertyName
, xListener
);
102 void SAL_CALL
Annotation::removePropertyChangeListener(const OUString
& aPropertyName
, const uno::Reference
<beans::XPropertyChangeListener
> & xListener
)
104 ::cppu::PropertySetMixin
<office::XAnnotation
>::removePropertyChangeListener(aPropertyName
, xListener
);
107 void SAL_CALL
Annotation::addVetoableChangeListener(const OUString
& aPropertyName
, const uno::Reference
<beans::XVetoableChangeListener
> & xListener
)
109 ::cppu::PropertySetMixin
<office::XAnnotation
>::addVetoableChangeListener(aPropertyName
, xListener
);
112 void SAL_CALL
Annotation::removeVetoableChangeListener(const OUString
& aPropertyName
, const uno::Reference
<beans::XVetoableChangeListener
> & xListener
)
114 ::cppu::PropertySetMixin
<office::XAnnotation
>::removeVetoableChangeListener(aPropertyName
, xListener
);
117 uno::Any SAL_CALL
Annotation::getAnchor()
119 std::unique_lock
g(m_aMutex
);
123 uno::Reference
<drawing::XDrawPage
> xPage( mpPage
->getUnoPage(), uno::UNO_QUERY
);
129 // css::office::XAnnotation:
130 geometry::RealPoint2D SAL_CALL
Annotation::getPosition()
132 std::unique_lock
g(m_aMutex
);
136 void SAL_CALL
Annotation::setPosition(const geometry::RealPoint2D
& the_value
)
138 prepareSet(u
"Position"_ustr
, uno::Any(), uno::Any(), nullptr);
140 std::unique_lock
g(m_aMutex
);
141 createChangeUndoImpl(g
);
142 m_Position
= the_value
;
146 // css::office::XAnnotation:
147 geometry::RealSize2D SAL_CALL
Annotation::getSize()
149 std::unique_lock
g(m_aMutex
);
153 void SAL_CALL
Annotation::setSize(const geometry::RealSize2D
& the_value
)
155 prepareSet(u
"Size"_ustr
, uno::Any(), uno::Any(), nullptr);
157 std::unique_lock
g(m_aMutex
);
158 createChangeUndoImpl(g
);
163 OUString SAL_CALL
Annotation::getAuthor()
165 std::unique_lock
g(m_aMutex
);
169 void SAL_CALL
Annotation::setAuthor(const OUString
& the_value
)
171 prepareSet(u
"Author"_ustr
, uno::Any(), uno::Any(), nullptr);
173 std::unique_lock
g(m_aMutex
);
174 createChangeUndoImpl(g
);
175 m_Author
= the_value
;
179 OUString SAL_CALL
Annotation::getInitials()
181 std::unique_lock
g(m_aMutex
);
185 void SAL_CALL
Annotation::setInitials(const OUString
& the_value
)
187 prepareSet(u
"Initials"_ustr
, uno::Any(), uno::Any(), nullptr);
189 std::unique_lock
g(m_aMutex
);
190 createChangeUndoImpl(g
);
191 m_Initials
= the_value
;
195 util::DateTime SAL_CALL
Annotation::getDateTime()
197 std::unique_lock
g(m_aMutex
);
201 void SAL_CALL
Annotation::setDateTime(const util::DateTime
& the_value
)
203 prepareSet(u
"DateTime"_ustr
, uno::Any(), uno::Any(), nullptr);
205 std::unique_lock
g(m_aMutex
);
206 createChangeUndoImpl(g
);
207 m_DateTime
= the_value
;
211 void Annotation::createChangeUndo()
213 std::unique_lock
g(m_aMutex
);
214 createChangeUndoImpl(g
);
217 void Annotation::createChangeUndoImpl(std::unique_lock
<std::mutex
>& g
)
219 SdrModel
* pModel
= GetModel(); // TTTT should use reference
220 if( pModel
&& pModel
->IsUndoEnabled() )
222 g
.unlock(); // AddUndo calls back into Annotation
223 pModel
->AddUndo(createUndoAnnotation());
229 pModel
->SetChanged();
230 uno::Reference
< XInterface
> xSource( static_cast<uno::XWeak
*>( this ) );
232 static_cast< SdDrawDocument
& >( *pModel
),
233 u
"OnAnnotationChanged"_ustr
,
238 rtl::Reference
<sdr::annotation::Annotation
> Annotation::clone(SdrPage
* pTargetPage
)
240 rtl::Reference
<sdr::annotation::Annotation
> aNewAnnotation
;
241 aNewAnnotation
= new sd::Annotation(comphelper::getProcessComponentContext(), pTargetPage
);
242 aNewAnnotation
->setPosition(getPosition());
243 aNewAnnotation
->setSize(getSize());
244 aNewAnnotation
->setAuthor(getAuthor());
245 aNewAnnotation
->setInitials(getInitials());
246 aNewAnnotation
->setDateTime(getDateTime());
247 aNewAnnotation
->setCreationInfo(getCreationInfo());
249 uno::Reference
<css::text::XTextCopy
> xSourceRange (getTextRange(), uno::UNO_QUERY
);
250 uno::Reference
<css::text::XTextCopy
> xRange (aNewAnnotation
->getTextRange(), uno::UNO_QUERY
);
251 if (xSourceRange
.is() && xRange
.is())
252 xRange
->copyText(xSourceRange
);
254 return aNewAnnotation
;
257 std::unique_ptr
<SdrUndoAction
> CreateUndoInsertOrRemoveAnnotation(rtl::Reference
<sdr::annotation::Annotation
>& xAnnotation
, bool bInsert
)
261 return std::make_unique
<UndoInsertOrRemoveAnnotation
>(xAnnotation
, bInsert
);
269 UndoInsertOrRemoveAnnotation::UndoInsertOrRemoveAnnotation(rtl::Reference
<sdr::annotation::Annotation
>& xAnnotation
, bool bInsert
)
270 : SdrUndoAction(*xAnnotation
->GetModel())
271 , mxAnnotation(xAnnotation
)
274 SdrPage
const* pPage
= mxAnnotation
->getPage();
277 sdr::annotation::AnnotationVector
const& rVector
= pPage
->getAnnotations();
278 auto iterator
= std::find(rVector
.begin(), rVector
.end(), mxAnnotation
);
279 mnIndex
+= std::distance(rVector
.begin(), iterator
);
283 void UndoInsertOrRemoveAnnotation::Undo()
285 SdrPage
* pPage
= mxAnnotation
->getPage();
286 SdrModel
* pModel
= mxAnnotation
->GetModel();
287 if (!pPage
|| !pModel
)
292 pPage
->removeAnnotationNoNotify(mxAnnotation
);
296 pPage
->addAnnotation(mxAnnotation
, mnIndex
);
297 LOKCommentNotifyAll(sdr::annotation::CommentNotificationType::Add
, *mxAnnotation
);
301 void UndoInsertOrRemoveAnnotation::Redo()
303 SdrPage
* pPage
= mxAnnotation
->getPage();
304 SdrModel
* pModel
= mxAnnotation
->GetModel();
305 if (!pPage
|| !pModel
)
310 pPage
->addAnnotationNoNotify(mxAnnotation
, mnIndex
);
311 LOKCommentNotifyAll(sdr::annotation::CommentNotificationType::Add
, *mxAnnotation
);
315 pPage
->removeAnnotationNoNotify(mxAnnotation
);
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */