1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/* This file is part of the KDE project
Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "FilterPopup.h"
#include <QButtonGroup>
#include <QCheckBox>
#include <QHash>
#include <QList>
#include <QScrollArea>
#include <QVBoxLayout>
#include <KLocalizedString>
#include "CellStorage.h"
#include "Database.h"
#include "Filter.h"
#include "Map.h"
#include "RowColumnFormat.h"
#include "Sheet.h"
#include "ValueConverter.h"
#include "commands/ApplyFilterCommand.h"
#include <algorithm>
using namespace Calligra::Sheets;
class FilterPopup::Private
{
public:
QAbstractButton* allCheckbox;
QAbstractButton* emptyCheckbox;
QAbstractButton* notEmptyCheckbox;
QList<QCheckBox*> checkboxes;
int fieldNumber;
Database database;
bool dirty;
public:
void initGUI(FilterPopup* parent, const Cell& cell, const Database* database);
};
void FilterPopup::Private::initGUI(FilterPopup* parent, const Cell& cell, const Database* database)
{
QButtonGroup* buttonGroup = new QButtonGroup(parent);
buttonGroup->setExclusive(false);
connect(buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)),
parent, SLOT(buttonClicked(QAbstractButton*)));
QVBoxLayout* layout = new QVBoxLayout(parent);
layout->setMargin(3);
layout->setSpacing(0);
allCheckbox = new QCheckBox(i18n("All"), parent);
buttonGroup->addButton(allCheckbox);
layout->addWidget(allCheckbox);
emptyCheckbox = new QCheckBox(i18n("Empty"), parent);
emptyCheckbox->setChecked(true);
buttonGroup->addButton(emptyCheckbox);
layout->addWidget(emptyCheckbox);
notEmptyCheckbox = new QCheckBox(i18n("Non-empty"), parent);
notEmptyCheckbox->setChecked(true);
buttonGroup->addButton(notEmptyCheckbox);
layout->addWidget(notEmptyCheckbox);
layout->addSpacing(3);
const Sheet* sheet = cell.sheet();
const QRect range = database->range().lastRange();
const bool isRowFilter = database->orientation() == Qt::Vertical;
const int start = isRowFilter ? range.top() : range.left();
const int end = isRowFilter ? range.bottom() : range.right();
const int j = isRowFilter ? cell.column() : cell.row();
QSet<QString> items;
for (int i = start + (database->containsHeader() ? 1 : 0); i <= end; ++i) {
const Value value = isRowFilter ? sheet->cellStorage()->value(j, i)
: sheet->cellStorage()->value(i, j);
const QString string = sheet->map()->converter()->asString(value).asString();
if (!string.isEmpty())
items.insert(string);
}
QWidget* scrollWidget = new QWidget(parent);
QVBoxLayout* scrollLayout = new QVBoxLayout(scrollWidget);
scrollLayout->setMargin(0);
scrollLayout->setSpacing(0);
const int fieldNumber = j - (isRowFilter ? range.left() : range.top());
const QHash<QString, Filter::Comparison> conditions = database->filter().conditions(fieldNumber);
const bool defaultCheckState = conditions.isEmpty() ? true
: !(conditions[conditions.keys()[0]] == Filter::Match ||
conditions[conditions.keys()[0]] == Filter::Empty);
QList<QString> sortedItems = items.toList();
std::sort(sortedItems.begin(), sortedItems.end());
bool isAll = true;
QCheckBox* item;
for (int i = 0; i < sortedItems.count(); ++i) {
const QString value = sortedItems[i];
item = new QCheckBox(value, scrollWidget);
item->setChecked(conditions.contains(value) ? !defaultCheckState : defaultCheckState);
buttonGroup->addButton(item);
scrollLayout->addWidget(item);
checkboxes.append(item);
isAll = isAll && item->isChecked();
}
emptyCheckbox->setChecked(conditions.contains("") ? !defaultCheckState : defaultCheckState);
allCheckbox->setChecked(isAll && emptyCheckbox->isChecked());
notEmptyCheckbox->setChecked(isAll);
QScrollArea* scrollArea = new QScrollArea(parent);
layout->addWidget(scrollArea);
scrollArea->setWidget(scrollWidget);
}
FilterPopup::FilterPopup(QWidget* parent, const Cell& cell, Database* database)
: QFrame(parent, Qt::Popup)
, d(new Private)
{
setAttribute(Qt::WA_DeleteOnClose);
setBackgroundRole(QPalette::Base);
setFrameStyle(QFrame::Panel | QFrame::Raised);
d->database = *database;
d->dirty = false;
d->initGUI(this, cell, database);
if (database->orientation() == Qt::Vertical)
d->fieldNumber = cell.column() - database->range().lastRange().left();
else // Qt::Horizontal
d->fieldNumber = cell.row() - database->range().lastRange().top();
debugSheets << "FilterPopup for fieldNumber" << d->fieldNumber;
}
FilterPopup::~FilterPopup()
{
delete d;
}
void FilterPopup::updateFilter(Filter* filter) const
{
if (d->allCheckbox->isChecked())
filter->removeConditions(d->fieldNumber); // remove all conditions for this field
else if (d->notEmptyCheckbox->isChecked()) {
// emptyCheckbox is not checked, because allCheckbox is not.
filter->removeConditions(d->fieldNumber);
filter->addCondition(Filter::AndComposition, d->fieldNumber, Filter::NotMatch, "");
} else {
filter->removeConditions(d->fieldNumber);
QList<QString> matchList;
QList<QString> notMatchList;
if (d->emptyCheckbox->isChecked())
matchList.append("");
else
notMatchList.append("");
foreach(QCheckBox* checkbox, d->checkboxes) {
if (checkbox->isChecked())
matchList.append(checkbox->text());
else
notMatchList.append(checkbox->text());
}
// be lazy; choose the comparison causing least effort
const Filter::Comparison comparison = (matchList.count() < notMatchList.count())
? Filter::Match : Filter::NotMatch;
const Filter::Composition composition = (comparison == Filter::Match)
? Filter::OrComposition : Filter::AndComposition;
const QList<QString> values = (comparison == Filter::Match) ? matchList : notMatchList;
debugSheets << "adding conditions for fieldNumber" << d->fieldNumber;
Filter subFilter;
for (int i = 0; i < values.count(); ++i)
subFilter.addCondition(composition, d->fieldNumber, comparison, values[i]);
filter->addSubFilter(Filter::AndComposition, subFilter);
}
}
void FilterPopup::closeEvent(QCloseEvent* event)
{
if (d->dirty) {
Filter filter = d->database.filter();
updateFilter(&filter);
// any real change?
if (d->database.filter() != filter) {
ApplyFilterCommand* command = new ApplyFilterCommand();
command->setSheet(d->database.range().lastSheet());
command->add(d->database.range());
command->setOldFilter(d->database.filter());
d->database.setFilter(filter);
d->database.dump();
command->setDatabase(d->database);
command->execute();
}
}
QFrame::closeEvent(event);
}
void FilterPopup::buttonClicked(QAbstractButton* button)
{
d->dirty = true;
if (button == d->allCheckbox) {
foreach(QCheckBox* checkbox, d->checkboxes)
checkbox->setChecked(button->isChecked());
d->emptyCheckbox->setChecked(button->isChecked());
d->notEmptyCheckbox->setChecked(button->isChecked());
} else if (button == d->emptyCheckbox) {
bool isAll = button->isChecked();
if (isAll) {
foreach(QCheckBox* checkbox, d->checkboxes) {
if (!checkbox->isChecked()) {
isAll = false;
break;
}
}
}
d->allCheckbox->setChecked(isAll);
} else if (button == d->notEmptyCheckbox) {
foreach(QCheckBox* checkbox, d->checkboxes)
checkbox->setChecked(button->isChecked());
d->allCheckbox->setChecked(button->isChecked() && d->emptyCheckbox->isChecked());
} else {
bool isAll = d->emptyCheckbox->isChecked();
if (isAll) {
foreach(QCheckBox* checkbox, d->checkboxes) {
if (!checkbox->isChecked()) {
isAll = false;
break;
}
}
}
d->allCheckbox->setChecked(isAll);
d->notEmptyCheckbox->setChecked(isAll);
}
}
void FilterPopup::showPopup(QWidget *parent, const Cell &cell, const QRect &cellRect, Database *database)
{
FilterPopup* popup = new FilterPopup(parent, cell, database);
const QPoint position(database->orientation() == Qt::Vertical ? cellRect.bottomLeft() : cellRect.topRight());
popup->move(parent->mapToGlobal(position));
popup->show();
}
|