summaryrefslogtreecommitdiff
path: root/src/filterwidget.h
blob: 4fb9831f085122f5fa5c68cff1d6445ab6168824 (plain)
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
#ifndef FILTERWIDGET_H
#define FILTERWIDGET_H

#include <QObject>
#include <QLineEdit>
#include <QToolButton>
#include <QList>
#include <QAbstractItemView>

class EventFilter;
class FilterWidget;

class FilterWidgetProxyModel : public QSortFilterProxyModel
{
  Q_OBJECT;

public:
  FilterWidgetProxyModel(FilterWidget& fw, QWidget* parent=nullptr);
  using QSortFilterProxyModel::invalidateFilter;

protected:
  bool filterAcceptsRow(int row, const QModelIndex& parent) const override;

private:
  FilterWidget& m_filter;
};


class FilterWidget : public QObject
{
  Q_OBJECT;

public:
  using predFun = std::function<bool (const QString& what)>;

  FilterWidget();

  void setEdit(QLineEdit* edit);
  void setList(QAbstractItemView* list);
  void clear();
  bool empty() const;

  QModelIndex map(const QModelIndex& index);

  bool matches(predFun pred) const;

signals:
  void changed();

private:
  QLineEdit* m_edit;
  QAbstractItemView* m_list;
  FilterWidgetProxyModel* m_proxy;
  EventFilter* m_eventFilter;
  QToolButton* m_clear;
  QString m_text;
  QList<QList<QString>> m_compiled;

  void unhook();
  void createClear();
  void hookEvents();
  void repositionClearButton();

  void onTextChanged();
  void onResized();

  void compile();
};

#endif // FILTERWIDGET_H