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 "ApplyFilterCommand.h"
21 
22 #include <KLocalizedString>
23 
24 #include "CellStorage.h"
25 #include "Damages.h"
26 #include "Map.h"
27 #include "Sheet.h"
28 #include "RowColumnFormat.h"
29 #include "RowFormatStorage.h"
30 
31 #include "database/Database.h"
32 #include "database/Filter.h"
33 
34 using namespace Calligra::Sheets;
35 
ApplyFilterCommand()36 ApplyFilterCommand::ApplyFilterCommand()
37         : AbstractRegionCommand()
38 {
39     setText(kundo2_i18n("Apply Filter"));
40 }
41 
~ApplyFilterCommand()42 ApplyFilterCommand::~ApplyFilterCommand()
43 {
44 }
45 
redo()46 void ApplyFilterCommand::redo()
47 {
48     m_undoData.clear();
49     Database database = m_database;
50 
51     Sheet* const sheet = database.range().lastSheet();
52     const QRect range = database.range().lastRange();
53     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
54     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
55     for (int i = start + 1; i <= end; ++i) {
56         const bool isFiltered = !database.filter().evaluate(database, i);
57 //         debugSheets <<"Filtering column/row" << i <<"?" << isFiltered;
58         if (database.orientation() == Qt::Vertical) {
59             m_undoData[i] = sheet->rowFormats()->isFiltered(i);
60             sheet->rowFormats()->setFiltered(i, i, isFiltered);
61         } else { // database.orientation() == Qt::Horizontal
62             m_undoData[i] = sheet->columnFormat(i)->isFiltered();
63             sheet->nonDefaultColumnFormat(i)->setFiltered(isFiltered);
64         }
65     }
66     if (database.orientation() == Qt::Vertical)
67         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::RowsChanged));
68     else // database.orientation() == Qt::Horizontal
69         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::ColumnsChanged));
70 
71     m_sheet->cellStorage()->setDatabase(*this, Database());
72     m_sheet->cellStorage()->setDatabase(*this, database);
73     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
74 }
75 
undo()76 void ApplyFilterCommand::undo()
77 {
78     Database database = m_database;
79     database.setFilter(*m_oldFilter);
80 
81     Sheet* const sheet = database.range().lastSheet();
82     const QRect range = database.range().lastRange();
83     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
84     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
85     for (int i = start + 1; i <= end; ++i) {
86         if (database.orientation() == Qt::Vertical)
87             sheet->rowFormats()->setFiltered(i, i, m_undoData[i]);
88         else // database.orientation() == Qt::Horizontal
89             sheet->nonDefaultColumnFormat(i)->setFiltered(m_undoData[i]);
90     }
91     if (database.orientation() == Qt::Vertical)
92         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::RowsChanged));
93     else // database.orientation() == Qt::Horizontal
94         sheet->map()->addDamage(new SheetDamage(sheet, SheetDamage::ColumnsChanged));
95 
96     m_sheet->cellStorage()->setDatabase(*this, Database());
97     m_sheet->cellStorage()->setDatabase(*this, database);
98     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
99 }
100 
setDatabase(const Database & database)101 void ApplyFilterCommand::setDatabase(const Database& database)
102 {
103     m_database = database;
104 }
105 
setOldFilter(const Filter & filter)106 void ApplyFilterCommand::setOldFilter(const Filter& filter)
107 {
108     m_oldFilter = new Filter(filter);
109 }
110