LibreOffice Module sc (master)  1
undostyl.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 <svl/itemset.hxx>
21 #include <vcl/virdev.hxx>
22 #include <osl/diagnose.h>
23 
24 #include <undostyl.hxx>
25 #include <docsh.hxx>
26 #include <docpool.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 
37 {
38 }
39 
41  aName( rOther.aName ),
42  aParent( rOther.aParent )
43 {
44  if (rOther.moItems)
45  moItems.emplace(*rOther.moItems);
46 }
47 
49 {
50  if (this != &rOther)
51  {
52  aName = rOther.aName;
53  aParent = rOther.aParent;
54  if (rOther.moItems)
55  moItems.emplace(*rOther.moItems);
56  else
57  moItems.reset();
58  }
59  return *this;
60 }
61 
63 {
64  if ( pSource )
65  {
66  aName = pSource->GetName();
67  aParent = pSource->GetParent();
68  moItems.emplace(const_cast<SfxStyleSheetBase*>(pSource)->GetItemSet());
69  }
70  else
71  {
72  aName.clear();
73  aParent.clear();
74  moItems.reset();
75  }
76 }
77 
79  const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
80  ScSimpleUndo( pDocSh ),
81  eFamily( eFam ),
82  aOldData( rOld ),
83  aNewData( rNew )
84 {
85 }
86 
88 {
89 }
90 
92 {
93  TranslateId pId = (eFamily == SfxStyleFamily::Para) ?
94  STR_UNDO_EDITCELLSTYLE :
95  STR_UNDO_EDITPAGESTYLE;
96  return ScResId(pId);
97 }
98 
99 static void lcl_DocStyleChanged( ScDocument* pDoc, const SfxStyleSheetBase* pStyle, bool bRemoved )
100 {
102 
104  Point aLogic = pVDev->LogicToPixel(Point(1000,1000), MapMode(MapUnit::MapTwip));
105  double nPPTX = aLogic.X() / 1000.0;
106  double nPPTY = aLogic.Y() / 1000.0;
107  Fraction aZoom(1,1);
108  pDoc->StyleSheetChanged( pStyle, bRemoved, pVDev, nPPTX, nPPTY, aZoom, aZoom );
109 
110  ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
111  if (pHdl)
112  pHdl->ForgetLastPattern();
113 }
114 
115 void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const OUString& rName,
116  SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
117 {
118  ScDocument& rDoc = pDocSh->GetDocument();
119  ScStyleSheetPool* pStlPool = rDoc.GetStyleSheetPool();
120  const OUString& aNewName = rData.GetName();
121  bool bDelete = aNewName.isEmpty(); // no new name -> delete style
122  bool bNew = ( rName.isEmpty() && !bDelete ); // creating new style
123 
124  SfxStyleSheetBase* pStyle = nullptr;
125  if ( !rName.isEmpty() )
126  {
127  // find old style to modify
128  pStyle = pStlPool->Find( rName, eStyleFamily );
129  OSL_ENSURE( pStyle, "style not found" );
130 
131  if ( pStyle && !bDelete )
132  {
133  // set new name
134  pStyle->SetName( aNewName );
135  }
136  }
137  else if ( !bDelete )
138  {
139  // create style (with new name)
140  pStyle = &pStlPool->Make( aNewName, eStyleFamily, SfxStyleSearchBits::UserDefined );
141 
142  if ( eStyleFamily == SfxStyleFamily::Para )
143  rDoc.GetPool()->CellStyleCreated( aNewName, rDoc );
144  }
145 
146  if ( pStyle )
147  {
148  if ( bDelete )
149  {
150  if ( eStyleFamily == SfxStyleFamily::Para )
151  lcl_DocStyleChanged( &rDoc, pStyle, true ); // TRUE: remove usage of style
152  else
153  rDoc.RemovePageStyleInUse( rName );
154 
155  // delete style
156  pStlPool->Remove( pStyle );
157  }
158  else
159  {
160  // modify style
161 
162  const OUString& aNewParent = rData.GetParent();
163  if ( aNewParent != pStyle->GetParent() )
164  pStyle->SetParent( aNewParent );
165 
166  SfxItemSet& rStyleSet = pStyle->GetItemSet();
167  const std::optional<SfxItemSet>& pNewSet = rData.GetItems();
168  OSL_ENSURE( pNewSet, "no ItemSet for style" );
169  if (pNewSet)
170  rStyleSet.Set( *pNewSet, false );
171 
172  if ( eStyleFamily == SfxStyleFamily::Para )
173  {
174  lcl_DocStyleChanged( &rDoc, pStyle, false ); // cell styles: row heights
175  }
176  else
177  {
178  // page styles
179 
180  if ( bNew && aNewName != rName )
181  rDoc.RenamePageStyleInUse( rName, aNewName );
182 
183  if (pNewSet)
184  rDoc.ModifyStyleSheet( *pStyle, *pNewSet );
185 
186  pDocSh->PageStyleModified( aNewName, true );
187  }
188  }
189  }
190 
191  pDocSh->PostPaint( 0,0,0, rDoc.MaxCol(),rDoc.MaxRow(),MAXTAB, PaintPartFlags::Grid|PaintPartFlags::Left );
192 
195 }
196 
198 {
199  BeginUndo();
201  EndUndo();
202 }
203 
205 {
206  BeginRedo();
208  EndRedo();
209 }
210 
212 {
213 }
214 
216 {
217  return false; // no repeat possible
218 }
219 
220 // apply page style
221 
223  mnTab( nTab ),
224  maOldStyle( rOldStyle )
225 {
226 }
227 
228 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell* pDocSh, const OUString& rNewStyle ) :
229  ScSimpleUndo( pDocSh ),
230  maNewStyle( rNewStyle )
231 {
232 }
233 
235 {
236 }
237 
238 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
239 {
240  maEntries.emplace_back( nTab, rOldStyle );
241 }
242 
244 {
245  return ScResId( STR_UNDO_APPLYPAGESTYLE );
246 }
247 
249 {
250  BeginUndo();
251  for( const auto& rEntry : maEntries )
252  {
253  pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, rEntry.maOldStyle );
254  ScPrintFunc( pDocShell, pDocShell->GetPrinter(), rEntry.mnTab ).UpdatePages();
255  }
256  EndUndo();
257 }
258 
260 {
261  BeginRedo();
262  for( const auto& rEntry : maEntries )
263  {
264  pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, maNewStyle );
265  ScPrintFunc( pDocShell, pDocShell->GetPrinter(), rEntry.mnTab ).UpdatePages();
266  }
267  EndRedo();
268 }
269 
271 {
273 }
274 
276 {
277  return false;
278 }
279 
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr double nPPTY
OUString aParent
Definition: undostyl.hxx:34
virtual bool CanRepeat(SfxRepeatTarget &rTarget) const override
Definition: undostyl.cxx:275
SfxPrinter * GetPrinter(bool bCreateIfNotExist=true)
Definition: docsh3.cxx:451
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:89
void CellStyleCreated(std::u16string_view rName, const ScDocument &rDoc)
Definition: docpool.cxx:370
bool Set(const SfxItemSet &, bool bDeep=true)
virtual bool SetParent(const OUString &)
void ModifyStyleSheet(SfxStyleSheetBase &rPageStyle, const SfxItemSet &rChanges)
Definition: documen8.cxx:211
virtual const OUString & GetParent() const
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true)
virtual SfxItemSet & GetItemSet()
void EndUndo()
Definition: undobase.cxx:122
virtual void Remove(SfxStyleSheetBase *pStyle) override
Definition: stlpool.cxx:116
virtual OUString GetComment() const override
Definition: undostyl.cxx:243
ScStyleSaveData aOldData
Definition: undostyl.hxx:53
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6087
static void lcl_DocStyleChanged(ScDocument *pDoc, const SfxStyleSheetBase *pStyle, bool bRemoved)
Definition: undostyl.cxx:99
virtual void Redo() override
Definition: undostyl.cxx:259
SfxStyleFamily eFamily
Definition: undostyl.hxx:52
const OUString & GetName() const
ScStyleSaveData & operator=(const ScStyleSaveData &rOther)
Definition: undostyl.cxx:48
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:872
const OUString & GetParent() const
Definition: undostyl.hxx:45
bool RemovePageStyleInUse(std::u16string_view rStrPageStyle)
Definition: documen8.cxx:327
virtual void Redo() override
Definition: undostyl.cxx:204
SfxStyleFamily
constexpr double nPPTX
void InitFromStyle(const SfxStyleSheetBase *pSource)
Definition: undostyl.cxx:62
void BeginRedo()
Definition: undobase.cxx:142
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:871
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:211
const OUString & GetName() const
Definition: undostyl.hxx:44
virtual void Undo() override
Definition: undostyl.cxx:248
#define SC_MOD()
Definition: scmod.hxx:250
ScDocShell * pDocShell
Definition: undobase.hxx:47
void PageStyleModified(std::u16string_view rStyleName, bool bApi)
Definition: docsh4.cxx:1749
bool RenamePageStyleInUse(std::u16string_view rOld, const OUString &rNew)
Definition: documen8.cxx:342
const SfxItemSet * GetItemSet(const SfxPoolItem &rAttr)
ScStyleSaveData aNewData
Definition: undostyl.hxx:54
virtual void Undo() override
Definition: undostyl.cxx:197
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:270
const SCTAB MAXTAB
Definition: address.hxx:70
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
Definition: docsh3.cxx:101
SC_DLLPUBLIC void SetPageStyle(SCTAB nTab, const OUString &rName)
Definition: document.cxx:6206
std::optional< SfxItemSet > moItems
Definition: undostyl.hxx:35
void StyleSheetChanged(const SfxStyleSheetBase *pStyleSheet, bool bRemoved, OutputDevice *pDev, double nPPTX, double nPPTY, const Fraction &rZoomX, const Fraction &rZoomY)
Definition: document.cxx:4987
ScUndoModifyStyle(ScDocShell *pDocSh, SfxStyleFamily eFam, const ScStyleSaveData &rOld, const ScStyleSaveData &rNew)
Definition: undostyl.cxx:78
void AddSheetAction(SCTAB nTab, const OUString &rOld)
Definition: undostyl.cxx:238
virtual SfxStyleSheetBase & Make(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits nMask=SfxStyleSearchBits::All) override
Definition: stlpool.cxx:72
OUString aName
ApplyStyleVec maEntries
Definition: undostyl.hxx:99
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
OUString aName
Definition: undostyl.hxx:33
SC_DLLPUBLIC ScStyleSheetPool * GetStyleSheetPool() const
Definition: document.cxx:6092
const std::optional< SfxItemSet > & GetItems() const
Definition: undostyl.hxx:46
virtual OUString GetComment() const override
Definition: undostyl.cxx:91
virtual ~ScUndoModifyStyle() override
Definition: undostyl.cxx:87
static void DoChange(ScDocShell *pDocSh, const OUString &rName, SfxStyleFamily eStyleFamily, const ScStyleSaveData &rData)
Definition: undostyl.cxx:115
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
ApplyStyleEntry(SCTAB nTab, const OUString &rOldStyle)
Definition: undostyl.cxx:222
void EndRedo()
Definition: undobase.cxx:151
virtual bool CanRepeat(SfxRepeatTarget &rTarget) const override
Definition: undostyl.cxx:215
void ForgetLastPattern()
Definition: inputhdl.cxx:2250
sal_Int16 SCTAB
Definition: types.hxx:22
void BeginUndo()
Definition: undobase.cxx:87
virtual ~ScUndoApplyPageStyle() override
Definition: undostyl.cxx:234
ScUndoApplyPageStyle(ScDocShell *pDocSh, const OUString &rNewStyle)
Definition: undostyl.cxx:228