1 /* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "NamedAreaCommand.h"
21
22 #include "KLocalizedString"
23
24 #include "Damages.h"
25 #include "FormulaStorage.h"
26 #include "calligra_sheets_limits.h"
27 #include "Map.h"
28 #include "NamedAreaManager.h"
29 #include "Sheet.h"
30
31 using namespace Calligra::Sheets;
32
NamedAreaCommand(KUndo2Command * parent)33 NamedAreaCommand::NamedAreaCommand(KUndo2Command* parent)
34 : AbstractRegionCommand(parent)
35 {
36 setText(kundo2_i18n("Add Named Area"));
37 }
38
~NamedAreaCommand()39 NamedAreaCommand::~NamedAreaCommand()
40 {
41 }
42
setAreaName(const QString & name)43 void NamedAreaCommand::setAreaName(const QString& name)
44 {
45 m_areaName = name;
46 }
47
setReverse(bool reverse)48 void NamedAreaCommand::setReverse(bool reverse)
49 {
50 AbstractRegionCommand::setReverse(reverse);
51 if (!m_reverse)
52 setText(kundo2_i18n("Add Named Area"));
53 else
54 setText(kundo2_i18n("Remove Named Area"));
55 }
56
preProcessing()57 bool NamedAreaCommand::preProcessing()
58 {
59 if (!m_firstrun)
60 return true;
61 if (m_reverse)
62 return true;
63
64 const Region namedArea = m_sheet->map()->namedAreaManager()->namedArea(m_areaName);
65 if (!namedArea.isEmpty()) {
66 if (namedArea == *this)
67 return false;
68 m_oldArea = namedArea;
69 }
70 // no protection or matrix lock check needed
71 return isContiguous();
72 }
73
mainProcessing()74 bool NamedAreaCommand::mainProcessing()
75 {
76 debugSheets ;
77 if (!m_reverse) {
78 if (!m_oldArea.isEmpty())
79 m_sheet->map()->namedAreaManager()->remove(m_areaName);
80 m_sheet->map()->namedAreaManager()->insert(*this, m_areaName);
81 } else {
82 m_sheet->map()->namedAreaManager()->remove(m_areaName);
83 if (!m_oldArea.isEmpty())
84 m_sheet->map()->namedAreaManager()->insert(m_oldArea, m_areaName);
85 }
86 return true;
87 }
88
postProcessing()89 bool NamedAreaCommand::postProcessing()
90 {
91 // update formulas containing either the new or the old name
92 Map* const map = m_sheet->map();
93 foreach(Sheet* sheet, map->sheetList()) {
94 const QString tmp = '\'' + m_areaName + '\'';
95 const FormulaStorage* const storage = sheet->formulaStorage();
96 for (int c = 0; c < storage->count(); ++c) {
97 if (storage->data(c).expression().contains(tmp)) {
98 Cell cell(sheet, storage->col(c), storage->row(c));
99 if (cell.makeFormula()) {
100 // recalculate cells
101 map->addDamage(new CellDamage(cell, CellDamage::Appearance | CellDamage::Binding |
102 CellDamage::Value));
103 }
104 }
105 }
106 }
107 return AbstractRegionCommand::postProcessing();
108 }
109