1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef LIBRARYFILTERWIDGET_H
19 #define LIBRARYFILTERWIDGET_H
20 
21 #include <memory>
22 
23 #include <QMap>
24 #include <QWidget>
25 
26 #include "librarymodel.h"
27 #include "savedgroupingmanager.h"
28 
29 class GroupByDialog;
30 class SettingsDialog;
31 class Ui_LibraryFilterWidget;
32 
33 struct QueryOptions;
34 
35 class QMenu;
36 class QActionGroup;
37 
38 class LibraryFilterWidget : public QWidget {
39   Q_OBJECT
40 
41  public:
42   LibraryFilterWidget(QWidget* parent = nullptr);
43   ~LibraryFilterWidget();
44 
45   static const int kFilterDelay = 500;  // msec
46 
47   enum DelayBehaviour {
48     AlwaysInstant,
49     DelayedOnLargeLibraries,
50     AlwaysDelayed,
51   };
52 
53   static QActionGroup* CreateGroupByActions(QObject* parent);
54 
55   void UpdateGroupByActions();
56   void SetFilterHint(const QString& hint);
SetApplyFilterToLibrary(bool filter_applies_to_model)57   void SetApplyFilterToLibrary(bool filter_applies_to_model) {
58     filter_applies_to_model_ = filter_applies_to_model;
59   }
SetDelayBehaviour(DelayBehaviour behaviour)60   void SetDelayBehaviour(DelayBehaviour behaviour) {
61     delay_behaviour_ = behaviour;
62   }
63   void SetAgeFilterEnabled(bool enabled);
64   void SetGroupByEnabled(bool enabled);
65   void ShowInLibrary(const QString& search);
66 
menu()67   QMenu* menu() const { return library_menu_; }
68   void AddMenuAction(QAction* action);
69 
SetSettingsGroup(const QString & group)70   void SetSettingsGroup(const QString& group) { settings_group_ = group; }
71   void SetLibraryModel(LibraryModel* model);
72 
73  public slots:
74   void SetQueryMode(QueryOptions::QueryMode view);
75   void FocusOnFilter(QKeyEvent* e);
76 
77 signals:
78   void UpPressed();
79   void DownPressed();
80   void ReturnPressed();
81   void Filter(const QString& text);
82 
83  protected:
84   void keyReleaseEvent(QKeyEvent* e);
85 
86  private slots:
87   void GroupingChanged(const LibraryModel::Grouping& g);
88   void GroupByClicked(QAction* action);
89   void SaveGroupBy();
90   void ShowGroupingManager();
91 
92   void FilterTextChanged(const QString& text);
93   void FilterDelayTimeout();
94 
95  private:
96   static QAction* CreateGroupByAction(const QString& text, QObject* parent,
97                                       const LibraryModel::Grouping& grouping);
98   void CheckCurrentGrouping(const LibraryModel::Grouping& g);
99 
100  private:
101   Ui_LibraryFilterWidget* ui_;
102   LibraryModel* model_;
103 
104   std::unique_ptr<GroupByDialog> group_by_dialog_;
105   std::unique_ptr<SavedGroupingManager> groupings_manager_;
106   SettingsDialog* settings_dialog_;
107 
108   QMenu* filter_age_menu_;
109   QMenu* group_by_menu_;
110   QMenu* library_menu_;
111   QActionGroup* group_by_group_;
112   QMap<QAction*, int> filter_ages_;
113 
114   QTimer* filter_delay_;
115 
116   bool filter_applies_to_model_;
117   DelayBehaviour delay_behaviour_;
118 
119   QString settings_group_;
120 };
121 
122 #endif  // LIBRARYFILTERWIDGET_H
123