Add a proof of concept for filtering the Dive List,

Press CTRL+F and a line edit will appear, whenever you write on that will
be used as a filter against all columns. The results are maybe somewhat
surprising in trip mode, but when sorting by another column this shows
some potential.

Hit ESC to remove the filtering.

I need to find a better position to put the Widget, but it's a proof of
concept.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2013-05-30 01:04:42 -03:00 committed by Dirk Hohndel
parent c4f06dc536
commit 77880b7a07
2 changed files with 40 additions and 2 deletions

View file

@ -14,17 +14,52 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QAction> #include <QAction>
#include <QLineEdit>
#include <QKeyEvent>
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false),
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), currentHeaderClicked(-1) currentHeaderClicked(-1), searchBox(new QLineEdit(this))
{ {
setUniformRowHeights(true); setUniformRowHeights(true);
setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate()); setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate());
QSortFilterProxyModel *model = new QSortFilterProxyModel(this); QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
model->setSortRole(TreeItemDT::SORT_ROLE); model->setSortRole(TreeItemDT::SORT_ROLE);
model->setFilterKeyColumn(-1); // filter all columns
setModel(model); setModel(model);
setSortingEnabled(false); setSortingEnabled(false);
header()->setContextMenuPolicy(Qt::ActionsContextMenu); header()->setContextMenuPolicy(Qt::ActionsContextMenu);
QAction *showSearchBox = new QAction(tr("Show Search Box"), this);
showSearchBox->setShortcut( Qt::CTRL + Qt::Key_F);
showSearchBox->setShortcutContext(Qt::ApplicationShortcut);
addAction(showSearchBox);
searchBox->installEventFilter(this);
searchBox->hide();
connect(showSearchBox, SIGNAL(triggered(bool)), this, SLOT(showSearchEdit()));
connect(searchBox, SIGNAL(textChanged(QString)), model, SLOT(setFilterFixedString(QString)));
}
void DiveListView::showSearchEdit()
{
searchBox->show();
searchBox->setFocus();
}
bool DiveListView::eventFilter(QObject* , QEvent* event)
{
if(event->type() != QEvent::KeyPress){
return false;
}
QKeyEvent *keyEv = static_cast<QKeyEvent*>(event);
if (keyEv->key() != Qt::Key_Escape){
return false;
}
searchBox->clear();
searchBox->hide();
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
m->setFilterFixedString(QString());
return true;
} }
void DiveListView::headerClicked(int i) void DiveListView::headerClicked(int i)

View file

@ -25,11 +25,13 @@ public:
void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
void currentChanged(const QModelIndex& current, const QModelIndex& previous); void currentChanged(const QModelIndex& current, const QModelIndex& previous);
void reload(DiveTripModel::Layout layout = DiveTripModel::TREE, bool forceSort = true); void reload(DiveTripModel::Layout layout = DiveTripModel::TREE, bool forceSort = true);
bool eventFilter(QObject* , QEvent* );
public slots: public slots:
void toggleColumnVisibilityByIndex(); void toggleColumnVisibilityByIndex();
void reloadHeaderActions(); void reloadHeaderActions();
void headerClicked(int); void headerClicked(int);
void showSearchEdit();
Q_SIGNALS: Q_SIGNALS:
void currentDiveChanged(int divenr); void currentDiveChanged(int divenr);
@ -37,6 +39,7 @@ private:
bool mouseClickSelection; bool mouseClickSelection;
int currentHeaderClicked; int currentHeaderClicked;
DiveTripModel::Layout currentLayout; DiveTripModel::Layout currentLayout;
QLineEdit *searchBox;
}; };
#endif // DIVELISTVIEW_H #endif // DIVELISTVIEW_H