File indexing completed on 2024-05-19 16:08:01

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "NamedAreaCommand.h"
0021 
0022 #include "KLocalizedString"
0023 
0024 #include "Damages.h"
0025 #include "FormulaStorage.h"
0026 #include "calligra_sheets_limits.h"
0027 #include "Map.h"
0028 #include "NamedAreaManager.h"
0029 #include "Sheet.h"
0030 
0031 using namespace Calligra::Sheets;
0032 
0033 NamedAreaCommand::NamedAreaCommand(KUndo2Command* parent)
0034         : AbstractRegionCommand(parent)
0035 {
0036     setText(kundo2_i18n("Add Named Area"));
0037 }
0038 
0039 NamedAreaCommand::~NamedAreaCommand()
0040 {
0041 }
0042 
0043 void NamedAreaCommand::setAreaName(const QString& name)
0044 {
0045     m_areaName = name;
0046 }
0047 
0048 void NamedAreaCommand::setReverse(bool reverse)
0049 {
0050     AbstractRegionCommand::setReverse(reverse);
0051     if (!m_reverse)
0052         setText(kundo2_i18n("Add Named Area"));
0053     else
0054         setText(kundo2_i18n("Remove Named Area"));
0055 }
0056 
0057 bool NamedAreaCommand::preProcessing()
0058 {
0059     if (!m_firstrun)
0060         return true;
0061     if (m_reverse)
0062         return true;
0063 
0064     const Region namedArea = m_sheet->map()->namedAreaManager()->namedArea(m_areaName);
0065     if (!namedArea.isEmpty()) {
0066         if (namedArea == *this)
0067             return false;
0068         m_oldArea = namedArea;
0069     }
0070     // no protection or matrix lock check needed
0071     return isContiguous();
0072 }
0073 
0074 bool NamedAreaCommand::mainProcessing()
0075 {
0076     debugSheets ;
0077     if (!m_reverse) {
0078         if (!m_oldArea.isEmpty())
0079             m_sheet->map()->namedAreaManager()->remove(m_areaName);
0080         m_sheet->map()->namedAreaManager()->insert(*this, m_areaName);
0081     } else {
0082         m_sheet->map()->namedAreaManager()->remove(m_areaName);
0083         if (!m_oldArea.isEmpty())
0084             m_sheet->map()->namedAreaManager()->insert(m_oldArea, m_areaName);
0085     }
0086     return true;
0087 }
0088 
0089 bool NamedAreaCommand::postProcessing()
0090 {
0091     // update formulas containing either the new or the old name
0092     Map* const map = m_sheet->map();
0093     foreach(Sheet* sheet, map->sheetList()) {
0094         const QString tmp = '\'' + m_areaName + '\'';
0095         const FormulaStorage* const storage = sheet->formulaStorage();
0096         for (int c = 0; c < storage->count(); ++c) {
0097             if (storage->data(c).expression().contains(tmp)) {
0098                 Cell cell(sheet, storage->col(c), storage->row(c));
0099                 if (cell.makeFormula()) {
0100                     // recalculate cells
0101                     map->addDamage(new CellDamage(cell, CellDamage::Appearance | CellDamage::Binding |
0102                                                   CellDamage::Value));
0103                 }
0104             }
0105         }
0106     }
0107     return AbstractRegionCommand::postProcessing();
0108 }