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/Widgets/WidgetTemplate.h"
25 #include "GUI/Utils/SearchableWidget/SelectionView.h"
26 #include "GUI/Utils/SearchableWidget/SearchableModel.h"
27 #include "Utils/Pimpl.h"
28 
29 #include <QKeyEvent>
30 #include <QTableView>
31 #include <QListView>
32 #include <QTreeView>
33 
34 class QAbstractItemView;
35 class QItemSelectionModel;
36 
43 {
45 
46 protected:
47  enum class SearchDirection : unsigned char
48  {
49  First,
50  Next,
51  Prev
52  };
53 
54  public:
55  explicit SearchableViewInterface(QAbstractItemView* view);
56  virtual ~SearchableViewInterface();
57 
58  virtual void set_search_model(SearchableModelInterface* model);
59 
60  virtual QModelIndex model_index(int row, int col, const QModelIndex& parent=QModelIndex()) const override final;
61  virtual int row_count(const QModelIndex& parent=QModelIndex()) const override final;
62  virtual int column_count(const QModelIndex& parent=QModelIndex()) const override final;
63  bool is_empty(const QModelIndex& parent=QModelIndex()) const;
64  bool has_rows(const QModelIndex& parent=QModelIndex()) const;
65 
66  virtual QItemSelectionModel* selection_model() const override final;
67  virtual void set_current_index(int idx) override final;
68 
69  bool is_minisearcher_active() const;
70  void set_mini_searcher_padding(int padding);
71 
72  protected:
73  virtual void select_match(const QString& str, SearchDirection direction);
74  virtual QModelIndex match_index(const QString& str, SearchDirection direction) const;
75  void handle_key_press(QKeyEvent* e) override;
76 };
77 
78 
79 template<typename View, typename Model>
81  public View,
83 {
84  private:
85  using View::setModel;
86  using SearchableViewInterface::set_search_model;
87 
88  public:
89  SearchableView(QWidget* parent=nullptr) :
90  View(parent),
92 
93  virtual ~SearchableView() {}
94 
95  virtual void set_model(Model* model)
96  {
97  setModel(model);
98  set_search_model(model);
99  }
100 
101  protected:
102  void keyPressEvent(QKeyEvent* e) override
103  {
104  if(!e->isAccepted())
105  {
106  handle_key_press(e);
107  if(e->isAccepted()){
108  return;
109  }
110  }
111 
112  View::keyPressEvent(e);
113  }
114 };
115 
118 
119 
120 #endif // SEARCHABLEVIEW_H
Definition: SearchableView.h:80
Template for Sayonara Widgets. This template is responsible for holding a reference to the settings...
Definition: WidgetTemplate.h:47
The SearchViewInterface class.
Definition: SearchableView.h:41
The SayonaraSelectionView class.
Definition: SelectionView.h:47
Definition: SearchableModel.h:34