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

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 "DeleteCommand.h"
0021 
0022 #include "CellStorage.h"
0023 #include "Region.h"
0024 #include "RowColumnFormat.h"
0025 #include "RowFormatStorage.h"
0026 #include "Sheet.h"
0027 #include "Validity.h"
0028 
0029 #include <KLocalizedString>
0030 
0031 using namespace Calligra::Sheets;
0032 
0033 DeleteCommand::DeleteCommand(KUndo2Command *parent)
0034         : AbstractDataManipulator(parent)
0035         , m_mode(Everything)
0036 {
0037     setText(kundo2_i18n("Delete"));
0038     m_checkLock = true;
0039 }
0040 
0041 DeleteCommand::~DeleteCommand()
0042 {
0043     qDeleteAll(m_columnFormats);
0044     qDeleteAll(m_rowFormats);
0045 }
0046 
0047 void DeleteCommand::setMode(Mode mode)
0048 {
0049     m_mode = mode;
0050 }
0051 
0052 bool DeleteCommand::process(Element* element)
0053 {
0054     Q_ASSERT(!m_reverse);
0055 
0056     // The RecalcManager needs a valid sheet.
0057     if (!element->sheet())
0058         element->setSheet(m_sheet);
0059 
0060     const QRect range = element->rect();
0061 
0062     if (element->isColumn()) {
0063         // column-wise processing
0064         for (int col = range.left(); col <= range.right(); ++col) {
0065             Cell cell = m_sheet->cellStorage()->firstInColumn(col);
0066             while (!cell.isNull()) {
0067                 m_sheet->cellStorage()->take(col, cell.row());
0068                 cell = m_sheet->cellStorage()->nextInColumn(col, cell.row());
0069             }
0070             if (m_mode == OnlyCells) {
0071                 continue;
0072             }
0073 
0074             const ColumnFormat* columnFormat = m_sheet->columnFormat(col);
0075             if (m_firstrun && !columnFormat->isDefault()) {
0076                 ColumnFormat* oldColumnFormat = new ColumnFormat(*columnFormat);
0077                 oldColumnFormat->setNext(0);
0078                 oldColumnFormat->setPrevious(0);
0079                 m_columnFormats.insert(oldColumnFormat);
0080             }
0081             m_sheet->deleteColumnFormat(col);
0082         }
0083     } else if (element->isRow()) {
0084         // row-wise processing
0085         for (int row = range.top(); row <= range.bottom(); ++row) {
0086             Cell cell = m_sheet->cellStorage()->firstInRow(row);
0087             while (!cell.isNull()) {
0088                 m_sheet->cellStorage()->take(cell.column(), row);
0089                 cell = m_sheet->cellStorage()->nextInRow(cell.column(), row);
0090             }
0091             if (m_mode == OnlyCells) {
0092                 continue;
0093             }
0094             // TODO: better storing of row formats
0095             if (m_firstrun && !m_sheet->rowFormats()->isDefaultRow(row)) {
0096                 m_rowFormats.insert(new RowFormat(m_sheet->rowFormats(), row));
0097             }
0098             m_sheet->deleteRowFormat(row);
0099         }
0100     } else {
0101         // row-wise processing
0102         for (int row = range.top(); row <= range.bottom(); ++row) {
0103             Cell cell = m_sheet->cellStorage()->firstInRow(row);
0104             if (!cell.isNull() && cell.column() < range.left())
0105                 cell = m_sheet->cellStorage()->nextInRow(range.left() - 1, row);
0106             while (!cell.isNull()) {
0107                 if (cell.column() > range.right())
0108                     break;
0109 
0110                 m_sheet->cellStorage()->take(cell.column(), row);
0111                 cell = m_sheet->cellStorage()->nextInRow(cell.column(), row);
0112             }
0113         }
0114     }
0115 
0116     // the rect storages
0117     m_sheet->cellStorage()->setComment(Region(range, element->sheet()), QString());
0118     m_sheet->cellStorage()->setConditions(Region(range, element->sheet()), Conditions());
0119     Style style;
0120     style.setDefault();
0121     m_sheet->cellStorage()->setStyle(Region(range, element->sheet()), style);
0122     m_sheet->cellStorage()->setValidity(Region(range, element->sheet()), Validity());
0123     return true;
0124 }
0125 
0126 bool DeleteCommand::mainProcessing()
0127 {
0128     if (m_reverse) {
0129         foreach(ColumnFormat* columnFormat, m_columnFormats) {
0130             m_sheet->insertColumnFormat(new ColumnFormat(*columnFormat));
0131         }
0132         foreach(RowFormat* rowFormat, m_rowFormats) {
0133             m_sheet->insertRowFormat(rowFormat);
0134         }
0135     }
0136     return AbstractDataManipulator::mainProcessing();
0137 }