File indexing completed on 2024-05-12 16:35:13

0001 /* This file is part of the KDE project
0002    Copyright 2004 Ariya Hidayat <ariya@kde.org>
0003    Copyright 2004 Laurent Montel <montel@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 // Local
0022 #include "SheetCommands.h"
0023 
0024 #include "Damages.h"
0025 #include "Localization.h"
0026 #include "Map.h"
0027 #include "Sheet.h"
0028 
0029 using namespace Calligra::Sheets;
0030 
0031 // ----- RenameSheetCommand -----
0032 
0033 RenameSheetCommand::RenameSheetCommand(Sheet* s, const QString &name)
0034 {
0035     sheet = s;
0036     if (s) oldName = s->sheetName();
0037     newName = name;
0038     setText(kundo2_i18n("Rename Sheet"));
0039 }
0040 
0041 void RenameSheetCommand::redo()
0042 {
0043     if (sheet)
0044         sheet->setSheetName(newName);
0045 }
0046 
0047 void RenameSheetCommand::undo()
0048 {
0049     if (sheet)
0050         sheet->setSheetName(oldName);
0051 }
0052 
0053 // ----- HideSheetCommand -----
0054 
0055 HideSheetCommand::HideSheetCommand(Sheet* sheet)
0056 {
0057     map = sheet->map();
0058     sheetName = sheet->sheetName();
0059     KUndo2MagicString n =  kundo2_i18n("Hide Sheet %1", sheetName);
0060     if (n.toString().length() > 64) n = kundo2_i18n("Hide Sheet");
0061     setText(n);
0062 }
0063 
0064 void HideSheetCommand::redo()
0065 {
0066     Sheet* sheet = map->findSheet(sheetName);
0067     if (!sheet) return;
0068 
0069     sheet->hideSheet(true);
0070 }
0071 
0072 void HideSheetCommand::undo()
0073 {
0074     Sheet* sheet = map->findSheet(sheetName);
0075     if (!sheet) return;
0076 
0077     sheet->hideSheet(false);
0078 }
0079 
0080 // ----- ShowSheetCommand -----
0081 
0082 ShowSheetCommand::ShowSheetCommand(Sheet* sheet, KUndo2Command* parent)
0083         : KUndo2Command(parent)
0084 {
0085     map = sheet->map();
0086     sheetName = sheet->sheetName();
0087     KUndo2MagicString n =  kundo2_i18n("Show Sheet %1", sheetName);
0088     if (n.toString().length() > 64) n = kundo2_i18n("Show Sheet");
0089     setText(n);
0090 }
0091 
0092 void ShowSheetCommand::redo()
0093 {
0094     Sheet* sheet = map->findSheet(sheetName);
0095     if (!sheet) return;
0096 
0097     sheet->hideSheet(false);
0098 }
0099 
0100 void ShowSheetCommand::undo()
0101 {
0102     Sheet* sheet = map->findSheet(sheetName);
0103     if (!sheet) return;
0104 
0105     sheet->hideSheet(true);
0106 }
0107 
0108 
0109 // ----- AddSheetCommand -----
0110 
0111 AddSheetCommand::AddSheetCommand(Sheet* sheet)
0112         : KUndo2Command(kundo2_i18n("Add Sheet"))
0113         , m_sheet(sheet)
0114         , m_firstrun(true)
0115 {
0116 }
0117 
0118 void AddSheetCommand::redo()
0119 {
0120     if (m_firstrun) {
0121         m_sheet->map()->addSheet(m_sheet);
0122         m_firstrun = false;
0123     } else {
0124         m_sheet->map()->reviveSheet(m_sheet);
0125     }
0126 }
0127 
0128 void AddSheetCommand::undo()
0129 {
0130     // The sheet becomes a zombie, i.e. it is not deleted,
0131     // so that the sheet pointer used in other commands later on stays valid.
0132     m_sheet->map()->removeSheet(m_sheet);
0133 }
0134 
0135 
0136 // ----- DuplicateSheetCommand -----
0137 
0138 DuplicateSheetCommand::DuplicateSheetCommand()
0139         : KUndo2Command(kundo2_i18n("Duplicate Sheet"))
0140         , m_oldSheet(0)
0141         , m_newSheet(0)
0142         , m_firstrun(true)
0143 {
0144 }
0145 
0146 void DuplicateSheetCommand::setSheet(Sheet* sheet)
0147 {
0148     m_oldSheet = sheet;
0149 }
0150 
0151 void DuplicateSheetCommand::redo()
0152 {
0153     // Once created the sheet stays alive forever. See comment in undo.
0154     if (m_firstrun) {
0155         m_newSheet = new Sheet(*m_oldSheet);
0156         m_newSheet->map()->addSheet(m_newSheet);
0157         m_firstrun = false;
0158     } else {
0159         m_newSheet->map()->reviveSheet(m_newSheet);
0160     }
0161 }
0162 
0163 void DuplicateSheetCommand::undo()
0164 {
0165     // The new sheet is not deleted, but just becomes a zombie,
0166     // so that the sheet pointer used in commands later on stays valid.
0167     m_newSheet->map()->removeSheet(m_newSheet);
0168 }
0169 
0170 
0171 // ----- RemoveSheetCommand -----
0172 
0173 RemoveSheetCommand::RemoveSheetCommand(Sheet* s)
0174 {
0175     sheet = s;
0176     map = sheet->map();
0177     setText(kundo2_i18n("Remove Sheet"));
0178 }
0179 
0180 void RemoveSheetCommand::redo()
0181 {
0182     sheet->map()->removeSheet(sheet);
0183 }
0184 
0185 void RemoveSheetCommand::undo()
0186 {
0187     sheet->map()->reviveSheet(sheet);
0188 }
0189 
0190 // ----- SheetPropertiesCommand -----
0191 
0192 SheetPropertiesCommand::SheetPropertiesCommand(Sheet* s)
0193 {
0194     sheet = s;
0195     map = s->map();
0196     oldDirection = newDirection = sheet->layoutDirection();
0197     oldAutoCalc = newAutoCalc = sheet->isAutoCalculationEnabled();
0198     oldShowGrid = newShowGrid = sheet->getShowGrid();
0199     oldShowPageOutline = newShowPageOutline = sheet->isShowPageOutline();
0200     oldShowFormula = newShowFormula = sheet->getShowFormula();
0201     oldHideZero = newHideZero = sheet->getHideZero();
0202     oldShowFormulaIndicator = newShowFormulaIndicator = sheet->getShowFormulaIndicator();
0203     oldShowCommentIndicator = newShowCommentIndicator = sheet->getShowCommentIndicator();
0204     oldColumnAsNumber = newColumnAsNumber = sheet->getShowColumnNumber();
0205     oldLcMode = newLcMode = sheet->getLcMode();
0206     oldCapitalizeFirstLetter = newCapitalizeFirstLetter = sheet->getFirstLetterUpper();
0207     setText(kundo2_i18n("Change Sheet Properties"));
0208 }
0209 
0210 void SheetPropertiesCommand::setLayoutDirection(Qt::LayoutDirection dir)
0211 {
0212     newDirection = dir;
0213 }
0214 
0215 void SheetPropertiesCommand::setAutoCalculationEnabled(bool b)
0216 {
0217     newAutoCalc = b;
0218 }
0219 
0220 void SheetPropertiesCommand::setShowGrid(bool b)
0221 {
0222     newShowGrid = b;
0223 }
0224 
0225 void SheetPropertiesCommand::setShowPageOutline(bool b)
0226 {
0227     newShowPageOutline = b;
0228 }
0229 
0230 void SheetPropertiesCommand::setShowFormula(bool b)
0231 {
0232     newShowFormula = b;
0233 }
0234 
0235 void SheetPropertiesCommand::setHideZero(bool b)
0236 {
0237     newHideZero = b;
0238 }
0239 
0240 void SheetPropertiesCommand::setShowFormulaIndicator(bool b)
0241 {
0242     newShowFormulaIndicator = b;
0243 }
0244 
0245 void SheetPropertiesCommand::setShowCommentIndicator(bool b)
0246 {
0247     newShowCommentIndicator = b;
0248 }
0249 
0250 void SheetPropertiesCommand::setColumnAsNumber(bool b)
0251 {
0252     newColumnAsNumber = b;
0253 }
0254 
0255 void SheetPropertiesCommand::setLcMode(bool b)
0256 {
0257     newLcMode = b;
0258 }
0259 
0260 void SheetPropertiesCommand::setCapitalizeFirstLetter(bool b)
0261 {
0262     newCapitalizeFirstLetter = b;
0263 }
0264 
0265 void SheetPropertiesCommand::redo()
0266 {
0267     sheet->setLayoutDirection(newDirection);
0268     sheet->setAutoCalculationEnabled(newAutoCalc);
0269     sheet->setShowGrid(newShowGrid);
0270     sheet->setShowPageOutline(newShowPageOutline);
0271     sheet->setShowFormula(newShowFormula);
0272     sheet->setHideZero(newHideZero);
0273     sheet->setShowFormulaIndicator(newShowFormulaIndicator);
0274     sheet->setShowCommentIndicator(newShowCommentIndicator);
0275     sheet->setShowColumnNumber(newColumnAsNumber);
0276     sheet->setLcMode(newLcMode);
0277     sheet->setFirstLetterUpper(newCapitalizeFirstLetter);
0278     sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::PropertiesChanged));
0279 }
0280 
0281 void SheetPropertiesCommand::undo()
0282 {
0283     sheet->setLayoutDirection(oldDirection);
0284     sheet->setAutoCalculationEnabled(oldAutoCalc);
0285     sheet->setShowGrid(oldShowGrid);
0286     sheet->setShowPageOutline(oldShowPageOutline);
0287     sheet->setShowFormula(oldShowFormula);
0288     sheet->setHideZero(oldHideZero);
0289     sheet->setShowFormulaIndicator(oldShowFormulaIndicator);
0290     sheet->setShowCommentIndicator(oldShowCommentIndicator);
0291     sheet->setShowColumnNumber(oldColumnAsNumber);
0292     sheet->setLcMode(oldLcMode);
0293     sheet->setFirstLetterUpper(oldCapitalizeFirstLetter);
0294     sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::PropertiesChanged));
0295 }