1 /* This file is part of the KDE project
2    Copyright 2004 Ariya Hidayat <ariya@kde.org>
3    Copyright 2004 Laurent Montel <montel@kde.org>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19 */
20 
21 // Local
22 #include "SheetCommands.h"
23 
24 #include "Damages.h"
25 #include "Localization.h"
26 #include "Map.h"
27 #include "Sheet.h"
28 
29 using namespace Calligra::Sheets;
30 
31 // ----- RenameSheetCommand -----
32 
RenameSheetCommand(Sheet * s,const QString & name)33 RenameSheetCommand::RenameSheetCommand(Sheet* s, const QString &name)
34 {
35     sheet = s;
36     if (s) oldName = s->sheetName();
37     newName = name;
38     setText(kundo2_i18n("Rename Sheet"));
39 }
40 
redo()41 void RenameSheetCommand::redo()
42 {
43     if (sheet)
44         sheet->setSheetName(newName);
45 }
46 
undo()47 void RenameSheetCommand::undo()
48 {
49     if (sheet)
50         sheet->setSheetName(oldName);
51 }
52 
53 // ----- HideSheetCommand -----
54 
HideSheetCommand(Sheet * sheet)55 HideSheetCommand::HideSheetCommand(Sheet* sheet)
56 {
57     map = sheet->map();
58     sheetName = sheet->sheetName();
59     KUndo2MagicString n =  kundo2_i18n("Hide Sheet %1", sheetName);
60     if (n.toString().length() > 64) n = kundo2_i18n("Hide Sheet");
61     setText(n);
62 }
63 
redo()64 void HideSheetCommand::redo()
65 {
66     Sheet* sheet = map->findSheet(sheetName);
67     if (!sheet) return;
68 
69     sheet->hideSheet(true);
70 }
71 
undo()72 void HideSheetCommand::undo()
73 {
74     Sheet* sheet = map->findSheet(sheetName);
75     if (!sheet) return;
76 
77     sheet->hideSheet(false);
78 }
79 
80 // ----- ShowSheetCommand -----
81 
ShowSheetCommand(Sheet * sheet,KUndo2Command * parent)82 ShowSheetCommand::ShowSheetCommand(Sheet* sheet, KUndo2Command* parent)
83         : KUndo2Command(parent)
84 {
85     map = sheet->map();
86     sheetName = sheet->sheetName();
87     KUndo2MagicString n =  kundo2_i18n("Show Sheet %1", sheetName);
88     if (n.toString().length() > 64) n = kundo2_i18n("Show Sheet");
89     setText(n);
90 }
91 
redo()92 void ShowSheetCommand::redo()
93 {
94     Sheet* sheet = map->findSheet(sheetName);
95     if (!sheet) return;
96 
97     sheet->hideSheet(false);
98 }
99 
undo()100 void ShowSheetCommand::undo()
101 {
102     Sheet* sheet = map->findSheet(sheetName);
103     if (!sheet) return;
104 
105     sheet->hideSheet(true);
106 }
107 
108 
109 // ----- AddSheetCommand -----
110 
AddSheetCommand(Sheet * sheet)111 AddSheetCommand::AddSheetCommand(Sheet* sheet)
112         : KUndo2Command(kundo2_i18n("Add Sheet"))
113         , m_sheet(sheet)
114         , m_firstrun(true)
115 {
116 }
117 
redo()118 void AddSheetCommand::redo()
119 {
120     if (m_firstrun) {
121         m_sheet->map()->addSheet(m_sheet);
122         m_firstrun = false;
123     } else {
124         m_sheet->map()->reviveSheet(m_sheet);
125     }
126 }
127 
undo()128 void AddSheetCommand::undo()
129 {
130     // The sheet becomes a zombie, i.e. it is not deleted,
131     // so that the sheet pointer used in other commands later on stays valid.
132     m_sheet->map()->removeSheet(m_sheet);
133 }
134 
135 
136 // ----- DuplicateSheetCommand -----
137 
DuplicateSheetCommand()138 DuplicateSheetCommand::DuplicateSheetCommand()
139         : KUndo2Command(kundo2_i18n("Duplicate Sheet"))
140         , m_oldSheet(0)
141         , m_newSheet(0)
142         , m_firstrun(true)
143 {
144 }
145 
setSheet(Sheet * sheet)146 void DuplicateSheetCommand::setSheet(Sheet* sheet)
147 {
148     m_oldSheet = sheet;
149 }
150 
redo()151 void DuplicateSheetCommand::redo()
152 {
153     // Once created the sheet stays alive forever. See comment in undo.
154     if (m_firstrun) {
155         m_newSheet = new Sheet(*m_oldSheet);
156         m_newSheet->map()->addSheet(m_newSheet);
157         m_firstrun = false;
158     } else {
159         m_newSheet->map()->reviveSheet(m_newSheet);
160     }
161 }
162 
undo()163 void DuplicateSheetCommand::undo()
164 {
165     // The new sheet is not deleted, but just becomes a zombie,
166     // so that the sheet pointer used in commands later on stays valid.
167     m_newSheet->map()->removeSheet(m_newSheet);
168 }
169 
170 
171 // ----- RemoveSheetCommand -----
172 
RemoveSheetCommand(Sheet * s)173 RemoveSheetCommand::RemoveSheetCommand(Sheet* s)
174 {
175     sheet = s;
176     map = sheet->map();
177     setText(kundo2_i18n("Remove Sheet"));
178 }
179 
redo()180 void RemoveSheetCommand::redo()
181 {
182     sheet->map()->removeSheet(sheet);
183 }
184 
undo()185 void RemoveSheetCommand::undo()
186 {
187     sheet->map()->reviveSheet(sheet);
188 }
189 
190 // ----- SheetPropertiesCommand -----
191 
SheetPropertiesCommand(Sheet * s)192 SheetPropertiesCommand::SheetPropertiesCommand(Sheet* s)
193 {
194     sheet = s;
195     map = s->map();
196     oldDirection = newDirection = sheet->layoutDirection();
197     oldAutoCalc = newAutoCalc = sheet->isAutoCalculationEnabled();
198     oldShowGrid = newShowGrid = sheet->getShowGrid();
199     oldShowPageOutline = newShowPageOutline = sheet->isShowPageOutline();
200     oldShowFormula = newShowFormula = sheet->getShowFormula();
201     oldHideZero = newHideZero = sheet->getHideZero();
202     oldShowFormulaIndicator = newShowFormulaIndicator = sheet->getShowFormulaIndicator();
203     oldShowCommentIndicator = newShowCommentIndicator = sheet->getShowCommentIndicator();
204     oldColumnAsNumber = newColumnAsNumber = sheet->getShowColumnNumber();
205     oldLcMode = newLcMode = sheet->getLcMode();
206     oldCapitalizeFirstLetter = newCapitalizeFirstLetter = sheet->getFirstLetterUpper();
207     setText(kundo2_i18n("Change Sheet Properties"));
208 }
209 
setLayoutDirection(Qt::LayoutDirection dir)210 void SheetPropertiesCommand::setLayoutDirection(Qt::LayoutDirection dir)
211 {
212     newDirection = dir;
213 }
214 
setAutoCalculationEnabled(bool b)215 void SheetPropertiesCommand::setAutoCalculationEnabled(bool b)
216 {
217     newAutoCalc = b;
218 }
219 
setShowGrid(bool b)220 void SheetPropertiesCommand::setShowGrid(bool b)
221 {
222     newShowGrid = b;
223 }
224 
setShowPageOutline(bool b)225 void SheetPropertiesCommand::setShowPageOutline(bool b)
226 {
227     newShowPageOutline = b;
228 }
229 
setShowFormula(bool b)230 void SheetPropertiesCommand::setShowFormula(bool b)
231 {
232     newShowFormula = b;
233 }
234 
setHideZero(bool b)235 void SheetPropertiesCommand::setHideZero(bool b)
236 {
237     newHideZero = b;
238 }
239 
setShowFormulaIndicator(bool b)240 void SheetPropertiesCommand::setShowFormulaIndicator(bool b)
241 {
242     newShowFormulaIndicator = b;
243 }
244 
setShowCommentIndicator(bool b)245 void SheetPropertiesCommand::setShowCommentIndicator(bool b)
246 {
247     newShowCommentIndicator = b;
248 }
249 
setColumnAsNumber(bool b)250 void SheetPropertiesCommand::setColumnAsNumber(bool b)
251 {
252     newColumnAsNumber = b;
253 }
254 
setLcMode(bool b)255 void SheetPropertiesCommand::setLcMode(bool b)
256 {
257     newLcMode = b;
258 }
259 
setCapitalizeFirstLetter(bool b)260 void SheetPropertiesCommand::setCapitalizeFirstLetter(bool b)
261 {
262     newCapitalizeFirstLetter = b;
263 }
264 
redo()265 void SheetPropertiesCommand::redo()
266 {
267     sheet->setLayoutDirection(newDirection);
268     sheet->setAutoCalculationEnabled(newAutoCalc);
269     sheet->setShowGrid(newShowGrid);
270     sheet->setShowPageOutline(newShowPageOutline);
271     sheet->setShowFormula(newShowFormula);
272     sheet->setHideZero(newHideZero);
273     sheet->setShowFormulaIndicator(newShowFormulaIndicator);
274     sheet->setShowCommentIndicator(newShowCommentIndicator);
275     sheet->setShowColumnNumber(newColumnAsNumber);
276     sheet->setLcMode(newLcMode);
277     sheet->setFirstLetterUpper(newCapitalizeFirstLetter);
278     sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::PropertiesChanged));
279 }
280 
undo()281 void SheetPropertiesCommand::undo()
282 {
283     sheet->setLayoutDirection(oldDirection);
284     sheet->setAutoCalculationEnabled(oldAutoCalc);
285     sheet->setShowGrid(oldShowGrid);
286     sheet->setShowPageOutline(oldShowPageOutline);
287     sheet->setShowFormula(oldShowFormula);
288     sheet->setHideZero(oldHideZero);
289     sheet->setShowFormulaIndicator(oldShowFormulaIndicator);
290     sheet->setShowCommentIndicator(oldShowCommentIndicator);
291     sheet->setShowColumnNumber(oldColumnAsNumber);
292     sheet->setLcMode(oldLcMode);
293     sheet->setFirstLetterUpper(oldCapitalizeFirstLetter);
294     sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::PropertiesChanged));
295 }
296