File indexing completed on 2024-05-19 16:07:59

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 "ApplyFilterCommand.h"
0021 
0022 #include <KLocalizedString>
0023 
0024 #include "CellStorage.h"
0025 #include "Damages.h"
0026 #include "Map.h"
0027 #include "Sheet.h"
0028 #include "RowColumnFormat.h"
0029 #include "RowFormatStorage.h"
0030 
0031 #include "database/Database.h"
0032 #include "database/Filter.h"
0033 
0034 using namespace Calligra::Sheets;
0035 
0036 ApplyFilterCommand::ApplyFilterCommand()
0037         : AbstractRegionCommand()
0038 {
0039     setText(kundo2_i18n("Apply Filter"));
0040 }
0041 
0042 ApplyFilterCommand::~ApplyFilterCommand()
0043 {
0044 }
0045 
0046 void ApplyFilterCommand::redo()
0047 {
0048     m_undoData.clear();
0049     Database database = m_database;
0050 
0051     Sheet* const sheet = database.range().lastSheet();
0052     const QRect range = database.range().lastRange();
0053     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
0054     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
0055     for (int i = start + 1; i <= end; ++i) {
0056         const bool isFiltered = !database.filter().evaluate(database, i);
0057 //         debugSheets <<"Filtering column/row" << i <<"?" << isFiltered;
0058         if (database.orientation() == Qt::Vertical) {
0059             m_undoData[i] = sheet->rowFormats()->isFiltered(i);
0060             sheet->rowFormats()->setFiltered(i, i, isFiltered);
0061         } else { // database.orientation() == Qt::Horizontal
0062             m_undoData[i] = sheet->columnFormat(i)->isFiltered();
0063             sheet->nonDefaultColumnFormat(i)->setFiltered(isFiltered);
0064         }
0065     }
0066     if (database.orientation() == Qt::Vertical)
0067         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::RowsChanged));
0068     else // database.orientation() == Qt::Horizontal
0069         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::ColumnsChanged));
0070 
0071     m_sheet->cellStorage()->setDatabase(*this, Database());
0072     m_sheet->cellStorage()->setDatabase(*this, database);
0073     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
0074 }
0075 
0076 void ApplyFilterCommand::undo()
0077 {
0078     Database database = m_database;
0079     database.setFilter(*m_oldFilter);
0080 
0081     Sheet* const sheet = database.range().lastSheet();
0082     const QRect range = database.range().lastRange();
0083     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
0084     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
0085     for (int i = start + 1; i <= end; ++i) {
0086         if (database.orientation() == Qt::Vertical)
0087             sheet->rowFormats()->setFiltered(i, i, m_undoData[i]);
0088         else // database.orientation() == Qt::Horizontal
0089             sheet->nonDefaultColumnFormat(i)->setFiltered(m_undoData[i]);
0090     }
0091     if (database.orientation() == Qt::Vertical)
0092         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::RowsChanged));
0093     else // database.orientation() == Qt::Horizontal
0094         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::ColumnsChanged));
0095 
0096     m_sheet->cellStorage()->setDatabase(*this, Database());
0097     m_sheet->cellStorage()->setDatabase(*this, database);
0098     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
0099 }
0100 
0101 void ApplyFilterCommand::setDatabase(const Database& database)
0102 {
0103     m_database = database;
0104 }
0105 
0106 void ApplyFilterCommand::setOldFilter(const Filter& filter)
0107 {
0108     m_oldFilter = new Filter(filter);
0109 }