1 /* This file is part of Clementine.
2    Copyright 2012, 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 GLOBALSEARCHVIEW_H
19 #define GLOBALSEARCHVIEW_H
20 
21 #include "searchprovider.h"
22 #include "library/librarymodel.h"
23 #include "ui/settingsdialog.h"
24 #include "playlist/playlistmanager.h"
25 
26 #include <QWidget>
27 
28 class Application;
29 class GlobalSearchModel;
30 class GroupByDialog;
31 class SearchProviderStatusWidget;
32 class SuggestionWidget;
33 class Ui_GlobalSearchView;
34 
35 class QActionGroup;
36 class QMimeData;
37 class QSortFilterProxyModel;
38 class QStandardItem;
39 class QStandardItemModel;
40 
41 class GlobalSearchView : public QWidget {
42   Q_OBJECT
43 
44  public:
45   GlobalSearchView(Application* app, QWidget* parent = nullptr);
46   ~GlobalSearchView();
47 
48   static const int kSwapModelsTimeoutMsec;
49   static const int kMaxSuggestions;
50   static const int kUpdateSuggestionsTimeoutMsec;
51 
52   // Called by the delegate
53   void LazyLoadArt(const QModelIndex& index);
54 
55   // QWidget
56   void showEvent(QShowEvent* e);
57   void hideEvent(QHideEvent* e);
58 
59   // QObject
60   bool eventFilter(QObject* object, QEvent* event);
61 
62  public slots:
63   void ReloadSettings();
64   void StartSearch(const QString& query);
65   void FocusSearchField();
66   void OpenSettingsDialog();
67 
68 signals:
69   void AddToPlaylist(QMimeData* data);
70 
71  private slots:
72   void UpdateSuggestions();
73 
74   void SwapModels();
75   void TextEdited(const QString& text);
76   void AddResults(int id, const SearchProvider::ResultList& results);
77   void ArtLoaded(int id, const QPixmap& pixmap);
78 
79   void FocusOnFilter(QKeyEvent* event);
80 
81   void AddSelectedToPlaylist();
82   void LoadSelected();
83   void OpenSelectedInNewPlaylist();
84   void AddSelectedToPlaylistEnqueue();
85 
86   void SearchForThis();
87 
88   void GroupByClicked(QAction* action);
89   void SetGroupBy(const LibraryModel::Grouping& grouping);
90 
91  private:
92   MimeData* SelectedMimeData();
93 
94   bool SearchKeyEvent(QKeyEvent* event);
95   bool ResultsContextMenuEvent(QContextMenuEvent* event);
96 
97  private:
98   Application* app_;
99   GlobalSearch* engine_;
100   Ui_GlobalSearchView* ui_;
101   QScopedPointer<GroupByDialog> group_by_dialog_;
102 
103   QMenu* context_menu_;
104   QList<QAction*> context_actions_;
105   QActionGroup* group_by_actions_;
106 
107   int last_search_id_;
108 
109   // Like graphics APIs have a front buffer and a back buffer, there's a front
110   // model and a back model - the front model is the one that's shown in the
111   // UI and the back model is the one that lies in wait.  current_model_ will
112   // point to either the front or the back model.
113   GlobalSearchModel* front_model_;
114   GlobalSearchModel* back_model_;
115   GlobalSearchModel* current_model_;
116 
117   QSortFilterProxyModel* front_proxy_;
118   QSortFilterProxyModel* back_proxy_;
119   QSortFilterProxyModel* current_proxy_;
120 
121   QMap<int, QModelIndex> art_requests_;
122 
123   QTimer* swap_models_timer_;
124   QTimer* update_suggestions_timer_;
125 
126   QList<SearchProviderStatusWidget*> provider_status_widgets_;
127   QList<SuggestionWidget*> suggestion_widgets_;
128 
129   QIcon search_icon_;
130   QIcon warning_icon_;
131 
132   bool show_providers_;
133   bool show_suggestions_;
134 };
135 
136 #endif  // GLOBALSEARCHVIEW_H
137