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