LibreOffice Module sw (master)  1
annotationmark.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 <algorithm>
21 #include <annotationmark.hxx>
22 
23 #include <doc.hxx>
25 #include <IDocumentState.hxx>
26 #include <fldbas.hxx>
27 #include <calbck.hxx>
28 #include <fmtfld.hxx>
29 #include <docufld.hxx>
30 #include <IDocumentUndoRedo.hxx>
31 #include <UndoBookmark.hxx>
32 #include <ndtxt.hxx>
33 #include <txtfld.hxx>
34 
35 namespace sw::mark
36 {
38  const SwPaM& rPaM,
39  const OUString& rName )
40  : MarkBase( rPaM, rName )
41  {
42  if ( rName.getLength() == 0 )
43  {
44  SetName( MarkBase::GenerateNewName("__Annotation__") );
45  }
46  }
47 
49  {
50  }
51 
52  void AnnotationMark::InitDoc(SwDoc* const io_pDoc,
53  sw::mark::InsertMode const, SwPosition const*const)
54  {
55  SwTextNode *pTextNode = GetMarkEnd().nNode.GetNode().GetTextNode();
56  assert(pTextNode);
57  SwTextField *const pTextField = pTextNode->GetFieldTextAttrAt(
58  GetMarkEnd().nContent.GetIndex()-1, true);
59  assert(pTextField != nullptr);
60  auto pPostItField
61  = dynamic_cast<const SwPostItField*>(pTextField->GetFormatField().GetField());
62  assert(pPostItField);
63  // use the annotation mark's name as the annotation name, if
64  // - the annotation field has an empty annotation name or
65  // - the annotation mark's name differs (on mark creation a name clash had been detected)
66  if ( pPostItField->GetName().isEmpty()
67  || pPostItField->GetName() != GetName() )
68  {
69  const_cast<SwPostItField*>(pPostItField)->SetName( GetName() );
70  }
71 
72  if (io_pDoc->GetIDocumentUndoRedo().DoesUndo())
73  {
74  io_pDoc->GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsBookmark>(*this) );
75  }
76  io_pDoc->getIDocumentState().SetModified();
77  }
78 
80  {
81  SwDoc* pDoc = GetMarkPos().GetDoc();
82  assert(pDoc != nullptr);
83 
84  const auto sName = GetName();
85  SwFieldType* pType = pDoc->getIDocumentFieldsAccess().GetFieldType( SwFieldIds::Postit, OUString(), false );
86  std::vector<SwFormatField*> vFields;
87  pType->GatherFields(vFields);
88  auto ppFound = std::find_if(vFields.begin(), vFields.end(), [&sName](SwFormatField* pF)
89  {
90  auto pPF = dynamic_cast<const SwPostItField*>(pF->GetField());
91  return pPF && pPF->GetName() == sName;
92  });
93  return ppFound != vFields.end() ? *ppFound : nullptr;
94  }
95 }
96 
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GenerateNewName(const OUString &rPrefix)
Definition: bookmrk.cxx:318
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:240
Marks a position in the document model.
Definition: pam.hxx:35
const SwField * GetField() const
Definition: fmtfld.hxx:111
virtual SwFieldType * GetFieldType(SwFieldIds nResId, const OUString &rName, bool bDbFieldMatching) const =0
SwNodeIndex nNode
Definition: pam.hxx:37
virtual void SetModified()=0
Must be called manually at changes of format.
Definition: doc.hxx:184
SwNode & GetNode() const
Definition: ndindex.hxx:119
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
void GatherFields(std::vector< SwFormatField * > &rvFormatFields, bool bCollectOnlyInDocNodes=true) const
Definition: fldbas.cxx:193
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
virtual SwPosition & GetMarkEnd() const override
Definition: bookmrk.hxx:64
virtual const OUString & GetName() const override
Definition: bookmrk.hxx:49
SwDoc * GetDoc() const
Returns the document this position is in.
Definition: pam.cxx:178
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const SwFormatField & GetFormatField() const
Definition: txatbase.hxx:191
virtual bool DoesUndo() const =0
Is Undo enabled?
const char * sName
AnnotationMark(const SwPaM &rPaM, const OUString &rName)
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
virtual void AppendUndo(std::unique_ptr< SwUndo > pUndo)=0
Add new Undo action.
void SetName(const OUString &rName)
Definition: bookmrk.hxx:77
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
SwTextField * GetFieldTextAttrAt(const sal_Int32 nIndex, const bool bIncludeInputFieldAtStart=false) const
Definition: ndtxt.cxx:1769
virtual ~AnnotationMark() override
const OUString & GetName() const
Definition: docufld.hxx:488
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
virtual SwPosition & GetMarkPos() const override
Definition: bookmrk.hxx:47
virtual void InitDoc(SwDoc *const io_Doc, sw::mark::InsertMode eMode, SwPosition const *pSepPos) override
InsertMode
Definition: IMark.hxx:31
const SwFormatField * GetAnnotationFormatField() const
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:844