Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/operation/ReplaceNoteTextOperation.cxx
Line
Count
Source
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
10
#include <operation/ReplaceNoteTextOperation.hxx>
11
12
#include <svx/svdocapt.hxx>
13
14
#include <docsh.hxx>
15
#include <drwlayer.hxx>
16
#include <editable.hxx>
17
#include <postit.hxx>
18
#include <undocell.hxx>
19
20
namespace sc
21
{
22
ReplaceNoteTextOperation::ReplaceNoteTextOperation(ScDocShell& rDocShell, const ScAddress& rPos,
23
                                                   const OUString& rNoteText,
24
                                                   const OUString* pAuthor, const OUString* pDate,
25
                                                   bool bApi)
26
0
    : Operation(OperationType::ReplaceNoteText, false, bApi)
27
0
    , mrDocShell(rDocShell)
28
0
    , maPos(rPos)
29
0
    , maText(rNoteText)
30
0
    , moAuthor(pAuthor ? std::optional<OUString>(*pAuthor) : std::nullopt)
31
0
    , moDate(pDate ? std::optional<OUString>(*pDate) : std::nullopt)
32
0
{
33
0
}
34
35
bool ReplaceNoteTextOperation::runImplementation()
36
0
{
37
0
    ScDocShellModificator aModificator(mrDocShell);
38
0
    ScDocument& rDoc = mrDocShell.GetDocument();
39
40
0
    ScAddress aPos = convertAddress(maPos);
41
42
0
    ScEditableTester aTester = ScEditableTester::CreateAndTestBlock(
43
0
        rDoc, aPos.Tab(), aPos.Col(), aPos.Row(), aPos.Col(), aPos.Row());
44
0
    if (aTester.IsEditable())
45
0
    {
46
0
        ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
47
0
        SfxUndoManager* pUndoMgr
48
0
            = (pDrawLayer && rDoc.IsUndoEnabled()) ? mrDocShell.GetUndoManager() : nullptr;
49
50
0
        ScNoteData aOldData;
51
0
        std::unique_ptr<ScPostIt> pOldNote = rDoc.ReleaseNote(aPos);
52
0
        sal_uInt32 nNoteId = 0;
53
0
        if (pOldNote)
54
0
        {
55
0
            nNoteId = pOldNote->GetId();
56
            // ensure existing caption object before draw undo tracking starts
57
0
            pOldNote->GetOrCreateCaption(aPos);
58
            // rescue note data for undo
59
0
            aOldData = pOldNote->GetNoteData();
60
0
        }
61
62
        // collect drawing undo actions for deleting/inserting caption objects
63
0
        if (pUndoMgr)
64
0
            pDrawLayer->BeginCalcUndo(false);
65
66
        // delete the note (creates drawing undo action for the caption object)
67
0
        bool hadOldNote(pOldNote);
68
0
        pOldNote.reset();
69
70
        // create new note (creates drawing undo action for the new caption object)
71
0
        ScNoteData aNewData;
72
0
        ScPostIt* pNewNote = nullptr;
73
0
        if ((pNewNote = ScNoteUtil::CreateNoteFromString(rDoc, aPos, maText, false, true, nNoteId)))
74
0
        {
75
0
            if (moAuthor)
76
0
                pNewNote->SetAuthor(*moAuthor);
77
0
            if (moDate)
78
0
                pNewNote->SetDate(*moDate);
79
80
            // rescue note data for undo
81
0
            aNewData = pNewNote->GetNoteData();
82
0
        }
83
84
        // create the undo action
85
0
        if (pUndoMgr && (aOldData.mxCaption || aNewData.mxCaption))
86
0
            pUndoMgr->AddUndoAction(std::make_unique<ScUndoReplaceNote>(
87
0
                mrDocShell, aPos, aOldData, aNewData, pDrawLayer->GetCalcUndo()));
88
89
        // repaint cell (to make note marker visible)
90
0
        mrDocShell.PostPaintCell(aPos);
91
92
0
        rDoc.SetStreamValid(aPos.Tab(), false);
93
94
0
        syncSheetViews();
95
96
0
        aModificator.SetDocumentModified();
97
98
        // Let our LOK clients know about the new/modified note
99
0
        if (pNewNote)
100
0
        {
101
0
            ScDocShell::LOKCommentNotify(hadOldNote ? LOKCommentNotificationType::Modify
102
0
                                                    : LOKCommentNotificationType::Add,
103
0
                                         rDoc, aPos, pNewNote);
104
0
        }
105
106
0
        return true;
107
0
    }
108
0
    else if (!mbApi)
109
0
    {
110
0
        mrDocShell.ErrorMessage(aTester.GetMessageId());
111
0
    }
112
113
0
    return false;
114
0
}
115
}
116
117
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */