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