kspread

ApplyFilterCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "ApplyFilterCommand.h"
00021 
00022 #include <klocale.h>
00023 
00024 #include "CellStorage.h"
00025 #include "Damages.h"
00026 #include "Map.h"
00027 #include "Sheet.h"
00028 #include "RowColumnFormat.h"
00029 
00030 #include "database/Database.h"
00031 #include "database/Filter.h"
00032 
00033 using namespace KSpread;
00034 
00035 ApplyFilterCommand::ApplyFilterCommand()
00036     : AbstractRegionCommand()
00037 {
00038     setText(i18n("Apply Filter"));
00039 }
00040 
00041 ApplyFilterCommand::~ApplyFilterCommand()
00042 {
00043 }
00044 
00045 void ApplyFilterCommand::redo()
00046 {
00047     m_undoData.clear();
00048     Database database = m_database;
00049 
00050     Sheet* const sheet = database.range().lastSheet();
00051     const QRect range = database.range().lastRange();
00052     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
00053     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
00054     for (int i = start + 1; i <= end; ++i)
00055     {
00056         const bool isFiltered = !database.filter().evaluate(database, i);
00057 //         kDebug() <<"Filtering column/row" << i <<"?" << isFiltered;
00058         if (database.orientation() == Qt::Vertical)
00059         {
00060             m_undoData[i] = sheet->rowFormat(i)->isFiltered();
00061             sheet->nonDefaultRowFormat(i)->setFiltered(isFiltered);
00062         }
00063         else // database.orientation() == Qt::Horizontal
00064         {
00065             m_undoData[i] = sheet->columnFormat(i)->isFiltered();
00066             sheet->nonDefaultColumnFormat(i)->setFiltered(isFiltered);
00067         }
00068     }
00069     if (database.orientation() == Qt::Vertical)
00070         sheet->emitHideRow();
00071     else // database.orientation() == Qt::Horizontal
00072         sheet->emitHideColumn();
00073 
00074     m_sheet->cellStorage()->setDatabase(*this, Database());
00075     m_sheet->cellStorage()->setDatabase(*this, database);
00076     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
00077 }
00078 
00079 void ApplyFilterCommand::undo()
00080 {
00081     Database database = m_database;
00082     database.setFilter(*m_oldFilter);
00083 
00084     Sheet* const sheet = database.range().lastSheet();
00085     const QRect range = database.range().lastRange();
00086     const int start = database.orientation() == Qt::Vertical ? range.top() : range.left();
00087     const int end = database.orientation() == Qt::Vertical ? range.bottom() : range.right();
00088     for (int i = start + 1; i <= end; ++i)
00089     {
00090         if (database.orientation() == Qt::Vertical)
00091             sheet->nonDefaultRowFormat(i)->setFiltered(m_undoData[i]);
00092         else // database.orientation() == Qt::Horizontal
00093             sheet->nonDefaultColumnFormat(i)->setFiltered(m_undoData[i]);
00094     }
00095     if (database.orientation() == Qt::Vertical)
00096         sheet->emitHideRow();
00097     else // database.orientation() == Qt::Horizontal
00098         sheet->emitHideColumn();
00099 
00100     m_sheet->cellStorage()->setDatabase(*this, Database());
00101     m_sheet->cellStorage()->setDatabase(*this, database);
00102     m_sheet->map()->addDamage(new CellDamage(m_sheet, *this, CellDamage::Appearance));
00103 }
00104 
00105 void ApplyFilterCommand::setDatabase(const Database& database)
00106 {
00107     m_database = database;
00108 }
00109 
00110 void ApplyFilterCommand::setOldFilter(const Filter& filter)
00111 {
00112     m_oldFilter = new Filter(filter);
00113 }