Add select-all, deselect-all and invert-selection options to filters

To every filter list add a context menu that allows selection of all,
selection of none or inversion of selection.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-12-24 14:34:53 +01:00
parent 7e744bc7aa
commit 5e86442bab
4 changed files with 30 additions and 0 deletions

View file

@ -500,6 +500,13 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button)
} }
} }
void FilterBase::addContextMenuEntry(const QString &s, void (FilterModelBase::*fn)())
{
QAction *act = new QAction(s, this);
connect(act, &QAction::triggered, model, fn);
ui.filterList->addAction(act);
}
FilterBase::FilterBase(FilterModelBase *model_, QWidget *parent) FilterBase::FilterBase(FilterModelBase *model_, QWidget *parent)
: QWidget(parent) : QWidget(parent)
, model(model_) , model(model_)
@ -513,6 +520,11 @@ FilterBase::FilterBase(FilterModelBase *model_, QWidget *parent)
filter->setFilterCaseSensitivity(Qt::CaseInsensitive); filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
connect(ui.filterInternalList, SIGNAL(textChanged(QString)), filter, SLOT(setFilterFixedString(QString))); connect(ui.filterInternalList, SIGNAL(textChanged(QString)), filter, SLOT(setFilterFixedString(QString)));
ui.filterList->setModel(filter); ui.filterList->setModel(filter);
addContextMenuEntry(tr("Select All"), &FilterModelBase::selectAll);
addContextMenuEntry(tr("Unselect All"), &FilterModelBase::clearFilter);
addContextMenuEntry(tr("Invert Selection"), &FilterModelBase::invertSelection);
ui.filterList->setContextMenuPolicy(Qt::ActionsContextMenu);
} }
void FilterBase::showEvent(QShowEvent *event) void FilterBase::showEvent(QShowEvent *event)

View file

@ -167,6 +167,7 @@ public:
}; };
class FilterBase : public QWidget { class FilterBase : public QWidget {
void addContextMenuEntry(const QString &s, void (FilterModelBase::*)());
protected: protected:
FilterBase(FilterModelBase *model, QWidget *parent = 0); FilterBase(FilterModelBase *model, QWidget *parent = 0);
FilterModelBase *model; FilterModelBase *model;

View file

@ -100,6 +100,21 @@ void FilterModelBase::clearFilter()
emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0));
} }
void FilterModelBase::selectAll()
{
std::fill(checkState.begin(), checkState.end(), true);
anyChecked = true;
emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0));
}
void FilterModelBase::invertSelection()
{
for (char &b: checkState)
b = !b;
anyChecked = std::any_of(checkState.begin(), checkState.end(), [](char b){return !!b;});
emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0));
}
SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent) SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent)
{ {
} }

View file

@ -11,6 +11,8 @@ class FilterModelBase : public QStringListModel {
public: public:
virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0; virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
void clearFilter(); void clearFilter();
void selectAll();
void invertSelection();
std::vector<char> checkState; std::vector<char> checkState;
bool anyChecked; bool anyChecked;
protected: protected: