~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/globalsearchwidget.h

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 GLOBALSEARCHWIDGET_H
 
19
#define GLOBALSEARCHWIDGET_H
 
20
 
 
21
#include "searchprovider.h"
 
22
#include "ui/settingsdialog.h"
 
23
 
 
24
#include <QScopedPointer>
 
25
#include <QWidget>
 
26
 
 
27
#include <boost/bimap.hpp>
 
28
 
 
29
class GlobalSearch;
 
30
class GlobalSearchTooltip;
 
31
class LibraryBackendInterface;
 
32
class Ui_GlobalSearchWidget;
 
33
 
 
34
class QDesktopWidget;
 
35
class QListView;
 
36
class QMimeData;
 
37
class QModelIndex;
 
38
class QSortFilterProxyModel;
 
39
class QStandardItemModel;
 
40
class QToolButton;
 
41
 
 
42
 
 
43
class GlobalSearchWidget : public QWidget {
 
44
  Q_OBJECT
 
45
 
 
46
public:
 
47
  GlobalSearchWidget(QWidget* parent = 0);
 
48
  ~GlobalSearchWidget();
 
49
 
 
50
  static const int kMinVisibleItems;
 
51
  static const int kMaxVisibleItems;
 
52
  static const int kSwapModelsTimeoutMsec;
 
53
  static const int kSuggestionTimeoutMsec;
 
54
  static const int kSuggestionCount;
 
55
 
 
56
  enum Role {
 
57
    Role_PrimaryResult = Qt::UserRole + 1,
 
58
    Role_AllResults,
 
59
    Role_LazyLoadingArt,
 
60
    Role_OrderArrived
 
61
  };
 
62
 
 
63
  void Init(GlobalSearch* engine_);
 
64
 
 
65
  // Called by the delegate
 
66
  void LazyLoadArt(const QModelIndex& index);
 
67
 
 
68
  // QObject
 
69
  bool eventFilter(QObject* o, QEvent* e);
 
70
 
 
71
public slots:
 
72
  void ReloadSettings();
 
73
  void StartSearch(const QString& query);
 
74
 
 
75
signals:
 
76
  void AddToPlaylist(QMimeData* data);
 
77
  void OpenSettingsAtPage(SettingsDialog::Page page);
 
78
 
 
79
protected:
 
80
  void resizeEvent(QResizeEvent* e);
 
81
  void paintEvent(QPaintEvent* e);
 
82
  void showEvent(QShowEvent* e);
 
83
  void hideEvent(QHideEvent* e);
 
84
 
 
85
private slots:
 
86
  void TextEdited(const QString& text);
 
87
  void AddResults(int id, const SearchProvider::ResultList& results);
 
88
 
 
89
  void ArtLoaded(int id, const QPixmap& pixmap);
 
90
 
 
91
  void TracksLoaded(int id, MimeData* mime_data);
 
92
 
 
93
  void ResultDoubleClicked();
 
94
  void AddCurrent();
 
95
  void AddAndPlayCurrent();
 
96
  void AddAndQueueCurrent();
 
97
  void ReplaceCurrent();
 
98
  void ReplaceAndPlayCurrent();
 
99
  void SettingsClicked();
 
100
 
 
101
  void HidePopup(bool manual);
 
102
  void UpdateTooltip();
 
103
  void UpdateTooltipPosition();
 
104
 
 
105
  void SwapModels();
 
106
 
 
107
  void NextSuggestion();
 
108
 
 
109
private:
 
110
  // Return values from CanCombineResults
 
111
  enum CombineAction {
 
112
    CannotCombine,  // The two results are different and can't be combined
 
113
    LeftPreferred,  // The two results can be combined - the left one is better
 
114
    RightPreferred  // The two results can be combined - the right one is better
 
115
  };
 
116
 
 
117
  class CombineCache {
 
118
  public:
 
119
    CombineCache(QAbstractItemModel* model);
 
120
 
 
121
    QModelIndexList FindCandidates(const QModelIndex& result) const;
 
122
    void Insert(const QModelIndex& index);
 
123
    void Remove(const QModelIndex& index);
 
124
    void Clear();
 
125
 
 
126
    static uint Hash(const QModelIndex& index);
 
127
 
 
128
  private:
 
129
    QAbstractItemModel* model_;
 
130
    QMultiMap<uint, int> data_;
 
131
  };
 
132
 
 
133
  void RepositionPopup();
 
134
  CombineAction CanCombineResults(const QModelIndex& left, const QModelIndex& right) const;
 
135
  void CombineResults(const QModelIndex& superior, const QModelIndex& inferior);
 
136
 
 
137
  bool EventFilterSearchWidget(QObject* o, QEvent* e);
 
138
  bool EventFilterPopup(QObject* o, QEvent* e);
 
139
 
 
140
  void LoadTracks(QAction* trigger);
 
141
 
 
142
private:
 
143
  Ui_GlobalSearchWidget* ui_;
 
144
 
 
145
  GlobalSearch* engine_;
 
146
  int last_id_;
 
147
  int order_arrived_counter_;
 
148
  bool closed_since_search_began_;
 
149
 
 
150
  QMap<int, QModelIndex> art_requests_;
 
151
  QMap<int, QAction*> track_requests_;
 
152
 
 
153
  // Like graphics APIs have a front buffer and a back buffer, there's a front
 
154
  // model and a back model - the front model is the one that's shown in the
 
155
  // UI and the back model is the one that lies in wait.  current_model_ will
 
156
  // point to either the front or the back model.
 
157
  QStandardItemModel* front_model_;
 
158
  QStandardItemModel* back_model_;
 
159
  QStandardItemModel* current_model_;
 
160
 
 
161
  QMap<QStandardItemModel*, CombineCache*> combine_cache_;
 
162
 
 
163
  QSortFilterProxyModel* front_proxy_;
 
164
  QSortFilterProxyModel* back_proxy_;
 
165
  QSortFilterProxyModel* current_proxy_;
 
166
 
 
167
  QListView* view_;
 
168
  bool consume_focus_out_;
 
169
 
 
170
  QTimer* swap_models_timer_;
 
171
 
 
172
  QPixmap background_;
 
173
  QPixmap background_scaled_;
 
174
 
 
175
  QDesktopWidget* desktop_;
 
176
 
 
177
  bool show_tooltip_;
 
178
  bool combine_identical_results_;
 
179
  QStringList provider_order_;
 
180
 
 
181
  QScopedPointer<GlobalSearchTooltip> tooltip_;
 
182
 
 
183
  QAction* add_;
 
184
  QAction* add_and_play_;
 
185
  QAction* add_and_queue_;
 
186
  QAction* replace_;
 
187
  QAction* replace_and_play_;
 
188
  QList<QAction*> actions_;
 
189
 
 
190
  QString hint_text_;
 
191
  QTimer* next_suggestion_timer_;
 
192
};
 
193
 
 
194
#endif // GLOBALSEARCHWIDGET_H