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 class ExtraTriggerMap;
38 
39 class MiniSearcherViewConnector : public QObject
40 {
41  Q_OBJECT
43 
44 public:
47 
48  void init();
49  bool is_active() const;
50  void set_extra_triggers(const QMap<QChar, QString>& map);
51  void handle_key_press(QKeyEvent* e);
52 
53 private slots:
54  void edit_changed(const QString& str);
55  void select_next();
56  void select_previous();
57 };
58 
59 
66 {
68 
69 protected:
70  enum class SearchDirection : unsigned char
71  {
72  First,
73  Next,
74  Prev
75  };
76 
77  public:
78  explicit SearchableViewInterface(QAbstractItemView* view);
79  virtual ~SearchableViewInterface();
80 
81  virtual void set_search_model(SearchableModelInterface* model);
82 
83  virtual QModelIndex model_index(int row, int col, const QModelIndex& parent=QModelIndex()) const override final;
84  virtual int row_count(const QModelIndex& parent=QModelIndex()) const override final;
85  virtual int column_count(const QModelIndex& parent=QModelIndex()) const override final;
86  bool is_empty(const QModelIndex& parent=QModelIndex()) const;
87  bool has_rows(const QModelIndex& parent=QModelIndex()) const;
88 
89  virtual QItemSelectionModel* selection_model() const override final;
90  virtual void set_current_index(int idx) override final;
91 
92  bool is_minisearcher_active() const;
93  virtual int viewport_height() const;
94  virtual int viewport_width() const;
95 
96  QAbstractItemView* view() const;
97 
98  int set_searchstring(const QString& str);
99  void select_next_match(const QString& str);
100  void select_previous_match(const QString& str);
101 
102  protected:
103  virtual QModelIndex match_index(const QString& str, SearchDirection direction) const;
104  virtual void select_match(const QString& str, SearchDirection direction);
105  void handle_key_press(QKeyEvent* e) override;
106 };
107 
108 
109 template<typename View, typename Model>
111  public View,
113 {
114  private:
115  using View::setModel;
116  using SearchableViewInterface::set_search_model;
117 
118  public:
119  SearchableView(QWidget* parent=nullptr) :
120  View(parent),
121  SearchableViewInterface(this) {}
122 
123  virtual ~SearchableView() {}
124 
125  virtual void set_model(Model* model)
126  {
127  setModel(model);
128  set_search_model(model);
129  }
130 
131  protected:
132  void keyPressEvent(QKeyEvent* e) override
133  {
134  if(!e->isAccepted())
135  {
136  handle_key_press(e);
137  if(e->isAccepted()){
138  return;
139  }
140  }
141 
142  View::keyPressEvent(e);
143  }
144 };
145 
148 
149 
150 #endif // SEARCHABLEVIEW_H
Definition: SearchableView.h:110
Template for Sayonara Widgets. This template is responsible for holding a reference to the settings...
Definition: WidgetTemplate.h:78
The SearchViewInterface class.
Definition: SearchableView.h:64
The SayonaraSelectionView class.
Definition: SelectionView.h:37
Definition: SearchableModel.h:34
Definition: SearchableView.h:39
Definition: org_mpris_media_player2_adaptor.h:21