MuseScore  3.4
Music composition and notation
filterabletreeview.h
Go to the documentation of this file.
1 //=============================================================================
2 // FilterableTreeView
3 //
4 // Copyright (C) 2018 Peter Jonas
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License version 2
8 // as published by the Free Software Foundation and appearing in
9 // the file LICENCE.GPL
10 //=============================================================================
11 
12 #ifndef __FILTERABLETREEVIEW_H__
13 #define __FILTERABLETREEVIEW_H__
14 
15 #include "filterableview.h"
16 
17 namespace Ms {
18 
19 //---------------------------------------------------------
20 // FilterableTreeViewTemplate
21 // We use this class template to create a class called FilterableTreeView
22 // that extends QTreeView, and a class called FilterableTreeWidget that
23 // extends QTreeWidget. The template allows us to create both at once without
24 // encountering inheritance problems or resorting to preprocessor hacks.
25 //---------------------------------------------------------
26 
27 template <typename T>
29  {
30 
31  private:
32  void toggleExpanded(const QModelIndex& node);
33  void toggleExpandedForUnselectable(const QModelIndex& node);
34  template <typename F> inline QModelIndex recurse(const F& func, const bool backwards = false) {
35  return recurseUnder(func, FilterableTreeViewTemplate<T>::rootIndex(), backwards);
36  }
37  template <typename F> inline QModelIndex recurse(const F& func, const QModelIndex& node, const bool backwards = false) {
38  if (node.isValid() && func(node))
39  return node; // test the node unless it is the root (root is invalid but can have decendants)
40  return recurseUnder(func, node, backwards); // test decendants of node
41  }
42  template <typename F> QModelIndex recurseUnder(const F& func, const QModelIndex& node, const bool backwards = false);
43 
44  protected:
45  virtual void keyPressEvent(QKeyEvent* event) override;
46 
47  public:
48  FilterableTreeViewTemplate(QWidget* parent = nullptr);
49  virtual void selectFirst() override;
50  virtual void selectNext() override;
51  virtual void selectPrevious() override;
52  inline virtual void toInitialState() override {
54  }
55  virtual void toInitialState(const QModelIndex& node);
56  inline virtual bool filter(const QString& searchString) override {
57  return filter(searchString, FilterableTreeViewTemplate<T>::rootIndex());
58  }
59  virtual bool filter(const QString& searchString, const QModelIndex& node);
60  };
61 
62 // Define aliases to hide scary template syntax
65 
66 // Now you can promote QTreeView to FilterableTreeView and QTreeWidget to FilterableTreeWidget
67 
68 }
69 
70 #endif // __FILTERABLETREEVIEW_H__
virtual void selectPrevious() override
Definition: filterabletreeview.cpp:137
void toggleExpandedForUnselectable(const QModelIndex &node)
Definition: filterabletreeview.cpp:68
FilterableTreeViewTemplate(QWidget *parent=nullptr)
Definition: filterabletreeview.cpp:21
virtual void selectFirst() override
Definition: filterabletreeview.cpp:82
QModelIndex recurseUnder(const F &func, const QModelIndex &node, const bool backwards=false)
Definition: filterabletreeview.cpp:233
Definition: filterabletreeview.h:28
virtual void selectNext() override
Definition: filterabletreeview.cpp:97
void toggleExpanded(const QModelIndex &node)
Definition: filterabletreeview.cpp:57
Definition: aeolus.cpp:26
virtual void toInitialState() override
Definition: filterabletreeview.h:52
QModelIndex recurse(const F &func, const bool backwards=false)
Definition: filterabletreeview.h:34
virtual bool filter(const QString &searchString) override
Definition: filterabletreeview.h:56
virtual void keyPressEvent(QKeyEvent *event) override
Definition: filterabletreeview.cpp:39
QModelIndex recurse(const F &func, const QModelIndex &node, const bool backwards=false)
Definition: filterabletreeview.h:37
Definition: filterableview.h:24