Sayonara Player
SearchableView.h
1/* SearchableView.h */
2
3/* Copyright (C) 2011-2020 Michael Lugmair (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
32class QAbstractItemView;
33class QItemSelectionModel;
34class ExtraTriggerMap;
36
38 public QObject
39{
40 Q_OBJECT
42
43 public:
44 explicit MiniSearcherViewConnector(SearchableViewInterface* searchableView, QObject* parent);
46
47 void init();
48 [[nodiscard]] bool isActive() const;
49 void setExtraTriggers(const QMap<QChar, QString>& map);
50 bool handleKeyPress(QKeyEvent* e);
51
52 private slots:
53 void lineEditChanged(const QString& str);
54 void selectNext();
55 void selectPrevious();
56};
57
60{
62
63 protected:
64 enum class SearchDirection :
65 unsigned char
66 {
67 First,
68 Next,
69 Prev
70 };
71
72 public:
73 explicit SearchableViewInterface(QAbstractItemView* view);
74 ~SearchableViewInterface() override;
75
76 [[nodiscard]] QAbstractItemView* view() const;
77 [[nodiscard]] virtual int viewportHeight() const;
78 [[nodiscard]] virtual int viewportWidth() const;
79
80 int setSearchstring(const QString& str);
81 void selectNextMatch(const QString& str);
82 void selectPreviousMatch(const QString& str);
83
84 virtual void searchDone();
85
86 protected:
87 virtual void setSearchModel(SearchableModelInterface* model);
88 [[nodiscard]] virtual QModelIndex matchIndex(const QString& str, SearchDirection direction) const;
89 virtual void selectMatch(const QString& str, SearchDirection direction);
90 bool handleKeyPress(QKeyEvent* e) override;
91};
92
93template<typename View, typename Model>
95 public View,
97{
98 private:
99 using View::setModel;
100 using SearchableViewInterface::setSearchModel;
101
102 public:
103 explicit SearchableView(QWidget* parent = nullptr) :
104 View(parent),
106
107 ~SearchableView() override = default;
108
109 virtual void setSearchableModel(Model* model)
110 {
111 View::setModel(model);
112 SearchableViewInterface::setSearchModel(model);
113 }
114
115 [[nodiscard]] int rowCount() const
116 {
117 return (View::model() == nullptr)
118 ? 0
119 : View::model()->rowCount();
120 }
121
122 protected:
123 void keyPressEvent(QKeyEvent* e) override
124 {
125 const auto processed = handleKeyPress(e);
126 if(!processed)
127 {
128 View::keyPressEvent(e);
129 }
130 }
131};
132
134
135#endif // SEARCHABLEVIEW_H
Template for Sayonara Widgets. This template is responsible for holding a reference to the settings.
Definition: WidgetTemplate.h:87
Definition: SearchableView.h:39
Definition: org_mpris_media_player2_adaptor.h:21
Definition: SearchableModel.h:32
Definition: SearchableView.h:60
Definition: SearchableView.h:97
Definition: SelectionView.h:35