ItemModelFilterLineEdit.hpp
1 #pragma once
2 #include <Library/RecursiveFilterProxy.hpp>
3 
4 #include <score/widgets/SearchLineEdit.hpp>
5 
6 #include <QSortFilterProxyModel>
7 #include <QTreeView>
8 
9 #include <functional>
10 
11 namespace Library
12 {
13 class RecursiveFilterProxy;
15 {
16 public:
17  ItemModelFilterLineEdit(RecursiveFilterProxy& proxy, QTreeView& tv, QWidget* p)
19  , m_proxy{proxy}
20  , m_view{tv}
21  {
22  connect(this, &QLineEdit::textEdited, this, [this] { search(); });
23  }
24 
25  void search() override
26  {
27  if(text() != m_proxy.pattern())
28  {
29  m_proxy.setPattern(text());
30 
31  if(!text().isEmpty())
32  {
33  m_view.expandAll();
34 
35  if(m_proxy.hasChildren())
36  {
37  auto item_to_select = QModelIndex();
38  while(m_proxy.hasChildren(item_to_select))
39  {
40  item_to_select = m_proxy.index(0, 0, item_to_select);
41  }
42  m_view.setCurrentIndex(item_to_select);
43  }
44  }
45  }
46 
47  if(text().isEmpty())
48  {
49  m_proxy.invalidate();
50  m_view.collapseAll();
51  }
52  if(reset)
53  {
54  reset();
55  }
56  }
57 
58  std::function<void()> reset;
59  RecursiveFilterProxy& m_proxy;
60  QTreeView& m_view;
61 };
62 
63 }
Definition: RecursiveFilterProxy.hpp:10
Definition: SearchLineEdit.hpp:12
Definition: ItemModelFilterLineEdit.hpp:15