strawberry-audio-player-win.../src/internet/internetsearchview.h

226 lines
6.3 KiB
C
Raw Normal View History

2018-08-09 18:10:03 +02:00
/*
* Strawberry Music Player
* This code was part of Clementine (GlobalSearch)
* Copyright 2012, David Sansome <me@davidsansome.com>
2020-04-13 06:30:40 +02:00
* Copyright 2018-2020, Jonas Kvinge <jonas@jkvinge.net>
2018-08-09 18:10:03 +02:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef INTERNETSEARCHVIEW_H
#define INTERNETSEARCHVIEW_H
2018-08-09 18:10:03 +02:00
#include "config.h"
#include <memory>
2020-04-13 06:30:40 +02:00
#include <QtGlobal>
2020-02-09 02:29:35 +01:00
#include <QObject>
2018-08-09 18:10:03 +02:00
#include <QWidget>
2020-04-13 06:30:40 +02:00
#include <QSet>
#include <QPair>
2018-08-09 18:10:03 +02:00
#include <QList>
2020-04-13 06:30:40 +02:00
#include <QMap>
2018-08-09 18:10:03 +02:00
#include <QString>
2020-04-13 06:30:40 +02:00
#include <QStringList>
#include <QUrl>
#include <QImage>
2018-08-09 18:10:03 +02:00
#include <QPixmap>
2020-04-13 06:30:40 +02:00
#include <QPixmapCache>
#include <QMetaType>
2018-08-09 18:10:03 +02:00
2020-02-09 02:29:35 +01:00
#include "core/song.h"
2018-08-09 18:10:03 +02:00
#include "collection/collectionmodel.h"
2020-04-13 06:30:40 +02:00
#include "covermanager/albumcoverloaderoptions.h"
#include "covermanager/albumcoverloaderresult.h"
2018-08-09 18:10:03 +02:00
#include "settings/settingsdialog.h"
class QSortFilterProxyModel;
class QMimeData;
class QTimer;
class QMenu;
class QAction;
class QActionGroup;
class QEvent;
class QKeyEvent;
class QShowEvent;
class QContextMenuEvent;
2020-04-13 06:30:40 +02:00
class QTimerEvent;
2018-08-09 18:10:03 +02:00
class Application;
2020-02-09 02:29:35 +01:00
class MimeData;
2018-08-09 18:10:03 +02:00
class GroupByDialog;
2020-04-13 06:30:40 +02:00
class InternetService;
class InternetSearchModel;
class Ui_InternetSearchView;
2018-08-09 18:10:03 +02:00
class InternetSearchView : public QWidget {
2018-08-09 18:10:03 +02:00
Q_OBJECT
public:
2020-04-07 16:49:15 +02:00
explicit InternetSearchView(QWidget *parent = nullptr);
2020-06-15 21:55:05 +02:00
~InternetSearchView() override;
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
enum SearchType {
SearchType_Artists = 1,
SearchType_Albums = 2,
SearchType_Songs = 3,
};
struct Result {
Song metadata_;
QString pixmap_cache_key_;
};
typedef QList<Result> ResultList;
2020-04-13 06:30:40 +02:00
void Init(Application *app, InternetService *service);
2018-08-09 18:10:03 +02:00
void LazyLoadAlbumCover(const QModelIndex &index);
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
protected:
struct PendingState {
PendingState() : orig_id_(-1) {}
PendingState(int orig_id, QStringList tokens) : orig_id_(orig_id), tokens_(tokens) {}
int orig_id_;
QStringList tokens_;
bool operator<(const PendingState &b) const {
return orig_id_ < b.orig_id_;
}
bool operator==(const PendingState &b) const {
return orig_id_ == b.orig_id_;
}
};
2020-06-15 21:55:05 +02:00
void showEvent(QShowEvent *e) override;
bool eventFilter(QObject *object, QEvent *e) override;
void timerEvent(QTimerEvent *e) override;
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
// These functions treat queries in the same way as CollectionQuery.
// They're useful for figuring out whether you got a result because it matched in the song title or the artist/album name.
static QStringList TokenizeQuery(const QString &query);
static bool Matches(const QStringList &tokens, const QString &string);
private:
struct DelayedSearch {
int id_;
QString query_;
SearchType type_;
};
bool SearchKeyEvent(QKeyEvent *e);
bool ResultsContextMenuEvent(QContextMenuEvent *e);
2018-08-09 18:10:03 +02:00
void FocusSearchField();
2020-04-13 06:30:40 +02:00
MimeData *SelectedMimeData();
void SetSearchType(const SearchType type);
int SearchAsync(const QString &query, SearchType type);
void SearchAsync(const int id, const QString &query, const SearchType type);
void SearchError(const int id, const QString &error);
void CancelSearch(const int id);
QString PixmapCacheKey(const Result &result) const;
bool FindCachedPixmap(const Result &result, QPixmap *pixmap) const;
int LoadAlbumCoverAsync(const Result &result);
2018-08-09 18:10:03 +02:00
signals:
2020-04-13 06:30:40 +02:00
void AddToPlaylist(QMimeData*);
void AddArtistsSignal(SongList);
void AddAlbumsSignal(SongList);
void AddSongsSignal(SongList);
2018-08-09 18:10:03 +02:00
private slots:
void SwapModels();
void TextEdited(const QString &text);
2020-04-13 06:30:40 +02:00
void StartSearch(const QString &query);
void SearchDone(const int service_id, const SongList &songs, const QString &error);
2020-06-14 18:58:24 +02:00
void UpdateStatus(const int service_id, const QString &text);
void ProgressSetMaximum(const int service_id, const int max);
void UpdateProgress(const int service_id, const int progress);
void AddResults(const int service_id, const ResultList &results);
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
void FocusOnFilter(QKeyEvent *e);
2018-08-09 18:10:03 +02:00
void AddSelectedToPlaylist();
void LoadSelected();
void OpenSelectedInNewPlaylist();
void AddSelectedToPlaylistEnqueue();
2020-04-13 06:30:40 +02:00
void AddArtists();
void AddAlbums();
void AddSongs();
2018-08-09 18:10:03 +02:00
void SearchForThis();
2020-04-13 06:30:40 +02:00
void OpenSettingsDialog();
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
void SearchArtistsClicked(const bool);
void SearchAlbumsClicked(const bool);
void SearchSongsClicked(const bool);
2018-08-09 18:10:03 +02:00
void GroupByClicked(QAction *action);
void SetGroupBy(const CollectionModel::Grouping &g);
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &albumcover_result);
2020-04-13 06:30:40 +02:00
public slots:
void ReloadSettings();
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
private:
static const int kSwapModelsTimeoutMsec;
static const int kDelayedSearchTimeoutMs;
static const int kArtHeight;
2018-08-09 18:10:03 +02:00
2020-04-13 06:30:40 +02:00
private:
2018-08-09 18:10:03 +02:00
Application *app_;
2020-04-13 06:30:40 +02:00
InternetService *service_;
Ui_InternetSearchView *ui_;
std::unique_ptr<GroupByDialog> group_by_dialog_;
2018-08-09 18:10:03 +02:00
QMenu *context_menu_;
QList<QAction*> context_actions_;
QActionGroup *group_by_actions_;
// Like graphics APIs have a front buffer and a back buffer, there's a front model and a back model
// The front model is the one that's shown in the UI and the back model is the one that lies in wait.
// current_model_ will point to either the front or the back model.
InternetSearchModel *front_model_;
InternetSearchModel *back_model_;
InternetSearchModel *current_model_;
2018-08-09 18:10:03 +02:00
QSortFilterProxyModel *front_proxy_;
QSortFilterProxyModel *back_proxy_;
QSortFilterProxyModel *current_proxy_;
QTimer *swap_models_timer_;
bool use_pretty_covers_;
2020-04-13 06:30:40 +02:00
SearchType search_type_;
bool search_error_;
int last_search_id_;
int searches_next_id_;
QMap<int, DelayedSearch> delayed_searches_;
QMap<int, PendingState> pending_searches_;
2020-04-13 06:30:40 +02:00
AlbumCoverLoaderOptions cover_loader_options_;
QMap<quint64, QPair<QModelIndex, QString>> cover_loader_tasks_;
2020-04-13 06:30:40 +02:00
QPixmapCache pixmap_cache_;
2018-08-09 18:10:03 +02:00
};
2020-04-13 06:30:40 +02:00
Q_DECLARE_METATYPE(InternetSearchView::Result)
Q_DECLARE_METATYPE(InternetSearchView::ResultList)
2018-08-09 18:10:03 +02:00
#endif // INTERNETSEARCHVIEW_H