Sayonara Player
SearchableView.h
1 /* SearchableView.h */
2 
3 /* Copyright (C) 2011-2017 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef SEARCHABLEVIEW_H
22 #define SEARCHABLEVIEW_H
23 
24 #include "GUI/Utils/SearchableWidget/SelectionView.h"
25 #include "Utils/Pimpl.h"
26 
27 #include <QKeyEvent>
28 #include <QTableView>
29 #include <QListView>
30 #include <QTreeView>
31 
32 class QAbstractItemView;
33 class QItemSelectionModel;
35 
42 {
44 
45 protected:
46  enum class SearchDirection : unsigned char
47  {
48  First,
49  Next,
50  Prev
51  };
52 
53  public:
54  explicit SearchableViewInterface(QAbstractItemView* view);
55  virtual ~SearchableViewInterface();
56 
57  virtual void set_search_model(SearchableModelInterface* model) final;
58 
59  virtual QModelIndex model_index(int row, int col, const QModelIndex& parent=QModelIndex()) const override final;
60  virtual int row_count(const QModelIndex& parent=QModelIndex()) const override final;
61  virtual int column_count(const QModelIndex& parent=QModelIndex()) const override final;
62 
63  virtual QItemSelectionModel* selection_model() const override final;
64  virtual void set_current_index(int idx) override final;
65 
66  bool is_minisearcher_active() const;
67  void set_mini_searcher_padding(int padding);
68 
69 protected:
70  virtual void select_match(const QString& str, SearchDirection direction);
71  virtual QModelIndex match_index(const QString& str, SearchDirection direction) const;
72  void handle_key_press(QKeyEvent* e) override;
73 
74 };
75 
76 template<typename View>
78  public View,
80 {
81 public:
82  SearchableView(QWidget* parent=nullptr) :
83  View(parent),
85 
86  virtual ~SearchableView() {}
87 
88 protected:
89  void keyPressEvent(QKeyEvent* e) override
90  {
91  if(!e->isAccepted()){
92  handle_key_press(e);
93  if(e->isAccepted()){
94  return;
95  }
96  }
97 
98  View::keyPressEvent(e);
99  }
100 };
101 
105 
106 #endif // SEARCHABLEVIEW_H
Definition: SearchableView.h:77
The SearchViewInterface class.
Definition: SearchableView.h:40
The SayonaraSelectionView class.
Definition: SelectionView.h:42
Definition: SearchableModel.h:34