Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/undo/undostyl.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
 * 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 <svl/itemset.hxx>
21
#include <utility>
22
#include <vcl/virdev.hxx>
23
#include <osl/diagnose.h>
24
25
#include <undostyl.hxx>
26
#include <docsh.hxx>
27
#include <stlpool.hxx>
28
#include <printfun.hxx>
29
#include <scmod.hxx>
30
#include <inputhdl.hxx>
31
#include <globstr.hrc>
32
#include <scresid.hxx>
33
34
//      modify style (cell or page style)
35
36
ScStyleSaveData::ScStyleSaveData()
37
0
{
38
0
}
39
40
ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData& rOther ) :
41
0
    aName( rOther.aName ),
42
0
    aParent( rOther.aParent )
43
0
{
44
0
    if (rOther.moItems)
45
0
        moItems.emplace(*rOther.moItems);
46
0
}
47
48
ScStyleSaveData& ScStyleSaveData::operator=( const ScStyleSaveData& rOther )
49
0
{
50
0
    if (this != &rOther)
51
0
    {
52
0
        aName   = rOther.aName;
53
0
        aParent = rOther.aParent;
54
0
        if (rOther.moItems)
55
0
            moItems.emplace(*rOther.moItems);
56
0
        else
57
0
            moItems.reset();
58
0
    }
59
0
    return *this;
60
0
}
61
62
void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase* pSource )
63
0
{
64
0
    if ( pSource )
65
0
    {
66
0
        aName   = pSource->GetName();
67
0
        aParent = pSource->GetParent();
68
0
        moItems.emplace(const_cast<SfxStyleSheetBase*>(pSource)->GetItemSet());
69
0
    }
70
0
    else
71
0
    {
72
0
        aName.clear();
73
0
        aParent.clear();
74
0
        moItems.reset();
75
0
    }
76
0
}
77
78
ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell& rShell, SfxStyleFamily eFam,
79
                    const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
80
0
    ScSimpleUndo( rShell ),
81
0
    eFamily( eFam ),
82
0
    aOldData( rOld ),
83
0
    aNewData( rNew )
84
0
{
85
0
}
86
87
ScUndoModifyStyle::~ScUndoModifyStyle()
88
0
{
89
0
}
90
91
OUString ScUndoModifyStyle::GetComment() const
92
0
{
93
0
    if (eFamily == SfxStyleFamily::Frame)
94
0
        return ScResId(STR_UNDO_EDITGRAPHICSTYLE);
95
0
    if (eFamily == SfxStyleFamily::Page)
96
0
        return ScResId(STR_UNDO_EDITPAGESTYLE);
97
98
0
    return ScResId(STR_UNDO_EDITCELLSTYLE);
99
0
}
100
101
static void lcl_DocStyleChanged( ScDocument* pDoc, const SfxStyleSheetBase* pStyle, bool bRemoved )
102
0
{
103
    //! move to document or docshell
104
105
0
    ScopedVclPtrInstance< VirtualDevice > pVDev;
106
0
    Point aLogic = pVDev->LogicToPixel(Point(1000,1000), MapMode(MapUnit::MapTwip));
107
0
    double nPPTX = aLogic.X() / 1000.0;
108
0
    double nPPTY = aLogic.Y() / 1000.0;
109
0
    Fraction aZoom(1,1);
110
0
    pDoc->StyleSheetChanged( pStyle, bRemoved, pVDev, nPPTX, nPPTY, aZoom, aZoom );
111
112
0
    ScInputHandler* pHdl = ScModule::get()->GetInputHdl();
113
0
    if (pHdl)
114
0
        pHdl->ForgetLastPattern();
115
0
}
116
117
void ScUndoModifyStyle::DoChange( ScDocShell& rDocSh, const OUString& rName,
118
                                    SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
119
0
{
120
0
    ScDocument& rDoc = rDocSh.GetDocument();
121
0
    ScStyleSheetPool* pStlPool = rDoc.GetStyleSheetPool();
122
0
    const OUString& aNewName = rData.GetName();
123
0
    bool bDelete = aNewName.isEmpty();         // no new name -> delete style
124
0
    bool bNew = ( rName.isEmpty() && !bDelete );   // creating new style
125
126
0
    SfxStyleSheetBase* pStyle = nullptr;
127
0
    if ( !rName.isEmpty() )
128
0
    {
129
        // find old style to modify
130
0
        pStyle = pStlPool->Find( rName, eStyleFamily );
131
0
        OSL_ENSURE( pStyle, "style not found" );
132
133
0
        if ( pStyle && !bDelete )
134
0
        {
135
            // set new name
136
0
            pStyle->SetName( aNewName );
137
0
        }
138
0
    }
139
0
    else if ( !bDelete )
140
0
    {
141
        // create style (with new name)
142
0
        pStyle = &pStlPool->Make( aNewName, eStyleFamily, SfxStyleSearchBits::UserDefined );
143
144
0
        if ( eStyleFamily == SfxStyleFamily::Para )
145
0
            rDoc.getCellAttributeHelper().CellStyleCreated(rDoc, aNewName);
146
0
    }
147
148
0
    if ( pStyle )
149
0
    {
150
0
        if ( bDelete )
151
0
        {
152
0
            if ( eStyleFamily == SfxStyleFamily::Para )
153
0
                lcl_DocStyleChanged( &rDoc, pStyle, true );      // TRUE: remove usage of style
154
0
            else if ( eStyleFamily == SfxStyleFamily::Page )
155
0
                rDoc.RemovePageStyleInUse( rName );
156
157
            // delete style
158
0
            pStlPool->Remove( pStyle );
159
0
        }
160
0
        else
161
0
        {
162
            // modify style
163
164
0
            const OUString& aNewParent = rData.GetParent();
165
0
            if ( aNewParent != pStyle->GetParent() )
166
0
                pStyle->SetParent( aNewParent );
167
168
0
            SfxItemSet& rStyleSet = pStyle->GetItemSet();
169
0
            const std::optional<SfxItemSet>& pNewSet = rData.GetItems();
170
0
            OSL_ENSURE( pNewSet, "no ItemSet for style" );
171
0
            if (pNewSet)
172
0
                rStyleSet.Set( *pNewSet, false );
173
174
0
            if ( eStyleFamily == SfxStyleFamily::Para )
175
0
            {
176
0
                lcl_DocStyleChanged( &rDoc, pStyle, false );     // cell styles: row heights
177
0
            }
178
0
            else if ( eStyleFamily == SfxStyleFamily::Page )
179
0
            {
180
                // page styles
181
182
0
                if ( bNew && aNewName != rName )
183
0
                    rDoc.RenamePageStyleInUse( rName, aNewName );
184
185
0
                if (pNewSet)
186
0
                    rDoc.ModifyStyleSheet( *pStyle, *pNewSet );
187
188
0
                rDocSh.PageStyleModified( aNewName, true );
189
0
            }
190
0
            else
191
0
                static_cast<SfxStyleSheet*>(pStyle)->Broadcast(SfxHint(SfxHintId::DataChanged));
192
0
        }
193
0
    }
194
195
0
    rDocSh.PostPaint( 0,0,0, rDoc.MaxCol(),rDoc.MaxRow(),MAXTAB, PaintPartFlags::Grid|PaintPartFlags::Left );
196
197
    //! undo/redo document modifications for deleted styles
198
    //! undo/redo modifications of number formatter
199
0
}
200
201
void ScUndoModifyStyle::Undo()
202
0
{
203
0
    BeginUndo();
204
0
    DoChange( rDocShell, aNewData.GetName(), eFamily, aOldData );
205
0
    EndUndo();
206
0
}
207
208
void ScUndoModifyStyle::Redo()
209
0
{
210
0
    BeginRedo();
211
0
    DoChange( rDocShell, aOldData.GetName(), eFamily, aNewData );
212
0
    EndRedo();
213
0
}
214
215
void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
216
0
{
217
0
}
218
219
bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
220
0
{
221
0
    return false;       // no repeat possible
222
0
}
223
224
//      apply page style
225
226
ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab, OUString aOldStyle ) :
227
0
    mnTab( nTab ),
228
0
    maOldStyle(std::move( aOldStyle ))
229
0
{
230
0
}
231
232
ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell& rDocSh, OUString aNewStyle ) :
233
0
    ScSimpleUndo( rDocSh ),
234
0
    maNewStyle(std::move( aNewStyle ))
235
0
{
236
0
}
237
238
ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
239
0
{
240
0
}
241
242
void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
243
0
{
244
0
    maEntries.emplace_back( nTab, rOldStyle );
245
0
}
246
247
OUString ScUndoApplyPageStyle::GetComment() const
248
0
{
249
0
    return ScResId( STR_UNDO_APPLYPAGESTYLE );
250
0
}
251
252
void ScUndoApplyPageStyle::Undo()
253
0
{
254
0
    BeginUndo();
255
0
    for( const auto& rEntry : maEntries )
256
0
    {
257
0
        rDocShell.GetDocument().SetPageStyle( rEntry.mnTab, rEntry.maOldStyle );
258
0
        ScPrintFunc( rDocShell, rDocShell.GetPrinter(), rEntry.mnTab ).UpdatePages();
259
0
    }
260
0
    EndUndo();
261
0
}
262
263
void ScUndoApplyPageStyle::Redo()
264
0
{
265
0
    BeginRedo();
266
0
    for( const auto& rEntry : maEntries )
267
0
    {
268
0
        rDocShell.GetDocument().SetPageStyle( rEntry.mnTab, maNewStyle );
269
0
        ScPrintFunc( rDocShell, rDocShell.GetPrinter(), rEntry.mnTab ).UpdatePages();
270
0
    }
271
0
    EndRedo();
272
0
}
273
274
void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
275
0
{
276
    //! set same page style to current tab
277
0
}
278
279
bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
280
0
{
281
0
    return false;
282
0
}
283
284
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */